challenge_remote_attestation.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package acme
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "errors"
  10. "slices"
  11. "time"
  12. "go.step.sm/crypto/jose"
  13. "github.com/smallstep/certificates/attestation"
  14. "github.com/smallstep/certificates/attestation/cmw"
  15. "github.com/smallstep/certificates/authority/provisioner"
  16. "github.com/smallstep/certificates/webhook"
  17. )
  18. // remoteAttest01Validate validates a remote-attest-01 challenge
  19. // (draft-ietf-acme-rats-03). The payload is a CMW record (RFC 9999) holding the
  20. // evidence: a TDX quote or an SEV-SNP report in a configfs-tsm report, or a
  21. // Confidential Space token. step-ca profiles the draft, which defines no
  22. // binding, by requiring the evidence to bind first
  23. //
  24. // SHA-256(keyAuthorization || DER SubjectPublicKeyInfo of the certificate key)
  25. //
  26. // where keyAuthorization is freshnessNonce || '.' || base64url(JWK thumbprint
  27. // of the account key). The binding makes the evidence fresh, ties it to the
  28. // account, and attests the key the order will be finalized with. A TDX quote or
  29. // an SEV-SNP report carries it as the first half of its report data and the
  30. // relying party's nonce, its claim nonce, as the second half; a Confidential
  31. // Space token carries it, base64url encoded without padding, as its first
  32. // eat_nonce value, and the relying party's nonces after it.
  33. //
  34. // The certificate request is sent only when the order is finalized, so this
  35. // validation verifies the evidence and stores the key authorization, the
  36. // binding and the claim nonce on the authorization, and Finalize checks the
  37. // binding against the request's key. Evidence replayed from another challenge
  38. // validates the challenge but can never finalize the order.
  39. //
  40. // An empty payload, or an empty JSON object, leaves the challenge unchanged.
  41. func remoteAttest01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey, payload []byte) error {
  42. if p := bytes.TrimSpace(payload); len(p) == 0 || bytes.Equal(p, []byte("{}")) {
  43. return nil
  44. }
  45. if ch.FreshnessNonce == "" {
  46. return NewErrorISE("remote-attest-01 challenge %s has no freshness nonce", ch.ID)
  47. }
  48. record, err := cmw.Parse(payload)
  49. if err != nil {
  50. return WrapError(ErrorMalformedType, err, "error parsing remote attestation")
  51. }
  52. prov := MustProvisionerFromContext(ctx)
  53. var audience string
  54. if linker, ok := LinkerFromContext(ctx); ok {
  55. audience = linker.GetLink(ctx, DirectoryLinkType)
  56. }
  57. result, bound, err := prov.VerifyRemoteAttestation(ctx, record, audience)
  58. switch {
  59. case errors.Is(err, attestation.ErrInvalidEvidence):
  60. return storeError(ctx, db, ch, true, WrapDetailedError(ErrorBadAttestationStatementType, err, "%s", err))
  61. case err != nil:
  62. return WrapErrorISE(err, "error verifying remote attestation")
  63. }
  64. var claimNonce string
  65. switch len(bound) {
  66. case provisioner.RemoteAttestationBindingSize:
  67. case provisioner.RemoteAttestationBindingSize + provisioner.RemoteAttestationClaimNonceSize:
  68. claimNonce = base64.RawURLEncoding.EncodeToString(bound[provisioner.RemoteAttestationBindingSize:])
  69. default:
  70. return NewErrorISE("remote attestation binds %d bytes", len(bound))
  71. }
  72. keyAuth, err := KeyAuthorization(ch.FreshnessNonce, jwk)
  73. if err != nil {
  74. return err
  75. }
  76. // The handler takes the authorization from the request's URL, and a
  77. // store that does not keep a challenge's authorization cannot refuse a
  78. // challenge requested under another one, so the attestation is stored
  79. // only on the authorization that holds the challenge, for its account.
  80. az, err := db.GetAuthorization(ctx, ch.AuthorizationID)
  81. if err != nil {
  82. return WrapErrorISE(err, "error loading authorization")
  83. }
  84. if az.AccountID != ch.AccountID || !slices.ContainsFunc(az.Challenges, func(c *Challenge) bool { return c.ID == ch.ID }) {
  85. return NewError(ErrorUnauthorizedType, "challenge %s does not belong to authorization %s", ch.ID, ch.AuthorizationID)
  86. }
  87. az.Attestation = &webhook.Attestation{
  88. Challenge: string(REMOTEATTEST01),
  89. Hint: az.Identifier.Value,
  90. FreshnessNonce: ch.FreshnessNonce,
  91. KeyAuthorization: keyAuth,
  92. BindingDigest: hex.EncodeToString(bound[:provisioner.RemoteAttestationBindingSize]),
  93. ClaimNonce: claimNonce,
  94. Result: result,
  95. }
  96. ch.Status = StatusValid
  97. ch.Error = nil
  98. ch.ValidatedAt = clock.Now().Format(time.RFC3339)
  99. ch.PayloadFormat = string(result.Format)
  100. // TODO: add method to update authorization and challenge atomically.
  101. if err := db.UpdateAuthorization(ctx, az); err != nil {
  102. return WrapErrorISE(err, "error updating authorization")
  103. }
  104. if err := db.UpdateChallenge(ctx, ch); err != nil {
  105. return WrapErrorISE(err, "error updating challenge")
  106. }
  107. return nil
  108. }
  109. // RemoteAttestationBinding returns the hex encoded binding remote-attest-01
  110. // evidence carries for a certificate request: SHA-256 of the key authorization
  111. // followed by the DER SubjectPublicKeyInfo of the request's key.
  112. func RemoteAttestationBinding(keyAuthorization string, csr *x509.CertificateRequest) string {
  113. h := sha256.New()
  114. h.Write([]byte(keyAuthorization))
  115. h.Write(csr.RawSubjectPublicKeyInfo)
  116. return hex.EncodeToString(h.Sum(nil))
  117. }