| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"crypto/ecdsa" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/sha256" |
| 10 |
"encoding/base64" |
| 11 |
"encoding/json" |
| 12 |
"fmt" |
| 13 |
"reflect" |
| 14 |
|
| 15 |
jose "github.com/go-jose/go-jose/v4" |
| 16 |
) |
| 17 |
|
| 18 |
// Parameters, claims and helpers for OpenID Connect Key Binding 1.0, which |
| 19 |
// binds an ID Token to a proof-of-possession key using DPoP proofs. |
| 20 |
const ( |
| 21 |
// DPoPJKTParam is the authorization request parameter carrying the |
| 22 |
// base64url-encoded SHA-256 JWK thumbprint of the binding key. |
| 23 |
// |
| 24 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 25 |
// This API may change or be removed without a major version bump. |
| 26 |
DPoPJKTParam = "dpop_jkt" |
| 27 |
|
| 28 |
// DPoPHeader is the HTTP header name used to carry a DPoP proof. |
| 29 |
// |
| 30 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 31 |
// This API may change or be removed without a major version bump. |
| 32 |
DPoPHeader = "DPoP" |
| 33 |
|
| 34 |
// DPoPProofType is the DPoP proof's typ header. |
| 35 |
// |
| 36 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 37 |
// This API may change or be removed without a major version bump. |
| 38 |
DPoPProofType jose.ContentType = "dpop+jwt" |
| 39 |
|
| 40 |
// IDTokenTypeDPoP is the key bound ID Token typ header. |
| 41 |
// |
| 42 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 43 |
// This API may change or be removed without a major version bump. |
| 44 |
IDTokenTypeDPoP jose.ContentType = "dpop+id_token" |
| 45 |
) |
| 46 |
|
| 47 |
// Confirmation is the cnf claim of a key-bound ID Token. |
| 48 |
// |
| 49 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 50 |
// This API may change or be removed without a major version bump. |
| 51 |
type Confirmation struct { |
| 52 |
JWK json.RawMessage `json:"jwk"` |
| 53 |
} |
| 54 |
|
| 55 |
// DPoPProofClaims represents the claims in a DPoP proof JWT. |
| 56 |
// |
| 57 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 58 |
// This API may change or be removed without a major version bump. |
| 59 |
type DPoPProofClaims struct { |
| 60 |
JWTID string `json:"jti"` |
| 61 |
HTTPMethod string `json:"htm"` |
| 62 |
HTTPURI string `json:"htu"` |
| 63 |
IssuedAt Time `json:"iat"` |
| 64 |
CodeHash string `json:"c_s256,omitempty"` |
| 65 |
} |
| 66 |
|
| 67 |
// UnmarshalJSON implements custom JSON unmarshaling for DPoPProofClaims. |
| 68 |
// |
| 69 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 70 |
// This API may change or be removed without a major version bump. |
| 71 |
func (c *DPoPProofClaims) UnmarshalJSON(data []byte) error { |
| 72 |
type claims DPoPProofClaims |
| 73 |
var decoded claims |
| 74 |
if err := json.Unmarshal(data, &decoded); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
var fields map[string]json.RawMessage |
| 78 |
if err := json.Unmarshal(data, &fields); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
if raw, ok := fields["iat"]; ok { |
| 82 |
var issuedAt int64 |
| 83 |
if bytes.Equal(raw, []byte("null")) { |
| 84 |
return &json.UnmarshalTypeError{Value: "null", Type: reflect.TypeOf(issuedAt), Field: "iat"} |
| 85 |
} |
| 86 |
if err := json.Unmarshal(raw, &issuedAt); err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
decoded.IssuedAt = Time(issuedAt) |
| 90 |
} |
| 91 |
*c = DPoPProofClaims(decoded) |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
// ValidDPoPJKT reports whether value is an unpadded base64url-encoded |
| 96 |
// SHA-256 JWK thumbprint. |
| 97 |
// |
| 98 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 99 |
// This API may change or be removed without a major version bump. |
| 100 |
func ValidDPoPJKT(value string) bool { |
| 101 |
decoded, err := base64.RawURLEncoding.Strict().DecodeString(value) |
| 102 |
return err == nil && len(decoded) == sha256.Size && base64.RawURLEncoding.EncodeToString(decoded) == value |
| 103 |
} |
| 104 |
|
| 105 |
// JWKThumbprint returns the RFC 7638 SHA-256 thumbprint of jwk, encoded |
| 106 |
// with unpadded base64url. |
| 107 |
// |
| 108 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 109 |
// This API may change or be removed without a major version bump. |
| 110 |
func JWKThumbprint(jwk *jose.JSONWebKey) (string, error) { |
| 111 |
thumbprint, err := jwk.Thumbprint(crypto.SHA256) |
| 112 |
if err != nil { |
| 113 |
return "", err |
| 114 |
} |
| 115 |
return base64.RawURLEncoding.EncodeToString(thumbprint), nil |
| 116 |
} |
| 117 |
|
| 118 |
// ValidateDPoPKeyStrength enforces minimum key strength for a DPoP |
| 119 |
// proof-of-possession key: RSA 2048-8192 bits, EC curve P-256, P-384 or P-521, |
| 120 |
// and Ed25519 (fixed strength). Other key types pass; callers are expected to |
| 121 |
// have already restricted the key to a public asymmetric type. Shared by the OP |
| 122 |
// and RP so the two cannot drift apart. |
| 123 |
// |
| 124 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 125 |
// This API may change or be removed without a major version bump. |
| 126 |
func ValidateDPoPKeyStrength(key any) error { |
| 127 |
switch k := key.(type) { |
| 128 |
case *rsa.PublicKey: |
| 129 |
bits := k.N.BitLen() |
| 130 |
if bits < 2048 || bits > 8192 { |
| 131 |
return fmt.Errorf("RSA key size %d bits is not allowed", bits) |
| 132 |
} |
| 133 |
case *ecdsa.PublicKey: |
| 134 |
switch k.Curve { |
| 135 |
case elliptic.P256(), elliptic.P384(), elliptic.P521(): |
| 136 |
default: |
| 137 |
return fmt.Errorf("EC curve %s is not allowed", k.Curve.Params().Name) |
| 138 |
} |
| 139 |
} |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
// CodeHash returns the c_s256 value for an authorization or device code. |
| 144 |
// |
| 145 |
// Experimental: OpenID Connect Key Binding 1.0 is a draft standard. |
| 146 |
// This API may change or be removed without a major version bump. |
| 147 |
func CodeHash(code string) string { |
| 148 |
hash := sha256.Sum256([]byte(code)) |
| 149 |
return base64.RawURLEncoding.EncodeToString(hash[:]) |
| 150 |
} |