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 size_t NodeBIO::Read(char* out, size_t size) {
195 size_t bytes_read = 0;
196 size_t expected = Length() > size ? size : Length();
200 while (bytes_read < expected) {
201 assert(read_head_->read_pos_ <= read_head_->write_pos_);
202 size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
208 memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
209 read_head_->read_pos_ += avail;
216 // Move to next buffer
217 if (read_head_->read_pos_ == read_head_->write_pos_) {
218 read_head_->read_pos_ = 0;
219 read_head_->write_pos_ = 0;
221 // But not get beyond write_head_
222 if (length_ != bytes_read && read_head_ != write_head_) {
223 read_head_ = read_head_->next_;
227 assert(expected == bytes_read);
228 length_ -= bytes_read;
230 // Free all empty buffers, but write_head's child
237 void NodeBIO::FreeEmpty() {
238 Buffer* child = write_head_->next_;
239 if (child == write_head_ || child == read_head_)
241 Buffer* cur = child->next_;
242 if (cur == write_head_ || cur == read_head_)
245 Buffer* prev = child;
246 while (cur != read_head_) {
247 // Skip embedded buffer, and continue deallocating again starting from it
254 assert(cur != write_head_);
255 assert(cur->write_pos_ == cur->read_pos_);
257 Buffer* next = cur->next_;
261 assert(prev == child || prev == &head_);
266 size_t NodeBIO::IndexOf(char delim, size_t limit) {
267 size_t bytes_read = 0;
268 size_t max = Length() > limit ? limit : Length();
270 Buffer* current = read_head_;
272 while (bytes_read < max) {
273 assert(current->read_pos_ <= current->write_pos_);
274 size_t avail = current->write_pos_ - current->read_pos_;
279 char* tmp = current->data_ + current->read_pos_;
281 while (off < avail && *tmp != delim) {
295 // Move to next buffer
296 if (current->read_pos_ + avail == kBufferLength) {
297 current = current->next_;
300 assert(max == bytes_read);
306 void NodeBIO::Write(const char* data, size_t size) {
310 size_t to_write = left;
311 assert(write_head_->write_pos_ <= kBufferLength);
312 size_t avail = kBufferLength - write_head_->write_pos_;
314 if (to_write > avail)
318 memcpy(write_head_->data_ + write_head_->write_pos_,
326 write_head_->write_pos_ += to_write;
327 assert(write_head_->write_pos_ <= kBufferLength);
329 // Go to next buffer if there still are some bytes to write
331 assert(write_head_->write_pos_ == kBufferLength);
332 TryAllocateForWrite();
333 write_head_ = write_head_->next_;
340 char* NodeBIO::PeekWritable(size_t* size) {
341 size_t available = kBufferLength - write_head_->write_pos_;
342 if (*size != 0 && available > *size)
347 return write_head_->data_ + write_head_->write_pos_;
351 void NodeBIO::Commit(size_t size) {
352 write_head_->write_pos_ += size;
354 assert(write_head_->write_pos_ <= kBufferLength);
356 // Allocate new buffer if write head is full,
357 // and there're no other place to go
358 TryAllocateForWrite();
359 if (write_head_->write_pos_ == kBufferLength)
360 write_head_ = write_head_->next_;
364 void NodeBIO::TryAllocateForWrite() {
365 // If write head is full, next buffer is either read head or not empty.
366 if (write_head_->write_pos_ == kBufferLength &&
367 (write_head_->next_ == read_head_ ||
368 write_head_->next_->write_pos_ != 0)) {
369 Buffer* next = new Buffer();
370 next->next_ = write_head_->next_;
371 write_head_->next_ = next;
376 void NodeBIO::Reset() {
377 while (read_head_->read_pos_ != read_head_->write_pos_) {
378 assert(read_head_->write_pos_ > read_head_->read_pos_);
380 length_ -= read_head_->write_pos_ - read_head_->read_pos_;
381 read_head_->write_pos_ = 0;
382 read_head_->read_pos_ = 0;
384 read_head_ = read_head_->next_;
386 write_head_ = read_head_;
387 assert(length_ == 0);
391 NodeBIO::~NodeBIO() {
392 Buffer* current = head_.next_;
393 while (current != &head_) {
394 Buffer* next = current->next_;