Skip to content

Commit

Permalink
crypto: fix "Invalid JWK" error messages
Browse files Browse the repository at this point in the history
PR-URL: #36200
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
  • Loading branch information
panva authored and danielleadams committed Dec 7, 2020
1 parent 39d0ced commit 841e8f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/crypto/crypto_dsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ std::shared_ptr<KeyObjectData> ImportJWKDsaKey(
!q_value->IsString() ||
!q_value->IsString() ||
(!x_value->IsUndefined() && !x_value->IsString())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK DSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK DSA key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -210,14 +210,14 @@ std::shared_ptr<KeyObjectData> ImportJWKDsaKey(
p.ToBN().release(),
q.ToBN().release(),
g.ToBN().release())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK DSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK DSA key");
return std::shared_ptr<KeyObjectData>();
}

if (type == kKeyTypePrivate) {
ByteSource x = ByteSource::FromEncodedString(env, x_value.As<String>());
if (!DSA_set0_key(dsa.get(), nullptr, x.ToBN().release())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK DSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK DSA key");
return std::shared_ptr<KeyObjectData>();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,15 @@ std::shared_ptr<KeyObjectData> ImportJWKEcKey(
if (!x_value->IsString() ||
!y_value->IsString() ||
(!d_value->IsUndefined() && !d_value->IsString())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK EC key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
return std::shared_ptr<KeyObjectData>();
}

KeyType type = d_value->IsString() ? kKeyTypePrivate : kKeyTypePublic;

ECKeyPointer ec(EC_KEY_new_by_curve_name(nid));
if (!ec) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK EC key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -699,14 +699,14 @@ std::shared_ptr<KeyObjectData> ImportJWKEcKey(
ec.get(),
x.ToBN().get(),
y.ToBN().get())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK EC key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
return std::shared_ptr<KeyObjectData>();
}

if (type == kKeyTypePrivate) {
ByteSource d = ByteSource::FromEncodedString(env, d_value.As<String>());
if (!EC_KEY_set_private_key(ec.get(), d.ToBN().get())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK EC key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
return std::shared_ptr<KeyObjectData>();
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,12 @@ std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
!jwk->Get(env->context(), env->jwk_d_string()).ToLocal(&d_value) ||
!n_value->IsString() ||
!e_value->IsString()) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}

if (!d_value->IsUndefined() && !d_value->IsString()) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -443,7 +443,7 @@ std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
n.ToBN().release(),
e.ToBN().release(),
nullptr)) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -459,7 +459,7 @@ std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
!jwk->Get(env->context(), env->jwk_dp_string()).ToLocal(&dp_value) ||
!jwk->Get(env->context(), env->jwk_dq_string()).ToLocal(&dq_value) ||
!jwk->Get(env->context(), env->jwk_qi_string()).ToLocal(&qi_value)) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -468,7 +468,7 @@ std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
!dp_value->IsString() ||
!dq_value->IsString() ||
!qi_value->IsString()) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}

Expand All @@ -486,7 +486,7 @@ std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
dp.ToBN().release(),
dq.ToBN().release(),
qi.ToBN().release())) {
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JSK RSA key");
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
return std::shared_ptr<KeyObjectData>();
}
}
Expand Down Expand Up @@ -547,4 +547,3 @@ void Initialize(Environment* env, Local<Object> target) {
} // namespace RSAAlg
} // namespace crypto
} // namespace node

0 comments on commit 841e8f4

Please sign in to comment.