Просмотр исходного кода

Bind attestation provenance to identifier

Josh Drake 3 месяцев назад
Родитель
Сommit
ee5adbf814
2 измененных файлов с 147 добавлено и 158 удалено
  1. 50 56
      acme/order.go
  2. 97 102
      acme/order_test.go

+ 50 - 56
acme/order.go

@@ -140,47 +140,37 @@ func (o *Order) UpdateStatus(ctx context.Context, db DB) error {
 	return nil
 }
 
-// getAuthorizationFingerprint returns a fingerprint from the list of authorizations. This
-// fingerprint is used on the device-attest-01 flow to verify the attestation
-// certificate public key with the CSR public key.
-//
-// There's no point on reading all the authorizations as there will be only one
-// for a permanent identifier.
-func (o *Order) getAuthorizationFingerprint(ctx context.Context, db DB) (string, error) {
-	for _, azID := range o.AuthorizationIDs {
-		az, err := db.GetAuthorization(ctx, azID)
-		if err != nil {
-			return "", WrapErrorISE(err, "error getting authorization %q", azID)
-		}
-		// There's no point on reading all the authorizations as there will
-		// be only one for a permanent identifier.
-		if az.Fingerprint != "" {
-			return az.Fingerprint, nil
-		}
-	}
-	return "", nil
+type authorizationAttestationData struct {
+	fingerprint string
+	format      string
 }
 
-// getAuthorizationAttestationFormat returns the validated attestation format
-// recorded by permanent-identifier authorizations. Empty values from legacy
-// authorization records remain unknown. Conflicting nonempty values are
-// rejected instead of choosing one.
-func (o *Order) getAuthorizationAttestationFormat(ctx context.Context, db DB) (string, error) {
-	var format string
+// getAuthorizationAttestationData returns the attested-key fingerprint and
+// validated format belonging to the selected permanent identifier. Empty
+// legacy formats remain unknown. Conflicting nonempty formats for duplicate
+// authorizations of that identifier are rejected instead of choosing one.
+func (o *Order) getAuthorizationAttestationData(ctx context.Context, db DB, permanentIdentifier string) (authorizationAttestationData, error) {
+	var data authorizationAttestationData
 	for _, azID := range o.AuthorizationIDs {
 		az, err := db.GetAuthorization(ctx, azID)
 		if err != nil {
-			return "", WrapErrorISE(err, "error getting authorization %q", azID)
+			return authorizationAttestationData{}, WrapErrorISE(err, "error getting authorization %q", azID)
+		}
+		if az.Identifier.Type != PermanentIdentifier || az.Identifier.Value != permanentIdentifier {
+			continue
+		}
+		if data.fingerprint == "" {
+			data.fingerprint = az.Fingerprint
 		}
-		if az.Identifier.Type != PermanentIdentifier || az.AttestationFormat == "" {
+		if az.AttestationFormat == "" {
 			continue
 		}
-		if format != "" && format != az.AttestationFormat {
-			return "", NewErrorISE("conflicting attestation formats %q and %q for order %s", format, az.AttestationFormat, o.ID)
+		if data.format != "" && data.format != az.AttestationFormat {
+			return authorizationAttestationData{}, NewErrorISE("conflicting attestation formats %q and %q for permanent identifier %q in order %s", data.format, az.AttestationFormat, permanentIdentifier, o.ID)
 		}
-		format = az.AttestationFormat
+		data.format = az.AttestationFormat
 	}
-	return format, nil
+	return data, nil
 }
 
 // Finalize signs a certificate if the necessary conditions for Order completion
@@ -208,14 +198,31 @@ func (o *Order) Finalize(ctx context.Context, db DB, csr *x509.CertificateReques
 		return NewErrorISE("unexpected status %s for order %s", o.Status, o.ID)
 	}
 
+	// Finalization currently issues for the first permanent identifier. Bind
+	// all attestation data to that exact identifier so another authorization
+	// cannot supply its fingerprint or provenance.
+	var permanentIdentifier string
+	for i := range o.Identifiers {
+		if o.Identifiers[i].Type == PermanentIdentifier {
+			permanentIdentifier = o.Identifiers[i].Value
+			break
+		}
+	}
+
+	var attestationData authorizationAttestationData
+	if permanentIdentifier != "" {
+		var err error
+		attestationData, err = o.getAuthorizationAttestationData(ctx, db, permanentIdentifier)
+		if err != nil {
+			return err
+		}
+	}
+
 	// Get key fingerprint if any. And then compare it with the CSR fingerprint.
 	//
 	// In device-attest-01 challenges we should check that the keys in the CSR
 	// and the attestation certificate are the same.
-	fingerprint, err := o.getAuthorizationFingerprint(ctx, db)
-	if err != nil {
-		return err
-	}
+	fingerprint := attestationData.fingerprint
 	if fingerprint != "" {
 		fp, err := keyutil.Fingerprint(csr.PublicKey)
 		if err != nil {
@@ -261,30 +268,17 @@ func (o *Order) Finalize(ctx context.Context, db DB, csr *x509.CertificateReques
 	// Custom sign options passed to authority.Sign
 	var extraOptions []provisioner.SignOption
 
-	// TODO: support for multiple identifiers?
-	var permanentIdentifier string
-	for i := range o.Identifiers {
-		if o.Identifiers[i].Type == PermanentIdentifier {
-			permanentIdentifier = o.Identifiers[i].Value
-			// the first (and only) Permanent Identifier that gets added to the certificate
-			// should be equal to the Subject Common Name if it's set. If not equal, the CSR
-			// is rejected, because the Common Name hasn't been challenged in that case. This
-			// could result in unauthorized access if a relying system relies on the Common
-			// Name in its authorization logic.
-			if csr.Subject.CommonName != "" && csr.Subject.CommonName != permanentIdentifier {
-				return NewError(ErrorBadCSRType, "CSR Subject Common Name does not match identifiers exactly: "+
-					"CSR Subject Common Name = %s, Order Permanent Identifier = %s", csr.Subject.CommonName, permanentIdentifier)
-			}
-			break
-		}
+	// TODO: support for multiple identifiers? The first (and only) Permanent
+	// Identifier that gets added to the certificate should be equal to the
+	// Subject Common Name if it's set. If not equal, the CSR is rejected,
+	// because the Common Name hasn't been challenged in that case.
+	if permanentIdentifier != "" && csr.Subject.CommonName != "" && csr.Subject.CommonName != permanentIdentifier {
+		return NewError(ErrorBadCSRType, "CSR Subject Common Name does not match identifiers exactly: "+
+			"CSR Subject Common Name = %s, Order Permanent Identifier = %s", csr.Subject.CommonName, permanentIdentifier)
 	}
 
 	var defaultTemplate string
 	if permanentIdentifier != "" {
-		attestationFormat, err := o.getAuthorizationAttestationFormat(ctx, db)
-		if err != nil {
-			return err
-		}
 		defaultTemplate = x509util.DefaultAttestedLeafTemplate
 		data.SetSubjectAlternativeNames(x509util.SubjectAlternativeName{
 			Type:  x509util.PermanentIdentifierType,
@@ -292,7 +286,7 @@ func (o *Order) Finalize(ctx context.Context, db DB, csr *x509.CertificateReques
 		})
 		extraOptions = append(extraOptions, provisioner.AttestationData{
 			PermanentIdentifier: permanentIdentifier,
-			Format:              attestationFormat,
+			Format:              attestationData.format,
 		})
 	} else {
 		defaultTemplate = x509util.DefaultLeafTemplate

+ 97 - 102
acme/order_test.go

@@ -835,6 +835,7 @@ func TestOrder_Finalize(t *testing.T) {
 					MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
 						return &Authorization{
 							ID:          id,
+							Identifier:  Identifier{Type: PermanentIdentifier, Value: "a-permanent-identifier"},
 							Fingerprint: "other-fingerprint",
 							Status:      StatusValid,
 						}, nil
@@ -2591,135 +2592,71 @@ func TestOrder_sans(t *testing.T) {
 	}
 }
 
-func TestOrder_getAuthorizationFingerprint(t *testing.T) {
-	ctx := context.Background()
-	type fields struct {
-		AuthorizationIDs []string
-	}
-	type args struct {
-		ctx context.Context
-		db  DB
-	}
-	tests := []struct {
-		name    string
-		fields  fields
-		args    args
-		want    string
-		wantErr bool
-	}{
-		{"ok", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
-			MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
-				return &Authorization{ID: id, Status: StatusValid}, nil
-			},
-		}}, "", false},
-		{"ok fingerprint", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
-			MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
-				if id == "az1" {
-					return &Authorization{ID: id, Status: StatusValid}, nil
-				}
-				return &Authorization{ID: id, Fingerprint: "fingerprint", Status: StatusValid}, nil
-			},
-		}}, "fingerprint", false},
-		{"fail", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
-			MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
-				return nil, errors.New("force")
-			},
-		}}, "", true},
-	}
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-			o := &Order{
-				AuthorizationIDs: tt.fields.AuthorizationIDs,
-			}
-			got, err := o.getAuthorizationFingerprint(tt.args.ctx, tt.args.db)
-			if (err != nil) != tt.wantErr {
-				t.Errorf("Order.getAuthorizationFingerprint() error = %v, wantErr %v", err, tt.wantErr)
-				return
-			}
-			if got != tt.want {
-				t.Errorf("Order.getAuthorizationFingerprint() = %v, want %v", got, tt.want)
-			}
-		})
-	}
-}
-
-func TestOrder_getAuthorizationAttestationFormat(t *testing.T) {
+func TestOrder_getAuthorizationAttestationDataMatchesIdentifier(t *testing.T) {
 	ctx := context.Background()
 	tests := []struct {
-		name    string
-		authzs  map[string]*Authorization
-		err     error
-		want    string
-		wantErr string
+		name                string
+		permanentIdentifier string
+		authzs              map[string]*Authorization
+		dbErr               error
+		want                authorizationAttestationData
+		wantErr             string
 	}{
 		{
-			name: "legacy empty format",
-			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier}},
-			},
-		},
-		{
-			name: "format",
-			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "apple"},
-			},
-			want: "apple",
-		},
-		{
-			name: "matching repeated formats",
+			name:                "matching identifier",
+			permanentIdentifier: "PID-A",
 			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "tpm"},
