Jelajahi Sumber

Add an attestation object to webhook requests

webhook.Attestation describes the evidence an ACME remote-attest-01
challenge verified: the challenge, the identifier's hint, the freshness
nonce, and the two digests the evidence binds, together with the
attestation result (format, media type, raw evidence, and the TDX,
SEV-SNP or Confidential Space data). It is sent as the attestation
field of the request body, set with WithAttestation. The existing
attestationData field, which carries device-attest-01's permanent
identifier, is unchanged.

WebhookController.AppendRequestBodyOptions lets ACME finalize add
request-body options to the controller its provisioner returned, for
data known only at finalize.

Claude-Session: https://claude.ai/code/session_01CjfEMLGpnHQaWsmxe1zmLF
Josh Drake 1 bulan lalu
induk
melakukan
3daddc6e18

+ 15 - 0
authority/provisioner/acme_remote_attestation_test.go

@@ -20,6 +20,7 @@ import (
 	"github.com/smallstep/certificates/attestation/confidentialspace"
 	"github.com/smallstep/certificates/attestation/confidentialspace"
 	cstest "github.com/smallstep/certificates/attestation/confidentialspace/confidentialspacetest"
 	cstest "github.com/smallstep/certificates/attestation/confidentialspace/confidentialspacetest"
 	"github.com/smallstep/certificates/authority/policy"
 	"github.com/smallstep/certificates/authority/policy"
+	"github.com/smallstep/certificates/webhook"
 )
 )
 
 
 const testRemoteAttestationAudience = "https://ca.example.com/acme/attested/directory"
 const testRemoteAttestationAudience = "https://ca.example.com/acme/attested/directory"
@@ -291,3 +292,17 @@ func TestIsJWTMediaType(t *testing.T) {
 	assert.False(t, isJWTMediaType("application/eat+cwt"))
 	assert.False(t, isJWTMediaType("application/eat+cwt"))
 	assert.False(t, isJWTMediaType(strings.Repeat(";", 3)))
 	assert.False(t, isJWTMediaType(strings.Repeat(";", 3)))
 }
 }
+
+func TestWebhookController_AppendRequestBodyOptions(t *testing.T) {
+	a := &webhook.Attestation{Challenge: "remote-attest-01"}
+	wc := &WebhookController{}
+	wc.AppendRequestBodyOptions(webhook.WithAttestation(a))
+
+	req := &webhook.RequestBody{}
+	require.NoError(t, wc.Enrich(context.Background(), req))
+	assert.Equal(t, a, req.Attestation)
+
+	req = &webhook.RequestBody{}
+	require.NoError(t, wc.Authorize(context.Background(), req))
+	assert.Equal(t, a, req.Attestation)
+}

+ 7 - 0
authority/provisioner/webhook.go

@@ -42,6 +42,13 @@ type WebhookController struct {
 	TemplateData  WebhookSetter
 	TemplateData  WebhookSetter
 }
 }
 
 
+// AppendRequestBodyOptions adds options to the bodies of the controller's
+// requests, for data that is known only after the controller is created, such
+// as the attestation an ACME order verified.
+func (wc *WebhookController) AppendRequestBodyOptions(opts ...webhook.RequestBodyOption) {
+	wc.options = append(wc.options, opts...)
+}
+
 // Enrich fetches data from remote servers and adds returned data to the
 // Enrich fetches data from remote servers and adds returned data to the
 // templateData
 // templateData
 func (wc *WebhookController) Enrich(ctx context.Context, req *webhook.RequestBody) error {
 func (wc *WebhookController) Enrich(ctx context.Context, req *webhook.RequestBody) error {

+ 51 - 0
webhook/attestation_test.go

@@ -0,0 +1,51 @@
+package webhook
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+
+	"github.com/smallstep/certificates/attestation"
+)
+
+func TestWithAttestation(t *testing.T) {
+	a := &Attestation{
+		Challenge:              "remote-attest-01",
+		Hint:                   "measured_boot",
+		FreshnessNonce:         "nonce",
+		KeyAuthorizationDigest: "00",
+		KeyDigest:              "01",
+		Result: &attestation.Result{
+			Format:    attestation.FormatTDX,
+			MediaType: "application/vnd.veraison.tsm-report+json",
+			Evidence:  []byte("quote"),
+			TDX:       &attestation.TDX{MRTD: "aa"},
+		},
+	}
+	rb, err := NewRequestBody(WithAttestation(a))
+	require.NoError(t, err)
+	assert.Equal(t, a, rb.Attestation)
+
+	// The result's fields are at the top level of the attestation object.
+	b, err := json.Marshal(rb)
+	require.NoError(t, err)
+	var got map[string]any
+	require.NoError(t, json.Unmarshal(b, &got))
+	att, ok := got["attestation"].(map[string]any)
+	require.True(t, ok)
+	assert.Equal(t, "remote-attest-01", att["challenge"])
+	assert.Equal(t, "measured_boot", att["hint"])
+	assert.Equal(t, "nonce", att["freshnessNonce"])
+	assert.Equal(t, "00", att["keyAuthorizationDigest"])
+	assert.Equal(t, "01", att["keyDigest"])
+	assert.Equal(t, "tdx", att["format"])
+	assert.Equal(t, "application/vnd.veraison.tsm-report+json", att["mediaType"])
+	assert.Equal(t, "cXVvdGU=", att["evidence"])
+	tdx, ok := att["tdx"].(map[string]any)
+	require.True(t, ok)
+	assert.Equal(t, "aa", tdx["mrtd"])
+	assert.NotContains(t, att, "sevSnp")
+	assert.NotContains(t, att, "confidentialSpace")
+}

+ 9 - 0
webhook/options.go

@@ -84,6 +84,15 @@ func WithAttestationData(data *AttestationData) RequestBodyOption {
 	}
 	}
 }
 }
 
 
