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) {
62 if (bio == NULL) return 0;
65 if (bio->init && bio->ptr != NULL) {
75 int NodeBIO::Read(BIO* bio, char* out, int len) {
77 BIO_clear_retry_flags(bio);
79 bytes = FromBIO(bio)->Read(out, len);
84 BIO_set_retry_read(bio);
92 char* NodeBIO::Peek(size_t* size) {
93 *size = read_head_->write_pos_ - read_head_->read_pos_;
94 return read_head_->data_ + read_head_->read_pos_;
98 int NodeBIO::Write(BIO* bio, const char* data, int len) {
99 BIO_clear_retry_flags(bio);
101 FromBIO(bio)->Write(data, len);
107 int NodeBIO::Puts(BIO* bio, const char* str) {
108 return Write(bio, str, strlen(str));
112 int NodeBIO::Gets(BIO* bio, char* out, int size) {
113 NodeBIO* nbio = FromBIO(bio);
115 if (nbio->Length() == 0)
118 int i = nbio->IndexOf('\n', size);
123 // Shift `i` a bit to NULL-terminate string later
135 long NodeBIO::Ctrl(BIO* bio, int cmd, long num, void* ptr) {
147 ret = nbio->Length() == 0;
149 case BIO_C_SET_BUF_MEM_EOF_RETURN:
153 ret = nbio->Length();
155 *reinterpret_cast<void**>(ptr) = NULL;
157 case BIO_C_SET_BUF_MEM:
158 assert(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
161 case BIO_C_GET_BUF_MEM_PTR:
162 assert(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
165 case BIO_CTRL_GET_CLOSE:
168 case BIO_CTRL_SET_CLOSE:
171 case BIO_CTRL_WPENDING:
174 case BIO_CTRL_PENDING:
175 ret = nbio->Length();
191 size_t NodeBIO::Read(char* out, size_t size) {
192 size_t bytes_read = 0;
193 size_t expected = Length() > size ? size : Length();
197 while (bytes_read < expected) {
198 assert(read_head_->read_pos_ <= read_head_->write_pos_);
199 size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
205 memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
206 read_head_->read_pos_ += avail;
213 // Move to next buffer
214 if (read_head_->read_pos_ == read_head_->write_pos_) {
215 read_head_->read_pos_ = 0;
216 read_head_->write_pos_ = 0;
218 // But not get beyond write_head_
219 if (length_ != bytes_read && read_head_ != write_head_) {
220 read_head_ = read_head_->next_;
224 assert(expected == bytes_read);
225 length_ -= bytes_read;
227 // Free all empty buffers, but write_head's child
234 void NodeBIO::FreeEmpty() {
235 Buffer* child = write_head_->next_;
236 if (child == write_head_ || child == read_head_)
238 Buffer* cur = child->next_;
239 if (cur == write_head_ || cur == read_head_)
242 Buffer* prev = child;
243 while (cur != read_head_) {
244 // Skip embedded buffer, and continue deallocating again starting from it
251 assert(cur != write_head_);
252 assert(cur->write_pos_ == cur->read_pos_);
254 Buffer* next = cur->next_;
258 assert(prev == child || prev == &head_);
263 size_t NodeBIO::IndexOf(char delim, size_t limit) {
264 size_t bytes_read = 0;
265 size_t max = Length() > limit ? limit : Length();
267 Buffer* current = read_head_;
269 while (bytes_read < max) {
270 assert(current->read_pos_ <= current->write_pos_);
271 size_t avail = current->write_pos_ - current->read_pos_;
276 char* tmp = current->data_ + current->read_pos_;
278 while (off < avail && *tmp != delim) {
292 // Move to next buffer
293 if (current->read_pos_ + avail == kBufferLength) {
294 current = current->next_;
297 assert(max == bytes_read);
303 void NodeBIO::Write(const char* data, size_t size) {
307 size_t to_write = left;
308 assert(write_head_->write_pos_ <= kBufferLength);
309 size_t avail = kBufferLength - write_head_->write_pos_;
311 if (to_write > avail)
315 memcpy(write_head_->data_ + write_head_->write_pos_,
323 write_head_->write_pos_ += to_write;
324 assert(write_head_->write_pos_ <= kBufferLength);
326 // Go to next buffer if there still are some bytes to write
328 assert(write_head_->write_pos_ == kBufferLength);
329 TryAllocateForWrite();
330 write_head_ = write_head_->next_;
337 char* NodeBIO::PeekWritable(size_t* size) {
338 size_t available = kBufferLength - write_head_->write_pos_;
339 if (*size != 0 && available > *size)
344 return write_head_->data_ + write_head_->write_pos_;
348 void NodeBIO::Commit(size_t size) {
349 write_head_->write_pos_ += size;
351 assert(write_head_->write_pos_ <= kBufferLength);
353 // Allocate new buffer if write head is full,
354 // and there're no other place to go
355 TryAllocateForWrite();
356 if (write_head_->write_pos_ == kBufferLength)
357 write_head_ = write_head_->next_;
361 void NodeBIO::TryAllocateForWrite() {
362 // If write head is full, next buffer is either read head or not empty.
363 if (write_head_->write_pos_ == kBufferLength &&
364 (write_head_->next_ == read_head_ ||
365 write_head_->next_->write_pos_ != 0)) {
366 Buffer* next = new Buffer();
367 next->next_ = write_head_->next_;
368 write_head_->next_ = next;
373 void NodeBIO::Reset() {
374 while (read_head_->read_pos_ != read_head_->write_pos_) {
375 assert(read_head_->write_pos_ > read_head_->read_pos_);
377 length_ -= read_head_->write_pos_ - read_head_->read_pos_;
378 read_head_->write_pos_ = 0;
379 read_head_->read_pos_ = 0;
381 read_head_ = read_head_->next_;
383 write_head_ = read_head_;
384 assert(length_ == 0);
388 NodeBIO::~NodeBIO() {
389 Buffer* current = head_.next_;
390 while (current != &head_) {
391 Buffer* next = current->next_;