| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/rsa" |
| 8 |
"errors" |
| 9 |
"strings" |
| 10 |
|
| 11 |
jose "github.com/go-jose/go-jose/v4" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
KeyUseSignature = "sig" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
ErrKeyMultiple = errors.New("multiple possible keys match") |
| 20 |
ErrKeyNone = errors.New("no possible keys matches") |
| 21 |
) |
| 22 |
|
| 23 |
// KeySet represents a set of JSON Web Keys |
| 24 |
// - remotely fetch via discovery and jwks_uri -> `remoteKeySet` |
| 25 |
// - held by the OP itself in storage -> `openIDKeySet` |
| 26 |
// - dynamically aggregated by request for OAuth JWT Profile Assertion -> `jwtProfileKeySet` |
| 27 |
type KeySet interface { |
| 28 |
// VerifySignature verifies the signature with the given keyset and returns the raw payload |
| 29 |
VerifySignature(ctx context.Context, jws *jose.JSONWebSignature) (payload []byte, err error) |
| 30 |
} |
| 31 |
|
| 32 |
// GetKeyIDAndAlg returns the `kid` and `alg` claim from the JWS header |
| 33 |
func GetKeyIDAndAlg(jws *jose.JSONWebSignature) (string, string) { |
| 34 |
keyID := "" |
| 35 |
alg := "" |
| 36 |
for _, sig := range jws.Signatures { |
| 37 |
keyID = sig.Header.KeyID |
| 38 |
alg = sig.Header.Algorithm |
| 39 |
break |
| 40 |
} |
| 41 |
return keyID, alg |
| 42 |
} |
| 43 |
|
| 44 |
// FindKey searches the given JSON Web Keys for the requested key ID, usage and key type |
| 45 |
// |
| 46 |
// will return the key immediately if matches exact (id, usage, type) |
| 47 |
// |
| 48 |
// will return false none or multiple match |
| 49 |
// |
| 50 |
// deprecated: use FindMatchingKey which will return an error (more specific) instead of just a bool |
| 51 |
// moved implementation already to FindMatchingKey |
| 52 |
func FindKey(keyID, use, expectedAlg string, keys ...jose.JSONWebKey) (jose.JSONWebKey, bool) { |
| 53 |
key, err := FindMatchingKey(keyID, use, expectedAlg, keys...) |
| 54 |
return key, err == nil |
| 55 |
} |
| 56 |
|
| 57 |
// FindMatchingKey searches the given JSON Web Keys for the requested key ID, usage and alg type |
| 58 |
// |
| 59 |
// will return the key immediately if matches exact (id, usage, type) |
| 60 |
// |
| 61 |
// will return a specific error if none (ErrKeyNone) or multiple (ErrKeyMultiple) match |
| 62 |
func FindMatchingKey(keyID, use, expectedAlg string, keys ...jose.JSONWebKey) (key jose.JSONWebKey, err error) { |
| 63 |
var validKeys []jose.JSONWebKey |
| 64 |
for _, k := range keys { |
| 65 |
// ignore all keys with wrong use (let empty use of published key pass) |
| 66 |
if k.Use != use && k.Use != "" { |
| 67 |
continue |
| 68 |
} |
| 69 |
// ignore all keys with wrong algorithm type |
| 70 |
if !algToKeyType(k.Key, expectedAlg) { |
| 71 |
continue |
| 72 |
} |
| 73 |
// if we get here, use and alg match, so an equal (not empty) keyID is an exact match |
| 74 |
if k.KeyID == keyID && keyID != "" { |
| 75 |
return k, nil |
| 76 |
} |
| 77 |
// keyIDs did not match or at least one was empty (if later, then it could be a match) |
| 78 |
if k.KeyID == "" || keyID == "" { |
| 79 |
validKeys = append(validKeys, k) |
| 80 |
} |
| 81 |
} |
| 82 |
// if we get here, no match was possible at all (use / alg) or no exact match due to |
| 83 |
// the signed JWT and / or the published keys didn't have a kid |
| 84 |
// if later applies and only one key could be found, we'll return it |
| 85 |
// otherwise a corresponding error will be thrown |
| 86 |
if len(validKeys) == 1 { |
| 87 |
return validKeys[0], nil |
| 88 |
} |
| 89 |
if len(validKeys) > 1 { |
| 90 |
return key, ErrKeyMultiple |
| 91 |
} |
| 92 |
return key, ErrKeyNone |
| 93 |
} |
| 94 |
|
| 95 |
func algToKeyType(key any, alg string) bool { |
| 96 |
if strings.HasPrefix(alg, "RS") || strings.HasPrefix(alg, "PS") { |
| 97 |
_, ok := key.(*rsa.PublicKey) |
| 98 |
return ok |
| 99 |
} |
| 100 |
if strings.HasPrefix(alg, "ES") { |
| 101 |
_, ok := key.(*ecdsa.PublicKey) |
| 102 |
return ok |
| 103 |
} |
| 104 |
if alg == string(jose.EdDSA) { |
| 105 |
_, ok := key.(ed25519.PublicKey) |
| 106 |
return ok |
| 107 |
} |
| 108 |
return false |
| 109 |
} |