-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential.go
305 lines (256 loc) · 8.88 KB
/
credential.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
package yeahapi
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"hash"
"math/big"
"github.com/gofrs/uuid"
)
type PubKeyCredentialOpts struct {
CredentialID string
Title string
PubKey string
PubKeyAlg int
Transports []AuthenticatorTransport
UserID uuid.UUID
Counter uint32
CredentialRequestID string
}
type PubKeyCredential struct {
ID uuid.UUID
CredentialID string
Title string
PubKey string
PubKeyAlg int
Transports []AuthenticatorTransport
UserID uuid.UUID
Counter uint32
CredentialRequestID uuid.UUID
}
type CredentialRequest struct {
ID uuid.UUID
Type string
Challenge string
Used bool
UserID uuid.UUID
}
type AuthenticatorTransport string
const (
AuthenticatorUSB AuthenticatorTransport = "usb"
AuthenticatorNFC AuthenticatorTransport = "nfc"
AuthenticatorBLE AuthenticatorTransport = "ble"
AuthenticatorInternal AuthenticatorTransport = "internal"
)
type AuthenticatorAttachment string
const (
AuthenticatorPlatform AuthenticatorAttachment = "platform"
AuthenticatorCrossPlatform AuthenticatorAttachment = "cross-platform"
)
type ResidentKeyRequirement string
const (
ResidentKeyDiscouraged ResidentKeyRequirement = "discouraged"
ResidentKeyPreferred ResidentKeyRequirement = "preferred"
ResidentKeyRequired ResidentKeyRequirement = "required"
)
type UserVerificationRequirement string
const (
UserVerificationRequired UserVerificationRequirement = "required"
UserVerificationPreferred UserVerificationRequirement = "preferred"
UserVerificationDiscouraged UserVerificationRequirement = "discouraged"
)
type AttestationConveyancePreference string
const (
AttestationNone AttestationConveyancePreference = "none"
AttestationIndirect AttestationConveyancePreference = "indirect"
AttestationDirect AttestationConveyancePreference = "direct"
AttestationEnterprise AttestationConveyancePreference = "enterprise"
)
type COSEAlgorithmIdentifier int
const (
COSEAlgES256 COSEAlgorithmIdentifier = -7
COSEAlgEdDSA COSEAlgorithmIdentifier = -8
COSEAlgRS256 COSEAlgorithmIdentifier = -257
)
type PubKeyCreateRequest struct {
ID uuid.UUID `json:"id"`
PubKey *PubKeyCredentialCreationOpts `json:"pubkey"`
Kind string
}
type PubKeyGetRequest struct {
ID uuid.UUID `json:"id"`
PubKey *PubKeyCredentialRequestOpts `json:"pubkey"`
UserID UserID
Kind string
}
type CollectedClientData struct {
Raw []byte
Type string `json:"type"`
Challenge string `json:"challenge"`
Origin string `json:"origin"`
}
type authenticatorResponse struct {
ClientDataJSON string `json:"client_data_json"`
}
type AuthenticatorData struct {
Raw []byte
RpIDHash []byte
UserPresent bool
UserVerified bool
Counter uint32
AAGUID []byte
CredentialID []byte
}
type AuthenticatorAttestationResponse struct {
authenticatorResponse
AuthenticatorData string `json:"authenticator_data"`
Transports []AuthenticatorTransport `json:"transports"`
PubKey string `json:"pubkey"`
PubKeyAlg int `json:"pubkey_alg"`
}
type AuthenticatorAssertionResponse struct {
authenticatorResponse
AuthenticatorData string `json:"authenticator_data"`
Signature string `json:"signature"`
UserHandle string `json:"user_handle"`
}
type rawPubKeyCredential struct {
ID string `json:"id"`
RawID string `json:"raw_id"`
}
type RawPubKeyCredentialAttestation struct {
rawPubKeyCredential
Response AuthenticatorAttestationResponse `json:"response"`
}
type RawPubKeyCredentialAssertion struct {
rawPubKeyCredential
Response AuthenticatorAssertionResponse `json:"response"`
}
type CreatePubKeyData struct {
ReqID uuid.UUID `json:"req_id"`
Credential RawPubKeyCredentialAttestation `json:"credential"`
Title string
}
type AssertPubKeyData struct {
ReqID uuid.UUID `json:"req_id"`
Credential RawPubKeyCredentialAssertion `json:"credential"`
}
type PubKeyCredentialRpEntity struct {
ID string `json:"id"`
Name string `json:"name"`
}
type PubKeyCredentialUserEntity struct {
ID UserID
EncodedID string `json:"id"`
DisplayName string `json:"display_name"`
}
type AuthenticatorSelectionCriteria struct {
AuthenticatorAttachment AuthenticatorAttachment `json:"authenticator_attachment,omitempty"`
ResidentKey ResidentKeyRequirement `json:"resident_key,omitempty"`
RequireResidentKey bool `json:"require_resident_key,omitempty"`
UserVerification UserVerificationRequirement `json:"user_verification,omitempty"`
}
type PubKeyCredentialDescriptor struct {
Type string `json:"type"`
ID uuid.UUID `json:"id"`
Transports []AuthenticatorTransport `json:"transports"`
}
type PubKeyCredentialParameters struct {
Type string `json:"type"`
Alg COSEAlgorithmIdentifier `json:"alg"`
}
type PubKeyCredentialCreationOpts struct {
Rp PubKeyCredentialRpEntity `json:"rp"`
User PubKeyCredentialUserEntity `json:"user"`
Challenge string `json:"challenge"`
PubKeyCredParams []PubKeyCredentialParameters `json:"pubkey_cred_params"`
Timeout int `json:"timeout"`
ExcludeCredentials []PubKeyCredentialDescriptor `json:"exclude_credentials,omitempty"`
AuthenticatorSelection AuthenticatorSelectionCriteria `json:"authenticator_selection,omitempty"`
Attestation AttestationConveyancePreference `json:"attestation"`
}
type PubKeyCredentialRequestOpts struct {
Challenge string `json:"challenge"`
Timeout int `json:"timeout"`
RpID string `json:"rp_id"`
AllowCredentials []PubKeyCredentialDescriptor `json:"allow_credentials"`
UserVerification UserVerificationRequirement `json:"user_verification"`
}
func (c *PubKeyCredential) Verify(clientData []byte, authnData []byte, sig string) error {
sigbytes, err := base64.RawURLEncoding.DecodeString(sig)
if err != nil {
return E(EInternal, "unable to decode signature")
}
clientDataHash := sha256.Sum256(clientData)
message := make([]byte, len(authnData)+len(clientDataHash))
copy(message, authnData)
copy(message[len(authnData):], clientDataHash[:])
return c.verifySignature(message, sigbytes)
}
var hashers = map[COSEAlgorithmIdentifier]func() hash.Hash{
COSEAlgES256: sha256.New,
COSEAlgEdDSA: sha512.New,
COSEAlgRS256: sha256.New,
}
func (c *PubKeyCredential) verifySignature(message []byte, sig []byte) error {
bytes, err := base64.RawURLEncoding.DecodeString(c.PubKey)
if err != nil {
return E(EInvalid, "unable to decode pubkey")
}
parsed, err := x509.ParsePKIXPublicKey(bytes)
if err != nil {
return E(EInvalid, "unable to parse pubkey")
}
hasher := hashers[COSEAlgorithmIdentifier(c.PubKeyAlg)]
if hasher == nil {
return E(EInvalid, "unsupported hashing algorithm")
}
h := hasher()
_, err = h.Write(message)
if err != nil {
return E(EInternal, "unable to hash the data")
}
digest := h.Sum(nil)
switch pk := parsed.(type) {
case *ecdsa.PublicKey:
type ecdsaSignature struct {
R, S *big.Int
}
var ecdsaSig ecdsaSignature
if rest, err := asn1.Unmarshal(sig, &ecdsaSig); err != nil {
return E(EInternal)
} else if len(rest) != 0 {
return E(EInvalid, "trailing data after ECDSA signature")
}
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
return E(EInvalid, "ECDSA signature contained zero or negative values")
}
if !ecdsa.Verify(pk, digest, ecdsaSig.R, ecdsaSig.S) {
return E(EInvalid, "ECDSA signature verification failed")
}
case *rsa.PublicKey:
if err := rsa.VerifyPKCS1v15(pk, crypto.SHA256, digest, sig); err != nil {
return E(EInvalid, "RSA signature verification failed")
}
default:
return E(EInternal, "unsupported key type")
}
return nil
}
type CredentialService interface {
PubKeyCreateRequest(ctx context.Context, user *User) (*PubKeyCreateRequest, error)
PubKeyGetRequest(ctx context.Context, userID UserID) (*PubKeyGetRequest, error)
CreatePubKey(ctx context.Context, credential *PubKeyCredential) error
VerifyPubKey(ctx context.Context) error
Request(ctx context.Context, id uuid.UUID) (*CredentialRequest, error)
Credentials(ctx context.Context, userID UserID) ([]PubKeyCredentialDescriptor, error)
Credential(ctx context.Context, id string) (*PubKeyCredential, error)
ValidateClientData(data string, req *CredentialRequest) (*CollectedClientData, error)
ValidateAuthnData(data string) (*AuthenticatorData, error)
}