3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 05:19:25 +01:00
ergo/vendor/github.com/golang-jwt/jwt/v5/rsa_pss.go

136 lines
3.4 KiB
Go
Raw Normal View History

//go:build go1.4
2020-06-12 23:04:36 +02:00
// +build go1.4
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
)
// SigningMethodRSAPSS implements the RSAPSS family of signing methods signing methods
2020-06-12 23:04:36 +02:00
type SigningMethodRSAPSS struct {
*SigningMethodRSA
Options *rsa.PSSOptions
2021-07-29 22:48:58 +02:00
// VerifyOptions is optional. If set overrides Options for rsa.VerifyPPS.
// Used to accept tokens signed with rsa.PSSSaltLengthAuto, what doesn't follow
// https://tools.ietf.org/html/rfc7518#section-3.5 but was used previously.
// See https://github.com/dgrijalva/jwt-go/issues/285#issuecomment-437451244 for details.
VerifyOptions *rsa.PSSOptions
2020-06-12 23:04:36 +02:00
}
2021-07-29 22:48:58 +02:00
// Specific instances for RS/PS and company.
2020-06-12 23:04:36 +02:00
var (
SigningMethodPS256 *SigningMethodRSAPSS
SigningMethodPS384 *SigningMethodRSAPSS
SigningMethodPS512 *SigningMethodRSAPSS
)
func init() {
// PS256
SigningMethodPS256 = &SigningMethodRSAPSS{
2021-07-29 22:48:58 +02:00
SigningMethodRSA: &SigningMethodRSA{
2020-06-12 23:04:36 +02:00
Name: "PS256",
Hash: crypto.SHA256,
},
2021-07-29 22:48:58 +02:00
Options: &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
},
VerifyOptions: &rsa.PSSOptions{
2020-06-12 23:04:36 +02:00
SaltLength: rsa.PSSSaltLengthAuto,
},
}
RegisterSigningMethod(SigningMethodPS256.Alg(), func() SigningMethod {
return SigningMethodPS256
})
// PS384
SigningMethodPS384 = &SigningMethodRSAPSS{
2021-07-29 22:48:58 +02:00
SigningMethodRSA: &SigningMethodRSA{
2020-06-12 23:04:36 +02:00
Name: "PS384",
Hash: crypto.SHA384,
},
2021-07-29 22:48:58 +02:00
Options: &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
},
VerifyOptions: &rsa.PSSOptions{
2020-06-12 23:04:36 +02:00
SaltLength: rsa.PSSSaltLengthAuto,
},
}
RegisterSigningMethod(SigningMethodPS384.Alg(), func() SigningMethod {
return SigningMethodPS384
})
// PS512
SigningMethodPS512 = &SigningMethodRSAPSS{
2021-07-29 22:48:58 +02:00
SigningMethodRSA: &SigningMethodRSA{
2020-06-12 23:04:36 +02:00
Name: "PS512",
Hash: crypto.SHA512,
},
2021-07-29 22:48:58 +02:00
Options: &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
},
VerifyOptions: &rsa.PSSOptions{
2020-06-12 23:04:36 +02:00
SaltLength: rsa.PSSSaltLengthAuto,
},
}
RegisterSigningMethod(SigningMethodPS512.Alg(), func() SigningMethod {
return SigningMethodPS512
})
}
// Verify implements token verification for the SigningMethod.
2020-06-12 23:04:36 +02:00
// For this verify method, key must be an rsa.PublicKey struct
func (m *SigningMethodRSAPSS) Verify(signingString string, sig []byte, key interface{}) error {
2020-06-12 23:04:36 +02:00
var rsaKey *rsa.PublicKey
switch k := key.(type) {
case *rsa.PublicKey:
rsaKey = k
default:
return newError("RSA-PSS verify expects *rsa.PublicKey", ErrInvalidKeyType)
2020-06-12 23:04:36 +02:00
}
// Create hasher
if !m.Hash.Available() {
return ErrHashUnavailable
}
hasher := m.Hash.New()
hasher.Write([]byte(signingString))
2021-07-29 22:48:58 +02:00
opts := m.Options
if m.VerifyOptions != nil {
opts = m.VerifyOptions
}
return rsa.VerifyPSS(rsaKey, m.Hash, hasher.Sum(nil), sig, opts)
2020-06-12 23:04:36 +02:00
}
// Sign implements token signing for the SigningMethod.
2020-06-12 23:04:36 +02:00
// For this signing method, key must be an rsa.PrivateKey struct
func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) ([]byte, error) {
2020-06-12 23:04:36 +02:00
var rsaKey *rsa.PrivateKey
switch k := key.(type) {
case *rsa.PrivateKey:
rsaKey = k
default:
return nil, newError("RSA-PSS sign expects *rsa.PrivateKey", ErrInvalidKeyType)
2020-06-12 23:04:36 +02:00
}
// Create the hasher
if !m.Hash.Available() {
return nil, ErrHashUnavailable
2020-06-12 23:04:36 +02:00
}
hasher := m.Hash.New()
hasher.Write([]byte(signingString))
// Sign the string and return the encoded bytes
if sigBytes, err := rsa.SignPSS(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil), m.Options); err == nil {
return sigBytes, nil
2020-06-12 23:04:36 +02:00
} else {
return nil, err
2020-06-12 23:04:36 +02:00
}
}