node-jsonwebtoken: Error in Verify(): PEM_read_bio_PUBKEY
On OSX 10.10.2, Node v0.12.0 and jsonwebtoken v4.1.0
Using jsonwebtoken to create my own RS256-signed tokens in the ActionHero framework. Loaded the public key with api.auth.publicKey = fs.readFileSync(path.join(__dirname, '../', api.config.auth.publicKey));
.
I am able encode, but not decode. When calling require('jsonwebtoken').verify(token,, api.auth.publicKey)
I get a PEM_read_bio_PUBKEY failed
error. Seems to be in the JWS library, judging from the stack trace.
Full error trace (in an ActionHero flavor):
2015-03-16 11:09:31 - error: ! uncaught error from action: action:status
2015-03-16 11:09:31 - error: ! connection details:
2015-03-16 11:09:31 - error: ! action: "status"
2015-03-16 11:09:31 - error: ! remoteIP: "127.0.0.1"
2015-03-16 11:09:31 - error: ! type: "web"
2015-03-16 11:09:31 - error: ! params: {"action":"status","apiVersion":1}
2015-03-16 11:09:31 - error: ! Error: PEM_read_bio_PUBKEY failed
2015-03-16 11:09:31 - error: ! at Error (native)
2015-03-16 11:09:31 - error: ! at Verify.verify (crypto.js:356:23)
2015-03-16 11:09:31 - error: ! at Object.verify (/project/Documents/Repositories/server/node_modules/jsonwebtoken/node_modules/jws/node_modules/jwa/index.js:65:21)
2015-03-16 11:09:31 - error: ! at Object.jwsVerify [as verify] (/project/Documents/Repositories/server/node_modules/jsonwebtoken/node_modules/jws/lib/verify-stream.js:68:15)
2015-03-16 11:09:31 - error: ! at Object.module.exports.verify (/project/Documents/Repositories/server/node_modules/jsonwebtoken/index.js:113:17)
2015-03-16 11:09:31 - error: ! at Object.api.auth.isAuthenticated (/project/Documents/Repositories/server/initializers/1500_auth.js:38:18)
2015-03-16 11:09:31 - error: ! at /project/Documents/Repositories/server/initializers/1500_auth.js:50:22
2015-03-16 11:09:31 - error: ! at /project/Documents/Repositories/server/node_modules/actionhero/initializers/actionProcessor.js:135:15
2015-03-16 11:09:31 - error: ! at /project/Documents/Repositories/server/node_modules/actionhero/node_modules/async/lib/async.js:610:21
2015-03-16 11:09:31 - error: ! at /project/Documents/Repositories/server/node_modules/actionhero/node_modules/async
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 20
Seems using the following openssl command:
I was able to use the private key to output the public key in the correct PEM format, and was able to get the verify step to work.
The problem is the way you format the public key.
The function is very strict on the formatting. Require begin and end lines, and line breaks at every 64 characters.
Something like this: -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D o2kQ+X5xK9cipRgEKwIDAQAB -----END PUBLIC KEY-----
For me and anyone else who has this same issue, I found this function which seems to have done the necessary conversion.
^^Thanks so much @chekkan!
Took me a while scratching my head when this didn’t quite work for me until I realized that I needed to replace the ‘CERTIFICATE’ with ‘PUBLIC KEY’ in my case.
The below worked for me (Heavily inspired from the code above leveraging ES6):
Public key needs to be in PKCS8 (OpenSSL default) format.
You can export it by running the following command:
ssh-keygen -e -m PKCS8 -f /path/to/key.pub > /path/to/converted_key.pub
Don’t know if this is the scope of this project, but maybe, it would be great to add support for different key formats, so we have out-of-the-box conversion when verifying, say:
jwt.verify(token, PEM_PUBLIC, { format: 'PKCS8', algorithms: ['RS256'], ignoreExpiration: true });
I had this issue with the error message
[Error: PEM_read_bio_PUBKEY failed]
and it turned out to be a simple issue of having inlined the string for the key and prepending it with a\n
. Just making sure the very first character was a-
solved the issue for me.thanks so much @chekkan and @mattdknapp!
i spent hours trying to figure out how to validate an
RS256
token generated by keycloak, and this thread saved me big time!I got the same error and problem was a sign error. I’ve used node-rsa to generate the keys like this:
The important thing is
scheme: 'pkcs1'
.Now you are able to use with no problem the keys on jwt sign and verify. Don’t forget that sign as to use the private key and verify the public key.
jwt.sign
jwt.verify
jwt.verify(token, PEM_PUBLIC, {algorithms: ['RS256'], ignoreExpiration: true}...
From @andresmatasuarez
That was the solution for me signing the JWT using RS256. After that conversion it was successfully signed
I guess this is a bit old, but there actually is a fix for this. I ran into this myself on Mavericks. The issue is the version of OpenSSL installed with Mavericks (and older versions of OSX); the man pages would have you believe that everything is dandy, but it isn’t. You either need to update OpenSSL using Homebrew (
brew install openssl
, I believe) or if you don’t use Homebrew, you need to upgrade to the latest version here: http://www.openssh.com/portable.htmlUpgrading the underlying binaries solved the issue. This is not a problem with the JSONWebToken library; essentially, the dependencies end up relying on system binaries to perform key manipulation. Apple just needs to update their stuff.
I really hope this helps someone. As a bonus, upgrading means that you can actually use ssh-keygen to read, generate, and convert ECDSA keys.