This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
/
authenticator.go
388 lines (317 loc) · 11 KB
/
authenticator.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package authenticator
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/binary"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"math"
"math/big"
"path"
"time"
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/protocol/googletpm"
"github.com/duo-labs/webauthn/protocol/webauthncose"
"github.com/fxamacker/cbor/v2"
)
type responseData struct {
AttestationObject string `json:"attestationObject"`
ClientDataJSON string `json:"clientDataJSON"`
}
type credentialResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Response responseData `json:"response"`
}
const (
userPresentFlag = 1
userVerifiedFlag = 4
hasAttdata = 64
)
type pubKeyCredSource struct {
Type string
ID string
Priv *ecdsa.PrivateKey
RPID string
userID []byte
}
// MockAuthenticator is a functional mock of a webauthn/FIDO authenticator device.
// It supports fido-u2f attestation and assertion, using the P256 curve, with direct attestation,
// using a provided CA certificate and private key to serve as an Attestation CA.
type MockAuthenticator struct {
CredSources map[string]*pubKeyCredSource
}
// New creates a MockAuthenticator.
func New() *MockAuthenticator {
return &MockAuthenticator{
CredSources: map[string]*pubKeyCredSource{},
}
}
// Authenticate generates a webauthn credential based on the provided parameters, signed by the given parent CA.
// Returns a JSON object containing the authentication response.
func (ma *MockAuthenticator) Authenticate(parentCertPath, parentKeyPath, requesterOrigin string, params *protocol.CredentialCreation) ([]byte, error) { // nolint:lll,funlen // it's just a long procedure
clientDataJSON, clientDataHash, err := collectClientData(requesterOrigin, params.Response.Challenge)
if err != nil {
return nil, err
}
parentCert, err := loadCert(parentCertPath)
if err != nil {
return nil, err
}
parentKey, err := loadKey(parentKeyPath)
if err != nil {
return nil, err
}
myPriv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
myPubCose := webauthncose.EC2PublicKeyData{
PublicKeyData: webauthncose.PublicKeyData{
KeyType: int64(webauthncose.EllipticKey),
Algorithm: int64(webauthncose.AlgES256), // mock authenticator is hardcoded to use ES256
},
Curve: int64(googletpm.CurveNISTP256),
XCoord: myPriv.X.Bytes(),
YCoord: myPriv.Y.Bytes(),
}
myPubCBOR, err := cbor.Marshal(myPubCose)
if err != nil {
return nil, err
}
randCredID := make([]byte, 16)
if _, e := rand.Reader.Read(randCredID); e != nil {
return nil, e
}
rpIDHash := sha256.Sum256([]byte(params.Response.RelyingParty.ID))
authData := protocol.AuthenticatorData{
RPIDHash: rpIDHash[:],
Flags: userPresentFlag | userVerifiedFlag | hasAttdata,
AttData: protocol.AttestedCredentialData{
AAGUID: bytes.Repeat([]byte{0}, 16), // fido-u2f AAGUID is 16 zeros
CredentialID: randCredID,
CredentialPublicKey: myPubCBOR, // pub key marshaled in webauthn COSE CBOR format
},
}
authDataBytes := marshalAuthData(&authData, true)
// generate contents of AttStatement
sig, err := makeSig(rpIDHash[:], clientDataHash, randCredID, myPriv)
if err != nil {
return nil, err
}
certASN, err := makeChildCert(parentCert, parentKey, myPriv.Public())
if err != nil {
return nil, err
}
attObj := protocol.AttestationObject{
RawAuthData: authDataBytes,
Format: "fido-u2f",
AttStatement: map[string]interface{}{
"x5c": [][]byte{certASN},
"sig": sig,
},
}
attObjBytes, err := cbor.Marshal(attObj)
if err != nil {
return nil, err
}
// save credential data for future assertions
credSource := pubKeyCredSource{
ID: base64.RawURLEncoding.EncodeToString(randCredID),
Type: "public-key",
Priv: myPriv,
userID: params.Response.User.ID,
RPID: params.Response.RelyingParty.ID,
}
ma.CredSources[credSource.ID] = &credSource
credResponse := credentialResponse{
ID: base64.RawURLEncoding.EncodeToString(authData.AttData.CredentialID),
Type: "public-key",
Response: responseData{
AttestationObject: base64.RawURLEncoding.EncodeToString(attObjBytes),
ClientDataJSON: base64.RawURLEncoding.EncodeToString(clientDataJSON),
},
}
return json.Marshal(credResponse)
}
func collectClientData(requesterOrigin string, challenge []byte) ([]byte, []byte, error) {
collectedClientData := &protocol.CollectedClientData{
Type: "webauthn.create",
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
Origin: requesterOrigin,
}
clientDataJSON, err := json.Marshal(collectedClientData)
if err != nil {
return nil, nil, err
}
clientDataHash := sha256.Sum256(clientDataJSON)
return clientDataJSON, clientDataHash[:], nil
}
// AssertionInternalResponse holds the internal data of an assertion response.
type AssertionInternalResponse struct {
AuthenticatorData protocol.URLEncodedBase64 `json:"authenticatorData"`
ClientDataJSON protocol.URLEncodedBase64 `json:"clientDataJSON"`
Signature protocol.URLEncodedBase64 `json:"signature"`
}
// AssertionResponse holds an assertion response.
type AssertionResponse struct {
ID string `json:"id"`
RawID string `json:"rawId"`
Type string `json:"type"`
Response AssertionInternalResponse `json:"response"`
}
// Assert creates a credential assertion for a relying party that's already registered via Authenticate.
// Returns a JSON object containing the assertion response.
func (ma *MockAuthenticator) Assert(requesterOrigin string, params *protocol.PublicKeyCredentialRequestOptions) ([]byte, error) { // nolint:lll // splitting lines would make this less readable
collectedClientData := &protocol.CollectedClientData{
Type: "webauthn.get",
Challenge: base64.RawURLEncoding.EncodeToString(params.Challenge),
Origin: requesterOrigin,
}
collectedClientDataBytes, err := json.Marshal(collectedClientData)
if err != nil {
return nil, err
}
ccdHash := sha256.Sum256(collectedClientDataBytes)
credSource, err := ma.getValidCred(params.AllowedCredentials)
if err != nil {
return nil, err
}
rpIDHash := sha256.Sum256([]byte(params.RelyingPartyID))
authBytes := marshalAuthData(&protocol.AuthenticatorData{
RPIDHash: rpIDHash[:],
Flags: userPresentFlag | userVerifiedFlag, // bit 0: user present, bit 2: user verified
Counter: 0, // incrementing the counter is optional, so we won't
}, false) // for credential assertion we don't include attestation data
sigData := bytes.NewBuffer(nil)
sigData.Write(authBytes)
sigData.Write(ccdHash[:])
sigHash := sha256.Sum256(sigData.Bytes())
sig, err := credSource.Priv.Sign(rand.Reader, sigHash[:], nil)
if err != nil {
return nil, err
}
out := AssertionResponse{
ID: credSource.ID,
RawID: credSource.ID,
Type: "public-key",
Response: AssertionInternalResponse{
AuthenticatorData: authBytes,
ClientDataJSON: collectedClientDataBytes,
Signature: sig,
},
}
return json.Marshal(&out)
}
func (ma *MockAuthenticator) getValidCred(credOpts []protocol.CredentialDescriptor) (*pubKeyCredSource, error) {
var credSource *pubKeyCredSource
var ok bool
for _, credOpt := range credOpts {
credMapKey := base64.RawURLEncoding.EncodeToString(credOpt.CredentialID)
if credSource, ok = ma.CredSources[credMapKey]; ok {
break
}
}
if !ok || credSource == nil || credSource.Priv == nil {
return nil, fmt.Errorf("no usable cred sources")
}
return credSource, nil
}
// makeChildCert returns a certificate signed by parent, with its PEM encoding and private key.
func makeChildCert(parent *x509.Certificate, parentPriv, myPub interface{}) ([]byte, error) {
certSerialNumber, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt32))
if err != nil {
return nil, err
}
keyID := make([]byte, 16)
if _, err = rand.Read(keyID); err != nil {
return nil, err
}
template := x509.Certificate{
SerialNumber: certSerialNumber,
Subject: pkix.Name{
CommonName: "BDD-test attestation certificate",
SerialNumber: fmt.Sprint(*certSerialNumber),
},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour * 24), // nolint:gomnd // really, nolint?
KeyUsage: x509.KeyUsageDigitalSignature,
BasicConstraintsValid: true,
SubjectKeyId: keyID,
}
return x509.CreateCertificate(rand.Reader, &template, parent, myPub, parentPriv)
}
// makeSig signs the authenticated data of an authentication statement.
func makeSig(rpIDHash, clientDataHash, credentialID []byte, privKey *ecdsa.PrivateKey) ([]byte, error) {
sigData := bytes.NewBuffer([]byte{0x00})
sigData.Write(rpIDHash)
sigData.Write(clientDataHash)
sigData.Write(credentialID)
sigData.Write([]byte{0x04}) // append octet 04, identifies uncompressed pubkey format
sigData.Write(privKey.X.Bytes())
sigData.Write(privKey.Y.Bytes())
sigHash := sha256.Sum256(sigData.Bytes())
return privKey.Sign(rand.Reader, sigHash[:], nil)
}
// marshalAuthData constructs the byte array of auth data in sections 6.1 and 6.5.1 of the spec.
// https://www.w3.org/TR/webauthn/#sctn-authenticator-data
// https://www.w3.org/TR/webauthn/#sctn-attested-credential-data
func marshalAuthData(authData *protocol.AuthenticatorData, includeAttData bool) []byte {
authDataBytes := bytes.NewBuffer(nil)
authDataBytes.Write(authData.RPIDHash)
authDataBytes.Write([]byte{byte(authData.Flags)})
counterBytes := []byte{0, 0, 0, 0}
binary.BigEndian.PutUint32(counterBytes, authData.Counter)
authDataBytes.Write(counterBytes)
if includeAttData {
authDataBytes.Write(authData.AttData.AAGUID)
credIDLen := []byte{0, 0}
binary.BigEndian.PutUint16(credIDLen, uint16(len(authData.AttData.CredentialID)))
authDataBytes.Write(credIDLen)
authDataBytes.Write(authData.AttData.CredentialID)
authDataBytes.Write(authData.AttData.CredentialPublicKey)
}
return authDataBytes.Bytes()
}
func loadCert(filePath string) (*x509.Certificate, error) {
data, errRead := ioutil.ReadFile(path.Clean(filePath))
if errRead != nil {
return nil, fmt.Errorf("failed to read cert file: %w", errRead)
}
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("failed to decode cert pem")
}
cert, errParse := x509.ParseCertificate(block.Bytes)
if errParse != nil {
return nil, fmt.Errorf("failed to parse cert: %w", errParse)
}
return cert, nil
}
func loadKey(filePath string) (*ecdsa.PrivateKey, error) {
data, errRead := ioutil.ReadFile(path.Clean(filePath))
if errRead != nil {
return nil, fmt.Errorf("failed to read key file: %w", errRead)
}
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("failed to decode key pem")
}
key, errParse := x509.ParseECPrivateKey(block.Bytes)
if errParse != nil {
return nil, fmt.Errorf("failed to parse key: %w", errParse)
}
return key, nil
}