-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattestation_statement_fido_u2f_test.go
149 lines (134 loc) · 4.21 KB
/
attestation_statement_fido_u2f_test.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
package webauthn
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"math/big"
"testing"
"github.com/pomerium/webauthn/cose"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVerifyFIDOU2FAttestationStatement(t *testing.T) {
t.Run("valid", func(t *testing.T) {
for _, name := range []string{"U2F"} {
response := readTestAuthenticatorAttestationResponse(t, name)
attestationObject, err := response.UnmarshalAttestationObject()
require.NoError(t, err)
clientDataJSONHash := response.GetClientDataJSONHash()
t.Run(name, func(t *testing.T) {
_, err = VerifyAttestationStatement(attestationObject, clientDataJSONHash)
assert.NoError(t, err)
})
}
})
key1, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
key2, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
rsaKey, err := rsa.GenerateKey(rand.Reader, 1024)
require.NoError(t, err)
t.Run("missing x5c", func(t *testing.T) {
attestationObject := createTestFIDOU2FAttestationObject(t,
[]byte{0, 0, 0, 0},
key1,
nil,
)
attestationObject.Statement["x5c"] = nil
_, err := VerifyAttestationStatement(attestationObject, ClientDataJSONHash{})
assert.Contains(t, err.Error(), "invalid x5c certificate")
})
t.Run("bad algorithm", func(t *testing.T) {
attestationObject := createTestFIDOU2FAttestationObject(t,
[]byte{0, 0, 0, 0},
key2,
nil,
)
_, err = VerifyAttestationStatement(attestationObject, ClientDataJSONHash{})
assert.Contains(t, err.Error(), "only the P-256 curve is supported")
})
t.Run("bad public key", func(t *testing.T) {
key, err := cose.NewRSAPublicKey(cose.AlgorithmRS256, rsaKey.PublicKey)
require.NoError(t, err)
rawKey, err := key.Marshal()
require.NoError(t, err)
attestationObject := createTestFIDOU2FAttestationObject(t,
[]byte{0, 0, 0, 0},
key1,
func(data *AuthenticatorData) {
data.AttestedCredentialData.CredentialPublicKey = rawKey
},
)
_, err = VerifyAttestationStatement(attestationObject, ClientDataJSONHash{})
assert.Contains(t, err.Error(), "only ECDSA keys are supported")
})
t.Run("bad signature", func(t *testing.T) {
attestationObject := createTestFIDOU2FAttestationObject(t,
[]byte{0, 0, 0, 0},
key1,
nil,
)
attestationObject.Statement["sig"] = nil
_, err := VerifyAttestationStatement(attestationObject, ClientDataJSONHash{})
assert.Contains(t, err.Error(), "invalid signature")
})
}
func createTestFIDOU2FAttestationObject(
t *testing.T,
rpID []byte,
privateKey *ecdsa.PrivateKey,
modifyAuthenticatorData func(data *AuthenticatorData),
) *AttestationObject {
x5ctpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
SubjectKeyId: generateSubjectKeyID(t, privateKey.Public()),
SignatureAlgorithm: x509.ECDSAWithSHA256,
KeyUsage: x509.KeyUsageDigitalSignature,
}
x5c, err := x509.CreateCertificate(rand.Reader, x5ctpl, x5ctpl, privateKey.Public(), privateKey)
require.NoError(t, err)
key, err := cose.NewECDSAPublicKey(cose.AlgorithmES256, privateKey.PublicKey)
require.NoError(t, err)
publicKeyU2F := key.RawX962ECC()
rawKey, err := key.Marshal()
require.NoError(t, err)
rpIDHash := sha256.Sum256(rpID)
authenticatorData := &AuthenticatorData{
RPIDHash: rpIDHash,
Flags: authenticatorFlagsAT,
SignCount: 0,
AttestedCredentialData: &AttestedCredentialData{
AAGUID: newRandomAAGUID(),
CredentialID: []byte{},
CredentialPublicKey: rawKey,
},
Extensions: []byte{},
}
if modifyAuthenticatorData != nil {
modifyAuthenticatorData(authenticatorData)
}
rawAuthenticatorData, err := authenticatorData.Marshal()
require.NoError(t, err)
verificationData := concat([]byte{0x00},
rpIDHash[:],
make([]byte, 32),
authenticatorData.AttestedCredentialData.CredentialID,
publicKeyU2F[:],
)
hashData := sha256.Sum256(verificationData)
rawSignature, err := ecdsa.SignASN1(rand.Reader, privateKey, hashData[:])
require.NoError(t, err)
return &AttestationObject{
AuthData: rawAuthenticatorData,
Format: AttestationFormatFIDOU2F,
Statement: AttestationStatement{
"sig": rawSignature,
"x5c": []interface{}{
x5c,
},
},
}
}