-				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "tpm"},
+				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, Fingerprint: "fingerprint-a", AttestationFormat: "apple"},
+				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-B"}, Fingerprint: "fingerprint-b", AttestationFormat: "tpm"},
 			},
-			want: "tpm",
+			want: authorizationAttestationData{fingerprint: "fingerprint-a", format: "apple"},
 		},
 		{
-			name: "known format with legacy empty format",
+			name:                "legacy selected identifier ignores later format",
+			permanentIdentifier: "PID-A",
 			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier}},
-				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "step"},
+				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, Fingerprint: "fingerprint-a"},
+				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-B"}, Fingerprint: "fingerprint-b", AttestationFormat: "tpm"},
 			},
-			want: "step",
+			want: authorizationAttestationData{fingerprint: "fingerprint-a"},
 		},
 		{
-			name: "ignore non-permanent identifier",
+			name:                "matching repeated format",
+			permanentIdentifier: "PID-A",
 			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: DNS}, AttestationFormat: "step"},
-				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "android-key"},
+				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, Fingerprint: "fingerprint-a", AttestationFormat: "step"},
+				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, AttestationFormat: "step"},
 			},
-			want: "android-key",
+			want: authorizationAttestationData{fingerprint: "fingerprint-a", format: "step"},
 		},
 		{
-			name: "conflicting formats",
+			name:                "conflicting formats for selected identifier",
+			permanentIdentifier: "PID-A",
 			authzs: map[string]*Authorization{
-				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "apple"},
-				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier}, AttestationFormat: "step"},
+				"az1": {ID: "az1", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, AttestationFormat: "apple"},
+				"az2": {ID: "az2", Identifier: Identifier{Type: PermanentIdentifier, Value: "PID-A"}, AttestationFormat: "tpm"},
 			},
 			wantErr: "conflicting attestation formats",
 		},
 		{
-			name:    "database error",
-			err:     errors.New("force"),
-			wantErr: "error getting authorization \"az1\": force",
+			name:                "database error",
+			permanentIdentifier: "PID-A",
+			dbErr:               errors.New("force"),
+			wantErr:             "error getting authorization \"az1\": force",
 		},
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			o := &Order{AuthorizationIDs: []string{"az1", "az2"}}
-			db := &MockDB{
-				MockGetAuthorization: func(_ context.Context, id string) (*Authorization, error) {
-					if tt.err != nil {
-						return nil, tt.err
-					}
-					if az, ok := tt.authzs[id]; ok {
-						return az, nil
-					}
-					return &Authorization{ID: id, Identifier: Identifier{Type: DNS}}, nil
-				},
-			}
+			o := &Order{ID: "orderID", AuthorizationIDs: []string{"az1", "az2"}}
+			db := &MockDB{MockGetAuthorization: func(_ context.Context, id string) (*Authorization, error) {
+				if tt.dbErr != nil {
+					return nil, tt.dbErr
+				}
+				return tt.authzs[id], nil
+			}}
 
