fix_handle_boringssl_and_openssl_incompatibilities.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Wed, 12 Feb 2020 15:08:04 -0800
  4. Subject: fix: handle BoringSSL and OpenSSL incompatibilities
  5. This patch corrects for imcompatibilities between OpenSSL, which Node.js uses,
  6. and BoringSSL which Electron uses via Chromium. Each incompatibility typically has
  7. ~2 paths forward:
  8. * Upstream a shim or adapted implementation to BoringSSL
  9. * Alter Node.js functionality to something which both libraries can handle.
  10. Where possible, we should seek to make this patch as minimal as possible.
  11. Upstreams:
  12. - https://github.com/nodejs/node/pull/39054
  13. - https://github.com/nodejs/node/pull/39138
  14. - https://github.com/nodejs/node/pull/39136
  15. diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc
  16. index 2e6e02d229b67bc97e0d15a2704e091d5289df9c..6fd0416820998bd0ba4c4cd4fe3093f144610f18 100644
  17. --- a/src/crypto/crypto_cipher.cc
  18. +++ b/src/crypto/crypto_cipher.cc
  19. @@ -27,7 +27,8 @@ using v8::Value;
  20. namespace crypto {
  21. namespace {
  22. bool IsSupportedAuthenticatedMode(const EVP_CIPHER* cipher) {
  23. - switch (EVP_CIPHER_mode(cipher)) {
  24. + const int mode = EVP_CIPHER_mode(cipher);
  25. + switch (mode) {
  26. case EVP_CIPH_CCM_MODE:
  27. case EVP_CIPH_GCM_MODE:
  28. #ifndef OPENSSL_NO_OCB
  29. diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc
  30. index c6120a655ec853aef11c66ed37d7ca0ffb957dd3..a52ca15cb0ab592d4196d4bd0f1133240967d70c 100644
  31. --- a/src/crypto/crypto_common.cc
  32. +++ b/src/crypto/crypto_common.cc
  33. @@ -158,7 +158,7 @@ const char* GetClientHelloALPN(const SSLPointer& ssl) {
  34. const unsigned char* buf;
  35. size_t len;
  36. size_t rem;
  37. -
  38. +#ifndef OPENSSL_IS_BORINGSSL
  39. if (!SSL_client_hello_get0_ext(
  40. ssl.get(),
  41. TLSEXT_TYPE_application_layer_protocol_negotiation,
  42. @@ -171,13 +171,15 @@ const char* GetClientHelloALPN(const SSLPointer& ssl) {
  43. len = (buf[0] << 8) | buf[1];
  44. if (len + 2 != rem) return nullptr;
  45. return reinterpret_cast<const char*>(buf + 3);
  46. +#endif
  47. + return nullptr;
  48. }
  49. const char* GetClientHelloServerName(const SSLPointer& ssl) {
  50. const unsigned char* buf;
  51. size_t len;
  52. size_t rem;
  53. -
  54. +#ifndef OPENSSL_IS_BORINGSSL
  55. if (!SSL_client_hello_get0_ext(
  56. ssl.get(),
  57. TLSEXT_TYPE_server_name,
  58. @@ -199,15 +201,20 @@ const char* GetClientHelloServerName(const SSLPointer& ssl) {
  59. if (len + 2 > rem)
  60. return nullptr;
  61. return reinterpret_cast<const char*>(buf + 5);
  62. +#endif
  63. + return nullptr;
  64. }
  65. const char* GetServerName(SSL* ssl) {
  66. return SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  67. }
  68. -bool SetGroups(SecureContext* sc, const char* groups) {
  69. - return SSL_CTX_set1_groups_list(sc->ctx().get(), groups) == 1;
  70. -}
  71. + bool SetGroups(SecureContext* sc, const char* groups) {
  72. +#ifndef OPENSSL_IS_BORINGSSL
  73. + return SSL_CTX_set1_groups_list(sc->ctx().get(), groups) == 1;
  74. +#endif
  75. + return SSL_CTX_set1_curves_list(sc->ctx().get(), groups) == 1;
  76. + }
  77. const char* X509ErrorCode(long err) { // NOLINT(runtime/int)
  78. const char* code = "UNSPECIFIED";
  79. @@ -1044,14 +1051,14 @@ MaybeLocal<Array> GetClientHelloCiphers(
  80. Environment* env,
  81. const SSLPointer& ssl) {
  82. EscapableHandleScope scope(env->isolate());
  83. - const unsigned char* buf;
  84. - size_t len = SSL_client_hello_get0_ciphers(ssl.get(), &buf);
  85. + // const unsigned char* buf = nullptr;
  86. + size_t len = 0; // SSL_client_hello_get0_ciphers(ssl.get(), &buf);
  87. size_t count = len / 2;
  88. MaybeStackBuffer<Local<Value>, 16> ciphers(count);
  89. int j = 0;
  90. for (size_t n = 0; n < len; n += 2) {
  91. - const SSL_CIPHER* cipher = SSL_CIPHER_find(ssl.get(), buf);
  92. - buf += 2;
  93. + const SSL_CIPHER* cipher = nullptr; // SSL_CIPHER_find(ssl.get(), buf);
  94. + // buf += 2;
  95. Local<Object> obj = Object::New(env->isolate());
  96. if (!Set(env->context(),
  97. obj,
  98. diff --git a/src/crypto/crypto_context.cc b/src/crypto/crypto_context.cc
  99. index 3876adf7d72211d8d8c5a94564168094ecfc660b..ccd166d9439a821328d2ad35352488960206f65e 100644
  100. --- a/src/crypto/crypto_context.cc
  101. +++ b/src/crypto/crypto_context.cc
  102. @@ -63,7 +63,7 @@ inline X509_STORE* GetOrCreateRootCertStore() {
  103. // Caller responsible for BIO_free_all-ing the returned object.
  104. BIOPointer LoadBIO(Environment* env, Local<Value> v) {
  105. if (v->IsString() || v->IsArrayBufferView()) {
  106. - BIOPointer bio(BIO_new(BIO_s_secmem()));
  107. + BIOPointer bio(BIO_new(BIO_s_mem()));
  108. if (!bio) return nullptr;
  109. ByteSource bsrc = ByteSource::FromStringOrBuffer(env, v);
  110. if (bsrc.size() > INT_MAX) return nullptr;
  111. @@ -861,10 +861,12 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
  112. // If the user specified "auto" for dhparams, the JavaScript layer will pass
  113. // true to this function instead of the original string. Any other string
  114. // value will be interpreted as custom DH parameters below.
  115. +#ifndef OPENSSL_IS_BORINGSSL
  116. if (args[0]->IsTrue()) {
  117. CHECK(SSL_CTX_set_dh_auto(sc->ctx_.get(), true));
  118. return;
  119. }
  120. +#endif
  121. DHPointer dh;
  122. {
  123. diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc
  124. index 408d6be2a9cfdbcb52af285204c07c40bf74a5a9..9c3244a14bc286d60805bac5719df6595d802768 100644
  125. --- a/src/crypto/crypto_dh.cc
  126. +++ b/src/crypto/crypto_dh.cc
  127. @@ -153,13 +153,11 @@ bool DiffieHellman::Init(BignumPointer&& bn_p, int g) {
  128. bool DiffieHellman::Init(const char* p, int p_len, int g) {
  129. dh_.reset(DH_new());
  130. if (p_len <= 0) {
  131. - ERR_put_error(ERR_LIB_BN, BN_F_BN_GENERATE_PRIME_EX,
  132. - BN_R_BITS_TOO_SMALL, __FILE__, __LINE__);
  133. + OPENSSL_PUT_ERROR(BN, BN_R_BITS_TOO_SMALL);
  134. return false;
  135. }
  136. if (g <= 1) {
  137. - ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS,
  138. - DH_R_BAD_GENERATOR, __FILE__, __LINE__);
  139. + OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
  140. return false;
  141. }
  142. BIGNUM* bn_p =
  143. @@ -177,21 +175,18 @@ bool DiffieHellman::Init(const char* p, int p_len, int g) {
  144. bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
  145. dh_.reset(DH_new());
  146. if (p_len <= 0) {
  147. - ERR_put_error(ERR_LIB_BN, BN_F_BN_GENERATE_PRIME_EX,
  148. - BN_R_BITS_TOO_SMALL, __FILE__, __LINE__);
  149. + OPENSSL_PUT_ERROR(BN, BN_R_BITS_TOO_SMALL);
  150. return false;
  151. }
  152. if (g_len <= 0) {
  153. - ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS,
  154. - DH_R_BAD_GENERATOR, __FILE__, __LINE__);
  155. + OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
  156. return false;
  157. }
  158. BIGNUM* bn_g =
  159. BN_bin2bn(reinterpret_cast<const unsigned char*>(g), g_len, nullptr);
  160. if (BN_is_zero(bn_g) || BN_is_one(bn_g)) {
  161. BN_free(bn_g);
  162. - ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS,
  163. - DH_R_BAD_GENERATOR, __FILE__, __LINE__);
  164. + OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
  165. return false;
  166. }
  167. BIGNUM* bn_p =
  168. @@ -218,8 +213,10 @@ typedef BignumPointer (*StandardizedGroupInstantiator)();
  169. inline StandardizedGroupInstantiator FindDiffieHellmanGroup(const char* name) {
  170. #define V(n, p) \
  171. if (StringEqualNoCase(name, n)) return InstantiateStandardizedGroup<p>
  172. +#ifndef OPENSSL_IS_BORINGSSL
  173. V("modp1", BN_get_rfc2409_prime_768);
  174. V("modp2", BN_get_rfc2409_prime_1024);
  175. +#endif
  176. V("modp5", BN_get_rfc3526_prime_1536);
  177. V("modp14", BN_get_rfc3526_prime_2048);
  178. V("modp15", BN_get_rfc3526_prime_3072);
  179. @@ -558,15 +555,20 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
  180. return EVPKeyCtxPointer();
  181. }
  182. +#ifndef OPENSSL_IS_BORINGSSL
  183. prime_fixed_value->release();
  184. bn_g.release();
  185. key_params = EVPKeyPointer(EVP_PKEY_new());
  186. CHECK(key_params);
  187. CHECK_EQ(EVP_PKEY_assign_DH(key_params.get(), dh.release()), 1);
  188. +#else
  189. + return EVPKeyCtxPointer();
  190. +#endif
  191. } else if (int* prime_size = std::get_if<int>(&params->params.prime)) {
  192. EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DH, nullptr));
  193. EVP_PKEY* raw_params = nullptr;
  194. +#ifndef OPENSSL_IS_BORINGSSL
  195. if (!param_ctx ||
  196. EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 ||
  197. EVP_PKEY_CTX_set_dh_paramgen_prime_len(
  198. @@ -580,6 +582,9 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
  199. }
  200. key_params = EVPKeyPointer(raw_params);
  201. +#else
  202. + return EVPKeyCtxPointer();
  203. +#endif
  204. } else {
  205. UNREACHABLE();
  206. }
  207. diff --git a/src/crypto/crypto_dsa.cc b/src/crypto/crypto_dsa.cc
  208. index 3fa4a415dc911a13afd90dfb31c1ed4ad0fd268f..fa48dffc31342c44a1c1207b9d4c3dc72ed93b60 100644
  209. --- a/src/crypto/crypto_dsa.cc
  210. +++ b/src/crypto/crypto_dsa.cc
  211. @@ -40,7 +40,7 @@ namespace crypto {
  212. EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
  213. EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, nullptr));
  214. EVP_PKEY* raw_params = nullptr;
  215. -
  216. +#ifndef OPENSSL_IS_BORINGSSL
  217. if (!param_ctx ||
  218. EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 ||
  219. EVP_PKEY_CTX_set_dsa_paramgen_bits(
  220. @@ -55,7 +55,9 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
  221. return EVPKeyCtxPointer();
  222. }
  223. }
  224. -
  225. +#else
  226. + return EVPKeyCtxPointer();
  227. +#endif
  228. if (EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0)
  229. return EVPKeyCtxPointer();
  230. diff --git a/src/crypto/crypto_random.cc b/src/crypto/crypto_random.cc
  231. index 245f352918696413f8f0f7cec94dbcec687685af..35c1c1ce9f0e3d59b75e3966d485bf70b846de5b 100644
  232. --- a/src/crypto/crypto_random.cc
  233. +++ b/src/crypto/crypto_random.cc
  234. @@ -140,7 +140,7 @@ Maybe<bool> RandomPrimeTraits::AdditionalConfig(
  235. params->bits = bits;
  236. params->safe = safe;
  237. - params->prime.reset(BN_secure_new());
  238. + params->prime.reset(BN_new());
  239. if (!params->prime) {
  240. THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
  241. return Nothing<bool>();
  242. diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc
  243. index 3f8499457cf10765fa55e5018a26580f2c5ef15d..31647423be528949db744cbea43586ece5243bac 100644
  244. --- a/src/crypto/crypto_rsa.cc
  245. +++ b/src/crypto/crypto_rsa.cc
  246. @@ -610,10 +610,11 @@ Maybe<bool> GetRsaKeyDetail(
  247. }
  248. if (params->saltLength != nullptr) {
  249. - if (ASN1_INTEGER_get_int64(&salt_length, params->saltLength) != 1) {
  250. - ThrowCryptoError(env, ERR_get_error(), "ASN1_INTEGER_get_in64 error");
  251. - return Nothing<bool>();
  252. - }
  253. + // TODO(codebytere): Upstream a shim to BoringSSL?
  254. + // if (ASN1_INTEGER_get_int64(&salt_length, params->saltLength) != 1) {
  255. + // ThrowCryptoError(env, ERR_get_error(), "ASN1_INTEGER_get_in64 error");
  256. + // return Nothing<bool>();
  257. + // }
  258. }
  259. if (target
  260. diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc
  261. index 5734d8fdc5505e1586f571c19b840bd56e9c9f1f..3034b114e081e2b32dd5b71653927a41af7d48df 100644
  262. --- a/src/crypto/crypto_util.cc
  263. +++ b/src/crypto/crypto_util.cc
  264. @@ -517,24 +517,15 @@ Maybe<bool> Decorate(Environment* env, Local<Object> obj,
  265. V(BIO) \
  266. V(PKCS7) \
  267. V(X509V3) \
  268. - V(PKCS12) \
  269. V(RAND) \
  270. - V(DSO) \
  271. V(ENGINE) \
  272. V(OCSP) \
  273. V(UI) \
  274. V(COMP) \
  275. V(ECDSA) \
  276. V(ECDH) \
  277. - V(OSSL_STORE) \
  278. - V(FIPS) \
  279. - V(CMS) \
  280. - V(TS) \
  281. V(HMAC) \
  282. - V(CT) \
  283. - V(ASYNC) \
  284. - V(KDF) \
  285. - V(SM2) \
  286. + V(HKDF) \
  287. V(USER) \
  288. #define V(name) case ERR_LIB_##name: lib = #name "_"; break;
  289. @@ -715,7 +706,7 @@ void SecureBuffer(const FunctionCallbackInfo<Value>& args) {
  290. CHECK(args[0]->IsUint32());
  291. Environment* env = Environment::GetCurrent(args);
  292. uint32_t len = args[0].As<Uint32>()->Value();
  293. - void* data = OPENSSL_secure_zalloc(len);
  294. + void* data = OPENSSL_malloc(len);
  295. if (data == nullptr) {
  296. // There's no memory available for the allocation.
  297. // Return nothing.
  298. @@ -726,7 +717,7 @@ void SecureBuffer(const FunctionCallbackInfo<Value>& args) {
  299. data,
  300. len,
  301. [](void* data, size_t len, void* deleter_data) {
  302. - OPENSSL_secure_clear_free(data, len);
  303. + OPENSSL_clear_free(data, len);
  304. },
  305. data);
  306. Local<ArrayBuffer> buffer = ArrayBuffer::New(env->isolate(), store);
  307. @@ -734,10 +725,12 @@ void SecureBuffer(const FunctionCallbackInfo<Value>& args) {
  308. }
  309. void SecureHeapUsed(const FunctionCallbackInfo<Value>& args) {
  310. +#ifndef OPENSSL_IS_BORINGSSL
  311. Environment* env = Environment::GetCurrent(args);
  312. if (CRYPTO_secure_malloc_initialized())
  313. args.GetReturnValue().Set(
  314. BigInt::New(env->isolate(), CRYPTO_secure_used()));
  315. +#endif
  316. }
  317. } // namespace
  318. diff --git a/src/node_metadata.cc b/src/node_metadata.cc
  319. index 22546e9de25bdf95a00d06057626b544d5bf6f28..3a20f2f6a153c2f0b48b86ed83b92484ac4c274a 100644
  320. --- a/src/node_metadata.cc
  321. +++ b/src/node_metadata.cc
  322. @@ -16,7 +16,7 @@
  323. #include "v8.h"
  324. #include "zlib.h"
  325. -#if HAVE_OPENSSL
  326. +#if HAVE_OPENSSL && !defined(OPENSSL_IS_BORINGSSL)
  327. #include <openssl/opensslv.h>
  328. #if NODE_OPENSSL_HAS_QUIC
  329. #include <openssl/quic.h>
  330. diff --git a/src/node_metadata.h b/src/node_metadata.h
  331. index cf051585e779e2b03bd7b95fe5008b89cc7f8162..9de49c6828468fdf846dcd4ad445390f14446099 100644
  332. --- a/src/node_metadata.h
  333. +++ b/src/node_metadata.h
  334. @@ -6,7 +6,7 @@
  335. #include <string>
  336. #include "node_version.h"
  337. -#if HAVE_OPENSSL
  338. +#if 0
  339. #include <openssl/crypto.h>
  340. #if NODE_OPENSSL_HAS_QUIC
  341. #include <openssl/quic.h>
  342. diff --git a/src/node_options.cc b/src/node_options.cc
  343. index b544f1209143c0d4a01b1df3257e5b2ba1d5bfee..f711ac936e76f9c16d15d7db759d0081a9eb018d 100644
  344. --- a/src/node_options.cc
  345. +++ b/src/node_options.cc
  346. @@ -6,7 +6,7 @@
  347. #include "node_external_reference.h"
  348. #include "node_internals.h"
  349. #include "node_sea.h"
  350. -#if HAVE_OPENSSL
  351. +#if HAVE_OPENSSL && !defined(OPENSSL_IS_BORINGSSL)
  352. #include "openssl/opensslv.h"
  353. #endif
  354. diff --git a/src/node_options.h b/src/node_options.h
  355. index bc18a45e681a3cd8d26ea94862d7a6eb3a6631fc..08141c540c0c3fe4f2a4fe66bf75557a71a1d8e6 100644
  356. --- a/src/node_options.h
  357. +++ b/src/node_options.h
  358. @@ -11,7 +11,7 @@
  359. #include "node_mutex.h"
  360. #include "util.h"
  361. -#if HAVE_OPENSSL
  362. +#if 0
  363. #include "openssl/opensslv.h"
  364. #endif