1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #include "node_crypto_bio.h"
23 #include "openssl/bio.h"
28 const BIO_METHOD NodeBIO::method = {
43 // The const_cast doesn't violate const correctness. OpenSSL's usage of
44 // BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
45 return BIO_new(const_cast<BIO_METHOD*>(&method));
49 int NodeBIO::New(BIO* bio) {
50 bio->ptr = new NodeBIO();
52 // XXX Why am I doing it?!
61 int NodeBIO::Free(BIO* bio) {
66 if (bio->init && bio->ptr != NULL) {
76 int NodeBIO::Read(BIO* bio, char* out, int len) {
78 BIO_clear_retry_flags(bio);
80 bytes = FromBIO(bio)->Read(out, len);
85 BIO_set_retry_read(bio);
93 char* NodeBIO::Peek(size_t* size) {
94 *size = read_head_->write_pos_ - read_head_->read_pos_;
95 return read_head_->data_ + read_head_->read_pos_;
99 int NodeBIO::Write(BIO* bio, const char* data, int len) {
100 BIO_clear_retry_flags(bio);
102 FromBIO(bio)->Write(data, len);
108 int NodeBIO::Puts(BIO* bio, const char* str) {
109 return Write(bio, str, strlen(str));
113 int NodeBIO::Gets(BIO* bio, char* out, int size) {
114 NodeBIO* nbio = FromBIO(bio);
116 if (nbio->Length() == 0)
119 int i = nbio->IndexOf('\n', size);
125 // Shift `i` a bit to NULL-terminate string later
138 long NodeBIO::Ctrl(BIO* bio, int cmd, long num, void* ptr) {
150 ret = nbio->Length() == 0;
152 case BIO_C_SET_BUF_MEM_EOF_RETURN:
156 ret = nbio->Length();
158 *reinterpret_cast<void**>(ptr) = NULL;
160 case BIO_C_SET_BUF_MEM:
161 assert(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
164 case BIO_C_GET_BUF_MEM_PTR:
165 assert(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
168 case BIO_CTRL_GET_CLOSE:
171 case BIO_CTRL_SET_CLOSE:
174 case BIO_CTRL_WPENDING:
177 case BIO_CTRL_PENDING:
178 ret = nbio->Length();
194 void NodeBIO::TryMoveReadHead() {
195 // `read_pos_` and `write_pos_` means the position of the reader and writer
196 // inside the buffer, respectively. When they're equal - its safe to reset
197 // them, because both reader and writer will continue doing their stuff
198 // from new (zero) positions.
199 if (read_head_->read_pos_ != read_head_->write_pos_)
203 read_head_->read_pos_ = 0;
204 read_head_->write_pos_ = 0;
206 // Move read_head_ forward, just in case if there're still some data to
207 // read in the next buffer.
208 if (read_head_ != write_head_)
209 read_head_ = read_head_->next_;
213 size_t NodeBIO::Read(char* out, size_t size) {
214 size_t bytes_read = 0;
215 size_t expected = Length() > size ? size : Length();
219 while (bytes_read < expected) {
220 assert(read_head_->read_pos_ <= read_head_->write_pos_);
221 size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
227 memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
228 read_head_->read_pos_ += avail;
237 assert(expected == bytes_read);
238 length_ -= bytes_read;
240 // Free all empty buffers, but write_head's child
247 void NodeBIO::FreeEmpty() {
248 Buffer* child = write_head_->next_;
249 if (child == write_head_ || child == read_head_)
251 Buffer* cur = child->next_;
252 if (cur == write_head_ || cur == read_head_)
255 Buffer* prev = child;
256 while (cur != read_head_) {
257 // Skip embedded buffer, and continue deallocating again starting from it
264 assert(cur != write_head_);
265 assert(cur->write_pos_ == cur->read_pos_);
267 Buffer* next = cur->next_;
271 assert(prev == child || prev == &head_);
276 size_t NodeBIO::IndexOf(char delim, size_t limit) {
277 size_t bytes_read = 0;
278 size_t max = Length() > limit ? limit : Length();
280 Buffer* current = read_head_;
282 while (bytes_read < max) {
283 assert(current->read_pos_ <= current->write_pos_);
284 size_t avail = current->write_pos_ - current->read_pos_;
289 char* tmp = current->data_ + current->read_pos_;
291 while (off < avail && *tmp != delim) {
305 // Move to next buffer
306 if (current->read_pos_ + avail == kBufferLength) {
307 current = current->next_;
310 assert(max == bytes_read);
316 void NodeBIO::Write(const char* data, size_t size) {
320 size_t to_write = left;
321 assert(write_head_->write_pos_ <= kBufferLength);
322 size_t avail = kBufferLength - write_head_->write_pos_;
324 if (to_write > avail)
328 memcpy(write_head_->data_ + write_head_->write_pos_,
336 write_head_->write_pos_ += to_write;
337 assert(write_head_->write_pos_ <= kBufferLength);
339 // Go to next buffer if there still are some bytes to write
341 assert(write_head_->write_pos_ == kBufferLength);
342 TryAllocateForWrite();
343 write_head_ = write_head_->next_;
345 // Additionally, since we're moved to the next buffer, read head
346 // may be moved as well.
354 char* NodeBIO::PeekWritable(size_t* size) {
355 size_t available = kBufferLength - write_head_->write_pos_;
356 if (*size != 0 && available > *size)
361 return write_head_->data_ + write_head_->write_pos_;
365 void NodeBIO::Commit(size_t size) {
366 write_head_->write_pos_ += size;
368 assert(write_head_->write_pos_ <= kBufferLength);
370 // Allocate new buffer if write head is full,
371 // and there're no other place to go
372 TryAllocateForWrite();
373 if (write_head_->write_pos_ == kBufferLength)
374 write_head_ = write_head_->next_;
378 void NodeBIO::TryAllocateForWrite() {
379 // If write head is full, next buffer is either read head or not empty.
380 if (write_head_->write_pos_ == kBufferLength &&
381 (write_head_->next_ == read_head_ ||
382 write_head_->next_->write_pos_ != 0)) {
383 Buffer* next = new Buffer();
384 next->next_ = write_head_->next_;
385 write_head_->next_ = next;
390 void NodeBIO::Reset() {
391 while (read_head_->read_pos_ != read_head_->write_pos_) {
392 assert(read_head_->write_pos_ > read_head_->read_pos_);
394 length_ -= read_head_->write_pos_ - read_head_->read_pos_;
395 read_head_->write_pos_ = 0;
396 read_head_->read_pos_ = 0;
398 read_head_ = read_head_->next_;
400 write_head_ = read_head_;
401 assert(length_ == 0);
405 NodeBIO::~NodeBIO() {
406 Buffer* current = head_.next_;
407 while (current != &head_) {
408 Buffer* next = current->next_;