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