75c832d13fb39e6626cf90e75ef72a30750bc3c6
[platform/upstream/nodejs.git] / src / node_crypto.cc
1 #include "node.h"
2 #include "node_buffer.h"
3 #include "node_crypto.h"
4 #include "node_crypto_bio.h"
5 #include "node_crypto_groups.h"
6 #include "tls_wrap.h"  // TLSCallbacks
7
8 #include "async-wrap.h"
9 #include "async-wrap-inl.h"
10 #include "env.h"
11 #include "env-inl.h"
12 #include "string_bytes.h"
13 #include "util.h"
14 #include "util-inl.h"
15 #include "v8.h"
16
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #if defined(_MSC_VER)
22 #define strcasecmp _stricmp
23 #endif
24
25 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
26 #define OPENSSL_CONST const
27 #else
28 #define OPENSSL_CONST
29 #endif
30
31 #define THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(val)         \
32   do {                                                        \
33     if (!Buffer::HasInstance(val) && !val->IsString()) {      \
34       return env->ThrowTypeError("Not a string or buffer");   \
35     }                                                         \
36   } while (0)
37
38 #define THROW_AND_RETURN_IF_NOT_BUFFER(val)                   \
39   do {                                                        \
40     if (!Buffer::HasInstance(val)) {                          \
41       return env->ThrowTypeError("Not a buffer");             \
42     }                                                         \
43   } while (0)
44
45 static const char PUBLIC_KEY_PFX[] =  "-----BEGIN PUBLIC KEY-----";
46 static const int PUBLIC_KEY_PFX_LEN = sizeof(PUBLIC_KEY_PFX) - 1;
47 static const char PUBRSA_KEY_PFX[] =  "-----BEGIN RSA PUBLIC KEY-----";
48 static const int PUBRSA_KEY_PFX_LEN = sizeof(PUBRSA_KEY_PFX) - 1;
49 static const char CERTIFICATE_PFX[] =  "-----BEGIN CERTIFICATE-----";
50 static const int CERTIFICATE_PFX_LEN = sizeof(CERTIFICATE_PFX) - 1;
51
52 static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL
53                                  | ASN1_STRFLGS_UTF8_CONVERT
54                                  | XN_FLAG_SEP_MULTILINE
55                                  | XN_FLAG_FN_SN;
56
57 namespace node {
58 namespace crypto {
59
60 using v8::Array;
61 using v8::Boolean;
62 using v8::Context;
63 using v8::EscapableHandleScope;
64 using v8::Exception;
65 using v8::External;
66 using v8::False;
67 using v8::FunctionCallbackInfo;
68 using v8::FunctionTemplate;
69 using v8::Handle;
70 using v8::HandleScope;
71 using v8::Integer;
72 using v8::Isolate;
73 using v8::Local;
74 using v8::Null;
75 using v8::Object;
76 using v8::Persistent;
77 using v8::PropertyAttribute;
78 using v8::PropertyCallbackInfo;
79 using v8::String;
80 using v8::V8;
81 using v8::Value;
82
83
84 // Forcibly clear OpenSSL's error stack on return. This stops stale errors
85 // from popping up later in the lifecycle of crypto operations where they
86 // would cause spurious failures. It's a rather blunt method, though.
87 // ERR_clear_error() isn't necessarily cheap either.
88 struct ClearErrorOnReturn {
89   ~ClearErrorOnReturn() { ERR_clear_error(); }
90 };
91
92 static uv_rwlock_t* locks;
93
94 const char* const root_certs[] = {
95 #include "node_root_certs.h"  // NOLINT(build/include_order)
96 };
97
98 X509_STORE* root_cert_store;
99
100 // Just to generate static methods
101 template class SSLWrap<TLSCallbacks>;
102 template void SSLWrap<TLSCallbacks>::AddMethods(Environment* env,
103                                                 Handle<FunctionTemplate> t);
104 template void SSLWrap<TLSCallbacks>::InitNPN(SecureContext* sc);
105 template SSL_SESSION* SSLWrap<TLSCallbacks>::GetSessionCallback(
106     SSL* s,
107     unsigned char* key,
108     int len,
109     int* copy);
110 template int SSLWrap<TLSCallbacks>::NewSessionCallback(SSL* s,
111                                                        SSL_SESSION* sess);
112 template void SSLWrap<TLSCallbacks>::OnClientHello(
113     void* arg,
114     const ClientHelloParser::ClientHello& hello);
115
116 #ifdef OPENSSL_NPN_NEGOTIATED
117 template int SSLWrap<TLSCallbacks>::AdvertiseNextProtoCallback(
118     SSL* s,
119     const unsigned char** data,
120     unsigned int* len,
121     void* arg);
122 template int SSLWrap<TLSCallbacks>::SelectNextProtoCallback(
123     SSL* s,
124     unsigned char** out,
125     unsigned char* outlen,
126     const unsigned char* in,
127     unsigned int inlen,
128     void* arg);
129 #endif
130 template int SSLWrap<TLSCallbacks>::TLSExtStatusCallback(SSL* s, void* arg);
131
132
133 static void crypto_threadid_cb(CRYPTO_THREADID* tid) {
134   static_assert(sizeof(uv_thread_t) <= sizeof(void*),  // NOLINT(runtime/sizeof)
135                 "uv_thread_t does not fit in a pointer");
136   CRYPTO_THREADID_set_pointer(tid, reinterpret_cast<void*>(uv_thread_self()));
137 }
138
139
140 static void crypto_lock_init(void) {
141   int i, n;
142
143   n = CRYPTO_num_locks();
144   locks = new uv_rwlock_t[n];
145
146   for (i = 0; i < n; i++)
147     if (uv_rwlock_init(locks + i))
148       abort();
149 }
150
151
152 static void crypto_lock_cb(int mode, int n, const char* file, int line) {
153   CHECK((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
154   CHECK((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));
155
156   if (mode & CRYPTO_LOCK) {
157     if (mode & CRYPTO_READ)
158       uv_rwlock_rdlock(locks + n);
159     else
160       uv_rwlock_wrlock(locks + n);
161   } else {
162     if (mode & CRYPTO_READ)
163       uv_rwlock_rdunlock(locks + n);
164     else
165       uv_rwlock_wrunlock(locks + n);
166   }
167 }
168
169
170 static int CryptoPemCallback(char *buf, int size, int rwflag, void *u) {
171   if (u) {
172     size_t buflen = static_cast<size_t>(size);
173     size_t len = strlen(static_cast<const char*>(u));
174     len = len > buflen ? buflen : len;
175     memcpy(buf, u, len);
176     return len;
177   }
178
179   return 0;
180 }
181
182
183 void ThrowCryptoError(Environment* env,
184                       unsigned long err,
185                       const char* default_message = nullptr) {
186   HandleScope scope(env->isolate());
187   if (err != 0 || default_message == nullptr) {
188     char errmsg[128] = { 0 };
189     ERR_error_string_n(err, errmsg, sizeof(errmsg));
190     env->ThrowError(errmsg);
191   } else {
192     env->ThrowError(default_message);
193   }
194 }
195
196
197 // Ensure that OpenSSL has enough entropy (at least 256 bits) for its PRNG.
198 // The entropy pool starts out empty and needs to fill up before the PRNG
199 // can be used securely.  Once the pool is filled, it never dries up again;
200 // its contents is stirred and reused when necessary.
201 //
202 // OpenSSL normally fills the pool automatically but not when someone starts
203 // generating random numbers before the pool is full: in that case OpenSSL
204 // keeps lowering the entropy estimate to thwart attackers trying to guess
205 // the initial state of the PRNG.
206 //
207 // When that happens, we will have to wait until enough entropy is available.
208 // That should normally never take longer than a few milliseconds.
209 //
210 // OpenSSL draws from /dev/random and /dev/urandom.  While /dev/random may
211 // block pending "true" randomness, /dev/urandom is a CSPRNG that doesn't
212 // block under normal circumstances.
213 //
214 // The only time when /dev/urandom may conceivably block is right after boot,
215 // when the whole system is still low on entropy.  That's not something we can
216 // do anything about.
217 inline void CheckEntropy() {
218   for (;;) {
219     int status = RAND_status();
220     CHECK_GE(status, 0);  // Cannot fail.
221     if (status != 0)
222       break;
223
224     // Give up, RAND_poll() not supported.
225     if (RAND_poll() == 0)
226       break;
227   }
228 }
229
230
231 bool EntropySource(unsigned char* buffer, size_t length) {
232   // Ensure that OpenSSL's PRNG is properly seeded.
233   CheckEntropy();
234   // RAND_bytes() can return 0 to indicate that the entropy data is not truly
235   // random. That's okay, it's still better than V8's stock source of entropy,
236   // which is /dev/urandom on UNIX platforms and the current time on Windows.
237   return RAND_bytes(buffer, length) != -1;
238 }
239
240
241 void SecureContext::Initialize(Environment* env, Handle<Object> target) {
242   Local<FunctionTemplate> t = env->NewFunctionTemplate(SecureContext::New);
243   t->InstanceTemplate()->SetInternalFieldCount(1);
244   t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));
245
246   env->SetProtoMethod(t, "init", SecureContext::Init);
247   env->SetProtoMethod(t, "setKey", SecureContext::SetKey);
248   env->SetProtoMethod(t, "setCert", SecureContext::SetCert);
249   env->SetProtoMethod(t, "addCACert", SecureContext::AddCACert);
250   env->SetProtoMethod(t, "addCRL", SecureContext::AddCRL);
251   env->SetProtoMethod(t, "addRootCerts", SecureContext::AddRootCerts);
252   env->SetProtoMethod(t, "setCiphers", SecureContext::SetCiphers);
253   env->SetProtoMethod(t, "setECDHCurve", SecureContext::SetECDHCurve);
254   env->SetProtoMethod(t, "setDHParam", SecureContext::SetDHParam);
255   env->SetProtoMethod(t, "setOptions", SecureContext::SetOptions);
256   env->SetProtoMethod(t, "setSessionIdContext",
257                       SecureContext::SetSessionIdContext);
258   env->SetProtoMethod(t, "setSessionTimeout",
259                       SecureContext::SetSessionTimeout);
260   env->SetProtoMethod(t, "close", SecureContext::Close);
261   env->SetProtoMethod(t, "loadPKCS12", SecureContext::LoadPKCS12);
262   env->SetProtoMethod(t, "getTicketKeys", SecureContext::GetTicketKeys);
263   env->SetProtoMethod(t, "setTicketKeys", SecureContext::SetTicketKeys);
264   env->SetProtoMethod(t, "getCertificate", SecureContext::GetCertificate<true>);
265   env->SetProtoMethod(t, "getIssuer", SecureContext::GetCertificate<false>);
266
267   NODE_SET_EXTERNAL(
268       t->PrototypeTemplate(),
269       "_external",
270       CtxGetter);
271
272   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"),
273               t->GetFunction());
274   env->set_secure_context_constructor_template(t);
275 }
276
277
278 void SecureContext::New(const FunctionCallbackInfo<Value>& args) {
279   Environment* env = Environment::GetCurrent(args);
280   new SecureContext(env, args.This());
281 }
282
283
284 void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
285   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
286   Environment* env = sc->env();
287
288   OPENSSL_CONST SSL_METHOD *method = SSLv23_method();
289
290   if (args.Length() == 1 && args[0]->IsString()) {
291     const node::Utf8Value sslmethod(env->isolate(), args[0]);
292
293     // Note that SSLv2 and SSLv3 are disallowed but SSLv2_method and friends
294     // are still accepted.  They are OpenSSL's way of saying that all known
295     // protocols are supported unless explicitly disabled (which we do below
296     // for SSLv2 and SSLv3.)
297     if (strcmp(*sslmethod, "SSLv2_method") == 0) {
298       return env->ThrowError("SSLv2 methods disabled");
299     } else if (strcmp(*sslmethod, "SSLv2_server_method") == 0) {
300       return env->ThrowError("SSLv2 methods disabled");
301     } else if (strcmp(*sslmethod, "SSLv2_client_method") == 0) {
302       return env->ThrowError("SSLv2 methods disabled");
303     } else if (strcmp(*sslmethod, "SSLv3_method") == 0) {
304       return env->ThrowError("SSLv3 methods disabled");
305     } else if (strcmp(*sslmethod, "SSLv3_server_method") == 0) {
306       return env->ThrowError("SSLv3 methods disabled");
307     } else if (strcmp(*sslmethod, "SSLv3_client_method") == 0) {
308       return env->ThrowError("SSLv3 methods disabled");
309     } else if (strcmp(*sslmethod, "SSLv23_method") == 0) {
310       method = SSLv23_method();
311     } else if (strcmp(*sslmethod, "SSLv23_server_method") == 0) {
312       method = SSLv23_server_method();
313     } else if (strcmp(*sslmethod, "SSLv23_client_method") == 0) {
314       method = SSLv23_client_method();
315     } else if (strcmp(*sslmethod, "TLSv1_method") == 0) {
316       method = TLSv1_method();
317     } else if (strcmp(*sslmethod, "TLSv1_server_method") == 0) {
318       method = TLSv1_server_method();
319     } else if (strcmp(*sslmethod, "TLSv1_client_method") == 0) {
320       method = TLSv1_client_method();
321     } else if (strcmp(*sslmethod, "TLSv1_1_method") == 0) {
322       method = TLSv1_1_method();
323     } else if (strcmp(*sslmethod, "TLSv1_1_server_method") == 0) {
324       method = TLSv1_1_server_method();
325     } else if (strcmp(*sslmethod, "TLSv1_1_client_method") == 0) {
326       method = TLSv1_1_client_method();
327     } else if (strcmp(*sslmethod, "TLSv1_2_method") == 0) {
328       method = TLSv1_2_method();
329     } else if (strcmp(*sslmethod, "TLSv1_2_server_method") == 0) {
330       method = TLSv1_2_server_method();
331     } else if (strcmp(*sslmethod, "TLSv1_2_client_method") == 0) {
332       method = TLSv1_2_client_method();
333     } else {
334       return env->ThrowError("Unknown method");
335     }
336   }
337
338   sc->ctx_ = SSL_CTX_new(method);
339
340   // Disable SSLv2 in the case when method == SSLv23_method() and the
341   // cipher list contains SSLv2 ciphers (not the default, should be rare.)
342   // The bundled OpenSSL doesn't have SSLv2 support but the system OpenSSL may.
343   // SSLv3 is disabled because it's susceptible to downgrade attacks (POODLE.)
344   SSL_CTX_set_options(sc->ctx_, SSL_OP_NO_SSLv2);
345   SSL_CTX_set_options(sc->ctx_, SSL_OP_NO_SSLv3);
346
347   // SSL session cache configuration
348   SSL_CTX_set_session_cache_mode(sc->ctx_,
349                                  SSL_SESS_CACHE_SERVER |
350                                  SSL_SESS_CACHE_NO_INTERNAL |
351                                  SSL_SESS_CACHE_NO_AUTO_CLEAR);
352   SSL_CTX_sess_set_get_cb(sc->ctx_, SSLWrap<Connection>::GetSessionCallback);
353   SSL_CTX_sess_set_new_cb(sc->ctx_, SSLWrap<Connection>::NewSessionCallback);
354
355   sc->ca_store_ = nullptr;
356 }
357
358
359 // Takes a string or buffer and loads it into a BIO.
360 // Caller responsible for BIO_free_all-ing the returned object.
361 static BIO* LoadBIO(Environment* env, Handle<Value> v) {
362   BIO* bio = NodeBIO::New();
363   if (!bio)
364     return nullptr;
365
366   HandleScope scope(env->isolate());
367
368   int r = -1;
369
370   if (v->IsString()) {
371     const node::Utf8Value s(env->isolate(), v);
372     r = BIO_write(bio, *s, s.length());
373   } else if (Buffer::HasInstance(v)) {
374     char* buffer_data = Buffer::Data(v);
375     size_t buffer_length = Buffer::Length(v);
376     r = BIO_write(bio, buffer_data, buffer_length);
377   }
378
379   if (r <= 0) {
380     BIO_free_all(bio);
381     return nullptr;
382   }
383
384   return bio;
385 }
386
387
388 // Takes a string or buffer and loads it into an X509
389 // Caller responsible for X509_free-ing the returned object.
390 static X509* LoadX509(Environment* env, Handle<Value> v) {
391   HandleScope scope(env->isolate());
392
393   BIO *bio = LoadBIO(env, v);
394   if (!bio)
395     return nullptr;
396
397   X509 * x509 = PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr);
398   if (!x509) {
399     BIO_free_all(bio);
400     return nullptr;
401   }
402
403   BIO_free_all(bio);
404   return x509;
405 }
406
407
408 void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
409   Environment* env = Environment::GetCurrent(args);
410
411   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
412
413   unsigned int len = args.Length();
414   if (len != 1 && len != 2) {
415     return env->ThrowTypeError("Bad parameter");
416   }
417   if (len == 2 && !args[1]->IsString()) {
418     return env->ThrowTypeError("Bad parameter");
419   }
420
421   BIO *bio = LoadBIO(env, args[0]);
422   if (!bio)
423     return;
424
425   node::Utf8Value passphrase(env->isolate(), args[1]);
426
427   EVP_PKEY* key = PEM_read_bio_PrivateKey(bio,
428                                           nullptr,
429                                           CryptoPemCallback,
430                                           len == 1 ? nullptr : *passphrase);
431
432   if (!key) {
433     BIO_free_all(bio);
434     unsigned long err = ERR_get_error();
435     if (!err) {
436       return env->ThrowError("PEM_read_bio_PrivateKey");
437     }
438     return ThrowCryptoError(env, err);
439   }
440
441   int rv = SSL_CTX_use_PrivateKey(sc->ctx_, key);
442   EVP_PKEY_free(key);
443   BIO_free_all(bio);
444
445   if (!rv) {
446     unsigned long err = ERR_get_error();
447     if (!err)
448       return env->ThrowError("SSL_CTX_use_PrivateKey");
449     return ThrowCryptoError(env, err);
450   }
451 }
452
453
454 int SSL_CTX_get_issuer(SSL_CTX* ctx, X509* cert, X509** issuer) {
455   int ret;
456
457   X509_STORE* store = SSL_CTX_get_cert_store(ctx);
458   X509_STORE_CTX store_ctx;
459
460   ret = X509_STORE_CTX_init(&store_ctx, store, nullptr, nullptr);
461   if (!ret)
462     goto end;
463
464   ret = X509_STORE_CTX_get1_issuer(issuer, &store_ctx, cert);
465   X509_STORE_CTX_cleanup(&store_ctx);
466
467  end:
468   return ret;
469 }
470
471
472 // Read a file that contains our certificate in "PEM" format,
473 // possibly followed by a sequence of CA certificates that should be
474 // sent to the peer in the Certificate message.
475 //
476 // Taken from OpenSSL - editted for style.
477 int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
478                                   BIO* in,
479                                   X509** cert,
480                                   X509** issuer) {
481   int ret = 0;
482   X509* x = nullptr;
483
484   x = PEM_read_bio_X509_AUX(in, nullptr, CryptoPemCallback, nullptr);
485
486   if (x == nullptr) {
487     SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
488     goto end;
489   }
490
491   ret = SSL_CTX_use_certificate(ctx, x);
492
493   if (ret) {
494     // If we could set up our certificate, now proceed to
495     // the CA certificates.
496     X509 *ca;
497     int r;
498     unsigned long err;
499
500     if (ctx->extra_certs != nullptr) {
501       sk_X509_pop_free(ctx->extra_certs, X509_free);
502       ctx->extra_certs = nullptr;
503     }
504
505     while ((ca = PEM_read_bio_X509(in, nullptr, CryptoPemCallback, nullptr))) {
506       r = SSL_CTX_add_extra_chain_cert(ctx, ca);
507
508       if (!r) {
509         X509_free(ca);
510         ret = 0;
511         goto end;
512       }
513       // Note that we must not free r if it was successfully
514       // added to the chain (while we must free the main
515       // certificate, since its reference count is increased
516       // by SSL_CTX_use_certificate).
517
518       // Find issuer
519       if (*issuer != nullptr || X509_check_issued(ca, x) != X509_V_OK)
520         continue;
521       *issuer = ca;
522     }
523
524     // When the while loop ends, it's usually just EOF.
525     err = ERR_peek_last_error();
526     if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
527         ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
528       ERR_clear_error();
529     } else  {
530       // some real error
531       ret = 0;
532     }
533   }
534
535   // Try getting issuer from a cert store
536   if (ret) {
537     if (*issuer == nullptr) {
538       ret = SSL_CTX_get_issuer(ctx, x, issuer);
539       ret = ret < 0 ? 0 : 1;
540       // NOTE: get_cert_store doesn't increment reference count,
541       // no need to free `store`
542     } else {
543       // Increment issuer reference count
544       CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
545     }
546   }
547
548  end:
549   if (x != nullptr)
550     *cert = x;
551   return ret;
552 }
553
554
555 void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
556   Environment* env = Environment::GetCurrent(args);
557
558   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
559
560   if (args.Length() != 1) {
561     return env->ThrowTypeError("Bad parameter");
562   }
563
564   BIO* bio = LoadBIO(env, args[0]);
565   if (!bio)
566     return;
567
568   int rv = SSL_CTX_use_certificate_chain(sc->ctx_,
569                                          bio,
570                                          &sc->cert_,
571                                          &sc->issuer_);
572
573   BIO_free_all(bio);
574
575   if (!rv) {
576     unsigned long err = ERR_get_error();
577     if (!err) {
578       return env->ThrowError("SSL_CTX_use_certificate_chain");
579     }
580     return ThrowCryptoError(env, err);
581   }
582 }
583
584
585 void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
586   bool newCAStore = false;
587   Environment* env = Environment::GetCurrent(args);
588
589   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
590
591   if (args.Length() != 1) {
592     return env->ThrowTypeError("Bad parameter");
593   }
594
595   if (!sc->ca_store_) {
596     sc->ca_store_ = X509_STORE_new();
597     newCAStore = true;
598   }
599
600   X509* x509 = LoadX509(env, args[0]);
601   if (!x509)
602     return;
603
604   X509_STORE_add_cert(sc->ca_store_, x509);
605   SSL_CTX_add_client_CA(sc->ctx_, x509);
606
607   X509_free(x509);
608
609   if (newCAStore) {
610     SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
611   }
612 }
613
614
615 void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
616   Environment* env = Environment::GetCurrent(args);
617
618   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
619
620   if (args.Length() != 1) {
621     return env->ThrowTypeError("Bad parameter");
622   }
623
624   ClearErrorOnReturn clear_error_on_return;
625   (void) &clear_error_on_return;  // Silence compiler warning.
626
627   BIO *bio = LoadBIO(env, args[0]);
628   if (!bio)
629     return;
630
631   X509_CRL *x509 =
632       PEM_read_bio_X509_CRL(bio, nullptr, CryptoPemCallback, nullptr);
633
634   if (x509 == nullptr) {
635     BIO_free_all(bio);
636     return;
637   }
638
639   X509_STORE_add_crl(sc->ca_store_, x509);
640   X509_STORE_set_flags(sc->ca_store_, X509_V_FLAG_CRL_CHECK |
641                                       X509_V_FLAG_CRL_CHECK_ALL);
642   BIO_free_all(bio);
643   X509_CRL_free(x509);
644 }
645
646
647
648 void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
649   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
650
651   CHECK_EQ(sc->ca_store_, nullptr);
652
653   if (!root_cert_store) {
654     root_cert_store = X509_STORE_new();
655
656     for (size_t i = 0; i < ARRAY_SIZE(root_certs); i++) {
657       BIO* bp = NodeBIO::New();
658
659       if (!BIO_write(bp, root_certs[i], strlen(root_certs[i]))) {
660         BIO_free_all(bp);
661         return;
662       }
663
664       X509 *x509 = PEM_read_bio_X509(bp, nullptr, CryptoPemCallback, nullptr);
665
666       if (x509 == nullptr) {
667         BIO_free_all(bp);
668         return;
669       }
670
671       X509_STORE_add_cert(root_cert_store, x509);
672
673       BIO_free_all(bp);
674       X509_free(x509);
675     }
676   }
677
678   sc->ca_store_ = root_cert_store;
679   SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
680 }
681
682
683 void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
684   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
685
686   if (args.Length() != 1 || !args[0]->IsString()) {
687     return sc->env()->ThrowTypeError("Bad parameter");
688   }
689
690   const node::Utf8Value ciphers(args.GetIsolate(), args[0]);
691   SSL_CTX_set_cipher_list(sc->ctx_, *ciphers);
692 }
693
694
695 void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {
696   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
697   Environment* env = sc->env();
698
699   if (args.Length() != 1 || !args[0]->IsString())
700     return env->ThrowTypeError("First argument should be a string");
701
702   node::Utf8Value curve(env->isolate(), args[0]);
703
704   int nid = OBJ_sn2nid(*curve);
705
706   if (nid == NID_undef)
707     return env->ThrowTypeError("First argument should be a valid curve name");
708
709   EC_KEY* ecdh = EC_KEY_new_by_curve_name(nid);
710
711   if (ecdh == nullptr)
712     return env->ThrowTypeError("First argument should be a valid curve name");
713
714   SSL_CTX_set_options(sc->ctx_, SSL_OP_SINGLE_ECDH_USE);
715   SSL_CTX_set_tmp_ecdh(sc->ctx_, ecdh);
716
717   EC_KEY_free(ecdh);
718 }
719
720
721 void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
722   SecureContext* sc = Unwrap<SecureContext>(args.This());
723   Environment* env = sc->env();
724
725   // Auto DH is not supported in openssl 1.0.1, so dhparam needs
726   // to be specifed explicitly
727   if (args.Length() != 1)
728     return env->ThrowTypeError("Bad parameter");
729
730   // Invalid dhparam is silently discarded and DHE is no longer used.
731   BIO* bio = LoadBIO(env, args[0]);
732   if (!bio)
733     return;
734
735   DH* dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr);
736   BIO_free_all(bio);
737
738   if (dh == nullptr)
739     return;
740
741   SSL_CTX_set_options(sc->ctx_, SSL_OP_SINGLE_DH_USE);
742   int r = SSL_CTX_set_tmp_dh(sc->ctx_, dh);
743   DH_free(dh);
744
745   if (!r)
746     return env->ThrowTypeError("Error setting temp DH parameter");
747 }
748
749
750 void SecureContext::SetOptions(const FunctionCallbackInfo<Value>& args) {
751   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
752
753   if (args.Length() != 1 || !args[0]->IntegerValue()) {
754     return sc->env()->ThrowTypeError("Bad parameter");
755   }
756
757   SSL_CTX_set_options(sc->ctx_, static_cast<long>(args[0]->IntegerValue()));
758 }
759
760
761 void SecureContext::SetSessionIdContext(
762     const FunctionCallbackInfo<Value>& args) {
763   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
764
765   if (args.Length() != 1 || !args[0]->IsString()) {
766     return sc->env()->ThrowTypeError("Bad parameter");
767   }
768
769   const node::Utf8Value sessionIdContext(args.GetIsolate(), args[0]);
770   const unsigned char* sid_ctx =
771       reinterpret_cast<const unsigned char*>(*sessionIdContext);
772   unsigned int sid_ctx_len = sessionIdContext.length();
773
774   int r = SSL_CTX_set_session_id_context(sc->ctx_, sid_ctx, sid_ctx_len);
775   if (r == 1)
776     return;
777
778   BIO* bio;
779   BUF_MEM* mem;
780   Local<String> message;
781
782   bio = BIO_new(BIO_s_mem());
783   if (bio == nullptr) {
784     message = FIXED_ONE_BYTE_STRING(args.GetIsolate(),
785                                     "SSL_CTX_set_session_id_context error");
786   } else {
787     ERR_print_errors(bio);
788     BIO_get_mem_ptr(bio, &mem);
789     message = OneByteString(args.GetIsolate(), mem->data, mem->length);
790     BIO_free_all(bio);
791   }
792
793   args.GetIsolate()->ThrowException(Exception::TypeError(message));
794 }
795
796
797 void SecureContext::SetSessionTimeout(const FunctionCallbackInfo<Value>& args) {
798   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
799
800   if (args.Length() != 1 || !args[0]->IsInt32()) {
801     return sc->env()->ThrowTypeError("Bad parameter");
802   }
803
804   int32_t sessionTimeout = args[0]->Int32Value();
805   SSL_CTX_set_timeout(sc->ctx_, sessionTimeout);
806 }
807
808
809 void SecureContext::Close(const FunctionCallbackInfo<Value>& args) {
810   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
811   sc->FreeCTXMem();
812 }
813
814
815 // Takes .pfx or .p12 and password in string or buffer format
816 void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
817   Environment* env = Environment::GetCurrent(args);
818
819   BIO* in = nullptr;
820   PKCS12* p12 = nullptr;
821   EVP_PKEY* pkey = nullptr;
822   X509* cert = nullptr;
823   STACK_OF(X509)* extraCerts = nullptr;
824   char* pass = nullptr;
825   bool ret = false;
826
827   SecureContext* sc = Unwrap<SecureContext>(args.Holder());
828
829   if (args.Length() < 1) {
830     return env->ThrowTypeError("Bad parameter");
831   }
832
833   in = LoadBIO(env, args[0]);
834   if (in == nullptr) {
835     return env->ThrowError("Unable to load BIO");
836   }
837
838   if (args.Length() >= 2) {
839     THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
840     size_t passlen = Buffer::Length(args[1]);
841     pass = new char[passlen + 1];
842     memcpy(pass, Buffer::Data(args[1]), passlen);
843     pass[passlen] = '\0';
844   }
845
846   if (d2i_PKCS12_bio(in, &p12) &&
847       PKCS12_parse(p12, pass, &pkey, &cert, &extraCerts) &&
848       SSL_CTX_use_certificate(sc->ctx_, cert) &&
849       SSL_CTX_use_PrivateKey(sc->ctx_, pkey)) {
850     // set extra certs
851     while (X509* x509 = sk_X509_pop(extraCerts)) {
852       if (!sc->ca_store_) {
853         sc->ca_store_ = X509_STORE_new();
854         SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
855       }
856
857       X509_STORE_add_cert(sc->ca_store_, x509);
858       SSL_CTX_add_client_CA(sc->ctx_, x509);
859       X509_free(x509);
860     }
861
862     EVP_PKEY_free(pkey);
863     X509_free(cert);
864     sk_X509_free(extraCerts);
865
866     ret = true;
867   }
868
869   PKCS12_free(p12);
870   BIO_free_all(in);
871   delete[] pass;
872
873   if (!ret) {
874     unsigned long err = ERR_get_error();
875     const char* str = ERR_reason_error_string(err);
876     return env->ThrowError(str);
877   }
878 }
879
880
881 void SecureContext::GetTicketKeys(const FunctionCallbackInfo<Value>& args) {
882 #if !defined(OPENSSL_NO_TLSEXT) && defined(SSL_CTX_get_tlsext_ticket_keys)
883
884   SecureContext* wrap = Unwrap<SecureContext>(args.Holder());
885
886   Local<Object> buff = Buffer::New(wrap->env(), 48);
887   if (SSL_CTX_get_tlsext_ticket_keys(wrap->ctx_,
888                                      Buffer::Data(buff),
889                                      Buffer::Length(buff)) != 1) {
890     return wrap->env()->ThrowError("Failed to fetch tls ticket keys");
891   }
892
893   args.GetReturnValue().Set(buff);
894 #endif  // !def(OPENSSL_NO_TLSEXT) && def(SSL_CTX_get_tlsext_ticket_keys)
895 }
896
897
898 void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) {
899 #if !defined(OPENSSL_NO_TLSEXT) && defined(SSL_CTX_get_tlsext_ticket_keys)
900   SecureContext* wrap = Unwrap<SecureContext>(args.Holder());
901
902   if (args.Length() < 1 ||
903       !Buffer::HasInstance(args[0]) ||
904       Buffer::Length(args[0]) != 48) {
905     return wrap->env()->ThrowTypeError("Bad argument");
906   }
907
908   if (SSL_CTX_set_tlsext_ticket_keys(wrap->ctx_,
909                                      Buffer::Data(args[0]),
910                                      Buffer::Length(args[0])) != 1) {
911     return wrap->env()->ThrowError("Failed to fetch tls ticket keys");
912   }
913
914   args.GetReturnValue().Set(true);
915 #endif  // !def(OPENSSL_NO_TLSEXT) && def(SSL_CTX_get_tlsext_ticket_keys)
916 }
917
918
919 void SecureContext::CtxGetter(Local<String> property,
920                               const PropertyCallbackInfo<Value>& info) {
921   HandleScope scope(info.GetIsolate());
922
923   SSL_CTX* ctx = Unwrap<SecureContext>(info.Holder())->ctx_;
924   Local<External> ext = External::New(info.GetIsolate(), ctx);
925   info.GetReturnValue().Set(ext);
926 }
927
928
929 template <bool primary>
930 void SecureContext::GetCertificate(const FunctionCallbackInfo<Value>& args) {
931   SecureContext* wrap = Unwrap<SecureContext>(args.Holder());
932   Environment* env = wrap->env();
933   X509* cert;
934
935   if (primary)
936     cert = wrap->cert_;
937   else
938     cert = wrap->issuer_;
939   if (cert == nullptr)
940     return args.GetReturnValue().Set(Null(env->isolate()));
941
942   int size = i2d_X509(cert, nullptr);
943   Local<Object> buff = Buffer::New(env, size);
944   unsigned char* serialized = reinterpret_cast<unsigned char*>(
945       Buffer::Data(buff));
946   i2d_X509(cert, &serialized);
947
948   args.GetReturnValue().Set(buff);
949 }
950
951
952 template <class Base>
953 void SSLWrap<Base>::AddMethods(Environment* env, Handle<FunctionTemplate> t) {
954   HandleScope scope(env->isolate());
955
956   env->SetProtoMethod(t, "getPeerCertificate", GetPeerCertificate);
957   env->SetProtoMethod(t, "getSession", GetSession);
958   env->SetProtoMethod(t, "setSession", SetSession);
959   env->SetProtoMethod(t, "loadSession", LoadSession);
960   env->SetProtoMethod(t, "isSessionReused", IsSessionReused);
961   env->SetProtoMethod(t, "isInitFinished", IsInitFinished);
962   env->SetProtoMethod(t, "verifyError", VerifyError);
963   env->SetProtoMethod(t, "getCurrentCipher", GetCurrentCipher);
964   env->SetProtoMethod(t, "endParser", EndParser);
965   env->SetProtoMethod(t, "renegotiate", Renegotiate);
966   env->SetProtoMethod(t, "shutdown", Shutdown);
967   env->SetProtoMethod(t, "getTLSTicket", GetTLSTicket);
968   env->SetProtoMethod(t, "newSessionDone", NewSessionDone);
969   env->SetProtoMethod(t, "setOCSPResponse", SetOCSPResponse);
970   env->SetProtoMethod(t, "requestOCSP", RequestOCSP);
971
972 #ifdef SSL_set_max_send_fragment
973   env->SetProtoMethod(t, "setMaxSendFragment", SetMaxSendFragment);
974 #endif  // SSL_set_max_send_fragment
975
976 #ifdef OPENSSL_NPN_NEGOTIATED
977   env->SetProtoMethod(t, "getNegotiatedProtocol", GetNegotiatedProto);
978 #endif  // OPENSSL_NPN_NEGOTIATED
979
980 #ifdef OPENSSL_NPN_NEGOTIATED
981   env->SetProtoMethod(t, "setNPNProtocols", SetNPNProtocols);
982 #endif
983
984   NODE_SET_EXTERNAL(
985       t->PrototypeTemplate(),
986       "_external",
987       SSLGetter);
988 }
989
990
991 template <class Base>
992 void SSLWrap<Base>::InitNPN(SecureContext* sc) {
993 #ifdef OPENSSL_NPN_NEGOTIATED
994   // Server should advertise NPN protocols
995   SSL_CTX_set_next_protos_advertised_cb(sc->ctx_,
996                                         AdvertiseNextProtoCallback,
997                                         nullptr);
998   // Client should select protocol from list of advertised
999   // If server supports NPN
1000   SSL_CTX_set_next_proto_select_cb(sc->ctx_, SelectNextProtoCallback, nullptr);
1001 #endif  // OPENSSL_NPN_NEGOTIATED
1002
1003 #ifdef NODE__HAVE_TLSEXT_STATUS_CB
1004   // OCSP stapling
1005   SSL_CTX_set_tlsext_status_cb(sc->ctx_, TLSExtStatusCallback);
1006   SSL_CTX_set_tlsext_status_arg(sc->ctx_, nullptr);
1007 #endif  // NODE__HAVE_TLSEXT_STATUS_CB
1008 }
1009
1010
1011 template <class Base>
1012 SSL_SESSION* SSLWrap<Base>::GetSessionCallback(SSL* s,
1013                                                unsigned char* key,
1014                                                int len,
1015                                                int* copy) {
1016   Base* w = static_cast<Base*>(SSL_get_app_data(s));
1017
1018   *copy = 0;
1019   SSL_SESSION* sess = w->next_sess_;
1020   w->next_sess_ = nullptr;
1021
1022   return sess;
1023 }
1024
1025
1026 template <class Base>
1027 int SSLWrap<Base>::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
1028   Base* w = static_cast<Base*>(SSL_get_app_data(s));
1029   Environment* env = w->ssl_env();
1030   HandleScope handle_scope(env->isolate());
1031   Context::Scope context_scope(env->context());
1032
1033   if (!w->session_callbacks_)
1034     return 0;
1035
1036   // Check if session is small enough to be stored
1037   int size = i2d_SSL_SESSION(sess, nullptr);
1038   if (size > SecureContext::kMaxSessionSize)
1039     return 0;
1040
1041   // Serialize session
1042   Local<Object> buff = Buffer::New(env, size);
1043   unsigned char* serialized = reinterpret_cast<unsigned char*>(
1044       Buffer::Data(buff));
1045   memset(serialized, 0, size);
1046   i2d_SSL_SESSION(sess, &serialized);
1047
1048   Local<Object> session = Buffer::New(env,
1049                                       reinterpret_cast<char*>(sess->session_id),
1050                                       sess->session_id_length);
1051   Local<Value> argv[] = { session, buff };
1052   w->new_session_wait_ = true;
1053   w->MakeCallback(env->onnewsession_string(), ARRAY_SIZE(argv), argv);
1054
1055   return 0;
1056 }
1057
1058
1059 template <class Base>
1060 void SSLWrap<Base>::OnClientHello(void* arg,
1061                                   const ClientHelloParser::ClientHello& hello) {
1062   Base* w = static_cast<Base*>(arg);
1063   Environment* env = w->ssl_env();
1064   HandleScope handle_scope(env->isolate());
1065   Context::Scope context_scope(env->context());
1066
1067   Local<Object> hello_obj = Object::New(env->isolate());
1068   Local<Object> buff = Buffer::New(
1069       env,
1070       reinterpret_cast<const char*>(hello.session_id()),
1071       hello.session_size());
1072   hello_obj->Set(env->session_id_string(), buff);
1073   if (hello.servername() == nullptr) {
1074     hello_obj->Set(env->servername_string(), String::Empty(env->isolate()));
1075   } else {
1076     Local<String> servername = OneByteString(env->isolate(),
1077                                              hello.servername(),
1078                                              hello.servername_size());
1079     hello_obj->Set(env->servername_string(), servername);
1080   }
1081   hello_obj->Set(env->tls_ticket_string(),
1082                  Boolean::New(env->isolate(), hello.has_ticket()));
1083   hello_obj->Set(env->ocsp_request_string(),
1084                  Boolean::New(env->isolate(), hello.ocsp_request()));
1085
1086   Local<Value> argv[] = { hello_obj };
1087   w->MakeCallback(env->onclienthello_string(), ARRAY_SIZE(argv), argv);
1088 }
1089
1090
1091 static bool SafeX509ExtPrint(BIO* out, X509_EXTENSION* ext) {
1092   // Only alt_name is escaped at the moment
1093   if (OBJ_obj2nid(ext->object) != NID_subject_alt_name)
1094     return false;
1095
1096   const X509V3_EXT_METHOD* method = X509V3_EXT_get(ext);
1097   if (method == NULL || method->it == NULL)
1098     return false;
1099
1100   if (method->i2v != reinterpret_cast<X509V3_EXT_I2V>(i2v_GENERAL_NAMES))
1101     return false;
1102
1103   const unsigned char* p = ext->value->data;
1104   GENERAL_NAMES* names = reinterpret_cast<GENERAL_NAMES*>(ASN1_item_d2i(
1105       NULL,
1106       &p,
1107       ext->value->length,
1108       ASN1_ITEM_ptr(method->it)));
1109   if (names == NULL)
1110     return false;
1111
1112   for (int i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1113     GENERAL_NAME* gen = sk_GENERAL_NAME_value(names, i);
1114
1115     if (i != 0)
1116       BIO_write(out, ", ", 2);
1117
1118     if (gen->type == GEN_DNS) {
1119       ASN1_IA5STRING* name = gen->d.dNSName;
1120
1121       BIO_write(out, "DNS:", 4);
1122       BIO_write(out, name->data, name->length);
1123     } else {
1124       STACK_OF(CONF_VALUE)* nval = i2v_GENERAL_NAME(
1125           const_cast<X509V3_EXT_METHOD*>(method), gen, NULL);
1126       if (nval == NULL)
1127         return false;
1128       X509V3_EXT_val_prn(out, nval, 0, 0);
1129     }
1130   }
1131
1132   return true;
1133 }
1134
1135
1136 static Local<Object> X509ToObject(Environment* env, X509* cert) {
1137   EscapableHandleScope scope(env->isolate());
1138
1139   Local<Object> info = Object::New(env->isolate());
1140
1141   BIO* bio = BIO_new(BIO_s_mem());
1142   BUF_MEM* mem;
1143   if (X509_NAME_print_ex(bio,
1144                          X509_get_subject_name(cert),
1145                          0,
1146                          X509_NAME_FLAGS) > 0) {
1147     BIO_get_mem_ptr(bio, &mem);
1148     info->Set(env->subject_string(),
1149               String::NewFromUtf8(env->isolate(), mem->data,
1150                                   String::kNormalString, mem->length));
1151   }
1152   (void) BIO_reset(bio);
1153
1154   X509_NAME* issuer_name = X509_get_issuer_name(cert);
1155   if (X509_NAME_print_ex(bio, issuer_name, 0, X509_NAME_FLAGS) > 0) {
1156     BIO_get_mem_ptr(bio, &mem);
1157     info->Set(env->issuer_string(),
1158               String::NewFromUtf8(env->isolate(), mem->data,
1159                                   String::kNormalString, mem->length));
1160   }
1161   (void) BIO_reset(bio);
1162
1163   int nids[] = { NID_subject_alt_name, NID_info_access };
1164   Local<String> keys[] = { env->subjectaltname_string(),
1165                            env->infoaccess_string() };
1166   CHECK_EQ(ARRAY_SIZE(nids), ARRAY_SIZE(keys));
1167   for (unsigned int i = 0; i < ARRAY_SIZE(nids); i++) {
1168     int index = X509_get_ext_by_NID(cert, nids[i], -1);
1169     if (index < 0)
1170       continue;
1171
1172     X509_EXTENSION* ext;
1173     int rv;
1174
1175     ext = X509_get_ext(cert, index);
1176     CHECK_NE(ext, nullptr);
1177
1178     if (!SafeX509ExtPrint(bio, ext)) {
1179       rv = X509V3_EXT_print(bio, ext, 0, 0);
1180       CHECK_EQ(rv, 1);
1181     }
1182
1183     BIO_get_mem_ptr(bio, &mem);
1184     info->Set(keys[i],
1185               String::NewFromUtf8(env->isolate(), mem->data,
1186                                   String::kNormalString, mem->length));
1187
1188     (void) BIO_reset(bio);
1189   }
1190
1191   EVP_PKEY* pkey = X509_get_pubkey(cert);
1192   RSA* rsa = nullptr;
1193   if (pkey != nullptr)
1194     rsa = EVP_PKEY_get1_RSA(pkey);
1195
1196   if (rsa != nullptr) {
1197       BN_print(bio, rsa->n);
1198       BIO_get_mem_ptr(bio, &mem);
1199       info->Set(env->modulus_string(),
1200                 String::NewFromUtf8(env->isolate(), mem->data,
1201                                     String::kNormalString, mem->length));
1202       (void) BIO_reset(bio);
1203
1204       BN_print(bio, rsa->e);
1205       BIO_get_mem_ptr(bio, &mem);
1206       info->Set(env->exponent_string(),
1207                 String::NewFromUtf8(env->isolate(), mem->data,
1208                                     String::kNormalString, mem->length));
1209       (void) BIO_reset(bio);
1210   }
1211
1212   if (pkey != nullptr) {
1213     EVP_PKEY_free(pkey);
1214     pkey = nullptr;
1215   }
1216   if (rsa != nullptr) {
1217     RSA_free(rsa);
1218     rsa = nullptr;
1219   }
1220
1221   ASN1_TIME_print(bio, X509_get_notBefore(cert));
1222   BIO_get_mem_ptr(bio, &mem);
1223   info->Set(env->valid_from_string(),
1224             String::NewFromUtf8(env->isolate(), mem->data,
1225                                 String::kNormalString, mem->length));
1226   (void) BIO_reset(bio);
1227
1228   ASN1_TIME_print(bio, X509_get_notAfter(cert));
1229   BIO_get_mem_ptr(bio, &mem);
1230   info->Set(env->valid_to_string(),
1231             String::NewFromUtf8(env->isolate(), mem->data,
1232                                 String::kNormalString, mem->length));
1233   BIO_free_all(bio);
1234
1235   unsigned int md_size, i;
1236   unsigned char md[EVP_MAX_MD_SIZE];
1237   if (X509_digest(cert, EVP_sha1(), md, &md_size)) {
1238     const char hex[] = "0123456789ABCDEF";
1239     char fingerprint[EVP_MAX_MD_SIZE * 3];
1240
1241     // TODO(indutny): Unify it with buffer's code
1242     for (i = 0; i < md_size; i++) {
1243       fingerprint[3*i] = hex[(md[i] & 0xf0) >> 4];
1244       fingerprint[(3*i)+1] = hex[(md[i] & 0x0f)];
1245       fingerprint[(3*i)+2] = ':';
1246     }
1247
1248     if (md_size > 0) {
1249       fingerprint[(3*(md_size-1))+2] = '\0';
1250     } else {
1251       fingerprint[0] = '\0';
1252     }
1253
1254     info->Set(env->fingerprint_string(),
1255               OneByteString(env->isolate(), fingerprint));
1256   }
1257
1258   STACK_OF(ASN1_OBJECT)* eku = static_cast<STACK_OF(ASN1_OBJECT)*>(
1259       X509_get_ext_d2i(cert, NID_ext_key_usage, nullptr, nullptr));
1260   if (eku != nullptr) {
1261     Local<Array> ext_key_usage = Array::New(env->isolate());
1262     char buf[256];
1263
1264     int j = 0;
1265     for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
1266       if (OBJ_obj2txt(buf, sizeof(buf), sk_ASN1_OBJECT_value(eku, i), 1) >= 0)
1267         ext_key_usage->Set(j++, OneByteString(env->isolate(), buf));
1268     }
1269
1270     sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
1271     info->Set(env->ext_key_usage_string(), ext_key_usage);
1272   }
1273
1274   if (ASN1_INTEGER* serial_number = X509_get_serialNumber(cert)) {
1275     if (BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number, nullptr)) {
1276       if (char* buf = BN_bn2hex(bn)) {
1277         info->Set(env->serial_number_string(),
1278                   OneByteString(env->isolate(), buf));
1279         OPENSSL_free(buf);
1280       }
1281       BN_free(bn);
1282     }
1283   }
1284
1285   // Raw DER certificate
1286   int size = i2d_X509(cert, nullptr);
1287   Local<Object> buff = Buffer::New(env, size);
1288   unsigned char* serialized = reinterpret_cast<unsigned char*>(
1289       Buffer::Data(buff));
1290   i2d_X509(cert, &serialized);
1291   info->Set(env->raw_string(), buff);
1292
1293   return scope.Escape(info);
1294 }
1295
1296
1297 // TODO(indutny): Split it into multiple smaller functions
1298 template <class Base>
1299 void SSLWrap<Base>::GetPeerCertificate(
1300     const FunctionCallbackInfo<Value>& args) {
1301   Base* w = Unwrap<Base>(args.Holder());
1302   Environment* env = w->ssl_env();
1303
1304   ClearErrorOnReturn clear_error_on_return;
1305   (void) &clear_error_on_return;  // Silence unused variable warning.
1306
1307   Local<Object> result;
1308   Local<Object> info;
1309
1310   // NOTE: This is because of the odd OpenSSL behavior. On client `cert_chain`
1311   // contains the `peer_certificate`, but on server it doesn't
1312   X509* cert = w->is_server() ? SSL_get_peer_certificate(w->ssl_) : nullptr;
1313   STACK_OF(X509)* ssl_certs = SSL_get_peer_cert_chain(w->ssl_);
1314   STACK_OF(X509)* peer_certs = nullptr;
1315   if (cert == nullptr && ssl_certs == nullptr)
1316     goto done;
1317
1318   if (cert == nullptr && sk_X509_num(ssl_certs) == 0)
1319     goto done;
1320
1321   // Short result requested
1322   if (args.Length() < 1 || !args[0]->IsTrue()) {
1323     result = X509ToObject(env,
1324                           cert == nullptr ? sk_X509_value(ssl_certs, 0) : cert);
1325     goto done;
1326   }
1327
1328   // Clone `ssl_certs`, because we are going to destruct it
1329   peer_certs = sk_X509_new(nullptr);
1330   if (cert != nullptr)
1331     sk_X509_push(peer_certs, cert);
1332   for (int i = 0; i < sk_X509_num(ssl_certs); i++) {
1333     cert = X509_dup(sk_X509_value(ssl_certs, i));
1334     if (cert == nullptr)
1335       goto done;
1336     if (!sk_X509_push(peer_certs, cert))
1337       goto done;
1338   }
1339
1340   // First and main certificate
1341   cert = sk_X509_value(peer_certs, 0);
1342   result = X509ToObject(env, cert);
1343   info = result;
1344
1345   // Put issuer inside the object
1346   cert = sk_X509_delete(peer_certs, 0);
1347   while (sk_X509_num(peer_certs) > 0) {
1348     int i;
1349     for (i = 0; i < sk_X509_num(peer_certs); i++) {
1350       X509* ca = sk_X509_value(peer_certs, i);
1351       if (X509_check_issued(ca, cert) != X509_V_OK)
1352         continue;
1353
1354       Local<Object> ca_info = X509ToObject(env, ca);
1355       info->Set(env->issuercert_string(), ca_info);
1356       info = ca_info;
1357
1358       // NOTE: Intentionally freeing cert that is not used anymore
1359       X509_free(cert);
1360
1361       // Delete cert and continue aggregating issuers
1362       cert = sk_X509_delete(peer_certs, i);
1363       break;
1364     }
1365
1366     // Issuer not found, break out of the loop
1367     if (i == sk_X509_num(peer_certs))
1368       break;
1369   }
1370
1371   // Last certificate should be self-signed
1372   while (X509_check_issued(cert, cert) != X509_V_OK) {
1373     X509* ca;
1374     if (SSL_CTX_get_issuer(w->ssl_->ctx, cert, &ca) <= 0)
1375       break;
1376
1377     Local<Object> ca_info = X509ToObject(env, ca);
1378     info->Set(env->issuercert_string(), ca_info);
1379     info = ca_info;
1380
1381     // NOTE: Intentionally freeing cert that is not used anymore
1382     X509_free(cert);
1383
1384     // Delete cert and continue aggregating issuers
1385     cert = ca;
1386   }
1387
1388   // Self-issued certificate
1389   if (X509_check_issued(cert, cert) == X509_V_OK)
1390     info->Set(env->issuercert_string(), info);
1391
1392   CHECK_NE(cert, nullptr);
1393
1394  done:
1395   if (cert != nullptr)
1396     X509_free(cert);
1397   if (peer_certs != nullptr)
1398     sk_X509_pop_free(peer_certs, X509_free);
1399   if (result.IsEmpty())
1400     result = Object::New(env->isolate());
1401   args.GetReturnValue().Set(result);
1402 }
1403
1404
1405 template <class Base>
1406 void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
1407   Environment* env = Environment::GetCurrent(args);
1408
1409   Base* w = Unwrap<Base>(args.Holder());
1410
1411   SSL_SESSION* sess = SSL_get_session(w->ssl_);
1412   if (sess == nullptr)
1413     return;
1414
1415   int slen = i2d_SSL_SESSION(sess, nullptr);
1416   CHECK_GT(slen, 0);
1417
1418   char* sbuf = new char[slen];
1419   unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
1420   i2d_SSL_SESSION(sess, &p);
1421   args.GetReturnValue().Set(Encode(env->isolate(), sbuf, slen, BUFFER));
1422   delete[] sbuf;
1423 }
1424
1425
1426 template <class Base>
1427 void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
1428   Environment* env = Environment::GetCurrent(args);
1429
1430   Base* w = Unwrap<Base>(args.Holder());
1431
1432   if (args.Length() < 1 ||
1433       (!args[0]->IsString() && !Buffer::HasInstance(args[0]))) {
1434     return env->ThrowTypeError("Bad argument");
1435   }
1436
1437   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
1438   size_t slen = Buffer::Length(args[0]);
1439   char* sbuf = new char[slen];
1440   memcpy(sbuf, Buffer::Data(args[0]), slen);
1441
1442   const unsigned char* p = reinterpret_cast<const unsigned char*>(sbuf);
1443   SSL_SESSION* sess = d2i_SSL_SESSION(nullptr, &p, slen);
1444
1445   delete[] sbuf;
1446
1447   if (sess == nullptr)
1448     return;
1449
1450   int r = SSL_set_session(w->ssl_, sess);
1451   SSL_SESSION_free(sess);
1452
1453   if (!r)
1454     return env->ThrowError("SSL_set_session error");
1455 }
1456
1457
1458 template <class Base>
1459 void SSLWrap<Base>::LoadSession(const FunctionCallbackInfo<Value>& args) {
1460   Base* w = Unwrap<Base>(args.Holder());
1461   Environment* env = w->ssl_env();
1462
1463   if (args.Length() >= 1 && Buffer::HasInstance(args[0])) {
1464     ssize_t slen = Buffer::Length(args[0]);
1465     char* sbuf = Buffer::Data(args[0]);
1466
1467     const unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
1468     SSL_SESSION* sess = d2i_SSL_SESSION(nullptr, &p, slen);
1469
1470     // Setup next session and move hello to the BIO buffer
1471     if (w->next_sess_ != nullptr)
1472       SSL_SESSION_free(w->next_sess_);
1473     w->next_sess_ = sess;
1474
1475     Local<Object> info = Object::New(env->isolate());
1476 #ifndef OPENSSL_NO_TLSEXT
1477     if (sess->tlsext_hostname == nullptr) {
1478       info->Set(env->servername_string(), False(args.GetIsolate()));
1479     } else {
1480       info->Set(env->servername_string(),
1481                 OneByteString(args.GetIsolate(), sess->tlsext_hostname));
1482     }
1483 #endif
1484     args.GetReturnValue().Set(info);
1485   }
1486 }
1487
1488
1489 template <class Base>
1490 void SSLWrap<Base>::IsSessionReused(const FunctionCallbackInfo<Value>& args) {
1491   Base* w = Unwrap<Base>(args.Holder());
1492   bool yes = SSL_session_reused(w->ssl_);
1493   args.GetReturnValue().Set(yes);
1494 }
1495
1496
1497 template <class Base>
1498 void SSLWrap<Base>::EndParser(const FunctionCallbackInfo<Value>& args) {
1499   Base* w = Unwrap<Base>(args.Holder());
1500   w->hello_parser_.End();
1501 }
1502
1503
1504 template <class Base>
1505 void SSLWrap<Base>::Renegotiate(const FunctionCallbackInfo<Value>& args) {
1506   Base* w = Unwrap<Base>(args.Holder());
1507
1508   ClearErrorOnReturn clear_error_on_return;
1509   (void) &clear_error_on_return;  // Silence unused variable warning.
1510
1511   bool yes = SSL_renegotiate(w->ssl_) == 1;
1512   args.GetReturnValue().Set(yes);
1513 }
1514
1515
1516 template <class Base>
1517 void SSLWrap<Base>::Shutdown(const FunctionCallbackInfo<Value>& args) {
1518   Base* w = Unwrap<Base>(args.Holder());
1519
1520   int rv = SSL_shutdown(w->ssl_);
1521   args.GetReturnValue().Set(rv);
1522 }
1523
1524
1525 template <class Base>
1526 void SSLWrap<Base>::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
1527   Base* w = Unwrap<Base>(args.Holder());
1528   Environment* env = w->ssl_env();
1529
1530   SSL_SESSION* sess = SSL_get_session(w->ssl_);
1531   if (sess == nullptr || sess->tlsext_tick == nullptr)
1532     return;
1533
1534   Local<Object> buf = Buffer::New(env,
1535                                   reinterpret_cast<char*>(sess->tlsext_tick),
1536                                   sess->tlsext_ticklen);
1537
1538   args.GetReturnValue().Set(buf);
1539 }
1540
1541
1542 template <class Base>
1543 void SSLWrap<Base>::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
1544   Base* w = Unwrap<Base>(args.Holder());
1545   w->new_session_wait_ = false;
1546   w->NewSessionDoneCb();
1547 }
1548
1549
1550 template <class Base>
1551 void SSLWrap<Base>::SetOCSPResponse(
1552     const v8::FunctionCallbackInfo<v8::Value>& args) {
1553 #ifdef NODE__HAVE_TLSEXT_STATUS_CB
1554   HandleScope scope(args.GetIsolate());
1555
1556   Base* w = Unwrap<Base>(args.Holder());
1557   if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
1558     return w->env()->ThrowTypeError("Must give a Buffer as first argument");
1559
1560   w->ocsp_response_.Reset(args.GetIsolate(), args[0].As<Object>());
1561 #endif  // NODE__HAVE_TLSEXT_STATUS_CB
1562 }
1563
1564
1565 template <class Base>
1566 void SSLWrap<Base>::RequestOCSP(
1567     const v8::FunctionCallbackInfo<v8::Value>& args) {
1568 #ifdef NODE__HAVE_TLSEXT_STATUS_CB
1569   HandleScope scope(args.GetIsolate());
1570
1571   Base* w = Unwrap<Base>(args.Holder());
1572
1573   SSL_set_tlsext_status_type(w->ssl_, TLSEXT_STATUSTYPE_ocsp);
1574 #endif  // NODE__HAVE_TLSEXT_STATUS_CB
1575 }
1576
1577
1578 #ifdef SSL_set_max_send_fragment
1579 template <class Base>
1580 void SSLWrap<Base>::SetMaxSendFragment(
1581     const v8::FunctionCallbackInfo<v8::Value>& args) {
1582   HandleScope scope(args.GetIsolate());
1583   CHECK(args.Length() >= 1 && args[0]->IsNumber());
1584
1585   Base* w = Unwrap<Base>(args.Holder());
1586
1587   int rv = SSL_set_max_send_fragment(w->ssl_, args[0]->Int32Value());
1588   args.GetReturnValue().Set(rv);
1589 }
1590 #endif  // SSL_set_max_send_fragment
1591
1592
1593 template <class Base>
1594 void SSLWrap<Base>::IsInitFinished(const FunctionCallbackInfo<Value>& args) {
1595   Base* w = Unwrap<Base>(args.Holder());
1596   bool yes = SSL_is_init_finished(w->ssl_);
1597   args.GetReturnValue().Set(yes);
1598 }
1599
1600
1601 template <class Base>
1602 void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) {
1603   Base* w = Unwrap<Base>(args.Holder());
1604
1605   // XXX(bnoordhuis) The UNABLE_TO_GET_ISSUER_CERT error when there is no
1606   // peer certificate is questionable but it's compatible with what was
1607   // here before.
1608   long x509_verify_error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
1609   if (X509* peer_cert = SSL_get_peer_certificate(w->ssl_)) {
1610     X509_free(peer_cert);
1611     x509_verify_error = SSL_get_verify_result(w->ssl_);
1612   }
1613
1614   if (x509_verify_error == X509_V_OK)
1615     return args.GetReturnValue().SetNull();
1616
1617   // XXX(bnoordhuis) X509_verify_cert_error_string() is not actually thread-safe
1618   // in the presence of invalid error codes.  Probably academical but something
1619   // to keep in mind if/when node ever grows multi-isolate capabilities.
1620   const char* reason = X509_verify_cert_error_string(x509_verify_error);
1621   const char* code = reason;
1622 #define CASE_X509_ERR(CODE) case X509_V_ERR_##CODE: code = #CODE; break;
1623   switch (x509_verify_error) {
1624     CASE_X509_ERR(UNABLE_TO_GET_ISSUER_CERT)
1625     CASE_X509_ERR(UNABLE_TO_GET_CRL)
1626     CASE_X509_ERR(UNABLE_TO_DECRYPT_CERT_SIGNATURE)
1627     CASE_X509_ERR(UNABLE_TO_DECRYPT_CRL_SIGNATURE)
1628     CASE_X509_ERR(UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY)
1629     CASE_X509_ERR(CERT_SIGNATURE_FAILURE)
1630     CASE_X509_ERR(CRL_SIGNATURE_FAILURE)
1631     CASE_X509_ERR(CERT_NOT_YET_VALID)
1632     CASE_X509_ERR(CERT_HAS_EXPIRED)
1633     CASE_X509_ERR(CRL_NOT_YET_VALID)
1634     CASE_X509_ERR(CRL_HAS_EXPIRED)
1635     CASE_X509_ERR(ERROR_IN_CERT_NOT_BEFORE_FIELD)
1636     CASE_X509_ERR(ERROR_IN_CERT_NOT_AFTER_FIELD)
1637     CASE_X509_ERR(ERROR_IN_CRL_LAST_UPDATE_FIELD)
1638     CASE_X509_ERR(ERROR_IN_CRL_NEXT_UPDATE_FIELD)
1639     CASE_X509_ERR(OUT_OF_MEM)
1640     CASE_X509_ERR(DEPTH_ZERO_SELF_SIGNED_CERT)
1641     CASE_X509_ERR(SELF_SIGNED_CERT_IN_CHAIN)
1642     CASE_X509_ERR(UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
1643     CASE_X509_ERR(UNABLE_TO_VERIFY_LEAF_SIGNATURE)
1644     CASE_X509_ERR(CERT_CHAIN_TOO_LONG)
1645     CASE_X509_ERR(CERT_REVOKED)
1646     CASE_X509_ERR(INVALID_CA)
1647     CASE_X509_ERR(PATH_LENGTH_EXCEEDED)
1648     CASE_X509_ERR(INVALID_PURPOSE)
1649     CASE_X509_ERR(CERT_UNTRUSTED)
1650     CASE_X509_ERR(CERT_REJECTED)
1651   }
1652 #undef CASE_X509_ERR
1653
1654   Isolate* isolate = args.GetIsolate();
1655   Local<String> reason_string = OneByteString(isolate, reason);
1656   Local<Value> exception_value = Exception::Error(reason_string);
1657   Local<Object> exception_object = exception_value->ToObject(isolate);
1658   exception_object->Set(FIXED_ONE_BYTE_STRING(isolate, "code"),
1659                         OneByteString(isolate, code));
1660   args.GetReturnValue().Set(exception_object);
1661 }
1662
1663
1664 template <class Base>
1665 void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
1666   Base* w = Unwrap<Base>(args.Holder());
1667   Environment* env = w->ssl_env();
1668
1669   OPENSSL_CONST SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
1670   if (c == nullptr)
1671     return;
1672
1673   Local<Object> info = Object::New(env->isolate());
1674   const char* cipher_name = SSL_CIPHER_get_name(c);
1675   info->Set(env->name_string(), OneByteString(args.GetIsolate(), cipher_name));
1676   const char* cipher_version = SSL_CIPHER_get_version(c);
1677   info->Set(env->version_string(),
1678             OneByteString(args.GetIsolate(), cipher_version));
1679   args.GetReturnValue().Set(info);
1680 }
1681
1682
1683 #ifdef OPENSSL_NPN_NEGOTIATED
1684 template <class Base>
1685 int SSLWrap<Base>::AdvertiseNextProtoCallback(SSL* s,
1686                                               const unsigned char** data,
1687                                               unsigned int* len,
1688                                               void* arg) {
1689   Base* w = static_cast<Base*>(SSL_get_app_data(s));
1690   Environment* env = w->env();
1691   HandleScope handle_scope(env->isolate());
1692   Context::Scope context_scope(env->context());
1693
1694   if (w->npn_protos_.IsEmpty()) {
1695     // No initialization - no NPN protocols
1696     *data = reinterpret_cast<const unsigned char*>("");
1697     *len = 0;
1698   } else {
1699     Local<Object> obj = PersistentToLocal(env->isolate(), w->npn_protos_);
1700     *data = reinterpret_cast<const unsigned char*>(Buffer::Data(obj));
1701     *len = Buffer::Length(obj);
1702   }
1703
1704   return SSL_TLSEXT_ERR_OK;
1705 }
1706
1707
1708 template <class Base>
1709 int SSLWrap<Base>::SelectNextProtoCallback(SSL* s,
1710                                            unsigned char** out,
1711                                            unsigned char* outlen,
1712                                            const unsigned char* in,
1713                                            unsigned int inlen,
1714                                            void* arg) {
1715   Base* w = static_cast<Base*>(SSL_get_app_data(s));
1716   Environment* env = w->env();
1717   HandleScope handle_scope(env->isolate());
1718   Context::Scope context_scope(env->context());
1719
1720   // Release old protocol handler if present
1721   w->selected_npn_proto_.Reset();
1722
1723   if (w->npn_protos_.IsEmpty()) {
1724     // We should at least select one protocol
1725     // If server is using NPN
1726     *out = reinterpret_cast<unsigned char*>(const_cast<char*>("http/1.1"));
1727     *outlen = 8;
1728
1729     // set status: unsupported
1730     w->selected_npn_proto_.Reset(env->isolate(), False(env->isolate()));
1731
1732     return SSL_TLSEXT_ERR_OK;
1733   }
1734
1735   Local<Object> obj = PersistentToLocal(env->isolate(), w->npn_protos_);
1736   const unsigned char* npn_protos =
1737       reinterpret_cast<const unsigned char*>(Buffer::Data(obj));
1738   size_t len = Buffer::Length(obj);
1739
1740   int status = SSL_select_next_proto(out, outlen, in, inlen, npn_protos, len);
1741   Handle<Value> result;
1742   switch (status) {
1743     case OPENSSL_NPN_UNSUPPORTED:
1744       result = Null(env->isolate());
1745       break;
1746     case OPENSSL_NPN_NEGOTIATED:
1747       result = OneByteString(env->isolate(), *out, *outlen);
1748       break;
1749     case OPENSSL_NPN_NO_OVERLAP:
1750       result = False(env->isolate());
1751       break;
1752     default:
1753       break;
1754   }
1755
1756   if (!result.IsEmpty())
1757     w->selected_npn_proto_.Reset(env->isolate(), result);
1758
1759   return SSL_TLSEXT_ERR_OK;
1760 }
1761
1762
1763 template <class Base>
1764 void SSLWrap<Base>::GetNegotiatedProto(
1765     const FunctionCallbackInfo<Value>& args) {
1766   Base* w = Unwrap<Base>(args.Holder());
1767
1768   if (w->is_client()) {
1769     if (w->selected_npn_proto_.IsEmpty() == false) {
1770       args.GetReturnValue().Set(w->selected_npn_proto_);
1771     }
1772     return;
1773   }
1774
1775   const unsigned char* npn_proto;
1776   unsigned int npn_proto_len;
1777
1778   SSL_get0_next_proto_negotiated(w->ssl_, &npn_proto, &npn_proto_len);
1779
1780   if (!npn_proto)
1781     return args.GetReturnValue().Set(false);
1782
1783   args.GetReturnValue().Set(
1784       OneByteString(args.GetIsolate(), npn_proto, npn_proto_len));
1785 }
1786
1787
1788 template <class Base>
1789 void SSLWrap<Base>::SetNPNProtocols(const FunctionCallbackInfo<Value>& args) {
1790   Base* w = Unwrap<Base>(args.Holder());
1791
1792   if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
1793     return w->env()->ThrowTypeError("Must give a Buffer as first argument");
1794
1795   w->npn_protos_.Reset(args.GetIsolate(), args[0].As<Object>());
1796 }
1797 #endif  // OPENSSL_NPN_NEGOTIATED
1798
1799
1800 #ifdef NODE__HAVE_TLSEXT_STATUS_CB
1801 template <class Base>
1802 int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
1803   Base* w = static_cast<Base*>(SSL_get_app_data(s));
1804   Environment* env = w->env();
1805   HandleScope handle_scope(env->isolate());
1806
1807   if (w->is_client()) {
1808     // Incoming response
1809     const unsigned char* resp;
1810     int len = SSL_get_tlsext_status_ocsp_resp(s, &resp);
1811     Local<Value> arg;
1812     if (resp == nullptr) {
1813       arg = Null(env->isolate());
1814     } else {
1815       arg = Buffer::New(
1816           env,
1817           reinterpret_cast<char*>(const_cast<unsigned char*>(resp)),
1818           len);
1819     }
1820
1821     w->MakeCallback(env->onocspresponse_string(), 1, &arg);
1822
1823     // Somehow, client is expecting different return value here
1824     return 1;
1825   } else {
1826     // Outgoing response
1827     if (w->ocsp_response_.IsEmpty())
1828       return SSL_TLSEXT_ERR_NOACK;
1829
1830     Local<Object> obj = PersistentToLocal(env->isolate(), w->ocsp_response_);
1831     char* resp = Buffer::Data(obj);
1832     size_t len = Buffer::Length(obj);
1833
1834     // OpenSSL takes control of the pointer after accepting it
1835     char* data = reinterpret_cast<char*>(malloc(len));
1836     CHECK_NE(data, nullptr);
1837     memcpy(data, resp, len);
1838
1839     if (!SSL_set_tlsext_status_ocsp_resp(s, data, len))
1840       free(data);
1841     w->ocsp_response_.Reset();
1842
1843     return SSL_TLSEXT_ERR_OK;
1844   }
1845 }
1846 #endif  // NODE__HAVE_TLSEXT_STATUS_CB
1847
1848
1849 template <class Base>
1850 void SSLWrap<Base>::SSLGetter(Local<String> property,
1851                         const PropertyCallbackInfo<Value>& info) {
1852   HandleScope scope(info.GetIsolate());
1853
1854   SSL* ssl = Unwrap<Base>(info.Holder())->ssl_;
1855   Local<External> ext = External::New(info.GetIsolate(), ssl);
1856   info.GetReturnValue().Set(ext);
1857 }
1858
1859
1860 void Connection::OnClientHelloParseEnd(void* arg) {
1861   Connection* conn = static_cast<Connection*>(arg);
1862
1863   // Write all accumulated data
1864   int r = BIO_write(conn->bio_read_,
1865                     reinterpret_cast<char*>(conn->hello_data_),
1866                     conn->hello_offset_);
1867   conn->HandleBIOError(conn->bio_read_, "BIO_write", r);
1868   conn->SetShutdownFlags();
1869 }
1870
1871
1872 #ifdef SSL_PRINT_DEBUG
1873 # define DEBUG_PRINT(...) fprintf (stderr, __VA_ARGS__)
1874 #else
1875 # define DEBUG_PRINT(...)
1876 #endif
1877
1878
1879 int Connection::HandleBIOError(BIO *bio, const char* func, int rv) {
1880   if (rv >= 0)
1881     return rv;
1882
1883   int retry = BIO_should_retry(bio);
1884   (void) retry;  // unused if !defined(SSL_PRINT_DEBUG)
1885
1886   if (BIO_should_write(bio)) {
1887     DEBUG_PRINT("[%p] BIO: %s want write. should retry %d\n",
1888                 ssl_,
1889                 func,
1890                 retry);
1891     return 0;
1892
1893   } else if (BIO_should_read(bio)) {
1894     DEBUG_PRINT("[%p] BIO: %s want read. should retry %d\n", ssl_, func, retry);
1895     return 0;
1896
1897   } else {
1898     char ssl_error_buf[512];
1899     ERR_error_string_n(rv, ssl_error_buf, sizeof(ssl_error_buf));
1900
1901     HandleScope scope(ssl_env()->isolate());
1902     Local<Value> exception =
1903         Exception::Error(OneByteString(ssl_env()->isolate(), ssl_error_buf));
1904     object()->Set(ssl_env()->error_string(), exception);
1905
1906     DEBUG_PRINT("[%p] BIO: %s failed: (%d) %s\n",
1907                 ssl_,
1908                 func,
1909                 rv,
1910                 ssl_error_buf);
1911
1912     return rv;
1913   }
1914
1915   return 0;
1916 }
1917
1918
1919 int Connection::HandleSSLError(const char* func,
1920                                int rv,
1921                                ZeroStatus zs,
1922                                SyscallStatus ss) {
1923   ClearErrorOnReturn clear_error_on_return;
1924   (void) &clear_error_on_return;  // Silence unused variable warning.
1925
1926   if (rv > 0)
1927     return rv;
1928   if (rv == 0 && zs == kZeroIsNotAnError)
1929     return rv;
1930
1931   int err = SSL_get_error(ssl_, rv);
1932
1933   if (err == SSL_ERROR_NONE) {
1934     return 0;
1935
1936   } else if (err == SSL_ERROR_WANT_WRITE) {
1937     DEBUG_PRINT("[%p] SSL: %s want write\n", ssl_, func);
1938     return 0;
1939
1940   } else if (err == SSL_ERROR_WANT_READ) {
1941     DEBUG_PRINT("[%p] SSL: %s want read\n", ssl_, func);
1942     return 0;
1943
1944   } else if (err == SSL_ERROR_ZERO_RETURN) {
1945     HandleScope scope(ssl_env()->isolate());
1946
1947     Local<Value> exception =
1948         Exception::Error(ssl_env()->zero_return_string());
1949     object()->Set(ssl_env()->error_string(), exception);
1950     return rv;
1951
1952   } else if (err == SSL_ERROR_SYSCALL && ss == kIgnoreSyscall) {
1953     return 0;
1954
1955   } else {
1956     HandleScope scope(ssl_env()->isolate());
1957     BUF_MEM* mem;
1958     BIO *bio;
1959
1960     CHECK(err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL);
1961
1962     // XXX We need to drain the error queue for this thread or else OpenSSL
1963     // has the possibility of blocking connections? This problem is not well
1964     // understood. And we should be somehow propagating these errors up
1965     // into JavaScript. There is no test which demonstrates this problem.
1966     // https://github.com/joyent/node/issues/1719
1967     bio = BIO_new(BIO_s_mem());
1968     if (bio != nullptr) {
1969       ERR_print_errors(bio);
1970       BIO_get_mem_ptr(bio, &mem);
1971       Local<Value> exception = Exception::Error(
1972           OneByteString(ssl_env()->isolate(),
1973             mem->data,
1974             mem->length));
1975       object()->Set(ssl_env()->error_string(), exception);
1976       BIO_free_all(bio);
1977     }
1978
1979     return rv;
1980   }
1981
1982   return 0;
1983 }
1984
1985
1986 void Connection::ClearError() {
1987 #ifndef NDEBUG
1988   HandleScope scope(ssl_env()->isolate());
1989
1990   // We should clear the error in JS-land
1991   Local<String> error_key = ssl_env()->error_string();
1992   Local<Value> error = object()->Get(error_key);
1993   CHECK_EQ(error->BooleanValue(), false);
1994 #endif  // NDEBUG
1995 }
1996
1997
1998 void Connection::SetShutdownFlags() {
1999   HandleScope scope(ssl_env()->isolate());
2000
2001   int flags = SSL_get_shutdown(ssl_);
2002
2003   if (flags & SSL_SENT_SHUTDOWN) {
2004     Local<String> sent_shutdown_key = ssl_env()->sent_shutdown_string();
2005     object()->Set(sent_shutdown_key, True(ssl_env()->isolate()));
2006   }
2007
2008   if (flags & SSL_RECEIVED_SHUTDOWN) {
2009     Local<String> received_shutdown_key = ssl_env()->received_shutdown_string();
2010     object()->Set(received_shutdown_key, True(ssl_env()->isolate()));
2011   }
2012 }
2013
2014
2015 void Connection::NewSessionDoneCb() {
2016   HandleScope scope(env()->isolate());
2017
2018   MakeCallback(env()->onnewsessiondone_string(), 0, nullptr);
2019 }
2020
2021
2022 void Connection::Initialize(Environment* env, Handle<Object> target) {
2023   Local<FunctionTemplate> t = env->NewFunctionTemplate(Connection::New);
2024   t->InstanceTemplate()->SetInternalFieldCount(1);
2025   t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"));
2026
2027   env->SetProtoMethod(t, "encIn", Connection::EncIn);
2028   env->SetProtoMethod(t, "clearOut", Connection::ClearOut);
2029   env->SetProtoMethod(t, "clearIn", Connection::ClearIn);
2030   env->SetProtoMethod(t, "encOut", Connection::EncOut);
2031   env->SetProtoMethod(t, "clearPending", Connection::ClearPending);
2032   env->SetProtoMethod(t, "encPending", Connection::EncPending);
2033   env->SetProtoMethod(t, "start", Connection::Start);
2034   env->SetProtoMethod(t, "close", Connection::Close);
2035
2036   SSLWrap<Connection>::AddMethods(env, t);
2037
2038
2039 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2040   env->SetProtoMethod(t, "getServername", Connection::GetServername);
2041   env->SetProtoMethod(t, "setSNICallback",  Connection::SetSNICallback);
2042 #endif
2043
2044   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"),
2045               t->GetFunction());
2046 }
2047
2048
2049 int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
2050   // Quoting SSL_set_verify(3ssl):
2051   //
2052   //   The VerifyCallback function is used to control the behaviour when
2053   //   the SSL_VERIFY_PEER flag is set. It must be supplied by the
2054   //   application and receives two arguments: preverify_ok indicates,
2055   //   whether the verification of the certificate in question was passed
2056   //   (preverify_ok=1) or not (preverify_ok=0). x509_ctx is a pointer to
2057   //   the complete context used for the certificate chain verification.
2058   //
2059   //   The certificate chain is checked starting with the deepest nesting
2060   //   level (the root CA certificate) and worked upward to the peer's
2061   //   certificate.  At each level signatures and issuer attributes are
2062   //   checked.  Whenever a verification error is found, the error number is
2063   //   stored in x509_ctx and VerifyCallback is called with preverify_ok=0.
2064   //   By applying X509_CTX_store_* functions VerifyCallback can locate the
2065   //   certificate in question and perform additional steps (see EXAMPLES).
2066   //   If no error is found for a certificate, VerifyCallback is called
2067   //   with preverify_ok=1 before advancing to the next level.
2068   //
2069   //   The return value of VerifyCallback controls the strategy of the
2070   //   further verification process. If VerifyCallback returns 0, the
2071   //   verification process is immediately stopped with "verification
2072   //   failed" state. If SSL_VERIFY_PEER is set, a verification failure
2073   //   alert is sent to the peer and the TLS/SSL handshake is terminated. If
2074   //   VerifyCallback returns 1, the verification process is continued. If
2075   //   VerifyCallback always returns 1, the TLS/SSL handshake will not be
2076   //   terminated with respect to verification failures and the connection
2077   //   will be established. The calling process can however retrieve the
2078   //   error code of the last verification error using
2079   //   SSL_get_verify_result(3) or by maintaining its own error storage
2080   //   managed by VerifyCallback.
2081   //
2082   //   If no VerifyCallback is specified, the default callback will be
2083   //   used.  Its return value is identical to preverify_ok, so that any
2084   //   verification failure will lead to a termination of the TLS/SSL
2085   //   handshake with an alert message, if SSL_VERIFY_PEER is set.
2086   //
2087   // Since we cannot perform I/O quickly enough in this callback, we ignore
2088   // all preverify_ok errors and let the handshake continue. It is
2089   // imparative that the user use Connection::VerifyError after the
2090   // 'secure' callback has been made.
2091   return 1;
2092 }
2093
2094
2095 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2096 int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
2097   Connection* conn = static_cast<Connection*>(SSL_get_app_data(s));
2098   Environment* env = conn->env();
2099   HandleScope scope(env->isolate());
2100
2101   const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
2102
2103   if (servername) {
2104     conn->servername_.Reset(env->isolate(),
2105                             OneByteString(env->isolate(), servername));
2106
2107     // Call the SNI callback and use its return value as context
2108     if (!conn->sniObject_.IsEmpty()) {
2109       conn->sniContext_.Reset();
2110
2111       Local<Value> arg = PersistentToLocal(env->isolate(), conn->servername_);
2112       Local<Value> ret = conn->MakeCallback(env->onselect_string(), 1, &arg);
2113
2114       // If ret is SecureContext
2115       Local<FunctionTemplate> secure_context_constructor_template =
2116           env->secure_context_constructor_template();
2117       if (secure_context_constructor_template->HasInstance(ret)) {
2118         conn->sniContext_.Reset(env->isolate(), ret);
2119         SecureContext* sc = Unwrap<SecureContext>(ret.As<Object>());
2120         InitNPN(sc);
2121         SSL_set_SSL_CTX(s, sc->ctx_);
2122       } else {
2123         return SSL_TLSEXT_ERR_NOACK;
2124       }
2125     }
2126   }
2127
2128   return SSL_TLSEXT_ERR_OK;
2129 }
2130 #endif
2131
2132 void Connection::New(const FunctionCallbackInfo<Value>& args) {
2133   Environment* env = Environment::GetCurrent(args);
2134
2135   if (args.Length() < 1 || !args[0]->IsObject()) {
2136     env->ThrowError("First argument must be a tls module SecureContext");
2137     return;
2138   }
2139
2140   SecureContext* sc = Unwrap<SecureContext>(args[0]->ToObject(env->isolate()));
2141
2142   bool is_server = args[1]->BooleanValue();
2143
2144   SSLWrap<Connection>::Kind kind =
2145       is_server ? SSLWrap<Connection>::kServer : SSLWrap<Connection>::kClient;
2146   Connection* conn = new Connection(env, args.This(), sc, kind);
2147   conn->bio_read_ = NodeBIO::New();
2148   conn->bio_write_ = NodeBIO::New();
2149
2150   SSL_set_app_data(conn->ssl_, conn);
2151
2152   if (is_server)
2153     SSL_set_info_callback(conn->ssl_, SSLInfoCallback);
2154
2155   InitNPN(sc);
2156
2157 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2158   if (is_server) {
2159     SSL_CTX_set_tlsext_servername_callback(sc->ctx_, SelectSNIContextCallback_);
2160   } else if (args[2]->IsString()) {
2161     const node::Utf8Value servername(env->isolate(), args[2]);
2162     SSL_set_tlsext_host_name(conn->ssl_, *servername);
2163   }
2164 #endif
2165
2166   SSL_set_bio(conn->ssl_, conn->bio_read_, conn->bio_write_);
2167
2168 #ifdef SSL_MODE_RELEASE_BUFFERS
2169   long mode = SSL_get_mode(conn->ssl_);
2170   SSL_set_mode(conn->ssl_, mode | SSL_MODE_RELEASE_BUFFERS);
2171 #endif
2172
2173
2174   int verify_mode;
2175   if (is_server) {
2176     bool request_cert = args[2]->BooleanValue();
2177     if (!request_cert) {
2178       // Note reject_unauthorized ignored.
2179       verify_mode = SSL_VERIFY_NONE;
2180     } else {
2181       bool reject_unauthorized = args[3]->BooleanValue();
2182       verify_mode = SSL_VERIFY_PEER;
2183       if (reject_unauthorized)
2184         verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2185     }
2186   } else {
2187     // Note request_cert and reject_unauthorized are ignored for clients.
2188     verify_mode = SSL_VERIFY_NONE;
2189   }
2190
2191
2192   // Always allow a connection. We'll reject in javascript.
2193   SSL_set_verify(conn->ssl_, verify_mode, VerifyCallback);
2194
2195   if (is_server) {
2196     SSL_set_accept_state(conn->ssl_);
2197   } else {
2198     SSL_set_connect_state(conn->ssl_);
2199   }
2200 }
2201
2202
2203 void Connection::SSLInfoCallback(const SSL *ssl_, int where, int ret) {
2204   if (!(where & (SSL_CB_HANDSHAKE_START | SSL_CB_HANDSHAKE_DONE)))
2205     return;
2206
2207   // Be compatible with older versions of OpenSSL. SSL_get_app_data() wants
2208   // a non-const SSL* in OpenSSL <= 0.9.7e.
2209   SSL* ssl = const_cast<SSL*>(ssl_);
2210   Connection* conn = static_cast<Connection*>(SSL_get_app_data(ssl));
2211   Environment* env = conn->env();
2212   HandleScope handle_scope(env->isolate());
2213   Context::Scope context_scope(env->context());
2214
2215   if (where & SSL_CB_HANDSHAKE_START) {
2216     conn->MakeCallback(env->onhandshakestart_string(), 0, nullptr);
2217   }
2218
2219   if (where & SSL_CB_HANDSHAKE_DONE) {
2220     conn->MakeCallback(env->onhandshakedone_string(), 0, nullptr);
2221   }
2222 }
2223
2224
2225 void Connection::EncIn(const FunctionCallbackInfo<Value>& args) {
2226   Connection* conn = Unwrap<Connection>(args.Holder());
2227   Environment* env = conn->env();
2228
2229   if (args.Length() < 3) {
2230     return env->ThrowTypeError("Takes 3 parameters");
2231   }
2232
2233   if (!Buffer::HasInstance(args[0])) {
2234     return env->ThrowTypeError("Second argument should be a buffer");
2235   }
2236
2237   char* buffer_data = Buffer::Data(args[0]);
2238   size_t buffer_length = Buffer::Length(args[0]);
2239
2240   size_t off = args[1]->Int32Value();
2241   size_t len = args[2]->Int32Value();
2242
2243   if (!Buffer::IsWithinBounds(off, len, buffer_length))
2244     return env->ThrowError("off + len > buffer.length");
2245
2246   int bytes_written;
2247   char* data = buffer_data + off;
2248
2249   if (conn->is_server() && !conn->hello_parser_.IsEnded()) {
2250     // Just accumulate data, everything will be pushed to BIO later
2251     if (conn->hello_parser_.IsPaused()) {
2252       bytes_written = 0;
2253     } else {
2254       // Copy incoming data to the internal buffer
2255       // (which has a size of the biggest possible TLS frame)
2256       size_t available = sizeof(conn->hello_data_) - conn->hello_offset_;
2257       size_t copied = len < available ? len : available;
2258       memcpy(conn->hello_data_ + conn->hello_offset_, data, copied);
2259       conn->hello_offset_ += copied;
2260
2261       conn->hello_parser_.Parse(conn->hello_data_, conn->hello_offset_);
2262       bytes_written = copied;
2263     }
2264   } else {
2265     bytes_written = BIO_write(conn->bio_read_, data, len);
2266     conn->HandleBIOError(conn->bio_read_, "BIO_write", bytes_written);
2267     conn->SetShutdownFlags();
2268   }
2269
2270   args.GetReturnValue().Set(bytes_written);
2271 }
2272
2273
2274 void Connection::ClearOut(const FunctionCallbackInfo<Value>& args) {
2275   Connection* conn = Unwrap<Connection>(args.Holder());
2276   Environment* env = conn->env();
2277
2278   if (args.Length() < 3) {
2279     return env->ThrowTypeError("Takes 3 parameters");
2280   }
2281
2282   if (!Buffer::HasInstance(args[0])) {
2283     return env->ThrowTypeError("Second argument should be a buffer");
2284   }
2285
2286   char* buffer_data = Buffer::Data(args[0]);
2287   size_t buffer_length = Buffer::Length(args[0]);
2288
2289   size_t off = args[1]->Int32Value();
2290   size_t len = args[2]->Int32Value();
2291
2292   if (!Buffer::IsWithinBounds(off, len, buffer_length))
2293     return env->ThrowError("off + len > buffer.length");
2294
2295   if (!SSL_is_init_finished(conn->ssl_)) {
2296     int rv;
2297
2298     if (conn->is_server()) {
2299       rv = SSL_accept(conn->ssl_);
2300       conn->HandleSSLError("SSL_accept:ClearOut",
2301                            rv,
2302                            kZeroIsAnError,
2303                            kSyscallError);
2304     } else {
2305       rv = SSL_connect(conn->ssl_);
2306       conn->HandleSSLError("SSL_connect:ClearOut",
2307                            rv,
2308                            kZeroIsAnError,
2309                            kSyscallError);
2310     }
2311
2312     if (rv < 0) {
2313       return args.GetReturnValue().Set(rv);
2314     }
2315   }
2316
2317   int bytes_read = SSL_read(conn->ssl_, buffer_data + off, len);
2318   conn->HandleSSLError("SSL_read:ClearOut",
2319                        bytes_read,
2320                        kZeroIsNotAnError,
2321                        kSyscallError);
2322   conn->SetShutdownFlags();
2323
2324   args.GetReturnValue().Set(bytes_read);
2325 }
2326
2327
2328 void Connection::ClearPending(const FunctionCallbackInfo<Value>& args) {
2329   Connection* conn = Unwrap<Connection>(args.Holder());
2330   int bytes_pending = BIO_pending(conn->bio_read_);
2331   args.GetReturnValue().Set(bytes_pending);
2332 }
2333
2334
2335 void Connection::EncPending(const FunctionCallbackInfo<Value>& args) {
2336   Connection* conn = Unwrap<Connection>(args.Holder());
2337   int bytes_pending = BIO_pending(conn->bio_write_);
2338   args.GetReturnValue().Set(bytes_pending);
2339 }
2340
2341
2342 void Connection::EncOut(const FunctionCallbackInfo<Value>& args) {
2343   Connection* conn = Unwrap<Connection>(args.Holder());
2344   Environment* env = conn->env();
2345
2346   if (args.Length() < 3) {
2347     return env->ThrowTypeError("Takes 3 parameters");
2348   }
2349
2350   if (!Buffer::HasInstance(args[0])) {
2351     return env->ThrowTypeError("Second argument should be a buffer");
2352   }
2353
2354   char* buffer_data = Buffer::Data(args[0]);
2355   size_t buffer_length = Buffer::Length(args[0]);
2356
2357   size_t off = args[1]->Int32Value();
2358   size_t len = args[2]->Int32Value();
2359
2360   if (!Buffer::IsWithinBounds(off, len, buffer_length))
2361     return env->ThrowError("off + len > buffer.length");
2362
2363   int bytes_read = BIO_read(conn->bio_write_, buffer_data + off, len);
2364
2365   conn->HandleBIOError(conn->bio_write_, "BIO_read:EncOut", bytes_read);
2366   conn->SetShutdownFlags();
2367
2368   args.GetReturnValue().Set(bytes_read);
2369 }
2370
2371
2372 void Connection::ClearIn(const FunctionCallbackInfo<Value>& args) {
2373   Connection* conn = Unwrap<Connection>(args.Holder());
2374   Environment* env = conn->env();
2375
2376   if (args.Length() < 3) {
2377     return env->ThrowTypeError("Takes 3 parameters");
2378   }
2379
2380   if (!Buffer::HasInstance(args[0])) {
2381     return env->ThrowTypeError("Second argument should be a buffer");
2382   }
2383
2384   char* buffer_data = Buffer::Data(args[0]);
2385   size_t buffer_length = Buffer::Length(args[0]);
2386
2387   size_t off = args[1]->Int32Value();
2388   size_t len = args[2]->Int32Value();
2389
2390   if (!Buffer::IsWithinBounds(off, len, buffer_length))
2391     return env->ThrowError("off + len > buffer.length");
2392
2393   if (!SSL_is_init_finished(conn->ssl_)) {
2394     int rv;
2395     if (conn->is_server()) {
2396       rv = SSL_accept(conn->ssl_);
2397       conn->HandleSSLError("SSL_accept:ClearIn",
2398                            rv,
2399                            kZeroIsAnError,
2400                            kSyscallError);
2401     } else {
2402       rv = SSL_connect(conn->ssl_);
2403       conn->HandleSSLError("SSL_connect:ClearIn",
2404                            rv,
2405                            kZeroIsAnError,
2406                            kSyscallError);
2407     }
2408
2409     if (rv < 0) {
2410       return args.GetReturnValue().Set(rv);
2411     }
2412   }
2413
2414   int bytes_written = SSL_write(conn->ssl_, buffer_data + off, len);
2415
2416   conn->HandleSSLError("SSL_write:ClearIn",
2417                        bytes_written,
2418                        len == 0 ? kZeroIsNotAnError : kZeroIsAnError,
2419                        kSyscallError);
2420   conn->SetShutdownFlags();
2421
2422   args.GetReturnValue().Set(bytes_written);
2423 }
2424
2425
2426 void Connection::Start(const FunctionCallbackInfo<Value>& args) {
2427   Connection* conn = Unwrap<Connection>(args.Holder());
2428
2429   int rv = 0;
2430   if (!SSL_is_init_finished(conn->ssl_)) {
2431     if (conn->is_server()) {
2432       rv = SSL_accept(conn->ssl_);
2433       conn->HandleSSLError("SSL_accept:Start",
2434                            rv,
2435                            kZeroIsAnError,
2436                            kSyscallError);
2437     } else {
2438       rv = SSL_connect(conn->ssl_);
2439       conn->HandleSSLError("SSL_connect:Start",
2440                            rv,
2441                            kZeroIsAnError,
2442                            kSyscallError);
2443     }
2444   }
2445   args.GetReturnValue().Set(rv);
2446 }
2447
2448
2449 void Connection::Close(const FunctionCallbackInfo<Value>& args) {
2450   Connection* conn = Unwrap<Connection>(args.Holder());
2451
2452   if (conn->ssl_ != nullptr) {
2453     SSL_free(conn->ssl_);
2454     conn->ssl_ = nullptr;
2455   }
2456 }
2457
2458
2459 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2460 void Connection::GetServername(const FunctionCallbackInfo<Value>& args) {
2461   Connection* conn = Unwrap<Connection>(args.Holder());
2462
2463   if (conn->is_server() && !conn->servername_.IsEmpty()) {
2464     args.GetReturnValue().Set(conn->servername_);
2465   } else {
2466     args.GetReturnValue().Set(false);
2467   }
2468 }
2469
2470
2471 void Connection::SetSNICallback(const FunctionCallbackInfo<Value>& args) {
2472   Connection* conn = Unwrap<Connection>(args.Holder());
2473   Environment* env = conn->env();
2474
2475   if (args.Length() < 1 || !args[0]->IsFunction()) {
2476     return env->ThrowError("Must give a Function as first argument");
2477   }
2478
2479   Local<Object> obj = Object::New(env->isolate());
2480   obj->Set(FIXED_ONE_BYTE_STRING(args.GetIsolate(), "onselect"), args[0]);
2481   conn->sniObject_.Reset(args.GetIsolate(), obj);
2482 }
2483 #endif
2484
2485
2486 void CipherBase::Initialize(Environment* env, Handle<Object> target) {
2487   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
2488
2489   t->InstanceTemplate()->SetInternalFieldCount(1);
2490
2491   env->SetProtoMethod(t, "init", Init);
2492   env->SetProtoMethod(t, "initiv", InitIv);
2493   env->SetProtoMethod(t, "update", Update);
2494   env->SetProtoMethod(t, "final", Final);
2495   env->SetProtoMethod(t, "setAutoPadding", SetAutoPadding);
2496   env->SetProtoMethod(t, "getAuthTag", GetAuthTag);
2497   env->SetProtoMethod(t, "setAuthTag", SetAuthTag);
2498   env->SetProtoMethod(t, "setAAD", SetAAD);
2499
2500   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"),
2501               t->GetFunction());
2502 }
2503
2504
2505 void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
2506   CHECK_EQ(args.IsConstructCall(), true);
2507   CipherKind kind = args[0]->IsTrue() ? kCipher : kDecipher;
2508   Environment* env = Environment::GetCurrent(args);
2509   new CipherBase(env, args.This(), kind);
2510 }
2511
2512
2513 void CipherBase::Init(const char* cipher_type,
2514                       const char* key_buf,
2515                       int key_buf_len) {
2516   HandleScope scope(env()->isolate());
2517
2518   CHECK_EQ(cipher_, nullptr);
2519   cipher_ = EVP_get_cipherbyname(cipher_type);
2520   if (cipher_ == nullptr) {
2521     return env()->ThrowError("Unknown cipher");
2522   }
2523
2524   unsigned char key[EVP_MAX_KEY_LENGTH];
2525   unsigned char iv[EVP_MAX_IV_LENGTH];
2526
2527   int key_len = EVP_BytesToKey(cipher_,
2528                                EVP_md5(),
2529                                nullptr,
2530                                reinterpret_cast<const unsigned char*>(key_buf),
2531                                key_buf_len,
2532                                1,
2533                                key,
2534                                iv);
2535
2536   EVP_CIPHER_CTX_init(&ctx_);
2537   const bool encrypt = (kind_ == kCipher);
2538   EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt);
2539   if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
2540     EVP_CIPHER_CTX_cleanup(&ctx_);
2541     return env()->ThrowError("Invalid key length");
2542   }
2543
2544   EVP_CipherInit_ex(&ctx_,
2545                     nullptr,
2546                     nullptr,
2547                     reinterpret_cast<unsigned char*>(key),
2548                     reinterpret_cast<unsigned char*>(iv),
2549                     kind_ == kCipher);
2550   initialised_ = true;
2551 }
2552
2553
2554 void CipherBase::Init(const FunctionCallbackInfo<Value>& args) {
2555   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2556
2557   if (args.Length() < 2 ||
2558       !(args[0]->IsString() && Buffer::HasInstance(args[1]))) {
2559     return cipher->env()->ThrowError("Must give cipher-type, key");
2560   }
2561
2562   const node::Utf8Value cipher_type(args.GetIsolate(), args[0]);
2563   const char* key_buf = Buffer::Data(args[1]);
2564   ssize_t key_buf_len = Buffer::Length(args[1]);
2565   cipher->Init(*cipher_type, key_buf, key_buf_len);
2566 }
2567
2568
2569 void CipherBase::InitIv(const char* cipher_type,
2570                         const char* key,
2571                         int key_len,
2572                         const char* iv,
2573                         int iv_len) {
2574   HandleScope scope(env()->isolate());
2575
2576   cipher_ = EVP_get_cipherbyname(cipher_type);
2577   if (cipher_ == nullptr) {
2578     return env()->ThrowError("Unknown cipher");
2579   }
2580
2581   /* OpenSSL versions up to 0.9.8l failed to return the correct
2582      iv_length (0) for ECB ciphers */
2583   if (EVP_CIPHER_iv_length(cipher_) != iv_len &&
2584       !(EVP_CIPHER_mode(cipher_) == EVP_CIPH_ECB_MODE && iv_len == 0)) {
2585     return env()->ThrowError("Invalid IV length");
2586   }
2587   EVP_CIPHER_CTX_init(&ctx_);
2588   const bool encrypt = (kind_ == kCipher);
2589   EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt);
2590   if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
2591     EVP_CIPHER_CTX_cleanup(&ctx_);
2592     return env()->ThrowError("Invalid key length");
2593   }
2594
2595   EVP_CipherInit_ex(&ctx_,
2596                     nullptr,
2597                     nullptr,
2598                     reinterpret_cast<const unsigned char*>(key),
2599                     reinterpret_cast<const unsigned char*>(iv),
2600                     kind_ == kCipher);
2601   initialised_ = true;
2602 }
2603
2604
2605 void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
2606   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2607   Environment* env = cipher->env();
2608
2609   if (args.Length() < 3 || !args[0]->IsString()) {
2610     return env->ThrowError("Must give cipher-type, key, and iv as argument");
2611   }
2612
2613   THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
2614   THROW_AND_RETURN_IF_NOT_BUFFER(args[2]);
2615
2616   const node::Utf8Value cipher_type(env->isolate(), args[0]);
2617   ssize_t key_len = Buffer::Length(args[1]);
2618   const char* key_buf = Buffer::Data(args[1]);
2619   ssize_t iv_len = Buffer::Length(args[2]);
2620   const char* iv_buf = Buffer::Data(args[2]);
2621   cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len);
2622 }
2623
2624
2625 bool CipherBase::IsAuthenticatedMode() const {
2626   // check if this cipher operates in an AEAD mode that we support.
2627   if (!cipher_)
2628     return false;
2629   int mode = EVP_CIPHER_mode(cipher_);
2630   return mode == EVP_CIPH_GCM_MODE;
2631 }
2632
2633
2634 bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const {
2635   // only callable after Final and if encrypting.
2636   if (initialised_ || kind_ != kCipher || !auth_tag_)
2637     return false;
2638   *out_len = auth_tag_len_;
2639   *out = new char[auth_tag_len_];
2640   memcpy(*out, auth_tag_, auth_tag_len_);
2641   return true;
2642 }
2643
2644
2645 void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
2646   Environment* env = Environment::GetCurrent(args);
2647   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2648
2649   char* out = nullptr;
2650   unsigned int out_len = 0;
2651
2652   if (cipher->GetAuthTag(&out, &out_len)) {
2653     Local<Object> buf = Buffer::Use(env, out, out_len);
2654     args.GetReturnValue().Set(buf);
2655   } else {
2656     env->ThrowError("Attempting to get auth tag in unsupported state");
2657   }
2658 }
2659
2660
2661 bool CipherBase::SetAuthTag(const char* data, unsigned int len) {
2662   if (!initialised_ || !IsAuthenticatedMode() || kind_ != kDecipher)
2663     return false;
2664   delete[] auth_tag_;
2665   auth_tag_len_ = len;
2666   auth_tag_ = new char[len];
2667   memcpy(auth_tag_, data, len);
2668   return true;
2669 }
2670
2671
2672 void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
2673   Environment* env = Environment::GetCurrent(args);
2674
2675   Local<Object> buf = args[0].As<Object>();
2676   if (!buf->IsObject() || !Buffer::HasInstance(buf))
2677     return env->ThrowTypeError("Argument must be a Buffer");
2678
2679   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2680
2681   if (!cipher->SetAuthTag(Buffer::Data(buf), Buffer::Length(buf)))
2682     env->ThrowError("Attempting to set auth tag in unsupported state");
2683 }
2684
2685
2686 bool CipherBase::SetAAD(const char* data, unsigned int len) {
2687   if (!initialised_ || !IsAuthenticatedMode())
2688     return false;
2689   int outlen;
2690   if (!EVP_CipherUpdate(&ctx_,
2691                         nullptr,
2692                         &outlen,
2693                         reinterpret_cast<const unsigned char*>(data),
2694                         len)) {
2695     return false;
2696   }
2697   return true;
2698 }
2699
2700
2701 void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
2702   Environment* env = Environment::GetCurrent(args);
2703
2704   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
2705
2706   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2707
2708   if (!cipher->SetAAD(Buffer::Data(args[0]), Buffer::Length(args[0])))
2709     env->ThrowError("Attempting to set AAD in unsupported state");
2710 }
2711
2712
2713 bool CipherBase::Update(const char* data,
2714                         int len,
2715                         unsigned char** out,
2716                         int* out_len) {
2717   if (!initialised_)
2718     return 0;
2719
2720   // on first update:
2721   if (kind_ == kDecipher && IsAuthenticatedMode() && auth_tag_ != nullptr) {
2722     EVP_CIPHER_CTX_ctrl(&ctx_,
2723                         EVP_CTRL_GCM_SET_TAG,
2724                         auth_tag_len_,
2725                         reinterpret_cast<unsigned char*>(auth_tag_));
2726     delete[] auth_tag_;
2727     auth_tag_ = nullptr;
2728   }
2729
2730   *out_len = len + EVP_CIPHER_CTX_block_size(&ctx_);
2731   *out = new unsigned char[*out_len];
2732   return EVP_CipherUpdate(&ctx_,
2733                           *out,
2734                           out_len,
2735                           reinterpret_cast<const unsigned char*>(data),
2736                           len);
2737 }
2738
2739
2740 void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
2741   Environment* env = Environment::GetCurrent(args);
2742
2743   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2744
2745   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
2746
2747   unsigned char* out = nullptr;
2748   bool r;
2749   int out_len = 0;
2750
2751   // Only copy the data if we have to, because it's a string
2752   if (args[0]->IsString()) {
2753     Local<String> string = args[0].As<String>();
2754     enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
2755     if (!StringBytes::IsValidString(env->isolate(), string, encoding))
2756       return env->ThrowTypeError("Bad input string");
2757     size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
2758     char* buf = new char[buflen];
2759     size_t written = StringBytes::Write(env->isolate(),
2760                                         buf,
2761                                         buflen,
2762                                         string,
2763                                         encoding);
2764     r = cipher->Update(buf, written, &out, &out_len);
2765     delete[] buf;
2766   } else {
2767     char* buf = Buffer::Data(args[0]);
2768     size_t buflen = Buffer::Length(args[0]);
2769     r = cipher->Update(buf, buflen, &out, &out_len);
2770   }
2771
2772   if (!r) {
2773     delete[] out;
2774     return ThrowCryptoError(env,
2775                             ERR_get_error(),
2776                             "Trying to add data in unsupported state");
2777   }
2778
2779   Local<Object> buf = Buffer::New(env, reinterpret_cast<char*>(out), out_len);
2780   if (out)
2781     delete[] out;
2782
2783   args.GetReturnValue().Set(buf);
2784 }
2785
2786
2787 bool CipherBase::SetAutoPadding(bool auto_padding) {
2788   if (!initialised_)
2789     return false;
2790   return EVP_CIPHER_CTX_set_padding(&ctx_, auto_padding);
2791 }
2792
2793
2794 void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
2795   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2796   cipher->SetAutoPadding(args.Length() < 1 || args[0]->BooleanValue());
2797 }
2798
2799
2800 bool CipherBase::Final(unsigned char** out, int *out_len) {
2801   if (!initialised_)
2802     return false;
2803
2804   *out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx_)];
2805   int r = EVP_CipherFinal_ex(&ctx_, *out, out_len);
2806
2807   if (r && kind_ == kCipher) {
2808     delete[] auth_tag_;
2809     auth_tag_ = nullptr;
2810     if (IsAuthenticatedMode()) {
2811       auth_tag_len_ = EVP_GCM_TLS_TAG_LEN;  // use default tag length
2812       auth_tag_ = new char[auth_tag_len_];
2813       memset(auth_tag_, 0, auth_tag_len_);
2814       EVP_CIPHER_CTX_ctrl(&ctx_,
2815                           EVP_CTRL_GCM_GET_TAG,
2816                           auth_tag_len_,
2817                           reinterpret_cast<unsigned char*>(auth_tag_));
2818     }
2819   }
2820
2821   EVP_CIPHER_CTX_cleanup(&ctx_);
2822   initialised_ = false;
2823
2824   return r == 1;
2825 }
2826
2827
2828 void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
2829   Environment* env = Environment::GetCurrent(args);
2830
2831   CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
2832
2833   unsigned char* out_value = nullptr;
2834   int out_len = -1;
2835   Local<Value> outString;
2836
2837   bool r = cipher->Final(&out_value, &out_len);
2838
2839   if (out_len <= 0 || !r) {
2840     delete[] out_value;
2841     out_value = nullptr;
2842     out_len = 0;
2843     if (!r) {
2844       const char* msg = cipher->IsAuthenticatedMode() ?
2845           "Unsupported state or unable to authenticate data" :
2846           "Unsupported state";
2847
2848       return ThrowCryptoError(env,
2849                               ERR_get_error(),
2850                               msg);
2851     }
2852   }
2853
2854   args.GetReturnValue().Set(
2855       Buffer::New(env, reinterpret_cast<char*>(out_value), out_len));
2856   delete[] out_value;
2857 }
2858
2859
2860 void Hmac::Initialize(Environment* env, v8::Handle<v8::Object> target) {
2861   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
2862
2863   t->InstanceTemplate()->SetInternalFieldCount(1);
2864
2865   env->SetProtoMethod(t, "init", HmacInit);
2866   env->SetProtoMethod(t, "update", HmacUpdate);
2867   env->SetProtoMethod(t, "digest", HmacDigest);
2868
2869   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"), t->GetFunction());
2870 }
2871
2872
2873 void Hmac::New(const FunctionCallbackInfo<Value>& args) {
2874   Environment* env = Environment::GetCurrent(args);
2875   new Hmac(env, args.This());
2876 }
2877
2878
2879 void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
2880   HandleScope scope(env()->isolate());
2881
2882   CHECK_EQ(md_, nullptr);
2883   md_ = EVP_get_digestbyname(hash_type);
2884   if (md_ == nullptr) {
2885     return env()->ThrowError("Unknown message digest");
2886   }
2887   HMAC_CTX_init(&ctx_);
2888   if (key_len == 0) {
2889     HMAC_Init(&ctx_, "", 0, md_);
2890   } else {
2891     HMAC_Init(&ctx_, key, key_len, md_);
2892   }
2893   initialised_ = true;
2894 }
2895
2896
2897 void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
2898   Hmac* hmac = Unwrap<Hmac>(args.Holder());
2899   Environment* env = hmac->env();
2900
2901   if (args.Length() < 2 || !args[0]->IsString()) {
2902     return env->ThrowError("Must give hashtype string, key as arguments");
2903   }
2904
2905   THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
2906
2907   const node::Utf8Value hash_type(env->isolate(), args[0]);
2908   const char* buffer_data = Buffer::Data(args[1]);
2909   size_t buffer_length = Buffer::Length(args[1]);
2910   hmac->HmacInit(*hash_type, buffer_data, buffer_length);
2911 }
2912
2913
2914 bool Hmac::HmacUpdate(const char* data, int len) {
2915   if (!initialised_)
2916     return false;
2917   HMAC_Update(&ctx_, reinterpret_cast<const unsigned char*>(data), len);
2918   return true;
2919 }
2920
2921
2922 void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
2923   Environment* env = Environment::GetCurrent(args);
2924
2925   Hmac* hmac = Unwrap<Hmac>(args.Holder());
2926
2927   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
2928
2929   // Only copy the data if we have to, because it's a string
2930   bool r;
2931   if (args[0]->IsString()) {
2932     Local<String> string = args[0].As<String>();
2933     enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
2934     if (!StringBytes::IsValidString(env->isolate(), string, encoding))
2935       return env->ThrowTypeError("Bad input string");
2936     size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
2937     char* buf = new char[buflen];
2938     size_t written = StringBytes::Write(env->isolate(),
2939                                         buf,
2940                                         buflen,
2941                                         string,
2942                                         encoding);
2943     r = hmac->HmacUpdate(buf, written);
2944     delete[] buf;
2945   } else {
2946     char* buf = Buffer::Data(args[0]);
2947     size_t buflen = Buffer::Length(args[0]);
2948     r = hmac->HmacUpdate(buf, buflen);
2949   }
2950
2951   if (!r) {
2952     return env->ThrowTypeError("HmacUpdate fail");
2953   }
2954 }
2955
2956
2957 bool Hmac::HmacDigest(unsigned char** md_value, unsigned int* md_len) {
2958   if (!initialised_)
2959     return false;
2960   *md_value = new unsigned char[EVP_MAX_MD_SIZE];
2961   HMAC_Final(&ctx_, *md_value, md_len);
2962   HMAC_CTX_cleanup(&ctx_);
2963   initialised_ = false;
2964   return true;
2965 }
2966
2967
2968 void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
2969   Environment* env = Environment::GetCurrent(args);
2970
2971   Hmac* hmac = Unwrap<Hmac>(args.Holder());
2972
2973   enum encoding encoding = BUFFER;
2974   if (args.Length() >= 1) {
2975     encoding = ParseEncoding(env->isolate(),
2976                              args[0]->ToString(env->isolate()),
2977                              BUFFER);
2978   }
2979
2980   unsigned char* md_value = nullptr;
2981   unsigned int md_len = 0;
2982
2983   bool r = hmac->HmacDigest(&md_value, &md_len);
2984   if (!r) {
2985     md_value = nullptr;
2986     md_len = 0;
2987   }
2988
2989   Local<Value> rc = StringBytes::Encode(env->isolate(),
2990                                         reinterpret_cast<const char*>(md_value),
2991                                         md_len,
2992                                         encoding);
2993   delete[] md_value;
2994   args.GetReturnValue().Set(rc);
2995 }
2996
2997
2998 void Hash::Initialize(Environment* env, v8::Handle<v8::Object> target) {
2999   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
3000
3001   t->InstanceTemplate()->SetInternalFieldCount(1);
3002
3003   env->SetProtoMethod(t, "update", HashUpdate);
3004   env->SetProtoMethod(t, "digest", HashDigest);
3005
3006   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"), t->GetFunction());
3007 }
3008
3009
3010 void Hash::New(const FunctionCallbackInfo<Value>& args) {
3011   Environment* env = Environment::GetCurrent(args);
3012
3013   if (args.Length() == 0 || !args[0]->IsString()) {
3014     return env->ThrowError("Must give hashtype string as argument");
3015   }
3016
3017   const node::Utf8Value hash_type(env->isolate(), args[0]);
3018
3019   Hash* hash = new Hash(env, args.This());
3020   if (!hash->HashInit(*hash_type)) {
3021     return env->ThrowError("Digest method not supported");
3022   }
3023 }
3024
3025
3026 bool Hash::HashInit(const char* hash_type) {
3027   CHECK_EQ(md_, nullptr);
3028   md_ = EVP_get_digestbyname(hash_type);
3029   if (md_ == nullptr)
3030     return false;
3031   EVP_MD_CTX_init(&mdctx_);
3032   EVP_DigestInit_ex(&mdctx_, md_, nullptr);
3033   initialised_ = true;
3034   return true;
3035 }
3036
3037
3038 bool Hash::HashUpdate(const char* data, int len) {
3039   if (!initialised_)
3040     return false;
3041   EVP_DigestUpdate(&mdctx_, data, len);
3042   return true;
3043 }
3044
3045
3046 void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
3047   Environment* env = Environment::GetCurrent(args);
3048
3049   Hash* hash = Unwrap<Hash>(args.Holder());
3050
3051   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
3052
3053   // Only copy the data if we have to, because it's a string
3054   bool r;
3055   if (args[0]->IsString()) {
3056     Local<String> string = args[0].As<String>();
3057     enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
3058     if (!StringBytes::IsValidString(env->isolate(), string, encoding))
3059       return env->ThrowTypeError("Bad input string");
3060     size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
3061     char* buf = new char[buflen];
3062     size_t written = StringBytes::Write(env->isolate(),
3063                                         buf,
3064                                         buflen,
3065                                         string,
3066                                         encoding);
3067     r = hash->HashUpdate(buf, written);
3068     delete[] buf;
3069   } else {
3070     char* buf = Buffer::Data(args[0]);
3071     size_t buflen = Buffer::Length(args[0]);
3072     r = hash->HashUpdate(buf, buflen);
3073   }
3074
3075   if (!r) {
3076     return env->ThrowTypeError("HashUpdate fail");
3077   }
3078 }
3079
3080
3081 void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
3082   Environment* env = Environment::GetCurrent(args);
3083
3084   Hash* hash = Unwrap<Hash>(args.Holder());
3085
3086   if (!hash->initialised_) {
3087     return env->ThrowError("Not initialized");
3088   }
3089
3090   enum encoding encoding = BUFFER;
3091   if (args.Length() >= 1) {
3092     encoding = ParseEncoding(env->isolate(),
3093                              args[0]->ToString(env->isolate()),
3094                              BUFFER);
3095   }
3096
3097   unsigned char md_value[EVP_MAX_MD_SIZE];
3098   unsigned int md_len;
3099
3100   EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
3101   EVP_MD_CTX_cleanup(&hash->mdctx_);
3102   hash->initialised_ = false;
3103
3104   Local<Value> rc = StringBytes::Encode(env->isolate(),
3105                                         reinterpret_cast<const char*>(md_value),
3106                                         md_len,
3107                                         encoding);
3108   args.GetReturnValue().Set(rc);
3109 }
3110
3111
3112 void SignBase::CheckThrow(SignBase::Error error) {
3113   HandleScope scope(env()->isolate());
3114
3115   switch (error) {
3116     case kSignUnknownDigest:
3117       return env()->ThrowError("Unknown message digest");
3118
3119     case kSignNotInitialised:
3120       return env()->ThrowError("Not initialised");
3121
3122     case kSignInit:
3123     case kSignUpdate:
3124     case kSignPrivateKey:
3125     case kSignPublicKey:
3126       {
3127         unsigned long err = ERR_get_error();
3128         if (err)
3129           return ThrowCryptoError(env(), err);
3130         switch (error) {
3131           case kSignInit:
3132             return env()->ThrowError("EVP_SignInit_ex failed");
3133           case kSignUpdate:
3134             return env()->ThrowError("EVP_SignUpdate failed");
3135           case kSignPrivateKey:
3136             return env()->ThrowError("PEM_read_bio_PrivateKey failed");
3137           case kSignPublicKey:
3138             return env()->ThrowError("PEM_read_bio_PUBKEY failed");
3139           default:
3140             abort();
3141         }
3142       }
3143
3144     case kSignOk:
3145       return;
3146   }
3147 }
3148
3149
3150
3151
3152 void Sign::Initialize(Environment* env, v8::Handle<v8::Object> target) {
3153   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
3154
3155   t->InstanceTemplate()->SetInternalFieldCount(1);
3156
3157   env->SetProtoMethod(t, "init", SignInit);
3158   env->SetProtoMethod(t, "update", SignUpdate);
3159   env->SetProtoMethod(t, "sign", SignFinal);
3160
3161   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"), t->GetFunction());
3162 }
3163
3164
3165 void Sign::New(const FunctionCallbackInfo<Value>& args) {
3166   Environment* env = Environment::GetCurrent(args);
3167   new Sign(env, args.This());
3168 }
3169
3170
3171 SignBase::Error Sign::SignInit(const char* sign_type) {
3172   CHECK_EQ(md_, nullptr);
3173   md_ = EVP_get_digestbyname(sign_type);
3174   if (!md_)
3175     return kSignUnknownDigest;
3176
3177   EVP_MD_CTX_init(&mdctx_);
3178   if (!EVP_SignInit_ex(&mdctx_, md_, nullptr))
3179     return kSignInit;
3180   initialised_ = true;
3181
3182   return kSignOk;
3183 }
3184
3185
3186 void Sign::SignInit(const FunctionCallbackInfo<Value>& args) {
3187   Sign* sign = Unwrap<Sign>(args.Holder());
3188
3189   if (args.Length() == 0 || !args[0]->IsString()) {
3190     return sign->env()->ThrowError("Must give signtype string as argument");
3191   }
3192
3193   const node::Utf8Value sign_type(args.GetIsolate(), args[0]);
3194   sign->CheckThrow(sign->SignInit(*sign_type));
3195 }
3196
3197
3198 SignBase::Error Sign::SignUpdate(const char* data, int len) {
3199   if (!initialised_)
3200     return kSignNotInitialised;
3201   if (!EVP_SignUpdate(&mdctx_, data, len))
3202     return kSignUpdate;
3203   return kSignOk;
3204 }
3205
3206
3207 void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
3208   Environment* env = Environment::GetCurrent(args);
3209
3210   Sign* sign = Unwrap<Sign>(args.Holder());
3211
3212   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
3213
3214   // Only copy the data if we have to, because it's a string
3215   Error err;
3216   if (args[0]->IsString()) {
3217     Local<String> string = args[0].As<String>();
3218     enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
3219     if (!StringBytes::IsValidString(env->isolate(), string, encoding))
3220       return env->ThrowTypeError("Bad input string");
3221     size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
3222     char* buf = new char[buflen];
3223     size_t written = StringBytes::Write(env->isolate(),
3224                                         buf,
3225                                         buflen,
3226                                         string,
3227                                         encoding);
3228     err = sign->SignUpdate(buf, written);
3229     delete[] buf;
3230   } else {
3231     char* buf = Buffer::Data(args[0]);
3232     size_t buflen = Buffer::Length(args[0]);
3233     err = sign->SignUpdate(buf, buflen);
3234   }
3235
3236   sign->CheckThrow(err);
3237 }
3238
3239
3240 SignBase::Error Sign::SignFinal(const char* key_pem,
3241                                 int key_pem_len,
3242                                 const char* passphrase,
3243                                 unsigned char** sig,
3244                                 unsigned int *sig_len) {
3245   if (!initialised_)
3246     return kSignNotInitialised;
3247
3248   BIO* bp = nullptr;
3249   EVP_PKEY* pkey = nullptr;
3250   bool fatal = true;
3251
3252   bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
3253   if (bp == nullptr)
3254     goto exit;
3255
3256   pkey = PEM_read_bio_PrivateKey(bp,
3257                                  nullptr,
3258                                  CryptoPemCallback,
3259                                  const_cast<char*>(passphrase));
3260   if (pkey == nullptr)
3261     goto exit;
3262
3263   if (EVP_SignFinal(&mdctx_, *sig, sig_len, pkey))
3264     fatal = false;
3265
3266   initialised_ = false;
3267
3268  exit:
3269   if (pkey != nullptr)
3270     EVP_PKEY_free(pkey);
3271   if (bp != nullptr)
3272     BIO_free_all(bp);
3273
3274   EVP_MD_CTX_cleanup(&mdctx_);
3275
3276   if (fatal)
3277     return kSignPrivateKey;
3278
3279   return kSignOk;
3280 }
3281
3282
3283 void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
3284   Environment* env = Environment::GetCurrent(args);
3285
3286   Sign* sign = Unwrap<Sign>(args.Holder());
3287
3288   unsigned char* md_value;
3289   unsigned int md_len;
3290
3291   unsigned int len = args.Length();
3292   enum encoding encoding = BUFFER;
3293   if (len >= 2 && args[1]->IsString()) {
3294     encoding = ParseEncoding(env->isolate(),
3295                              args[1]->ToString(env->isolate()),
3296                              BUFFER);
3297   }
3298
3299   node::Utf8Value passphrase(env->isolate(), args[2]);
3300
3301   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
3302   size_t buf_len = Buffer::Length(args[0]);
3303   char* buf = Buffer::Data(args[0]);
3304
3305   md_len = 8192;  // Maximum key size is 8192 bits
3306   md_value = new unsigned char[md_len];
3307
3308   Error err = sign->SignFinal(
3309       buf,
3310       buf_len,
3311       len >= 3 && !args[2]->IsNull() ? *passphrase : nullptr,
3312       &md_value,
3313       &md_len);
3314   if (err != kSignOk) {
3315     delete[] md_value;
3316     md_value = nullptr;
3317     md_len = 0;
3318     return sign->CheckThrow(err);
3319   }
3320
3321   Local<Value> rc = StringBytes::Encode(env->isolate(),
3322                                         reinterpret_cast<const char*>(md_value),
3323                                         md_len,
3324                                         encoding);
3325   delete[] md_value;
3326   args.GetReturnValue().Set(rc);
3327 }
3328
3329
3330 void Verify::Initialize(Environment* env, v8::Handle<v8::Object> target) {
3331   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
3332
3333   t->InstanceTemplate()->SetInternalFieldCount(1);
3334
3335   env->SetProtoMethod(t, "init", VerifyInit);
3336   env->SetProtoMethod(t, "update", VerifyUpdate);
3337   env->SetProtoMethod(t, "verify", VerifyFinal);
3338
3339   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"),
3340               t->GetFunction());
3341 }
3342
3343
3344 void Verify::New(const FunctionCallbackInfo<Value>& args) {
3345   Environment* env = Environment::GetCurrent(args);
3346   new Verify(env, args.This());
3347 }
3348
3349
3350 SignBase::Error Verify::VerifyInit(const char* verify_type) {
3351   CHECK_EQ(md_, nullptr);
3352   md_ = EVP_get_digestbyname(verify_type);
3353   if (md_ == nullptr)
3354     return kSignUnknownDigest;
3355
3356   EVP_MD_CTX_init(&mdctx_);
3357   if (!EVP_VerifyInit_ex(&mdctx_, md_, nullptr))
3358     return kSignInit;
3359   initialised_ = true;
3360
3361   return kSignOk;
3362 }
3363
3364
3365 void Verify::VerifyInit(const FunctionCallbackInfo<Value>& args) {
3366   Verify* verify = Unwrap<Verify>(args.Holder());
3367
3368   if (args.Length() == 0 || !args[0]->IsString()) {
3369     return verify->env()->ThrowError("Must give verifytype string as argument");
3370   }
3371
3372   const node::Utf8Value verify_type(args.GetIsolate(), args[0]);
3373   verify->CheckThrow(verify->VerifyInit(*verify_type));
3374 }
3375
3376
3377 SignBase::Error Verify::VerifyUpdate(const char* data, int len) {
3378   if (!initialised_)
3379     return kSignNotInitialised;
3380
3381   if (!EVP_VerifyUpdate(&mdctx_, data, len))
3382     return kSignUpdate;
3383
3384   return kSignOk;
3385 }
3386
3387
3388 void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
3389   Environment* env = Environment::GetCurrent(args);
3390
3391   Verify* verify = Unwrap<Verify>(args.Holder());
3392
3393   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
3394
3395   // Only copy the data if we have to, because it's a string
3396   Error err;
3397   if (args[0]->IsString()) {
3398     Local<String> string = args[0].As<String>();
3399     enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
3400     if (!StringBytes::IsValidString(env->isolate(), string, encoding))
3401       return env->ThrowTypeError("Bad input string");
3402     size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
3403     char* buf = new char[buflen];
3404     size_t written = StringBytes::Write(env->isolate(),
3405                                         buf,
3406                                         buflen,
3407                                         string,
3408                                         encoding);
3409     err = verify->VerifyUpdate(buf, written);
3410     delete[] buf;
3411   } else {
3412     char* buf = Buffer::Data(args[0]);
3413     size_t buflen = Buffer::Length(args[0]);
3414     err = verify->VerifyUpdate(buf, buflen);
3415   }
3416
3417   verify->CheckThrow(err);
3418 }
3419
3420
3421 SignBase::Error Verify::VerifyFinal(const char* key_pem,
3422                                     int key_pem_len,
3423                                     const char* sig,
3424                                     int siglen,
3425                                     bool* verify_result) {
3426   if (!initialised_)
3427     return kSignNotInitialised;
3428
3429   ClearErrorOnReturn clear_error_on_return;
3430   (void) &clear_error_on_return;  // Silence compiler warning.
3431
3432   EVP_PKEY* pkey = nullptr;
3433   BIO* bp = nullptr;
3434   X509* x509 = nullptr;
3435   bool fatal = true;
3436   int r = 0;
3437
3438   bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
3439   if (bp == nullptr)
3440     goto exit;
3441
3442   // Check if this is a PKCS#8 or RSA public key before trying as X.509.
3443   // Split this out into a separate function once we have more than one
3444   // consumer of public keys.
3445   if (strncmp(key_pem, PUBLIC_KEY_PFX, PUBLIC_KEY_PFX_LEN) == 0) {
3446     pkey = PEM_read_bio_PUBKEY(bp, nullptr, CryptoPemCallback, nullptr);
3447     if (pkey == nullptr)
3448       goto exit;
3449   } else if (strncmp(key_pem, PUBRSA_KEY_PFX, PUBRSA_KEY_PFX_LEN) == 0) {
3450     RSA* rsa =
3451         PEM_read_bio_RSAPublicKey(bp, nullptr, CryptoPemCallback, nullptr);
3452     if (rsa) {
3453       pkey = EVP_PKEY_new();
3454       if (pkey)
3455         EVP_PKEY_set1_RSA(pkey, rsa);
3456       RSA_free(rsa);
3457     }
3458     if (pkey == nullptr)
3459       goto exit;
3460   } else {
3461     // X.509 fallback
3462     x509 = PEM_read_bio_X509(bp, nullptr, CryptoPemCallback, nullptr);
3463     if (x509 == nullptr)
3464       goto exit;
3465
3466     pkey = X509_get_pubkey(x509);
3467     if (pkey == nullptr)
3468       goto exit;
3469   }
3470
3471   fatal = false;
3472   r = EVP_VerifyFinal(&mdctx_,
3473                       reinterpret_cast<const unsigned char*>(sig),
3474                       siglen,
3475                       pkey);
3476
3477  exit:
3478   if (pkey != nullptr)
3479     EVP_PKEY_free(pkey);
3480   if (bp != nullptr)
3481     BIO_free_all(bp);
3482   if (x509 != nullptr)
3483     X509_free(x509);
3484
3485   EVP_MD_CTX_cleanup(&mdctx_);
3486   initialised_ = false;
3487
3488   if (fatal)
3489     return kSignPublicKey;
3490
3491   *verify_result = r == 1;
3492   return kSignOk;
3493 }
3494
3495
3496 void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
3497   Environment* env = Environment::GetCurrent(args);
3498
3499   Verify* verify = Unwrap<Verify>(args.Holder());
3500
3501   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
3502   char* kbuf = Buffer::Data(args[0]);
3503   ssize_t klen = Buffer::Length(args[0]);
3504
3505   THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1]);
3506
3507   // BINARY works for both buffers and binary strings.
3508   enum encoding encoding = BINARY;
3509   if (args.Length() >= 3) {
3510     encoding = ParseEncoding(env->isolate(),
3511                              args[2]->ToString(env->isolate()),
3512                              BINARY);
3513   }
3514
3515   ssize_t hlen = StringBytes::Size(env->isolate(), args[1], encoding);
3516
3517   // only copy if we need to, because it's a string.
3518   char* hbuf;
3519   if (args[1]->IsString()) {
3520     hbuf = new char[hlen];
3521     ssize_t hwritten = StringBytes::Write(env->isolate(),
3522                                           hbuf,
3523                                           hlen,
3524                                           args[1],
3525                                           encoding);
3526     CHECK_EQ(hwritten, hlen);
3527   } else {
3528     hbuf = Buffer::Data(args[1]);
3529   }
3530
3531   bool verify_result;
3532   Error err = verify->VerifyFinal(kbuf, klen, hbuf, hlen, &verify_result);
3533   if (args[1]->IsString())
3534     delete[] hbuf;
3535   if (err != kSignOk)
3536     return verify->CheckThrow(err);
3537   args.GetReturnValue().Set(verify_result);
3538 }
3539
3540
3541 template <PublicKeyCipher::Operation operation,
3542           PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
3543           PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3544 bool PublicKeyCipher::Cipher(const char* key_pem,
3545                              int key_pem_len,
3546                              const char* passphrase,
3547                              int padding,
3548                              const unsigned char* data,
3549                              int len,
3550                              unsigned char** out,
3551                              size_t* out_len) {
3552   EVP_PKEY* pkey = nullptr;
3553   EVP_PKEY_CTX* ctx = nullptr;
3554   BIO* bp = nullptr;
3555   X509* x509 = nullptr;
3556   bool fatal = true;
3557
3558   bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
3559   if (bp == nullptr)
3560     goto exit;
3561
3562   // Check if this is a PKCS#8 or RSA public key before trying as X.509 and
3563   // private key.
3564   if (operation == kEncrypt &&
3565       strncmp(key_pem, PUBLIC_KEY_PFX, PUBLIC_KEY_PFX_LEN) == 0) {
3566     pkey = PEM_read_bio_PUBKEY(bp, nullptr, nullptr, nullptr);
3567     if (pkey == nullptr)
3568       goto exit;
3569   } else if (operation == kEncrypt &&
3570              strncmp(key_pem, PUBRSA_KEY_PFX, PUBRSA_KEY_PFX_LEN) == 0) {
3571     RSA* rsa = PEM_read_bio_RSAPublicKey(bp, nullptr, nullptr, nullptr);
3572     if (rsa) {
3573       pkey = EVP_PKEY_new();
3574       if (pkey)
3575         EVP_PKEY_set1_RSA(pkey, rsa);
3576       RSA_free(rsa);
3577     }
3578     if (pkey == nullptr)
3579       goto exit;
3580   } else if (operation == kEncrypt &&
3581              strncmp(key_pem, CERTIFICATE_PFX, CERTIFICATE_PFX_LEN) == 0) {
3582     x509 = PEM_read_bio_X509(bp, nullptr, CryptoPemCallback, nullptr);
3583     if (x509 == nullptr)
3584       goto exit;
3585
3586     pkey = X509_get_pubkey(x509);
3587     if (pkey == nullptr)
3588       goto exit;
3589   } else {
3590     pkey = PEM_read_bio_PrivateKey(bp,
3591                                    nullptr,
3592                                    CryptoPemCallback,
3593                                    const_cast<char*>(passphrase));
3594     if (pkey == nullptr)
3595       goto exit;
3596   }
3597
3598   ctx = EVP_PKEY_CTX_new(pkey, nullptr);
3599   if (!ctx)
3600     goto exit;
3601   if (EVP_PKEY_cipher_init(ctx) <= 0)
3602     goto exit;
3603   if (EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0)
3604     goto exit;
3605
3606   if (EVP_PKEY_cipher(ctx, nullptr, out_len, data, len) <= 0)
3607     goto exit;
3608
3609   *out = new unsigned char[*out_len];
3610
3611   if (EVP_PKEY_cipher(ctx, *out, out_len, data, len) <= 0)
3612     goto exit;
3613
3614   fatal = false;
3615
3616  exit:
3617   if (pkey != nullptr)
3618     EVP_PKEY_free(pkey);
3619   if (bp != nullptr)
3620     BIO_free_all(bp);
3621   if (ctx != nullptr)
3622     EVP_PKEY_CTX_free(ctx);
3623
3624   return !fatal;
3625 }
3626
3627
3628 template <PublicKeyCipher::Operation operation,
3629           PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
3630           PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3631 void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3632   Environment* env = Environment::GetCurrent(args);
3633
3634   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
3635   char* kbuf = Buffer::Data(args[0]);
3636   ssize_t klen = Buffer::Length(args[0]);
3637
3638   THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
3639   char* buf = Buffer::Data(args[1]);
3640   ssize_t len = Buffer::Length(args[1]);
3641
3642   int padding = args[2]->Uint32Value();
3643
3644   String::Utf8Value passphrase(args[3]);
3645
3646   unsigned char* out_value = nullptr;
3647   size_t out_len = 0;
3648
3649   bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3650       kbuf,
3651       klen,
3652       args.Length() >= 3 && !args[2]->IsNull() ? *passphrase : nullptr,
3653       padding,
3654       reinterpret_cast<const unsigned char*>(buf),
3655       len,
3656       &out_value,
3657       &out_len);
3658
3659   if (out_len == 0 || !r) {
3660     delete[] out_value;
3661     out_value = nullptr;
3662     out_len = 0;
3663     if (!r) {
3664       return ThrowCryptoError(env,
3665         ERR_get_error());
3666     }
3667   }
3668
3669   args.GetReturnValue().Set(
3670       Buffer::New(env, reinterpret_cast<char*>(out_value), out_len));
3671   delete[] out_value;
3672 }
3673
3674
3675 void DiffieHellman::Initialize(Environment* env, Handle<Object> target) {
3676   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
3677
3678   const PropertyAttribute attributes =
3679       static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
3680
3681   t->InstanceTemplate()->SetInternalFieldCount(1);
3682
3683   env->SetProtoMethod(t, "generateKeys", GenerateKeys);
3684   env->SetProtoMethod(t, "computeSecret", ComputeSecret);
3685   env->SetProtoMethod(t, "getPrime", GetPrime);
3686   env->SetProtoMethod(t, "getGenerator", GetGenerator);
3687   env->SetProtoMethod(t, "getPublicKey", GetPublicKey);
3688   env->SetProtoMethod(t, "getPrivateKey", GetPrivateKey);
3689   env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
3690   env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
3691
3692   t->InstanceTemplate()->SetAccessor(env->verify_error_string(),
3693                                      DiffieHellman::VerifyErrorGetter,
3694                                      nullptr,
3695                                      Handle<Value>(),
3696                                      v8::DEFAULT,
3697                                      attributes);
3698
3699   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
3700               t->GetFunction());
3701
3702   Local<FunctionTemplate> t2 = env->NewFunctionTemplate(DiffieHellmanGroup);
3703   t2->InstanceTemplate()->SetInternalFieldCount(1);
3704
3705   env->SetProtoMethod(t2, "generateKeys", GenerateKeys);
3706   env->SetProtoMethod(t2, "computeSecret", ComputeSecret);
3707   env->SetProtoMethod(t2, "getPrime", GetPrime);
3708   env->SetProtoMethod(t2, "getGenerator", GetGenerator);
3709   env->SetProtoMethod(t2, "getPublicKey", GetPublicKey);
3710   env->SetProtoMethod(t2, "getPrivateKey", GetPrivateKey);
3711
3712   t2->InstanceTemplate()->SetAccessor(env->verify_error_string(),
3713                                       DiffieHellman::VerifyErrorGetter,
3714                                       nullptr,
3715                                       Handle<Value>(),
3716                                       v8::DEFAULT,
3717                                       attributes);
3718
3719   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
3720               t2->GetFunction());
3721 }
3722
3723
3724 bool DiffieHellman::Init(int primeLength, int g) {
3725   dh = DH_new();
3726   DH_generate_parameters_ex(dh, primeLength, g, 0);
3727   bool result = VerifyContext();
3728   if (!result)
3729     return false;
3730   initialised_ = true;
3731   return true;
3732 }
3733
3734
3735 bool DiffieHellman::Init(const char* p, int p_len, int g) {
3736   dh = DH_new();
3737   dh->p = BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, 0);
3738   dh->g = BN_new();
3739   if (!BN_set_word(dh->g, g))
3740     return false;
3741   bool result = VerifyContext();
3742   if (!result)
3743     return false;
3744   initialised_ = true;
3745   return true;
3746 }
3747
3748
3749 bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
3750   dh = DH_new();
3751   dh->p = BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, 0);
3752   dh->g = BN_bin2bn(reinterpret_cast<const unsigned char*>(g), g_len, 0);
3753   bool result = VerifyContext();
3754   if (!result)
3755     return false;
3756   initialised_ = true;
3757   return true;
3758 }
3759
3760
3761 void DiffieHellman::DiffieHellmanGroup(
3762     const FunctionCallbackInfo<Value>& args) {
3763   Environment* env = Environment::GetCurrent(args);
3764   DiffieHellman* diffieHellman = new DiffieHellman(env, args.This());
3765
3766   if (args.Length() != 1 || !args[0]->IsString()) {
3767     return env->ThrowError("No group name given");
3768   }
3769
3770   bool initialized = false;
3771
3772   const node::Utf8Value group_name(env->isolate(), args[0]);
3773   for (unsigned int i = 0; i < ARRAY_SIZE(modp_groups); ++i) {
3774     const modp_group* it = modp_groups + i;
3775
3776     if (strcasecmp(*group_name, it->name) != 0)
3777       continue;
3778
3779     initialized = diffieHellman->Init(it->prime,
3780                                       it->prime_size,
3781                                       it->gen,
3782                                       it->gen_size);
3783     if (!initialized)
3784       env->ThrowError("Initialization failed");
3785     return;
3786   }
3787
3788   env->ThrowError("Unknown group");
3789 }
3790
3791
3792 void DiffieHellman::New(const FunctionCallbackInfo<Value>& args) {
3793   Environment* env = Environment::GetCurrent(args);
3794   DiffieHellman* diffieHellman =
3795       new DiffieHellman(env, args.This());
3796   bool initialized = false;
3797
3798   if (args.Length() == 2) {
3799     if (args[0]->IsInt32()) {
3800       if (args[1]->IsInt32()) {
3801         initialized = diffieHellman->Init(args[0]->Int32Value(),
3802                                           args[1]->Int32Value());
3803       }
3804     } else {
3805       if (args[1]->IsInt32()) {
3806         initialized = diffieHellman->Init(Buffer::Data(args[0]),
3807                                           Buffer::Length(args[0]),
3808                                           args[1]->Int32Value());
3809       } else {
3810         initialized = diffieHellman->Init(Buffer::Data(args[0]),
3811                                           Buffer::Length(args[0]),
3812                                           Buffer::Data(args[1]),
3813                                           Buffer::Length(args[1]));
3814       }
3815     }
3816   }
3817
3818   if (!initialized) {
3819     return env->ThrowError("Initialization failed");
3820   }
3821 }
3822
3823
3824 void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
3825   Environment* env = Environment::GetCurrent(args);
3826
3827   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3828
3829   if (!diffieHellman->initialised_) {
3830     return env->ThrowError("Not initialized");
3831   }
3832
3833   if (!DH_generate_key(diffieHellman->dh)) {
3834     return env->ThrowError("Key generation failed");
3835   }
3836
3837   int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
3838   char* data = new char[dataSize];
3839   BN_bn2bin(diffieHellman->dh->pub_key,
3840             reinterpret_cast<unsigned char*>(data));
3841
3842   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3843   delete[] data;
3844 }
3845
3846
3847 void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) {
3848   Environment* env = Environment::GetCurrent(args);
3849
3850   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3851
3852   if (!diffieHellman->initialised_) {
3853     return env->ThrowError("Not initialized");
3854   }
3855
3856   int dataSize = BN_num_bytes(diffieHellman->dh->p);
3857   char* data = new char[dataSize];
3858   BN_bn2bin(diffieHellman->dh->p, reinterpret_cast<unsigned char*>(data));
3859
3860   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3861   delete[] data;
3862 }
3863
3864
3865 void DiffieHellman::GetGenerator(const FunctionCallbackInfo<Value>& args) {
3866   Environment* env = Environment::GetCurrent(args);
3867
3868   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3869
3870   if (!diffieHellman->initialised_) {
3871     return env->ThrowError("Not initialized");
3872   }
3873
3874   int dataSize = BN_num_bytes(diffieHellman->dh->g);
3875   char* data = new char[dataSize];
3876   BN_bn2bin(diffieHellman->dh->g, reinterpret_cast<unsigned char*>(data));
3877
3878   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3879   delete[] data;
3880 }
3881
3882
3883 void DiffieHellman::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
3884   Environment* env = Environment::GetCurrent(args);
3885
3886   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3887
3888   if (!diffieHellman->initialised_) {
3889     return env->ThrowError("Not initialized");
3890   }
3891
3892   if (diffieHellman->dh->pub_key == nullptr) {
3893     return env->ThrowError("No public key - did you forget to generate one?");
3894   }
3895
3896   int dataSize = BN_num_bytes(diffieHellman->dh->pub_key);
3897   char* data = new char[dataSize];
3898   BN_bn2bin(diffieHellman->dh->pub_key,
3899             reinterpret_cast<unsigned char*>(data));
3900
3901   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3902   delete[] data;
3903 }
3904
3905
3906 void DiffieHellman::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
3907   Environment* env = Environment::GetCurrent(args);
3908
3909   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3910
3911   if (!diffieHellman->initialised_) {
3912     return env->ThrowError("Not initialized");
3913   }
3914
3915   if (diffieHellman->dh->priv_key == nullptr) {
3916     return env->ThrowError("No private key - did you forget to generate one?");
3917   }
3918
3919   int dataSize = BN_num_bytes(diffieHellman->dh->priv_key);
3920   char* data = new char[dataSize];
3921   BN_bn2bin(diffieHellman->dh->priv_key,
3922             reinterpret_cast<unsigned char*>(data));
3923
3924   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3925   delete[] data;
3926 }
3927
3928
3929 void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
3930   Environment* env = Environment::GetCurrent(args);
3931
3932   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
3933
3934   if (!diffieHellman->initialised_) {
3935     return env->ThrowError("Not initialized");
3936   }
3937
3938   ClearErrorOnReturn clear_error_on_return;
3939   (void) &clear_error_on_return;  // Silence compiler warning.
3940   BIGNUM* key = nullptr;
3941
3942   if (args.Length() == 0) {
3943     return env->ThrowError("First argument must be other party's public key");
3944   } else {
3945     THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
3946     key = BN_bin2bn(
3947         reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3948         Buffer::Length(args[0]),
3949         0);
3950   }
3951
3952   int dataSize = DH_size(diffieHellman->dh);
3953   char* data = new char[dataSize];
3954
3955   int size = DH_compute_key(reinterpret_cast<unsigned char*>(data),
3956                             key,
3957                             diffieHellman->dh);
3958
3959   if (size == -1) {
3960     int checkResult;
3961     int checked;
3962
3963     checked = DH_check_pub_key(diffieHellman->dh, key, &checkResult);
3964     BN_free(key);
3965     delete[] data;
3966
3967     if (!checked) {
3968       return env->ThrowError("Invalid key");
3969     } else if (checkResult) {
3970       if (checkResult & DH_CHECK_PUBKEY_TOO_SMALL) {
3971         return env->ThrowError("Supplied key is too small");
3972       } else if (checkResult & DH_CHECK_PUBKEY_TOO_LARGE) {
3973         return env->ThrowError("Supplied key is too large");
3974       } else {
3975         return env->ThrowError("Invalid key");
3976       }
3977     } else {
3978       return env->ThrowError("Invalid key");
3979     }
3980   }
3981
3982   BN_free(key);
3983   CHECK_GE(size, 0);
3984
3985   // DH_size returns number of bytes in a prime number
3986   // DH_compute_key returns number of bytes in a remainder of exponent, which
3987   // may have less bytes than a prime number. Therefore add 0-padding to the
3988   // allocated buffer.
3989   if (size != dataSize) {
3990     CHECK(dataSize > size);
3991     memmove(data + dataSize - size, data, size);
3992     memset(data, 0, dataSize - size);
3993   }
3994
3995   args.GetReturnValue().Set(Encode(env->isolate(), data, dataSize, BUFFER));
3996   delete[] data;
3997 }
3998
3999
4000 void DiffieHellman::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
4001   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
4002   Environment* env = diffieHellman->env();
4003
4004   if (!diffieHellman->initialised_) {
4005     return env->ThrowError("Not initialized");
4006   }
4007
4008   if (args.Length() == 0) {
4009     return env->ThrowError("First argument must be public key");
4010   } else {
4011     THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4012     diffieHellman->dh->pub_key = BN_bin2bn(
4013         reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
4014         Buffer::Length(args[0]), 0);
4015   }
4016 }
4017
4018
4019 void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4020   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
4021   Environment* env = diffieHellman->env();
4022
4023   if (!diffieHellman->initialised_) {
4024     return env->ThrowError("Not initialized");
4025   }
4026
4027   if (args.Length() == 0) {
4028     return env->ThrowError("First argument must be private key");
4029   } else {
4030     THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4031     diffieHellman->dh->priv_key = BN_bin2bn(
4032         reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
4033         Buffer::Length(args[0]),
4034         0);
4035   }
4036 }
4037
4038
4039 void DiffieHellman::VerifyErrorGetter(Local<String> property,
4040                                       const PropertyCallbackInfo<Value>& args) {
4041   HandleScope scope(args.GetIsolate());
4042
4043   DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.Holder());
4044
4045   if (!diffieHellman->initialised_)
4046     return diffieHellman->env()->ThrowError("Not initialized");
4047
4048   args.GetReturnValue().Set(diffieHellman->verifyError_);
4049 }
4050
4051
4052 bool DiffieHellman::VerifyContext() {
4053   int codes;
4054   if (!DH_check(dh, &codes))
4055     return false;
4056   verifyError_ = codes;
4057   return true;
4058 }
4059
4060
4061 void ECDH::Initialize(Environment* env, Handle<Object> target) {
4062   HandleScope scope(env->isolate());
4063
4064   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
4065
4066   t->InstanceTemplate()->SetInternalFieldCount(1);
4067
4068   env->SetProtoMethod(t, "generateKeys", GenerateKeys);
4069   env->SetProtoMethod(t, "computeSecret", ComputeSecret);
4070   env->SetProtoMethod(t, "getPublicKey", GetPublicKey);
4071   env->SetProtoMethod(t, "getPrivateKey", GetPrivateKey);
4072   env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
4073   env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
4074
4075   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"),
4076               t->GetFunction());
4077 }
4078
4079
4080 void ECDH::New(const FunctionCallbackInfo<Value>& args) {
4081   Environment* env = Environment::GetCurrent(args);
4082
4083   // TODO(indutny): Support raw curves?
4084   CHECK(args[0]->IsString());
4085   node::Utf8Value curve(env->isolate(), args[0]);
4086
4087   int nid = OBJ_sn2nid(*curve);
4088   if (nid == NID_undef)
4089     return env->ThrowTypeError("First argument should be a valid curve name");
4090
4091   EC_KEY* key = EC_KEY_new_by_curve_name(nid);
4092   if (key == nullptr)
4093     return env->ThrowError("Failed to create EC_KEY using curve name");
4094
4095   new ECDH(env, args.This(), key);
4096 }
4097
4098
4099 void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4100   Environment* env = Environment::GetCurrent(args);
4101
4102   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4103
4104   if (!EC_KEY_generate_key(ecdh->key_))
4105     return env->ThrowError("Failed to generate EC_KEY");
4106
4107   ecdh->generated_ = true;
4108 }
4109
4110
4111 EC_POINT* ECDH::BufferToPoint(char* data, size_t len) {
4112   EC_POINT* pub;
4113   int r;
4114
4115   pub = EC_POINT_new(group_);
4116   if (pub == nullptr) {
4117     env()->ThrowError("Failed to allocate EC_POINT for a public key");
4118     return nullptr;
4119   }
4120
4121   r = EC_POINT_oct2point(
4122       group_,
4123       pub,
4124       reinterpret_cast<unsigned char*>(data),
4125       len,
4126       nullptr);
4127   if (!r) {
4128     env()->ThrowError("Failed to translate Buffer to a EC_POINT");
4129     goto fatal;
4130   }
4131
4132   return pub;
4133
4134  fatal:
4135   EC_POINT_free(pub);
4136   return nullptr;
4137 }
4138
4139
4140 void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4141   Environment* env = Environment::GetCurrent(args);
4142
4143   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4144
4145   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4146
4147   EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0]),
4148                                       Buffer::Length(args[0]));
4149   if (pub == nullptr)
4150     return;
4151
4152   // NOTE: field_size is in bits
4153   int field_size = EC_GROUP_get_degree(ecdh->group_);
4154   size_t out_len = (field_size + 7) / 8;
4155   char* out = static_cast<char*>(malloc(out_len));
4156   CHECK_NE(out, nullptr);
4157
4158   int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr);
4159   EC_POINT_free(pub);
4160   if (!r) {
4161     free(out);
4162     return env->ThrowError("Failed to compute ECDH key");
4163   }
4164
4165   args.GetReturnValue().Set(Buffer::Use(env, out, out_len));
4166 }
4167
4168
4169 void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
4170   Environment* env = Environment::GetCurrent(args);
4171
4172   // Conversion form
4173   CHECK_EQ(args.Length(), 1);
4174
4175   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4176
4177   if (!ecdh->generated_)
4178     return env->ThrowError("You should generate ECDH keys first");
4179
4180   const EC_POINT* pub = EC_KEY_get0_public_key(ecdh->key_);
4181   if (pub == nullptr)
4182     return env->ThrowError("Failed to get ECDH public key");
4183
4184   int size;
4185   point_conversion_form_t form =
4186       static_cast<point_conversion_form_t>(args[0]->Uint32Value());
4187
4188   size = EC_POINT_point2oct(ecdh->group_, pub, form, nullptr, 0, nullptr);
4189   if (size == 0)
4190     return env->ThrowError("Failed to get public key length");
4191
4192   unsigned char* out = static_cast<unsigned char*>(malloc(size));
4193   CHECK_NE(out, nullptr);
4194
4195   int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
4196   if (r != size) {
4197     free(out);
4198     return env->ThrowError("Failed to get public key");
4199   }
4200
4201   args.GetReturnValue().Set(Buffer::Use(env,
4202                                         reinterpret_cast<char*>(out),
4203                                         size));
4204 }
4205
4206
4207 void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4208   Environment* env = Environment::GetCurrent(args);
4209
4210   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4211
4212   if (!ecdh->generated_)
4213     return env->ThrowError("You should generate ECDH keys first");
4214
4215   const BIGNUM* b = EC_KEY_get0_private_key(ecdh->key_);
4216   if (b == nullptr)
4217     return env->ThrowError("Failed to get ECDH private key");
4218
4219   int size = BN_num_bytes(b);
4220   unsigned char* out = static_cast<unsigned char*>(malloc(size));
4221   CHECK_NE(out, nullptr);
4222
4223   if (size != BN_bn2bin(b, out)) {
4224     free(out);
4225     return env->ThrowError("Failed to convert ECDH private key to Buffer");
4226   }
4227
4228   args.GetReturnValue().Set(Buffer::Use(env,
4229                                         reinterpret_cast<char*>(out),
4230                                         size));
4231 }
4232
4233
4234 void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4235   Environment* env = Environment::GetCurrent(args);
4236
4237   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4238
4239   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4240
4241   BIGNUM* priv = BN_bin2bn(
4242       reinterpret_cast<unsigned char*>(Buffer::Data(args[0].As<Object>())),
4243       Buffer::Length(args[0].As<Object>()),
4244       nullptr);
4245   if (priv == nullptr)
4246     return env->ThrowError("Failed to convert Buffer to BN");
4247
4248   if (!EC_KEY_set_private_key(ecdh->key_, priv))
4249     return env->ThrowError("Failed to convert BN to a private key");
4250 }
4251
4252
4253 void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
4254   Environment* env = Environment::GetCurrent(args);
4255
4256   ECDH* ecdh = Unwrap<ECDH>(args.Holder());
4257
4258   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4259
4260   EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0].As<Object>()),
4261                                       Buffer::Length(args[0].As<Object>()));
4262   if (pub == nullptr)
4263     return;
4264
4265   int r = EC_KEY_set_public_key(ecdh->key_, pub);
4266   EC_POINT_free(pub);
4267   if (!r)
4268     return env->ThrowError("Failed to convert BN to a private key");
4269 }
4270
4271
4272 class PBKDF2Request : public AsyncWrap {
4273  public:
4274   PBKDF2Request(Environment* env,
4275                 Local<Object> object,
4276                 const EVP_MD* digest,
4277                 ssize_t passlen,
4278                 char* pass,
4279                 ssize_t saltlen,
4280                 char* salt,
4281                 ssize_t iter,
4282                 ssize_t keylen)
4283       : AsyncWrap(env, object, AsyncWrap::PROVIDER_CRYPTO),
4284         digest_(digest),
4285         error_(0),
4286         passlen_(passlen),
4287         pass_(pass),
4288         saltlen_(saltlen),
4289         salt_(salt),
4290         keylen_(keylen),
4291         key_(static_cast<char*>(malloc(keylen))),
4292         iter_(iter) {
4293     if (key() == nullptr)
4294       FatalError("node::PBKDF2Request()", "Out of Memory");
4295   }
4296
4297   ~PBKDF2Request() override {
4298     persistent().Reset();
4299   }
4300
4301   uv_work_t* work_req() {
4302     return &work_req_;
4303   }
4304
4305   inline const EVP_MD* digest() const {
4306     return digest_;
4307   }
4308
4309   inline ssize_t passlen() const {
4310     return passlen_;
4311   }
4312
4313   inline char* pass() const {
4314     return pass_;
4315   }
4316
4317   inline ssize_t saltlen() const {
4318     return saltlen_;
4319   }
4320
4321   inline char* salt() const {
4322     return salt_;
4323   }
4324
4325   inline ssize_t keylen() const {
4326     return keylen_;
4327   }
4328
4329   inline char* key() const {
4330     return key_;
4331   }
4332
4333   inline ssize_t iter() const {
4334     return iter_;
4335   }
4336
4337   inline void release() {
4338     free(pass_);
4339     passlen_ = 0;
4340     free(salt_);
4341     saltlen_ = 0;
4342     free(key_);
4343     keylen_ = 0;
4344   }
4345
4346   inline int error() const {
4347     return error_;
4348   }
4349
4350   inline void set_error(int err) {
4351     error_ = err;
4352   }
4353
4354   uv_work_t work_req_;
4355
4356  private:
4357   const EVP_MD* digest_;
4358   int error_;
4359   ssize_t passlen_;
4360   char* pass_;
4361   ssize_t saltlen_;
4362   char* salt_;
4363   ssize_t keylen_;
4364   char* key_;
4365   ssize_t iter_;
4366 };
4367
4368
4369 void EIO_PBKDF2(PBKDF2Request* req) {
4370   req->set_error(PKCS5_PBKDF2_HMAC(
4371     req->pass(),
4372     req->passlen(),
4373     reinterpret_cast<unsigned char*>(req->salt()),
4374     req->saltlen(),
4375     req->iter(),
4376     req->digest(),
4377     req->keylen(),
4378     reinterpret_cast<unsigned char*>(req->key())));
4379   memset(req->pass(), 0, req->passlen());
4380   memset(req->salt(), 0, req->saltlen());
4381 }
4382
4383
4384 void EIO_PBKDF2(uv_work_t* work_req) {
4385   PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
4386   EIO_PBKDF2(req);
4387 }
4388
4389
4390 void EIO_PBKDF2After(PBKDF2Request* req, Local<Value> argv[2]) {
4391   if (req->error()) {
4392     argv[0] = Undefined(req->env()->isolate());
4393     argv[1] = Encode(req->env()->isolate(), req->key(), req->keylen(), BUFFER);
4394     memset(req->key(), 0, req->keylen());
4395   } else {
4396     argv[0] = Exception::Error(req->env()->pbkdf2_error_string());
4397     argv[1] = Undefined(req->env()->isolate());
4398   }
4399 }
4400
4401
4402 void EIO_PBKDF2After(uv_work_t* work_req, int status) {
4403   CHECK_EQ(status, 0);
4404   PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
4405   Environment* env = req->env();
4406   HandleScope handle_scope(env->isolate());
4407   Context::Scope context_scope(env->context());
4408   Local<Value> argv[2];
4409   EIO_PBKDF2After(req, argv);
4410   req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
4411   req->release();
4412   delete req;
4413 }
4414
4415
4416 void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4417   Environment* env = Environment::GetCurrent(args);
4418
4419   const EVP_MD* digest = nullptr;
4420   const char* type_error = nullptr;
4421   char* pass = nullptr;
4422   char* salt = nullptr;
4423   ssize_t passlen = -1;
4424   ssize_t saltlen = -1;
4425   ssize_t keylen = -1;
4426   ssize_t iter = -1;
4427   PBKDF2Request* req = nullptr;
4428   Local<Object> obj;
4429
4430   if (args.Length() != 5 && args.Length() != 6) {
4431     type_error = "Bad parameter";
4432     goto err;
4433   }
4434
4435   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4436   passlen = Buffer::Length(args[0]);
4437   if (passlen < 0) {
4438     type_error = "Bad password";
4439     goto err;
4440   }
4441
4442   THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
4443
4444   pass = static_cast<char*>(malloc(passlen));
4445   if (pass == nullptr) {
4446     FatalError("node::PBKDF2()", "Out of Memory");
4447   }
4448   memcpy(pass, Buffer::Data(args[0]), passlen);
4449
4450   saltlen = Buffer::Length(args[1]);
4451   if (saltlen < 0) {
4452     type_error = "Bad salt";
4453     goto err;
4454   }
4455
4456   salt = static_cast<char*>(malloc(saltlen));
4457   if (salt == nullptr) {
4458     FatalError("node::PBKDF2()", "Out of Memory");
4459   }
4460   memcpy(salt, Buffer::Data(args[1]), saltlen);
4461
4462   if (!args[2]->IsNumber()) {
4463     type_error = "Iterations not a number";
4464     goto err;
4465   }
4466
4467   iter = args[2]->Int32Value();
4468   if (iter < 0) {
4469     type_error = "Bad iterations";
4470     goto err;
4471   }
4472
4473   if (!args[3]->IsNumber()) {
4474     type_error = "Key length not a number";
4475     goto err;
4476   }
4477
4478   keylen = args[3]->Int32Value();
4479   if (keylen < 0) {
4480     type_error = "Bad key length";
4481     goto err;
4482   }
4483
4484   if (args[4]->IsString()) {
4485     node::Utf8Value digest_name(env->isolate(), args[4]);
4486     digest = EVP_get_digestbyname(*digest_name);
4487     if (digest == nullptr) {
4488       type_error = "Bad digest name";
4489       goto err;
4490     }
4491   }
4492
4493   if (digest == nullptr) {
4494     digest = EVP_sha1();
4495   }
4496
4497   obj = Object::New(env->isolate());
4498   req = new PBKDF2Request(env,
4499                           obj,
4500                           digest,
4501                           passlen,
4502                           pass,
4503                           saltlen,
4504                           salt,
4505                           iter,
4506                           keylen);
4507
4508   if (args[5]->IsFunction()) {
4509     obj->Set(env->ondone_string(), args[5]);
4510     // XXX(trevnorris): This will need to go with the rest of domains.
4511     if (env->in_domain())
4512       obj->Set(env->domain_string(), env->domain_array()->Get(0));
4513     uv_queue_work(env->event_loop(),
4514                   req->work_req(),
4515                   EIO_PBKDF2,
4516                   EIO_PBKDF2After);
4517   } else {
4518     Local<Value> argv[2];
4519     EIO_PBKDF2(req);
4520     EIO_PBKDF2After(req, argv);
4521     if (argv[0]->IsObject())
4522       env->isolate()->ThrowException(argv[0]);
4523     else
4524       args.GetReturnValue().Set(argv[1]);
4525   }
4526   return;
4527
4528  err:
4529   free(salt);
4530   free(pass);
4531   return env->ThrowTypeError(type_error);
4532 }
4533
4534
4535 // Only instantiate within a valid HandleScope.
4536 class RandomBytesRequest : public AsyncWrap {
4537  public:
4538   RandomBytesRequest(Environment* env, Local<Object> object, size_t size)
4539       : AsyncWrap(env, object, AsyncWrap::PROVIDER_CRYPTO),
4540         error_(0),
4541         size_(size),
4542         data_(static_cast<char*>(malloc(size))) {
4543     if (data() == nullptr)
4544       FatalError("node::RandomBytesRequest()", "Out of Memory");
4545   }
4546
4547   ~RandomBytesRequest() override {
4548     persistent().Reset();
4549   }
4550
4551   uv_work_t* work_req() {
4552     return &work_req_;
4553   }
4554
4555   inline size_t size() const {
4556     return size_;
4557   }
4558
4559   inline char* data() const {
4560     return data_;
4561   }
4562
4563   inline void release() {
4564     free(data_);
4565     size_ = 0;
4566   }
4567
4568   inline void return_memory(char** d, size_t* len) {
4569     *d = data_;
4570     data_ = nullptr;
4571     *len = size_;
4572     size_ = 0;
4573   }
4574
4575   inline unsigned long error() const {
4576     return error_;
4577   }
4578
4579   inline void set_error(unsigned long err) {
4580     error_ = err;
4581   }
4582
4583   uv_work_t work_req_;
4584
4585  private:
4586   unsigned long error_;
4587   size_t size_;
4588   char* data_;
4589 };
4590
4591
4592 template <bool pseudoRandom>
4593 void RandomBytesWork(uv_work_t* work_req) {
4594   RandomBytesRequest* req =
4595       ContainerOf(&RandomBytesRequest::work_req_, work_req);
4596   int r;
4597
4598   // Ensure that OpenSSL's PRNG is properly seeded.
4599   CheckEntropy();
4600
4601   if (pseudoRandom == true) {
4602     r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data()),
4603                           req->size());
4604   } else {
4605     r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data()), req->size());
4606   }
4607
4608   // RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
4609   // result is not cryptographically strong - but that's not an error.
4610   if (r == 0 && pseudoRandom == false) {
4611     req->set_error(ERR_get_error());
4612   } else if (r == -1) {
4613     req->set_error(static_cast<unsigned long>(-1));
4614   }
4615 }
4616
4617
4618 // don't call this function without a valid HandleScope
4619 void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
4620   if (req->error()) {
4621     char errmsg[256] = "Operation not supported";
4622
4623     if (req->error() != static_cast<unsigned long>(-1))
4624       ERR_error_string_n(req->error(), errmsg, sizeof errmsg);
4625
4626     argv[0] = Exception::Error(OneByteString(req->env()->isolate(), errmsg));
4627     argv[1] = Null(req->env()->isolate());
4628     req->release();
4629   } else {
4630     char* data = nullptr;
4631     size_t size;
4632     req->return_memory(&data, &size);
4633     argv[0] = Null(req->env()->isolate());
4634     argv[1] = Buffer::Use(data, size);
4635   }
4636 }
4637
4638
4639 void RandomBytesAfter(uv_work_t* work_req, int status) {
4640   CHECK_EQ(status, 0);
4641   RandomBytesRequest* req =
4642       ContainerOf(&RandomBytesRequest::work_req_, work_req);
4643   Environment* env = req->env();
4644   HandleScope handle_scope(env->isolate());
4645   Context::Scope context_scope(env->context());
4646   Local<Value> argv[2];
4647   RandomBytesCheck(req, argv);
4648   req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
4649   delete req;
4650 }
4651
4652
4653 template <bool pseudoRandom>
4654 void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4655   Environment* env = Environment::GetCurrent(args);
4656
4657   // maybe allow a buffer to write to? cuts down on object creation
4658   // when generating random data in a loop
4659   if (!args[0]->IsUint32()) {
4660     return env->ThrowTypeError("size must be a number >= 0");
4661   }
4662
4663   const uint32_t size = args[0]->Uint32Value();
4664   if (size > Buffer::kMaxLength) {
4665     return env->ThrowTypeError("size > Buffer::kMaxLength");
4666   }
4667
4668   Local<Object> obj = Object::New(env->isolate());
4669   RandomBytesRequest* req = new RandomBytesRequest(env, obj, size);
4670
4671   if (args[1]->IsFunction()) {
4672     obj->Set(FIXED_ONE_BYTE_STRING(args.GetIsolate(), "ondone"), args[1]);
4673     // XXX(trevnorris): This will need to go with the rest of domains.
4674     if (env->in_domain())
4675       obj->Set(env->domain_string(), env->domain_array()->Get(0));
4676     uv_queue_work(env->event_loop(),
4677                   req->work_req(),
4678                   RandomBytesWork<pseudoRandom>,
4679                   RandomBytesAfter);
4680     args.GetReturnValue().Set(obj);
4681   } else {
4682     Local<Value> argv[2];
4683     RandomBytesWork<pseudoRandom>(req->work_req());
4684     RandomBytesCheck(req, argv);
4685     delete req;
4686
4687     if (!argv[0]->IsNull())
4688       env->isolate()->ThrowException(argv[0]);
4689     else
4690       args.GetReturnValue().Set(argv[1]);
4691   }
4692 }
4693
4694
4695 void GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
4696   Environment* env = Environment::GetCurrent(args);
4697
4698   SSL_CTX* ctx = SSL_CTX_new(TLSv1_server_method());
4699   if (ctx == nullptr) {
4700     return env->ThrowError("SSL_CTX_new() failed.");
4701   }
4702
4703   SSL* ssl = SSL_new(ctx);
4704   if (ssl == nullptr) {
4705     SSL_CTX_free(ctx);
4706     return env->ThrowError("SSL_new() failed.");
4707   }
4708
4709   Local<Array> arr = Array::New(env->isolate());
4710   STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl);
4711
4712   for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
4713     SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
4714     arr->Set(i, OneByteString(args.GetIsolate(), SSL_CIPHER_get_name(cipher)));
4715   }
4716
4717   SSL_free(ssl);
4718   SSL_CTX_free(ctx);
4719
4720   args.GetReturnValue().Set(arr);
4721 }
4722
4723
4724 class CipherPushContext {
4725  public:
4726   explicit CipherPushContext(Environment* env)
4727       : arr(Array::New(env->isolate())),
4728         env_(env) {
4729   }
4730
4731   inline Environment* env() const { return env_; }
4732
4733   Local<Array> arr;
4734
4735  private:
4736   Environment* env_;
4737 };
4738
4739
4740 template <class TypeName>
4741 static void array_push_back(const TypeName* md,
4742                             const char* from,
4743                             const char* to,
4744                             void* arg) {
4745   CipherPushContext* ctx = static_cast<CipherPushContext*>(arg);
4746   ctx->arr->Set(ctx->arr->Length(), OneByteString(ctx->env()->isolate(), from));
4747 }
4748
4749
4750 void GetCiphers(const FunctionCallbackInfo<Value>& args) {
4751   Environment* env = Environment::GetCurrent(args);
4752   CipherPushContext ctx(env);
4753   EVP_CIPHER_do_all_sorted(array_push_back<EVP_CIPHER>, &ctx);
4754   args.GetReturnValue().Set(ctx.arr);
4755 }
4756
4757
4758 void GetHashes(const FunctionCallbackInfo<Value>& args) {
4759   Environment* env = Environment::GetCurrent(args);
4760   CipherPushContext ctx(env);
4761   EVP_MD_do_all_sorted(array_push_back<EVP_MD>, &ctx);
4762   args.GetReturnValue().Set(ctx.arr);
4763 }
4764
4765
4766 void Certificate::Initialize(Environment* env, Handle<Object> target) {
4767   HandleScope scope(env->isolate());
4768
4769   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
4770
4771   t->InstanceTemplate()->SetInternalFieldCount(1);
4772
4773   env->SetProtoMethod(t, "verifySpkac", VerifySpkac);
4774   env->SetProtoMethod(t, "exportPublicKey", ExportPublicKey);
4775   env->SetProtoMethod(t, "exportChallenge", ExportChallenge);
4776
4777   target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Certificate"),
4778               t->GetFunction());
4779 }
4780
4781
4782 void Certificate::New(const FunctionCallbackInfo<Value>& args) {
4783   Environment* env = Environment::GetCurrent(args);
4784   new Certificate(env, args.This());
4785 }
4786
4787
4788 bool Certificate::VerifySpkac(const char* data, unsigned int len) {
4789   bool i = 0;
4790   EVP_PKEY* pkey = nullptr;
4791   NETSCAPE_SPKI* spki = nullptr;
4792
4793   spki = NETSCAPE_SPKI_b64_decode(data, len);
4794   if (spki == nullptr)
4795     goto exit;
4796
4797   pkey = X509_PUBKEY_get(spki->spkac->pubkey);
4798   if (pkey == nullptr)
4799     goto exit;
4800
4801   i = NETSCAPE_SPKI_verify(spki, pkey) > 0;
4802
4803  exit:
4804   if (pkey != nullptr)
4805     EVP_PKEY_free(pkey);
4806
4807   if (spki != nullptr)
4808     NETSCAPE_SPKI_free(spki);
4809
4810   return i;
4811 }
4812
4813
4814 void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4815   Certificate* certificate = Unwrap<Certificate>(args.Holder());
4816   Environment* env = certificate->env();
4817   bool i = false;
4818
4819   if (args.Length() < 1)
4820     return env->ThrowTypeError("Missing argument");
4821
4822   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4823
4824   size_t length = Buffer::Length(args[0]);
4825   if (length == 0)
4826     return args.GetReturnValue().Set(i);
4827
4828   char* data = Buffer::Data(args[0]);
4829   CHECK_NE(data, nullptr);
4830
4831   i = certificate->VerifySpkac(data, length);
4832
4833   args.GetReturnValue().Set(i);
4834 }
4835
4836
4837 const char* Certificate::ExportPublicKey(const char* data, int len) {
4838   char* buf = nullptr;
4839   EVP_PKEY* pkey = nullptr;
4840   NETSCAPE_SPKI* spki = nullptr;
4841
4842   BIO* bio = BIO_new(BIO_s_mem());
4843   if (bio == nullptr)
4844     goto exit;
4845
4846   spki = NETSCAPE_SPKI_b64_decode(data, len);
4847   if (spki == nullptr)
4848     goto exit;
4849
4850   pkey = NETSCAPE_SPKI_get_pubkey(spki);
4851   if (pkey == nullptr)
4852     goto exit;
4853
4854   if (PEM_write_bio_PUBKEY(bio, pkey) <= 0)
4855     goto exit;
4856
4857   BIO_write(bio, "\0", 1);
4858   BUF_MEM* ptr;
4859   BIO_get_mem_ptr(bio, &ptr);
4860
4861   buf = new char[ptr->length];
4862   memcpy(buf, ptr->data, ptr->length);
4863
4864  exit:
4865   if (pkey != nullptr)
4866     EVP_PKEY_free(pkey);
4867
4868   if (spki != nullptr)
4869     NETSCAPE_SPKI_free(spki);
4870
4871   if (bio != nullptr)
4872     BIO_free_all(bio);
4873
4874   return buf;
4875 }
4876
4877
4878 void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
4879   Environment* env = Environment::GetCurrent(args);
4880
4881   Certificate* certificate = Unwrap<Certificate>(args.Holder());
4882
4883   if (args.Length() < 1)
4884     return env->ThrowTypeError("Missing argument");
4885
4886   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4887
4888   size_t length = Buffer::Length(args[0]);
4889   if (length == 0)
4890     return args.GetReturnValue().SetEmptyString();
4891
4892   char* data = Buffer::Data(args[0]);
4893   CHECK_NE(data, nullptr);
4894
4895   const char* pkey = certificate->ExportPublicKey(data, length);
4896   if (pkey == nullptr)
4897     return args.GetReturnValue().SetEmptyString();
4898
4899   Local<Value> out = Encode(env->isolate(), pkey, strlen(pkey), BUFFER);
4900
4901   delete[] pkey;
4902
4903   args.GetReturnValue().Set(out);
4904 }
4905
4906
4907 const char* Certificate::ExportChallenge(const char* data, int len) {
4908   NETSCAPE_SPKI* sp = nullptr;
4909
4910   sp = NETSCAPE_SPKI_b64_decode(data, len);
4911   if (sp == nullptr)
4912     return nullptr;
4913
4914   const char* buf = nullptr;
4915   buf = reinterpret_cast<const char*>(ASN1_STRING_data(sp->spkac->challenge));
4916
4917   return buf;
4918 }
4919
4920
4921 void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
4922   Environment* env = Environment::GetCurrent(args);
4923
4924   Certificate* crt = Unwrap<Certificate>(args.Holder());
4925
4926   if (args.Length() < 1)
4927     return env->ThrowTypeError("Missing argument");
4928
4929   THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
4930
4931   size_t len = Buffer::Length(args[0]);
4932   if (len == 0)
4933     return args.GetReturnValue().SetEmptyString();
4934
4935   char* data = Buffer::Data(args[0]);
4936   CHECK_NE(data, nullptr);
4937
4938   const char* cert = crt->ExportChallenge(data, len);
4939   if (cert == nullptr)
4940     return args.GetReturnValue().SetEmptyString();
4941
4942   Local<Value> outString = Encode(env->isolate(), cert, strlen(cert), BUFFER);
4943
4944   delete[] cert;
4945
4946   args.GetReturnValue().Set(outString);
4947 }
4948
4949
4950 void InitCryptoOnce() {
4951   SSL_library_init();
4952   OpenSSL_add_all_algorithms();
4953   SSL_load_error_strings();
4954
4955   crypto_lock_init();
4956   CRYPTO_set_locking_callback(crypto_lock_cb);
4957   CRYPTO_THREADID_set_callback(crypto_threadid_cb);
4958
4959   // Turn off compression. Saves memory and protects against CRIME attacks.
4960 #if !defined(OPENSSL_NO_COMP)
4961 #if OPENSSL_VERSION_NUMBER < 0x00908000L
4962   STACK_OF(SSL_COMP)* comp_methods = SSL_COMP_get_compression_method();
4963 #else
4964   STACK_OF(SSL_COMP)* comp_methods = SSL_COMP_get_compression_methods();
4965 #endif
4966   sk_SSL_COMP_zero(comp_methods);
4967   CHECK_EQ(sk_SSL_COMP_num(comp_methods), 0);
4968 #endif
4969
4970 #ifndef OPENSSL_NO_ENGINE
4971   ERR_load_ENGINE_strings();
4972   ENGINE_load_builtin_engines();
4973 #endif  // !OPENSSL_NO_ENGINE
4974 }
4975
4976
4977 #ifndef OPENSSL_NO_ENGINE
4978 void SetEngine(const FunctionCallbackInfo<Value>& args) {
4979   Environment* env = Environment::GetCurrent(args);
4980   CHECK(args.Length() >= 2 && args[0]->IsString());
4981   unsigned int flags = args[1]->Uint32Value();
4982
4983   ClearErrorOnReturn clear_error_on_return;
4984   (void) &clear_error_on_return;  // Silence compiler warning.
4985
4986   const node::Utf8Value engine_id(env->isolate(), args[0]);
4987   ENGINE* engine = ENGINE_by_id(*engine_id);
4988
4989   // Engine not found, try loading dynamically
4990   if (engine == nullptr) {
4991     engine = ENGINE_by_id("dynamic");
4992     if (engine != nullptr) {
4993       if (!ENGINE_ctrl_cmd_string(engine, "SO_PATH", *engine_id, 0) ||
4994           !ENGINE_ctrl_cmd_string(engine, "LOAD", nullptr, 0)) {
4995         ENGINE_free(engine);
4996         engine = nullptr;
4997       }
4998     }
4999   }
5000
5001   if (engine == nullptr) {
5002     int err = ERR_get_error();
5003     if (err == 0) {
5004       char tmp[1024];
5005       snprintf(tmp, sizeof(tmp), "Engine \"%s\" was not found", *engine_id);
5006       return env->ThrowError(tmp);
5007     } else {
5008       return ThrowCryptoError(env, err);
5009     }
5010   }
5011
5012   int r = ENGINE_set_default(engine, flags);
5013   ENGINE_free(engine);
5014   if (r == 0)
5015     return ThrowCryptoError(env, ERR_get_error());
5016 }
5017 #endif  // !OPENSSL_NO_ENGINE
5018
5019
5020 // FIXME(bnoordhuis) Handle global init correctly.
5021 void InitCrypto(Handle<Object> target,
5022                 Handle<Value> unused,
5023                 Handle<Context> context,
5024                 void* priv) {
5025   static uv_once_t init_once = UV_ONCE_INIT;
5026   uv_once(&init_once, InitCryptoOnce);
5027
5028   Environment* env = Environment::GetCurrent(context);
5029   SecureContext::Initialize(env, target);
5030   Connection::Initialize(env, target);
5031   CipherBase::Initialize(env, target);
5032   DiffieHellman::Initialize(env, target);
5033   ECDH::Initialize(env, target);
5034   Hmac::Initialize(env, target);
5035   Hash::Initialize(env, target);
5036   Sign::Initialize(env, target);
5037   Verify::Initialize(env, target);
5038   Certificate::Initialize(env, target);
5039
5040 #ifndef OPENSSL_NO_ENGINE
5041   env->SetMethod(target, "setEngine", SetEngine);
5042 #endif  // !OPENSSL_NO_ENGINE
5043   env->SetMethod(target, "PBKDF2", PBKDF2);
5044   env->SetMethod(target, "randomBytes", RandomBytes<false>);
5045   env->SetMethod(target, "pseudoRandomBytes", RandomBytes<true>);
5046   env->SetMethod(target, "getSSLCiphers", GetSSLCiphers);
5047   env->SetMethod(target, "getCiphers", GetCiphers);
5048   env->SetMethod(target, "getHashes", GetHashes);
5049   env->SetMethod(target, "publicEncrypt",
5050                  PublicKeyCipher::Cipher<PublicKeyCipher::kEncrypt,
5051                                          EVP_PKEY_encrypt_init,
5052                                          EVP_PKEY_encrypt>);
5053   env->SetMethod(target, "privateDecrypt",
5054                  PublicKeyCipher::Cipher<PublicKeyCipher::kDecrypt,
5055                                          EVP_PKEY_decrypt_init,
5056                                          EVP_PKEY_decrypt>);
5057 }
5058
5059 }  // namespace crypto
5060 }  // namespace node
5061
5062 NODE_MODULE_CONTEXT_AWARE_BUILTIN(crypto, node::crypto::InitCrypto)