Skip to content

The pkcs1 format is sensitive to content outside the BEGIN header #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
garethsime opened this issue Mar 20, 2018 · 3 comments
Closed

Comments

@garethsime
Copy link
Contributor

The pkcs1 import doesn't strip characters outside of the BEGIN RSA PRIVATE KEY header:

var pem = data.replace('-----BEGIN RSA PRIVATE KEY-----', '')
    .replace('-----END RSA PRIVATE KEY-----', '')
    .replace(/\s+|\n\r|\n|\r$/gm, '');

If you have a key file that starts or ends like this:

Some random text
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
Some other text

Then you'll get an error when the key is parsed:

{ InvalidAsn1Error: Expected 0x2: got 0xac
    at newInvalidAsn1Error (/Users/user-name/.nvm/versions/node/v7.10.0/lib/node_modules/asap-cli/node_modules/asn1/lib/ber/errors.js:7:13)
    at Reader.readString (/Users/user-name/.nvm/versions/node/v7.10.0/lib/node_modules/asap-cli/node_modules/asn1/lib/ber/reader.js:168:11)
    at Object.privateImport (/Users/user-name/.nvm/versions/node/v7.10.0/lib/node_modules/asap-cli/node_modules/node-rsa/src/formats/pkcs1.js:64:16)
    at Object.detectAndImport (/Users/user-name/.nvm/versions/node/v7.10.0/lib/node_modules/asap-cli/node_modules/node-rsa/src/formats/formats.js:63:48)
    at NodeRSA.module.exports.NodeRSA.importKey (/Users/user-name/.nvm/versions/node/v7.10.0/lib/node_modules/asap-cli/node_modules/node-rsa/src/NodeRSA.js:185:22)
@rzcoder
Copy link
Owner

rzcoder commented Mar 21, 2018

Correct behavior, import wait for correct data, it will not trying parse key from some random text document.

@garethsime
Copy link
Contributor Author

From RFC 7468:

Data before the encapsulation boundaries are permitted, and parsers MUST NOT malfunction when processing such data.

Would you be willing to accept a PR if I had a go at fixing it? I don't want to impose :)

@rzcoder
Copy link
Owner

rzcoder commented Mar 21, 2018

Ok, seems you are right :) PR welkome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants