GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_bio.cc Lines: 238 276 86.2 %
Date: 2022-06-14 04:15:41 Branches: 125 176 71.0 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "crypto/crypto_bio.h"
23
#include "base_object-inl.h"
24
#include "memory_tracker-inl.h"
25
#include "util-inl.h"
26
27
#include <openssl/bio.h>
28
29
#include <climits>
30
#include <cstring>
31
32
namespace node {
33
namespace crypto {
34
35
62828
BIOPointer NodeBIO::New(Environment* env) {
36
62828
  BIOPointer bio(BIO_new(GetMethod()));
37

62828
  if (bio && env != nullptr)
38
24772
    NodeBIO::FromBIO(bio.get())->env_ = env;
39
62828
  return bio;
40
}
41
42
43
38056
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
44
76112
  BIOPointer bio = New(env);
45
46
76112
  if (!bio ||
47
38056
      len > INT_MAX ||
48


114168
      BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
49
38056
      BIO_set_mem_eof_return(bio.get(), 0) != 1) {
50
    return BIOPointer();
51
  }
52
53
38056
  return bio;
54
}
55
56
57
62828
int NodeBIO::New(BIO* bio) {
58
62828
  BIO_set_data(bio, new NodeBIO());
59
62828
  BIO_set_init(bio, 1);
60
61
62828
  return 1;
62
}
63
64
65
62818
int NodeBIO::Free(BIO* bio) {
66
62818
  if (bio == nullptr)
67
    return 0;
68
69
62818
  if (BIO_get_shutdown(bio)) {
70

62818
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
71
62818
      delete FromBIO(bio);
72
62818
      BIO_set_data(bio, nullptr);
73
    }
74
  }
75
76
62818
  return 1;
77
}
78
79
80
36768
int NodeBIO::Read(BIO* bio, char* out, int len) {
81
36768
  BIO_clear_retry_flags(bio);
82
83
36768
  NodeBIO* nbio = FromBIO(bio);
84
36768
  int bytes = nbio->Read(out, len);
85
86
36768
  if (bytes == 0) {
87
7670
    bytes = nbio->eof_return();
88
7670
    if (bytes != 0) {
89
7668
      BIO_set_retry_read(bio);
90
    }
91
  }
92
93
36768
  return bytes;
94
}
95
96
97
20
char* NodeBIO::Peek(size_t* size) {
98
20
  *size = read_head_->write_pos_ - read_head_->read_pos_;
99
20
  return read_head_->data_ + read_head_->read_pos_;
100
}
101
102
103
7683
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
104
7683
  Buffer* pos = read_head_;
105
7683
  size_t max = *count;
106
7683
  size_t total = 0;
107
108
  size_t i;
109
7819
  for (i = 0; i < max; i++) {
110
7819
    size[i] = pos->write_pos_ - pos->read_pos_;
111
7819
    total += size[i];
112
7819
    out[i] = pos->data_ + pos->read_pos_;
113
114
    /* Don't get past write head */
115
7819
    if (pos == write_head_)
116
7683
      break;
117
    else
118
136
      pos = pos->next_;
119
  }
120
121
7683
  if (i == max)
122
    *count = i;
123
  else
124
7683
    *count = i + 1;
125
126
7683
  return total;
