diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 41698e5f02939e..c616fbedb06bbd 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -152,7 +152,6 @@ const { const kAsymmetricKeyType = Symbol('kAsymmetricKeyType'); const kAsymmetricKeyDetails = Symbol('kAsymmetricKeyDetails'); - const kAsymmetricKeyJWKProperties = Symbol('kAsymmetricKeyJWKProperties'); function normalizeKeyDetails(details = {}) { if (details.publicExponent !== undefined) { @@ -190,28 +189,6 @@ const { return {}; } } - - [kAsymmetricKeyJWKProperties]() { - switch (this.asymmetricKeyType) { - case 'rsa': return {}; - case 'ec': - switch (this.asymmetricKeyDetails.namedCurve) { - case 'prime256v1': return { crv: 'P-256' }; - case 'secp256k1': return { crv: 'secp256k1' }; - case 'secp384r1': return { crv: 'P-384' }; - case 'secp521r1': return { crv: 'P-521' }; - default: - throw new ERR_CRYPTO_JWK_UNSUPPORTED_CURVE( - this.asymmetricKeyDetails.namedCurve); - } - case 'ed25519': return { crv: 'Ed25519' }; - case 'ed448': return { crv: 'Ed448' }; - case 'x25519': return { crv: 'X25519' }; - case 'x448': return { crv: 'X448' }; - default: - throw new ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(); - } - } } class PublicKeyObject extends AsymmetricKeyObject { @@ -221,8 +198,7 @@ const { export(options) { if (options && options.format === 'jwk') { - const properties = this[kAsymmetricKeyJWKProperties](); - return this[kHandle].exportJwk(properties); + return this[kHandle].exportJwk({}); } const { format, @@ -243,8 +219,7 @@ const { throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS( 'jwk', 'does not support encryption'); } - const properties = this[kAsymmetricKeyJWKProperties](); - return this[kHandle].exportJwk(properties); + return this[kHandle].exportJwk({}); } const { format, diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 56a0ef8fb06a14..ab9806314e65d3 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -923,7 +923,6 @@ E('ERR_CRYPTO_INVALID_JWK', 'Invalid JWK data', TypeError); E('ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE', 'Invalid key object type %s, expected %s.', TypeError); E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error); -E('ERR_CRYPTO_JWK_UNSUPPORTED_CURVE', 'Unsupported JWK EC curve: %s.', Error); E('ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE', 'Unsupported JWK Key Type.', Error); E('ERR_CRYPTO_PBKDF2_ERROR', 'PBKDF2 error', Error); E('ERR_CRYPTO_SCRYPT_INVALID_PARAMETER', 'Invalid scrypt parameter', Error); diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index f0ad45529e0039..6497c3f6a56730 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -740,6 +740,33 @@ Maybe ExportJWKEcKey( return Nothing(); } + Local crv_name; + const int nid = EC_GROUP_get_curve_name(group); + switch (nid) { + case NID_X9_62_prime256v1: + crv_name = OneByteString(env->isolate(), "P-256"); + break; + case NID_secp256k1: + crv_name = OneByteString(env->isolate(), "secp256k1"); + break; + case NID_secp384r1: + crv_name = OneByteString(env->isolate(), "P-384"); + break; + case NID_secp521r1: + crv_name = OneByteString(env->isolate(), "P-521"); + break; + default: + ERR_CRYPTO_JWK_UNSUPPORTED_CURVE(env->isolate(), + "Unsupported JWK EC curve: %s.", + OBJ_nid2sn(nid)); + } + if (target->Set( + env->context(), + env->jwk_crv_string(), + crv_name).IsNothing()) { + return Nothing(); + } + if (key->GetKeyType() == kKeyTypePrivate) { const BIGNUM* pvt = EC_KEY_get0_private_key(ec); return SetEncodedValue( diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 89ad0874e0ffb9..468236aa65e9da 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -501,6 +501,8 @@ Maybe ExportJWKAsymmetricKey( case EVP_PKEY_X25519: // Fall through case EVP_PKEY_X448: return ExportJWKEdKey(env, key, target); + default: + ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env->isolate()); } THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); return Just(false); diff --git a/src/node_errors.h b/src/node_errors.h index 96659f3a400826..0f70fe81b9aa1c 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -49,6 +49,8 @@ void OnFatalError(const char* location, const char* message); V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, RangeError) \ V(ERR_CRYPTO_INVALID_STATE, Error) \ V(ERR_CRYPTO_INVALID_TAG_LENGTH, RangeError) \ + V(ERR_CRYPTO_JWK_UNSUPPORTED_CURVE, Error) \ + V(ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE, Error) \ V(ERR_CRYPTO_OPERATION_FAILED, Error) \ V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, RangeError) \ V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \ @@ -136,6 +138,7 @@ ERRORS_WITH_CODE(V) V(ERR_CRYPTO_INVALID_SCRYPT_PARAMS, "Invalid scrypt params") \ V(ERR_CRYPTO_INVALID_STATE, "Invalid state") \ V(ERR_CRYPTO_INVALID_TAG_LENGTH, "Invalid taglength") \ + V(ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE, "Unsupported JWK Key Type.") \ V(ERR_CRYPTO_OPERATION_FAILED, "Operation failed") \ V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, \ "Input buffers must have the same byte length") \