-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathassertion.go
144 lines (119 loc) · 3.89 KB
/
assertion.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
package virtualwebauthn
import (
"crypto"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
)
/// Options
type AssertionOptions struct {
Challenge []byte `json:"challenge,omitempty"`
AllowCredentials []string `json:"allowCredentials,omitempty"`
RelyingPartyID string `json:"rpId,omitempty"`
}
func ParseAssertionOptions(str string) (assertionOptions *AssertionOptions, err error) {
values := assertionOptionsValues{}
err = json.Unmarshal([]byte(str), &values)
if err != nil {
return nil, err
}
if values.PublicKey != nil {
values = *values.PublicKey
}
assertionOptions = &AssertionOptions{
RelyingPartyID: values.RelyingPartyID,
}
if len(values.Challenge) == 0 {
return nil, errors.New("failed to find challenge in options")
}
challenge, err := base64.RawURLEncoding.DecodeString(values.Challenge)
if err != nil {
return nil, err
}
assertionOptions.Challenge = challenge
for _, cred := range values.AllowCredentials {
if len(cred.ID) == 0 {
return nil, errors.New("allowed credential has an empty id")
}
assertionOptions.AllowCredentials = append(assertionOptions.AllowCredentials, cred.ID)
}
return assertionOptions, nil
}
/// Response
func CreateAssertionResponse(rp RelyingParty, auth Authenticator, cred Credential, options AssertionOptions) string {
clientData := clientData{
Type: "webauthn.get",
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)
rpIDHash := sha256.Sum256([]byte(rp.ID))
flags := authenticatorDataFlags(
!auth.Options.UserNotPresent,
!auth.Options.UserNotVerified,
auth.Options.BackupEligible,
auth.Options.BackupState,
false,
false,
)
authData := []byte{}
authData = append(authData, rpIDHash[:]...)
authData = append(authData, flags)
authData = append(authData, bigEndianBytes(cred.Counter, 4)...)
authDataEncoded := base64.RawURLEncoding.EncodeToString(authData)
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")
}
credIDEncoded := base64.RawURLEncoding.EncodeToString(cred.ID)
assertionResponse := assertionResponse{
AuthenticatorData: authDataEncoded,
ClientDataJSON: clientDataJSONEncoded,
Signature: base64.RawURLEncoding.EncodeToString(sig),
UserHandle: base64.RawURLEncoding.EncodeToString(auth.Options.UserHandle),
}
assertionResult := assertionResult{
Type: "public-key",
ID: credIDEncoded,
RawID: credIDEncoded,
Response: assertionResponse,
}
assertionResultBytes, err := json.Marshal(assertionResult)
if err != nil {
panic("failed to marshal json")
}
return string(assertionResultBytes)
}
/// Helpers
type assertionOptionsValues struct {
Challenge string `json:"challenge,omitempty"`
AllowCredentials []assertionOptionsAllowCredential `json:"allowCredentials,omitempty"`
RelyingPartyID string `json:"rpId,omitempty"`
PublicKey *assertionOptionsValues `json:"publicKey,omitempty"`
}
type assertionOptionsAllowCredential struct {
Type string `json:"type"`
ID string `json:"id"`
}
type assertionResponse struct {
AuthenticatorData string `json:"authenticatorData"`
ClientDataJSON string `json:"clientDataJSON"`
Signature string `json:"signature"`
UserHandle string `json:"userHandle,omitempty"`
}
type assertionResult struct {
Type string `json:"type"`
ID string `json:"id"`
RawID string `json:"rawId"`
Response assertionResponse `json:"response"`
}