Merge remote-tracking branch 'upstream/v0.10'
[platform/upstream/nodejs.git] / src / node_crypto_bio.cc
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 <string.h>
25
26 namespace node {
27
28 const BIO_METHOD NodeBIO::method = {
29   BIO_TYPE_MEM,
30   "node.js SSL buffer",
31   NodeBIO::Write,
32   NodeBIO::Read,
33   NodeBIO::Puts,
34   NodeBIO::Gets,
35   NodeBIO::Ctrl,
36   NodeBIO::New,
37   NodeBIO::Free,
38   NULL
39 };
40
41
42 BIO* NodeBIO::New() {
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));
46 }
47
48
49 int NodeBIO::New(BIO* bio) {
50   bio->ptr = new NodeBIO();
51
52   // XXX Why am I doing it?!
53   bio->shutdown = 1;
54   bio->init = 1;
55   bio->num = -1;
56
57   return 1;
58 }
59
60
61 int NodeBIO::Free(BIO* bio) {
62   if (bio == NULL)
63     return 0;
64
65   if (bio->shutdown) {
66     if (bio->init && bio->ptr != NULL) {
67       delete FromBIO(bio);
68       bio->ptr = NULL;
69     }
70   }
71
72   return 1;
73 }
74
75
76 int NodeBIO::Read(BIO* bio, char* out, int len) {
77   int bytes;
78   BIO_clear_retry_flags(bio);
79
80   bytes = FromBIO(bio)->Read(out, len);
81
82   if (bytes == 0) {
83     bytes = bio->num;
84     if (bytes != 0) {
85       BIO_set_retry_read(bio);
86     }
87   }
88
89   return bytes;
90 }
91
92
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_;
96 }
97
98
99 int NodeBIO::Write(BIO* bio, const char* data, int len) {
100   BIO_clear_retry_flags(bio);
101
102   FromBIO(bio)->Write(data, len);
103
104   return len;
105 }
106
107
108 int NodeBIO::Puts(BIO* bio, const char* str) {
109   return Write(bio, str, strlen(str));
110 }
111
112
113 int NodeBIO::Gets(BIO* bio, char* out, int size) {
114   NodeBIO* nbio =  FromBIO(bio);
115
116   if (nbio->Length() == 0)
117     return 0;
118
119   int i = nbio->IndexOf('\n', size);
120
121   // Include '\n'
122   if (i < size)
123     i++;
124
125   // Shift `i` a bit to NULL-terminate string later
126   if (size == i)
127     i--;
128
129   // Flush read data
130   nbio->Read(out, i);
131
132   out[i] = 0;
133
134   return i;
135 }
136
137
138 long NodeBIO::Ctrl(BIO* bio, int cmd, long num, void* ptr) {
139   NodeBIO* nbio;
140   long ret;
141
142   nbio = FromBIO(bio);
143   ret = 1;
144
145   switch (cmd) {
146     case BIO_CTRL_RESET:
147       nbio->Reset();
148       break;
149     case BIO_CTRL_EOF:
150       ret = nbio->Length() == 0;
151       break;
152     case BIO_C_SET_BUF_MEM_EOF_RETURN:
153       bio->num = num;
154       break;
155     case BIO_CTRL_INFO:
156       ret = nbio->Length();
157       if (ptr != NULL)
158         *reinterpret_cast<void**>(ptr) = NULL;
159       break;
160     case BIO_C_SET_BUF_MEM:
161       assert(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
162       abort();
163       break;
164     case BIO_C_GET_BUF_MEM_PTR:
165       assert(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
166       ret = 0;
167       break;
168     case BIO_CTRL_GET_CLOSE:
169       ret = bio->shutdown;
170       break;
171     case BIO_CTRL_SET_CLOSE:
172       bio->shutdown = num;
173       break;
174     case BIO_CTRL_WPENDING:
175       ret = 0;
176       break;
177     case BIO_CTRL_PENDING:
178       ret = nbio->Length();
179       break;
180     case BIO_CTRL_DUP:
181     case BIO_CTRL_FLUSH:
182       ret = 1;
183       break;
184     case BIO_CTRL_PUSH:
185     case BIO_CTRL_POP:
186     default:
187       ret = 0;
188       break;
189   }
190   return ret;
191 }
192
193
194 size_t NodeBIO::Read(char* out, size_t size) {
195   size_t bytes_read = 0;
196   size_t expected = Length() > size ? size : Length();
197   size_t offset = 0;
198   size_t left = size;
199
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_;
203     if (avail > left)
204       avail = left;
205
206     // Copy data
207     if (out != NULL)
208       memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
209     read_head_->read_pos_ += avail;
210
211     // Move pointers
212     bytes_read += avail;
213     offset += avail;
214     left -= avail;
215
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;
220
221       // But not get beyond write_head_
222       if (length_ != bytes_read && read_head_ != write_head_) {
223         read_head_ = read_head_->next_;
224       }
225     }
226   }
227   assert(expected == bytes_read);
228   length_ -= bytes_read;
229
230   // Free all empty buffers, but write_head's child
231   FreeEmpty();
232
233   return bytes_read;
234 }
235
236
237 void NodeBIO::FreeEmpty() {
238   Buffer* child = write_head_->next_;
239   if (child == write_head_ || child == read_head_)
240     return;
241   Buffer* cur = child->next_;
242   if (cur == write_head_ || cur == read_head_)
243     return;
244
245   Buffer* prev = child;
246   while (cur != read_head_) {
247     // Skip embedded buffer, and continue deallocating again starting from it
248     if (cur == &head_) {
249       prev->next_ = cur;
250       prev = cur;
251       cur = head_.next_;
252       continue;
253     }
254     assert(cur != write_head_);
255     assert(cur->write_pos_ == cur->read_pos_);
256
257     Buffer* next = cur->next_;
258     delete cur;
259     cur = next;
260   }
261   assert(prev == child || prev == &head_);
262   prev->next_ = cur;
263 }
264
265
266 size_t NodeBIO::IndexOf(char delim, size_t limit) {
267   size_t bytes_read = 0;
268   size_t max = Length() > limit ? limit : Length();
269   size_t left = limit;
270   Buffer* current = read_head_;
271
272   while (bytes_read < max) {
273     assert(current->read_pos_ <= current->write_pos_);
274     size_t avail = current->write_pos_ - current->read_pos_;
275     if (avail > left)
276       avail = left;
277
278     // Walk through data
279     char* tmp = current->data_ + current->read_pos_;
280     size_t off = 0;
281     while (off < avail && *tmp != delim) {
282       off++;
283       tmp++;
284     }
285
286     // Move pointers
287     bytes_read += off;
288     left -= off;
289
290     // Found `delim`
291     if (off != avail) {
292       return bytes_read;
293     }
294
295     // Move to next buffer
296     if (current->read_pos_ + avail == kBufferLength) {
297       current = current->next_;
298     }
299   }
300   assert(max == bytes_read);
301
302   return max;
303 }
304
305
306 void NodeBIO::Write(const char* data, size_t size) {
307   size_t offset = 0;
308   size_t left = size;
309   while (left > 0) {
310     size_t to_write = left;
311     assert(write_head_->write_pos_ <= kBufferLength);
312     size_t avail = kBufferLength - write_head_->write_pos_;
313
314     if (to_write > avail)
315       to_write = avail;
316
317     // Copy data
318     memcpy(write_head_->data_ + write_head_->write_pos_,
319            data + offset,
320            to_write);
321
322     // Move pointers
323     left -= to_write;
324     offset += to_write;
325     length_ += to_write;
326     write_head_->write_pos_ += to_write;
327     assert(write_head_->write_pos_ <= kBufferLength);
328
329     // Go to next buffer if there still are some bytes to write
330     if (left != 0) {
331       assert(write_head_->write_pos_ == kBufferLength);
332       TryAllocateForWrite();
333       write_head_ = write_head_->next_;
334     }
335   }
336   assert(left == 0);
337 }
338
339
340 char* NodeBIO::PeekWritable(size_t* size) {
341   size_t available = kBufferLength - write_head_->write_pos_;
342   if (*size != 0 && available > *size)
343     available = *size;
344   else
345     *size = available;
346
347   return write_head_->data_ + write_head_->write_pos_;
348 }
349
350
351 void NodeBIO::Commit(size_t size) {
352   write_head_->write_pos_ += size;
353   length_ += size;
354   assert(write_head_->write_pos_ <= kBufferLength);
355
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_;
361 }
362
363
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;
372   }
373 }
374
375
376 void NodeBIO::Reset() {
377   while (read_head_->read_pos_ != read_head_->write_pos_) {
378     assert(read_head_->write_pos_ > read_head_->read_pos_);
379
380     length_ -= read_head_->write_pos_ - read_head_->read_pos_;
381     read_head_->write_pos_ = 0;
382     read_head_->read_pos_ = 0;
383
384     read_head_ = read_head_->next_;
385   }
386   write_head_ = read_head_;
387   assert(length_ == 0);
388 }
389
390
391 NodeBIO::~NodeBIO() {
392   Buffer* current = head_.next_;
393   while (current != &head_) {
394     Buffer* next = current->next_;
395     delete current;
396     current = next;
397   }
398
399   read_head_ = NULL;
400   write_head_ = NULL;
401 }
402
403 }  // namespace node