attestation.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package acme
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/sha256"
  6. "crypto/subtle"
  7. "errors"
  8. "slices"
  9. "github.com/fxamacker/cbor/v2"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. "go.step.sm/crypto/jose"
  12. )
  13. // DeviceAttestationParams supplies the expected device-attest-01 bindings.
  14. // The caller must authorize Identifier and issue, expire, and consume Token.
  15. // AccountKey is the ACME account key used to form the key authorization; Apple
  16. // statements bind directly to Token and do not use AccountKey.
  17. type DeviceAttestationParams struct {
  18. Token string
  19. Identifier string
  20. AccountKey *jose.JSONWebKey
  21. }
  22. // DeviceAttestationResult describes the verified statement. Fingerprint uses
  23. // keyutil.Fingerprint's default encoding and must be compared with the key the
  24. // caller intends to enroll or certify. It is not an authorization decision.
  25. type DeviceAttestationResult struct {
  26. Format string
  27. Fingerprint string
  28. // ChallengeBound is false for legacy Apple statements without a nonce.
  29. ChallengeBound bool
  30. // IdentifierBound reports a nonempty expected identifier attested by the statement.
  31. // It is false for TPM statements without permanent identifiers.
  32. // ACME binds these later through the CSR subject; standalone callers must
  33. // apply an equivalent check or reject the result.
  34. IdentifierBound bool
  35. }
  36. // VerifyDeviceAttestation verifies a CBOR device-attest-01 attestation object
  37. // without reading or updating an ACME database. It enforces the provisioner's
  38. // format and trust-root policy, signatures, and the challenge and identifier
  39. // checks used by ACME. Android additionally requires a *provisioner.ACME for
  40. // its Android-specific policies. attObj is raw CBOR, not the base64url-encoded
  41. // JSON challenge response.
  42. //
  43. // Existing ACME compatibility is preserved: Apple statements may omit their
  44. // nonce and TPM statements may omit permanent identifiers. Callers must inspect
  45. // ChallengeBound and IdentifierBound and reject absent bindings unless they
  46. // provide equivalent assurance elsewhere. A successful result alone does not
  47. // prove freshness: the caller must enforce Token's lifecycle and compare the
  48. // returned Fingerprint with the public key being enrolled or certified.
  49. func VerifyDeviceAttestation(ctx context.Context, prov Provisioner, params DeviceAttestationParams, attObj []byte) (*DeviceAttestationResult, error) {
  50. ch := &Challenge{Token: params.Token, Value: params.Identifier}
  51. jwk := params.AccountKey
  52. if len(attObj) == 0 || bytes.Equal(attObj, []byte("{}")) {
  53. return nil, NewDetailedError(ErrorBadAttestationStatementType, "attObj must not be empty")
  54. }
  55. cborDecoderOptions := cbor.DecOptions{}
  56. cborDecoder, err := cborDecoderOptions.DecMode()
  57. if err != nil {
  58. return nil, WrapErrorISE(err, "failed creating CBOR decoder")
  59. }
  60. if err := cborDecoder.Wellformed(attObj); err != nil {
  61. return nil, NewDetailedError(ErrorBadAttestationStatementType, "attObj is not well formed CBOR: %v", err)
  62. }
  63. att := attestationObject{}
  64. if err := cborDecoder.Unmarshal(attObj, &att); err != nil {
  65. return nil, WrapErrorISE(err, "failed unmarshalling CBOR")
  66. }
  67. format := att.Format
  68. if prov == nil {
  69. return nil, NewErrorISE("attestation provisioner is required")
  70. }
  71. result := &DeviceAttestationResult{Format: format}
  72. if !prov.IsAttestationFormatEnabled(ctx, provisioner.ACMEAttestationFormat(format)) {
  73. if format != "apple" && format != "step" && format != "tpm" && format != "android-key" {
  74. return nil, NewDetailedError(ErrorBadAttestationStatementType, "unsupported attestation object format %q", format)
  75. }
  76. return nil, NewError(ErrorBadAttestationStatementType, "attestation format %q is not enabled", format)
  77. }
  78. switch format {
  79. case "android-key":
  80. data, err := doAndroidKeyAttestationFormat(ctx, prov, ch, jwk, &att)
  81. if err != nil {
  82. if acmeError, ok := errors.AsType[*Error](err); ok {
  83. return nil, acmeError
  84. }
  85. return nil, WrapErrorISE(err, "error validating attestation")
  86. }
  87. // Enforce hardware security level (TrustedEnvironment or StrongBox; Software not allowed)
  88. if data.Attestation.AttestationSecurityLevel < 1 {
  89. return nil, NewDetailedError(ErrorBadAttestationStatementType, "insufficient security level: %d", data.Attestation.AttestationSecurityLevel)
  90. }
  91. // Enforce hardware backed device serial
  92. if ch.Value != string(data.Attestation.TeeEnforced.AttestationIdSerial) {
  93. subproblem := NewSubproblemWithIdentifier(
  94. ErrorRejectedIdentifierType,
  95. Identifier{Type: "permanent-identifier", Value: ch.Value},
  96. "challenge identifier %q doesn't match any of the attested hardware identifiers %q",
  97. ch.Value, []string{string(data.Attestation.TeeEnforced.AttestationIdSerial)},
  98. )
  99. return nil, NewDetailedError(ErrorBadAttestationStatementType, "permanent identifier does not match").AddSubproblems(subproblem)
  100. }
  101. // Update attestation key fingerprint to compare against the CSR
  102. result.Fingerprint = data.Fingerprint
  103. result.IdentifierBound = ch.Value != ""
  104. result.ChallengeBound = true
  105. case "apple":
  106. data, err := doAppleAttestationFormat(ctx, prov, ch, &att)
  107. if err != nil {
  108. if acmeError, ok := errors.AsType[*Error](err); ok {
  109. return nil, acmeError
  110. }
  111. return nil, WrapErrorISE(err, "error validating attestation")
  112. }
  113. // Validate nonce with SHA-256 of the token.
  114. if len(data.Nonce) != 0 {
  115. sum := sha256.Sum256([]byte(ch.Token))
  116. if subtle.ConstantTimeCompare(data.Nonce, sum[:]) != 1 {
  117. return nil, NewDetailedError(ErrorBadAttestationStatementType, "challenge token does not match")
  118. }
  119. }
  120. // Validate Apple's ClientIdentifier (Identifier.Value) with device
  121. // identifiers.
  122. //
  123. // Note: We might want to use an external service for this.
  124. if data.UDID != ch.Value && data.SerialNumber != ch.Value {
  125. subproblem := NewSubproblemWithIdentifier(
  126. ErrorRejectedIdentifierType,
  127. Identifier{Type: "permanent-identifier", Value: ch.Value},
  128. "challenge identifier %q doesn't match any of the attested hardware identifiers %q", ch.Value, []string{data.UDID, data.SerialNumber},
  129. )
  130. return nil, NewDetailedError(ErrorBadAttestationStatementType, "permanent identifier does not match").AddSubproblems(subproblem)
  131. }
  132. // Update attestation key fingerprint to compare against the CSR
  133. result.Fingerprint = data.Fingerprint
  134. result.IdentifierBound = ch.Value != ""
  135. result.ChallengeBound = len(data.Nonce) != 0
  136. case "step":
  137. data, err := doStepAttestationFormat(ctx, prov, ch, jwk, &att)
  138. if err != nil {
  139. if acmeError, ok := errors.AsType[*Error](err); ok {
  140. return nil, acmeError
  141. }
  142. return nil, WrapErrorISE(err, "error validating attestation")
  143. }
  144. // Validate the YubiKey serial number from the attestation
  145. // certificate with the challenged Order value.
  146. //
  147. // Note: We might want to use an external service for this.
  148. if data.SerialNumber != ch.Value {
  149. subproblem := NewSubproblemWithIdentifier(
  150. ErrorRejectedIdentifierType,
  151. Identifier{Type: "permanent-identifier", Value: ch.Value},
  152. "challenge identifier %q doesn't match the attested hardware identifier %q", ch.Value, data.SerialNumber,
  153. )
  154. return nil, NewDetailedError(ErrorBadAttestationStatementType, "permanent identifier does not match").AddSubproblems(subproblem)
  155. }
  156. // Update attestation key fingerprint to compare against the CSR
  157. result.Fingerprint = data.Fingerprint
  158. result.IdentifierBound = ch.Value != ""
  159. result.ChallengeBound = true
  160. case "tpm":
  161. data, err := doTPMAttestationFormat(ctx, prov, ch, jwk, &att)
  162. if err != nil {
  163. if acmeError, ok := errors.AsType[*Error](err); ok {
  164. return nil, acmeError
  165. }
  166. return nil, WrapErrorISE(err, "error validating attestation")
  167. }
  168. // TODO(hs): currently this will allow a request for which no PermanentIdentifiers have been
  169. // extracted from the AK certificate. This is currently the case for AK certs from the CLI, as we
  170. // haven't implemented a way for AK certs requested by the CLI to always contain the requested
  171. // PermanentIdentifier. Omitting the check below doesn't allow just any request, as the Order can
  172. // still fail if the challenge value isn't equal to the CSR subject.
  173. if len(data.PermanentIdentifiers) > 0 && !slices.Contains(data.PermanentIdentifiers, ch.Value) { // TODO(hs): add support for HardwareModuleName
  174. subproblem := NewSubproblemWithIdentifier(
  175. ErrorRejectedIdentifierType,
  176. Identifier{Type: "permanent-identifier", Value: ch.Value},
  177. "challenge identifier %q doesn't match any of the attested hardware identifiers %q", ch.Value, data.PermanentIdentifiers,
  178. )
  179. return nil, NewDetailedError(ErrorBadAttestationStatementType, "permanent identifier does not match").AddSubproblems(subproblem)
  180. }
  181. // Update attestation key fingerprint to compare against the CSR
  182. result.Fingerprint = data.Fingerprint
  183. result.IdentifierBound = ch.Value != "" && len(data.PermanentIdentifiers) != 0
  184. result.ChallengeBound = true
  185. default:
  186. return nil, NewDetailedError(ErrorBadAttestationStatementType, "unsupported attestation object format %q", format)
  187. }
  188. return result, nil
  189. }