-
Notifications
You must be signed in to change notification settings - Fork 2
/
attested_credential_data.go
102 lines (88 loc) · 3.13 KB
/
attested_credential_data.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
package webauthn
import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/pomerium/webauthn/fido"
)
// AAGUIDSize is the number of bytes of an AAGUID in the AttestedCredentialData.
const AAGUIDSize = fido.AAGUIDSize
// AAGUID is the Authenticator Attestation GUID.
type AAGUID = fido.AAGUID
func newRandomAAGUID() AAGUID {
var aaguid AAGUID
_, err := io.ReadFull(rand.Reader, aaguid[:])
if err != nil {
panic(err)
}
return aaguid
}
// ErrInvalidAttestedCredentialData indicates the attested credential data is invalid.
var ErrInvalidAttestedCredentialData = errors.New("invalid attested credential data")
// AttestedCredentialData is added to the authenticator data when generating an attestation object for a given
// credential.
type AttestedCredentialData struct {
// AAGUID is the AAGUID of the authenticator.
AAGUID AAGUID
// The CredentialID is a probabilistically-unique byte sequence identifying a public key credential source and
// its authentication assertions.
CredentialID []byte
// CredentialPublicKey is the credential public key encoded in COSE_Key format.
CredentialPublicKey []byte
}
// UnmarshalAttestedCredentialData unmarshals an AttestedCredentialData according to the data layout described in
// https://www.w3.org/TR/webauthn-2/#sctn-attested-credential-data:
//
// aaguid: 16 bytes
// credentialIdLength: 2 bytes, 16-bit unsigned big-endian = L
// credentialId: L bytes
// credentialPublicKey: variable, CTAP2 canonical CBOR encoding form
func UnmarshalAttestedCredentialData(raw []byte) (data *AttestedCredentialData, remaining []byte, err error) {
data = new(AttestedCredentialData)
// unmarshal AAGUID
if len(raw) < AAGUIDSize {
return nil, nil, fmt.Errorf("%w: missing AAGUID", ErrInvalidAttestedCredentialData)
}
copy(data.AAGUID[:], raw[:AAGUIDSize])
raw = raw[AAGUIDSize:]
// unmarshal credential id
if len(raw) < 2 {
return nil, nil, fmt.Errorf("%w: missing credential id length", ErrInvalidAttestedCredentialData)
}
credentialIDLength := int(binary.BigEndian.Uint16(raw[:2]))
raw = raw[2:]
if len(raw) < credentialIDLength {
return nil, nil, fmt.Errorf("%w: missing credential id", ErrInvalidAttestedCredentialData)
}
data.CredentialID = raw[:credentialIDLength]
raw = raw[credentialIDLength:]
// unmarshal credential public key
data.CredentialPublicKey, raw, err = extractCBOR(raw)
if err != nil {
return nil, nil, err
}
return data, raw, nil
}
// Marshal marshals the attested credential data in the format described in Unmarshal.
func (attestedCredentialData *AttestedCredentialData) Marshal() ([]byte, error) {
if attestedCredentialData == nil {
return nil, fmt.Errorf("cannot marshal a nil attested credential")
}
var buf bytes.Buffer
if err := write(&buf, attestedCredentialData.AAGUID[:]...); err != nil {
return nil, err
}
if err := writeUint16(&buf, uint16(len(attestedCredentialData.CredentialID))); err != nil {
return nil, err
}
if err := write(&buf, attestedCredentialData.CredentialID...); err != nil {
return nil, err
}
if err := write(&buf, attestedCredentialData.CredentialPublicKey...); err != nil {
return nil, err
}
return buf.Bytes(), nil
}