javascriptnode.jsopensslcryptographyjson-web-token

nodejs jsonwebtoken with PEM file: error "PEM_read_bio:no start line"


I know there are a bunch of similar questions here but after hours of reading I cannot find a solution to mine. So:

I am trying to use jwk-to-pem for signing a JSON web token using jsonwebtoken. I have previously achieved this but now the same code does not work. After calling jwt.sign with the generated pem I get error:0906D06C:PEM routines:PEM_read_bio:no start line. I know that common reasons would include wrong linebreaks like \r or generally wrong format. I actually even dug through the core nodejs code and found the line throwing this error.

Countless adjustments and attempts brought no solution. Specifically:

What am I missing here? What possible causes I did not think of can this have?


Solution

  • I found a very simple explanation: I was by mistake using the public instead of the private key. The error thrown by openssl/crypto is very misleading.

    Using a private pem/key the signing works as expected.