2021-06-17 23:56:46 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 23:56:46 +02:00
|
|
|
|
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-08-25 22:50:38 +02:00
|
|
|
"crypto/ed25519"
|
2021-06-17 23:56:46 +02:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
2023-07-19 11:57:10 +02:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2021-06-17 23:56:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInvalidAlgorithmType represents an invalid algorithm error.
|
|
|
|
type ErrInvalidAlgorithmType struct {
|
2022-08-09 03:53:40 +02:00
|
|
|
Algorithm string
|
2021-06-17 23:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrInvalidAlgorithmType) Error() string {
|
2022-08-09 03:53:40 +02:00
|
|
|
return fmt.Sprintf("JWT signing algorithm is not supported: %s", err.Algorithm)
|
2021-06-17 23:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// JWTSigningKey represents a algorithm/key pair to sign JWTs
|
|
|
|
type JWTSigningKey interface {
|
|
|
|
IsSymmetric() bool
|
|
|
|
SigningMethod() jwt.SigningMethod
|
2023-07-04 20:36:08 +02:00
|
|
|
SignKey() any
|
|
|
|
VerifyKey() any
|
2021-06-17 23:56:46 +02:00
|
|
|
ToJWK() (map[string]string, error)
|
|
|
|
PreProcessToken(*jwt.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
type hmacSigningKey struct {
|
|
|
|
signingMethod jwt.SigningMethod
|
|
|
|
secret []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key hmacSigningKey) IsSymmetric() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key hmacSigningKey) SigningMethod() jwt.SigningMethod {
|
|
|
|
return key.signingMethod
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key hmacSigningKey) SignKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.secret
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key hmacSigningKey) VerifyKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.secret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key hmacSigningKey) ToJWK() (map[string]string, error) {
|
|
|
|
return map[string]string{
|
|
|
|
"kty": "oct",
|
|
|
|
"alg": key.SigningMethod().Alg(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key hmacSigningKey) PreProcessToken(*jwt.Token) {}
|
|
|
|
|
|
|
|
type rsaSingingKey struct {
|
|
|
|
signingMethod jwt.SigningMethod
|
|
|
|
key *rsa.PrivateKey
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRSASingingKey(signingMethod jwt.SigningMethod, key *rsa.PrivateKey) (rsaSingingKey, error) {
|
2023-05-12 19:27:50 +02:00
|
|
|
kid, err := util.CreatePublicKeyFingerprint(key.Public().(*rsa.PublicKey))
|
2021-06-17 23:56:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return rsaSingingKey{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsaSingingKey{
|
|
|
|
signingMethod,
|
|
|
|
key,
|
|
|
|
base64.RawURLEncoding.EncodeToString(kid),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key rsaSingingKey) IsSymmetric() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key rsaSingingKey) SigningMethod() jwt.SigningMethod {
|
|
|
|
return key.signingMethod
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key rsaSingingKey) SignKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.key
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key rsaSingingKey) VerifyKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.key.Public()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key rsaSingingKey) ToJWK() (map[string]string, error) {
|
|
|
|
pubKey := key.key.Public().(*rsa.PublicKey)
|
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"kty": "RSA",
|
|
|
|
"alg": key.SigningMethod().Alg(),
|
|
|
|
"kid": key.id,
|
|
|
|
"e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(pubKey.E)).Bytes()),
|
|
|
|
"n": base64.RawURLEncoding.EncodeToString(pubKey.N.Bytes()),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key rsaSingingKey) PreProcessToken(token *jwt.Token) {
|
|
|
|
token.Header["kid"] = key.id
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:50:38 +02:00
|
|
|
type eddsaSigningKey struct {
|
|
|
|
signingMethod jwt.SigningMethod
|
|
|
|
key ed25519.PrivateKey
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEdDSASingingKey(signingMethod jwt.SigningMethod, key ed25519.PrivateKey) (eddsaSigningKey, error) {
|
2023-05-12 19:27:50 +02:00
|
|
|
kid, err := util.CreatePublicKeyFingerprint(key.Public().(ed25519.PublicKey))
|
2021-08-25 22:50:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return eddsaSigningKey{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return eddsaSigningKey{
|
|
|
|
signingMethod,
|
|
|
|
key,
|
|
|
|
base64.RawURLEncoding.EncodeToString(kid),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key eddsaSigningKey) IsSymmetric() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key eddsaSigningKey) SigningMethod() jwt.SigningMethod {
|
|
|
|
return key.signingMethod
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key eddsaSigningKey) SignKey() any {
|
2021-08-25 22:50:38 +02:00
|
|
|
return key.key
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key eddsaSigningKey) VerifyKey() any {
|
2021-08-25 22:50:38 +02:00
|
|
|
return key.key.Public()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key eddsaSigningKey) ToJWK() (map[string]string, error) {
|
|
|
|
pubKey := key.key.Public().(ed25519.PublicKey)
|
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"alg": key.SigningMethod().Alg(),
|
|
|
|
"kid": key.id,
|
|
|
|
"kty": "OKP",
|
|
|
|
"crv": "Ed25519",
|
|
|
|
"x": base64.RawURLEncoding.EncodeToString(pubKey),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key eddsaSigningKey) PreProcessToken(token *jwt.Token) {
|
|
|
|
token.Header["kid"] = key.id
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:56:46 +02:00
|
|
|
type ecdsaSingingKey struct {
|
|
|
|
signingMethod jwt.SigningMethod
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newECDSASingingKey(signingMethod jwt.SigningMethod, key *ecdsa.PrivateKey) (ecdsaSingingKey, error) {
|
2023-05-12 19:27:50 +02:00
|
|
|
kid, err := util.CreatePublicKeyFingerprint(key.Public().(*ecdsa.PublicKey))
|
2021-06-17 23:56:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return ecdsaSingingKey{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ecdsaSingingKey{
|
|
|
|
signingMethod,
|
|
|
|
key,
|
|
|
|
base64.RawURLEncoding.EncodeToString(kid),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key ecdsaSingingKey) IsSymmetric() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key ecdsaSingingKey) SigningMethod() jwt.SigningMethod {
|
|
|
|
return key.signingMethod
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key ecdsaSingingKey) SignKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.key
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func (key ecdsaSingingKey) VerifyKey() any {
|
2021-06-17 23:56:46 +02:00
|
|
|
return key.key.Public()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
|
|
|
|
pubKey := key.key.Public().(*ecdsa.PublicKey)
|
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"kty": "EC",
|
|
|
|
"alg": key.SigningMethod().Alg(),
|
|
|
|
"kid": key.id,
|
|
|
|
"crv": pubKey.Params().Name,
|
|
|
|
"x": base64.RawURLEncoding.EncodeToString(pubKey.X.Bytes()),
|
|
|
|
"y": base64.RawURLEncoding.EncodeToString(pubKey.Y.Bytes()),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key ecdsaSingingKey) PreProcessToken(token *jwt.Token) {
|
|
|
|
token.Header["kid"] = key.id
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:50:38 +02:00
|
|
|
// CreateJWTSigningKey creates a signing key from an algorithm / key pair.
|
2023-07-04 20:36:08 +02:00
|
|
|
func CreateJWTSigningKey(algorithm string, key any) (JWTSigningKey, error) {
|
2021-06-17 23:56:46 +02:00
|
|
|
var signingMethod jwt.SigningMethod
|
|
|
|
switch algorithm {
|
|
|
|
case "HS256":
|
|
|
|
signingMethod = jwt.SigningMethodHS256
|
|
|
|
case "HS384":
|
|
|
|
signingMethod = jwt.SigningMethodHS384
|
|
|
|
case "HS512":
|
|
|
|
signingMethod = jwt.SigningMethodHS512
|
|
|
|
|
|
|
|
case "RS256":
|
|
|
|
signingMethod = jwt.SigningMethodRS256
|
|
|
|
case "RS384":
|
|
|
|
signingMethod = jwt.SigningMethodRS384
|
|
|
|
case "RS512":
|
|
|
|
signingMethod = jwt.SigningMethodRS512
|
|
|
|
|
|
|
|
case "ES256":
|
|
|
|
signingMethod = jwt.SigningMethodES256
|
|
|
|
case "ES384":
|
|
|
|
signingMethod = jwt.SigningMethodES384
|
|
|
|
case "ES512":
|
|
|
|
signingMethod = jwt.SigningMethodES512
|
2021-08-25 22:50:38 +02:00
|
|
|
case "EdDSA":
|
|
|
|
signingMethod = jwt.SigningMethodEdDSA
|
2021-06-17 23:56:46 +02:00
|
|
|
default:
|
|
|
|
return nil, ErrInvalidAlgorithmType{algorithm}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch signingMethod.(type) {
|
2021-08-25 22:50:38 +02:00
|
|
|
case *jwt.SigningMethodEd25519:
|
|
|
|
privateKey, ok := key.(ed25519.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ErrInvalidKeyType
|
|
|
|
}
|
|
|
|
return newEdDSASingingKey(signingMethod, privateKey)
|
2021-06-17 23:56:46 +02:00
|
|
|
case *jwt.SigningMethodECDSA:
|
|
|
|
privateKey, ok := key.(*ecdsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ErrInvalidKeyType
|
|
|
|
}
|
|
|
|
return newECDSASingingKey(signingMethod, privateKey)
|
|
|
|
case *jwt.SigningMethodRSA:
|
|
|
|
privateKey, ok := key.(*rsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ErrInvalidKeyType
|
|
|
|
}
|
|
|
|
return newRSASingingKey(signingMethod, privateKey)
|
|
|
|
default:
|
|
|
|
secret, ok := key.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ErrInvalidKeyType
|
|
|
|
}
|
|
|
|
return hmacSigningKey{signingMethod, secret}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultSigningKey is the default signing key for JWTs.
|
|
|
|
var DefaultSigningKey JWTSigningKey
|
|
|
|
|
|
|
|
// InitSigningKey creates the default signing key from settings or creates a random key.
|
|
|
|
func InitSigningKey() error {
|
|
|
|
var err error
|
2023-07-04 20:36:08 +02:00
|
|
|
var key any
|
2021-06-17 23:56:46 +02:00
|
|
|
|
|
|
|
switch setting.OAuth2.JWTSigningAlgorithm {
|
|
|
|
case "HS256":
|
|
|
|
fallthrough
|
|
|
|
case "HS384":
|
|
|
|
fallthrough
|
|
|
|
case "HS512":
|
2023-04-25 17:06:39 +02:00
|
|
|
key, err = loadSymmetricKey()
|
2021-06-17 23:56:46 +02:00
|
|
|
case "RS256":
|
|
|
|
fallthrough
|
|
|
|
case "RS384":
|
|
|
|
fallthrough
|
|
|
|
case "RS512":
|
|
|
|
fallthrough
|
|
|
|
case "ES256":
|
|
|
|
fallthrough
|
|
|
|
case "ES384":
|
|
|
|
fallthrough
|
|
|
|
case "ES512":
|
2021-08-25 22:50:38 +02:00
|
|
|
fallthrough
|
|
|
|
case "EdDSA":
|
2021-06-17 23:56:46 +02:00
|
|
|
key, err = loadOrCreateAsymmetricKey()
|
|
|
|
default:
|
|
|
|
return ErrInvalidAlgorithmType{setting.OAuth2.JWTSigningAlgorithm}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("Error while loading or creating JWT key: %w", err)
|
2021-06-17 23:56:46 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 22:50:38 +02:00
|
|
|
signingKey, err := CreateJWTSigningKey(setting.OAuth2.JWTSigningAlgorithm, key)
|
2021-06-17 23:56:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultSigningKey = signingKey
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 17:06:39 +02:00
|
|
|
// loadSymmetricKey checks if the configured secret is valid.
|
|
|
|
// If it is not valid, it will return an error.
|
2023-07-04 20:36:08 +02:00
|
|
|
func loadSymmetricKey() (any, error) {
|
2023-08-14 12:30:16 +02:00
|
|
|
return util.Base64FixedDecode(base64.RawURLEncoding, []byte(setting.OAuth2.JWTSecretBase64), 32)
|
2021-06-17 23:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadOrCreateAsymmetricKey checks if the configured private key exists.
|
|
|
|
// If it does not exist a new random key gets generated and saved on the configured path.
|
2023-07-04 20:36:08 +02:00
|
|
|
func loadOrCreateAsymmetricKey() (any, error) {
|
2021-06-17 23:56:46 +02:00
|
|
|
keyPath := setting.OAuth2.JWTSigningPrivateKeyFile
|
|
|
|
|
|
|
|
isExist, err := util.IsExist(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to check if %s exists. Error: %v", keyPath, err)
|
|
|
|
}
|
|
|
|
if !isExist {
|
|
|
|
err := func() error {
|
2023-07-04 20:36:08 +02:00
|
|
|
key, err := func() (any, error) {
|
2021-08-25 22:50:38 +02:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(setting.OAuth2.JWTSigningAlgorithm, "RS"):
|
2021-06-17 23:56:46 +02:00
|
|
|
return rsa.GenerateKey(rand.Reader, 4096)
|
2021-08-25 22:50:38 +02:00
|
|
|
case setting.OAuth2.JWTSigningAlgorithm == "EdDSA":
|
|
|
|
_, pk, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
return pk, err
|
|
|
|
default:
|
|
|
|
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2021-06-17 23:56:46 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := x509.MarshalPKCS8PrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeyPEM := &pem.Block{Type: "PRIVATE KEY", Bytes: bytes}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(keyPath), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
|
2021-06-17 23:56:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
log.Error("Close: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pem.Encode(f, privateKeyPEM)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error generating private key: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 07:38:34 +02:00
|
|
|
bytes, err := os.ReadFile(keyPath)
|
2021-06-17 23:56:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode(bytes)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no valid PEM data found in %s", keyPath)
|
|
|
|
} else if block.Type != "PRIVATE KEY" {
|
|
|
|
return nil, fmt.Errorf("expected PRIVATE KEY, got %s in %s", block.Type, keyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParsePKCS8PrivateKey(block.Bytes)
|
|
|
|
}
|