GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_bio.cc Lines: 238 276 86.2 %
Date: 2022-06-06 04:15:48 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
36761
int NodeBIO::Read(BIO* bio, char* out, int len) {
81
36761
  BIO_clear_retry_flags(bio);
82
83
36761
  NodeBIO* nbio = FromBIO(bio);
84
36761
  int bytes = nbio->Read(out, len);
85
86
36761
  if (bytes == 0) {
87
7663
    bytes = nbio->eof_return();
88
7663
    if (bytes != 0) {
89
7661
      BIO_set_retry_read(bio);
90
    }
91
  }
92
93
36761
  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
7728
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
104
7728
  Buffer* pos = read_head_;
105
7728
  size_t max = *count;
106
7728
  size_t total = 0;
107
108
  size_t i;
109
7864
  for (i = 0; i < max; i++) {
110
7864
    size[i] = pos->write_pos_ - pos->read_pos_;
111
7864
    total += size[i];
112
7864
    out[i] = pos->data_ + pos->read_pos_;
113
114
    /* Don't get past write head */
115
7864
    if (pos == write_head_)
116
7728
      break;
117
    else
118
136
      pos = pos->next_;
119
  }
120
121
7728
  if (i == max)
122
    *count = i;
123
  else
124
7728
    *count = i + 1;
125
126
7728
  return total;
127
}
128
129
130
48945
int NodeBIO::Write(BIO* bio, const char* data, int len) {
131
48945
  BIO_clear_retry_flags(bio);
132
133
48945
  FromBIO(bio)->Write(data, len);
134
135
48945
  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
73396
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
73396
  nbio = FromBIO(bio);
175
73396
  ret = 1;
176
177



73396
  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
17286
    case BIO_CTRL_PENDING:
209
17286
      ret = nbio->Length();
210
17286
      break;
211
6121
    case BIO_CTRL_DUP:
212
    case BIO_CTRL_FLUSH:
213
6121
      ret = 1;
214
6121
      break;
215
11901
    case BIO_CTRL_PUSH:
216
    case BIO_CTRL_POP:
217
    default:
218
11901
      ret = 0;
219
11901
      break;
220
  }
221
73396
  return ret;
222
}
223
224
225
68044
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
68044
  if (method == nullptr) {
231
5216
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
232
5216
    BIO_meth_set_write(method, Write);
233
5216
    BIO_meth_set_read(method, Read);
234
5216
    BIO_meth_set_puts(method, Puts);
235
5216
    BIO_meth_set_gets(method, Gets);
236
5216
    BIO_meth_set_ctrl(method, Ctrl);
237
5216
    BIO_meth_set_create(method, New);
238
5216
    BIO_meth_set_destroy(method, Free);
239
  }
240
241
68044
  return method;
242
}
243
244
245
955134
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
955134
  while (read_head_->read_pos_ != 0 &&
251
902839
         read_head_->read_pos_ == read_head_->write_pos_) {
252
    // Reset positions
253
51423
    read_head_->read_pos_ = 0;
254
51423
    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
51423
    if (read_head_ != write_head_)
259
346
      read_head_ = read_head_->next_;
260
  }
261
903711
}
262
263
264
910375
size_t NodeBIO::Read(char* out, size_t size) {
265
910375
  size_t bytes_read = 0;
266
910375
  size_t expected = Length() > size ? size : Length();
267
910375
  size_t offset = 0;
268
910375
  size_t left = size;
269
270
1813214
  while (bytes_read < expected) {
271
902839
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
272
902839
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
273
902839
    if (avail > left)
274
851416
      avail = left;
275
276
    // Copy data
277
902839
    if (out != nullptr)
278
895168
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
279
902839
    read_head_->read_pos_ += avail;
280
281
    // Move pointers
282
902839
    bytes_read += avail;
283
902839
    offset += avail;
284
902839
    left -= avail;
285
286
902839
    TryMoveReadHead();
287
  }
288
910375
  CHECK_EQ(expected, bytes_read);
289
910375
  length_ -= bytes_read;
290
291
  // Free all empty buffers, but write_head's child
292
910375
  FreeEmpty();
293
294
910375
  return bytes_read;
295
}
296
297
298
910375
void NodeBIO::FreeEmpty() {
299
910375
  if (write_head_ == nullptr)
300
910374
    return;
301
908620
  Buffer* child = write_head_->next_;
302

908620
  if (child == write_head_ || child == read_head_)
303
903136
    return;
304
5484
  Buffer* cur = child->next_;
305

5484
  if (cur == write_head_ || cur == read_head_)
306
5483
    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
48945
void NodeBIO::Write(const char* data, size_t size) {
362
48945
  size_t offset = 0;
363
48945
  size_t left = size;
364
365
  // Allocate initial buffer if the ring is empty
366
48945
  TryAllocateForWrite(left);
367
368
98028
  while (left > 0) {
369
49083
    size_t to_write = left;
370
49083
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
371
49083
    size_t avail = write_head_->len_ - write_head_->write_pos_;
372
373
49083
    if (to_write > avail)
374
138
      to_write = avail;
375
376
    // Copy data
377
49083
    memcpy(write_head_->data_ + write_head_->write_pos_,
378
49083
           data + offset,
379
           to_write);
380
381
    // Move pointers
382
49083
    left -= to_write;
383
49083
    offset += to_write;
384
49083
    length_ += to_write;
385
49083
    write_head_->write_pos_ += to_write;
386
49083
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
387
388
    // Go to next buffer if there still are some bytes to write
389
49083
    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
48945
  CHECK_EQ(left, 0);
400
48945
}
401
402
403
7170
char* NodeBIO::PeekWritable(size_t* size) {
404
7170
  TryAllocateForWrite(*size);
405
406
7170
  size_t available = write_head_->len_ - write_head_->write_pos_;
407

7170
  if (*size == 0 || available <= *size)
408
7127
    *size = available;
409
410
7170
  return write_head_->data_ + write_head_->write_pos_;
411
}
412
413
414
6344
void NodeBIO::Commit(size_t size) {
415
6344
  write_head_->write_pos_ += size;
416
6344
  length_ += size;
417
6344
  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
6344
  TryAllocateForWrite(0);
422
6344
  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
6344
}
430
431
432
62597
void NodeBIO::TryAllocateForWrite(size_t hint) {
433
62597
  Buffer* w = write_head_;
434
62597
  Buffer* r = read_head_;
435
  // If write head is full, next buffer is either read head or not empty.
436
62597
  if (w == nullptr ||
437
19890
      (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
62597
}
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
1157626
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
499
1157626
  CHECK_NOT_NULL(BIO_get_data(bio));
500
1157626
  return static_cast<NodeBIO*>(BIO_get_data(bio));
501
}
502
503
504
}  // namespace crypto
505
}  // namespace node