From 3981853c003bf695850643f3820c4ad6b9924c10 Mon Sep 17 00:00:00 2001 From: Tim Perry <1526883+pimterry@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:15:01 +0200 Subject: [PATCH] crypto: return a clearer error when loading an unsupported pkcs12 PR-URL: https://github.com/nodejs/node/pull/54485 Reviewed-By: Luigi Pinca --- src/crypto/crypto_context.cc | 10 ++++++++++ test/fixtures/keys/legacy.pfx | Bin 0 -> 1058 bytes test/parallel/test-tls-legacy-pfx.js | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test/fixtures/keys/legacy.pfx create mode 100644 test/parallel/test-tls-legacy-pfx.js diff --git a/src/crypto/crypto_context.cc b/src/crypto/crypto_context.cc index 48fecc82c159d8..aa5ba34c761224 100644 --- a/src/crypto/crypto_context.cc +++ b/src/crypto/crypto_context.cc @@ -1148,6 +1148,16 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo& args) { if (!ret) { // TODO(@jasnell): Should this use ThrowCryptoError? unsigned long err = ERR_get_error(); // NOLINT(runtime/int) + +#if OPENSSL_VERSION_MAJOR >= 3 + if (ERR_GET_REASON(err) == ERR_R_UNSUPPORTED) { + // OpenSSL's "unsupported" error without any context is very + // common and not very helpful, so we override it: + return THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION( + env, "Unsupported PKCS12 PFX data"); + } +#endif + const char* str = ERR_reason_error_string(err); str = str != nullptr ? str : "Unknown error"; diff --git a/test/fixtures/keys/legacy.pfx b/test/fixtures/keys/legacy.pfx new file mode 100644 index 0000000000000000000000000000000000000000..66fa746fa5e573d2e7cded1632863f4294736c97 GIT binary patch literal 1058 zcmXqLVv%EFWHxAGe!|A7)#lOmotKfFaX}OFRhA~^i$LMC22G4_5K?Rlni%f_g>Eu2 zG61Pd2pL8LPdJB_*TBX=4#DLy5M<%#l6n>(cGBh|6B7pm6Cpf9%f4EI^wC}l~H*|O@h-RD>9 z{gxN^IrB!dzBnp)HHxX_TGWu`9>&P53UVn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