crypto: skip private RSA parameters in key details · nodejs/node@ef957ac · GitHub
Skip to content

Commit ef957ac

Browse files
panvaaduh95
authored andcommitted
crypto: skip private RSA parameters in key details
Retrieving asymmetricKeyDetails only needs the modulus, public exponent, and RSA-PSS restrictions. Add a public-only Rsa view so provider-backed keys do not also extract private components or probe additional primes. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66108 Backport-PR-URL: #66233 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8feccc5 commit ef957ac

4 files changed

Lines changed: 50 additions & 20 deletions

File tree

Lines changed: 26 additions & 0 deletions

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6827,13 +6827,22 @@ ASN1StringPointer EncodeRsaPssParams(const Rsa::PssParams& params) {
68276827

68286828
Rsa::Rsa() : rsa_(false) {}
68296829

6830-
Rsa::Rsa(const EVP_PKEY* pkey) : Rsa() {
6830+
Rsa::Rsa(const EVP_PKEY* pkey, Selection selection) : Rsa() {
68316831
rsa_pss_ = EVPKeyPointer::isA(pkey, KeyAlgorithm::RSA_PSS);
68326832
if (!EVPKeyPointer::isA(pkey, KeyAlgorithm::RSA) && !rsa_pss_) return;
68336833
if (!GetPKeyBnParam(pkey, OSSL_PKEY_PARAM_RSA_N, &n_) ||
68346834
!GetPKeyBnParam(pkey, OSSL_PKEY_PARAM_RSA_E, &e_)) {
68356835
return;
68366836
}
6837+
if (rsa_pss_) {
6838+
MarkPopErrorOnReturn pop_errors;
6839+
PssParams params;
6840+
if (ReadRsaPssParams(pkey, &params)) pss_params_ = params;
6841+
}
6842+
if (selection == Selection::Public) {
6843+
rsa_ = true;
6844+
return;
6845+
}
68376846
if (!GetOptionalPKeyBnParam(pkey, OSSL_PKEY_PARAM_RSA_D, &d_) ||
68386847
!GetOptionalPKeyBnParam(pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, &p_) ||
68396848
!GetOptionalPKeyBnParam(pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, &q_) ||
@@ -6856,19 +6865,21 @@ Rsa::Rsa(const EVP_PKEY* pkey) : Rsa() {
68566865
other_prime_infos_.push_back(std::move(info));
68576866
}
68586867

6859-
if (rsa_pss_) {
6860-
MarkPopErrorOnReturn pop_errors;
6861-
PssParams params;
6862-
if (ReadRsaPssParams(pkey, &params)) pss_params_ = params;
6863-
}
6864-
68656868
rsa_ = true;
68666869
}
68676870
#else
68686871
Rsa::Rsa() : rsa_(nullptr) {}
68696872
Rsa::Rsa(OSSL3_CONST RSA* ptr) : rsa_(ptr) {}
68706873
#endif
68716874

6875+
Rsa Rsa::PublicOnly(const EVPKeyPointer& key) {
6876+
#if NCRYPTO_USE_OPENSSL3_PROVIDER
6877+
return Rsa(key.get(), Selection::Public);
6878+
#else
6879+
return key;
6880+
#endif
6881+
}
6882+
68726883
const Rsa::PublicKey Rsa::getPublicKey() const {
68736884
#if NCRYPTO_USE_OPENSSL3_PROVIDER
68746885
if (!rsa_) return {};

‎deps/ncrypto/ncrypto.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,10 @@ class Dsa final {
650650
class Rsa final {
651651
public:
652652
Rsa();
653+
enum class Selection { Public, Private };
654+
static Rsa PublicOnly(const EVPKeyPointer& key);
653655
#if NCRYPTO_USE_OPENSSL3_PROVIDER
654-
explicit Rsa(const EVP_PKEY* pkey);
656+
explicit Rsa(const EVP_PKEY* pkey, Selection selection = Selection::Private);
655657
#else
656658
Rsa(OSSL3_CONST RSA* rsa);
657659
#endif

‎src/crypto/crypto_rsa.cc‎

Lines changed: 3 additions & 12 deletions

0 commit comments

Comments
 (0)