RSA multi-prime private keys are read as two-prime keys, and signatures leak a factor of the modulus
Description
pki.privateKeyFromPem() and pki.privateKeyFromAsn1() ignore the RSAPrivateKey version and the otherPrimeInfos field, so a multi-prime RSA key is read with only its first two primes. Signatures made with such a key are invalid, and each one reveals a factor of the modulus to anyone who sees it. Fixed in node-forge 1.4.4.
Details
RFC 8017 (A.1.2) allows RSA private keys with more than two primes: version 1, with the extra primes in otherPrimeInfos. node-forge only supports two-prime keys, but pki.privateKeyFromAsn1() in lib/rsa.js (used by pki.privateKeyFromPem(), PKCS#8 and PKCS#12 key parsing) never checked the version, and ASN.1 validation ignores the extra field. A multi-prime key is therefore read as if it were a two-prime key with modulus n, primes p and q, and the CRT values for p and q.
forge's private-key operation uses the CRT with p and q, so its result is correct modulo p*q but not modulo the remaining primes. For a signature s made this way, s^e equals the encoded message modulo p*q but not modulo n, so gcd(s^e - EM, n) = p*q, where EM is the PKCS#1 encoding of the signed message, which a verifier can compute. One signature therefore splits n into p*q and the product of the other primes. For a three-prime key (the usual case; OpenSSL with rsa_keygen_primes:3) this reveals the third prime and leaves a two-prime modulus of about two thirds of the size: for a 2048-bit key, a 1366-bit RSA modulus instead of 2048 bits.
Decryption with such a key fails, and the signatures don't verify, so applications that use a multi-prime key with forge see errors immediately. Keys generated by forge, by OpenSSL by default, by Node.js and by most other tools are two-prime and not affected.
The fix rejects RSAPrivateKeys with a version other than 0 or with otherPrimeInfos ("Only two-prime RSAPrivateKey version 0 is supported."). This also rejects two-prime keys with an invalid version, as reported in github.com/
Patches
Fixed in node-forge 1.4.4.
Workarounds
Don't use multi-prime RSA keys with node-forge. Before loading a key from an untrusted or unknown source, check that its RSAPrivateKey version is 0. Multi-prime keys that were used to sign with node-forge should be replaced, since their signatures reveal a factor of the modulus.
References
- RFC 8017, appendix A.1.2 (RSA private key syntax): rfc-editor.org/
rfc/ rfc8017#appendix-A.1.2 - Issue #1143 (RSAPrivateKey version not checked): github.com/
digitalbazaar/ forge/ issues/ 1143 - Boneh, DeMillo and Lipton, "On the Importance of Checking Cryptographic Protocols for Faults" (Eurocrypt 1997): the same gcd recovers factors from a faulty CRT signature.
Severity
CVSS 3.1: CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:N/A:N (3.7, Low) CVSS 4.0 (informational): CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:L/VI:N/VA:N/SC:N/SI:N/SA:N (6.3) CVSS 4.0 has no counterpart to 3.1's AC:H beyond AT:P and scores this higher; the severity rating follows CVSS 3.1.
Proof of concept
Generate a three-prime key and sign with forge:
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 \ -pkeyopt rsa_keygen_primes:3 -out mp3.pem
var key = forge.pki.privateKeyFromPem(pem); // <= 1.4.3: loads var md = forge.md.sha256.create(); md.update('hello'); var s = key.sign(md); // the signature doesn't verify (Node.js crypto.verify() is false), and // gcd(s^e - EM, n), with EM the PKCS#1 v1.5 encoding of SHA-256('hello'), // is a 1366-bit factor of the 2048-bit n // 1.4.4: privateKeyFromPem() throws "Cannot read private key. Only // two-prime RSAPrivateKey version 0 is supported."
Regression tests are in tests/unit/rsa.js ("RSAPrivateKey version").
Impact
Applications that load a multi-prime RSA private key with node-forge and sign with it are affected. Each signature reveals the product of two of the key's primes, and so the remaining prime(s), to anyone who sees the signature, which reduces the key's security (for a 2048-bit three-prime key, to that of a 1366-bit RSA key). The signatures are invalid and decryption fails, so the problem is visible to the application. Two-prime keys, which nearly all tools generate, are not affected.