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