-
Notifications
You must be signed in to change notification settings - Fork 2
/
attestation_statement.go
214 lines (187 loc) · 8.45 KB
/
attestation_statement.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
package webauthn
import (
"crypto/x509"
"errors"
"fmt"
"github.com/pomerium/webauthn/cose"
"github.com/pomerium/webauthn/tpm"
)
var (
// ErrInvalidAttestationStatement indicates that an attestation statement is invalid.
ErrInvalidAttestationStatement = errors.New("invalid attestation statement")
// ErrInvalidCertificate indicates that an attestation statement has an invalid x5c certificate.
ErrInvalidCertificate = errors.New("invalid certificate")
// ErrInvalidCertInfo indicates that an attestation statement has an invalid certInfo field.
ErrInvalidCertInfo = errors.New("invalid certInfo")
// ErrInvalidPubArea indicates that an attestation statement has an invalid pubArea field.
ErrInvalidPubArea = errors.New("invalid pubArea")
// ErrMissingCertificate indicates that an attestation statement is missing an x5c certificate.
ErrMissingCertificate = errors.New("missing certificate")
// ErrMissingCertInfo indicates that an attestation statement is missing the certInfo field.
ErrMissingCertInfo = errors.New("missing certInfo")
// ErrMissingPubArea indicates that an attestation statement is missing a pubArea field.
ErrMissingPubArea = errors.New("missing pubArea")
)
// AttestationFormat is the attestation format.
type AttestationFormat string
// Attestation formats from https://www.w3.org/TR/webauthn-2/#sctn-defined-attestation-formats
const (
AttestationFormatAndroidKey AttestationFormat = "android-key"
AttestationFormatAndroidSafetyNet AttestationFormat = "android-safetynet"
AttestationFormatApple AttestationFormat = "apple"
AttestationFormatFIDOU2F AttestationFormat = "fido-u2f"
AttestationFormatNone AttestationFormat = "none"
AttestationFormatPacked AttestationFormat = "packed"
AttestationFormatTPM AttestationFormat = "tpm"
)
// AllAttestationFormats are all the attestation formats.
var AllAttestationFormats = []AttestationFormat{
AttestationFormatAndroidKey,
AttestationFormatAndroidSafetyNet,
AttestationFormatApple,
AttestationFormatFIDOU2F,
AttestationFormatNone,
AttestationFormatPacked,
AttestationFormatTPM,
}
// AttestationType is the attestation type.
type AttestationType string
// Attestation types from https://www.w3.org/TR/webauthn-2/#sctn-attestation-types
const (
// AttestationTypeBasic indicates the authenticator’s attestation key pair is specific to an authenticator "model",
// i.e., a "batch" of authenticators. Thus, authenticators of the same, or similar, model often share the same
// attestation key pair.
AttestationTypeBasic = "Basic"
// AttestationTypeSelf (also known as surrogate basic attestation) indicates the Authenticator does not have any
// specific attestation key pair. Instead it uses the credential private key to create the attestation signature.
// Authenticators without meaningful protection measures for an attestation private key typically use this
// attestation type.
AttestationTypeSelf = "Self"
// AttestationTypeAttestationCA indicates an authenticator is based on a Trusted Platform Module (TPM) and holds an
// authenticator-specific "endorsement key" (EK). This key is used to securely communicate with a trusted third
// party, the Attestation CA (formerly known as a "Privacy CA"). The authenticator can generate multiple
// attestation identity key pairs (AIK) and requests an Attestation CA to issue an AIK certificate for each. Using
// this approach, such an authenticator can limit the exposure of the EK (which is a global correlation handle) to
// Attestation CA(s). AIKs can be requested for each authenticator-generated public key credential individually,
// and conveyed to Relying Parties as attestation certificates.
AttestationTypeAttestationCA = "AttCA"
// AttestationTypeAnonymizationCA indicates the authenticator uses an Anonymization CA which dynamically generates
// per-credential attestation certificates such that the attestation statements presented to Relying Parties do
// not provide uniquely identifiable information, e.g., that might be used for tracking purposes.
AttestationTypeAnonymizationCA = "AnonCA"
// AttestationTypeNone indicates no attestation information is available.
AttestationTypeNone = "None"
// AttestationTypeUnknown indicates the attestation type is not known.
AttestationTypeUnknown = "Unknown"
)
// AllAttestationTypes are all the known attestation types.
var AllAttestationTypes = []AttestationType{
AttestationTypeBasic,
AttestationTypeSelf,
AttestationTypeAttestationCA,
AttestationTypeAnonymizationCA,
AttestationTypeNone,
AttestationTypeUnknown,
}
// AttestationStatement is a map of data stored in an AttestationObject according to one of the pre-defined attestation
// statement formats.
type AttestationStatement map[string]interface{}
// GetAlgorithm gets the "alg" field of the attestation statement. If no field is found, or the field contains invalid
// data, 0 will be returned.
func (attestationStatement AttestationStatement) GetAlgorithm() cose.Algorithm {
alg, _ := attestationStatement["alg"].(int64)
return cose.Algorithm(alg)
}
// GetSignature gets the "sig" field of the attestation statement. It returns nil if no field is found, or the field
// does not contain a byte slice.
func (attestationStatement AttestationStatement) GetSignature() []byte {
sig, _ := attestationStatement["sig"].([]byte)
return sig
}
// UnmarshalCertificates unmarshals X.509 certificates stored in an x5c key.
func (attestationStatement AttestationStatement) UnmarshalCertificates() ([]*x509.Certificate, error) {
x5c, ok := attestationStatement["x5c"]
if !ok {
return nil, ErrMissingCertificate
}
x5cs, ok := x5c.([]interface{})
if !ok {
return nil, ErrInvalidCertificate
}
var certificates []*x509.Certificate
for _, x5c := range x5cs {
bs, ok := x5c.([]byte)
if !ok {
return nil, ErrInvalidCertificate
}
certificate, err := x509.ParseCertificate(bs)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidCertificate, err)
}
certificates = append(certificates, certificate)
}
return certificates, nil
}
// UnmarshalCertInfo unmarshals the TPM certInfo from an attestation statement.
func (attestationStatement AttestationStatement) UnmarshalCertInfo() (*tpm.AttestationData, error) {
certInfo, ok := attestationStatement["certInfo"]
if !ok {
return nil, ErrMissingCertInfo
}
bs, ok := certInfo.([]byte)
if !ok {
return nil, ErrInvalidCertInfo
}
attestationData, err := tpm.UnmarshalAttestationData(bs)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidCertInfo, err)
}
return attestationData, nil
}
// UnmarshalPubArea unmarshals the pubArea field from the attestation statement.
func (attestationStatement AttestationStatement) UnmarshalPubArea() (*tpm.Public, error) {
pubArea, ok := attestationStatement["pubArea"]
if !ok {
return nil, ErrMissingPubArea
}
bs, ok := pubArea.([]byte)
if !ok {
return nil, ErrInvalidPubArea
}
public, err := tpm.UnmarshalPublic(bs)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidPubArea, err)
}
return public, nil
}
// VerifyAttestationStatementResult is the result of running VerifyAttestationStatement.
type VerifyAttestationStatementResult struct {
Type AttestationType
TrustPaths [][]*x509.Certificate
}
// VerifyAttestationStatement verifies that an AttestationObject's attestation statement is valid according to the
// verification procedures defined for know attestation statement formats.
func VerifyAttestationStatement(
attestationObject *AttestationObject,
clientDataJSONHash ClientDataJSONHash,
) (*VerifyAttestationStatementResult, error) {
switch attestationObject.Format {
case AttestationFormatAndroidKey:
return VerifyAndroidKeyAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatAndroidSafetyNet:
return VerifyAndroidSafetyNetAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatApple:
return VerifyAppleAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatFIDOU2F:
return VerifyFIDOU2FAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatNone:
return VerifyNoneAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatPacked:
return VerifyPackedAttestationStatement(attestationObject, clientDataJSONHash)
case AttestationFormatTPM:
return VerifyTPMAttestationStatement(attestationObject, clientDataJSONHash)
default:
return nil, fmt.Errorf("%w: unknown format %s", ErrInvalidAttestationStatement,
attestationObject.Format)
}
}