127
}
128
129
130
48716
int NodeBIO::Write(BIO* bio, const char* data, int len) {
131
48716
  BIO_clear_retry_flags(bio);
132
133
48716
  FromBIO(bio)->Write(data, len);
134
135
48716
  return len;
136
}
137
138
139
int NodeBIO::Puts(BIO* bio, const char* str) {
140
  return Write(bio, str, strlen(str));
141
}
142
143
144
867636
int NodeBIO::Gets(BIO* bio, char* out, int size) {
145
867636
  NodeBIO* nbio = FromBIO(bio);
146
147
867636
  if (nbio->Length() == 0)
148
1566
    return 0;
149
150
866070
  int i = nbio->IndexOf('\n', size);
151
152
  // Include '\n', if it's there.  If not, don't read off the end.
153


866070
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
154
830562
    i++;
155
156
  // Shift `i` a bit to nullptr-terminate string later
157
866070
  if (size == i)
158
    i--;
159
160
  // Flush read data
161
866070
  nbio->Read(out, i);
162
163
866070
  out[i] = 0;
164
165
866070
  return i;
166
}
167
168
169
73313
long NodeBIO::Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
170
                   void* ptr) {
171
  NodeBIO* nbio;
172
  long ret;  // NOLINT(runtime/int)
173
174
73313
  nbio = FromBIO(bio);
175
73313
  ret = 1;
176
177



73313
  switch (cmd) {
178
    case BIO_CTRL_RESET:
179
      nbio->Reset();
180
      break;
181
32
    case BIO_CTRL_EOF:
182
32
      ret = nbio->Length() == 0;
183
32
      break;
184
38056
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
185
38056
      nbio->set_eof_return(num);
186
38056
      break;
187
    case BIO_CTRL_INFO:
188
      ret = nbio->Length();
189
      if (ptr != nullptr)
190
        *reinterpret_cast<void**>(ptr) = nullptr;
191
      break;
192
    case BIO_C_SET_BUF_MEM:
193
      CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
194
      break;
195
    case BIO_C_GET_BUF_MEM_PTR:
196
      CHECK(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
197
      ret = 0;
198
      break;
199
    case BIO_CTRL_GET_CLOSE:
200
      ret = BIO_get_shutdown(bio);
201
      break;
202
    case BIO_CTRL_SET_CLOSE:
203
      BIO_set_shutdown(bio, num);
204
      break;
205
    case BIO_CTRL_WPENDING:
206
      ret = 0;
207
      break;
208
17202
    case BIO_CTRL_PENDING:
209
17202
      ret = nbio->Length();
210
17202
      break;
211
6122
    case BIO_CTRL_DUP:
212
    case BIO_CTRL_FLUSH:
213
6122
      ret = 1;
214
6122
      break;
215
11901
    case BIO_CTRL_PUSH:
216
    case BIO_CTRL_POP:
217
    default:
218
11901
      ret = 0;
219
11901
      break;
220
  }
221
73313
  return ret;
222
}
223
224
225
68043
const BIO_METHOD* NodeBIO::GetMethod() {
226
  // This is called from InitCryptoOnce() to avoid race conditions during
227
  // initialization.
228
  static BIO_METHOD* method = nullptr;
229
230
68043
  if (method == nullptr) {
231
5215
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
232
5215
    BIO_meth_set_write(method, Write);
233
5215
    BIO_meth_set_read(method, Read);
234
5215
    BIO_meth_set_puts(method, Puts);
235
5215
    BIO_meth_set_gets(method, Gets);
236
5215
    BIO_meth_set_ctrl(method, Ctrl);
237
5215
    BIO_meth_set_create(method, New);
238
5215
    BIO_meth_set_destroy(method, Free);
239
  }
240
241
68043
  return method;
242
}
243
244
245
955050
void NodeBIO::TryMoveReadHead() {
246
  // `read_pos_` and `write_pos_` means the position of the reader and writer
247
  // inside the buffer, respectively. When they're equal - its safe to reset
248
  // them, because both reader and writer will continue doing their stuff
249
  // from new (zero) positions.
250
955050
  while (read_head_->read_pos_ != 0 &&
251
902794
         read_head_->read_pos_ == read_head_->write_pos_) {
252
    // Reset positions
253
51384
    read_head_->read_pos_ = 0;
254
51384
    read_head_->write_pos_ = 0;
255
256
    // Move read_head_ forward, just in case if there're still some data to
257
    // read in the next buffer.
258
51384
    if (read_head_ != write_head_)
259
346
      read_head_ = read_head_->next_;
260
  }
261
903666
}
262
263
264
910337
size_t NodeBIO::Read(char* out, size_t size) {
265
910337
  size_t bytes_read = 0;
266
910337
  size_t expected = Length() > size ? size : Length();
267
910337
  size_t offset = 0;
268
910337
  size_t left = size;
269
270
1813131
  while (bytes_read < expected) {
271
902794
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
272
902794
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
273
902794
    if (avail > left)
274
851410
      avail = left;
275
276
    // Copy data
277
902794
    if (out != nullptr)
278
895168
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
279
902794
    read_head_->read_pos_ += avail;
280
281
    // Move pointers
282
902794
    bytes_read += avail;
283
902794
    offset += avail;
284
902794
    left -= avail;
285
286
902794
    TryMoveReadHead();
287
  }
288
910337
  CHECK_EQ(expected, bytes_read);
289
910337
  length_ -= bytes_read;
290
291
  // Free all empty buffers, but write_head's child
292
910337
  FreeEmpty();
293
294
910337
  return bytes_read;
295
}
296
297
298
910337
void NodeBIO::FreeEmpty() {
299
910337
  if (write_head_ == nullptr)
300
910336
    return;
301
908582
  Buffer* child = write_head_->next_;
302

908582
  if (child == write_head_ || child == read_head_)
303
903143
    return;
304
5439
  Buffer* cur = child->next_;
305

5439
  if (cur == write_head_ || cur == read_head_)
306
5438
    return;
307
308
1
  Buffer* prev = child;
309
2
  while (cur != read_head_) {
310
1
    CHECK_NE(cur, write_head_);
311
1
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
312
313
1
    Buffer* next = cur->next_;
314
1
    delete cur;
315
1
    cur = next;
316
  }
317
1
  prev->next_ = cur;
318
}
319
320
321
866070
size_t NodeBIO::IndexOf(char delim, size_t limit) {
322
866070
  size_t bytes_read = 0;
323
866070
  size_t max = Length() > limit ? limit : Length();
324
866070
  size_t left = limit;
325
866070
  Buffer* current = read_head_;
326
327
901578
  while (bytes_read < max) {
328
866070
    CHECK_LE(current->read_pos_, current->write_pos_);
329
866070
    size_t avail = current->write_pos_ - current->read_pos_;
330
866070
    if (avail > left)
331
709100
      avail = left;
332
333
    // Walk through data
334
866070
    char* tmp = current->data_ + current->read_pos_;
335
866070
    size_t off = 0;
336

57939115
    while (off < avail && *tmp != delim) {
337
57073045
      off++;
338
57073045
      tmp++;
339
    }
340
341
    // Move pointers
342
866070
    bytes_read += off;
343
866070
    left -= off;
344
345
    // Found `delim`
346
866070
    if (off != avail) {
347
830562
      return bytes_read;
348
    }
349
350
    // Move to next buffer
351
35508
    if (current->read_pos_ + avail == current->len_) {
352
28728
      current = current->next_;
353
    }
354
  }
355
35508
  CHECK_EQ(max, bytes_read);
356
357
35508
  return max;
358
}
359
360
361
48716
void NodeBIO::Write(const char* data, size_t size) {
362
48716
  size_t offset = 0;
363
48716
  size_t left = size;
364
365
  // Allocate initial buffer if the ring is empty
366
48716
  TryAllocateForWrite(left);
367
368
97570
  while (left > 0) {
369
48854
    size_t to_write = left;
370
48854
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
371
48854
    size_t avail = write_head_->len_ - write_head_->write_pos_;
372
373
48854
    if (to_write > avail)
374
138
      to_write = avail;
375
376
    // Copy data
377
48854
    memcpy(write_head_->data_ + write_head_->write_pos_,
378
48854
           data + offset,
379
           to_write);
380
381
    // Move pointers
382
48854
    left -= to_write;
383
48854
    offset += to_write;
384
48854
    length_ += to_write;
385
48854
    write_head_->write_pos_ += to_write;
386
48854
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
387
388
    // Go to next buffer if there still are some bytes to write
389
48854
    if (left != 0) {
390
138
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
391
138
      TryAllocateForWrite(left);
392
138
      write_head_ = write_head_->next_;
393
394
      // Additionally, since we're moved to the next buffer, read head
395
      // may be moved as well.
396
138
      TryMoveReadHead();
397
    }
398
  }
399
48716
  CHECK_EQ(left, 0);
400
48716
}
401
402
403
7176
char* NodeBIO::PeekWritable(size_t* size) {
404
7176
  TryAllocateForWrite(*size);
405
406
7176
  size_t available = write_head_->len_ - write_head_->write_pos_;
407

7176
  if (*size == 0 || available <= *size)
408
7133
    *size = available;
409
410
7176
  return write_head_->data_ + write_head_->write_pos_;
411
}
412
413
414
6350
void NodeBIO::Commit(size_t size) {
415
6350
  write_head_->write_pos_ += size;
416
6350
  length_ += size;
417
6350
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
418
419
  // Allocate new buffer if write head is full,
420
  // and there're no other place to go
421
6350
  TryAllocateForWrite(0);
422
6350
  if (write_head_->write_pos_ == write_head_->len_) {
423
734
    write_head_ = write_head_->next_;
424
425
    // Additionally, since we're moved to the next buffer, read head
426
    // may be moved as well.
427
734
    TryMoveReadHead();
428
  }
429
6350
}
430
431
432
62380
void NodeBIO::TryAllocateForWrite(size_t hint) {
433
62380
  Buffer* w = write_head_;
434
62380
  Buffer* r = read_head_;
435
  // If write head is full, next buffer is either read head or not empty.
436
62380
  if (w == nullptr ||
437
19673
      (w->write_pos_ == w->len_ &&
438

874
       (w->next_ == r || w->next_->write_pos_ != 0))) {
439
43375
    size_t len = w == nullptr ? initial_ :
440
                             kThroughputBufferLength;
441
43375
    if (len < hint)
442
33218
      len = hint;
443
444
    // If there is a one time allocation size hint, use it.
445
43375
    if (allocate_hint_ > len) {
446
23
      len = allocate_hint_;
447
23
      allocate_hint_ = 0;
448
    }
449
450
43375
    Buffer* next = new Buffer(env_, len);
451
452
43375
    if (w == nullptr) {
453
42707
      next->next_ = next;
454
42707
      write_head_ = next;
455
42707
      read_head_ = next;
456
    } else {
457
668
      next->next_ = w->next_;
458
668
      w->next_ = next;
459
    }
460
  }
461
62380
}
462
463
464
void NodeBIO::Reset() {
465
  if (read_head_ == nullptr)
466
    return;
467
468
  while (read_head_->read_pos_ != read_head_->write_pos_) {
469
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
470
471
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
472
    read_head_->write_pos_ = 0;
473
    read_head_->read_pos_ = 0;
474
475
    read_head_ = read_head_->next_;
476
  }
477
  write_head_ = read_head_;
478
  CHECK_EQ(length_, 0);
479
}
480
481
482
251272
NodeBIO::~NodeBIO() {
483
125636
  if (read_head_ == nullptr)
484
40242
    return;
485
486
85394
  Buffer* current = read_head_;
487
1328
  do {
488
86722
    Buffer* next = current->next_;
489
86722
    delete current;
490
86722
    current = next;
491
86722
  } while (current != read_head_);
492
493
85394
  read_head_ = nullptr;
494
85394
  write_head_ = nullptr;
495
251272
}
496
497
498
1157197
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
499
1157197
  CHECK_NOT_NULL(BIO_get_data(bio));
500
1157197
  return static_cast<NodeBIO*>(BIO_get_data(bio));
501
}
502
503
504
}  // namespace crypto
505
}  // namespace node