Skip to content

Commit 574debc

Browse files
panvaRafaelGSS
authored andcommitted
crypto: decorate async crypto job errors with OpenSSL error details
PR-URL: nodejs#62348 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f8dc189 commit 574debc

32 files changed

+443
-80
lines changed

src/crypto/crypto_argon2.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ Maybe<void> Argon2Traits::AdditionalConfig(
124124
bool Argon2Traits::DeriveBits(Environment* env,
125125
const Argon2Config& config,
126126
ByteSource* out,
127-
CryptoJobMode mode) {
127+
CryptoJobMode mode,
128+
CryptoErrorStore* errors) {
128129
// If the config.length is zero-length, just return an empty buffer.
129130
// It's useless, yes, but allowed via the API.
130131
if (config.keylen == 0) {
@@ -144,7 +145,10 @@ bool Argon2Traits::DeriveBits(Environment* env,
144145
config.ad,
145146
config.type);
146147

147-
if (!dp) return false;
148+
if (!dp) {
149+
errors->Insert(NodeCryptoError::ARGON2_FAILED);
150+
return false;
151+
}
148152
DCHECK(!dp.isSecure());
149153
*out = ByteSource::Allocated(dp.release());
150154
return true;

src/crypto/crypto_argon2.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ struct Argon2Traits final {
5959
static bool DeriveBits(Environment* env,
6060
const Argon2Config& config,
6161
ByteSource* out,
62-
CryptoJobMode mode);
62+
CryptoJobMode mode,
63+
CryptoErrorStore* errors);
6364

6465
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
6566
const Argon2Config& config,

src/crypto/crypto_dh.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,11 @@ MaybeLocal<Value> DHBitsTraits::EncodeOutput(Environment* env,
504504
bool DHBitsTraits::DeriveBits(Environment* env,
505505
const DHBitsConfig& params,
506506
ByteSource* out,
507-
CryptoJobMode mode) {
507+
CryptoJobMode mode,
508+
CryptoErrorStore* errors) {
508509
auto dp = DHPointer::stateless(params.private_key.GetAsymmetricKey(),
509510
params.public_key.GetAsymmetricKey());
510511
if (!dp) {
511-
bool can_throw = mode == CryptoJobMode::kCryptoJobSync;
512-
513-
if (can_throw) {
514-
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
515-
if (err) ThrowCryptoError(env, err, "diffieHellman failed");
516-
}
517512
return false;
518513
}
519514

src/crypto/crypto_dh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ struct DHBitsTraits final {
8383
static bool DeriveBits(Environment* env,
8484
const DHBitsConfig& params,
8585
ByteSource* out_,
86-
CryptoJobMode mode);
86+
CryptoJobMode mode,
87+
CryptoErrorStore* errors);
8788

8889
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
8990
const DHBitsConfig& params,

src/crypto/crypto_ec.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ Maybe<void> ECDHBitsTraits::AdditionalConfig(
433433
bool ECDHBitsTraits::DeriveBits(Environment* env,
434434
const ECDHBitsConfig& params,
435435
ByteSource* out,
436-
CryptoJobMode mode) {
436+
CryptoJobMode mode,
437+
CryptoErrorStore* errors) {
437438
size_t len = 0;
438439
const auto& m_privkey = params.private_.GetAsymmetricKey();
439440
const auto& m_pubkey = params.public_.GetAsymmetricKey();
@@ -464,8 +465,10 @@ bool ECDHBitsTraits::DeriveBits(Environment* env,
464465
const EC_KEY* public_key = m_pubkey;
465466

466467
const auto group = ECKeyPointer::GetGroup(private_key);
467-
if (group == nullptr)
468+
if (group == nullptr) {
469+
errors->Insert(NodeCryptoError::ECDH_FAILED);
468470
return false;
471+
}
469472

470473
CHECK(ECKeyPointer::Check(private_key));
471474
CHECK(ECKeyPointer::Check(public_key));

src/crypto/crypto_ec.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ struct ECDHBitsTraits final {
8080
static bool DeriveBits(Environment* env,
8181
const ECDHBitsConfig& params,
8282
ByteSource* out_,
83-
CryptoJobMode mode);
83+
CryptoJobMode mode,
84+
CryptoErrorStore* errors);
8485

8586
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
8687
const ECDHBitsConfig& params,

src/crypto/crypto_hash.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ Maybe<void> HashTraits::AdditionalConfig(
540540
bool HashTraits::DeriveBits(Environment* env,
541541
const HashConfig& params,
542542
ByteSource* out,
543-
CryptoJobMode mode) {
543+
CryptoJobMode mode,
544+
CryptoErrorStore* errors) {
544545
auto ctx = EVPMDCtxPointer::New();
545546

546547
if (!ctx.digestInit(params.digest) || !ctx.digestUpdate(params.in))

src/crypto/crypto_hash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ struct HashTraits final {
7373
static bool DeriveBits(Environment* env,
7474
const HashConfig& params,
7575
ByteSource* out,
76-
CryptoJobMode mode);
76+
CryptoJobMode mode,
77+
CryptoErrorStore* errors);
7778

7879
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
7980
const HashConfig& params,

src/crypto/crypto_hkdf.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ Maybe<void> HKDFTraits::AdditionalConfig(
100100
bool HKDFTraits::DeriveBits(Environment* env,
101101
const HKDFConfig& params,
102102
ByteSource* out,
103-
CryptoJobMode mode) {
103+
CryptoJobMode mode,
104+
CryptoErrorStore* errors) {
104105
auto dp = ncrypto::hkdf(params.digest,
105106
ncrypto::Buffer<const unsigned char>{
106107
.data = reinterpret_cast<const unsigned char*>(
@@ -116,7 +117,10 @@ bool HKDFTraits::DeriveBits(Environment* env,
116117
.len = params.salt.size(),
117118
},
118119
params.length);
119-
if (!dp) return false;
120+
if (!dp) {
121+
errors->Insert(NodeCryptoError::HKDF_FAILED);
122+
return false;
123+
}
120124

121125
DCHECK(!dp.isSecure());
122126
*out = ByteSource::Allocated(dp.release());

src/crypto/crypto_hkdf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct HKDFTraits final {
4545
static bool DeriveBits(Environment* env,
4646
const HKDFConfig& params,
4747
ByteSource* out,
48-
CryptoJobMode mode);
48+
CryptoJobMode mode,
49+
CryptoErrorStore* errors);
4950

5051
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
5152
const HKDFConfig& params,

0 commit comments

Comments
 (0)