Pārlūkot izejas kodu

Refuse a remote-attest-01 challenge under another authorization

The challenge handler takes the authorization from the request's URL and
the nosql store ignores it, so a remote-attest-01 challenge requested
under another authorization of the same account stored its attestation
on that authorization and made its own valid without one. Finalize
checked the binding only where an attestation was stored, so the order
of the challenge then finalized with any key: evidence replayed from
another challenge, which validates since the binding is checked only at
finalize, issued a certificate for a key no evidence attested. Step CA
Pro's store, which finds a challenge only in its own authorization, was
not exposed.

The validator now stores the attestation only on an authorization that
holds the challenge, for the challenge's account, and refuses with
unauthorized otherwise; and finalize refuses a remote-attestation
authorization without an attestation, whatever made it valid.

Found by an adversarial review of the branch. device-attest-01's
validator has the same shape with the key fingerprint upstream.

Claude-Session: https://claude.ai/code/session_01CjfEMLGpnHQaWsmxe1zmLF
Josh Drake 1 mēnesi atpakaļ
vecāks
revīzija
3a8e37d160

+ 8 - 0
acme/challenge_remote_attestation.go

@@ -8,6 +8,7 @@ import (
 	"encoding/base64"
 	"encoding/base64"
 	"encoding/hex"
 	"encoding/hex"
 	"errors"
 	"errors"
+	"slices"
 	"time"
 	"time"
 
 
 	"go.step.sm/crypto/jose"
 	"go.step.sm/crypto/jose"
@@ -81,10 +82,17 @@ func remoteAttest01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose
 		return err
 		return err
 	}
 	}
 
 
+	// The handler takes the authorization from the request's URL, and a
+	// store that does not keep a challenge's authorization cannot refuse a
+	// challenge requested under another one, so the attestation is stored
+	// only on the authorization that holds the challenge, for its account.
 	az, err := db.GetAuthorization(ctx, ch.AuthorizationID)
 	az, err := db.GetAuthorization(ctx, ch.AuthorizationID)
 	if err != nil {
 	if err != nil {
 		return WrapErrorISE(err, "error loading authorization")
 		return WrapErrorISE(err, "error loading authorization")
 	}
 	}
