|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2024 TTBT Enterprises LLC |
| 4 | +// Copyright (c) 2024 Robin Thellend <rthellend@rthellend.com> |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | + |
| 24 | +// Package jwks implements a JSON Web Key Set (JWKS) management system. |
| 25 | +package jwks |
| 26 | + |
| 27 | +import ( |
| 28 | + "crypto" |
| 29 | + "crypto/ecdsa" |
| 30 | + "crypto/ed25519" |
| 31 | + "crypto/elliptic" |
| 32 | + "crypto/rsa" |
| 33 | + "crypto/sha256" |
| 34 | + "crypto/x509" |
| 35 | + "encoding/base64" |
| 36 | + "encoding/hex" |
| 37 | + "fmt" |
| 38 | + "math/big" |
| 39 | +) |
| 40 | + |
| 41 | +// JWKS is a JSON Web Key Set. |
| 42 | +type JWKS struct { |
| 43 | + Keys []JWK `json:"keys"` |
| 44 | +} |
| 45 | + |
| 46 | +// JWK is a JSON Web Key. |
| 47 | +type JWK struct { |
| 48 | + Type string `json:"kty"` |
| 49 | + Use string `json:"use"` |
| 50 | + ID string `json:"kid"` |
| 51 | + Alg string `json:"alg"` |
| 52 | + // EC |
| 53 | + Curve string `json:"crv,omitempty"` |
| 54 | + X string `json:"x,omitempty"` |
| 55 | + Y string `json:"y,omitempty"` |
| 56 | + // RSA |
| 57 | + N string `json:"n,omitempty"` |
| 58 | + E string `json:"e,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +// PublicKey returns the crypto.PublicKey from the JWK. |
| 62 | +func (k JWK) PublicKey() (crypto.PublicKey, error) { |
| 63 | + switch k.Type { |
| 64 | + case "EC": |
| 65 | + var curve elliptic.Curve |
| 66 | + switch k.Curve { |
| 67 | + case "P-256": |
| 68 | + curve = elliptic.P256() |
| 69 | + case "P-384": |
| 70 | + curve = elliptic.P384() |
| 71 | + case "P-521": |
| 72 | + curve = elliptic.P521() |
| 73 | + default: |
| 74 | + return nil, fmt.Errorf("unsupported EC curve %q", k.Curve) |
| 75 | + } |
| 76 | + x, err := base64.RawURLEncoding.DecodeString(k.X) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + y, err := base64.RawURLEncoding.DecodeString(k.Y) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + return &ecdsa.PublicKey{Curve: curve, X: new(big.Int).SetBytes(x), Y: new(big.Int).SetBytes(y)}, nil |
| 85 | + case "RSA": |
| 86 | + n, err := base64.RawURLEncoding.DecodeString(k.N) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + eBytes, err := base64.RawURLEncoding.DecodeString(k.E) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + return &rsa.PublicKey{N: new(big.Int).SetBytes(n), E: int(new(big.Int).SetBytes(eBytes).Int64())}, nil |
| 95 | + case "OKP": // EdDSA |
| 96 | + x, err := base64.RawURLEncoding.DecodeString(k.X) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + return ed25519.PublicKey(x), nil |
| 101 | + default: |
| 102 | + return nil, fmt.Errorf("unknown key type %q", k.Type) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// PublicKeyToJWK converts a crypto.PublicKey to a JWK. |
| 107 | +func PublicKeyToJWK(pub crypto.PublicKey) *JWK { |
| 108 | + var jwk JWK |
| 109 | + switch pub := pub.(type) { |
| 110 | + case *ecdsa.PublicKey: |
| 111 | + var alg, crv string |
| 112 | + switch pub.Curve { |
| 113 | + case elliptic.P256(): |
| 114 | + alg = "ES256" |
| 115 | + crv = "P-256" |
| 116 | + case elliptic.P384(): |
| 117 | + alg = "ES384" |
| 118 | + crv = "P-384" |
| 119 | + case elliptic.P521(): |
| 120 | + alg = "ES512" |
| 121 | + crv = "P-521" |
| 122 | + default: |
| 123 | + return nil |
| 124 | + } |
| 125 | + size := (pub.Curve.Params().BitSize + 7) / 8 |
| 126 | + xBytes := make([]byte, size) |
| 127 | + yBytes := make([]byte, size) |
| 128 | + pub.X.FillBytes(xBytes) |
| 129 | + pub.Y.FillBytes(yBytes) |
| 130 | + jwk = JWK{ |
| 131 | + Type: "EC", |
| 132 | + Use: "sig", |
| 133 | + Alg: alg, |
| 134 | + Curve: crv, |
| 135 | + X: base64.RawURLEncoding.EncodeToString(xBytes), |
| 136 | + Y: base64.RawURLEncoding.EncodeToString(yBytes), |
| 137 | + } |
| 138 | + case ed25519.PublicKey: |
| 139 | + jwk = JWK{ |
| 140 | + Type: "OKP", |
| 141 | + Use: "sig", |
| 142 | + Alg: "EdDSA", |
| 143 | + Curve: "Ed25519", |
| 144 | + X: base64.RawURLEncoding.EncodeToString(pub), |
| 145 | + } |
| 146 | + case *rsa.PublicKey: |
| 147 | + jwk = JWK{ |
| 148 | + Type: "RSA", |
| 149 | + Use: "sig", |
| 150 | + Alg: "RS256", |
| 151 | + N: base64.RawURLEncoding.EncodeToString(pub.N.Bytes()), |
| 152 | + E: base64.RawURLEncoding.EncodeToString(big.NewInt(int64(pub.E)).Bytes()), |
| 153 | + } |
| 154 | + default: |
| 155 | + return nil |
| 156 | + } |
| 157 | + // Calculate Key ID (kid) |
| 158 | + b, err := x509.MarshalPKIXPublicKey(pub) |
| 159 | + if err == nil { |
| 160 | + sum := sha256.Sum256(b) |
| 161 | + jwk.ID = hex.EncodeToString(sum[:])[:16] |
| 162 | + } |
| 163 | + return &jwk |
| 164 | +} |
| 165 | + |
| 166 | +// New returns a new JWKS from the provided public keys. |
| 167 | +// Note: RSA keys default to alg:"RS256", which may not be correct, and might need to be updated. |
| 168 | +func New(keys []crypto.PublicKey) *JWKS { |
| 169 | + var out JWKS |
| 170 | + for _, pub := range keys { |
| 171 | + if k := PublicKeyToJWK(pub); k != nil { |
| 172 | + out.Keys = append(out.Keys, *k) |
| 173 | + } |
| 174 | + } |
| 175 | + return &out |
| 176 | +} |
0 commit comments