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

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


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

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


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



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

911849
  if (child == write_head_ || child == read_head_)
303
906667
    return;
304
5182
  Buffer* cur = child->next_;
305

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

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

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

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