GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_bio.cc Lines: 235 276 85.1 %
Date: 2021-09-23 04:12:37 Branches: 124 176 70.5 %

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 "allocated_buffer-inl.h"
26
#include "util-inl.h"
27
28
#include <openssl/bio.h>
29
30
#include <climits>
31
#include <cstring>
32
33
namespace node {
34
namespace crypto {
35
36
63669
BIOPointer NodeBIO::New(Environment* env) {
37
63669
  BIOPointer bio(BIO_new(GetMethod()));
38

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


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

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


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



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

934952
  if (child == write_head_ || child == read_head_)
304
929475
    return;
305
5477
  Buffer* cur = child->next_;
306

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

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

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

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