Node-jsonwebtoken: error:04075070:rsa routines:RSA_sign:digest too big for rsa key

Created on 18 Aug 2020  Â·  6Comments  Â·  Source: auth0/node-jsonwebtoken

Description

I can get HS512 to work, but when I switch to RS512, I get error:04075070:rsa routines:RSA_sign:digest too big for rsa key. I get the same error whether using RS256 or RS512.

Reproduction

My code is this:

const jwt = require("jsonwebtoken");

const token = jwt.sign({ sub: 1 }, "...", { expiresIn: '7d', algorithm: "RS512" });

Environment

  • Version of this library used: 8.5.1
  • Version of the platform or framework used, if applicable:
  • Other relevant versions (language, server software, OS, browser): Node in Windows
  • Other modules/plugins/libraries that might be involved: express-jwt

Most helpful comment

Your RSA key is of insufficient bit length. 2048 bits is minimum required by the JWA specification.

Furthermore, don't sign with HS* using an RSA key :)

All 6 comments

Your RSA key is of insufficient bit length. 2048 bits is minimum required by the JWA specification.

Furthermore, don't sign with HS* using an RSA key :)

Actually I'm getting the exact same issue (Node 15, Ubuntu)

Here's an example with a random private key:

```-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAKrNEPQdc2yJURauXDa2Yu2KLj6sHkM3EDMWrHEC/4RKdrMt9qFl
Hm11YoQRYIm2v/mtdBjrg+FD+MnaEtChYFkCAwEAAQJAX2YsqvzsJFpKVW3MCa8z
cFP24oCGYqqVvMNUS8cN6tgzm5XBZIuc7c2NQ+XzG7xGmvkklbTtKGCkBlrJHJ8L
AQIhAPfjw4kaK1RPga10Cc/+s4QGIRJUPsICBwVY2fFUDMk5AiEAsGOil+vUDr35
yMKod0NwCbUubukGHX/x6fxyY38G8CECIFtjM7Y62JiHizZIHkn5C3Nz0wNv3Ycm
ujHldMu8gc45AiEAg+6SP+WxoDEycpo+na4AUBKHn4eF/myjiis7d/YcP2ECIFnv
bdO+HrfCgkAcbMw0d2pEisYXfZWkAW8q9NJ4OyMp
-----END RSA PRIVATE KEY-----

yields:
Error: error:04075070:rsa routines:RSA_sign:digest too big for rsa key

Stack trace:

at Sign.sign (node:internal/crypto/sig:124:29)
aebc7411959c_web | at Object.sign (/srv/www/node_modules/jwa/index.js:152:45)
aebc7411959c_web | at Object.jwsSign [as sign] (/srv/www/node_modules/jws/lib/sign-stream.js:32:24)
aebc7411959c_web | at Object.module.exports [as sign] (/srv/www/node_modules/jsonwebtoken/sign.js:204:16)
aebc7411959c_web | at /srv/www/main.js:101:35
aebc7411959c_web | at Query. (/srv/www/db.js:45:17)
aebc7411959c_web | at Query. (/srv/www/node_modules/mysql/lib/Connection.js:526:10)
aebc7411959c_web | at Query._callback (/srv/www/node_modules/mysql/lib/Connection.js:488:16)
aebc7411959c_web | at Query.Sequence.end (/srv/www/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
aebc7411959c_web | at Query._handleFinalResultPacket (/srv/www/node_modules/mysql/lib/protocol/sequences/Query.js:149:8) {

So I'm assuming it may be rather a nodejs bug ?

const token = jwt.sign( tokenPayload, myprivateKey, { algorithm: 'RS512', audience: req.params.uid } );
```

So I'm assuming it may be rather a nodejs bug ?

What makes you assume that? Your random private key's modulus length is 512 bits. Insufficient. JWA specification mandates a minimum of 2048. Your issue is the same as the OP's and so should be the answer.

