Skip to content

Forge doesn't validate version field when parsing RSA PRIVATE KEY PEM #1143

Description

@Jennifer-first

fc8017 shows: "An RSA private key should be represented with the ASN.1 type RSAPrivateKey: RSAPrivateKey ::= SEQUENCE {
version Version,
modulus INTEGER, -- n
publicExponent INTEGER, --e
privateExponent INTEGER, -- d
prime1 INTEGER, --p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1)
exponent2 INTEGER, -- d mod (q-1)
coefficient INTEGER, -- (inverse of q) mod p
otherPrimeInfos OtherPrimeInfos OPTIONAL
},
and, for version, version is the version number, for compatibility with future revisions of this document. It SHALL be 0 for this version of the document, unless multi-prime is used; in which case, it SHALL be 1." However, here forge accepts a key with version number 17.

To Reproduce:
Steps to reproduce the behavior:

const forge = require('node-forge');
const fs = require('fs');
const filePath = process.argv[2];
const keyPem = fs.readFileSync(filePath, 'utf8');
const privateKey = forge.pki.privateKeyFromPem(keyPem);
console.dir(privateKey, { depth: null, colors: true });

node parse_key.js invalid.key

Expected behavior
Since the version number is not 0 or 1, it should be rejected.

invalid.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions