-
Notifications
You must be signed in to change notification settings - Fork 9
/
attestation.go
203 lines (168 loc) · 5.6 KB
/
attestation.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
package virtualwebauthn
import (
"crypto"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
)
/// Options
type AttestationOptions struct {
Challenge []byte `json:"challenge,omitempty"`
ExcludeCredentials []string `json:"excludeCredentials,omitempty"`
RelyingPartyID string `json:"rpId,omitempty"`
RelyingPartyName string `json:"rpName,omitempty"`
UserID string `json:"user,omitempty"`
UserName string `json:"userName,omitempty"`
UserDisplayName string `json:"userDisplayName,omitempty"`
}
func ParseAttestationOptions(str string) (attestationOptions *AttestationOptions, err error) {
values := attestationOptionsValues{}
err = json.Unmarshal([]byte(str), &values)
if err != nil {
return nil, err
}
if values.PublicKey != nil {
values = *values.PublicKey
}
attestationOptions = &AttestationOptions{
RelyingPartyID: values.RP.ID,
RelyingPartyName: values.RP.Name,
}
decodedUserID, err := base64.RawURLEncoding.DecodeString(values.User.ID)
if err != nil {
return nil, errors.New("failed to decode user id in response")
}
attestationOptions.UserID = string(decodedUserID)
attestationOptions.UserName = values.User.Name
attestationOptions.UserDisplayName = values.User.DisplayName
if len(values.Challenge) == 0 {
return nil, errors.New("failed to find challenge in response")
}
challenge, err := base64.RawURLEncoding.DecodeString(values.Challenge)
if err != nil {
return nil, err
}
attestationOptions.Challenge = challenge
for _, cred := range values.ExcludeCredentials {
if len(cred.ID) == 0 {
return nil, errors.New("allowed credential has an empty id")
}
attestationOptions.ExcludeCredentials = append(attestationOptions.ExcludeCredentials, cred.ID)
}
return attestationOptions, nil
}
/// Response
func CreateAttestationResponse(rp RelyingParty, auth Authenticator, cred Credential, options AttestationOptions) string {
clientData := clientData{
Type: "webauthn.create",
Challenge: base64.RawURLEncoding.EncodeToString(options.Challenge),
Origin: rp.Origin,
}
clientDataJSON, err := json.Marshal(clientData)
if err != nil {
panic("failed to marshal json")
}
clientDataJSONEncoded := base64.RawURLEncoding.EncodeToString(clientDataJSON)
publicKeyData := cred.Key.AttestationData()
credData := []byte{}
credData = append(credData, auth.Aaguid[:]...)
credData = append(credData, bigEndianBytes(len(cred.ID), 2)...)
credData = append(credData, cred.ID...)
credData = append(credData, publicKeyData...)
rpIDHash := sha256.Sum256([]byte(rp.ID))
flags := authenticatorDataFlags(
!auth.Options.UserNotPresent,
!auth.Options.UserNotVerified,
auth.Options.BackupEligible,
auth.Options.BackupState,
true,
false,
)
authData := []byte{}
authData = append(authData, rpIDHash[:]...)
authData = append(authData, flags)
authData = append(authData, bigEndianBytes(cred.Counter, 4)...)
authData = append(authData, credData...)
clientDataJSONHashed := sha256.Sum256(clientDataJSON)
verifyData := append(authData, clientDataJSONHashed[:]...)
hasher := crypto.SHA256.New()
hasher.Write(verifyData)
digest := hasher.Sum(nil)
sig, err := cred.Key.Sign(digest)
if err != nil {
panic("failed to sign digest")
}
var algo int
if cred.Key.Type == KeyTypeEC2 {
algo = ec2SHA256Algo
} else if cred.Key.Type == KeyTypeRSA {
algo = rsaSHA256Algo
}
attestationObject := attestationObject{
Format: "packed",
AuthData: authData,
Statement: attestationStatement{
Algorithm: algo,
Signature: sig,
},
}
attestationObjectBytes := marshalCbor(attestationObject)
attestationObjectEncoded := base64.RawURLEncoding.EncodeToString(attestationObjectBytes)
credIDEncoded := base64.RawURLEncoding.EncodeToString(cred.ID)
attestationResponse := attestationResponse{
AttestationObject: attestationObjectEncoded,
ClientDataJSON: clientDataJSONEncoded,
}
attestationResult := attestationResult{
Type: "public-key",
ID: credIDEncoded,
RawID: credIDEncoded,
Response: attestationResponse,
}
attestationResultBytes, err := json.Marshal(attestationResult)
if err != nil {
panic("failed to marshal json")
}
return string(attestationResultBytes)
}
/// Helpers
type attestationOptionsValues struct {
Challenge string `json:"challenge,omitempty"`
ExcludeCredentials []attestationOptionsExcludeCredential `json:"excludeCredentials,omitempty"`
RP attestationOptionsRelyingParty `json:"rp,omitempty"`
User attestationOptionsUser `json:"user,omitempty"`
PublicKey *attestationOptionsValues `json:"publicKey,omitempty"`
}
type attestationOptionsRelyingParty struct {
ID string `json:"id"`
Name string `json:"name"`
}
type attestationOptionsUser struct {
ID string `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
}
type attestationOptionsExcludeCredential struct {
Type string `json:"type"`
ID string `json:"id"`
}
type attestationStatement struct {
Algorithm int `json:"alg"`
Signature []byte `json:"sig"`
}
type attestationObject struct {
Format string `json:"fmt"`
Statement attestationStatement `json:"attStmt"`
AuthData []byte `json:"authData"`
}
type attestationResponse struct {
AttestationObject string `json:"attestationObject"`
ClientDataJSON string `json:"clientDataJSON"`
}
type attestationResult struct {
Type string `json:"type"`
ID string `json:"id"`
RawID string `json:"rawId"`
Response attestationResponse `json:"response"`
}