-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
crypto.publicDecrypt and crypto.publicEncrypt doesn't detect PEM encoded public key in parameters #13612
Comments
cc/ @nodejs/crypto |
And this seems to be the case in all versions I tried (>= node.js 4.6). |
@rinne both of those keys are technically invalid because they start with a newline. Node.js looks at the beginning of the key to figure out what kind of a key it is and assumes PrivateKey if all else fails. (see here) That works when the key actually is a private key (I guess OpenSSL is lenient about whitespace) and doesn't if it's a public key.
cc @nodejs/crypto |
This seems reasonable at first glance. |
Then the next question is whether OpenSSL has any function that automatically detects key type, or if a long chain of if/else with comparisons is really the only way. |
Oh my. The problem indeed was the newline in the beginning of the key. Weird that it wasn't a problem with the private key though. And isn't the beginning and the end markers with base64 in between just something to get around the possible trash around the actual payload? Anyways, I leave it to you, whether you want just to close the ticket or do something about it. One thing that at least needs fixing, is the documentation of the crypto session of node.js. Even in public key encrypt/decrypt it talks about:
It does say later that the private key can be used instead of the public key, but still it's clearly wrong. And a comment about the whitespace would be welcome :). |
Indeed, if I insert garbage in the beginning of the private key, it still works just fine. Maybe instead of inspecting the beginning of the key, Node.js should just try calling each of OpenSSL's
You're welcome to submit a PR for that. 😉 |
OK, I did. #13633 |
I think this should probably be fixed, if you read RFC 7468, on page 4:
If there's no objections, I'd like to take on this change |
Pull request welcome. Node.js probably needs to start using node/deps/openssl/openssl/crypto/pem/pem_pkey.c Lines 78 to 148 in b3e5367
Consolidate the public/private key handling in src/node_crypto.cc while you're at it. |
Up, this issue is still present in node v8.11 |
PR-URL: #23164 Fixes: #13612 Fixes: #22815 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
crypto.publicDecrypt and crypto.publicEncrypt doesn't detect PEM encoded public key in parameters.
The text was updated successfully, but these errors were encountered: