Skip to content

Commit 2df9bdb

Browse files
refactor(protocol): adjust formatting
This addresses formatting styles from the previous commit.
1 parent 339114c commit 2df9bdb

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

metadata/metadata.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,26 @@ type Statement struct {
319319
AuthenticatorGetInfo AuthenticatorGetInfo
320320
}
321321

322-
func (s *Statement) Verifier() (opts x509.VerifyOptions) {
322+
func (s *Statement) Verifier(x5cis []*x509.Certificate) (opts x509.VerifyOptions) {
323323
roots := x509.NewCertPool()
324324

325325
for _, root := range s.AttestationRootCertificates {
326326
roots.AddCert(root)
327327
}
328328

329+
var intermediates *x509.CertPool
330+
331+
if len(x5cis) > 0 {
332+
intermediates = x509.NewCertPool()
333+
334+
for _, x5c := range x5cis {
335+
intermediates.AddCert(x5c)
336+
}
337+
}
338+
329339
return x509.VerifyOptions{
330-
Roots: roots,
340+
Roots: roots,
341+
Intermediates: intermediates,
331342
}
332343
}
333344

protocol/attestation.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,55 +232,55 @@ func (a *AttestationObject) VerifyAttestation(clientDataHash []byte, mds metadat
232232
}
233233

234234
var (
235-
x5c *x509.Certificate
236-
parents []*x509.Certificate
235+
x5c, parsed *x509.Certificate
236+
x5cis []*x509.Certificate
237+
raw []byte
238+
ok bool
237239
)
238240

239241
if len(x5cs) == 0 {
240242
return ErrInvalidAttestation.WithDetails("Unable to parse attestation certificate from x5c during attestation validation").WithInfo("The attestation had no certificates")
241243
}
242244

243245
for _, x5cAny := range x5cs {
244-
x5cRaw, ok := x5cAny.([]byte)
245-
if !ok {
246+
if raw, ok = x5cAny.([]byte); !ok {
246247
return ErrInvalidAttestation.WithDetails("Unable to parse attestation certificate from x5c during attestation validation").WithInfo(fmt.Sprintf("The first certificate in the attestation was type '%T' but '[]byte' was expected", x5cs[0]))
247248
}
248-
x5cParsed, err := x509.ParseCertificate(x5cRaw)
249-
if err != nil {
249+
250+
if parsed, err = x509.ParseCertificate(raw); err != nil {
250251
return ErrInvalidAttestation.WithDetails("Unable to parse attestation certificate from x5c during attestation validation").WithInfo(fmt.Sprintf("Error returned from x509.ParseCertificate: %+v", err))
251252
}
253+
252254
if x5c == nil {
253-
x5c = x5cParsed
255+
x5c = parsed
254256
} else {
255-
parents = append(parents, x5cParsed)
257+
x5cis = append(x5cis, parsed)
256258
}
257259
}
258260

259261
if attestationType == string(metadata.AttCA) {
260262
if err = tpmParseSANExtension(x5c); err != nil {
261263
return err
262264
}
265+
263266
if err = tpmRemoveEKU(x5c); err != nil {
264267
return err
265268
}
266-
for _, parent := range parents {
269+
270+
for _, parent := range x5cis {
267271
if err = tpmRemoveEKU(parent); err != nil {
268272
return err
269273
}
270274
}
271275
}
272276

273-
if x5c.Subject.CommonName != x5c.Issuer.CommonName {
277+
if x5c != nil && x5c.Subject.CommonName != x5c.Issuer.CommonName {
274278
if !entry.MetadataStatement.AttestationTypes.HasBasicFull() {
275279
return ErrInvalidAttestation.WithDetails("Unable to validate attestation statement signature during attestation validation: attestation with full attestation from authenticator that does not support full attestation")
276280
}
277-
verifier := entry.MetadataStatement.Verifier()
278-
if len(parents) != 0 {
279-
verifier.Intermediates = x509.NewCertPool()
280-
for _, parent := range parents {
281-
verifier.Intermediates.AddCert(parent)
282-
}
283-
}
281+
282+
verifier := entry.MetadataStatement.Verifier(x5cis)
283+
284284
if _, err = x5c.Verify(verifier); err != nil {
285285
return ErrInvalidAttestation.WithDetails(fmt.Sprintf("Unable to validate attestation signature statement during attestation validation: invalid certificate chain from MDS: %v", err))
286286
}

protocol/attestation_tpm.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,40 @@ var (
395395
oidExtensionSubjectAltName = []int{2, 5, 29, 17}
396396
oidExtensionExtendedKeyUsage = []int{2, 5, 29, 37}
397397
oidExtensionBasicConstraints = []int{2, 5, 29, 19}
398-
399-
// From wincrypt.h of Windows SDK.
400-
// Enhanced Key Usage for Privacy CA encryption certificate
401-
oidKpPrivacyCA = []int{1, 3, 6, 1, 4, 1, 311, 21, 36}
398+
oidKpPrivacyCA = []int{1, 3, 6, 1, 4, 1, 311, 21, 36}
402399
)
403400

404401
type tpmBasicConstraints struct {
405402
IsCA bool `asn1:"optional"`
406403
MaxPathLen int `asn1:"optional,default:-1"`
407404
}
408405

409-
// remove extension key usage to avoid ExtKeyUsage check failure
410-
// see also https://github.com/go-webauthn/webauthn/issues/342
406+
// Remove extension key usage to avoid ExtKeyUsage check failure.
411407
func tpmRemoveEKU(x5c *x509.Certificate) error {
412-
var unknown []asn1.ObjectIdentifier
413-
hasAiK := false
408+
var (
409+
unknown []asn1.ObjectIdentifier
410+
hasAiK bool
411+
)
412+
414413
for _, eku := range x5c.UnknownExtKeyUsage {
415414
if eku.Equal(tcgKpAIKCertificate) {
416415
hasAiK = true
416+
417417
continue
418418
}
419+
419420
if eku.Equal(oidKpPrivacyCA) {
420421
continue
421422
}
423+
422424
unknown = append(unknown, eku)
423425
}
426+
424427
if !hasAiK {
425428
return ErrAttestationFormat.WithDetails("AIK certificate missing EKU")
426429
}
430+
427431
x5c.UnknownExtKeyUsage = unknown
432+
428433
return nil
429434
}

0 commit comments

Comments
 (0)