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