Ver código fonte

Improve `android-key` x5c verification

The x5c verification now also checks the leaf to not have been
revoked, and the Android Key Attestation extension is searched
for in reverse order.
Herman Slatman 4 meses atrás
pai
commit
b335becce2
3 arquivos alterados com 55 adições e 37 exclusões
  1. 41 30
      acme/challenge.go
  2. 11 4
      authority/provisioner/acme.go
  3. 3 3
      authority/provisioner/provisioner.go

+ 41 - 30
acme/challenge.go

@@ -849,7 +849,7 @@ func deviceAttest01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose
 
 	switch format {
 	case "android-key":
-		data, err := doAndroidKeyAttestionFormat(ctx, prov, ch, jwk, &att)
+		data, err := doAndroidKeyAttestationFormat(ctx, prov, ch, jwk, &att)
 		if err != nil {
 			var acmeError *Error
 			if errors.As(err, &acmeError) {
@@ -1462,8 +1462,12 @@ type androidKeyAttestationData struct {
 	Attestation *attestation.KeyDescription
 }
 
+// findAndroidAttestationCert traverses the slice of [*x509.Certificate] from
+// end to start (root -> intermediate -> leaf) to locate the certificate closest
+// to the root carrying an Android Key Attestation extension.
 func findAndroidAttestationCert(certs []*x509.Certificate) *x509.Certificate {
-	for _, cert := range certs {
+	for i := len(certs) - 1; i >= 0; i-- {
+		cert := certs[i]
 		for _, ext := range cert.Extensions {
 			if ext.Id.Equal(oidAndroidAttestation) {
 				return cert
@@ -1474,7 +1478,7 @@ func findAndroidAttestationCert(certs []*x509.Certificate) *x509.Certificate {
 	return nil
 }
 
-// doAndroidKeyAttestionFormat handles ACME Device Attestation requests for
+// doAndroidKeyAttestationFormat handles ACME Device Attestation requests for
 // the "android-key" attestation format. Its verification logic is based on
 // the documentation at https://developer.android.com/privacy-and-security/security-key-attestation
 //
@@ -1486,8 +1490,11 @@ func findAndroidAttestationCert(certs []*x509.Certificate) *x509.Certificate {
 //     See the section about the provisioning information extension for more details.
 //  6. Find the nearest certificate to the root that contains the key attestation certificate extension. If the provisioning information certificate extension was present, the key attestation certificate extension must be in the immediately subsequent certificate. Use the parser to extract the key attestation certificate extension data from that certificate.
 //  7. Check the extension data that you've retrieved in the previous steps for consistency and compare with the set of values that you expect the hardware-backed key to contain.
-func doAndroidKeyAttestionFormat(ctx context.Context, prov Provisioner, ch *Challenge, jwk *jose.JSONWebKey, att *attestationObject) (*androidKeyAttestationData, error) {
-	acmeProv := prov.(*provisioner.ACME)
+func doAndroidKeyAttestationFormat(ctx context.Context, prov Provisioner, ch *Challenge, jwk *jose.JSONWebKey, att *attestationObject) (*androidKeyAttestationData, error) {
+	acmeProv, ok := prov.(*provisioner.ACME)
+	if !ok {
+		return nil, NewErrorISE("provisioner in context is not an ACME provisioner")
+	}
 
 	roots, ok := prov.GetAttestationRoots()
 	if !ok {
@@ -1512,29 +1519,18 @@ func doAndroidKeyAttestionFormat(ctx context.Context, prov Provisioner, ch *Chal
 	if len(x5c) == 0 {
 		return nil, NewDetailedError(ErrorRejectedIdentifierType, "x5c is empty")
 	}
-	der, ok := x5c[0].([]byte)
-	if !ok {
-		return nil, NewDetailedError(ErrorBadAttestationStatementType, "x5c[0] is malformed")
-	}
-	leaf, err := x509.ParseCertificate(der)
-	if err != nil {
-		return nil, WrapDetailedError(ErrorBadAttestationStatementType, err, "failed to parse leaf certificate")
-	}
-
-	var certs = make([]*x509.Certificate, 0, len(x5c))
-	certs = append(certs, leaf)
 
 	// Parse intermediates and root
 	intermediates := x509.NewCertPool()
-	var root *x509.Certificate
-	for i, v := range x5c[1:] {
+	var leaf, root *x509.Certificate
+	for i, v := range x5c {
 		der, ok := v.([]byte)
 		if !ok {
 			return nil, NewDetailedError(ErrorBadAttestationStatementType, "x5c element is malformed")
 		}
 		cert, err := x509.ParseCertificate(der)
 		if err != nil {
-			return nil, WrapDetailedError(ErrorBadAttestationStatementType, err, "failed parsing certificate at index %d", i+1)
+			return nil, WrapDetailedError(ErrorBadAttestationStatementType, err, "failed parsing certificate at index %d", i)
 		}
 
 		// Verify certificate serial number against CRL
@@ -1546,39 +1542,54 @@ func doAndroidKeyAttestionFormat(ctx context.Context, prov Provisioner, ch *Chal
 			return nil, NewDetailedError(ErrorBadAttestationStatementType, "x5c element contains a revoked certificate")
 		}
 
-		if i == len(x5c)-2 {
-			// Last cert = root
-			certs = append(certs, cert)
-			root = cert // TODO: perform additional validation?
-		} else {
-			certs = append(certs, cert)
+		switch i {
+		case 0: // leaf
+			leaf = cert
+		case len(x5c) - 1: // root
+			root = cert
+		default: // intermediates
 			intermediates.AddCert(cert)
 		}
 	}
 
+	// Require a leaf in the chain
+	if leaf == nil {
+		return nil, NewDetailedError(ErrorBadAttestationStatementType, "missing leaf certificate in x5c chain")
+	}
+
 	// Require a root in the chain
 	if root == nil {
 		return nil, NewDetailedError(ErrorBadAttestationStatementType, "missing root certificate in x5c chain")
 	}
 
 	// Verify the root is one of the trusted roots
-	if _, err := root.Verify(x509.VerifyOptions{
+	_, err := root.Verify(x509.VerifyOptions{
 		Roots:       roots, // TODO: ensure Verify does the right thing for this case
 		CurrentTime: time.Now().Truncate(time.Second),
-	}); err != nil {
+	})
+	if err != nil {
 		return nil, NewDetailedError(ErrorBadAttestationStatementType, "root certificate in chain is not trusted")
 	}
 
 	// Validate the full chain including root as trust anchor
-	if _, err := leaf.Verify(x509.VerifyOptions{
+	chains, err := leaf.Verify(x509.VerifyOptions{
 		Intermediates: intermediates,
 		Roots:         roots,
 		CurrentTime:   time.Now().Truncate(time.Second),
 		KeyUsages:     []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
-	}); err != nil {
+	})
+	if err != nil {
 		return nil, WrapDetailedError(ErrorBadAttestationStatementType, err, "x5c chain verification failed")
 	}
 
+	switch {
+	case len(chains) == 0:
+		return nil, NewDetailedError(ErrorBadAttestationStatementType, "x5c does not contain a valid certificate chain")
+	case len(chains) > 1:
+		// currently we strictly prohibit multiple valid signing chains
+		return nil, NewDetailedError(ErrorBadAttestationStatementType, "x5c contains multiple (%d) valid certificate chains", len(chains))
+	}
+
 	// Get signature
 	sig, ok := att.AttStatement["sig"].([]byte)
 	if !ok {
@@ -1591,7 +1602,7 @@ func doAndroidKeyAttestionFormat(ctx context.Context, prov Provisioner, ch *Chal
 	}
 
 	// Find the attestation certificate
-	attCert := findAndroidAttestationCert(certs) // TODO: Google docs describe "closest to the root"
+	attCert := findAndroidAttestationCert(chains[0])
 	if attCert == nil {
 		return nil, NewDetailedError(ErrorBadAttestationStatementType, "no attestation certificate with OID 1.3.6.1.4.1.11129.2.1.17 found in the cert chain")
 	}

+ 11 - 4
authority/provisioner/acme.go

@@ -241,9 +241,13 @@ func (p *ACME) loadAndroidCRL(ctx context.Context) error {
 			Reason string `json:"reason"`
 		} `json:"entries"`
 	}
-	res, err := p.ctl.GetHTTPClient().Get(androidAttestationStatusURL)
+	req, err := http.NewRequestWithContext(ctx, http.MethodGet, androidAttestationStatusURL, http.NoBody)
 	if err != nil {
-		return fmt.Errorf("client: error making Android CRL request: %s\n", err)
+		return fmt.Errorf("failed creating Android CRL request: %w", err)
+	}
+	res, err := p.ctl.GetHTTPClient().Do(req)
+	if err != nil {
+		return fmt.Errorf("failed performing Android CRL request: %w", err)
 	}
 	defer res.Body.Close()
 
@@ -256,7 +260,10 @@ func (p *ACME) loadAndroidCRL(ctx context.Context) error {
 		return fmt.Errorf("error decoding Android CRL JSON: %w", err)
 	}
 
-	// Extract keys into a slice
+	// Extract serials into a slice. Currently no distinction is being made
+	// based on the status of the certificate. The status can be "REVOKED"
+	// and "SUSPENDED". Both statuses will result in a certificate to be
+	// considered revoked.
 	serials := make([]string, 0, len(CRLResponse.Entries))
 	for k := range CRLResponse.Entries {
 		serials = append(serials, k)
@@ -463,7 +470,7 @@ func (p *ACME) IsAndroidCertificateRevoked(ctx context.Context, cert *x509.Certi
 	if p.androidCRLFetchedAt.IsZero() || time.Now().After(p.androidCRLFetchedAt.Add(24*time.Hour)) {
 		// reinitialize the Android CRL
 		if err := p.loadAndroidCRL(ctx); err != nil {
-			return false, err
+			return true, err
 		}
 	}
 

+ 3 - 3
authority/provisioner/provisioner.go

@@ -282,9 +282,9 @@ type Config struct {
 	// WrapTransport references the function that should wrap any [http.Transport] initialized
 	// down the Config's chain.
 	WrapTransport TransportWrapper
-	// AndroidKeyCRLProvider references an implementation of [androidkey.CRLProvider]
-	// that is responsible for resolving revoked Android Key Attestation certificate
-	// numbers.
+	// AndroidKeyCRLChecker references an implementation of [androidkey.CRLChecker]
+	// that is responsible for checking revoked Android Key Attestation certificate
+	// serial numbers.
 	AndroidKeyCRLChecker androidkey.CRLChecker
 }