summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTobias Nießen <tniessen@tnie.de>2021-08-23 12:34:30 +0000
committerMichaël Zasso <targos@protonmail.com>2021-09-06 09:19:21 +0200
commit34c627e4bc3dd17942392807ed5a60cd62361f7d (patch)
tree6decd3e7afe88bd06b47201ae2b3dcf4e11ae61b /src
parent3a8f77ac0d865a3e073eedb6af4e9ab82fe36bc2 (diff)
downloadnode-new-34c627e4bc3dd17942392807ed5a60cd62361f7d.tar.gz
crypto: add RSA-PSS params to asymmetricKeyDetails
Fixes: https://github.com/nodejs/node/issues/39837 Refs: https://github.com/openssl/openssl/pull/10568 PR-URL: https://github.com/nodejs/node/pull/39851 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/crypto/crypto_rsa.cc82
-rw-r--r--src/env.h3
2 files changed, 81 insertions, 4 deletions
diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc
index 5bbeb01ab5..1bbf9a1753 100644
--- a/src/crypto/crypto_rsa.cc
+++ b/src/crypto/crypto_rsa.cc
@@ -547,10 +547,84 @@ Maybe<bool> GetRsaKeyDetail(
reinterpret_cast<unsigned char*>(public_exponent.data());
CHECK_EQ(BN_bn2binpad(e, data, len), len);
- return target->Set(
- env->context(),
- env->public_exponent_string(),
- public_exponent.ToArrayBuffer());
+ if (target
+ ->Set(
+ env->context(),
+ env->public_exponent_string(),
+ public_exponent.ToArrayBuffer())
+ .IsNothing()) {
+ return Nothing<bool>();
+ }
+
+ if (type == EVP_PKEY_RSA_PSS) {
+ // Due to the way ASN.1 encoding works, default values are omitted when
+ // encoding the data structure. However, there are also RSA-PSS keys for
+ // which no parameters are set. In that case, the ASN.1 RSASSA-PSS-params
+ // sequence will be missing entirely and RSA_get0_pss_params will return
+ // nullptr. If parameters are present but all parameters are set to their
+ // default values, an empty sequence will be stored in the ASN.1 structure.
+ // In that case, RSA_get0_pss_params does not return nullptr but all fields
+ // of the returned RSA_PSS_PARAMS will be set to nullptr.
+
+ const RSA_PSS_PARAMS* params = RSA_get0_pss_params(rsa);
+ if (params != nullptr) {
+ int hash_nid = NID_sha1;
+ int mgf_nid = NID_mgf1;
+ int mgf1_hash_nid = NID_sha1;
+ int64_t salt_length = 20;
+
+ if (params->hashAlgorithm != nullptr) {
+ hash_nid = OBJ_obj2nid(params->hashAlgorithm->algorithm);
+ }
+
+ if (target
+ ->Set(
+ env->context(),
+ env->hash_algorithm_string(),
+ OneByteString(env->isolate(), OBJ_nid2ln(hash_nid)))
+ .IsNothing()) {
+ return Nothing<bool>();
+ }
+
+ if (params->maskGenAlgorithm != nullptr) {
+ mgf_nid = OBJ_obj2nid(params->maskGenAlgorithm->algorithm);
+ if (mgf_nid == NID_mgf1) {
+ mgf1_hash_nid = OBJ_obj2nid(params->maskHash->algorithm);
+ }
+ }
+
+ // If, for some reason, the MGF is not MGF1, then the MGF1 hash function
+ // is intentionally not added to the object.
+ if (mgf_nid == NID_mgf1) {
+ if (target
+ ->Set(
+ env->context(),
+ env->mgf1_hash_algorithm_string(),
+ OneByteString(env->isolate(), OBJ_nid2ln(mgf1_hash_nid)))
+ .IsNothing()) {
+ return Nothing<bool>();
+ }
+ }
+
+ if (params->saltLength != nullptr) {
+ if (ASN1_INTEGER_get_int64(&salt_length, params->saltLength) != 1) {
+ ThrowCryptoError(env, ERR_get_error(), "ASN1_INTEGER_get_in64 error");
+ return Nothing<bool>();
+ }
+ }
+
+ if (target
+ ->Set(
+ env->context(),
+ env->salt_length_string(),
+ Number::New(env->isolate(), static_cast<double>(salt_length)))
+ .IsNothing()) {
+ return Nothing<bool>();
+ }
+ }
+ }
+
+ return Just<bool>(true);
}
namespace RSAAlg {
diff --git a/src/env.h b/src/env.h
index 8760bc4361..4fd5be8e15 100644
--- a/src/env.h
+++ b/src/env.h
@@ -268,6 +268,7 @@ constexpr size_t kFsStatsBufferLength =
V(gid_string, "gid") \
V(h2_string, "h2") \
V(handle_string, "handle") \
+ V(hash_algorithm_string, "hashAlgorithm") \
V(help_text_string, "helpText") \
V(homedir_string, "homedir") \
V(host_string, "host") \
@@ -316,6 +317,7 @@ constexpr size_t kFsStatsBufferLength =
V(message_port_string, "messagePort") \
V(message_string, "message") \
V(messageerror_string, "messageerror") \
+ V(mgf1_hash_algorithm_string, "mgf1HashAlgorithm") \
V(minttl_string, "minttl") \
V(module_string, "module") \
V(modulus_string, "modulus") \
@@ -385,6 +387,7 @@ constexpr size_t kFsStatsBufferLength =
V(replacement_string, "replacement") \
V(require_string, "require") \
V(retry_string, "retry") \
+ V(salt_length_string, "saltLength") \
V(scheme_string, "scheme") \
V(scopeid_string, "scopeid") \
V(serial_number_string, "serialNumber") \