2022-05-28 02:11:16 +03:00
|
|
|
//go:build go1.4
|
2015-07-14 20:20:58 +03:00
|
|
|
// +build go1.4
|
|
|
|
|
|
|
|
package jwt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
2022-05-28 02:11:16 +03:00
|
|
|
"os"
|
2015-07-14 20:20:58 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-11-11 22:32:06 +03:00
|
|
|
"time"
|
2015-07-14 20:20:58 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/golang-jwt/jwt/v5/test"
|
2015-07-14 20:20:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var rsaPSSTestData = []struct {
|
|
|
|
name string
|
|
|
|
tokenString string
|
|
|
|
alg string
|
|
|
|
claims map[string]interface{}
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Basic PS256",
|
|
|
|
"eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIifQ.PPG4xyDVY8ffp4CcxofNmsTDXsrVG2npdQuibLhJbv4ClyPTUtR5giNSvuxo03kB6I8VXVr0Y9X7UxhJVEoJOmULAwRWaUsDnIewQa101cVhMa6iR8X37kfFoiZ6NkS-c7henVkkQWu2HtotkEtQvN5hFlk8IevXXPmvZlhQhwzB1sGzGYnoi1zOfuL98d3BIjUjtlwii5w6gYG2AEEzp7HnHCsb3jIwUPdq86Oe6hIFjtBwduIK90ca4UqzARpcfwxHwVLMpatKask00AgGVI0ysdk0BLMjmLutquD03XbThHScC2C2_Pp4cHWgMzvbgLU2RYYZcZRKr46QeNgz9w",
|
|
|
|
"PS256",
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Basic PS384",
|
|
|
|
"eyJhbGciOiJQUzM4NCIsInR5cCI6IkpXVCJ9.eyJmb28iOi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VG2npdQuibLhJbv4ClyPTUtR5giNSvuxo03kB6I8VXVr0Y9X7UxhJVEoJOmULAwRWaUsDnIewQa101cVhMa6iR8X37kfFoiZ6NkS-c7henVkkQWu2HtotkEtQvN5hFlk8IevXXPmvZlhQhwzB1sGzGYnoi1zOfuL98d3BIjUjtlwii5w6gYG2AEEzp7HnHCsb3jIwUPdq86Oe6hIFjtBwduIK90ca4UqzARpcfwxHwVLMpatKask00AgGVI0ysdk0BLMjmLutquD03XbThHScC2C2_Pp4cHWgMzvbgLU2RYYZcZRKr46QeNgz9W",
|
|
|
|
"PS256",
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRSAPSSVerify(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key.pub")
|
2015-07-14 20:20:58 +03:00
|
|
|
var rsaPSSKey *rsa.PublicKey
|
|
|
|
if rsaPSSKey, err = jwt.ParseRSAPublicKeyFromPEM(key); err != nil {
|
|
|
|
t.Errorf("Unable to parse RSA public key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, data := range rsaPSSTestData {
|
|
|
|
parts := strings.Split(data.tokenString, ".")
|
|
|
|
|
|
|
|
method := jwt.GetSigningMethod(data.alg)
|
2023-03-24 21:13:09 +03:00
|
|
|
err := method.Verify(strings.Join(parts[0:2], "."), decodeSegment(t, parts[2]), rsaPSSKey)
|
2015-07-14 20:20:58 +03:00
|
|
|
if data.valid && err != nil {
|
|
|
|
t.Errorf("[%v] Error while verifying key: %v", data.name, err)
|
|
|
|
}
|
|
|
|
if !data.valid && err == nil {
|
|
|
|
t.Errorf("[%v] Invalid key passed validation", data.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRSAPSSSign(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2015-07-14 20:20:58 +03:00
|
|
|
var rsaPSSKey *rsa.PrivateKey
|
|
|
|
if rsaPSSKey, err = jwt.ParseRSAPrivateKeyFromPEM(key); err != nil {
|
|
|
|
t.Errorf("Unable to parse RSA private key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, data := range rsaPSSTestData {
|
2023-07-18 09:44:48 +03:00
|
|
|
if !data.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(data.tokenString, ".")
|
|
|
|
method := jwt.GetSigningMethod(data.alg)
|
|
|
|
sig, err := method.Sign(strings.Join(parts[0:2], "."), rsaPSSKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%v] Error signing token: %v", data.name, err)
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
|
2023-07-18 09:44:48 +03:00
|
|
|
ssig := encodeSegment(sig)
|
|
|
|
if ssig == parts[2] {
|
|
|
|
t.Errorf("[%v] Signatures shouldn't match\nnew:\n%v\noriginal:\n%v", data.name, ssig, parts[2])
|
2015-07-14 20:20:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 22:32:06 +03:00
|
|
|
|
|
|
|
func TestRSAPSSSaltLengthCompatibility(t *testing.T) {
|
|
|
|
// Fails token verify, if salt length is auto.
|
|
|
|
ps256SaltLengthEqualsHash := &jwt.SigningMethodRSAPSS{
|
|
|
|
SigningMethodRSA: jwt.SigningMethodPS256.SigningMethodRSA,
|
|
|
|
Options: &rsa.PSSOptions{
|
|
|
|
SaltLength: rsa.PSSSaltLengthEqualsHash,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Behaves as before https://github.com/dgrijalva/jwt-go/issues/285 fix.
|
|
|
|
ps256SaltLengthAuto := &jwt.SigningMethodRSAPSS{
|
|
|
|
SigningMethodRSA: jwt.SigningMethodPS256.SigningMethodRSA,
|
|
|
|
Options: &rsa.PSSOptions{
|
|
|
|
SaltLength: rsa.PSSSaltLengthAuto,
|
|
|
|
},
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
if !verify(t, jwt.SigningMethodPS256, makeToken(ps256SaltLengthEqualsHash)) {
|
2018-11-11 22:32:06 +03:00
|
|
|
t.Error("SigningMethodPS256 should accept salt length that is defined in RFC")
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
if !verify(t, ps256SaltLengthEqualsHash, makeToken(jwt.SigningMethodPS256)) {
|
2018-11-11 22:32:06 +03:00
|
|
|
t.Error("Sign by SigningMethodPS256 should have salt length that is defined in RFC")
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
if !verify(t, jwt.SigningMethodPS256, makeToken(ps256SaltLengthAuto)) {
|
2018-11-11 22:32:06 +03:00
|
|
|
t.Error("SigningMethodPS256 should accept auto salt length to be compatible with previous versions")
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
if !verify(t, ps256SaltLengthAuto, makeToken(jwt.SigningMethodPS256)) {
|
2018-11-11 22:32:06 +03:00
|
|
|
t.Error("Sign by SigningMethodPS256 should be accepted by previous versions")
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
if verify(t, ps256SaltLengthEqualsHash, makeToken(ps256SaltLengthAuto)) {
|
2018-11-11 22:32:06 +03:00
|
|
|
t.Error("Auto salt length should be not accepted, when RFC salt length is required")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeToken(method jwt.SigningMethod) string {
|
2021-08-22 20:23:13 +03:00
|
|
|
token := jwt.NewWithClaims(method, jwt.RegisteredClaims{
|
2018-11-11 22:32:06 +03:00
|
|
|
Issuer: "example",
|
2021-08-22 20:23:13 +03:00
|
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
2018-11-11 22:32:06 +03:00
|
|
|
})
|
|
|
|
privateKey := test.LoadRSAPrivateKeyFromDisk("test/sample_key")
|
|
|
|
signed, err := token.SignedString(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return signed
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:13:09 +03:00
|
|
|
func verify(t *testing.T, signingMethod jwt.SigningMethod, token string) bool {
|
2018-11-11 22:32:06 +03:00
|
|
|
segments := strings.Split(token, ".")
|
2023-03-24 21:13:09 +03:00
|
|
|
err := signingMethod.Verify(strings.Join(segments[:2], "."), decodeSegment(t, segments[2]), test.LoadRSAPublicKeyFromDisk("test/sample_key.pub"))
|
2018-11-11 22:32:06 +03:00
|
|
|
return err == nil
|
|
|
|
}
|