1

I'm using https://github.com/dgrijalva/jwt-go to build a JWT using a 256-bit private PEM key. I'm using SigningMethodRS256 to sign the JWT:

signBytes, _ := ioutil.ReadFile(privKeyPath)
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(signBytes)
token := jwt.NewWithClaims(jwt.SigningMethodRS256, middleware.CognitoAccessTokenClaim{
    CustomArray:  []string{"testString"},
    StandardClaims: jwt.StandardClaims{
    ExpiresAt: 1500,
    },
})
jwtString, err := token.SignedString(signKey)

On the last line, I get an error when signing the jwt: crypto/rsa: message too long for RSA public key size. Does anyone know what causes this? The size of the pem file seems correct.

Grokify
  • 15,092
  • 6
  • 60
  • 81
  • 5
    256 bits is a very small key size. Is there a reason for that? You can solve this just by generating a larger key (2048 or 4096 bits). See https://github.com/dgrijalva/jwt-go/issues/213. – chash Jun 12 '20 at 17:03

2 Answers2

4

You need split message to chunks

func EncryptOAEP(hash hash.Hash, random io.Reader, public *rsa.PublicKey, msg []byte, label []byte) ([]byte, error) {
    msgLen := len(msg)
    step := public.Size() - 2*hash.Size() - 2
    var encryptedBytes []byte

    for start := 0; start < msgLen; start += step {
        finish := start + step
        if finish > msgLen {
            finish = msgLen
        }

        encryptedBlockBytes, err := rsa.EncryptOAEP(hash, random, public, msg[start:finish], label)
        if err != nil {
            return nil, err
        }

        encryptedBytes = append(encryptedBytes, encryptedBlockBytes...)
    }

    return encryptedBytes, nil
}

func DecryptOAEP(hash hash.Hash, random io.Reader, private *rsa.PrivateKey, msg []byte, label []byte) ([]byte, error) {
    msgLen := len(msg)
    step := private.PublicKey.Size()
    var decryptedBytes []byte

    for start := 0; start < msgLen; start += step {
        finish := start + step
        if finish > msgLen {
            finish = msgLen
        }

        decryptedBlockBytes, err := rsa.DecryptOAEP(hash, random, private, msg[start:finish], label)
        if err != nil {
            return nil, err
        }

        decryptedBytes = append(decryptedBytes, decryptedBlockBytes...)
    }

    return decryptedBytes, nil
}
Ninazu
  • 151
  • 6
0

Maybe the way you generated your private key is not correct. I resolved the same issue by taking reference from here

Steps to generate key

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub

Steps to use it using jwt-go

package main

import (
    "fmt"
    "github.com/dgrijalva/jwt-go"
    "io/ioutil"
    "time"
)

func panicOnError(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    signBytes, err := ioutil.ReadFile("./jwtRS256.key")
    panicOnError(err)
    signKey, err := jwt.ParseRSAPrivateKeyFromPEM(signBytes)
    panicOnError(err)
    verifyBytes, err := ioutil.ReadFile("./jwtRS256.key.pub")
    panicOnError(err)
    verifyKey, err := jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
    panicOnError(err)
    claims := jwt.MapClaims{
        "exp": time.Now().Add(time.Minute).Unix(),
    }
    fmt.Println(claims)
    t := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
    tokenString, err := t.SignedString(signKey)
    panicOnError(err)
    fmt.Println(tokenString)
    token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
        return verifyKey, nil
    })
    panicOnError(err)
    fmt.Println(token.Claims)
}
aquaman
  • 1,523
  • 5
  • 20
  • 39