GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_crypto_bio.cc Lines: 11 258 4.3 %
Date: 2019-02-01 22:03:38 Branches: 1 176 0.6 %

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 "node_crypto_bio.h"
23
#include "openssl/bio.h"
24
#include "util-inl.h"
25
#include <limits.h>
26
#include <string.h>
27
28
namespace node {
29
namespace crypto {
30
31
#if OPENSSL_VERSION_NUMBER < 0x10100000L
32
#define BIO_set_data(bio, data) bio->ptr = data
33
#define BIO_get_data(bio) bio->ptr
34
#define BIO_set_shutdown(bio, shutdown_) bio->shutdown = shutdown_
35
#define BIO_get_shutdown(bio) bio->shutdown
36
#define BIO_set_init(bio, init_) bio->init = init_
37
#define BIO_get_init(bio) bio->init
38
#endif
39
40
41
BIOPointer NodeBIO::New(Environment* env) {
42
  // The const_cast doesn't violate const correctness.  OpenSSL's usage of
43
  // BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
44
  BIOPointer bio(BIO_new(const_cast<BIO_METHOD*>(GetMethod())));
45
  if (bio && env != nullptr)
46
    NodeBIO::FromBIO(bio.get())->env_ = env;
47
  return bio;
48
}
49
50
51
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
52
  BIOPointer bio = New(env);
53
54
  if (!bio ||
55
      len > INT_MAX ||
56
      BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
57
      BIO_set_mem_eof_return(bio.get(), 0) != 1) {
58
    return BIOPointer();
59
  }
60
61
  return bio;
62
}
63
64
65
int NodeBIO::New(BIO* bio) {
66
  BIO_set_data(bio, new NodeBIO());
67
  BIO_set_init(bio, 1);
68
69
  return 1;
70
}
71
72
73
int NodeBIO::Free(BIO* bio) {
74
  if (bio == nullptr)
75
    return 0;
76
77
  if (BIO_get_shutdown(bio)) {
78
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
79
      delete FromBIO(bio);
80
      BIO_set_data(bio, nullptr);
81
    }
82
  }
83
84
  return 1;
85
}
86
87
88
int NodeBIO::Read(BIO* bio, char* out, int len) {
89
  BIO_clear_retry_flags(bio);
90
91
  NodeBIO* nbio = FromBIO(bio);
92
  int bytes = nbio->Read(out, len);
93
94
  if (bytes == 0) {
95
    bytes = nbio->eof_return();
96
    if (bytes != 0) {
97
      BIO_set_retry_read(bio);
98
    }
99
  }
100
101
  return bytes;
102
}
103
104
105
char* NodeBIO::Peek(size_t* size) {
106
  *size = read_head_->write_pos_ - read_head_->read_pos_;
107
  return read_head_->data_ + read_head_->read_pos_;
108
}
109
110
111
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
112
  Buffer* pos = read_head_;
113
  size_t max = *count;
114
  size_t total = 0;
115
116
  size_t i;
117
  for (i = 0; i < max; i++) {
118
    size[i] = pos->write_pos_ - pos->read_pos_;
119
    total += size[i];
120
    out[i] = pos->data_ + pos->read_pos_;
121
122
    /* Don't get past write head */
123
    if (pos == write_head_)
124
      break;
125
    else
126
      pos = pos->next_;
127
  }
128
129
  if (i == max)
130
    *count = i;
131
  else
132
    *count = i + 1;
133
134
  return total;
