-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_assertion.go
162 lines (139 loc) · 3.69 KB
/
client_assertion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package identity
import (
"crypto"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"time"
"github.com/KarlGW/azcfg/internal/uuid"
)
// newCertificateAssertion creates a new assertion jwt for a client certificate credential.
func newCertificateAssertion(endpoint, clientID string, cert certificate) (jwt, error) {
header := header{
ALG: "RS256",
TYP: "JWT",
X5T: cert.thumbprint,
}
if len(cert.x5c) > 0 {
header.X5C = cert.x5c
}
uuid, err := uuid.New()
if err != nil {
return jwt{}, err
}
claims := claims{
AUD: endpoint,
EXP: time.Now().Add(time.Minute * 5).Unix(),
ISS: clientID,
JTI: uuid,
NBF: time.Now().Unix(),
SUB: clientID,
IAT: time.Now().Unix(),
}
t := jwt{
header: header,
claims: claims,
}
if err := t.sign(cert.key); err != nil {
return jwt{}, err
}
return t, nil
}
// jwt is a JSON web token used for client credential with assertion.
type jwt struct {
header header
signature signature
claims claims
}
// Encode returns the jwt as a string encoded for use in the request body.
func (t jwt) Encode() string {
return t.header.Encode() + "." + t.claims.Encode() + "." + t.signature.Encode()
}
// sign the jwt with the private key.
func (t *jwt) sign(key *rsa.PrivateKey) error {
data := []byte(t.header.Encode() + "." + t.claims.Encode())
hashed := sha256.Sum256(data)
signature, err := rsa.SignPKCS1v15(nil, key, crypto.SHA256, hashed[:])
if err == nil {
t.signature = signature
}
return err
}
// header is a JSON web token header.
type header struct {
ALG string `json:"alg"`
TYP string `json:"typ"`
X5T string `json:"x5t"`
X5C []string `json:"x5c,omitempty"`
}
// Encode the header as a base64 encoded string.
func (h header) Encode() string {
b, _ := json.Marshal(h)
return base64.URLEncoding.EncodeToString(b)
}
// claims is a JSON web token claims.
type claims struct {
AUD string `json:"aud"`
ISS string `json:"iss"`
JTI string `json:"jti"`
SUB string `json:"sub"`
EXP int64 `json:"exp"`
NBF int64 `json:"nbf"`
IAT int64 `json:"iat"`
}
// Encode the claims as a base64 encoded string.
func (c claims) Encode() string {
b, _ := json.Marshal(c)
return base64.URLEncoding.EncodeToString(b)
}
// signature is a JSON web token signature.
type signature []byte
// Encode the signature as a base64 encoded string.
func (s signature) Encode() string {
return base64.URLEncoding.EncodeToString(s)
}
// certificate is a processed certificate chain and key that contains
// the main certificate, the private key, the thumbprint and
// the x5c chain.
type certificate struct {
cert *x509.Certificate
key *rsa.PrivateKey
thumbprint string
x5c []string
}
// isZero returns true if certificate is empty.
func (c certificate) isZero() bool {
return c.cert == nil && c.key == nil && len(c.thumbprint) == 0
}
// newCertificate processes the provided certificates and key and returns
// a certificate.
func newCertificate(certs []*x509.Certificate, key *rsa.PrivateKey) (certificate, error) {
c := certificate{
key: key,
}
if key == nil {
return certificate{}, errors.New("private key is required")
}
for _, cert := range certs {
if cert == nil {
continue
}
ck, ok := cert.PublicKey.(*rsa.PublicKey)
if ok && c.key.E == ck.E && c.key.N.Cmp(ck.N) == 0 {
c.cert = cert
hashed := sha1.Sum(c.cert.Raw)
c.thumbprint = base64.StdEncoding.EncodeToString(hashed[:])
c.x5c = append([]string{base64.StdEncoding.EncodeToString(c.cert.Raw)}, c.x5c...)
} else {
c.x5c = append(c.x5c, base64.StdEncoding.EncodeToString(cert.Raw))
}
}
if c.cert == nil {
return certificate{}, errors.New("provided certificates and key does not match")
}
return c, nil
}