Skip to content

Commit 6be5eb0

Browse files
committed
crypto: decorate async crypto job errors with OpenSSL error details
1 parent b328bf7 commit 6be5eb0

30 files changed

+435
-76
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
@@ -532,16 +532,11 @@ MaybeLocal<Value> DHBitsTraits::EncodeOutput(Environment* env,
532532
bool DHBitsTraits::DeriveBits(Environment* env,
533533
const DHBitsConfig& params,
534534
ByteSource* out,
535-
CryptoJobMode mode) {
535+
CryptoJobMode mode,
536+
CryptoErrorStore* errors) {
536537
auto dp = DHPointer::stateless(params.private_key.GetAsymmetricKey(),
537538
params.public_key.GetAsymmetricKey());
538539
if (!dp) {
539-
bool can_throw = mode == CryptoJobMode::kCryptoJobSync;
540-
541-
if (can_throw) {
542-
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
543-
if (err) ThrowCryptoError(env, err, "diffieHellman failed");
544-
}
545540
return false;
546541
}
547542

src/crypto/crypto_dh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ struct DHBitsTraits final {
106106
static bool DeriveBits(Environment* env,
107107
const DHBitsConfig& params,
108108
ByteSource* out_,
109-
CryptoJobMode mode);
109+
CryptoJobMode mode,
110+
CryptoErrorStore* errors);
110111

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

src/crypto/crypto_ec.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ Maybe<void> ECDHBitsTraits::AdditionalConfig(
435435
bool ECDHBitsTraits::DeriveBits(Environment* env,
436436
const ECDHBitsConfig& params,
437437
ByteSource* out,
438-
CryptoJobMode mode) {
438+
CryptoJobMode mode,
439+
CryptoErrorStore* errors) {
439440
size_t len = 0;
440441
const auto& m_privkey = params.private_.GetAsymmetricKey();
441442
const auto& m_pubkey = params.public_.GetAsymmetricKey();
@@ -466,8 +467,10 @@ bool ECDHBitsTraits::DeriveBits(Environment* env,
466467
const EC_KEY* public_key = m_pubkey;
467468

468469
const auto group = ECKeyPointer::GetGroup(private_key);
469-
if (group == nullptr)
470+
if (group == nullptr) {
471+
errors->Insert(NodeCryptoError::ECDH_FAILED);
470472
return false;
473+
}
471474

472475
CHECK(ECKeyPointer::Check(private_key));
473476
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)