135
}
136
137
138
int NodeBIO::Write(BIO* bio, const char* data, int len) {
139
  BIO_clear_retry_flags(bio);
140
141
  FromBIO(bio)->Write(data, len);
142
143
  return len;
144
}
145
146
147
int NodeBIO::Puts(BIO* bio, const char* str) {
148
  return Write(bio, str, strlen(str));
149
}
150
151
152
int NodeBIO::Gets(BIO* bio, char* out, int size) {
153
  NodeBIO* nbio = FromBIO(bio);
154
155
  if (nbio->Length() == 0)
156
    return 0;
157
158
  int i = nbio->IndexOf('\n', size);
159
160
  // Include '\n', if it's there.  If not, don't read off the end.
161
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
162
    i++;
163
164
  // Shift `i` a bit to nullptr-terminate string later
165
  if (size == i)
166
    i--;
167
168
  // Flush read data
169
  nbio->Read(out, i);
170
171
  out[i] = 0;
172
173
  return i;
174
}
175
176
177
long NodeBIO::Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
178
                   void* ptr) {
179
  NodeBIO* nbio;
180
  long ret;  // NOLINT(runtime/int)
181
182
  nbio = FromBIO(bio);
183
  ret = 1;
184
185
  switch (cmd) {
186
    case BIO_CTRL_RESET:
187
      nbio->Reset();
188
      break;
189
    case BIO_CTRL_EOF:
190
      ret = nbio->Length() == 0;
191
      break;
192
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
193
      nbio->set_eof_return(num);
194
      break;
195
    case BIO_CTRL_INFO:
196
      ret = nbio->Length();
197
      if (ptr != nullptr)
198
        *reinterpret_cast<void**>(ptr) = nullptr;
199
      break;
200
    case BIO_C_SET_BUF_MEM:
201
      CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
202
      break;
203
    case BIO_C_GET_BUF_MEM_PTR:
204
      CHECK(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
205
      ret = 0;
206
      break;
207
    case BIO_CTRL_GET_CLOSE:
208
      ret = BIO_get_shutdown(bio);
209
      break;
210
    case BIO_CTRL_SET_CLOSE:
211
      BIO_set_shutdown(bio, num);
212
      break;
213
    case BIO_CTRL_WPENDING:
214
      ret = 0;
215
      break;
216
    case BIO_CTRL_PENDING:
217
      ret = nbio->Length();
218
      break;
219
    case BIO_CTRL_DUP:
220
    case BIO_CTRL_FLUSH:
221
      ret = 1;
222
      break;
223
    case BIO_CTRL_PUSH:
224
    case BIO_CTRL_POP:
225
    default:
226
      ret = 0;
227
      break;
228
  }
229
  return ret;
230
}
231
232
233
9
const BIO_METHOD* NodeBIO::GetMethod() {
234
#if OPENSSL_VERSION_NUMBER < 0x10100000L
235
  static const BIO_METHOD method = {
236
    BIO_TYPE_MEM,
237
    "node.js SSL buffer",
238
    Write,
239
    Read,
240
    Puts,
241
    Gets,
242
    Ctrl,
243
    New,
244
    Free,
245
    nullptr
246
  };
247
248
  return &method;
249
#else
250
  // This is called from InitCryptoOnce() to avoid race conditions during
251
  // initialization.
252
  static BIO_METHOD* method = nullptr;
253
254
9
  if (method == nullptr) {
255
9
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
256
9
    BIO_meth_set_write(method, Write);
257
9
    BIO_meth_set_read(method, Read);
258
9
    BIO_meth_set_puts(method, Puts);
259
9
    BIO_meth_set_gets(method, Gets);
260
9
    BIO_meth_set_ctrl(method, Ctrl);
261
9
    BIO_meth_set_create(method, New);
262
9
    BIO_meth_set_destroy(method, Free);
263
  }
264
265
9
  return method;
266
#endif
267
}
268
269
270
void NodeBIO::TryMoveReadHead() {
271
  // `read_pos_` and `write_pos_` means the position of the reader and writer
272
  // inside the buffer, respectively. When they're equal - its safe to reset
273
  // them, because both reader and writer will continue doing their stuff
274
  // from new (zero) positions.
275
  while (read_head_->read_pos_ != 0 &&
276
         read_head_->read_pos_ == read_head_->write_pos_) {
277
    // Reset positions
278
    read_head_->read_pos_ = 0;
279
    read_head_->write_pos_ = 0;
280
281
    // Move read_head_ forward, just in case if there're still some data to
282
    // read in the next buffer.
283
    if (read_head_ != write_head_)
284
      read_head_ = read_head_->next_;
285
  }
286
}
287
288
289
size_t NodeBIO::Read(char* out, size_t size) {
290
  size_t bytes_read = 0;
291
  size_t expected = Length() > size ? size : Length();
292
  size_t offset = 0;
293
  size_t left = size;
294
295
  while (bytes_read < expected) {
296
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
297
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
298
    if (avail > left)
299
      avail = left;
300
301
    // Copy data
302
    if (out != nullptr)
303
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
304
    read_head_->read_pos_ += avail;
305
306
    // Move pointers
307
    bytes_read += avail;
308
    offset += avail;
309
    left -= avail;
310
311
    TryMoveReadHead();
312
  }
313
  CHECK_EQ(expected, bytes_read);
314
  length_ -= bytes_read;
315
316
  // Free all empty buffers, but write_head's child
317
  FreeEmpty();
318
319
  return bytes_read;
320
}
321
322
323
void NodeBIO::FreeEmpty() {
324
  if (write_head_ == nullptr)
325
    return;
326
  Buffer* child = write_head_->next_;
327
  if (child == write_head_ || child == read_head_)
328
    return;
329
  Buffer* cur = child->next_;
330
  if (cur == write_head_ || cur == read_head_)
331
    return;
332
333
  Buffer* prev = child;
334
  while (cur != read_head_) {
335
    CHECK_NE(cur, write_head_);
336
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
337
338
    Buffer* next = cur->next_;
339
    delete cur;
340
    cur = next;
341
  }
342
  prev->next_ = cur;
343
}
344
345
346
size_t NodeBIO::IndexOf(char delim, size_t limit) {
347
  size_t bytes_read = 0;
348
  size_t max = Length() > limit ? limit : Length();
349
  size_t left = limit;
350
  Buffer* current = read_head_;
351
352
  while (bytes_read < max) {
353
    CHECK_LE(current->read_pos_, current->write_pos_);
354
    size_t avail = current->write_pos_ - current->read_pos_;
355
    if (avail > left)
356
      avail = left;
357
358
    // Walk through data
359
    char* tmp = current->data_ + current->read_pos_;
360
    size_t off = 0;
361
    while (off < avail && *tmp != delim) {
362
      off++;
363
      tmp++;
364
    }
365
366
    // Move pointers
367
    bytes_read += off;
368
    left -= off;
369
370
    // Found `delim`
371
    if (off != avail) {
372
      return bytes_read;
373
    }
374
375
    // Move to next buffer
376
    if (current->read_pos_ + avail == current->len_) {
377
      current = current->next_;
378
    }
379
  }
380
  CHECK_EQ(max, bytes_read);
381
382
  return max;
383
}
384
385
386
void NodeBIO::Write(const char* data, size_t size) {
387
  size_t offset = 0;
388
  size_t left = size;
389
390
  // Allocate initial buffer if the ring is empty
391
  TryAllocateForWrite(left);
392
393
  while (left > 0) {
394
    size_t to_write = left;
395
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
396
    size_t avail = write_head_->len_ - write_head_->write_pos_;
397
398
    if (to_write > avail)
399
      to_write = avail;
400
401
    // Copy data
402
    memcpy(write_head_->data_ + write_head_->write_pos_,
403
           data + offset,
404
           to_write);
405
406
    // Move pointers
407
    left -= to_write;
408
    offset += to_write;
409
    length_ += to_write;
410
    write_head_->write_pos_ += to_write;
411
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
412
413
    // Go to next buffer if there still are some bytes to write
414
    if (left != 0) {
415
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
416
      TryAllocateForWrite(left);
417
      write_head_ = write_head_->next_;
418
419
      // Additionally, since we're moved to the next buffer, read head
420
      // may be moved as well.
421
      TryMoveReadHead();
422
    }
423
  }
424
  CHECK_EQ(left, 0);
425
}
426
427
428
char* NodeBIO::PeekWritable(size_t* size) {
429
  TryAllocateForWrite(*size);
430
431
  size_t available = write_head_->len_ - write_head_->write_pos_;
432
  if (*size != 0 && available > *size)
433
    available = *size;
434
  else
435
    *size = available;
436
437
  return write_head_->data_ + write_head_->write_pos_;
438
}
439
440
441
void NodeBIO::Commit(size_t size) {
442
  write_head_->write_pos_ += size;
443
  length_ += size;
444
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
445
446
  // Allocate new buffer if write head is full,
447
  // and there're no other place to go
448
  TryAllocateForWrite(0);
449
  if (write_head_->write_pos_ == write_head_->len_) {
450
    write_head_ = write_head_->next_;
451
452
    // Additionally, since we're moved to the next buffer, read head
453
    // may be moved as well.
454
    TryMoveReadHead();
455
  }
456
}
457
458
459
void NodeBIO::TryAllocateForWrite(size_t hint) {
460
  Buffer* w = write_head_;
461
  Buffer* r = read_head_;
462
  // If write head is full, next buffer is either read head or not empty.
463
  if (w == nullptr ||
464
      (w->write_pos_ == w->len_ &&
465
       (w->next_ == r || w->next_->write_pos_ != 0))) {
466
    size_t len = w == nullptr ? initial_ :
467
                             kThroughputBufferLength;
468
    if (len < hint)
469
      len = hint;
470
    Buffer* next = new Buffer(env_, len);
471
472
    if (w == nullptr) {
473
      next->next_ = next;
474
      write_head_ = next;
475
      read_head_ = next;
476
    } else {
477
      next->next_ = w->next_;
478
      w->next_ = next;
479
    }
480
  }
481
}
482
483
484
void NodeBIO::Reset() {
485
  if (read_head_ == nullptr)
486
    return;
487
488
  while (read_head_->read_pos_ != read_head_->write_pos_) {
489
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
490
491
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
492
    read_head_->write_pos_ = 0;
493
    read_head_->read_pos_ = 0;
494
495
    read_head_ = read_head_->next_;
496
  }
497
  write_head_ = read_head_;
498
  CHECK_EQ(length_, 0);
499
}
500
501
502
NodeBIO::~NodeBIO() {
503
  if (read_head_ == nullptr)
504
    return;
505
506
  Buffer* current = read_head_;
507
  do {
508
    Buffer* next = current->next_;
509
    delete current;
510
    current = next;
511
  } while (current != read_head_);
512
513
  read_head_ = nullptr;
514
  write_head_ = nullptr;
515
}
516
517
518
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
519
  CHECK_NOT_NULL(BIO_get_data(bio));
520
  return static_cast<NodeBIO*>(BIO_get_data(bio));
521
}
522
523
524
}  // namespace crypto
525
}  // namespace node