Debian Codegraph @

examples/mfa/mfacosigner/mocks/webauthn.go

raw
1 // Copyright 2024 OpenPubkey
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16
17 package mocks
18
19 import (
20 "crypto"
21 "crypto/ecdsa"
22 "crypto/rand"
23 "crypto/sha256"
24
25 "github.com/go-webauthn/webauthn/protocol"
26 "github.com/go-webauthn/webauthn/protocol/webauthncbor"
27 "github.com/go-webauthn/webauthn/protocol/webauthncose"
28 "github.com/openpubkey/openpubkey/jose"
29 "github.com/openpubkey/openpubkey/util"
30 )
31
32 // For testing purposes we create a WebAuthn device to run the client part of the protocol
33 type WebAuthnDevice struct {
34 signer crypto.Signer
35 PubkeyCbor []byte
36 RpID string
37 RpIDHash []byte
38 Userhandle []byte
39 RawID []byte
40 AuthFlags byte
41 Counter uint32
42 }
43
44 func NewWebauthnDevice(rpID string) (*WebAuthnDevice, error) {
45 alg := jose.ES256
46 signer, err := util.GenKeyPair(alg)
47 if err != nil {
48 return nil, err
49 }
50
51 pubkey := signer.Public().(*ecdsa.PublicKey)
52 pubkeyCbor := webauthncose.EC2PublicKeyData{
53 PublicKeyData: webauthncose.PublicKeyData{
54 KeyType: int64(webauthncose.EllipticKey),
55 Algorithm: int64(webauthncose.AlgES256),
56 },
57 Curve: int64(webauthncose.AlgES256),
58 XCoord: pubkey.X.Bytes(),
59 YCoord: pubkey.Y.Bytes(),
60 }
61 pubkeyCborBytes, err := webauthncbor.Marshal(pubkeyCbor)
62 if err != nil {
63 return nil, err
64 }
65
66 rpIDHash := sha256.Sum256([]byte(rpID))
67
68 return &WebAuthnDevice{
69 signer: signer,
70 PubkeyCbor: pubkeyCborBytes,
71 RpID: rpID,
72 RpIDHash: rpIDHash[:],
73
74 // Checked by Webauthn RP to distinguish between different
75 // users accounts sharing the same device with the same RP.
76 // userHandle == user.WebAuthnID()?
77 //
78 // Not all devices can store a user handle it is allowed to be null
79 // https://developer.mozilla.org/en-US/docs/Web/API/AuthenticatorAssertionResponse/userHandle
80 //
81 // In OpenPubkey MFA Cosigner RP we set this to the ID Token sub
82 Userhandle: nil,
83
84 // The ID of the public key credential held by the device
85 RawID: []byte{5, 1, 1, 1, 1},
86
87 // Flag 0x41 has two bits set. 0b001 and 0b101
88 // FlagUserPresent Bit 00000001 - the user is present (UP flag)
89 // FlagUserVerified Bit 00000100 - user is verified using a biometric or PIN (UV flag)
90 AuthFlags: 0x41,
91
92 // Signature counter, used to identify cloned devices see https://www.w3.org/TR/webauthn/#signature-counter
93 Counter: 0,
94 }, nil
95 }
96
97 func (wa *WebAuthnDevice) RegResp(createCreation *protocol.CredentialCreation) (*protocol.ParsedCredentialCreationData, error) {
98 wa.Userhandle = []byte(createCreation.Response.User.ID.(protocol.URLEncodedBase64))
99
100 return &protocol.ParsedCredentialCreationData{
101 Response: protocol.ParsedAttestationResponse{
102 CollectedClientData: protocol.CollectedClientData{
103 Type: protocol.CeremonyType("webauthn.create"),
104 Challenge: createCreation.Response.Challenge.String(),
105 Origin: createCreation.Response.RelyingParty.ID,
106 },
107 AttestationObject: protocol.AttestationObject{
108 Format: "none",
109 AuthData: protocol.AuthenticatorData{
110 RPIDHash: wa.RpIDHash,
111 Counter: wa.Counter,
112 Flags: protocol.AuthenticatorFlags(wa.AuthFlags),
113 AttData: protocol.AttestedCredentialData{
114 AAGUID: make([]byte, 16),
115 CredentialID: wa.RawID,
116 CredentialPublicKey: wa.PubkeyCbor,
117 },
118 },
119 },
120 Transports: []protocol.AuthenticatorTransport{protocol.USB, protocol.NFC, "fake"},
121 },
122 }, nil
123 }
124
125 func (wa *WebAuthnDevice) LoginResp(credAssert *protocol.CredentialAssertion) (*protocol.ParsedCredentialAssertionData, error) {
126 loginRespData := &protocol.ParsedCredentialAssertionData{
127 ParsedPublicKeyCredential: protocol.ParsedPublicKeyCredential{
128 // Checked by Webauthn RP to see if public key supplied is on the
129 // allowlist of public keys for this user:
130 // parsedResponse.RawID == session.AllowedCredentialIDs?
131 RawID: wa.RawID,
132 },
133 Response: protocol.ParsedAssertionResponse{
134 CollectedClientData: protocol.CollectedClientData{
135 Type: protocol.CeremonyType("webauthn.get"),
136 Challenge: credAssert.Response.Challenge.String(),
137 Origin: wa.RpID,
138 },
139 AuthenticatorData: protocol.AuthenticatorData{
140 RPIDHash: wa.RpIDHash,
141 Counter: wa.Counter,
142 Flags: protocol.AuthenticatorFlags(wa.AuthFlags),
143 },
144 UserHandle: wa.Userhandle, // Not a required field:
145 },
146 }
147
148 return wa.SignLoginChallenge(loginRespData)
149 }
150
151 func (wa *WebAuthnDevice) SignLoginChallenge(loginRespData *protocol.ParsedCredentialAssertionData) (*protocol.ParsedCredentialAssertionData, error) {
152 clientDataHash := sha256.Sum256(loginRespData.Raw.AssertionResponse.ClientDataJSON)
153 sigData := append(loginRespData.Raw.AssertionResponse.AuthenticatorData, clientDataHash[:]...)
154 sigHash := sha256.Sum256(sigData)
155 sigWebauthn, err := wa.signer.Sign(rand.Reader, sigHash[:], crypto.SHA256)
156 if err != nil {
157 return nil, err
158 }
159 loginRespData.Response.Signature = sigWebauthn
160 return loginRespData, nil
161 }