Hmmm so I can make sure I understand (as I don't know anything about cryptography), what is the relation (if any) between the algorithm (RS512, RS256) I would choose and the modulus length ?

I can sign my token with, say, the RS256 algorithm and a modulus of 2048, if I wanted ? That is, they don't need to match

modulus is one of the components of your RSA private key, its length must be 2048 bits to be universally usable with JOSE. Your key's bit length is 512, generate a key of size 2048 and you'll be able to use all RSA based signing algorithms (it doesn't matter what the algorithm's "number" is since that only dictates the hash algorithm).


Example random 2048 bit RSA private key (click to expand)

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKc4ClmCAmAf2
ud82uQDfr+jh++ni/iH91TmU8eRJiULHnqE7Jjg75hNz1ZqYevQiVyKaOuunRhAI
lo7Prr4oJQIoFjsM9yONgnLnEk7bLOF3+RZRh2oGT4AqqK6qpgLsze7VPjot0BlK
pL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4Jnla/2WaNAoGAd+hD
BjUz0fh+qov//8zJoUr1SNuVJGnxiSrWf6/mu3lJJO8FKbpnPJmzpeVifelVFghf
yQPERLBkqayH+Ig09a6RhPA1e7XRSJaJhzTaUtey5gBzRQqZ3Fls9ivf49d2+jpW
0i+SRMHSmmZMegWmBeAaYshS9v0hrtIRlxoJVZkCgYEAwFWnUP2qSA6lSyB8d0PL
i88xXdXdlh3hWSbZ2XnhmmLJ5ZgrHfLimrkLQiOSYEmaJvilzw4UDTcjU7R3D+9t
wsgrYg8vC4KY0iMe2GKfbweli7ujmp719ZpiaVre+ZSn0q1k0dijW0m51zyUmz5p
JByfO2CsSg9+oVkM1jbYPKk=
-----END PRIVATE KEY-----

Okaaay that certainly explains a lot. Thank you very much !

On Mon, 18 Jan 2021, 20:54 Filip Skokan, notifications@github.com wrote:

modulus is one of the components of your RSA private key, its length must
be 2048 to be universally usable with JOSE. Your key's bit length is 512,
generate a key of size 2048 and you'll be able to use all RSA based signing
algorithms (it doesn't matter what the algorithm's "number" is since that
only dictates the hash algorithm).
Example random 2048 bit RSA private key (click to expand)

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKc4ClmCAmAf2
ud82uQDfr+jh++ni/iH91TmU8eRJiULHnqE7Jjg75hNz1ZqYevQiVyKaOuunRhAI
lo7Prr4oJQIoFjsM9yONgnLnEk7bLOF3+RZRh2oGT4AqqK6qpgLsze7VPjot0BlK
pLB0mxU+Q+8otj45lmelGvN5a5/cdE0n0bIiRVMWfKVVt948oJHDKZbS7IfEM/Ya
w+XAuIyTq/AQFAmHiXMQXmExy/LWlfgX2SC10Uq5qJTPlcfDueCMlUbR8QPDcZTn
HZiWc5yxN8OLTcBieUgvNUdE1kUUqlDuHcLtql2e9qTCpKCPTdEW0kSr3OiI/YnK
x1Pf95rxAgMBAAECggEAeH+fyPMl4jZpTEqZxrXEouQBdchcQEZCFyXatQgCqR1o
KAr9/XAi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4UDTcjU7R3D+9t
wsgrYg8vC4KY0iMe2GKfbweli7ujmp719ZpiaVre+ZSn0q1k0dijW0m51zyUmz5p
JByfO2CsSg9+oVkM1jbYPKk=
-----END PRIVATE KEY-----

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/auth0/node-jsonwebtoken/issues/739#issuecomment-762443789,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AALKLW4VZGV3THP6R47TLEDS2SGV5ANCNFSM4QDXVJZQ
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samholmes picture samholmes  Â·  5Comments

p-brighenti picture p-brighenti  Â·  4Comments

shea256 picture shea256  Â·  3Comments

ngminhduong picture ngminhduong  Â·  3Comments

AndreOneti picture AndreOneti  Â·  3Comments