+	if az.AccountID != ch.AccountID || !slices.ContainsFunc(az.Challenges, func(c *Challenge) bool { return c.ID == ch.ID }) {
+		return NewError(ErrorUnauthorizedType, "challenge %s does not belong to authorization %s", ch.ID, ch.AuthorizationID)
+	}
 	az.Attestation = &webhook.Attestation{
 	az.Attestation = &webhook.Attestation{
 		Challenge:        string(REMOTEATTEST01),
 		Challenge:        string(REMOTEATTEST01),
 		Hint:             az.Identifier.Value,
 		Hint:             az.Identifier.Value,

+ 38 - 11
acme/challenge_remote_attestation_test.go

@@ -90,6 +90,7 @@ func TestRemoteAttest01Validate(t *testing.T) {
 		payload []byte
 		payload []byte
 		nonce   string
 		nonce   string
 		verify  func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error)
 		verify  func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error)
+		az      func(az *Authorization)
 		want    want
 		want    want
 	}{
 	}{
 		{"ok confidential space", testRemoteAttestationCMW, testFreshnessNonce,
 		{"ok confidential space", testRemoteAttestationCMW, testFreshnessNonce,
@@ -97,37 +98,49 @@ func TestRemoteAttest01Validate(t *testing.T) {
 				assert.Equal(t, "application/eat+jwt", r.MediaType)
 				assert.Equal(t, "application/eat+jwt", r.MediaType)
 				assert.Equal(t, "https://ca.example.com/acme/acme/directory", aud)
 				assert.Equal(t, "https://ca.example.com/acme/acme/directory", aud)
 				return csResult, binding, nil
 				return csResult, binding, nil
-			}, want{status: StatusValid, stored: true, format: "confidential-space", result: csResult}},
+			}, nil, want{status: StatusValid, stored: true, format: "confidential-space", result: csResult}},
 		// A quote's report data carries the relying party's claim nonce after
 		// A quote's report data carries the relying party's claim nonce after
 		// the binding.
 		// the binding.
 		{"ok tdx with claim nonce", testRemoteAttestationCMW, testFreshnessNonce,
 		{"ok tdx with claim nonce", testRemoteAttestationCMW, testFreshnessNonce,
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 				return tdxResult, append(append([]byte(nil), binding...), claimNonce...), nil
 				return tdxResult, append(append([]byte(nil), binding...), claimNonce...), nil
-			}, want{status: StatusValid, stored: true, format: "tdx", result: tdxResult,
+			}, nil, want{status: StatusValid, stored: true, format: "tdx", result: tdxResult,
 				claimNonce: base64.RawURLEncoding.EncodeToString(claimNonce)}},
 				claimNonce: base64.RawURLEncoding.EncodeToString(claimNonce)}},
 		// The binding is checked when the order is finalized, so evidence
 		// The binding is checked when the order is finalized, so evidence
 		// with any binding validates the challenge.
 		// with any binding validates the challenge.
 		{"ok binding checked at finalize", testRemoteAttestationCMW, testFreshnessNonce,
 		{"ok binding checked at finalize", testRemoteAttestationCMW, testFreshnessNonce,
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 				return csResult, make([]byte, 32), nil
 				return csResult, make([]byte, 32), nil
-			}, want{status: StatusValid, stored: true, format: "confidential-space", result: csResult}},
-		{"ok empty payload", nil, testFreshnessNonce, nil, want{status: StatusPending}},
-		{"ok empty object", []byte(" {} "), testFreshnessNonce, nil, want{status: StatusPending}},
-		{"fail no nonce", testRemoteAttestationCMW, "", nil, want{status: StatusPending, err: "has no freshness nonce"}},
-		{"fail malformed", []byte(`["application/eat+jwt"]`), testFreshnessNonce, nil,
+			}, nil, want{status: StatusValid, stored: true, format: "confidential-space", result: csResult}},
+		{"ok empty payload", nil, testFreshnessNonce, nil, nil, want{status: StatusPending}},
+		{"ok empty object", []byte(" {} "), testFreshnessNonce, nil, nil, want{status: StatusPending}},
+		{"fail no nonce", testRemoteAttestationCMW, "", nil, nil, want{status: StatusPending, err: "has no freshness nonce"}},
+		{"fail malformed", []byte(`["application/eat+jwt"]`), testFreshnessNonce, nil, nil,
 			want{status: StatusPending, err: "must have 2 or 3 elements"}},
 			want{status: StatusPending, err: "must have 2 or 3 elements"}},
 		{"fail invalid evidence", testRemoteAttestationCMW, testFreshnessNonce,
 		{"fail invalid evidence", testRemoteAttestationCMW, testFreshnessNonce,
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 				return nil, nil, attestation.Invalidf("attestation token dbgstat is %q", "enabled")
 				return nil, nil, attestation.Invalidf("attestation token dbgstat is %q", "enabled")
-			}, want{status: StatusInvalid, errType: "badAttestationStatement", errDetail: `attestation token dbgstat is "enabled"`}},
+			}, nil, want{status: StatusInvalid, errType: "badAttestationStatement", errDetail: `attestation token dbgstat is "enabled"`}},
 		{"fail verifier", testRemoteAttestationCMW, testFreshnessNonce,
 		{"fail verifier", testRemoteAttestationCMW, testFreshnessNonce,
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 				return nil, nil, errors.New("error fetching tdx collateral")
 				return nil, nil, errors.New("error fetching tdx collateral")
-			}, want{status: StatusPending, err: "error verifying remote attestation"}},
+			}, nil, want{status: StatusPending, err: "error verifying remote attestation"}},
 		{"fail bound size", testRemoteAttestationCMW, testFreshnessNonce,
 		{"fail bound size", testRemoteAttestationCMW, testFreshnessNonce,
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
 				return csResult, binding[:16], nil
 				return csResult, binding[:16], nil
-			}, want{status: StatusPending, err: "binds 16 bytes"}},
+			}, nil, want{status: StatusPending, err: "binds 16 bytes"}},
+		// The challenge is requested under an authorization from the URL; the
+		// attestation must not land on one that does not hold the challenge.
+		{"fail challenge of another authorization", testRemoteAttestationCMW, testFreshnessNonce,
+			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
+				return csResult, binding, nil
+			}, func(az *Authorization) { az.Challenges = []*Challenge{{ID: "otherChID"}} },
+			want{status: StatusPending, err: "does not belong to authorization"}},
+		{"fail authorization of another account", testRemoteAttestationCMW, testFreshnessNonce,
+			func(context.Context, *cmw.Record, string) (*attestation.Result, []byte, error) {
+				return csResult, binding, nil
+			}, func(az *Authorization) { az.AccountID = "otherAccID" },
+			want{status: StatusPending, err: "does not belong to authorization"}},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -138,7 +151,14 @@ func TestRemoteAttest01Validate(t *testing.T) {
 			db := &MockDB{
 			db := &MockDB{
 				MockGetAuthorization: func(_ context.Context, id string) (*Authorization, error) {
 				MockGetAuthorization: func(_ context.Context, id string) (*Authorization, error) {
 					assert.Equal(t, "azID", id)
 					assert.Equal(t, "azID", id)
-					return &Authorization{ID: id, Identifier: Identifier{Type: RemoteAttestation, Value: "measured_boot"}}, nil
+					az := &Authorization{
+						ID: id, AccountID: "accID", Identifier: Identifier{Type: RemoteAttestation, Value: "measured_boot"},
+						Challenges: []*Challenge{{ID: "chID"}},
+					}
+					if tt.az != nil {
+						tt.az(az)
+					}
+					return az, nil
 				},
 				},
 				MockUpdateAuthorization: func(_ context.Context, az *Authorization) error {
 				MockUpdateAuthorization: func(_ context.Context, az *Authorization) error {
 					updatedAz = az
 					updatedAz = az
@@ -157,6 +177,7 @@ func TestRemoteAttest01Validate(t *testing.T) {
 			ctx = NewLinkerContext(ctx, NewLinker("ca.example.com", "acme"))
 			ctx = NewLinkerContext(ctx, NewLinker("ca.example.com", "acme"))
 			ch := &Challenge{
 			ch := &Challenge{
 				ID:              "chID",
 				ID:              "chID",
+				AccountID:       "accID",
 				AuthorizationID: "azID",
 				AuthorizationID: "azID",
 				Type:            REMOTEATTEST01,
 				Type:            REMOTEATTEST01,
 				Status:          StatusPending,
 				Status:          StatusPending,
@@ -243,6 +264,12 @@ func TestOrder_checkAttestedKeys(t *testing.T) {
 			"csr does not match the attested key"},
 			"csr does not match the attested key"},
 		{"fail attestation without key authorization", map[string]*Authorization{"az1": {Attestation: &webhook.Attestation{}}, "az2": {}}, nil,
 		{"fail attestation without key authorization", map[string]*Authorization{"az1": {Attestation: &webhook.Attestation{}}, "az2": {}}, nil,
 			"csr does not match the attested key"},
 			"csr does not match the attested key"},
+		// A remote-attestation authorization made valid without its
+		// challenge's attestation (a lost update, a challenge requested under
+		// another authorization) fails closed.
+		{"fail remote attestation without attestation", map[string]*Authorization{
+			"az1": {Identifier: Identifier{Type: RemoteAttestation}}, "az2": {},
+		}, nil, "has no attestation for its remote-attestation identifier"},
 		// Every attested key is checked, not only the first one found.
 		// Every attested key is checked, not only the first one found.
 		{"fail second fingerprint", map[string]*Authorization{"az1": {Fingerprint: fp}, "az2": {Fingerprint: otherFP}}, nil,
 		{"fail second fingerprint", map[string]*Authorization{"az1": {Fingerprint: fp}, "az2": {Fingerprint: otherFP}}, nil,
 			"csr does not match the attested key"},
 			"csr does not match the attested key"},

+ 6 - 0
acme/order.go

@@ -171,6 +171,12 @@ func (o *Order) checkAttestedKeys(ctx context.Context, db DB, csr *x509.Certific
 				return nil, NewError(ErrorUnauthorizedType, "order %s csr does not match the attested key", o.ID)
 				return nil, NewError(ErrorUnauthorizedType, "order %s csr does not match the attested key", o.ID)
 			}
 			}
 		}
 		}
+		// A remote-attestation authorization is valid only through its
+		// challenge, which stores the attestation: one without it never
+		// finalizes, whatever made it valid.
+		if az.Identifier.Type == RemoteAttestation && az.Attestation == nil {
+			return nil, NewError(ErrorUnauthorizedType, "order %s has no attestation for its remote-attestation identifier", o.ID)
+		}
 		if az.Attestation != nil {
 		if az.Attestation != nil {
 			if az.Attestation.KeyAuthorization == "" {
 			if az.Attestation.KeyAuthorization == "" {
 				return nil, NewError(ErrorUnauthorizedType, "order %s csr does not match the attested key", o.ID)
 				return nil, NewError(ErrorUnauthorizedType, "order %s csr does not match the attested key", o.ID)