-			got, err := o.getAuthorizationAttestationFormat(ctx, db)
+			got, err := o.getAuthorizationAttestationData(ctx, db, tt.permanentIdentifier)
 			if tt.wantErr != "" {
 				if assert.Error(t, err) {
 					assert.True(t, strings.Contains(err.Error(), tt.wantErr))
@@ -2793,6 +2730,64 @@ func TestOrder_FinalizeAttestationData(t *testing.T) {
 	}
 }
 
+func TestOrder_FinalizeUsesAttestationDataForSelectedPermanentIdentifier(t *testing.T) {
+	signer, err := keyutil.GenerateSigner("EC", "P-256", 0)
+	assert.FatalError(t, err)
+	fingerprint, err := keyutil.Fingerprint(signer.Public())
+	assert.FatalError(t, err)
+
+	o := &Order{
+		ID:               "orderID",
+		AccountID:        "accountID",
+		Status:           StatusReady,
+		ExpiresAt:        clock.Now().Add(time.Minute),
+		AuthorizationIDs: []string{"authz-a", "authz-b"},
+		Identifiers: []Identifier{
+			{Type: PermanentIdentifier, Value: "PID-A"},
+			{Type: PermanentIdentifier, Value: "PID-B"},
+		},
+	}
+	csr := &x509.CertificateRequest{Subject: pkix.Name{CommonName: "PID-A"}, PublicKey: signer.Public()}
+	db := &MockDB{
+		MockGetAuthorization: func(_ context.Context, id string) (*Authorization, error) {
+			if id == "authz-a" {
+				return &Authorization{
+					ID:          id,
+					Identifier:  Identifier{Type: PermanentIdentifier, Value: "PID-A"},
+					Fingerprint: fingerprint,
+				}, nil
+			}
+			return &Authorization{
+				ID:                id,
+				Identifier:        Identifier{Type: PermanentIdentifier, Value: "PID-B"},
+				Fingerprint:       "different-fingerprint",
+				AttestationFormat: "tpm",
+			}, nil
+		},
+		MockCreateCertificate: func(_ context.Context, cert *Certificate) error {
+			cert.ID = "certID"
+			return nil
+		},
+		MockUpdateOrder: func(_ context.Context, _ *Order) error { return nil },
+	}
+	ca := &mockSignAuth{signWithContext: func(_ context.Context, _ *x509.CertificateRequest, _ provisioner.SignOptions, opts ...provisioner.SignOption) ([]*x509.Certificate, error) {
+		var got provisioner.AttestationData
+		for _, opt := range opts {
+			if data, ok := opt.(provisioner.AttestationData); ok {
+				got = data
+			}
+		}
+		assert.Equals(t, got, provisioner.AttestationData{PermanentIdentifier: "PID-A"})
+		return []*x509.Certificate{{PublicKey: signer.Public()}}, nil
+	}}
+	prov := &MockProvisioner{
+		MauthorizeSign: func(context.Context, string) ([]provisioner.SignOption, error) { return nil, nil },
+		MgetOptions:    func() *provisioner.Options { return nil },
+	}
+
+	assert.FatalError(t, o.Finalize(context.Background(), db, csr, ca, prov))
+}
+
 func TestOrder_FinalizeConflictingAttestationFormats(t *testing.T) {
 	o := &Order{
 		ID:               "orderID",