crypto: use better memory BIO implementation
[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 int NodeBIO::Write(BIO* bio, const char* data, int len) {
86   BIO_clear_retry_flags(bio);
87
88   FromBIO(bio)->Write(data, len);
89
90   return len;
91 }
92
93
94 int NodeBIO::Puts(BIO* bio, const char* str) {
95   return Write(bio, str, strlen(str));
96 }
97
98
99 int NodeBIO::Gets(BIO* bio, char* out, int size) {
100   NodeBIO* nbio =  FromBIO(bio);
101
102   if (nbio->Length() == 0)
103     return 0;
104
105   int i = nbio->IndexOf('\n', size);
106
107   // Include '\n'
108   if (i < size) i++;
109
110   // Shift `i` a bit to NULL-terminate string later
111   if (size == i) i--;
112
113   // Flush read data
114   nbio->Read(out, i);
115
116   out[i] = 0;
117
118   return i;
119 }
120
121
122 long NodeBIO::Ctrl(BIO* bio, int cmd, long num, void* ptr) {
123   NodeBIO* nbio;
124   long ret;
125
126   nbio = FromBIO(bio);
127   ret = 1;
128
129   switch (cmd) {
130    case BIO_CTRL_RESET:
131     nbio->Reset();
132     break;
133    case BIO_CTRL_EOF:
134     ret = nbio->Length() == 0;
135     break;
136    case BIO_C_SET_BUF_MEM_EOF_RETURN:
137     bio->num = num;
138     break;
139    case BIO_CTRL_INFO:
140     ret = nbio->Length();
141     if (ptr != NULL)
142       *reinterpret_cast<void**>(ptr) = NULL;
143     break;
144    case BIO_C_SET_BUF_MEM:
145     assert(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
146     abort();
147     break;
148    case BIO_C_GET_BUF_MEM_PTR:
149     assert(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
150     ret = 0;
151     break;
152    case BIO_CTRL_GET_CLOSE:
153     ret = bio->shutdown;
154     break;
155    case BIO_CTRL_SET_CLOSE:
156     bio->shutdown = num;
157     break;
158    case BIO_CTRL_WPENDING:
159     ret = 0;
160     break;
161    case BIO_CTRL_PENDING:
162     ret = nbio->Length();
163     break;
164    case BIO_CTRL_DUP:
165    case BIO_CTRL_FLUSH:
166     ret = 1;
167     break;
168    case BIO_CTRL_PUSH:
169    case BIO_CTRL_POP:
170    default:
171     ret = 0;
172     break;
173   }
174   return ret;
175 }
176
177
178 size_t NodeBIO::Read(char* out, size_t size) {
179   size_t bytes_read = 0;
180   size_t expected = Length() > size ? size : Length();
181
182   while (bytes_read < expected) {
183     assert(read_head_->read_pos_ <= read_head_->write_pos_);
184     size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
185     if (avail > size)
186       avail = size;
187
188     // Copy data
189     if (out != NULL)
190       memcpy(out, read_head_->data_ + read_head_->read_pos_, avail);
191     read_head_->read_pos_ += avail;
192
193     // Move pointers
194     bytes_read += avail;
195     out += avail;
196     size -= avail;
197
198     // Move to next buffer
199     if (read_head_->read_pos_ == read_head_->write_pos_) {
200       read_head_->read_pos_ = 0;
201       read_head_->write_pos_ = 0;
202       read_head_ = read_head_->next_;
203     }
204   }
205   assert(expected == bytes_read);
206   length_ -= bytes_read;
207
208   return bytes_read;
209 }
210
211
212 size_t NodeBIO::IndexOf(char delim, size_t limit) {
213   size_t bytes_read = 0;
214   size_t max = Length() > limit ? limit : Length();
215   Buffer* current = read_head_;
216
217   while (bytes_read < max) {
218     assert(current->read_pos_ <= current->write_pos_);
219     size_t avail = current->write_pos_ - current->read_pos_;
220     if (avail > limit)
221       avail = limit;
222
223     // Walk through data
224     char* tmp = current->data_ + current->read_pos_;
225     size_t off = 0;
226     while (off < avail && *tmp != delim) {
227       off++;
228       tmp++;
229     }
230
231     // Move pointers
232     bytes_read += off;
233     limit -= off;
234
235     // Found `delim`
236     if (off != avail) {
237       return bytes_read;
238     }
239
240     // Move to next buffer
241     if (current->read_pos_ + avail == kBufferLength) {
242       current = current->next_;
243     }
244   }
245   assert(max == bytes_read);
246
247   return max;
248 }
249
250
251 void NodeBIO::Write(const char* data, size_t len) {
252   while (len > 0) {
253     size_t to_write = len;
254     assert(write_head_->write_pos_ <= kBufferLength);
255     size_t avail = kBufferLength - write_head_->write_pos_;
256
257     if (to_write > avail)
258       to_write = avail;
259
260     // Copy data
261     memcpy(write_head_->data_ + write_head_->write_pos_, data, to_write);
262     write_head_->write_pos_ += to_write;
263     assert(write_head_->write_pos_ <= kBufferLength);
264
265     // Move pointers
266     len -= to_write;
267     data += to_write;
268     length_ += to_write;
269
270     // Still have some bytes left:
271     //  1. Go to next buffer
272     //  2. Allocate new if next is already full or is partially read
273     //     (is read head)
274     if (write_head_->next_->write_pos_ == kBufferLength ||
275         write_head_->next_->read_pos_ != 0) {
276       Buffer* next = new Buffer();
277       next->next_ = write_head_->next_;
278       write_head_->next_ = next;
279     }
280     write_head_ = write_head_->next_;
281   }
282   assert(len == 0);
283 }
284
285
286 void NodeBIO::Reset() {
287   while (read_head_->read_pos_ != read_head_->write_pos_) {
288     assert(read_head_->write_pos_ > read_head_->read_pos_);
289
290     length_ -= read_head_->write_pos_ - read_head_->read_pos_;
291     read_head_->write_pos_ = 0;
292     read_head_->read_pos_ = 0;
293
294     read_head_ = read_head_->next_;
295   }
296   assert(length_ == 0);
297 }
298
299
300 NodeBIO::~NodeBIO() {
301   Buffer* current = head_.next_;
302   while (current != &head_) {
303     Buffer* next = current->next_;
304     delete current;
305     current = next;
306   }
307
308   read_head_ = NULL;
309   write_head_ = NULL;
310 }
311
312 } // namespace node