+// WithAttestation sets the attestation evidence an ACME remote-attest-01
+// challenge verified.
+func WithAttestation(a *Attestation) RequestBodyOption {
+	return func(rb *RequestBody) error {
+		rb.Attestation = a
+		return nil
+	}
+}
+
 func WithAuthorizationPrincipal(p string) RequestBodyOption {
 func WithAuthorizationPrincipal(p string) RequestBodyOption {
 	return func(rb *RequestBody) error {
 	return func(rb *RequestBody) error {
 		rb.AuthorizationPrincipal = p
 		rb.AuthorizationPrincipal = p

+ 30 - 0
webhook/types.go

@@ -6,6 +6,8 @@ import (
 
 
 	"go.step.sm/crypto/sshutil"
 	"go.step.sm/crypto/sshutil"
 	"go.step.sm/crypto/x509util"
 	"go.step.sm/crypto/x509util"
+
+	"github.com/smallstep/certificates/attestation"
 )
 )
 
 
 // ResponseBody is the body returned by webhook servers.
 // ResponseBody is the body returned by webhook servers.
@@ -69,6 +71,32 @@ type AttestationData struct {
 	PermanentIdentifier string `json:"permanentIdentifier"`
 	PermanentIdentifier string `json:"permanentIdentifier"`
 }
 }
 
 
+// Attestation is the attestation evidence an ACME remote-attest-01 challenge
+// verified (draft-ietf-acme-rats), sent to webhook servers when the order is
+// finalized and given to certificate templates as .Attestation.
+//
+// The evidence binds two SHA-256 digests: of the challenge's key authorization,
+// and of the DER SubjectPublicKeyInfo of the certificate's key. The CA has
+// checked both, so the certificate's key is the attested one. Binary values are
+// hex encoded. The fields of the embedded attestation.Result, the format and the
+// verified measurements or claims, are at the top level.
+type Attestation struct {
+	// Challenge is the challenge type that verified the evidence,
+	// remote-attest-01.
+	Challenge string `json:"challenge"`
+	// Hint is the value of the order's remote-attestation identifier.
+	Hint string `json:"hint"`
+	// FreshnessNonce is the challenge's freshness nonce.
+	FreshnessNonce string `json:"freshnessNonce"`
+	// KeyAuthorizationDigest is the SHA-256 digest of the challenge's key
+	// authorization, which the evidence binds.
+	KeyAuthorizationDigest string `json:"keyAuthorizationDigest"`
+	// KeyDigest is the SHA-256 digest of the DER SubjectPublicKeyInfo of the
+	// certificate's key, which the evidence binds.
+	KeyDigest string `json:"keyDigest"`
+	*attestation.Result
+}
+
 // X5CCertificate is the authorization certificate sent to webhook servers for
 // X5CCertificate is the authorization certificate sent to webhook servers for
 // enriching or authorizing webhooks when signing X509 or SSH certificates using
 // enriching or authorizing webhooks when signing X509 or SSH certificates using
 // the X5C provisioner.
 // the X5C provisioner.
@@ -86,6 +114,8 @@ type RequestBody struct {
 	ProvisionerName string    `json:"provisionerName,omitempty"`
 	ProvisionerName string    `json:"provisionerName,omitempty"`
 	// Only set after successfully completing acme device-attest-01 challenge
 	// Only set after successfully completing acme device-attest-01 challenge
 	AttestationData *AttestationData `json:"attestationData,omitempty"`
 	AttestationData *AttestationData `json:"attestationData,omitempty"`
+	// Only set after successfully completing acme remote-attest-01 challenge
+	Attestation *Attestation `json:"attestation,omitempty"`
 	// Set for most provisioners, but not acme or scep
 	// Set for most provisioners, but not acme or scep
 	// Token any `json:"token,omitempty"`
 	// Token any `json:"token,omitempty"`
 	// Exactly one of the remaining fields should be set
 	// Exactly one of the remaining fields should be set