GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_bio.cc Lines: 238 276 86.2 %
Date: 2022-12-07 04:23:16 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
66420
BIOPointer NodeBIO::New(Environment* env) {
36
66420
  BIOPointer bio(BIO_new(GetMethod()));
37

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


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

66410
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
71
66410
      delete FromBIO(bio);
72
66410
      BIO_set_data(bio, nullptr);
73
    }
74
  }
75
76
66410
  return 1;
77
}
78
79
80
37075
int NodeBIO::Read(BIO* bio, char* out, int len) {
81
37075
  BIO_clear_retry_flags(bio);
82
83
37075
  NodeBIO* nbio = FromBIO(bio);
84
37075
  int bytes = nbio->Read(out, len);
85
86
37075
  if (bytes == 0) {
87
7813
    bytes = nbio->eof_return();
88
7813
    if (bytes != 0) {
89
7811
      BIO_set_retry_read(bio);
90
    }
91
  }
92
93
37075
  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
7403
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
104
7403
  Buffer* pos = read_head_;
105
7403
  size_t max = *count;
106
7403
  size_t total = 0;
107
108
  size_t i;
109
7535
  for (i = 0; i < max; i++) {
110
7535
    size[i] = pos->write_pos_ - pos->read_pos_;
111
7535
    total += size[i];
112
7535
    out[i] = pos->data_ + pos->read_pos_;
113
114
    /* Don't get past write head */
115
7535
    if (pos == write_head_)
116
7403
      break;
117
    else
118
132
      pos = pos->next_;
119
  }
120
121
7403
  if (i == max)
122
    *count = i;
123
  else
124
7403
    *count = i + 1;
125
126
7403
  return total;
127
}
128
129
130
51328
int NodeBIO::Write(BIO* bio, const char* data, int len) {
131
51328
  BIO_clear_retry_flags(bio);
132
133
51328
  FromBIO(bio)->Write(data, len);
134
135
51328
  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
964948
int NodeBIO::Gets(BIO* bio, char* out, int size) {
145
964948
  NodeBIO* nbio = FromBIO(bio);
146
147
964948
  if (nbio->Length() == 0)
148
1575
    return 0;
149
150
963373
  int i = nbio->IndexOf('\n', size);
151
152
  // Include '\n', if it's there.  If not, don't read off the end.
153


963373
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
154
924327
    i++;
155
156
  // Shift `i` a bit to nullptr-terminate string later
157
963373
  if (size == i)
158
    i--;
159
160
  // Flush read data
161
963373
  nbio->Read(out, i);
162
163
963373
  out[i] = 0;
164
165
963373
  return i;
166
}
167
168
169
76582
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
76582
  nbio = FromBIO(bio);
175
76582
  ret = 1;
176
177



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

1005903
  if (child == write_head_ || child == read_head_)
303
1000870
    return;
304
5033
  Buffer* cur = child->next_;
305

5033
  if (cur == write_head_ || cur == read_head_)
306
5032
    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
963373
size_t NodeBIO::IndexOf(char delim, size_t limit) {
322
963373
  size_t bytes_read = 0;
323
963373
  size_t max = Length() > limit ? limit : Length();
324
963373
  size_t left = limit;
325
963373
  Buffer* current = read_head_;
326
327
1002419
  while (bytes_read < max) {
328
963373
    CHECK_LE(current->read_pos_, current->write_pos_);
329
963373
    size_t avail = current->write_pos_ - current->read_pos_;
330
963373
    if (avail > left)
331
791699
      avail = left;
332
333
    // Walk through data
334
963373
    char* tmp = current->data_ + current->read_pos_;
335
963373
    size_t off = 0;
336

64421004
    while (off < avail && *tmp != delim) {
337
63457631
      off++;
338
63457631
      tmp++;
339
    }
340
341
    // Move pointers
342
963373
    bytes_read += off;
343
963373
    left -= off;
344
345
    // Found `delim`
346
963373
    if (off != avail) {
347
924327
      return bytes_read;
348
    }
349
350
    // Move to next buffer
351
39046
    if (current->read_pos_ + avail == current->len_) {
352
30578
      current = current->next_;
353
    }
354
  }
355
39046
  CHECK_EQ(max, bytes_read);
356
357
39046
  return max;
358
}
359
360
361
51328
void NodeBIO::Write(const char* data, size_t size) {
362
51328
  size_t offset = 0;
363
51328
  size_t left = size;
364
365
  // Allocate initial buffer if the ring is empty
366
51328
  TryAllocateForWrite(left);
367
368
102790
  while (left > 0) {
369
51462
    size_t to_write = left;
370
51462
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
371
51462
    size_t avail = write_head_->len_ - write_head_->write_pos_;
372
373
51462
    if (to_write > avail)
374
134
      to_write = avail;
375
376
    // Copy data
377
51462
    memcpy(write_head_->data_ + write_head_->write_pos_,
378
51462
           data + offset,
379
           to_write);
380
381
    // Move pointers
382
51462
    left -= to_write;
383
51462
    offset += to_write;
384
51462
    length_ += to_write;
385
51462
    write_head_->write_pos_ += to_write;
386
51462
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
387
388
    // Go to next buffer if there still are some bytes to write
389
51462
    if (left != 0) {
390
134
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
391
134
      TryAllocateForWrite(left);
392
134
      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
134
      TryMoveReadHead();
397
    }
398
  }
399
51328
  CHECK_EQ(left, 0);
400
51328
}
401
402
403
6690
char* NodeBIO::PeekWritable(size_t* size) {
404
6690
  TryAllocateForWrite(*size);
405
406
6690
  size_t available = write_head_->len_ - write_head_->write_pos_;
407

6690
  if (*size == 0 || available <= *size)
408
6648
    *size = available;
409
410
6690
  return write_head_->data_ + write_head_->write_pos_;
411
}
412
413
414
6403
void NodeBIO::Commit(size_t size) {
415
6403
  write_head_->write_pos_ += size;
416
6403
  length_ += size;
417
6403
  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
6403
  TryAllocateForWrite(0);
422
6403
  if (write_head_->write_pos_ == write_head_->len_) {
423
711
    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
711
    TryMoveReadHead();
428
  }
429
6403
}
430
431
432
64555
void NodeBIO::TryAllocateForWrite(size_t hint) {
433
64555
  Buffer* w = write_head_;
434
64555
  Buffer* r = read_head_;
435
  // If write head is full, next buffer is either read head or not empty.
436
64555
  if (w == nullptr ||
437
18260
      (w->write_pos_ == w->len_ &&
438

847
       (w->next_ == r || w->next_->write_pos_ != 0))) {
439
46957
    size_t len = w == nullptr ? initial_ :
440
                             kThroughputBufferLength;
441
46957
    if (len < hint)
442
36116
      len = hint;
443
444
    // If there is a one time allocation size hint, use it.
445
46957
    if (allocate_hint_ > len) {
446
22
      len = allocate_hint_;
447
22
      allocate_hint_ = 0;
448
    }
449
450
46957
    Buffer* next = new Buffer(env_, len);
451
452
46957
    if (w == nullptr) {
453
46295
      next->next_ = next;
454
46295
      write_head_ = next;
455
46295
      read_head_ = next;
456
    } else {
457
662
      next->next_ = w->next_;
458
662
      w->next_ = next;
459
    }
460
  }
461
64555
}
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
265640
NodeBIO::~NodeBIO() {
483
132820
  if (read_head_ == nullptr)
484
40250
    return;
485
486
92570
  Buffer* current = read_head_;
487
1316
  do {
488
93886
    Buffer* next = current->next_;
489
93886
    delete current;
490
93886
    current = next;
491
93886
  } while (current != read_head_);
492
493
92570
  read_head_ = nullptr;
494
92570
  write_head_ = nullptr;
495
265640
}
496
497
498
1263097
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
499
1263097
  CHECK_NOT_NULL(BIO_get_data(bio));
500
1263097
  return static_cast<NodeBIO*>(BIO_get_data(bio));
501
}
502
503
504
}  // namespace crypto
505
}  // namespace node