-
Notifications
You must be signed in to change notification settings - Fork 0
/
vapid.go
274 lines (235 loc) · 7.34 KB
/
vapid.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/url"
"strings"
"time"
)
// RFC9292 - VAPID
var (
// encoded {"typ":"JWT","alg":"ES256"}
vapidPrefix = []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.")
// encoded {"typ":"JWT","alg":"EdDSA"}
//https://tools.ietf.org/html/rfc8037
vapidPrefixED = []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.")
dot = []byte(".")
)
type JWTHead struct {
Typ string `json:"typ"`
Alg string `json:"alg,omitempty"`
}
// VAPIDToken creates a token with the specified endpoint, using configured Sub id
// and a default expiration (1h).
//
// Format is "vapid t=TOKEN k=PUBKEY
//
// The optional (unauthenticated) Sub field is populated from Name@Domain or TrustDomain.
// The DMesh VIP is based on the public key of the signer.
// AUD is the URL from the subscription - for DMesh https://VIP:5228/s or
// https://DOMAIN:5228/s
func (auth *Auth) VAPIDToken(aud string) string {
jwt := JWT{}
u, err := url.Parse(aud)
if err != nil || len(u.Host) == 0 {
jwt.Aud = []string{aud}
} else {
jwt.Aud = []string{"https://" + u.Host}
}
if auth.TrustDomain != "" {
jwt.Sub = auth.TrustDomain
if auth.Name != "" {
jwt.Sub = auth.Name + "@" + auth.TrustDomain
}
}
jwt.Exp = time.Now().Unix() + 3600
t, _ := json.Marshal(jwt)
return auth.rawVAPIDSign(t)
}
func (auth *Auth) rawVAPIDSign(t []byte) string {
enc := base64.RawURLEncoding
// Base64URL for the content of the token
t64 := make([]byte, enc.EncodedLen(len(t)))
enc.Encode(t64, t)
c0 := *auth.Cert
token := make([]byte, len(t)+len(vapidPrefix)+100)
if _, ok := c0.PrivateKey.(*ecdsa.PrivateKey); ok {
token = append(token[:0], vapidPrefix...)
} else if _, ok := c0.PrivateKey.(ed25519.PrivateKey); ok {
token = append(token[:0], vapidPrefixED...)
} else {
return ""
}
token = append(token, t64...)
hasher := crypto.SHA256.New()
hasher.Write(token)
var sig []byte
if ec, ok := c0.PrivateKey.(*ecdsa.PrivateKey); ok {
if r, s, err := ecdsa.Sign(rand.Reader, ec, hasher.Sum(nil)); err == nil {
// Vapid key is 32 bytes
keyBytes := 32
sig = make([]byte, 2*keyBytes)
rBytes := r.Bytes()
rBytesPadded := make([]byte, keyBytes)
copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
sBytes := s.Bytes()
sBytesPadded := make([]byte, keyBytes)
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
sig = append(sig[:0], rBytesPadded...)
sig = append(sig, sBytesPadded...)
}
} else if ed, ok := c0.PrivateKey.(ed25519.PrivateKey); ok {
sig, _ = ed.Sign(rand.Reader, hasher.Sum(nil), nil)
}
sigB64 := make([]byte, enc.EncodedLen(len(sig)))
enc.Encode(sigB64, sig)
token = append(token, dot...)
token = append(token, sigB64...)
return "vapid t=" + string(token) + ", k=" + auth.pub64
}
func JwtRawParse(tok string) (*JWTHead, *JWT, []byte, []byte, error) {
// Token is parsed with square/go-jose
parts := strings.Split(tok, ".")
if len(parts) < 2 {
return nil, nil, nil, nil, fmt.Errorf("VAPID: malformed jwt, parts=%d", len(parts))
}
head, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("VAPI: malformed jwt %v", err)
}
h := &JWTHead{}
json.Unmarshal(head, h)
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("VAPI: malformed jwt %v", err)
}
b := &JWT{}
json.Unmarshal(payload, b)
b.Raw = string(payload)
sig, err := base64.RawURLEncoding.DecodeString(parts[2])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("VAPI: malformed jwt %v", err)
}
return h, b, []byte(tok[0 : len(parts[0])+len(parts[1])+1]), sig, nil
}
func jwtParseAndCheckSig(tok string, pk crypto.PublicKey) (*JWT, error) {
h, b, txt, sig, err := JwtRawParse(tok)
if err != nil {
return nil, err
}
hasher := crypto.SHA256.New()
hasher.Write(txt)
if h.Alg == "ES256" {
r := big.NewInt(0).SetBytes(sig[0:32])
s := big.NewInt(0).SetBytes(sig[32:64])
match := ecdsa.Verify(pk.(*ecdsa.PublicKey), hasher.Sum(nil), r, s)
if !match {
return nil, errors.New("invalid ES256 signature")
}
return b, nil
} else if h.Alg == "EdDSA" {
ok := ed25519.Verify(pk.(ed25519.PublicKey), hasher.Sum(nil), sig)
if !ok {
return nil, errors.New("invalid ED25519 signature")
}
} else if h.Alg == "RS256" {
rsak := pk.(*rsa.PublicKey)
hashed := hasher.Sum(nil)
err = rsa.VerifyPKCS1v15(rsak, crypto.SHA256, hashed, sig)
if err != nil {
return nil, err
}
return b, nil
}
return nil, errors.New("Unsupported " + h.Alg)
}
// CheckVAPID verifies the signature and returns the token and public key.
// expCheck should be set to current time to set expiration
//
// Data is extracted from VAPID header - 'vapid' scheme and t/k params
//
// Does not check audience or other parms.
func CheckVAPID(tok string, now time.Time) (jwt *JWT, pub []byte, err error) {
// Istio uses oidc - will make a HTTP request to fetch the .well-known from
// iss.
// provider, err := oidc.NewProvider(context.Background(), iss)
// Provider uses verifier, using KeySet interface 'verifySignature(jwt)
// The keyset is expected to be cached and configured (trusted)
scheme, _, keys := ParseAuthorization(tok)
if scheme != "vapid" {
return nil, nil, errors.New("Unexected scheme " + scheme)
}
tok = keys["t"]
pubk := keys["k"]
publicUncomp, err := base64.RawURLEncoding.DecodeString(pubk)
if err != nil {
return nil, nil, fmt.Errorf("VAPI: malformed jwt %v", err)
}
var pk crypto.PublicKey
if len(publicUncomp) == 32 {
pk = ed25519.PublicKey(publicUncomp)
} else if len(publicUncomp) == 65 {
x, y := elliptic.Unmarshal(elliptic.P256(), publicUncomp)
pk = &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
} else {
return nil, nil, fmt.Errorf("VAPI: malformed jwt %d", len(pubk))
}
b, err := jwtParseAndCheckSig(tok, pk)
if err != nil {
return nil, nil, err
}
if !now.IsZero() {
expT := time.Unix(b.Exp, 0)
if expT.Before(now) {
return nil, nil, errors.New("Expired token")
}
}
return b, publicUncomp, nil
}
// ParseAuthorization splits the Authorization header, returning the scheme and parameters.
// Used with the "scheme k=v,k=v" format.
func ParseAuthorization(auth string) (string, string, map[string]string) {
auth = strings.TrimSpace(auth)
params := map[string]string{}
spaceIdx := strings.Index(auth, " ")
if spaceIdx == -1 {
return "", auth, params
}
scheme := auth[0:spaceIdx]
auth = auth[spaceIdx:]
if strings.Index(auth, "=") < 0 {
return scheme, auth, params
}
pl := strings.Split(auth, ",")
for _, p := range pl {
p = strings.Trim(p, " ")
kv := strings.Split(p, "=")
if len(kv) == 2 {
key := strings.Trim(kv[0], " ")
params[key] = kv[1]
}
}
return scheme, "", params
}