-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore.go
293 lines (260 loc) · 10.1 KB
/
explore.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
package main
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/protocol/webauthncose"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"io"
"os"
"reflect"
)
// Look at the response with new credentials, and see if those credentials, in turns
// are signed by the authenticator itself.
//
// There are three parts to it
// - whether the credential id and public key are signed at all, or just provided to us
// - whether they are signed by authenticator's key, or by the private key of the credential
// - whether we can verify authenticator key in some way.
func verifyAttestationsResponse(response *protocol.ParsedCredentialCreationData) string {
serializedClientData := response.Raw.AttestationResponse.ClientDataJSON
authData := response.Response.AttestationObject.RawAuthData
attStmt := response.Response.AttestationObject.AttStatement
// No signature. We can store id and public key, but no idea what produced them
if attStmt == nil || len(attStmt) == 0 {
return "OK, authenticator is unknown"
}
// We have some signature
signature := attStmt["sig"].([]byte)
if x5c, ok := attStmt["x5c"]; ok {
// When the x5c field is present, it means that signature is made by authenticator's
// private key. First element of x5c contains the matching certificate, with a public
// key.
cert, err := x509.ParseCertificate(x5c.([]interface{})[0].([]byte))
if err != nil {
return "OK, authenticator certificate is unparseable"
}
// See if we validate authenticator certificate. For now,
// we only support validation using YubiKey root CA.
var certificateStatus = "not validated"
uuidObj, _ := uuid.FromBytes(response.Response.AttestationObject.AuthData.AttData.AAGUID)
var rootPEM []byte
if rootCertificates == nil {
prepareRootCertificates()
}
if root, ok := rootCertificates[uuidObj.String()]; ok {
log.Info().Msgf("found root certificate for %v in official blob", uuidObj)
rootPEM = []byte(root)
} else {
// Try YubiKey root
rootPEM, err = os.ReadFile("yubico-u2f-ca-certs.txt")
if err != nil {
log.Info().Err(err).Msg("cannot read root certificate")
}
log.Info().Msgf("using root certificate for YubiKey")
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(rootPEM)
if !ok {
log.Error().Msg("cannot append root certificate")
}
opts := x509.VerifyOptions{
Roots: roots,
}
if _, err := cert.Verify(opts); err != nil {
log.Info().Err(err).Msg("cannot verify certificate")
} else {
certificateStatus = "validated via root CA"
}
if cert.PublicKeyAlgorithm.String() == "ECDSA" {
publicKey := cert.PublicKey.(*ecdsa.PublicKey)
hasher := crypto.SHA256.New()
clientDataHash := sha256.Sum256(serializedClientData)
sigData := append(authData, clientDataHash[:]...)
hasher.Write(sigData)
r := ecdsa.VerifyASN1(publicKey, hasher.Sum(nil), signature)
if !r {
return "Signature verification failed"
}
} else {
return fmt.Sprintf("Unsupported signature algorithm %v", cert.PublicKeyAlgorithm)
}
return fmt.Sprintf("OK, credential signed by %s, %s", cert.Subject.CommonName, certificateStatus)
} else {
// We have attStmt, but no certificates in it. In this case, the data is signed by
// the credential private key. It does not really prove anything, other than the
// fact that the other wise can sign.
publicKey := response.Response.AttestationObject.AuthData.AttData.CredentialPublicKey
clientDataHash := sha256.Sum256(serializedClientData)
sigData := append(authData, clientDataHash[:]...)
key, err := webauthncose.ParsePublicKey(publicKey)
if err != nil {
log.Err(err).Msg("cannot parse public key")
}
valid, err := webauthncose.VerifySignature(key, sigData, signature)
if err != nil {
log.Err(err).Msg("error verifying signature")
}
if !valid {
log.Info().Msg("signature is not valid")
}
return "OK, credential is self-signed"
}
}
// Verify that attestation response indeed contains a valid signature of challange
// using credential private key. We use public key stored in the server for the check.
//
// Important: the webauthn library already does that check, and many others. We repeat
// it here only for illustration. Don't even think about reusing this in production.
func verifyAssertionResponse(response *protocol.ParsedCredentialAssertionData, publicKey []byte) string {
serializedClientData := response.Raw.AssertionResponse.ClientDataJSON
authData := response.Raw.AssertionResponse.AuthenticatorData
//signature, _ := base64.RawURLEncoding.DecodeString(string(response.Raw.AssertionResponse.Signature))
clientDataHash := sha256.Sum256(serializedClientData)
signedData := append(authData, clientDataHash[:]...)
key, err := webauthncose.ParsePublicKey(publicKey)
if err != nil {
log.Err(err).Msg("cannot parse public key")
return "cannot parse public key"
}
valid, err := webauthncose.VerifySignature(key, signedData, response.Response.Signature)
if err != nil {
log.Err(err).Msg("error verifying signature")
return "could not verify signature"
}
if !valid {
log.Info().Msg("signature is not valid")
return "signature is not valid"
}
return "signature is valid"
}
// The response that JS gets from the navigator.credentials.create call is
// a weird mix of UTF8 encoded JSON, a binary format called CBOR, and
// totally custom binary formats.
//
// This function converts it into a hierarchical tree of the actual data
// that can be shown as JSON.
func explainAttestationResponse(reader io.Reader) map[string]interface{} {
empty := map[string]interface{}{
"error": "could not decode the data",
}
m := make(map[string]interface{})
if err := json.NewDecoder(reader).Decode(&m); err != nil {
log.Err(err).Msg("cannot decode top-level response")
return empty
}
response := m["response"].(map[string]interface{})
if cd, ok := response["clientDataJSON"]; ok {
// This field is base64 encoding of JSON
clientDataBytes, _ := base64.RawURLEncoding.DecodeString(cd.(string))
cdm := make(map[string]interface{})
json.NewDecoder(bytes.NewReader(clientDataBytes)).Decode(&cdm)
response["clientDataJSON"] = cdm
}
if ao, ok := response["attestationObject"]; ok {
// This field is base64 encoding of binary serialization format called CBOR
content, _ := base64.RawURLEncoding.DecodeString(ao.(string))
aom := make(map[string]interface{})
decMode, _ := cbor.DecOptions{
DefaultMapType: reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf((*interface{})(nil)).Elem()),
}.DecMode()
err := decMode.Unmarshal(content, &aom)
if err != nil {
log.Err(err).Msg("cannot unmarshal attestationObject")
return empty
}
// This one is a nested binary format, and we'll use the webauthn library to
// unpack it for us.
authDataContent := aom["authData"].([]byte)
ad := &protocol.AuthenticatorData{}
if err := ad.Unmarshal(authDataContent); err == nil {
// Now convert AuthenticatorData to map, so that we can make more changes
adJsonBytes, _ := json.Marshal(ad)
adm := make(map[string]interface{})
json.Unmarshal(adJsonBytes, &adm)
aom["authData"] = adm
// Convert authenticator GUID from binary to string
attData := adm["att_data"].(map[string]interface{})
aaguid_base64 := attData["aaguid"].(string)
aaguid_bytes, _ := base64.StdEncoding.DecodeString(aaguid_base64)
aaguid, _ := uuid.FromBytes(aaguid_bytes)
attData["aaguid"] = aaguid.String()
} else {
log.Err(err).Msg("Could not decode auth data")
return empty
}
response["attestationObject"] = aom
}
return response
}
// Just like for attestation response, this function renders assertion
// response in a more readable fashion.
func explainAssertionResponse(reader io.Reader) map[string]interface{} {
empty := map[string]interface{}{
"error": "could not decode the data",
}
m := make(map[string]interface{})
if err := json.NewDecoder(reader).Decode(&m); err != nil {
log.Err(err).Msg("cannot decode top-level response")
return empty
}
response := m["response"].(map[string]interface{})
clientDataBytes, _ := base64.RawURLEncoding.DecodeString(response["clientDataJSON"].(string))
cdm := make(map[string]interface{})
json.NewDecoder(bytes.NewReader(clientDataBytes)).Decode(&cdm)
response["clientDataJSON"] = cdm
authDataContent, _ := base64.RawURLEncoding.DecodeString(response["authenticatorData"].(string))
ad := &protocol.AuthenticatorData{}
if err := ad.Unmarshal(authDataContent); err == nil {
response["authenticatorData"] = ad
} else {
log.Err(err).Msg("Could not decode auth data")
return empty
}
return response
}
// Map from AAGUID to PEM string for root certificate
var rootCertificates map[string]string
// There is an official binary blob containing root certificates for various authenticators
// This function parses it and stores authenticator ids and root certificates.
//
// As the earlier functions, this code should not be used for production purposes.
func prepareRootCertificates() {
rootCertificates = make(map[string]string)
jwtParser := jwt.NewParser()
// Downloaded from https://mds3.fidoalliance.org/
// See https://fidoalliance.org/metadata/#:~:text=For%20assistance%20on%20the%20FIDO,%40mymds.fidoalliance.org.
blogBytes, _ := os.ReadFile("blob.jwt")
token, _, err := jwtParser.ParseUnverified(string(blogBytes), jwt.MapClaims{})
if err != nil {
log.Err(err).Msg("cannot parse metadata TOC")
return
}
entries := token.Claims.(jwt.MapClaims)["entries"].([]interface{})
for _, e := range entries {
em := e.(map[string]interface{})
if guid, ok := em["aaguid"]; ok {
if metadataStatement_ := em["metadataStatement"]; ok {
metadataStatement := metadataStatement_.(map[string]interface{})
if roots_, ok := metadataStatement["attestationRootCertificates"]; ok {
roots := roots_.([]interface{})
if len(roots) > 0 {
firstRootString := roots[0].(string)
// Add PEM envelope, since crypto.x509 cannot parse just the raw data.
rootCertificates[guid.(string)] = "-----BEGIN CERTIFICATE-----\n" + firstRootString +
"\n-----END CERTIFICATE-----\n"
}
}
}
}
}
}