src: ensure that openssl's PRNG is fully seeded
[platform/upstream/nodejs.git] / src / node_crypto.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.h"
23 #include "node_crypto_groups.h"
24 #include "v8.h"
25
26 #include "node.h"
27 #include "node_buffer.h"
28 #include "string_bytes.h"
29
30 #include <string.h>
31 #ifdef _MSC_VER
32 #define strcasecmp _stricmp
33 #endif
34
35 #include <stdlib.h>
36 #include <errno.h>
37
38 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
39 # define OPENSSL_CONST const
40 #else
41 # define OPENSSL_CONST
42 #endif
43
44 #define ASSERT_IS_STRING_OR_BUFFER(val) \
45   if (!Buffer::HasInstance(val) && !val->IsString()) { \
46     return ThrowException(Exception::TypeError(String::New( \
47             "Not a string or buffer"))); \
48   }
49
50 #define ASSERT_IS_BUFFER(val) \
51   if (!Buffer::HasInstance(val)) { \
52     return ThrowException(Exception::TypeError(String::New("Not a buffer"))); \
53   }
54
55 static const char PUBLIC_KEY_PFX[] =  "-----BEGIN PUBLIC KEY-----";
56 static const int PUBLIC_KEY_PFX_LEN = sizeof(PUBLIC_KEY_PFX) - 1;
57 static const char PUBRSA_KEY_PFX[] =  "-----BEGIN RSA PUBLIC KEY-----";
58 static const int PUBRSA_KEY_PFX_LEN = sizeof(PUBRSA_KEY_PFX) - 1;
59 static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL
60                                  | ASN1_STRFLGS_ESC_MSB
61                                  | XN_FLAG_SEP_MULTILINE
62                                  | XN_FLAG_FN_SN;
63
64 namespace node {
65
66 const char* root_certs[] = {
67 #include "node_root_certs.h"  // NOLINT(build/include_order)
68   NULL
69 };
70
71 namespace crypto {
72
73 using namespace v8;
74
75 // Forcibly clear OpenSSL's error stack on return. This stops stale errors
76 // from popping up later in the lifecycle of crypto operations where they
77 // would cause spurious failures. It's a rather blunt method, though.
78 // ERR_clear_error() isn't necessarily cheap either.
79 struct ClearErrorOnReturn {
80   ~ClearErrorOnReturn() { ERR_clear_error(); }
81 };
82
83 static Persistent<String> errno_symbol;
84 static Persistent<String> syscall_symbol;
85 static Persistent<String> subject_symbol;
86 static Persistent<String> subjectaltname_symbol;
87 static Persistent<String> modulus_symbol;
88 static Persistent<String> exponent_symbol;
89 static Persistent<String> issuer_symbol;
90 static Persistent<String> valid_from_symbol;
91 static Persistent<String> valid_to_symbol;
92 static Persistent<String> fingerprint_symbol;
93 static Persistent<String> name_symbol;
94 static Persistent<String> version_symbol;
95 static Persistent<String> ext_key_usage_symbol;
96 static Persistent<String> onhandshakestart_sym;
97 static Persistent<String> onhandshakedone_sym;
98 static Persistent<String> onclienthello_sym;
99 static Persistent<String> onnewsession_sym;
100 static Persistent<String> sessionid_sym;
101
102 static Persistent<FunctionTemplate> secure_context_constructor;
103
104 static uv_rwlock_t* locks;
105
106
107 static void crypto_threadid_cb(CRYPTO_THREADID* tid) {
108   CRYPTO_THREADID_set_numeric(tid, uv_thread_self());
109 }
110
111
112 static void crypto_lock_init(void) {
113   int i, n;
114
115   n = CRYPTO_num_locks();
116   locks = new uv_rwlock_t[n];
117
118   for (i = 0; i < n; i++)
119     if (uv_rwlock_init(locks + i))
120       abort();
121 }
122
123
124 static void crypto_lock_cb(int mode, int n, const char* file, int line) {
125   assert((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
126   assert((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));
127
128   if (mode & CRYPTO_LOCK) {
129     if (mode & CRYPTO_READ)
130       uv_rwlock_rdlock(locks + n);
131     else
132       uv_rwlock_wrlock(locks + n);
133   } else {
134     if (mode & CRYPTO_READ)
135       uv_rwlock_rdunlock(locks + n);
136     else
137       uv_rwlock_wrunlock(locks + n);
138   }
139 }
140
141
142 Handle<Value> ThrowCryptoErrorHelper(unsigned long err, bool is_type_error) {
143   HandleScope scope;
144   char errmsg[128];
145   ERR_error_string_n(err, errmsg, sizeof(errmsg));
146   return is_type_error ? ThrowTypeError(errmsg) : ThrowError(errmsg);
147 }
148
149
150 Handle<Value> ThrowCryptoError(unsigned long err) {
151   return ThrowCryptoErrorHelper(err, false);
152 }
153
154
155 Handle<Value> ThrowCryptoTypeError(unsigned long err) {
156   return ThrowCryptoErrorHelper(err, true);
157 }
158
159
160 // Ensure that OpenSSL has enough entropy (at least 256 bits) for its PRNG.
161 // The entropy pool starts out empty and needs to fill up before the PRNG
162 // can be used securely.  Once the pool is filled, it never dries up again;
163 // its contents is stirred and reused when necessary.
164 //
165 // OpenSSL normally fills the pool automatically but not when someone starts
166 // generating random numbers before the pool is full: in that case OpenSSL
167 // keeps lowering the entropy estimate to thwart attackers trying to guess
168 // the initial state of the PRNG.
169 //
170 // When that happens, we will have to wait until enough entropy is available.
171 // That should normally never take longer than a few milliseconds.
172 //
173 // OpenSSL draws from /dev/random and /dev/urandom.  While /dev/random may
174 // block pending "true" randomness, /dev/urandom is a CSPRNG that doesn't
175 // block under normal circumstances.
176 //
177 // The only time when /dev/urandom may conceivably block is right after boot,
178 // when the whole system is still low on entropy.  That's not something we can
179 // do anything about.
180 inline void CheckEntropy() {
181   for (;;) {
182     int status = RAND_status();
183     assert(status >= 0);  // Cannot fail.
184     if (status != 0)
185       break;
186     if (RAND_poll() == 0)  // Give up, RAND_poll() not supported.
187       break;
188   }
189 }
190
191
192 bool EntropySource(unsigned char* buffer, size_t length) {
193   // Ensure that OpenSSL's PRNG is properly seeded.
194   CheckEntropy();
195   // RAND_bytes() can return 0 to indicate that the entropy data is not truly
196   // random. That's okay, it's still better than V8's stock source of entropy,
197   // which is /dev/urandom on UNIX platforms and the current time on Windows.
198   return RAND_bytes(buffer, length) != -1;
199 }
200
201
202 void SecureContext::Initialize(Handle<Object> target) {
203   HandleScope scope;
204
205   Local<FunctionTemplate> t = FunctionTemplate::New(SecureContext::New);
206   secure_context_constructor = Persistent<FunctionTemplate>::New(t);
207
208   t->InstanceTemplate()->SetInternalFieldCount(1);
209   t->SetClassName(String::NewSymbol("SecureContext"));
210
211   NODE_SET_PROTOTYPE_METHOD(t, "init", SecureContext::Init);
212   NODE_SET_PROTOTYPE_METHOD(t, "setKey", SecureContext::SetKey);
213   NODE_SET_PROTOTYPE_METHOD(t, "setCert", SecureContext::SetCert);
214   NODE_SET_PROTOTYPE_METHOD(t, "addCACert", SecureContext::AddCACert);
215   NODE_SET_PROTOTYPE_METHOD(t, "addCRL", SecureContext::AddCRL);
216   NODE_SET_PROTOTYPE_METHOD(t, "addRootCerts", SecureContext::AddRootCerts);
217   NODE_SET_PROTOTYPE_METHOD(t, "setCiphers", SecureContext::SetCiphers);
218   NODE_SET_PROTOTYPE_METHOD(t, "setOptions", SecureContext::SetOptions);
219   NODE_SET_PROTOTYPE_METHOD(t, "setSessionIdContext",
220                                SecureContext::SetSessionIdContext);
221   NODE_SET_PROTOTYPE_METHOD(t, "close", SecureContext::Close);
222   NODE_SET_PROTOTYPE_METHOD(t, "loadPKCS12", SecureContext::LoadPKCS12);
223
224   target->Set(String::NewSymbol("SecureContext"), t->GetFunction());
225 }
226
227
228 Handle<Value> SecureContext::New(const Arguments& args) {
229   HandleScope scope;
230   SecureContext *p = new SecureContext();
231   p->Wrap(args.Holder());
232   return args.This();
233 }
234
235
236 Handle<Value> SecureContext::Init(const Arguments& args) {
237   HandleScope scope;
238
239   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
240
241   OPENSSL_CONST SSL_METHOD *method = SSLv23_method();
242
243   if (args.Length() == 1 && args[0]->IsString()) {
244     String::Utf8Value sslmethod(args[0]);
245
246     if (strcmp(*sslmethod, "SSLv2_method") == 0) {
247 #ifndef OPENSSL_NO_SSL2
248       method = SSLv2_method();
249 #else
250       return ThrowException(Exception::Error(String::New("SSLv2 methods disabled")));
251 #endif
252     } else if (strcmp(*sslmethod, "SSLv2_server_method") == 0) {
253 #ifndef OPENSSL_NO_SSL2
254       method = SSLv2_server_method();
255 #else
256       return ThrowException(Exception::Error(String::New("SSLv2 methods disabled")));
257 #endif
258     } else if (strcmp(*sslmethod, "SSLv2_client_method") == 0) {
259 #ifndef OPENSSL_NO_SSL2
260       method = SSLv2_client_method();
261 #else
262       return ThrowException(Exception::Error(String::New("SSLv2 methods disabled")));
263 #endif
264     } else if (strcmp(*sslmethod, "SSLv3_method") == 0) {
265       method = SSLv3_method();
266     } else if (strcmp(*sslmethod, "SSLv3_server_method") == 0) {
267       method = SSLv3_server_method();
268     } else if (strcmp(*sslmethod, "SSLv3_client_method") == 0) {
269       method = SSLv3_client_method();
270     } else if (strcmp(*sslmethod, "SSLv23_method") == 0) {
271       method = SSLv23_method();
272     } else if (strcmp(*sslmethod, "SSLv23_server_method") == 0) {
273       method = SSLv23_server_method();
274     } else if (strcmp(*sslmethod, "SSLv23_client_method") == 0) {
275       method = SSLv23_client_method();
276     } else if (strcmp(*sslmethod, "TLSv1_method") == 0) {
277       method = TLSv1_method();
278     } else if (strcmp(*sslmethod, "TLSv1_server_method") == 0) {
279       method = TLSv1_server_method();
280     } else if (strcmp(*sslmethod, "TLSv1_client_method") == 0) {
281       method = TLSv1_client_method();
282     } else {
283       return ThrowException(Exception::Error(String::New("Unknown method")));
284     }
285   }
286
287   sc->ctx_ = SSL_CTX_new(method);
288
289   // SSL session cache configuration
290   SSL_CTX_set_session_cache_mode(sc->ctx_,
291                                  SSL_SESS_CACHE_SERVER |
292                                  SSL_SESS_CACHE_NO_INTERNAL |
293                                  SSL_SESS_CACHE_NO_AUTO_CLEAR);
294   SSL_CTX_sess_set_get_cb(sc->ctx_, GetSessionCallback);
295   SSL_CTX_sess_set_new_cb(sc->ctx_, NewSessionCallback);
296
297   sc->ca_store_ = NULL;
298   return True();
299 }
300
301
302 SSL_SESSION* SecureContext::GetSessionCallback(SSL* s,
303                                                unsigned char* key,
304                                                int len,
305                                                int* copy) {
306   HandleScope scope;
307
308   Connection* p = static_cast<Connection*>(SSL_get_app_data(s));
309
310   *copy = 0;
311   SSL_SESSION* sess = p->next_sess_;
312   p->next_sess_ = NULL;
313
314   return sess;
315 }
316
317
318 void SessionDataFree(char* data, void* hint) {
319   delete[] data;
320 }
321
322
323 int SecureContext::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
324   HandleScope scope;
325
326   Connection* p = static_cast<Connection*>(SSL_get_app_data(s));
327
328   // Check if session is small enough to be stored
329   int size = i2d_SSL_SESSION(sess, NULL);
330   if (size > kMaxSessionSize) return 0;
331
332   // Serialize session
333   char* serialized = new char[size];
334   unsigned char* pserialized = reinterpret_cast<unsigned char*>(serialized);
335   memset(serialized, 0, size);
336   i2d_SSL_SESSION(sess, &pserialized);
337
338   Handle<Value> argv[2] = {
339     Buffer::New(reinterpret_cast<char*>(sess->session_id),
340                 sess->session_id_length)->handle_,
341     Buffer::New(serialized, size, SessionDataFree, NULL)->handle_
342   };
343
344   if (onnewsession_sym.IsEmpty()) {
345     onnewsession_sym = NODE_PSYMBOL("onnewsession");
346   }
347   MakeCallback(p->handle_, onnewsession_sym, ARRAY_SIZE(argv), argv);
348
349   return 0;
350 }
351
352
353 // Takes a string or buffer and loads it into a BIO.
354 // Caller responsible for BIO_free-ing the returned object.
355 static BIO* LoadBIO (Handle<Value> v) {
356   BIO *bio = BIO_new(BIO_s_mem());
357   if (!bio) return NULL;
358
359   HandleScope scope;
360
361   int r = -1;
362
363   if (v->IsString()) {
364     String::Utf8Value s(v);
365     r = BIO_write(bio, *s, s.length());
366   } else if (Buffer::HasInstance(v)) {
367     char* buffer_data = Buffer::Data(v);
368     size_t buffer_length = Buffer::Length(v);
369     r = BIO_write(bio, buffer_data, buffer_length);
370   }
371
372   if (r <= 0) {
373     BIO_free(bio);
374     return NULL;
375   }
376
377   return bio;
378 }
379
380
381 // Takes a string or buffer and loads it into an X509
382 // Caller responsible for X509_free-ing the returned object.
383 static X509* LoadX509 (Handle<Value> v) {
384   HandleScope scope; // necessary?
385
386   BIO *bio = LoadBIO(v);
387   if (!bio) return NULL;
388
389   X509 * x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
390   if (!x509) {
391     BIO_free(bio);
392     return NULL;
393   }
394
395   BIO_free(bio);
396   return x509;
397 }
398
399
400 Handle<Value> SecureContext::SetKey(const Arguments& args) {
401   HandleScope scope;
402
403   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
404
405   unsigned int len = args.Length();
406   if (len != 1 && len != 2) {
407     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
408   }
409   if (len == 2 && !args[1]->IsString()) {
410     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
411   }
412
413   BIO *bio = LoadBIO(args[0]);
414   if (!bio) return False();
415
416   String::Utf8Value passphrase(args[1]);
417
418   EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, NULL, NULL,
419                                           len == 1 ? NULL : *passphrase);
420
421   if (!key) {
422     BIO_free(bio);
423     unsigned long err = ERR_get_error();
424     if (!err) {
425       return ThrowException(Exception::Error(
426           String::New("PEM_read_bio_PrivateKey")));
427     }
428     return ThrowCryptoError(err);
429   }
430
431   SSL_CTX_use_PrivateKey(sc->ctx_, key);
432   EVP_PKEY_free(key);
433   BIO_free(bio);
434
435   return True();
436 }
437
438
439 // Read a file that contains our certificate in "PEM" format,
440 // possibly followed by a sequence of CA certificates that should be
441 // sent to the peer in the Certificate message.
442 //
443 // Taken from OpenSSL - editted for style.
444 int SSL_CTX_use_certificate_chain(SSL_CTX *ctx, BIO *in) {
445   int ret = 0;
446   X509 *x = NULL;
447
448   x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
449
450   if (x == NULL) {
451     SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
452     goto end;
453   }
454
455   ret = SSL_CTX_use_certificate(ctx, x);
456
457   if (ERR_peek_error() != 0) {
458     // Key/certificate mismatch doesn't imply ret==0 ...
459     ret = 0;
460   }
461
462   if (ret) {
463     // If we could set up our certificate, now proceed to
464     // the CA certificates.
465     X509 *ca;
466     int r;
467     unsigned long err;
468
469     if (ctx->extra_certs != NULL) {
470       sk_X509_pop_free(ctx->extra_certs, X509_free);
471       ctx->extra_certs = NULL;
472     }
473
474     while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
475       r = SSL_CTX_add_extra_chain_cert(ctx, ca);
476
477       if (!r) {
478         X509_free(ca);
479         ret = 0;
480         goto end;
481       }
482       // Note that we must not free r if it was successfully
483       // added to the chain (while we must free the main
484       // certificate, since its reference count is increased
485       // by SSL_CTX_use_certificate).
486     }
487
488     // When the while loop ends, it's usually just EOF.
489     err = ERR_peek_last_error();
490     if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
491         ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
492       ERR_clear_error();
493     } else  {
494       // some real error
495       ret = 0;
496     }
497   }
498
499 end:
500   if (x != NULL) X509_free(x);
501   return ret;
502 }
503
504
505 Handle<Value> SecureContext::SetCert(const Arguments& args) {
506   HandleScope scope;
507
508   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
509
510   if (args.Length() != 1) {
511     return ThrowException(Exception::TypeError(
512           String::New("Bad parameter")));
513   }
514
515   BIO* bio = LoadBIO(args[0]);
516   if (!bio) return False();
517
518   int rv = SSL_CTX_use_certificate_chain(sc->ctx_, bio);
519
520   BIO_free(bio);
521
522   if (!rv) {
523     unsigned long err = ERR_get_error();
524     if (!err) {
525       return ThrowException(Exception::Error(
526           String::New("SSL_CTX_use_certificate_chain")));
527     }
528     return ThrowCryptoError(err);
529   }
530
531   return True();
532 }
533
534
535 Handle<Value> SecureContext::AddCACert(const Arguments& args) {
536   bool newCAStore = false;
537   HandleScope scope;
538
539   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
540
541   if (args.Length() != 1) {
542     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
543   }
544
545   if (!sc->ca_store_) {
546     sc->ca_store_ = X509_STORE_new();
547     newCAStore = true;
548   }
549
550   X509* x509 = LoadX509(args[0]);
551   if (!x509) return False();
552
553   X509_STORE_add_cert(sc->ca_store_, x509);
554   SSL_CTX_add_client_CA(sc->ctx_, x509);
555
556   X509_free(x509);
557
558   if (newCAStore) {
559     SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
560   }
561
562   return True();
563 }
564
565
566 Handle<Value> SecureContext::AddCRL(const Arguments& args) {
567   HandleScope scope;
568
569   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
570
571   if (args.Length() != 1) {
572     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
573   }
574
575   BIO *bio = LoadBIO(args[0]);
576   if (!bio) return False();
577
578   X509_CRL *x509 = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
579
580   if (x509 == NULL) {
581     BIO_free(bio);
582     return False();
583   }
584
585   X509_STORE_add_crl(sc->ca_store_, x509);
586
587   X509_STORE_set_flags(sc->ca_store_, X509_V_FLAG_CRL_CHECK |
588                                       X509_V_FLAG_CRL_CHECK_ALL);
589
590   BIO_free(bio);
591   X509_CRL_free(x509);
592
593   return True();
594 }
595
596
597
598 Handle<Value> SecureContext::AddRootCerts(const Arguments& args) {
599   HandleScope scope;
600
601   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
602
603   assert(sc->ca_store_ == NULL);
604
605   if (!root_cert_store) {
606     root_cert_store = X509_STORE_new();
607
608     for (int i = 0; root_certs[i]; i++) {
609       BIO *bp = BIO_new(BIO_s_mem());
610
611       if (!BIO_write(bp, root_certs[i], strlen(root_certs[i]))) {
612         BIO_free(bp);
613         return False();
614       }
615
616       X509 *x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL);
617
618       if (x509 == NULL) {
619         BIO_free(bp);
620         return False();
621       }
622
623       X509_STORE_add_cert(root_cert_store, x509);
624
625       BIO_free(bp);
626       X509_free(x509);
627     }
628   }
629
630   sc->ca_store_ = root_cert_store;
631   SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
632
633   return True();
634 }
635
636
637 Handle<Value> SecureContext::SetCiphers(const Arguments& args) {
638   HandleScope scope;
639
640   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
641
642   if (args.Length() != 1 || !args[0]->IsString()) {
643     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
644   }
645
646   String::Utf8Value ciphers(args[0]);
647   SSL_CTX_set_cipher_list(sc->ctx_, *ciphers);
648
649   return True();
650 }
651
652 Handle<Value> SecureContext::SetOptions(const Arguments& args) {
653   HandleScope scope;
654
655   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
656
657   if (args.Length() != 1 || !args[0]->IntegerValue()) {
658     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
659   }
660
661   SSL_CTX_set_options(sc->ctx_, args[0]->IntegerValue());
662
663   return True();
664 }
665
666 Handle<Value> SecureContext::SetSessionIdContext(const Arguments& args) {
667   HandleScope scope;
668
669   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
670
671   if (args.Length() != 1 || !args[0]->IsString()) {
672     return ThrowException(Exception::TypeError(String::New("Bad parameter")));
673   }
674
675   String::Utf8Value sessionIdContext(args[0]);
676   const unsigned char* sid_ctx = (const unsigned char*) *sessionIdContext;
677   unsigned int sid_ctx_len = sessionIdContext.length();
678
679   int r = SSL_CTX_set_session_id_context(sc->ctx_, sid_ctx, sid_ctx_len);
680   if (r != 1) {
681     Local<String> message;
682     BIO* bio;
683     BUF_MEM* mem;
684     if ((bio = BIO_new(BIO_s_mem()))) {
685       ERR_print_errors(bio);
686       BIO_get_mem_ptr(bio, &mem);
687       message = String::New(mem->data, mem->length);
688       BIO_free(bio);
689     } else {
690       message = String::New("SSL_CTX_set_session_id_context error");
691     }
692     return ThrowException(Exception::TypeError(message));
693   }
694
695   return True();
696 }
697
698 Handle<Value> SecureContext::Close(const Arguments& args) {
699   HandleScope scope;
700   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
701   sc->FreeCTXMem();
702   return False();
703 }
704
705 //Takes .pfx or .p12 and password in string or buffer format
706 Handle<Value> SecureContext::LoadPKCS12(const Arguments& args) {
707   HandleScope scope;
708
709   BIO* in = NULL;
710   PKCS12* p12 = NULL;
711   EVP_PKEY* pkey = NULL;
712   X509* cert = NULL;
713   STACK_OF(X509)* extraCerts = NULL;
714   char* pass = NULL;
715   bool ret = false;
716
717   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder());
718
719   if (args.Length() < 1) {
720     return ThrowException(Exception::TypeError(
721           String::New("Bad parameter")));
722   }
723
724   in = LoadBIO(args[0]);
725   if (in == NULL) {
726     return ThrowException(Exception::Error(
727           String::New("Unable to load BIO")));
728   }
729
730   if (args.Length() >= 2) {
731     ASSERT_IS_BUFFER(args[1]);
732
733     int passlen = Buffer::Length(args[1]);
734     if (passlen < 0) {
735       BIO_free(in);
736       return ThrowException(Exception::TypeError(
737             String::New("Bad password")));
738     }
739     pass = new char[passlen + 1];
740     int pass_written = DecodeWrite(pass, passlen, args[1], BINARY);
741
742     assert(pass_written == passlen);
743     pass[passlen] = '\0';
744   }
745
746   if (d2i_PKCS12_bio(in, &p12) &&
747       PKCS12_parse(p12, pass, &pkey, &cert, &extraCerts) &&
748       SSL_CTX_use_certificate(sc->ctx_, cert) &&
749       SSL_CTX_use_PrivateKey(sc->ctx_, pkey))
750   {
751     // set extra certs
752     while (X509* x509 = sk_X509_pop(extraCerts)) {
753       if (!sc->ca_store_) {
754         sc->ca_store_ = X509_STORE_new();
755         SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
756       }
757
758       X509_STORE_add_cert(sc->ca_store_, x509);
759       SSL_CTX_add_client_CA(sc->ctx_, x509);
760       X509_free(x509);
761     }
762
763     EVP_PKEY_free(pkey);
764     X509_free(cert);
765     sk_X509_free(extraCerts);
766
767     ret = true;
768   }
769
770   PKCS12_free(p12);
771   BIO_free(in);
772   delete[] pass;
773
774   if (!ret) {
775     unsigned long err = ERR_get_error();
776     const char* str = ERR_reason_error_string(err);
777     return ThrowException(Exception::Error(String::New(str)));
778   }
779
780   return True();
781 }
782
783
784 size_t ClientHelloParser::Write(const uint8_t* data, size_t len) {
785   HandleScope scope;
786
787   // Just accumulate data, everything will be pushed to BIO later
788   if (state_ == kPaused) return 0;
789
790   // Copy incoming data to the internal buffer
791   // (which has a size of the biggest possible TLS frame)
792   size_t available = sizeof(data_) - offset_;
793   size_t copied = len < available ? len : available;
794   memcpy(data_ + offset_, data, copied);
795   offset_ += copied;
796
797   // Vars for parsing hello
798   bool is_clienthello = false;
799   uint8_t session_size = -1;
800   uint8_t* session_id = NULL;
801   Local<Object> hello;
802   Handle<Value> argv[1];
803
804   switch (state_) {
805    case kWaiting:
806     // >= 5 bytes for header parsing
807     if (offset_ < 5) break;
808
809     if (data_[0] == kChangeCipherSpec || data_[0] == kAlert ||
810         data_[0] == kHandshake || data_[0] == kApplicationData) {
811       frame_len_ = (data_[3] << 8) + data_[4];
812       state_ = kTLSHeader;
813       body_offset_ = 5;
814     } else {
815       frame_len_ = (data_[0] << 8) + data_[1];
816       state_ = kSSLHeader;
817       if (*data_ & 0x40) {
818         // header with padding
819         body_offset_ = 3;
820       } else {
821         // without padding
822         body_offset_ = 2;
823       }
824     }
825
826     // Sanity check (too big frame, or too small)
827     if (frame_len_ >= sizeof(data_)) {
828       // Let OpenSSL handle it
829       Finish();
830       return copied;
831     }
832    case kTLSHeader:
833    case kSSLHeader:
834     // >= 5 + frame size bytes for frame parsing
835     if (offset_ < body_offset_ + frame_len_) break;
836
837     // Skip unsupported frames and gather some data from frame
838
839     // TODO: Check protocol version
840     if (data_[body_offset_] == kClientHello) {
841       is_clienthello = true;
842       uint8_t* body;
843       size_t session_offset;
844
845       if (state_ == kTLSHeader) {
846         // Skip frame header, hello header, protocol version and random data
847         session_offset = body_offset_ + 4 + 2 + 32;
848
849         if (session_offset + 1 < offset_) {
850           body = data_ + session_offset;
851           session_size = *body;
852           session_id = body + 1;
853         }
854       } else if (state_ == kSSLHeader) {
855         // Skip header, version
856         session_offset = body_offset_ + 3;
857
858         if (session_offset + 4 < offset_) {
859           body = data_ + session_offset;
860
861           int ciphers_size = (body[0] << 8) + body[1];
862
863           if (body + 4 + ciphers_size < data_ + offset_) {
864             session_size = (body[2] << 8) + body[3];
865             session_id = body + 4 + ciphers_size;
866           }
867         }
868       } else {
869         // Whoa? How did we get here?
870         abort();
871       }
872
873       // Check if we overflowed (do not reply with any private data)
874       if (session_id == NULL ||
875           session_size > 32 ||
876           session_id + session_size > data_ + offset_) {
877         Finish();
878         return copied;
879       }
880
881       // TODO: Parse other things?
882     }
883
884     // Not client hello - let OpenSSL handle it
885     if (!is_clienthello) {
886       Finish();
887       return copied;
888     }
889
890     // Parse frame, call javascript handler and
891     // move parser into the paused state
892     if (onclienthello_sym.IsEmpty()) {
893       onclienthello_sym = NODE_PSYMBOL("onclienthello");
894     }
895     if (sessionid_sym.IsEmpty()) {
896       sessionid_sym = NODE_PSYMBOL("sessionId");
897     }
898
899     state_ = kPaused;
900     hello = Object::New();
901     hello->Set(sessionid_sym,
902                Buffer::New(reinterpret_cast<char*>(session_id),
903                            session_size)->handle_);
904
905     argv[0] = hello;
906     MakeCallback(conn_->handle_, onclienthello_sym, 1, argv);
907     break;
908    case kEnded:
909    default:
910     break;
911   }
912
913   return copied;
914 }
915
916
917 void ClientHelloParser::Finish() {
918   assert(state_ != kEnded);
919   state_ = kEnded;
920
921   // Write all accumulated data
922   int r = BIO_write(conn_->bio_read_, reinterpret_cast<char*>(data_), offset_);
923   conn_->HandleBIOError(conn_->bio_read_, "BIO_write", r);
924   conn_->SetShutdownFlags();
925 }
926
927
928 #ifdef SSL_PRINT_DEBUG
929 # define DEBUG_PRINT(...) fprintf (stderr, __VA_ARGS__)
930 #else
931 # define DEBUG_PRINT(...)
932 #endif
933
934
935 int Connection::HandleBIOError(BIO *bio, const char* func, int rv) {
936   if (rv >= 0) return rv;
937
938   int retry = BIO_should_retry(bio);
939   (void) retry; // unused if !defined(SSL_PRINT_DEBUG)
940
941   if (BIO_should_write(bio)) {
942     DEBUG_PRINT("[%p] BIO: %s want write. should retry %d\n", ssl_, func, retry);
943     return 0;
944
945   } else if (BIO_should_read(bio)) {
946     DEBUG_PRINT("[%p] BIO: %s want read. should retry %d\n", ssl_, func, retry);
947     return 0;
948
949   } else {
950    static char ssl_error_buf[512];
951     ERR_error_string_n(rv, ssl_error_buf, sizeof(ssl_error_buf));
952
953     HandleScope scope;
954     Local<Value> e = Exception::Error(String::New(ssl_error_buf));
955     handle_->Set(String::New("error"), e);
956
957     DEBUG_PRINT("[%p] BIO: %s failed: (%d) %s\n", ssl_, func, rv, ssl_error_buf);
958
959     return rv;
960   }
961
962   return 0;
963 }
964
965
966 int Connection::HandleSSLError(const char* func,
967                                int rv,
968                                ZeroStatus zs,
969                                SyscallStatus ss) {
970   ClearErrorOnReturn clear_error_on_return;
971   (void) &clear_error_on_return;  // Silence unused variable warning.
972
973   if (rv > 0) return rv;
974   if ((rv == 0) && (zs == kZeroIsNotAnError)) return rv;
975
976   int err = SSL_get_error(ssl_, rv);
977
978   if (err == SSL_ERROR_NONE) {
979     return 0;
980
981   } else if (err == SSL_ERROR_WANT_WRITE) {
982     DEBUG_PRINT("[%p] SSL: %s want write\n", ssl_, func);
983     return 0;
984
985   } else if (err == SSL_ERROR_WANT_READ) {
986     DEBUG_PRINT("[%p] SSL: %s want read\n", ssl_, func);
987     return 0;
988
989   } else if (err == SSL_ERROR_ZERO_RETURN) {
990     handle_->Set(String::New("error"),
991                  Exception::Error(String::New("ZERO_RETURN")));
992     return rv;
993
994   } else if ((err == SSL_ERROR_SYSCALL) && (ss == kIgnoreSyscall)) {
995     return 0;
996
997   } else {
998     HandleScope scope;
999     BUF_MEM* mem;
1000     BIO *bio;
1001
1002     assert(err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL);
1003
1004     // XXX We need to drain the error queue for this thread or else OpenSSL
1005     // has the possibility of blocking connections? This problem is not well
1006     // understood. And we should be somehow propagating these errors up
1007     // into JavaScript. There is no test which demonstrates this problem.
1008     // https://github.com/joyent/node/issues/1719
1009     if ((bio = BIO_new(BIO_s_mem()))) {
1010       ERR_print_errors(bio);
1011       BIO_get_mem_ptr(bio, &mem);
1012       Local<Value> e = Exception::Error(String::New(mem->data, mem->length));
1013       handle_->Set(String::New("error"), e);
1014       BIO_free(bio);
1015     }
1016
1017     return rv;
1018   }
1019
1020   return 0;
1021 }
1022
1023
1024 void Connection::ClearError() {
1025 #ifndef NDEBUG
1026   HandleScope scope;
1027
1028   // We should clear the error in JS-land
1029   assert(handle_->Get(String::New("error"))->BooleanValue() == false);
1030 #endif // NDEBUG
1031 }
1032
1033
1034 void Connection::SetShutdownFlags() {
1035   HandleScope scope;
1036
1037   int flags = SSL_get_shutdown(ssl_);
1038
1039   if (flags & SSL_SENT_SHUTDOWN) {
1040     handle_->Set(String::New("sentShutdown"), True());
1041   }
1042
1043   if (flags & SSL_RECEIVED_SHUTDOWN) {
1044     handle_->Set(String::New("receivedShutdown"), True());
1045   }
1046 }
1047
1048
1049 void Connection::Initialize(Handle<Object> target) {
1050   HandleScope scope;
1051
1052   Local<FunctionTemplate> t = FunctionTemplate::New(Connection::New);
1053   t->InstanceTemplate()->SetInternalFieldCount(1);
1054   t->SetClassName(String::NewSymbol("Connection"));
1055
1056   NODE_SET_PROTOTYPE_METHOD(t, "encIn", Connection::EncIn);
1057   NODE_SET_PROTOTYPE_METHOD(t, "clearOut", Connection::ClearOut);
1058   NODE_SET_PROTOTYPE_METHOD(t, "clearIn", Connection::ClearIn);
1059   NODE_SET_PROTOTYPE_METHOD(t, "encOut", Connection::EncOut);
1060   NODE_SET_PROTOTYPE_METHOD(t, "clearPending", Connection::ClearPending);
1061   NODE_SET_PROTOTYPE_METHOD(t, "encPending", Connection::EncPending);
1062   NODE_SET_PROTOTYPE_METHOD(t, "getPeerCertificate", Connection::GetPeerCertificate);
1063   NODE_SET_PROTOTYPE_METHOD(t, "getSession", Connection::GetSession);
1064   NODE_SET_PROTOTYPE_METHOD(t, "setSession", Connection::SetSession);
1065   NODE_SET_PROTOTYPE_METHOD(t, "loadSession", Connection::LoadSession);
1066   NODE_SET_PROTOTYPE_METHOD(t, "isSessionReused", Connection::IsSessionReused);
1067   NODE_SET_PROTOTYPE_METHOD(t, "isInitFinished", Connection::IsInitFinished);
1068   NODE_SET_PROTOTYPE_METHOD(t, "verifyError", Connection::VerifyError);
1069   NODE_SET_PROTOTYPE_METHOD(t, "getCurrentCipher", Connection::GetCurrentCipher);
1070   NODE_SET_PROTOTYPE_METHOD(t, "start", Connection::Start);
1071   NODE_SET_PROTOTYPE_METHOD(t, "shutdown", Connection::Shutdown);
1072   NODE_SET_PROTOTYPE_METHOD(t, "close", Connection::Close);
1073
1074 #ifdef OPENSSL_NPN_NEGOTIATED
1075   NODE_SET_PROTOTYPE_METHOD(t, "getNegotiatedProtocol", Connection::GetNegotiatedProto);
1076   NODE_SET_PROTOTYPE_METHOD(t, "setNPNProtocols", Connection::SetNPNProtocols);
1077 #endif
1078
1079
1080 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
1081   NODE_SET_PROTOTYPE_METHOD(t, "getServername", Connection::GetServername);
1082   NODE_SET_PROTOTYPE_METHOD(t, "setSNICallback",  Connection::SetSNICallback);
1083 #endif
1084
1085   target->Set(String::NewSymbol("Connection"), t->GetFunction());
1086 }
1087
1088
1089 static int VerifyCallback(int preverify_ok, X509_STORE_CTX *ctx) {
1090   // Quoting SSL_set_verify(3ssl):
1091   //
1092   //   The VerifyCallback function is used to control the behaviour when
1093   //   the SSL_VERIFY_PEER flag is set. It must be supplied by the
1094   //   application and receives two arguments: preverify_ok indicates,
1095   //   whether the verification of the certificate in question was passed
1096   //   (preverify_ok=1) or not (preverify_ok=0). x509_ctx is a pointer to
1097   //   the complete context used for the certificate chain verification.
1098   //
1099   //   The certificate chain is checked starting with the deepest nesting
1100   //   level (the root CA certificate) and worked upward to the peer's
1101   //   certificate.  At each level signatures and issuer attributes are
1102   //   checked.  Whenever a verification error is found, the error number is
1103   //   stored in x509_ctx and VerifyCallback is called with preverify_ok=0.
1104   //   By applying X509_CTX_store_* functions VerifyCallback can locate the
1105   //   certificate in question and perform additional steps (see EXAMPLES).
1106   //   If no error is found for a certificate, VerifyCallback is called
1107   //   with preverify_ok=1 before advancing to the next level.
1108   //
1109   //   The return value of VerifyCallback controls the strategy of the
1110   //   further verification process. If VerifyCallback returns 0, the
1111   //   verification process is immediately stopped with "verification
1112   //   failed" state. If SSL_VERIFY_PEER is set, a verification failure
1113   //   alert is sent to the peer and the TLS/SSL handshake is terminated. If
1114   //   VerifyCallback returns 1, the verification process is continued. If
1115   //   VerifyCallback always returns 1, the TLS/SSL handshake will not be
1116   //   terminated with respect to verification failures and the connection
1117   //   will be established. The calling process can however retrieve the
1118   //   error code of the last verification error using
1119   //   SSL_get_verify_result(3) or by maintaining its own error storage
1120   //   managed by VerifyCallback.
1121   //
1122   //   If no VerifyCallback is specified, the default callback will be
1123   //   used.  Its return value is identical to preverify_ok, so that any
1124   //   verification failure will lead to a termination of the TLS/SSL
1125   //   handshake with an alert message, if SSL_VERIFY_PEER is set.
1126   //
1127   // Since we cannot perform I/O quickly enough in this callback, we ignore
1128   // all preverify_ok errors and let the handshake continue. It is
1129   // imparative that the user use Connection::VerifyError after the
1130   // 'secure' callback has been made.
1131   return 1;
1132 }
1133
1134 #ifdef OPENSSL_NPN_NEGOTIATED
1135
1136 int Connection::AdvertiseNextProtoCallback_(SSL *s,
1137                                             const unsigned char** data,
1138                                             unsigned int *len,
1139                                             void *arg) {
1140
1141   Connection *p = static_cast<Connection*>(SSL_get_app_data(s));
1142
1143   if (p->npnProtos_.IsEmpty()) {
1144     // No initialization - no NPN protocols
1145     *data = reinterpret_cast<const unsigned char*>("");
1146     *len = 0;
1147   } else {
1148     *data = reinterpret_cast<const unsigned char*>(Buffer::Data(p->npnProtos_));
1149     *len = Buffer::Length(p->npnProtos_);
1150   }
1151
1152   return SSL_TLSEXT_ERR_OK;
1153 }
1154
1155 int Connection::SelectNextProtoCallback_(SSL *s,
1156                              unsigned char** out, unsigned char* outlen,
1157                              const unsigned char* in,
1158                              unsigned int inlen, void *arg) {
1159   Connection *p = static_cast<Connection*> SSL_get_app_data(s);
1160
1161   // Release old protocol handler if present
1162   if (!p->selectedNPNProto_.IsEmpty()) {
1163     p->selectedNPNProto_.Dispose();
1164   }
1165
1166   if (p->npnProtos_.IsEmpty()) {
1167     // We should at least select one protocol
1168     // If server is using NPN
1169     *out = reinterpret_cast<unsigned char*>(const_cast<char*>("http/1.1"));
1170     *outlen = 8;
1171
1172     // set status unsupported
1173     p->selectedNPNProto_ = Persistent<Value>::New(False());
1174
1175     return SSL_TLSEXT_ERR_OK;
1176   }
1177
1178   const unsigned char* npnProtos =
1179       reinterpret_cast<const unsigned char*>(Buffer::Data(p->npnProtos_));
1180
1181   int status = SSL_select_next_proto(out, outlen, in, inlen, npnProtos,
1182                                      Buffer::Length(p->npnProtos_));
1183
1184   switch (status) {
1185     case OPENSSL_NPN_UNSUPPORTED:
1186       p->selectedNPNProto_ = Persistent<Value>::New(Null());
1187       break;
1188     case OPENSSL_NPN_NEGOTIATED:
1189       p->selectedNPNProto_ = Persistent<Value>::New(String::New(
1190                                  reinterpret_cast<const char*>(*out), *outlen
1191                              ));
1192       break;
1193     case OPENSSL_NPN_NO_OVERLAP:
1194       p->selectedNPNProto_ = Persistent<Value>::New(False());
1195       break;
1196     default:
1197       break;
1198   }
1199
1200   return SSL_TLSEXT_ERR_OK;
1201 }
1202 #endif
1203
1204 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
1205 int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
1206   HandleScope scope;
1207
1208   Connection *p = static_cast<Connection*> SSL_get_app_data(s);
1209
1210   const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
1211
1212   if (servername) {
1213     if (!p->servername_.IsEmpty()) {
1214       p->servername_.Dispose();
1215     }
1216     p->servername_ = Persistent<String>::New(String::New(servername));
1217
1218     // Call the SNI callback and use its return value as context
1219     if (!p->sniObject_.IsEmpty()) {
1220       if (!p->sniContext_.IsEmpty()) {
1221         p->sniContext_.Dispose();
1222       }
1223
1224       // Get callback init args
1225       Local<Value> argv[1] = {*p->servername_};
1226
1227       // Call it
1228       Local<Value> ret = Local<Value>::New(MakeCallback(p->sniObject_,
1229                                                         "onselect",
1230                                                         ARRAY_SIZE(argv),
1231                                                         argv));
1232
1233       // If ret is SecureContext
1234       if (secure_context_constructor->HasInstance(ret)) {
1235         p->sniContext_ = Persistent<Value>::New(ret);
1236         SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(
1237                                 Local<Object>::Cast(ret));
1238         p->InitNPN(sc, true);
1239         SSL_set_SSL_CTX(s, sc->ctx_);
1240       } else {
1241         return SSL_TLSEXT_ERR_NOACK;
1242       }
1243     }
1244   }
1245
1246   return SSL_TLSEXT_ERR_OK;
1247 }
1248 #endif
1249
1250 Handle<Value> Connection::New(const Arguments& args) {
1251   HandleScope scope;
1252
1253   Connection *p = new Connection();
1254   p->Wrap(args.Holder());
1255
1256   if (args.Length() < 1 || !args[0]->IsObject()) {
1257     return ThrowException(Exception::Error(String::New(
1258       "First argument must be a crypto module Credentials")));
1259   }
1260
1261   SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args[0]->ToObject());
1262
1263   bool is_server = args[1]->BooleanValue();
1264
1265   p->ssl_ = SSL_new(sc->ctx_);
1266   p->bio_read_ = BIO_new(BIO_s_mem());
1267   p->bio_write_ = BIO_new(BIO_s_mem());
1268
1269   SSL_set_app_data(p->ssl_, p);
1270
1271   if (is_server) SSL_set_info_callback(p->ssl_, SSLInfoCallback);
1272
1273   p->InitNPN(sc, is_server);
1274
1275 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
1276   if (is_server) {
1277     SSL_CTX_set_tlsext_servername_callback(sc->ctx_, SelectSNIContextCallback_);
1278   } else {
1279     String::Utf8Value servername(args[2]);
1280     SSL_set_tlsext_host_name(p->ssl_, *servername);
1281   }
1282 #endif
1283
1284   SSL_set_bio(p->ssl_, p->bio_read_, p->bio_write_);
1285
1286 #ifdef SSL_MODE_RELEASE_BUFFERS
1287   long mode = SSL_get_mode(p->ssl_);
1288   SSL_set_mode(p->ssl_, mode | SSL_MODE_RELEASE_BUFFERS);
1289 #endif
1290
1291
1292   int verify_mode;
1293   if (is_server) {
1294     bool request_cert = args[2]->BooleanValue();
1295     if (!request_cert) {
1296       // Note reject_unauthorized ignored.
1297       verify_mode = SSL_VERIFY_NONE;
1298     } else {
1299       bool reject_unauthorized = args[3]->BooleanValue();
1300       verify_mode = SSL_VERIFY_PEER;
1301       if (reject_unauthorized) verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1302     }
1303   } else {
1304     // Note request_cert and reject_unauthorized are ignored for clients.
1305     verify_mode = SSL_VERIFY_NONE;
1306   }
1307
1308
1309   // Always allow a connection. We'll reject in javascript.
1310   SSL_set_verify(p->ssl_, verify_mode, VerifyCallback);
1311
1312   if ((p->is_server_ = is_server)) {
1313     SSL_set_accept_state(p->ssl_);
1314   } else {
1315     SSL_set_connect_state(p->ssl_);
1316   }
1317
1318   return args.This();
1319 }
1320
1321
1322 void Connection::SSLInfoCallback(const SSL *ssl_, int where, int ret) {
1323   // Be compatible with older versions of OpenSSL. SSL_get_app_data() wants
1324   // a non-const SSL* in OpenSSL <= 0.9.7e.
1325   SSL* ssl = const_cast<SSL*>(ssl_);
1326   if (where & SSL_CB_HANDSHAKE_START) {
1327     HandleScope scope;
1328     Connection* c = static_cast<Connection*>(SSL_get_app_data(ssl));
1329     if (onhandshakestart_sym.IsEmpty()) {
1330       onhandshakestart_sym = NODE_PSYMBOL("onhandshakestart");
1331     }
1332     MakeCallback(c->handle_, onhandshakestart_sym, 0, NULL);
1333   }
1334   if (where & SSL_CB_HANDSHAKE_DONE) {
1335     HandleScope scope;
1336     Connection* c = static_cast<Connection*>(SSL_get_app_data(ssl));
1337     if (onhandshakedone_sym.IsEmpty()) {
1338       onhandshakedone_sym = NODE_PSYMBOL("onhandshakedone");
1339     }
1340     MakeCallback(c->handle_, onhandshakedone_sym, 0, NULL);
1341   }
1342 }
1343
1344
1345 Handle<Value> Connection::EncIn(const Arguments& args) {
1346   HandleScope scope;
1347
1348   Connection *ss = Connection::Unwrap(args);
1349
1350   if (args.Length() < 3) {
1351     return ThrowException(Exception::TypeError(
1352           String::New("Takes 3 parameters")));
1353   }
1354
1355   if (!Buffer::HasInstance(args[0])) {
1356     return ThrowException(Exception::TypeError(
1357           String::New("Second argument should be a buffer")));
1358   }
1359
1360   char* buffer_data = Buffer::Data(args[0]);
1361   size_t buffer_length = Buffer::Length(args[0]);
1362
1363   size_t off = args[1]->Int32Value();
1364   size_t len = args[2]->Int32Value();
1365   if (!Buffer::IsWithinBounds(off, len, buffer_length)) {
1366     return ThrowException(Exception::Error(
1367           String::New("off + len > buffer.length")));
1368   }
1369
1370   int bytes_written;
1371   char* data = buffer_data + off;
1372
1373   if (ss->is_server_ && !ss->hello_parser_.ended()) {
1374     bytes_written = ss->hello_parser_.Write(reinterpret_cast<uint8_t*>(data),
1375                                             len);
1376   } else {
1377     bytes_written = BIO_write(ss->bio_read_, data, len);
1378     ss->HandleBIOError(ss->bio_read_, "BIO_write", bytes_written);
1379     ss->SetShutdownFlags();
1380   }
1381
1382   return scope.Close(Integer::New(bytes_written));
1383 }
1384
1385
1386 Handle<Value> Connection::ClearOut(const Arguments& args) {
1387   HandleScope scope;
1388
1389   Connection *ss = Connection::Unwrap(args);
1390
1391   if (args.Length() < 3) {
1392     return ThrowException(Exception::TypeError(
1393           String::New("Takes 3 parameters")));
1394   }
1395
1396   if (!Buffer::HasInstance(args[0])) {
1397     return ThrowException(Exception::TypeError(
1398           String::New("Second argument should be a buffer")));
1399   }
1400
1401   char* buffer_data = Buffer::Data(args[0]);
1402   size_t buffer_length = Buffer::Length(args[0]);
1403
1404   size_t off = args[1]->Int32Value();
1405   size_t len = args[2]->Int32Value();
1406   if (!Buffer::IsWithinBounds(off, len, buffer_length)) {
1407     return ThrowException(Exception::Error(
1408           String::New("off + len > buffer.length")));
1409   }
1410
1411   if (!SSL_is_init_finished(ss->ssl_)) {
1412     int rv;
1413
1414     if (ss->is_server_) {
1415       rv = SSL_accept(ss->ssl_);
1416       ss->HandleSSLError("SSL_accept:ClearOut",
1417                          rv,
1418                          kZeroIsAnError,
1419                          kSyscallError);
1420     } else {
1421       rv = SSL_connect(ss->ssl_);
1422       ss->HandleSSLError("SSL_connect:ClearOut",
1423                          rv,
1424                          kZeroIsAnError,
1425                          kSyscallError);
1426     }
1427
1428     if (rv < 0) return scope.Close(Integer::New(rv));
1429   }
1430
1431   int bytes_read = SSL_read(ss->ssl_, buffer_data + off, len);
1432   ss->HandleSSLError("SSL_read:ClearOut",
1433                      bytes_read,
1434                      kZeroIsNotAnError,
1435                      kSyscallError);
1436   ss->SetShutdownFlags();
1437
1438   return scope.Close(Integer::New(bytes_read));
1439 }
1440
1441
1442 Handle<Value> Connection::ClearPending(const Arguments& args) {
1443   HandleScope scope;
1444
1445   Connection *ss = Connection::Unwrap(args);
1446
1447   int bytes_pending = BIO_pending(ss->bio_read_);
1448   return scope.Close(Integer::New(bytes_pending));
1449 }
1450
1451
1452 Handle<Value> Connection::EncPending(const Arguments& args) {
1453   HandleScope scope;
1454
1455   Connection *ss = Connection::Unwrap(args);
1456
1457   int bytes_pending = BIO_pending(ss->bio_write_);
1458   return scope.Close(Integer::New(bytes_pending));
1459 }
1460
1461
1462 Handle<Value> Connection::EncOut(const Arguments& args) {
1463   HandleScope scope;
1464
1465   Connection *ss = Connection::Unwrap(args);
1466
1467   if (args.Length() < 3) {
1468     return ThrowException(Exception::TypeError(
1469           String::New("Takes 3 parameters")));
1470   }
1471
1472   if (!Buffer::HasInstance(args[0])) {
1473     return ThrowException(Exception::TypeError(
1474           String::New("Second argument should be a buffer")));
1475   }
1476
1477   char* buffer_data = Buffer::Data(args[0]);
1478   size_t buffer_length = Buffer::Length(args[0]);
1479
1480   size_t off = args[1]->Int32Value();
1481   size_t len = args[2]->Int32Value();
1482   if (!Buffer::IsWithinBounds(off, len, buffer_length)) {
1483     return ThrowException(Exception::Error(
1484           String::New("off + len > buffer.length")));
1485   }
1486
1487   int bytes_read = BIO_read(ss->bio_write_, buffer_data + off, len);
1488
1489   ss->HandleBIOError(ss->bio_write_, "BIO_read:EncOut", bytes_read);
1490   ss->SetShutdownFlags();
1491
1492   return scope.Close(Integer::New(bytes_read));
1493 }
1494
1495
1496 Handle<Value> Connection::ClearIn(const Arguments& args) {
1497   HandleScope scope;
1498
1499   Connection *ss = Connection::Unwrap(args);
1500
1501   if (args.Length() < 3) {
1502     return ThrowException(Exception::TypeError(
1503           String::New("Takes 3 parameters")));
1504   }
1505
1506   if (!Buffer::HasInstance(args[0])) {
1507     return ThrowException(Exception::TypeError(
1508           String::New("Second argument should be a buffer")));
1509   }
1510
1511   char* buffer_data = Buffer::Data(args[0]);
1512   size_t buffer_length = Buffer::Length(args[0]);
1513
1514   size_t off = args[1]->Int32Value();
1515   size_t len = args[2]->Int32Value();
1516   if (!Buffer::IsWithinBounds(off, len, buffer_length)) {
1517     return ThrowException(Exception::Error(
1518           String::New("off + len > buffer.length")));
1519   }
1520
1521   if (!SSL_is_init_finished(ss->ssl_)) {
1522     int rv;
1523     if (ss->is_server_) {
1524       rv = SSL_accept(ss->ssl_);
1525       ss->HandleSSLError("SSL_accept:ClearIn",
1526                          rv,
1527                          kZeroIsAnError,
1528                          kSyscallError);
1529     } else {
1530       rv = SSL_connect(ss->ssl_);
1531       ss->HandleSSLError("SSL_connect:ClearIn",
1532                          rv,
1533                          kZeroIsAnError,
1534                          kSyscallError);
1535     }
1536
1537     if (rv < 0) return scope.Close(Integer::New(rv));
1538   }
1539
1540   int bytes_written = SSL_write(ss->ssl_, buffer_data + off, len);
1541
1542   ss->HandleSSLError("SSL_write:ClearIn",
1543                      bytes_written,
1544                      len == 0 ? kZeroIsNotAnError : kZeroIsAnError,
1545                      kSyscallError);
1546   ss->SetShutdownFlags();
1547
1548   return scope.Close(Integer::New(bytes_written));
1549 }
1550
1551
1552 Handle<Value> Connection::GetPeerCertificate(const Arguments& args) {
1553   HandleScope scope;
1554
1555   Connection *ss = Connection::Unwrap(args);
1556
1557   if (ss->ssl_ == NULL) return Undefined();
1558   Local<Object> info = Object::New();
1559   X509* peer_cert = SSL_get_peer_certificate(ss->ssl_);
1560   if (peer_cert != NULL) {
1561     BIO* bio = BIO_new(BIO_s_mem());
1562     BUF_MEM* mem;
1563     if (X509_NAME_print_ex(bio, X509_get_subject_name(peer_cert), 0,
1564                            X509_NAME_FLAGS) > 0) {
1565       BIO_get_mem_ptr(bio, &mem);
1566       info->Set(subject_symbol, String::New(mem->data, mem->length));
1567     }
1568     (void) BIO_reset(bio);
1569
1570     if (X509_NAME_print_ex(bio, X509_get_issuer_name(peer_cert), 0,
1571                            X509_NAME_FLAGS) > 0) {
1572       BIO_get_mem_ptr(bio, &mem);
1573       info->Set(issuer_symbol, String::New(mem->data, mem->length));
1574     }
1575     (void) BIO_reset(bio);
1576
1577     int index = X509_get_ext_by_NID(peer_cert, NID_subject_alt_name, -1);
1578     if (index >= 0) {
1579       X509_EXTENSION* ext;
1580       int rv;
1581
1582       ext = X509_get_ext(peer_cert, index);
1583       assert(ext != NULL);
1584
1585       rv = X509V3_EXT_print(bio, ext, 0, 0);
1586       assert(rv == 1);
1587
1588       BIO_get_mem_ptr(bio, &mem);
1589       info->Set(subjectaltname_symbol, String::New(mem->data, mem->length));
1590
1591       (void) BIO_reset(bio);
1592     }
1593
1594     EVP_PKEY *pkey = NULL;
1595     RSA *rsa = NULL;
1596     if( NULL != (pkey = X509_get_pubkey(peer_cert))
1597         && NULL != (rsa = EVP_PKEY_get1_RSA(pkey)) ) {
1598         BN_print(bio, rsa->n);
1599         BIO_get_mem_ptr(bio, &mem);
1600         info->Set(modulus_symbol, String::New(mem->data, mem->length) );
1601         (void) BIO_reset(bio);
1602
1603         BN_print(bio, rsa->e);
1604         BIO_get_mem_ptr(bio, &mem);
1605         info->Set(exponent_symbol, String::New(mem->data, mem->length) );
1606         (void) BIO_reset(bio);
1607     }
1608
1609     if (pkey != NULL) {
1610       EVP_PKEY_free(pkey);
1611       pkey = NULL;
1612     }
1613     if (rsa != NULL) {
1614       RSA_free(rsa);
1615       rsa = NULL;
1616     }
1617
1618     ASN1_TIME_print(bio, X509_get_notBefore(peer_cert));
1619     BIO_get_mem_ptr(bio, &mem);
1620     info->Set(valid_from_symbol, String::New(mem->data, mem->length));
1621     (void) BIO_reset(bio);
1622
1623     ASN1_TIME_print(bio, X509_get_notAfter(peer_cert));
1624     BIO_get_mem_ptr(bio, &mem);
1625     info->Set(valid_to_symbol, String::New(mem->data, mem->length));
1626     BIO_free(bio);
1627
1628     unsigned int md_size, i;
1629     unsigned char md[EVP_MAX_MD_SIZE];
1630     if (X509_digest(peer_cert, EVP_sha1(), md, &md_size)) {
1631       const char hex[] = "0123456789ABCDEF";
1632       char fingerprint[EVP_MAX_MD_SIZE * 3];
1633
1634       for (i = 0; i<md_size; i++) {
1635         fingerprint[3*i] = hex[(md[i] & 0xf0) >> 4];
1636         fingerprint[(3*i)+1] = hex[(md[i] & 0x0f)];
1637         fingerprint[(3*i)+2] = ':';
1638       }
1639
1640       if (md_size > 0) {
1641         fingerprint[(3*(md_size-1))+2] = '\0';
1642       }
1643       else {
1644         fingerprint[0] = '\0';
1645       }
1646
1647       info->Set(fingerprint_symbol, String::New(fingerprint));
1648     }
1649
1650     STACK_OF(ASN1_OBJECT) *eku = (STACK_OF(ASN1_OBJECT) *)X509_get_ext_d2i(
1651         peer_cert, NID_ext_key_usage, NULL, NULL);
1652     if (eku != NULL) {
1653       Local<Array> ext_key_usage = Array::New();
1654       char buf[256];
1655
1656       for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
1657         memset(buf, 0, sizeof(buf));
1658         OBJ_obj2txt(buf, sizeof(buf) - 1, sk_ASN1_OBJECT_value(eku, i), 1);
1659         ext_key_usage->Set(Integer::New(i), String::New(buf));
1660       }
1661
1662       sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
1663       info->Set(ext_key_usage_symbol, ext_key_usage);
1664     }
1665
1666     X509_free(peer_cert);
1667   }
1668   return scope.Close(info);
1669 }
1670
1671 Handle<Value> Connection::GetSession(const Arguments& args) {
1672   HandleScope scope;
1673
1674   Connection *ss = Connection::Unwrap(args);
1675
1676   if (ss->ssl_ == NULL) return Undefined();
1677
1678   SSL_SESSION* sess = SSL_get_session(ss->ssl_);
1679   if (!sess) return Undefined();
1680
1681   int slen = i2d_SSL_SESSION(sess, NULL);
1682   assert(slen > 0);
1683
1684   if (slen > 0) {
1685     unsigned char* sbuf = new unsigned char[slen];
1686     unsigned char* p = sbuf;
1687     i2d_SSL_SESSION(sess, &p);
1688     Local<Value> s = Encode(sbuf, slen, BINARY);
1689     delete[] sbuf;
1690     return scope.Close(s);
1691   }
1692
1693   return Null();
1694 }
1695
1696 Handle<Value> Connection::SetSession(const Arguments& args) {
1697   HandleScope scope;
1698
1699   Connection *ss = Connection::Unwrap(args);
1700
1701   if (args.Length() < 1 ||
1702       (!args[0]->IsString() && !Buffer::HasInstance(args[0]))) {
1703     Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
1704     return ThrowException(exception);
1705   }
1706
1707   ASSERT_IS_BUFFER(args[0]);
1708   ssize_t slen = Buffer::Length(args[0]);
1709
1710   if (slen < 0) {
1711     Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
1712     return ThrowException(exception);
1713   }
1714
1715   char* sbuf = new char[slen];
1716
1717   ssize_t wlen = DecodeWrite(sbuf, slen, args[0], BINARY);
1718   assert(wlen == slen);
1719
1720   const unsigned char* p = reinterpret_cast<const unsigned char*>(sbuf);
1721   SSL_SESSION* sess = d2i_SSL_SESSION(NULL, &p, wlen);
1722
1723   delete [] sbuf;
1724
1725   if (!sess)
1726     return Undefined();
1727
1728   int r = SSL_set_session(ss->ssl_, sess);
1729   SSL_SESSION_free(sess);
1730
1731   if (!r) {
1732     Local<String> eStr = String::New("SSL_set_session error");
1733     return ThrowException(Exception::Error(eStr));
1734   }
1735
1736   return True();
1737 }
1738
1739 Handle<Value> Connection::LoadSession(const Arguments& args) {
1740   HandleScope scope;
1741
1742   Connection *ss = Connection::Unwrap(args);
1743
1744   if (args.Length() >= 1 && Buffer::HasInstance(args[0])) {
1745     ssize_t slen = Buffer::Length(args[0].As<Object>());
1746     char* sbuf = Buffer::Data(args[0].As<Object>());
1747
1748     const unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
1749     SSL_SESSION* sess = d2i_SSL_SESSION(NULL, &p, slen);
1750
1751     // Setup next session and move hello to the BIO buffer
1752     if (ss->next_sess_ != NULL) {
1753       SSL_SESSION_free(ss->next_sess_);
1754     }
1755     ss->next_sess_ = sess;
1756   }
1757
1758   ss->hello_parser_.Finish();
1759
1760   return True();
1761 }
1762
1763 Handle<Value> Connection::IsSessionReused(const Arguments& args) {
1764   HandleScope scope;
1765
1766   Connection *ss = Connection::Unwrap(args);
1767
1768   if (ss->ssl_ == NULL || SSL_session_reused(ss->ssl_) == false) {
1769     return False();
1770   }
1771
1772   return True();
1773 }
1774
1775
1776 Handle<Value> Connection::Start(const Arguments& args) {
1777   HandleScope scope;
1778
1779   Connection *ss = Connection::Unwrap(args);
1780
1781   if (!SSL_is_init_finished(ss->ssl_)) {
1782     int rv;
1783     if (ss->is_server_) {
1784       rv = SSL_accept(ss->ssl_);
1785       ss->HandleSSLError("SSL_accept:Start", rv, kZeroIsAnError, kSyscallError);
1786     } else {
1787       rv = SSL_connect(ss->ssl_);
1788       ss->HandleSSLError("SSL_connect:Start",
1789                          rv,
1790                          kZeroIsAnError,
1791                          kSyscallError);
1792     }
1793
1794     return scope.Close(Integer::New(rv));
1795   }
1796
1797   return scope.Close(Integer::New(0));
1798 }
1799
1800
1801 Handle<Value> Connection::Shutdown(const Arguments& args) {
1802   HandleScope scope;
1803
1804   Connection *ss = Connection::Unwrap(args);
1805
1806   if (ss->ssl_ == NULL) return False();
1807   int rv = SSL_shutdown(ss->ssl_);
1808   ss->HandleSSLError("SSL_shutdown", rv, kZeroIsNotAnError, kIgnoreSyscall);
1809   ss->SetShutdownFlags();
1810
1811   return scope.Close(Integer::New(rv));
1812 }
1813
1814
1815 Handle<Value> Connection::IsInitFinished(const Arguments& args) {
1816   HandleScope scope;
1817
1818   Connection *ss = Connection::Unwrap(args);
1819
1820   if (ss->ssl_ == NULL || SSL_is_init_finished(ss->ssl_) == false) {
1821     return False();
1822   }
1823
1824   return True();
1825 }
1826
1827
1828 Handle<Value> Connection::VerifyError(const Arguments& args) {
1829   HandleScope scope;
1830
1831   Connection *ss = Connection::Unwrap(args);
1832
1833   if (ss->ssl_ == NULL) return Null();
1834
1835
1836   // XXX Do this check in JS land?
1837   X509* peer_cert = SSL_get_peer_certificate(ss->ssl_);
1838   if (peer_cert == NULL) {
1839     // We requested a certificate and they did not send us one.
1840     // Definitely an error.
1841     // XXX is this the right error message?
1842     return scope.Close(Exception::Error(
1843           String::New("UNABLE_TO_GET_ISSUER_CERT")));
1844   }
1845   X509_free(peer_cert);
1846
1847
1848   long x509_verify_error = SSL_get_verify_result(ss->ssl_);
1849
1850   Local<String> s;
1851
1852   switch (x509_verify_error) {
1853     case X509_V_OK:
1854       return Null();
1855
1856     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1857       s = String::New("UNABLE_TO_GET_ISSUER_CERT");
1858       break;
1859
1860     case X509_V_ERR_UNABLE_TO_GET_CRL:
1861       s = String::New("UNABLE_TO_GET_CRL");
1862       break;
1863
1864     case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1865       s = String::New("UNABLE_TO_DECRYPT_CERT_SIGNATURE");
1866       break;
1867
1868     case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1869       s = String::New("UNABLE_TO_DECRYPT_CRL_SIGNATURE");
1870       break;
1871
1872     case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1873       s = String::New("UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY");
1874       break;
1875
1876     case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1877       s = String::New("CERT_SIGNATURE_FAILURE");
1878       break;
1879
1880     case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1881       s = String::New("CRL_SIGNATURE_FAILURE");
1882       break;
1883
1884     case X509_V_ERR_CERT_NOT_YET_VALID:
1885       s = String::New("CERT_NOT_YET_VALID");
1886       break;
1887
1888     case X509_V_ERR_CERT_HAS_EXPIRED:
1889       s = String::New("CERT_HAS_EXPIRED");
1890       break;
1891
1892     case X509_V_ERR_CRL_NOT_YET_VALID:
1893       s = String::New("CRL_NOT_YET_VALID");
1894       break;
1895
1896     case X509_V_ERR_CRL_HAS_EXPIRED:
1897       s = String::New("CRL_HAS_EXPIRED");
1898       break;
1899
1900     case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1901       s = String::New("ERROR_IN_CERT_NOT_BEFORE_FIELD");
1902       break;
1903
1904     case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1905       s = String::New("ERROR_IN_CERT_NOT_AFTER_FIELD");
1906       break;
1907
1908     case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1909       s = String::New("ERROR_IN_CRL_LAST_UPDATE_FIELD");
1910       break;
1911
1912     case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1913       s = String::New("ERROR_IN_CRL_NEXT_UPDATE_FIELD");
1914       break;
1915
1916     case X509_V_ERR_OUT_OF_MEM:
1917       s = String::New("OUT_OF_MEM");
1918       break;
1919
1920     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1921       s = String::New("DEPTH_ZERO_SELF_SIGNED_CERT");
1922       break;
1923
1924     case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1925       s = String::New("SELF_SIGNED_CERT_IN_CHAIN");
1926       break;
1927
1928     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1929       s = String::New("UNABLE_TO_GET_ISSUER_CERT_LOCALLY");
1930       break;
1931
1932     case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1933       s = String::New("UNABLE_TO_VERIFY_LEAF_SIGNATURE");
1934       break;
1935
1936     case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1937       s = String::New("CERT_CHAIN_TOO_LONG");
1938       break;
1939
1940     case X509_V_ERR_CERT_REVOKED:
1941       s = String::New("CERT_REVOKED");
1942       break;
1943
1944     case X509_V_ERR_INVALID_CA:
1945       s = String::New("INVALID_CA");
1946       break;
1947
1948     case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1949       s = String::New("PATH_LENGTH_EXCEEDED");
1950       break;
1951
1952     case X509_V_ERR_INVALID_PURPOSE:
1953       s = String::New("INVALID_PURPOSE");
1954       break;
1955
1956     case X509_V_ERR_CERT_UNTRUSTED:
1957       s = String::New("CERT_UNTRUSTED");
1958       break;
1959
1960     case X509_V_ERR_CERT_REJECTED:
1961       s = String::New("CERT_REJECTED");
1962       break;
1963
1964     default:
1965       s = String::New(X509_verify_cert_error_string(x509_verify_error));
1966       break;
1967   }
1968
1969   return scope.Close(Exception::Error(s));
1970 }
1971
1972
1973 Handle<Value> Connection::GetCurrentCipher(const Arguments& args) {
1974   HandleScope scope;
1975
1976   Connection *ss = Connection::Unwrap(args);
1977
1978   OPENSSL_CONST SSL_CIPHER *c;
1979
1980   if ( ss->ssl_ == NULL ) return Undefined();
1981   c = SSL_get_current_cipher(ss->ssl_);
1982   if ( c == NULL ) return Undefined();
1983   Local<Object> info = Object::New();
1984   const char* cipher_name = SSL_CIPHER_get_name(c);
1985   info->Set(name_symbol, String::New(cipher_name));
1986   const char* cipher_version = SSL_CIPHER_get_version(c);
1987   info->Set(version_symbol, String::New(cipher_version));
1988   return scope.Close(info);
1989 }
1990
1991 Handle<Value> Connection::Close(const Arguments& args) {
1992   HandleScope scope;
1993
1994   Connection *ss = Connection::Unwrap(args);
1995
1996   if (ss->ssl_ != NULL) {
1997     SSL_free(ss->ssl_);
1998     ss->ssl_ = NULL;
1999   }
2000   return True();
2001 }
2002
2003
2004 void Connection::InitNPN(SecureContext* sc, bool is_server) {
2005 #ifdef OPENSSL_NPN_NEGOTIATED
2006   if (is_server) {
2007     // Server should advertise NPN protocols
2008     SSL_CTX_set_next_protos_advertised_cb(sc->ctx_,
2009                                           AdvertiseNextProtoCallback_,
2010                                           NULL);
2011   } else {
2012     // Client should select protocol from advertised
2013     // If server supports NPN
2014     SSL_CTX_set_next_proto_select_cb(sc->ctx_,
2015                                      SelectNextProtoCallback_,
2016                                      NULL);
2017   }
2018 #endif
2019 }
2020
2021 #ifdef OPENSSL_NPN_NEGOTIATED
2022 Handle<Value> Connection::GetNegotiatedProto(const Arguments& args) {
2023   HandleScope scope;
2024
2025   Connection *ss = Connection::Unwrap(args);
2026
2027   if (ss->is_server_) {
2028     const unsigned char* npn_proto;
2029     unsigned int npn_proto_len;
2030
2031     SSL_get0_next_proto_negotiated(ss->ssl_, &npn_proto, &npn_proto_len);
2032
2033     if (!npn_proto) {
2034       return False();
2035     }
2036
2037     return scope.Close(String::New(reinterpret_cast<const char*>(npn_proto),
2038                                    npn_proto_len));
2039   } else {
2040     return ss->selectedNPNProto_;
2041   }
2042 }
2043
2044 Handle<Value> Connection::SetNPNProtocols(const Arguments& args) {
2045   HandleScope scope;
2046
2047   Connection *ss = Connection::Unwrap(args);
2048
2049   if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
2050     return ThrowException(Exception::Error(String::New(
2051            "Must give a Buffer as first argument")));
2052   }
2053
2054   // Release old handle
2055   if (!ss->npnProtos_.IsEmpty()) {
2056     ss->npnProtos_.Dispose();
2057   }
2058   ss->npnProtos_ = Persistent<Object>::New(args[0]->ToObject());
2059
2060   return True();
2061 };
2062 #endif
2063
2064 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2065 Handle<Value> Connection::GetServername(const Arguments& args) {
2066   HandleScope scope;
2067
2068   Connection *ss = Connection::Unwrap(args);
2069
2070   if (ss->is_server_ && !ss->servername_.IsEmpty()) {
2071     return ss->servername_;
2072   } else {
2073     return False();
2074   }
2075 }
2076
2077 Handle<Value> Connection::SetSNICallback(const Arguments& args) {
2078   HandleScope scope;
2079
2080   Connection *ss = Connection::Unwrap(args);
2081
2082   if (args.Length() < 1 || !args[0]->IsFunction()) {
2083     return ThrowException(Exception::Error(String::New(
2084            "Must give a Function as first argument")));
2085   }
2086
2087   // Release old handle
2088   if (!ss->sniObject_.IsEmpty()) {
2089     ss->sniObject_.Dispose();
2090   }
2091   ss->sniObject_ = Persistent<Object>::New(Object::New());
2092   ss->sniObject_->Set(String::New("onselect"), args[0]);
2093
2094   return True();
2095 }
2096 #endif
2097
2098
2099 class Cipher : public ObjectWrap {
2100  public:
2101   static void Initialize (v8::Handle<v8::Object> target) {
2102     HandleScope scope;
2103
2104     Local<FunctionTemplate> t = FunctionTemplate::New(New);
2105
2106     t->InstanceTemplate()->SetInternalFieldCount(1);
2107
2108     NODE_SET_PROTOTYPE_METHOD(t, "init", CipherInit);
2109     NODE_SET_PROTOTYPE_METHOD(t, "initiv", CipherInitIv);
2110     NODE_SET_PROTOTYPE_METHOD(t, "update", CipherUpdate);
2111     NODE_SET_PROTOTYPE_METHOD(t, "setAutoPadding", SetAutoPadding);
2112     NODE_SET_PROTOTYPE_METHOD(t, "final", CipherFinal);
2113
2114     target->Set(String::NewSymbol("Cipher"), t->GetFunction());
2115   }
2116
2117
2118   bool CipherInit(char* cipherType, char* key_buf, int key_buf_len) {
2119     cipher = EVP_get_cipherbyname(cipherType);
2120     if(!cipher) {
2121       fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
2122       return false;
2123     }
2124
2125     unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
2126     int key_len = EVP_BytesToKey(cipher, EVP_md5(), NULL,
2127       (unsigned char*) key_buf, key_buf_len, 1, key, iv);
2128
2129     EVP_CIPHER_CTX_init(&ctx);
2130     EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, NULL, true);
2131     if (!EVP_CIPHER_CTX_set_key_length(&ctx, key_len)) {
2132       fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len);
2133       EVP_CIPHER_CTX_cleanup(&ctx);
2134       return false;
2135     }
2136     EVP_CipherInit_ex(&ctx, NULL, NULL,
2137       (unsigned char*)key,
2138       (unsigned char*)iv, true);
2139     initialised_ = true;
2140     return true;
2141   }
2142
2143
2144   bool CipherInitIv(char* cipherType,
2145                     char* key,
2146                     int key_len,
2147                     char* iv,
2148                     int iv_len) {
2149     cipher = EVP_get_cipherbyname(cipherType);
2150     if(!cipher) {
2151       fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
2152       return false;
2153     }
2154     /* OpenSSL versions up to 0.9.8l failed to return the correct
2155        iv_length (0) for ECB ciphers */
2156     if (EVP_CIPHER_iv_length(cipher) != iv_len &&
2157       !(EVP_CIPHER_mode(cipher) == EVP_CIPH_ECB_MODE && iv_len == 0)) {
2158       fprintf(stderr, "node-crypto : Invalid IV length %d\n", iv_len);
2159       return false;
2160     }
2161     EVP_CIPHER_CTX_init(&ctx);
2162     EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, NULL, true);
2163     if (!EVP_CIPHER_CTX_set_key_length(&ctx, key_len)) {
2164       fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len);
2165       EVP_CIPHER_CTX_cleanup(&ctx);
2166       return false;
2167     }
2168     EVP_CipherInit_ex(&ctx, NULL, NULL,
2169       (unsigned char*)key,
2170       (unsigned char*)iv, true);
2171     initialised_ = true;
2172     return true;
2173   }
2174
2175   int CipherUpdate(char* data, int len, unsigned char** out, int* out_len) {
2176     if (!initialised_) return 0;
2177     *out_len = len+EVP_CIPHER_CTX_block_size(&ctx);
2178     *out = new unsigned char[*out_len];
2179     return EVP_CipherUpdate(&ctx, *out, out_len, (unsigned char*)data, len);
2180   }
2181
2182   int SetAutoPadding(bool auto_padding) {
2183     if (!initialised_) return 0;
2184     return EVP_CIPHER_CTX_set_padding(&ctx, auto_padding ? 1 : 0);
2185   }
2186
2187   int CipherFinal(unsigned char** out, int *out_len) {
2188     if (!initialised_) return 0;
2189     *out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx)];
2190     int r = EVP_CipherFinal_ex(&ctx,*out, out_len);
2191     EVP_CIPHER_CTX_cleanup(&ctx);
2192     initialised_ = false;
2193     return r;
2194   }
2195
2196
2197  protected:
2198
2199   static Handle<Value> New(const Arguments& args) {
2200     HandleScope scope;
2201
2202     Cipher *cipher = new Cipher();
2203     cipher->Wrap(args.This());
2204     return args.This();
2205   }
2206
2207   static Handle<Value> CipherInit(const Arguments& args) {
2208     HandleScope scope;
2209
2210     Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
2211
2212     if (args.Length() <= 1
2213         || !args[0]->IsString()
2214         || !(args[1]->IsString() || Buffer::HasInstance(args[1])))
2215     {
2216       return ThrowException(Exception::Error(String::New(
2217         "Must give cipher-type, key")));
2218     }
2219
2220     ASSERT_IS_BUFFER(args[1]);
2221     ssize_t key_buf_len = Buffer::Length(args[1]);
2222
2223     if (key_buf_len < 0) {
2224       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2225       return ThrowException(exception);
2226     }
2227
2228     char* key_buf = new char[key_buf_len];
2229     ssize_t key_written = DecodeWrite(key_buf, key_buf_len, args[1], BINARY);
2230     assert(key_written == key_buf_len);
2231
2232     String::Utf8Value cipherType(args[0]);
2233
2234     bool r = cipher->CipherInit(*cipherType, key_buf, key_buf_len);
2235
2236     delete [] key_buf;
2237
2238     if (!r) return ThrowCryptoError(ERR_get_error());
2239
2240     return args.This();
2241   }
2242
2243
2244   static Handle<Value> CipherInitIv(const Arguments& args) {
2245     Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
2246
2247     HandleScope scope;
2248
2249
2250     if (args.Length() <= 2
2251         || !args[0]->IsString()
2252         || !(args[1]->IsString() || Buffer::HasInstance(args[1]))
2253         || !(args[2]->IsString() || Buffer::HasInstance(args[2])))
2254     {
2255       return ThrowException(Exception::Error(String::New(
2256         "Must give cipher-type, key, and iv as argument")));
2257     }
2258
2259     ASSERT_IS_BUFFER(args[1]);
2260     ssize_t key_len = Buffer::Length(args[1]);
2261
2262     if (key_len < 0) {
2263       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2264       return ThrowException(exception);
2265     }
2266
2267     ASSERT_IS_BUFFER(args[2]);
2268     ssize_t iv_len = Buffer::Length(args[2]);
2269
2270     if (iv_len < 0) {
2271       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2272       return ThrowException(exception);
2273     }
2274
2275     char* key_buf = new char[key_len];
2276     ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
2277     assert(key_written == key_len);
2278
2279     char* iv_buf = new char[iv_len];
2280     ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
2281     assert(iv_written == iv_len);
2282
2283     String::Utf8Value cipherType(args[0]);
2284
2285     bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
2286
2287     delete [] key_buf;
2288     delete [] iv_buf;
2289
2290     if (!r) return ThrowCryptoError(ERR_get_error());
2291
2292     return args.This();
2293   }
2294
2295   static Handle<Value> CipherUpdate(const Arguments& args) {
2296     Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
2297
2298     HandleScope scope;
2299
2300     ASSERT_IS_STRING_OR_BUFFER(args[0]);
2301
2302     // Only copy the data if we have to, because it's a string
2303     unsigned char* out = 0;
2304     int out_len = 0, r;
2305     if (args[0]->IsString()) {
2306       Local<String> string = args[0].As<String>();
2307       enum encoding encoding = ParseEncoding(args[1], BINARY);
2308       if (!StringBytes::IsValidString(string, encoding))
2309         return ThrowTypeError("Bad input string");
2310       size_t buflen = StringBytes::StorageSize(string, encoding);
2311       char* buf = new char[buflen];
2312       size_t written = StringBytes::Write(buf, buflen, string, encoding);
2313       r = cipher->CipherUpdate(buf, written, &out, &out_len);
2314       delete[] buf;
2315     } else {
2316       char* buf = Buffer::Data(args[0]);
2317       size_t buflen = Buffer::Length(args[0]);
2318       r = cipher->CipherUpdate(buf, buflen, &out, &out_len);
2319     }
2320
2321     if (r == 0) {
2322       delete[] out;
2323       return ThrowCryptoTypeError(ERR_get_error());
2324     }
2325
2326     Local<Value> outString;
2327     outString = Encode(out, out_len, BUFFER);
2328
2329     if (out) delete[] out;
2330
2331     return scope.Close(outString);
2332   }
2333
2334   static Handle<Value> SetAutoPadding(const Arguments& args) {
2335     HandleScope scope;
2336     Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
2337
2338     cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
2339
2340     return Undefined();
2341   }
2342
2343   static Handle<Value> CipherFinal(const Arguments& args) {
2344     Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
2345
2346     HandleScope scope;
2347
2348     unsigned char* out_value = NULL;
2349     int out_len = -1;
2350     Local<Value> outString ;
2351
2352     int r = cipher->CipherFinal(&out_value, &out_len);
2353
2354     if (out_len <= 0 || r == 0) {
2355       delete[] out_value;
2356       out_value = NULL;
2357       if (r == 0) return ThrowCryptoTypeError(ERR_get_error());
2358     }
2359
2360     outString = Encode(out_value, out_len, BUFFER);
2361
2362     delete [] out_value;
2363     return scope.Close(outString);
2364   }
2365
2366   Cipher () : ObjectWrap ()
2367   {
2368     initialised_ = false;
2369   }
2370
2371   ~Cipher () {
2372     if (initialised_) {
2373       EVP_CIPHER_CTX_cleanup(&ctx);
2374     }
2375   }
2376
2377  private:
2378
2379   EVP_CIPHER_CTX ctx; /* coverity[member_decl] */
2380   const EVP_CIPHER *cipher; /* coverity[member_decl] */
2381   bool initialised_;
2382 };
2383
2384
2385
2386 class Decipher : public ObjectWrap {
2387  public:
2388   static void
2389   Initialize (v8::Handle<v8::Object> target)
2390   {
2391     HandleScope scope;
2392
2393     Local<FunctionTemplate> t = FunctionTemplate::New(New);
2394
2395     t->InstanceTemplate()->SetInternalFieldCount(1);
2396
2397     NODE_SET_PROTOTYPE_METHOD(t, "init", DecipherInit);
2398     NODE_SET_PROTOTYPE_METHOD(t, "initiv", DecipherInitIv);
2399     NODE_SET_PROTOTYPE_METHOD(t, "update", DecipherUpdate);
2400     NODE_SET_PROTOTYPE_METHOD(t, "final", DecipherFinal);
2401     NODE_SET_PROTOTYPE_METHOD(t, "finaltol", DecipherFinal); // remove someday
2402     NODE_SET_PROTOTYPE_METHOD(t, "setAutoPadding", SetAutoPadding);
2403
2404     target->Set(String::NewSymbol("Decipher"), t->GetFunction());
2405   }
2406
2407   bool DecipherInit(char* cipherType, char* key_buf, int key_buf_len) {
2408     cipher_ = EVP_get_cipherbyname(cipherType);
2409
2410     if(!cipher_) {
2411       fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
2412       return false;
2413     }
2414
2415     unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
2416     int key_len = EVP_BytesToKey(cipher_,
2417                                  EVP_md5(),
2418                                  NULL,
2419                                  (unsigned char*)(key_buf),
2420                                  key_buf_len,
2421                                  1,
2422                                  key,
2423                                  iv);
2424
2425     EVP_CIPHER_CTX_init(&ctx);
2426     EVP_CipherInit_ex(&ctx, cipher_, NULL, NULL, NULL, false);
2427     if (!EVP_CIPHER_CTX_set_key_length(&ctx, key_len)) {
2428       fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len);
2429       EVP_CIPHER_CTX_cleanup(&ctx);
2430       return false;
2431     }
2432     EVP_CipherInit_ex(&ctx, NULL, NULL,
2433       (unsigned char*)key,
2434       (unsigned char*)iv, false);
2435     initialised_ = true;
2436     return true;
2437   }
2438
2439
2440   bool DecipherInitIv(char* cipherType,
2441                       char* key,
2442                       int key_len,
2443                       char* iv,
2444                       int iv_len) {
2445     cipher_ = EVP_get_cipherbyname(cipherType);
2446     if(!cipher_) {
2447       fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
2448       return false;
2449     }
2450     /* OpenSSL versions up to 0.9.8l failed to return the correct
2451       iv_length (0) for ECB ciphers */
2452     if (EVP_CIPHER_iv_length(cipher_) != iv_len &&
2453       !(EVP_CIPHER_mode(cipher_) == EVP_CIPH_ECB_MODE && iv_len == 0)) {
2454       fprintf(stderr, "node-crypto : Invalid IV length %d\n", iv_len);
2455       return false;
2456     }
2457     EVP_CIPHER_CTX_init(&ctx);
2458     EVP_CipherInit_ex(&ctx, cipher_, NULL, NULL, NULL, false);
2459     if (!EVP_CIPHER_CTX_set_key_length(&ctx, key_len)) {
2460       fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len);
2461       EVP_CIPHER_CTX_cleanup(&ctx);
2462       return false;
2463     }
2464     EVP_CipherInit_ex(&ctx, NULL, NULL,
2465       (unsigned char*)key,
2466       (unsigned char*)iv, false);
2467     initialised_ = true;
2468     return true;
2469   }
2470
2471   int DecipherUpdate(char* data, int len, unsigned char** out, int* out_len) {
2472     if (!initialised_) {
2473       *out_len = 0;
2474       *out = NULL;
2475       return 0;
2476     }
2477
2478     *out_len = len+EVP_CIPHER_CTX_block_size(&ctx);
2479     *out = new unsigned char[*out_len];
2480
2481     return EVP_CipherUpdate(&ctx, *out, out_len, (unsigned char*)data, len);
2482   }
2483
2484   int SetAutoPadding(bool auto_padding) {
2485     if (!initialised_) return 0;
2486     return EVP_CIPHER_CTX_set_padding(&ctx, auto_padding ? 1 : 0);
2487   }
2488
2489   // coverity[alloc_arg]
2490   int DecipherFinal(unsigned char** out, int *out_len) {
2491     int r;
2492
2493     if (!initialised_) {
2494       *out_len = 0;
2495       *out = NULL;
2496       return 0;
2497     }
2498
2499     *out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx)];
2500     r = EVP_CipherFinal_ex(&ctx,*out,out_len);
2501     EVP_CIPHER_CTX_cleanup(&ctx);
2502     initialised_ = false;
2503     return r;
2504   }
2505
2506
2507  protected:
2508
2509   static Handle<Value> New (const Arguments& args) {
2510     HandleScope scope;
2511
2512     Decipher *cipher = new Decipher();
2513     cipher->Wrap(args.This());
2514     return args.This();
2515   }
2516
2517   static Handle<Value> DecipherInit(const Arguments& args) {
2518     Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
2519
2520     HandleScope scope;
2521
2522     if (args.Length() <= 1
2523         || !args[0]->IsString()
2524         || !(args[1]->IsString() || Buffer::HasInstance(args[1])))
2525     {
2526       return ThrowException(Exception::Error(String::New(
2527         "Must give cipher-type, key as argument")));
2528     }
2529
2530     ASSERT_IS_BUFFER(args[1]);
2531     ssize_t key_len = Buffer::Length(args[1]);
2532
2533     if (key_len < 0) {
2534       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2535       return ThrowException(exception);
2536     }
2537
2538     char* key_buf = new char[key_len];
2539     ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
2540     assert(key_written == key_len);
2541
2542     String::Utf8Value cipherType(args[0]);
2543
2544     bool r = cipher->DecipherInit(*cipherType, key_buf,key_len);
2545
2546     delete [] key_buf;
2547
2548     if (!r) {
2549       return ThrowException(Exception::Error(String::New("DecipherInit error")));
2550     }
2551
2552     return args.This();
2553   }
2554
2555   static Handle<Value> DecipherInitIv(const Arguments& args) {
2556     Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
2557
2558     HandleScope scope;
2559
2560     if (args.Length() <= 2
2561         || !args[0]->IsString()
2562         || !(args[1]->IsString() || Buffer::HasInstance(args[1]))
2563         || !(args[2]->IsString() || Buffer::HasInstance(args[2])))
2564     {
2565       return ThrowException(Exception::Error(String::New(
2566         "Must give cipher-type, key, and iv as argument")));
2567     }
2568
2569     ASSERT_IS_BUFFER(args[1]);
2570     ssize_t key_len = Buffer::Length(args[1]);
2571
2572     if (key_len < 0) {
2573       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2574       return ThrowException(exception);
2575     }
2576
2577     ASSERT_IS_BUFFER(args[2]);
2578     ssize_t iv_len = Buffer::Length(args[2]);
2579
2580     if (iv_len < 0) {
2581       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2582       return ThrowException(exception);
2583     }
2584
2585     char* key_buf = new char[key_len];
2586     ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
2587     assert(key_written == key_len);
2588
2589     char* iv_buf = new char[iv_len];
2590     ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
2591     assert(iv_written == iv_len);
2592
2593     String::Utf8Value cipherType(args[0]);
2594
2595     bool r = cipher->DecipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
2596
2597     delete [] key_buf;
2598     delete [] iv_buf;
2599
2600     if (!r) {
2601       return ThrowException(Exception::Error(String::New("DecipherInitIv error")));
2602     }
2603
2604     return args.This();
2605   }
2606
2607   static Handle<Value> DecipherUpdate(const Arguments& args) {
2608     HandleScope scope;
2609
2610     Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
2611
2612     ASSERT_IS_STRING_OR_BUFFER(args[0]);
2613
2614     // Only copy the data if we have to, because it's a string
2615     unsigned char* out = 0;
2616     int out_len = 0, r;
2617     if (args[0]->IsString()) {
2618       Local<String> string = args[0].As<String>();
2619       enum encoding encoding = ParseEncoding(args[1], BINARY);
2620       if (!StringBytes::IsValidString(string, encoding))
2621         return ThrowTypeError("Bad input string");
2622       size_t buflen = StringBytes::StorageSize(string, encoding);
2623       char* buf = new char[buflen];
2624       size_t written = StringBytes::Write(buf, buflen, string, encoding);
2625       r = cipher->DecipherUpdate(buf, written, &out, &out_len);
2626       delete[] buf;
2627     } else {
2628       char* buf = Buffer::Data(args[0]);
2629       size_t buflen = Buffer::Length(args[0]);
2630       r = cipher->DecipherUpdate(buf, buflen, &out, &out_len);
2631     }
2632
2633     if (r == 0) {
2634       delete[] out;
2635       return ThrowCryptoTypeError(ERR_get_error());
2636     }
2637
2638     Local<Value> outString;
2639     outString = Encode(out, out_len, BUFFER);
2640
2641     if (out) delete [] out;
2642
2643     return scope.Close(outString);
2644   }
2645
2646   static Handle<Value> SetAutoPadding(const Arguments& args) {
2647     HandleScope scope;
2648     Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
2649
2650     cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
2651
2652     return Undefined();
2653   }
2654
2655   static Handle<Value> DecipherFinal(const Arguments& args) {
2656     HandleScope scope;
2657
2658     Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
2659
2660     unsigned char* out_value = NULL;
2661     int out_len = -1;
2662     Local<Value> outString;
2663
2664     int r = cipher->DecipherFinal(&out_value, &out_len);
2665
2666     if (out_len <= 0 || r == 0) {
2667       delete [] out_value; // allocated even if out_len == 0
2668       out_value = NULL;
2669       if (r == 0) return ThrowCryptoTypeError(ERR_get_error());
2670     }
2671
2672     outString = Encode(out_value, out_len, BUFFER);
2673     delete [] out_value;
2674     return scope.Close(outString);
2675   }
2676
2677   Decipher () : ObjectWrap () {
2678     initialised_ = false;
2679   }
2680
2681   ~Decipher () {
2682     if (initialised_) {
2683       EVP_CIPHER_CTX_cleanup(&ctx);
2684     }
2685   }
2686
2687  private:
2688
2689   EVP_CIPHER_CTX ctx;
2690   const EVP_CIPHER *cipher_;
2691   bool initialised_;
2692 };
2693
2694
2695
2696
2697 class Hmac : public ObjectWrap {
2698  public:
2699   static void Initialize (v8::Handle<v8::Object> target) {
2700     HandleScope scope;
2701
2702     Local<FunctionTemplate> t = FunctionTemplate::New(New);
2703
2704     t->InstanceTemplate()->SetInternalFieldCount(1);
2705
2706     NODE_SET_PROTOTYPE_METHOD(t, "init", HmacInit);
2707     NODE_SET_PROTOTYPE_METHOD(t, "update", HmacUpdate);
2708     NODE_SET_PROTOTYPE_METHOD(t, "digest", HmacDigest);
2709
2710     target->Set(String::NewSymbol("Hmac"), t->GetFunction());
2711   }
2712
2713   bool HmacInit(char* hashType, char* key, int key_len) {
2714     md = EVP_get_digestbyname(hashType);
2715     if(!md) {
2716       fprintf(stderr, "node-crypto : Unknown message digest %s\n", hashType);
2717       return false;
2718     }
2719     HMAC_CTX_init(&ctx);
2720     if (key_len == 0) {
2721       HMAC_Init(&ctx, "", 0, md);
2722     } else {
2723       HMAC_Init(&ctx, key, key_len, md);
2724     }
2725     initialised_ = true;
2726     return true;
2727
2728   }
2729
2730   int HmacUpdate(char* data, int len) {
2731     if (!initialised_) return 0;
2732     HMAC_Update(&ctx, (unsigned char*)data, len);
2733     return 1;
2734   }
2735
2736   int HmacDigest(unsigned char** md_value, unsigned int *md_len) {
2737     if (!initialised_) return 0;
2738     *md_value = new unsigned char[EVP_MAX_MD_SIZE];
2739     HMAC_Final(&ctx, *md_value, md_len);
2740     HMAC_CTX_cleanup(&ctx);
2741     initialised_ = false;
2742     return 1;
2743   }
2744
2745
2746  protected:
2747
2748   static Handle<Value> New (const Arguments& args) {
2749     HandleScope scope;
2750
2751     Hmac *hmac = new Hmac();
2752     hmac->Wrap(args.This());
2753     return args.This();
2754   }
2755
2756   static Handle<Value> HmacInit(const Arguments& args) {
2757     Hmac *hmac = ObjectWrap::Unwrap<Hmac>(args.This());
2758
2759     HandleScope scope;
2760
2761     if (args.Length() == 0 || !args[0]->IsString()) {
2762       return ThrowException(Exception::Error(String::New(
2763         "Must give hashtype string as argument")));
2764     }
2765
2766     ASSERT_IS_BUFFER(args[1]);
2767     ssize_t len = Buffer::Length(args[1]);
2768
2769     if (len < 0) {
2770       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
2771       return ThrowException(exception);
2772     }
2773
2774     String::Utf8Value hashType(args[0]);
2775
2776     bool r;
2777
2778     if( Buffer::HasInstance(args[1])) {
2779       char* buffer_data = Buffer::Data(args[1]);
2780       size_t buffer_length = Buffer::Length(args[1]);
2781
2782       r = hmac->HmacInit(*hashType, buffer_data, buffer_length);
2783     } else {
2784       char* buf = new char[len];
2785       ssize_t written = DecodeWrite(buf, len, args[1], BINARY);
2786       assert(written == len);
2787
2788       r = hmac->HmacInit(*hashType, buf, len);
2789
2790       delete [] buf;
2791     }
2792
2793     if (!r) {
2794       return ThrowException(Exception::Error(String::New("hmac error")));
2795     }
2796
2797     return args.This();
2798   }
2799
2800   static Handle<Value> HmacUpdate(const Arguments& args) {
2801     Hmac *hmac = ObjectWrap::Unwrap<Hmac>(args.This());
2802
2803     HandleScope scope;
2804
2805     ASSERT_IS_STRING_OR_BUFFER(args[0]);
2806
2807     // Only copy the data if we have to, because it's a string
2808     int r;
2809     if (args[0]->IsString()) {
2810       Local<String> string = args[0].As<String>();
2811       enum encoding encoding = ParseEncoding(args[1], BINARY);
2812       if (!StringBytes::IsValidString(string, encoding))
2813         return ThrowTypeError("Bad input string");
2814       size_t buflen = StringBytes::StorageSize(string, encoding);
2815       char* buf = new char[buflen];
2816       size_t written = StringBytes::Write(buf, buflen, string, encoding);
2817       r = hmac->HmacUpdate(buf, written);
2818       delete[] buf;
2819     } else {
2820       char* buf = Buffer::Data(args[0]);
2821       size_t buflen = Buffer::Length(args[0]);
2822       r = hmac->HmacUpdate(buf, buflen);
2823     }
2824
2825     if (!r) {
2826       Local<Value> exception = Exception::TypeError(String::New("HmacUpdate fail"));
2827       return ThrowException(exception);
2828     }
2829
2830     return args.This();
2831   }
2832
2833   static Handle<Value> HmacDigest(const Arguments& args) {
2834     Hmac *hmac = ObjectWrap::Unwrap<Hmac>(args.This());
2835
2836     HandleScope scope;
2837
2838     enum encoding encoding = BUFFER;
2839     if (args.Length() >= 1) {
2840       encoding = ParseEncoding(args[0]->ToString(), BUFFER);
2841     }
2842
2843     unsigned char* md_value = NULL;
2844     unsigned int md_len = 0;
2845     Local<Value> outString;
2846
2847     int r = hmac->HmacDigest(&md_value, &md_len);
2848     if (r == 0) {
2849       md_value = NULL;
2850       md_len = 0;
2851     }
2852
2853     outString = StringBytes::Encode(
2854           reinterpret_cast<const char*>(md_value), md_len, encoding);
2855
2856     delete[] md_value;
2857     return scope.Close(outString);
2858   }
2859
2860   Hmac () : ObjectWrap () {
2861     initialised_ = false;
2862   }
2863
2864   ~Hmac () {
2865     if (initialised_) {
2866       HMAC_CTX_cleanup(&ctx);
2867     }
2868   }
2869
2870  private:
2871
2872   HMAC_CTX ctx; /* coverity[member_decl] */
2873   const EVP_MD *md; /* coverity[member_decl] */
2874   bool initialised_;
2875 };
2876
2877
2878 class Hash : public ObjectWrap {
2879  public:
2880   static void Initialize (v8::Handle<v8::Object> target) {
2881     HandleScope scope;
2882
2883     Local<FunctionTemplate> t = FunctionTemplate::New(New);
2884
2885     t->InstanceTemplate()->SetInternalFieldCount(1);
2886
2887     NODE_SET_PROTOTYPE_METHOD(t, "update", HashUpdate);
2888     NODE_SET_PROTOTYPE_METHOD(t, "digest", HashDigest);
2889
2890     target->Set(String::NewSymbol("Hash"), t->GetFunction());
2891   }
2892
2893   bool HashInit (const char* hashType) {
2894     md = EVP_get_digestbyname(hashType);
2895     if(!md) return false;
2896     EVP_MD_CTX_init(&mdctx);
2897     EVP_DigestInit_ex(&mdctx, md, NULL);
2898     initialised_ = true;
2899     return true;
2900   }
2901
2902   int HashUpdate(char* data, int len) {
2903     if (!initialised_) return 0;
2904     EVP_DigestUpdate(&mdctx, data, len);
2905     return 1;
2906   }
2907
2908
2909  protected:
2910
2911   static Handle<Value> New (const Arguments& args) {
2912     HandleScope scope;
2913
2914     if (args.Length() == 0 || !args[0]->IsString()) {
2915       return ThrowException(Exception::Error(String::New(
2916         "Must give hashtype string as argument")));
2917     }
2918
2919     String::Utf8Value hashType(args[0]);
2920
2921     Hash *hash = new Hash();
2922     if (!hash->HashInit(*hashType)) {
2923       delete hash;
2924       return ThrowException(Exception::Error(String::New(
2925         "Digest method not supported")));
2926     }
2927
2928     hash->Wrap(args.This());
2929     return args.This();
2930   }
2931
2932   static Handle<Value> HashUpdate(const Arguments& args) {
2933     HandleScope scope;
2934
2935     Hash *hash = ObjectWrap::Unwrap<Hash>(args.This());
2936
2937     ASSERT_IS_STRING_OR_BUFFER(args[0]);
2938
2939     // Only copy the data if we have to, because it's a string
2940     int r;
2941     if (args[0]->IsString()) {
2942       Local<String> string = args[0].As<String>();
2943       enum encoding encoding = ParseEncoding(args[1], BINARY);
2944       if (!StringBytes::IsValidString(string, encoding))
2945         return ThrowTypeError("Bad input string");
2946       size_t buflen = StringBytes::StorageSize(string, encoding);
2947       char* buf = new char[buflen];
2948       size_t written = StringBytes::Write(buf, buflen, string, encoding);
2949       r = hash->HashUpdate(buf, written);
2950       delete[] buf;
2951     } else {
2952       char* buf = Buffer::Data(args[0]);
2953       size_t buflen = Buffer::Length(args[0]);
2954       r = hash->HashUpdate(buf, buflen);
2955     }
2956
2957     if (!r) {
2958       Local<Value> exception = Exception::TypeError(String::New("HashUpdate fail"));
2959       return ThrowException(exception);
2960     }
2961
2962     return args.This();
2963   }
2964
2965   static Handle<Value> HashDigest(const Arguments& args) {
2966     HandleScope scope;
2967
2968     Hash *hash = ObjectWrap::Unwrap<Hash>(args.This());
2969
2970     if (!hash->initialised_) {
2971       return ThrowException(Exception::Error(String::New("Not initialized")));
2972     }
2973
2974     enum encoding encoding = BUFFER;
2975     if (args.Length() >= 1) {
2976       encoding = ParseEncoding(args[0]->ToString(), BUFFER);
2977     }
2978
2979     unsigned char md_value[EVP_MAX_MD_SIZE];
2980     unsigned int md_len;
2981
2982     EVP_DigestFinal_ex(&hash->mdctx, md_value, &md_len);
2983     EVP_MD_CTX_cleanup(&hash->mdctx);
2984     hash->initialised_ = false;
2985
2986     return scope.Close(StringBytes::Encode(
2987           reinterpret_cast<const char*>(md_value), md_len, encoding));
2988   }
2989
2990   Hash () : ObjectWrap () {
2991     initialised_ = false;
2992   }
2993
2994   ~Hash () {
2995     if (initialised_) {
2996       EVP_MD_CTX_cleanup(&mdctx);
2997     }
2998   }
2999
3000  private:
3001
3002   EVP_MD_CTX mdctx; /* coverity[member_decl] */
3003   const EVP_MD *md; /* coverity[member_decl] */
3004   bool initialised_;
3005 };
3006
3007 class Sign : public ObjectWrap {
3008  public:
3009   static void
3010   Initialize (v8::Handle<v8::Object> target) {
3011     HandleScope scope;
3012
3013     Local<FunctionTemplate> t = FunctionTemplate::New(New);
3014
3015     t->InstanceTemplate()->SetInternalFieldCount(1);
3016
3017     NODE_SET_PROTOTYPE_METHOD(t, "init", SignInit);
3018     NODE_SET_PROTOTYPE_METHOD(t, "update", SignUpdate);
3019     NODE_SET_PROTOTYPE_METHOD(t, "sign", SignFinal);
3020
3021     target->Set(String::NewSymbol("Sign"), t->GetFunction());
3022   }
3023
3024   bool SignInit (const char* signType) {
3025     md = EVP_get_digestbyname(signType);
3026     if(!md) {
3027       printf("Unknown message digest %s\n", signType);
3028       return false;
3029     }
3030     EVP_MD_CTX_init(&mdctx);
3031     EVP_SignInit_ex(&mdctx, md, NULL);
3032     initialised_ = true;
3033     return true;
3034
3035   }
3036
3037   int SignUpdate(char* data, int len) {
3038     if (!initialised_) return 0;
3039     EVP_SignUpdate(&mdctx, data, len);
3040     return 1;
3041   }
3042
3043   int SignFinal(unsigned char** md_value,
3044                 unsigned int *md_len,
3045                 char* key_pem,
3046                 int key_pemLen) {
3047     if (!initialised_) return 0;
3048
3049     BIO *bp = NULL;
3050     EVP_PKEY* pkey;
3051     bp = BIO_new(BIO_s_mem());
3052     if(!BIO_write(bp, key_pem, key_pemLen)) return 0;
3053
3054     pkey = PEM_read_bio_PrivateKey( bp, NULL, NULL, NULL );
3055     if (pkey == NULL) {
3056       ERR_print_errors_fp(stderr);
3057       return 0;
3058     }
3059
3060     if (!EVP_SignFinal(&mdctx, *md_value, md_len, pkey)) {
3061       ERR_print_errors_fp(stderr);
3062       return 0;
3063     }
3064     EVP_MD_CTX_cleanup(&mdctx);
3065     initialised_ = false;
3066     EVP_PKEY_free(pkey);
3067     BIO_free(bp);
3068     return 1;
3069   }
3070
3071
3072  protected:
3073
3074   static Handle<Value> New (const Arguments& args) {
3075     HandleScope scope;
3076
3077     Sign *sign = new Sign();
3078     sign->Wrap(args.This());
3079
3080     return args.This();
3081   }
3082
3083   static Handle<Value> SignInit(const Arguments& args) {
3084     HandleScope scope;
3085
3086     Sign *sign = ObjectWrap::Unwrap<Sign>(args.This());
3087
3088     if (args.Length() == 0 || !args[0]->IsString()) {
3089       return ThrowException(Exception::Error(String::New(
3090         "Must give signtype string as argument")));
3091     }
3092
3093     String::Utf8Value signType(args[0]);
3094
3095     bool r = sign->SignInit(*signType);
3096
3097     if (!r) {
3098       return ThrowException(Exception::Error(String::New("SignInit error")));
3099     }
3100
3101     return args.This();
3102   }
3103
3104   static Handle<Value> SignUpdate(const Arguments& args) {
3105     Sign *sign = ObjectWrap::Unwrap<Sign>(args.This());
3106
3107     HandleScope scope;
3108
3109     ASSERT_IS_STRING_OR_BUFFER(args[0]);
3110
3111     // Only copy the data if we have to, because it's a string
3112     int r;
3113     if (args[0]->IsString()) {
3114       Local<String> string = args[0].As<String>();
3115       enum encoding encoding = ParseEncoding(args[1], BINARY);
3116       if (!StringBytes::IsValidString(string, encoding))
3117         return ThrowTypeError("Bad input string");
3118       size_t buflen = StringBytes::StorageSize(string, encoding);
3119       char* buf = new char[buflen];
3120       size_t written = StringBytes::Write(buf, buflen, string, encoding);
3121       r = sign->SignUpdate(buf, written);
3122       delete[] buf;
3123     } else {
3124       char* buf = Buffer::Data(args[0]);
3125       size_t buflen = Buffer::Length(args[0]);
3126       r = sign->SignUpdate(buf, buflen);
3127     }
3128
3129     if (!r) {
3130       Local<Value> exception = Exception::TypeError(String::New("SignUpdate fail"));
3131       return ThrowException(exception);
3132     }
3133
3134     return args.This();
3135   }
3136
3137   static Handle<Value> SignFinal(const Arguments& args) {
3138     Sign *sign = ObjectWrap::Unwrap<Sign>(args.This());
3139
3140     HandleScope scope;
3141
3142     unsigned char* md_value;
3143     unsigned int md_len;
3144     Local<Value> outString;
3145
3146     ASSERT_IS_BUFFER(args[0]);
3147     ssize_t len = Buffer::Length(args[0]);
3148
3149     enum encoding encoding = BUFFER;
3150     if (args.Length() >= 2) {
3151       encoding = ParseEncoding(args[1]->ToString(), BUFFER);
3152     }
3153
3154     char* buf = new char[len];
3155     ssize_t written = DecodeWrite(buf, len, args[0], BUFFER);
3156     assert(written == len);
3157
3158     md_len = 8192; // Maximum key size is 8192 bits
3159     md_value = new unsigned char[md_len];
3160
3161     int r = sign->SignFinal(&md_value, &md_len, buf, len);
3162     if (r == 0) {
3163       delete [] buf;
3164       delete [] md_value;
3165       md_value = NULL;
3166       md_len = r;
3167       return ThrowException(Exception::Error(String::New("SignFinal error")));
3168     }
3169
3170     delete [] buf;
3171
3172     outString = StringBytes::Encode(
3173         reinterpret_cast<const char*>(md_value), md_len, encoding);
3174
3175     delete [] md_value;
3176     return scope.Close(outString);
3177   }
3178
3179   Sign () : ObjectWrap () {
3180     initialised_ = false;
3181   }
3182
3183   ~Sign () {
3184     if (initialised_) {
3185       EVP_MD_CTX_cleanup(&mdctx);
3186     }
3187   }
3188
3189  private:
3190
3191   EVP_MD_CTX mdctx; /* coverity[member_decl] */
3192   const EVP_MD *md; /* coverity[member_decl] */
3193   bool initialised_;
3194 };
3195
3196 class Verify : public ObjectWrap {
3197  public:
3198   static void Initialize (v8::Handle<v8::Object> target) {
3199     HandleScope scope;
3200
3201     Local<FunctionTemplate> t = FunctionTemplate::New(New);
3202
3203     t->InstanceTemplate()->SetInternalFieldCount(1);
3204
3205     NODE_SET_PROTOTYPE_METHOD(t, "init", VerifyInit);
3206     NODE_SET_PROTOTYPE_METHOD(t, "update", VerifyUpdate);
3207     NODE_SET_PROTOTYPE_METHOD(t, "verify", VerifyFinal);
3208
3209     target->Set(String::NewSymbol("Verify"), t->GetFunction());
3210   }
3211
3212
3213   bool VerifyInit (const char* verifyType) {
3214     md = EVP_get_digestbyname(verifyType);
3215     if(!md) {
3216       fprintf(stderr, "node-crypto : Unknown message digest %s\n", verifyType);
3217       return false;
3218     }
3219     EVP_MD_CTX_init(&mdctx);
3220     EVP_VerifyInit_ex(&mdctx, md, NULL);
3221     initialised_ = true;
3222     return true;
3223   }
3224
3225
3226   int VerifyUpdate(char* data, int len) {
3227     if (!initialised_) return 0;
3228     EVP_VerifyUpdate(&mdctx, data, len);
3229     return 1;
3230   }
3231
3232
3233   int VerifyFinal(char* key_pem, int key_pemLen, unsigned char* sig, int siglen) {
3234     if (!initialised_) return 0;
3235
3236     ClearErrorOnReturn clear_error_on_return;
3237     (void) &clear_error_on_return;  // Silence compiler warning.
3238
3239     EVP_PKEY* pkey = NULL;
3240     BIO *bp = NULL;
3241     X509 *x509 = NULL;
3242     int r = 0;
3243
3244     bp = BIO_new(BIO_s_mem());
3245     if (bp == NULL) {
3246       ERR_print_errors_fp(stderr);
3247       return 0;
3248     }
3249     if(!BIO_write(bp, key_pem, key_pemLen)) {
3250       ERR_print_errors_fp(stderr);
3251       return 0;
3252     }
3253
3254     // Check if this is a PKCS#8 or RSA public key before trying as X.509.
3255     // Split this out into a separate function once we have more than one
3256     // consumer of public keys.
3257     if (strncmp(key_pem, PUBLIC_KEY_PFX, PUBLIC_KEY_PFX_LEN) == 0) {
3258       pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL);
3259       if (pkey == NULL) {
3260         ERR_print_errors_fp(stderr);
3261         return 0;
3262       }
3263     } else if (strncmp(key_pem, PUBRSA_KEY_PFX, PUBRSA_KEY_PFX_LEN) == 0) {
3264       RSA* rsa = PEM_read_bio_RSAPublicKey(bp, NULL, NULL, NULL);
3265       if (rsa) {
3266         pkey = EVP_PKEY_new();
3267         if (pkey) EVP_PKEY_set1_RSA(pkey, rsa);
3268         RSA_free(rsa);
3269       }
3270       if (pkey == NULL) {
3271         ERR_print_errors_fp(stderr);
3272         return 0;
3273       }
3274     } else {
3275       // X.509 fallback
3276       x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL);
3277       if (x509 == NULL) {
3278         ERR_print_errors_fp(stderr);
3279         return 0;
3280       }
3281
3282       pkey = X509_get_pubkey(x509);
3283       if (pkey == NULL) {
3284         ERR_print_errors_fp(stderr);
3285         return 0;
3286       }
3287     }
3288
3289     r = EVP_VerifyFinal(&mdctx, sig, siglen, pkey);
3290
3291     if(pkey != NULL)
3292       EVP_PKEY_free (pkey);
3293     if (x509 != NULL)
3294       X509_free(x509);
3295     if (bp != NULL)
3296       BIO_free(bp);
3297     EVP_MD_CTX_cleanup(&mdctx);
3298     initialised_ = false;
3299
3300     return r;
3301   }
3302
3303
3304  protected:
3305
3306   static Handle<Value> New (const Arguments& args) {
3307     HandleScope scope;
3308
3309     Verify *verify = new Verify();
3310     verify->Wrap(args.This());
3311
3312     return args.This();
3313   }
3314
3315
3316   static Handle<Value> VerifyInit(const Arguments& args) {
3317     Verify *verify = ObjectWrap::Unwrap<Verify>(args.This());
3318
3319     HandleScope scope;
3320
3321     if (args.Length() == 0 || !args[0]->IsString()) {
3322       return ThrowException(Exception::Error(String::New(
3323         "Must give verifytype string as argument")));
3324     }
3325
3326     String::Utf8Value verifyType(args[0]);
3327
3328     bool r = verify->VerifyInit(*verifyType);
3329
3330     if (!r) {
3331       return ThrowException(Exception::Error(String::New("VerifyInit error")));
3332     }
3333
3334     return args.This();
3335   }
3336
3337
3338   static Handle<Value> VerifyUpdate(const Arguments& args) {
3339     HandleScope scope;
3340
3341     Verify *verify = ObjectWrap::Unwrap<Verify>(args.This());
3342
3343     ASSERT_IS_STRING_OR_BUFFER(args[0]);
3344
3345     // Only copy the data if we have to, because it's a string
3346     int r;
3347     if (args[0]->IsString()) {
3348       Local<String> string = args[0].As<String>();
3349       enum encoding encoding = ParseEncoding(args[1], BINARY);
3350       if (!StringBytes::IsValidString(string, encoding))
3351         return ThrowTypeError("Bad input string");
3352       size_t buflen = StringBytes::StorageSize(string, encoding);
3353       char* buf = new char[buflen];
3354       size_t written = StringBytes::Write(buf, buflen, string, encoding);
3355       r = verify->VerifyUpdate(buf, written);
3356       delete[] buf;
3357     } else {
3358       char* buf = Buffer::Data(args[0]);
3359       size_t buflen = Buffer::Length(args[0]);
3360       r = verify->VerifyUpdate(buf, buflen);
3361     }
3362
3363     if (!r) {
3364       Local<Value> exception = Exception::TypeError(String::New("VerifyUpdate fail"));
3365       return ThrowException(exception);
3366     }
3367
3368     return args.This();
3369   }
3370
3371
3372   static Handle<Value> VerifyFinal(const Arguments& args) {
3373     HandleScope scope;
3374
3375     Verify *verify = ObjectWrap::Unwrap<Verify>(args.This());
3376
3377     ASSERT_IS_BUFFER(args[0]);
3378     ssize_t klen = Buffer::Length(args[0]);
3379
3380     if (klen < 0) {
3381       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
3382       return ThrowException(exception);
3383     }
3384
3385     char* kbuf = new char[klen];
3386     ssize_t kwritten = DecodeWrite(kbuf, klen, args[0], BINARY);
3387     assert(kwritten == klen);
3388
3389     ASSERT_IS_STRING_OR_BUFFER(args[1]);
3390
3391     // BINARY works for both buffers and binary strings.
3392     enum encoding encoding = BINARY;
3393     if (args.Length() >= 3) {
3394       encoding = ParseEncoding(args[2]->ToString(), BINARY);
3395     }
3396
3397     ssize_t hlen = StringBytes::Size(args[1], encoding);
3398
3399     if (hlen < 0) {
3400       delete[] kbuf;
3401       Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
3402       return ThrowException(exception);
3403     }
3404
3405     unsigned char* hbuf = new unsigned char[hlen];
3406     ssize_t hwritten = StringBytes::Write(
3407         reinterpret_cast<char*>(hbuf), hlen, args[1], encoding);
3408     assert(hwritten == hlen);
3409
3410     int r;
3411     r = verify->VerifyFinal(kbuf, klen, hbuf, hlen);
3412
3413     delete[] kbuf;
3414     delete[] hbuf;
3415
3416     return Boolean::New(r && r != -1);
3417   }
3418
3419   Verify () : ObjectWrap () {
3420     initialised_ = false;
3421   }
3422
3423   ~Verify () {
3424     if (initialised_) {
3425       EVP_MD_CTX_cleanup(&mdctx);
3426     }
3427   }
3428
3429  private:
3430
3431   EVP_MD_CTX mdctx; /* coverity[member_decl] */
3432   const EVP_MD *md; /* coverity[member_decl] */
3433   bool initialised_;
3434
3435 };
3436
3437 class DiffieHellman : public ObjectWrap {
3438  public:
3439   static void Initialize(v8::Handle<v8::Object> target) {
3440     HandleScope scope;
3441
3442     Local<FunctionTemplate> t = FunctionTemplate::New(New);
3443
3444     t->InstanceTemplate()->SetInternalFieldCount(1);
3445
3446     NODE_SET_PROTOTYPE_METHOD(t, "generateKeys", GenerateKeys);
3447     NODE_SET_PROTOTYPE_METHOD(t, "computeSecret", ComputeSecret);
3448     NODE_SET_PROTOTYPE_METHOD(t, "getPrime", GetPrime);
3449     NODE_SET_PROTOTYPE_METHOD(t, "getGenerator", GetGenerator);
3450     NODE_SET_PROTOTYPE_METHOD(t, "getPublicKey", GetPublicKey);
3451     NODE_SET_PROTOTYPE_METHOD(t, "getPrivateKey", GetPrivateKey);
3452     NODE_SET_PROTOTYPE_METHOD(t, "setPublicKey", SetPublicKey);
3453     NODE_SET_PROTOTYPE_METHOD(t, "setPrivateKey", SetPrivateKey);
3454
3455     target->Set(String::NewSymbol("DiffieHellman"), t->GetFunction());
3456
3457     Local<FunctionTemplate> t2 = FunctionTemplate::New(DiffieHellmanGroup);
3458     t2->InstanceTemplate()->SetInternalFieldCount(1);
3459
3460     NODE_SET_PROTOTYPE_METHOD(t2, "generateKeys", GenerateKeys);
3461     NODE_SET_PROTOTYPE_METHOD(t2, "computeSecret", ComputeSecret);
3462     NODE_SET_PROTOTYPE_METHOD(t2, "getPrime", GetPrime);
3463     NODE_SET_PROTOTYPE_METHOD(t2, "getGenerator", GetGenerator);
3464     NODE_SET_PROTOTYPE_METHOD(t2, "getPublicKey", GetPublicKey);
3465     NODE_SET_PROTOTYPE_METHOD(t2, "getPrivateKey", GetPrivateKey);
3466
3467     target->Set(String::NewSymbol("DiffieHellmanGroup"), t2->GetFunction());
3468   }
3469
3470   bool Init(int primeLength) {
3471     dh = DH_new();
3472     DH_generate_parameters_ex(dh, primeLength, DH_GENERATOR_2, 0);
3473     bool result = VerifyContext();
3474     if (!result) return false;
3475     initialised_ = true;
3476     return true;
3477   }
3478
3479   bool Init(unsigned char* p, int p_len) {
3480     dh = DH_new();
3481     dh->p = BN_bin2bn(p, p_len, 0);
3482     dh->g = BN_new();
3483     if (!BN_set_word(dh->g, 2)) return false;
3484     bool result = VerifyContext();
3485     if (!result) return false;
3486     initialised_ = true;
3487     return true;
3488   }
3489
3490   bool Init(unsigned char* p, int p_len, unsigned char* g, int g_len) {
3491     dh = DH_new();
3492     dh->p = BN_bin2bn(p, p_len, 0);
3493     dh->g = BN_bin2bn(g, g_len, 0);
3494     initialised_ = true;
3495     return true;
3496   }
3497
3498  protected:
3499   static Handle<Value> DiffieHellmanGroup(const Arguments& args) {
3500     HandleScope scope;
3501
3502     DiffieHellman* diffieHellman = new DiffieHellman();
3503
3504     if (args.Length() != 1 || !args[0]->IsString()) {
3505       return ThrowException(Exception::Error(
3506           String::New("No group name given")));
3507     }
3508
3509     String::Utf8Value group_name(args[0]);
3510
3511     modp_group* it = modp_groups;
3512
3513     while(it->name != NULL) {
3514       if (!strcasecmp(*group_name, it->name))
3515           break;
3516       it++;
3517     }
3518
3519     if (it->name != NULL) {
3520       diffieHellman->Init(it->prime, it->prime_size,
3521               it->gen, it->gen_size);
3522     } else {
3523       return ThrowException(Exception::Error(
3524           String::New("Unknown group")));
3525     }
3526
3527     diffieHellman->Wrap(args.This());
3528
3529     return args.This();
3530   }
3531
3532   static Handle<Value> New(const Arguments& args) {
3533     HandleScope scope;
3534
3535     DiffieHellman* diffieHellman = new DiffieHellman();
3536     bool initialized = false;
3537
3538     if (args.Length() > 0) {
3539       if (args[0]->IsInt32()) {
3540         initialized = diffieHellman->Init(args[0]->Int32Value());
3541       } else {
3542         initialized = diffieHellman->Init(
3543                 reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3544                 Buffer::Length(args[0]));
3545       }
3546     }
3547
3548     if (!initialized) {
3549       return ThrowException(Exception::Error(
3550             String::New("Initialization failed")));
3551     }
3552
3553     diffieHellman->Wrap(args.This());
3554
3555     return args.This();
3556   }
3557
3558   static Handle<Value> GenerateKeys(const Arguments& args) {
3559     DiffieHellman* diffieHellman =
3560       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3561
3562     HandleScope scope;
3563
3564     if (!diffieHellman->initialised_) {
3565       return ThrowException(Exception::Error(
3566             String::New("Not initialized")));
3567     }
3568
3569     if (!DH_generate_key(diffieHellman->dh)) {
3570       return ThrowException(Exception::Error(
3571             String::New("Key generation failed")));
3572     }
3573
3574     Local<Value> outString;
3575
3576     int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
3577     char* data = new char[dataSize];
3578     BN_bn2bin(diffieHellman->dh->pub_key,
3579         reinterpret_cast<unsigned char*>(data));
3580
3581     outString = Encode(data, dataSize, BUFFER);
3582     delete[] data;
3583
3584     return scope.Close(outString);
3585   }
3586
3587   static Handle<Value> GetPrime(const Arguments& args) {
3588     DiffieHellman* diffieHellman =
3589       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3590
3591     HandleScope scope;
3592
3593     if (!diffieHellman->initialised_) {
3594       return ThrowException(Exception::Error(String::New("Not initialized")));
3595     }
3596
3597     int dataSize = BN_num_bytes(diffieHellman->dh->p);
3598     char* data = new char[dataSize];
3599     BN_bn2bin(diffieHellman->dh->p, reinterpret_cast<unsigned char*>(data));
3600
3601     Local<Value> outString;
3602
3603     outString = Encode(data, dataSize, BUFFER);
3604
3605     delete[] data;
3606
3607     return scope.Close(outString);
3608   }
3609
3610   static Handle<Value> GetGenerator(const Arguments& args) {
3611     DiffieHellman* diffieHellman =
3612       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3613
3614     HandleScope scope;
3615
3616     if (!diffieHellman->initialised_) {
3617       return ThrowException(Exception::Error(String::New("Not initialized")));
3618     }
3619
3620     int dataSize = BN_num_bytes(diffieHellman->dh->g);
3621     char* data = new char[dataSize];
3622     BN_bn2bin(diffieHellman->dh->g, reinterpret_cast<unsigned char*>(data));
3623
3624     Local<Value> outString;
3625
3626     outString = Encode(data, dataSize, BUFFER);
3627
3628     delete[] data;
3629
3630     return scope.Close(outString);
3631   }
3632
3633   static Handle<Value> GetPublicKey(const Arguments& args) {
3634     DiffieHellman* diffieHellman =
3635       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3636
3637     HandleScope scope;
3638
3639     if (!diffieHellman->initialised_) {
3640       return ThrowException(Exception::Error(String::New("Not initialized")));
3641     }
3642
3643     if (diffieHellman->dh->pub_key == NULL) {
3644       return ThrowException(Exception::Error(
3645             String::New("No public key - did you forget to generate one?")));
3646     }
3647
3648     int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
3649     char* data = new char[dataSize];
3650     BN_bn2bin(diffieHellman->dh->pub_key,
3651         reinterpret_cast<unsigned char*>(data));
3652
3653     Local<Value> outString;
3654
3655     outString = Encode(data, dataSize, BUFFER);
3656
3657     delete[] data;
3658
3659     return scope.Close(outString);
3660   }
3661
3662   static Handle<Value> GetPrivateKey(const Arguments& args) {
3663     DiffieHellman* diffieHellman =
3664       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3665
3666     HandleScope scope;
3667
3668     if (!diffieHellman->initialised_) {
3669       return ThrowException(Exception::Error(String::New("Not initialized")));
3670     }
3671
3672     if (diffieHellman->dh->priv_key == NULL) {
3673       return ThrowException(Exception::Error(
3674             String::New("No private key - did you forget to generate one?")));
3675     }
3676
3677     int dataSize = BN_num_bytes(diffieHellman->dh->priv_key);
3678     char* data = new char[dataSize];
3679     BN_bn2bin(diffieHellman->dh->priv_key,
3680         reinterpret_cast<unsigned char*>(data));
3681
3682     Local<Value> outString;
3683
3684     outString = Encode(data, dataSize, BUFFER);
3685
3686     delete[] data;
3687
3688     return scope.Close(outString);
3689   }
3690
3691   static Handle<Value> ComputeSecret(const Arguments& args) {
3692     HandleScope scope;
3693
3694     DiffieHellman* diffieHellman =
3695       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3696
3697     if (!diffieHellman->initialised_) {
3698       return ThrowException(Exception::Error(String::New("Not initialized")));
3699     }
3700
3701     ClearErrorOnReturn clear_error_on_return;
3702     (void) &clear_error_on_return;  // Silence compiler warning.
3703     BIGNUM* key = 0;
3704
3705     if (args.Length() == 0) {
3706       return ThrowException(Exception::Error(
3707             String::New("First argument must be other party's public key")));
3708     } else {
3709       ASSERT_IS_BUFFER(args[0]);
3710       key = BN_bin2bn(
3711         reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3712         Buffer::Length(args[0]), 0);
3713     }
3714
3715     int dataSize = DH_size(diffieHellman->dh);
3716     char* data = new char[dataSize];
3717
3718     int size = DH_compute_key(reinterpret_cast<unsigned char*>(data),
3719       key, diffieHellman->dh);
3720
3721     if (size == -1) {
3722       int checkResult;
3723       int checked;
3724
3725       checked = DH_check_pub_key(diffieHellman->dh, key, &checkResult);
3726       BN_free(key);
3727       delete[] data;
3728
3729       if (!checked) {
3730         return ThrowException(Exception::Error(String::New("Invalid key")));
3731       } else if (checkResult) {
3732         if (checkResult & DH_CHECK_PUBKEY_TOO_SMALL) {
3733           return ThrowException(Exception::Error(
3734                 String::New("Supplied key is too small")));
3735         } else if (checkResult & DH_CHECK_PUBKEY_TOO_LARGE) {
3736           return ThrowException(Exception::Error(
3737                 String::New("Supplied key is too large")));
3738         } else {
3739           return ThrowException(Exception::Error(String::New("Invalid key")));
3740         }
3741       } else {
3742         return ThrowException(Exception::Error(String::New("Invalid key")));
3743       }
3744     }
3745
3746     BN_free(key);
3747     assert(size >= 0);
3748
3749     // DH_size returns number of bytes in a prime number
3750     // DH_compute_key returns number of bytes in a remainder of exponent, which
3751     // may have less bytes than a prime number. Therefore add 0-padding to the
3752     // allocated buffer.
3753     if (size != dataSize) {
3754       assert(dataSize > size);
3755       memmove(data + dataSize - size, data, size);
3756       memset(data, 0, dataSize - size);
3757     }
3758
3759     Local<Value> outString;
3760
3761     outString = Encode(data, dataSize, BUFFER);
3762
3763     delete[] data;
3764     return scope.Close(outString);
3765   }
3766
3767   static Handle<Value> SetPublicKey(const Arguments& args) {
3768     HandleScope scope;
3769
3770     DiffieHellman* diffieHellman =
3771       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3772
3773     if (!diffieHellman->initialised_) {
3774       return ThrowException(Exception::Error(String::New("Not initialized")));
3775     }
3776
3777     if (args.Length() == 0) {
3778       return ThrowException(Exception::Error(
3779             String::New("First argument must be public key")));
3780     } else {
3781       ASSERT_IS_BUFFER(args[0]);
3782       diffieHellman->dh->pub_key =
3783         BN_bin2bn(
3784           reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3785           Buffer::Length(args[0]), 0);
3786     }
3787
3788     return args.This();
3789   }
3790
3791   static Handle<Value> SetPrivateKey(const Arguments& args) {
3792     HandleScope scope;
3793
3794     DiffieHellman* diffieHellman =
3795       ObjectWrap::Unwrap<DiffieHellman>(args.This());
3796
3797     if (!diffieHellman->initialised_) {
3798       return ThrowException(Exception::Error(
3799             String::New("Not initialized")));
3800     }
3801
3802     if (args.Length() == 0) {
3803       return ThrowException(Exception::Error(
3804             String::New("First argument must be private key")));
3805     } else {
3806       ASSERT_IS_BUFFER(args[0]);
3807       diffieHellman->dh->priv_key =
3808         BN_bin2bn(
3809           reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3810           Buffer::Length(args[0]), 0);
3811     }
3812
3813     return args.This();
3814   }
3815
3816   DiffieHellman() : ObjectWrap() {
3817     initialised_ = false;
3818     dh = NULL;
3819   }
3820
3821   ~DiffieHellman() {
3822     if (dh != NULL) {
3823       DH_free(dh);
3824     }
3825   }
3826
3827  private:
3828   bool VerifyContext() {
3829     int codes;
3830     if (!DH_check(dh, &codes)) return false;
3831     if (codes & DH_CHECK_P_NOT_SAFE_PRIME) return false;
3832     if (codes & DH_CHECK_P_NOT_PRIME) return false;
3833     if (codes & DH_UNABLE_TO_CHECK_GENERATOR) return false;
3834     if (codes & DH_NOT_SUITABLE_GENERATOR) return false;
3835     return true;
3836   }
3837
3838   bool initialised_;
3839   DH* dh;
3840 };
3841
3842
3843 struct pbkdf2_req {
3844   uv_work_t work_req;
3845   int err;
3846   char* pass;
3847   size_t passlen;
3848   char* salt;
3849   size_t saltlen;
3850   size_t iter;
3851   char* key;
3852   size_t keylen;
3853   Persistent<Object> obj;
3854 };
3855
3856
3857 void EIO_PBKDF2(pbkdf2_req* req) {
3858   req->err = PKCS5_PBKDF2_HMAC_SHA1(
3859     req->pass,
3860     req->passlen,
3861     (unsigned char*)req->salt,
3862     req->saltlen,
3863     req->iter,
3864     req->keylen,
3865     (unsigned char*)req->key);
3866   memset(req->pass, 0, req->passlen);
3867   memset(req->salt, 0, req->saltlen);
3868 }
3869
3870
3871 void EIO_PBKDF2(uv_work_t* work_req) {
3872   pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req);
3873   EIO_PBKDF2(req);
3874 }
3875
3876
3877 void EIO_PBKDF2After(pbkdf2_req* req, Local<Value> argv[2]) {
3878   if (req->err) {
3879     argv[0] = Local<Value>::New(Undefined());
3880     argv[1] = Encode(req->key, req->keylen, BUFFER);
3881     memset(req->key, 0, req->keylen);
3882   } else {
3883     argv[0] = Exception::Error(String::New("PBKDF2 error"));
3884     argv[1] = Local<Value>::New(Undefined());
3885   }
3886
3887   delete[] req->pass;
3888   delete[] req->salt;
3889   delete[] req->key;
3890   delete req;
3891 }
3892
3893
3894 void EIO_PBKDF2After(uv_work_t* work_req, int status) {
3895   assert(status == 0);
3896   pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req);
3897   HandleScope scope;
3898   Local<Value> argv[2];
3899   Persistent<Object> obj = req->obj;
3900   EIO_PBKDF2After(req, argv);
3901   MakeCallback(obj, "ondone", ARRAY_SIZE(argv), argv);
3902   obj.Dispose();
3903 }
3904
3905
3906 Handle<Value> PBKDF2(const Arguments& args) {
3907   HandleScope scope;
3908
3909   const char* type_error = NULL;
3910   char* pass = NULL;
3911   char* salt = NULL;
3912   ssize_t passlen = -1;
3913   ssize_t saltlen = -1;
3914   ssize_t keylen = -1;
3915   ssize_t pass_written = -1;
3916   ssize_t salt_written = -1;
3917   ssize_t iter = -1;
3918   pbkdf2_req* req = NULL;
3919
3920   if (args.Length() != 4 && args.Length() != 5) {
3921     type_error = "Bad parameter";
3922     goto err;
3923   }
3924
3925   ASSERT_IS_BUFFER(args[0]);
3926   passlen = Buffer::Length(args[0]);
3927   if (passlen < 0) {
3928     type_error = "Bad password";
3929     goto err;
3930   }
3931
3932   pass = new char[passlen];
3933   pass_written = DecodeWrite(pass, passlen, args[0], BINARY);
3934   assert(pass_written == passlen);
3935
3936   ASSERT_IS_BUFFER(args[1]);
3937   saltlen = Buffer::Length(args[1]);
3938   if (saltlen < 0) {
3939     type_error = "Bad salt";
3940     goto err;
3941   }
3942
3943   salt = new char[saltlen];
3944   salt_written = DecodeWrite(salt, saltlen, args[1], BINARY);
3945   assert(salt_written == saltlen);
3946
3947   if (!args[2]->IsNumber()) {
3948     type_error = "Iterations not a number";
3949     goto err;
3950   }
3951
3952   iter = args[2]->Int32Value();
3953   if (iter < 0) {
3954     type_error = "Bad iterations";
3955     goto err;
3956   }
3957
3958   if (!args[3]->IsNumber()) {
3959     type_error = "Key length not a number";
3960     goto err;
3961   }
3962
3963   keylen = args[3]->Int32Value();
3964   if (keylen < 0) {
3965     type_error = "Bad key length";
3966     goto err;
3967   }
3968
3969   req = new pbkdf2_req;
3970   req->err = 0;
3971   req->pass = pass;
3972   req->passlen = passlen;
3973   req->salt = salt;
3974   req->saltlen = saltlen;
3975   req->iter = iter;
3976   req->key = new char[keylen];
3977   req->keylen = keylen;
3978
3979   if (args[4]->IsFunction()) {
3980     req->obj = Persistent<Object>::New(Object::New());
3981     req->obj->Set(String::New("ondone"), args[4]);
3982     uv_queue_work(uv_default_loop(),
3983                   &req->work_req,
3984                   EIO_PBKDF2,
3985                   EIO_PBKDF2After);
3986     return Undefined();
3987   } else {
3988     Local<Value> argv[2];
3989     EIO_PBKDF2(req);
3990     EIO_PBKDF2After(req, argv);
3991     if (argv[0]->IsObject()) return ThrowException(argv[0]);
3992     return scope.Close(argv[1]);
3993   }
3994
3995 err:
3996   delete[] salt;
3997   delete[] pass;
3998   return ThrowException(Exception::TypeError(String::New(type_error)));
3999 }
4000
4001
4002 struct RandomBytesRequest {
4003   ~RandomBytesRequest();
4004   Persistent<Object> obj_;
4005   unsigned long error_; // openssl error code or zero
4006   uv_work_t work_req_;
4007   size_t size_;
4008   char* data_;
4009 };
4010
4011
4012 RandomBytesRequest::~RandomBytesRequest() {
4013   if (obj_.IsEmpty()) return;
4014   obj_.Dispose();
4015   obj_.Clear();
4016 }
4017
4018
4019 void RandomBytesFree(char* data, void* hint) {
4020   delete[] data;
4021 }
4022
4023
4024 template <bool pseudoRandom>
4025 void RandomBytesWork(uv_work_t* work_req) {
4026   RandomBytesRequest* req = container_of(work_req,
4027                                          RandomBytesRequest,
4028                                          work_req_);
4029   int r;
4030
4031   // Ensure that OpenSSL's PRNG is properly seeded.
4032   CheckEntropy();
4033
4034   if (pseudoRandom == true) {
4035     r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data_),
4036                           req->size_);
4037   } else {
4038     r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data_), req->size_);
4039   }
4040
4041   // RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
4042   // result is not cryptographically strong - but that's not an error.
4043   if (r == 0 && pseudoRandom == false) {
4044     req->error_ = ERR_get_error();
4045   } else if (r == -1) {
4046     req->error_ = static_cast<unsigned long>(-1);
4047   }
4048 }
4049
4050
4051 // don't call this function without a valid HandleScope
4052 void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
4053   if (req->error_) {
4054     char errmsg[256] = "Operation not supported";
4055
4056     if (req->error_ != (unsigned long) -1)
4057       ERR_error_string_n(req->error_, errmsg, sizeof errmsg);
4058
4059     argv[0] = Exception::Error(String::New(errmsg));
4060     argv[1] = Local<Value>::New(Null());
4061   }
4062   else {
4063     // avoids the malloc + memcpy
4064     Buffer* buffer = Buffer::New(req->data_, req->size_, RandomBytesFree, NULL);
4065     argv[0] = Local<Value>::New(Null());
4066     argv[1] = Local<Object>::New(buffer->handle_);
4067     req->data_ = NULL;
4068   }
4069   free(req->data_);
4070 }
4071
4072
4073 void RandomBytesAfter(uv_work_t* work_req, int status) {
4074   assert(status == 0);
4075   RandomBytesRequest* req = container_of(work_req,
4076                                          RandomBytesRequest,
4077                                          work_req_);
4078   HandleScope scope;
4079   Local<Value> argv[2];
4080   RandomBytesCheck(req, argv);
4081   MakeCallback(req->obj_, "ondone", ARRAY_SIZE(argv), argv);
4082   delete req;
4083 }
4084
4085
4086 template <bool pseudoRandom>
4087 Handle<Value> RandomBytes(const Arguments& args) {
4088   HandleScope scope;
4089
4090   // maybe allow a buffer to write to? cuts down on object creation
4091   // when generating random data in a loop
4092   if (!args[0]->IsUint32()) {
4093     return ThrowTypeError("Argument #1 must be number > 0");
4094   }
4095
4096   const uint32_t size = args[0]->Uint32Value();
4097   if (size > Buffer::kMaxLength) {
4098     return ThrowTypeError("size > Buffer::kMaxLength");
4099   }
4100
4101   RandomBytesRequest* req = new RandomBytesRequest();
4102   req->error_ = 0;
4103   req->data_ = new char[size];
4104   req->size_ = size;
4105
4106   if (args[1]->IsFunction()) {
4107     req->obj_ = Persistent<Object>::New(Object::New());
4108     req->obj_->Set(String::New("ondone"), args[1]);
4109
4110     uv_queue_work(uv_default_loop(),
4111                   &req->work_req_,
4112                   RandomBytesWork<pseudoRandom>,
4113                   RandomBytesAfter);
4114
4115     return req->obj_;
4116   }
4117   else {
4118     Local<Value> argv[2];
4119     RandomBytesWork<pseudoRandom>(&req->work_req_);
4120     RandomBytesCheck(req, argv);
4121     delete req;
4122
4123     if (!argv[0]->IsNull())
4124       return ThrowException(argv[0]);
4125     else
4126       return argv[1];
4127   }
4128 }
4129
4130
4131 Handle<Value> GetSSLCiphers(const Arguments& args) {
4132   HandleScope scope;
4133
4134   SSL_CTX* ctx = SSL_CTX_new(TLSv1_server_method());
4135   if (ctx == NULL) {
4136     return ThrowError("SSL_CTX_new() failed.");
4137   }
4138
4139   SSL* ssl = SSL_new(ctx);
4140   if (ssl == NULL) {
4141     SSL_CTX_free(ctx);
4142     return ThrowError("SSL_new() failed.");
4143   }
4144
4145   Local<Array> arr = Array::New();
4146   STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl);
4147
4148   for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
4149     SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
4150     arr->Set(i, String::New(SSL_CIPHER_get_name(cipher)));
4151   }
4152
4153   SSL_free(ssl);
4154   SSL_CTX_free(ctx);
4155
4156   return scope.Close(arr);
4157 }
4158
4159
4160 template <class TypeName>
4161 static void array_push_back(const TypeName* md,
4162                             const char* from,
4163                             const char* to,
4164                             void* arg) {
4165   Local<Array>& arr = *static_cast<Local<Array>*>(arg);
4166   arr->Set(arr->Length(), String::New(from));
4167 }
4168
4169
4170 Handle<Value> GetCiphers(const Arguments& args) {
4171   HandleScope scope;
4172   Local<Array> arr = Array::New();
4173   EVP_CIPHER_do_all_sorted(array_push_back<EVP_CIPHER>, &arr);
4174   return scope.Close(arr);
4175 }
4176
4177
4178 Handle<Value> GetHashes(const Arguments& args) {
4179   HandleScope scope;
4180   Local<Array> arr = Array::New();
4181   EVP_MD_do_all_sorted(array_push_back<EVP_MD>, &arr);
4182   return scope.Close(arr);
4183 }
4184
4185
4186 void InitCrypto(Handle<Object> target) {
4187   HandleScope scope;
4188
4189   SSL_library_init();
4190   OpenSSL_add_all_algorithms();
4191   OpenSSL_add_all_digests();
4192   SSL_load_error_strings();
4193   ERR_load_crypto_strings();
4194
4195   crypto_lock_init();
4196   CRYPTO_set_locking_callback(crypto_lock_cb);
4197   CRYPTO_THREADID_set_callback(crypto_threadid_cb);
4198
4199   // Turn off compression. Saves memory - do it in userland.
4200 #if !defined(OPENSSL_NO_COMP)
4201   STACK_OF(SSL_COMP)* comp_methods =
4202 #if OPENSSL_VERSION_NUMBER < 0x00908000L
4203     SSL_COMP_get_compression_method()
4204 #else
4205     SSL_COMP_get_compression_methods()
4206 #endif
4207   ;
4208   sk_SSL_COMP_zero(comp_methods);
4209   assert(sk_SSL_COMP_num(comp_methods) == 0);
4210 #endif
4211
4212   SecureContext::Initialize(target);
4213   Connection::Initialize(target);
4214   Cipher::Initialize(target);
4215   Decipher::Initialize(target);
4216   DiffieHellman::Initialize(target);
4217   Hmac::Initialize(target);
4218   Hash::Initialize(target);
4219   Sign::Initialize(target);
4220   Verify::Initialize(target);
4221
4222   NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
4223   NODE_SET_METHOD(target, "randomBytes", RandomBytes<false>);
4224   NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<true>);
4225   NODE_SET_METHOD(target, "getSSLCiphers", GetSSLCiphers);
4226   NODE_SET_METHOD(target, "getCiphers", GetCiphers);
4227   NODE_SET_METHOD(target, "getHashes", GetHashes);
4228
4229   subject_symbol    = NODE_PSYMBOL("subject");
4230   issuer_symbol     = NODE_PSYMBOL("issuer");
4231   valid_from_symbol = NODE_PSYMBOL("valid_from");
4232   valid_to_symbol   = NODE_PSYMBOL("valid_to");
4233   subjectaltname_symbol = NODE_PSYMBOL("subjectaltname");
4234   modulus_symbol        = NODE_PSYMBOL("modulus");
4235   exponent_symbol       = NODE_PSYMBOL("exponent");
4236   fingerprint_symbol   = NODE_PSYMBOL("fingerprint");
4237   name_symbol       = NODE_PSYMBOL("name");
4238   version_symbol    = NODE_PSYMBOL("version");
4239   ext_key_usage_symbol = NODE_PSYMBOL("ext_key_usage");
4240 }
4241
4242 }  // namespace crypto
4243 }  // namespace node
4244
4245 NODE_MODULE(node_crypto, node::crypto::InitCrypto)