Skip to content

Commit ebddeaf

Browse files
committed
src: pull in more electron boringssl adjustments
Electron has a number of patches they float in order to build node.js with boringssl. We already incorporate some of those, this brings in more of them.
1 parent ed52ab9 commit ebddeaf

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

src/crypto/crypto_cipher.cc

+4
Original file line numberDiff line numberDiff line change
@@ -993,11 +993,15 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
993993
return ThrowCryptoError(env, ERR_get_error());
994994
}
995995

996+
#ifndef OPENSSL_IS_BORINGSSL
997+
// RSA implicit rejection here is not supported by BoringSSL.
998+
// Skip this check when boring is used.
996999
if (!ctx.setRsaImplicitRejection()) {
9971000
return THROW_ERR_INVALID_ARG_VALUE(
9981001
env,
9991002
"RSA_PKCS1_PADDING is no longer supported for private decryption");
10001003
}
1004+
#endif
10011005
}
10021006

10031007
const EVP_MD* digest = nullptr;

src/crypto/crypto_dh.cc

+33-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "memory_tracker-inl.h"
88
#include "ncrypto.h"
99
#include "node_errors.h"
10+
#ifndef OPENSSL_IS_BORINGSSL
1011
#include "openssl/bnerr.h"
12+
#endif
1113
#include "openssl/dh.h"
1214
#include "threadpoolwork-inl.h"
1315
#include "v8.h"
@@ -88,11 +90,15 @@ void New(const FunctionCallbackInfo<Value>& args) {
8890
if (args[0]->IsInt32()) {
8991
int32_t bits = args[0].As<Int32>()->Value();
9092
if (bits < 2) {
93+
#ifndef OPENSSL_IS_BORINGSSL
9194
#if OPENSSL_VERSION_MAJOR >= 3
9295
ERR_put_error(ERR_LIB_DH, 0, DH_R_MODULUS_TOO_SMALL, __FILE__, __LINE__);
9396
#else
9497
ERR_put_error(ERR_LIB_BN, 0, BN_R_BITS_TOO_SMALL, __FILE__, __LINE__);
95-
#endif
98+
#endif // OPENSSL_VERSION_MAJOR >= 3
99+
#else // OPENSSL_IS_BORINGSSL
100+
OPENSSL_PUT_ERROR(BN, BN_R_BITS_TOO_SMALL);
101+
#endif // OPENSSL_IS_BORINGSSL
96102
return ThrowCryptoError(env, ERR_get_error(), "Invalid prime length");
97103
}
98104

@@ -105,7 +111,11 @@ void New(const FunctionCallbackInfo<Value>& args) {
105111
}
106112
int32_t generator = args[1].As<Int32>()->Value();
107113
if (generator < 2) {
114+
#ifndef OPENSSL_IS_BORINGSSL
108115
ERR_put_error(ERR_LIB_DH, 0, DH_R_BAD_GENERATOR, __FILE__, __LINE__);
116+
#else
117+
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
118+
#endif
109119
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
110120
}
111121

@@ -134,12 +144,20 @@ void New(const FunctionCallbackInfo<Value>& args) {
134144
if (args[1]->IsInt32()) {
135145
int32_t generator = args[1].As<Int32>()->Value();
136146
if (generator < 2) {
147+
#ifndef OPENSSL_IS_BORINGSSL
137148
ERR_put_error(ERR_LIB_DH, 0, DH_R_BAD_GENERATOR, __FILE__, __LINE__);
149+
#else
150+
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
151+
#endif
138152
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
139153
}
140154
bn_g = BignumPointer::New();
141155
if (!bn_g.setWord(generator)) {
156+
#ifndef OPENSSL_IS_BORINGSSL
142157
ERR_put_error(ERR_LIB_DH, 0, DH_R_BAD_GENERATOR, __FILE__, __LINE__);
158+
#else
159+
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
160+
#endif
143161
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
144162
}
145163
} else {
@@ -148,11 +166,19 @@ void New(const FunctionCallbackInfo<Value>& args) {
148166
return THROW_ERR_OUT_OF_RANGE(env, "generator is too big");
149167
bn_g = BignumPointer(reinterpret_cast<uint8_t*>(arg1.data()), arg1.size());
150168
if (!bn_g) {
169+
#ifndef OPENSSL_IS_BORINGSSL
151170
ERR_put_error(ERR_LIB_DH, 0, DH_R_BAD_GENERATOR, __FILE__, __LINE__);
171+
#else
172+
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
173+
#endif
152174
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
153175
}
154176
if (bn_g.getWord() < 2) {
177+
#ifndef OPENSSL_IS_BORINGSSL
155178
ERR_put_error(ERR_LIB_DH, 0, DH_R_BAD_GENERATOR, __FILE__, __LINE__);
179+
#else
180+
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
181+
#endif
156182
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
157183
}
158184
}
@@ -398,14 +424,19 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
398424
if (!dh) return {};
399425

400426
key_params = EVPKeyPointer::NewDH(std::move(dh));
401-
} else if (int* prime_size = std::get_if<int>(&params->params.prime)) {
427+
} else if (std::get_if<int>(&params->params.prime)) {
402428
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DH);
429+
#ifndef OPENSSL_IS_BORINGSSL
430+
int* prime_size = std::get_if<int>(&params->params.prime);
403431
if (!param_ctx.initForParamgen() ||
404432
!param_ctx.setDhParameters(*prime_size, params->params.generator)) {
405433
return {};
406434
}
407435

408436
key_params = param_ctx.paramgen();
437+
#else
438+
return {};
439+
#endif
409440
} else {
410441
UNREACHABLE();
411442
}

src/crypto/crypto_dsa.cc

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ using v8::Value;
2929

3030
namespace crypto {
3131
EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
32+
#ifdef OPENSSL_IS_BORINGSSL
33+
// Operation is unsupported in BoringSSL
34+
return {};
35+
#else
3236
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DSA);
3337

3438
if (!param_ctx.initForParamgen() ||
@@ -46,6 +50,7 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
4650
EVPKeyCtxPointer key_ctx = key_params.newCtx();
4751
if (!key_ctx.initForKeygen()) return {};
4852
return key_ctx;
53+
#endif
4954
}
5055

5156
// Input arguments for DsaKeyPairGenJob

src/crypto/crypto_keys.cc

+5
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,10 @@ void KeyObjectHandle::GetAsymmetricKeyType(
950950
}
951951

952952
bool KeyObjectHandle::CheckEcKeyData() const {
953+
#ifdef OPENSSL_IS_BORINGSSL
954+
// Operation is unsupported on BoringSSL
955+
return true;
956+
#else
953957
MarkPopErrorOnReturn mark_pop_error_on_return;
954958

955959
const auto& key = data_.GetAsymmetricKey();
@@ -962,6 +966,7 @@ bool KeyObjectHandle::CheckEcKeyData() const {
962966
}
963967

964968
return ctx.publicCheck();
969+
#endif
965970
}
966971

967972
void KeyObjectHandle::CheckEcKeyData(const FunctionCallbackInfo<Value>& args) {

src/crypto/crypto_util.cc

+17
Original file line numberDiff line numberDiff line change
@@ -680,29 +680,46 @@ void SecureBuffer(const FunctionCallbackInfo<Value>& args) {
680680
CHECK(args[0]->IsUint32());
681681
Environment* env = Environment::GetCurrent(args);
682682
uint32_t len = args[0].As<Uint32>()->Value();
683+
#ifndef OPENSSL_IS_BORINGSSL
683684
void* data = OPENSSL_secure_zalloc(len);
685+
#else
686+
void* data = OPENSSL_malloc(len);
687+
#endif
684688
if (data == nullptr) {
685689
// There's no memory available for the allocation.
686690
// Return nothing.
687691
return;
688692
}
693+
#ifdef OPENSSL_IS_BORINGSSL
694+
memset(data, 0, len);
695+
#endif
689696
std::shared_ptr<BackingStore> store =
690697
ArrayBuffer::NewBackingStore(
691698
data,
692699
len,
693700
[](void* data, size_t len, void* deleter_data) {
701+
#ifndef OPENSSL_IS_BORINGSSL
694702
OPENSSL_secure_clear_free(data, len);
703+
#else
704+
OPENSSL_clear_free(data, len);
705+
#endif
695706
},
696707
data);
697708
Local<ArrayBuffer> buffer = ArrayBuffer::New(env->isolate(), store);
698709
args.GetReturnValue().Set(Uint8Array::New(buffer, 0, len));
699710
}
700711

701712
void SecureHeapUsed(const FunctionCallbackInfo<Value>& args) {
713+
#ifndef OPENSSL_IS_BORINGSSL
702714
Environment* env = Environment::GetCurrent(args);
703715
if (CRYPTO_secure_malloc_initialized())
704716
args.GetReturnValue().Set(
705717
BigInt::New(env->isolate(), CRYPTO_secure_used()));
718+
#else
719+
// BoringSSL does not have the secure heap and therefore
720+
// will always return 0.
721+
args.GetReturnValue().Set(BigInt::New(args.GetIsolate(), 0));
722+
#endif
706723
}
707724
} // namespace
708725

0 commit comments

Comments
 (0)