Skip to content

Commit 9624049

Browse files
jasnelltargos
authored andcommitted
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. PR-URL: #56858 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 45692e9 commit 9624049

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
@@ -1005,11 +1005,15 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
10051005
return ThrowCryptoError(env, ERR_get_error());
10061006
}
10071007

1008+
#ifndef OPENSSL_IS_BORINGSSL
1009+
// RSA implicit rejection here is not supported by BoringSSL.
1010+
// Skip this check when boring is used.
10081011
if (!ctx.setRsaImplicitRejection()) {
10091012
return THROW_ERR_INVALID_ARG_VALUE(
10101013
env,
10111014
"RSA_PKCS1_PADDING is no longer supported for private decryption");
10121015
}
1016+
#endif
10131017
}
10141018

10151019
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
@@ -28,6 +28,10 @@ using v8::Value;
2828

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

3337
if (!param_ctx.initForParamgen() ||
@@ -45,6 +49,7 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
4549
EVPKeyCtxPointer key_ctx = key_params.newCtx();
4650
if (!key_ctx.initForKeygen()) return {};
4751
return key_ctx;
52+
#endif
4853
}
4954

5055
// Input arguments for DsaKeyPairGenJob

src/crypto/crypto_keys.cc

+5
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ void KeyObjectHandle::GetAsymmetricKeyType(
924924
}
925925

926926
bool KeyObjectHandle::CheckEcKeyData() const {
927+
#ifdef OPENSSL_IS_BORINGSSL
928+
// Operation is unsupported on BoringSSL
929+
return true;
930+
#else
927931
MarkPopErrorOnReturn mark_pop_error_on_return;
928932

929933
const auto& key = data_.GetAsymmetricKey();
@@ -933,6 +937,7 @@ bool KeyObjectHandle::CheckEcKeyData() const {
933937

934938
return data_.GetKeyType() == kKeyTypePrivate ? ctx.privateCheck()
935939
: ctx.publicCheck();
940+
#endif
936941
}
937942

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

src/crypto/crypto_util.cc

+17
Original file line numberDiff line numberDiff line change
@@ -682,29 +682,46 @@ void SecureBuffer(const FunctionCallbackInfo<Value>& args) {
682682
CHECK(args[0]->IsUint32());
683683
Environment* env = Environment::GetCurrent(args);
684684
uint32_t len = args[0].As<Uint32>()->Value();
685+
#ifndef OPENSSL_IS_BORINGSSL
685686
void* data = OPENSSL_secure_zalloc(len);
687+
#else
688+
void* data = OPENSSL_malloc(len);
689+
#endif
686690
if (data == nullptr) {
687691
// There's no memory available for the allocation.
688692
// Return nothing.
689693
return;
690694
}
695+
#ifdef OPENSSL_IS_BORINGSSL
696+
memset(data, 0, len);
697+
#endif
691698
std::shared_ptr<BackingStore> store =
692699
ArrayBuffer::NewBackingStore(
693700
data,
694701
len,
695702
[](void* data, size_t len, void* deleter_data) {
703+
#ifndef OPENSSL_IS_BORINGSSL
696704
OPENSSL_secure_clear_free(data, len);
705+
#else
706+
OPENSSL_clear_free(data, len);
707+
#endif
697708
},
698709
data);
699710
Local<ArrayBuffer> buffer = ArrayBuffer::New(env->isolate(), store);
700711
args.GetReturnValue().Set(Uint8Array::New(buffer, 0, len));
701712
}
702713

703714
void SecureHeapUsed(const FunctionCallbackInfo<Value>& args) {
715+
#ifndef OPENSSL_IS_BORINGSSL
704716
Environment* env = Environment::GetCurrent(args);
705717
if (CRYPTO_secure_malloc_initialized())
706718
args.GetReturnValue().Set(
707719
BigInt::New(env->isolate(), CRYPTO_secure_used()));
720+
#else
721+
// BoringSSL does not have the secure heap and therefore
722+
// will always return 0.
723+
args.GetReturnValue().Set(BigInt::New(args.GetIsolate(), 0));
724+
#endif
708725
}
709726
} // namespace
710727

0 commit comments

Comments
 (0)