apple.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Package apple verifies attestation statements of the "apple" format of ACME
  2. // device-attest-01 challenges, made by Apple's Managed Device Attestation.
  3. package apple
  4. import (
  5. "crypto/x509"
  6. "encoding/asn1"
  7. "fmt"
  8. "time"
  9. "go.step.sm/crypto/keyutil"
  10. "go.step.sm/crypto/pemutil"
  11. "github.com/smallstep/certificates/attestation"
  12. )
  13. // Apple Enterprise Attestation Root CA from
  14. // https://www.apple.com/certificateauthority/private/
  15. const appleEnterpriseAttestationRootCA = `-----BEGIN CERTIFICATE-----
  16. MIICJDCCAamgAwIBAgIUQsDCuyxyfFxeq/bxpm8frF15hzcwCgYIKoZIzj0EAwMw
  17. UTEtMCsGA1UEAwwkQXBwbGUgRW50ZXJwcmlzZSBBdHRlc3RhdGlvbiBSb290IENB
  18. MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUzAeFw0yMjAyMTYxOTAx
  19. MjRaFw00NzAyMjAwMDAwMDBaMFExLTArBgNVBAMMJEFwcGxlIEVudGVycHJpc2Ug
  20. QXR0ZXN0YXRpb24gUm9vdCBDQTETMBEGA1UECgwKQXBwbGUgSW5jLjELMAkGA1UE
  21. BhMCVVMwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAT6Jigq+Ps9Q4CoT8t8q+UnOe2p
  22. oT9nRaUfGhBTbgvqSGXPjVkbYlIWYO+1zPk2Sz9hQ5ozzmLrPmTBgEWRcHjA2/y7
  23. 7GEicps9wn2tj+G89l3INNDKETdxSPPIZpPj8VmjQjBAMA8GA1UdEwEB/wQFMAMB
  24. Af8wHQYDVR0OBBYEFPNqTQGd8muBpV5du+UIbVbi+d66MA4GA1UdDwEB/wQEAwIB
  25. BjAKBggqhkjOPQQDAwNpADBmAjEA1xpWmTLSpr1VH4f8Ypk8f3jMUKYz4QPG8mL5
  26. 8m9sX/b2+eXpTv2pH4RZgJjucnbcAjEA4ZSB6S45FlPuS/u4pTnzoz632rA+xW/T
  27. ZwFEh9bhKjJ+5VQ9/Do1os0u3LEkgN/r
  28. -----END CERTIFICATE-----`
  29. var (
  30. oidAppleSerialNumber = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 9, 1}
  31. oidAppleUniqueDeviceIdentifier = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 9, 2}
  32. oidAppleSecureEnclaveProcessorOSVersion = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 10, 2}
  33. oidAppleNonce = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 11, 1}
  34. )
  35. // Data is what a verified "apple" attestation statement attests.
  36. type Data struct {
  37. // Nonce is the freshness nonce the attestation certificate binds, if any.
  38. Nonce []byte
  39. // SerialNumber is the serial number of the device.
  40. SerialNumber string
  41. // UDID is the unique device identifier of the device.
  42. UDID string
  43. // SEPVersion is the version of the operating system of the device's
  44. // Secure Enclave Processor.
  45. SEPVersion string
  46. // Certificate is the attestation certificate, the leaf of the x5c chain,
  47. // which holds the attested key.
  48. Certificate *x509.Certificate
  49. // Fingerprint is the fingerprint of the attested key.
  50. Fingerprint string
  51. }
  52. // Verify verifies the x5c chain of an "apple" attestation statement against
  53. // roots, or against Apple's Enterprise Attestation Root CA if roots is nil,
  54. // and returns what its attestation certificate attests. Errors of the
  55. // statement match attestation.ErrInvalidEvidence.
  56. func Verify(attStmt map[string]any, roots *x509.CertPool) (*Data, error) {
  57. // Use the default attestation root if none is configured.
  58. if roots == nil {
  59. root, err := pemutil.ParseCertificate([]byte(appleEnterpriseAttestationRootCA))
  60. if err != nil {
  61. return nil, fmt.Errorf("error parsing apple enterprise ca: %w", err)
  62. }
  63. roots = x509.NewCertPool()
  64. roots.AddCert(root)
  65. }
  66. x5c, ok := attStmt["x5c"].([]any)
  67. if !ok {
  68. return nil, attestation.Invalidf("x5c not present")
  69. }
  70. if len(x5c) == 0 {
  71. return nil, attestation.Invalidf("x5c is empty")
  72. }
  73. der, ok := x5c[0].([]byte)
  74. if !ok {
  75. return nil, attestation.Invalidf("x5c is malformed")
  76. }
  77. leaf, err := x509.ParseCertificate(der)
  78. if err != nil {
  79. return nil, attestation.Invalidf("x5c is malformed: %w", err)
  80. }
  81. intermediates := x509.NewCertPool()
  82. for _, v := range x5c[1:] {
  83. der, ok = v.([]byte)
  84. if !ok {
  85. return nil, attestation.Invalidf("x5c is malformed")
  86. }
  87. cert, err := x509.ParseCertificate(der)
  88. if err != nil {
  89. return nil, attestation.Invalidf("x5c is malformed: %w", err)
  90. }
  91. intermediates.AddCert(cert)
  92. }
  93. if _, err := leaf.Verify(x509.VerifyOptions{
  94. Intermediates: intermediates,
  95. Roots: roots,
  96. CurrentTime: time.Now().Truncate(time.Second),
  97. KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
  98. }); err != nil {
  99. return nil, attestation.Invalidf("x5c is not valid: %w", err)
  100. }
  101. data := &Data{
  102. Certificate: leaf,
  103. }
  104. if data.Fingerprint, err = keyutil.Fingerprint(leaf.PublicKey); err != nil {
  105. return nil, fmt.Errorf("error calculating key fingerprint: %w", err)
  106. }
  107. for _, ext := range leaf.Extensions {
  108. switch {
  109. case ext.Id.Equal(oidAppleSerialNumber):
  110. data.SerialNumber = string(ext.Value)
  111. case ext.Id.Equal(oidAppleUniqueDeviceIdentifier):
  112. data.UDID = string(ext.Value)
  113. case ext.Id.Equal(oidAppleSecureEnclaveProcessorOSVersion):
  114. data.SEPVersion = string(ext.Value)
  115. case ext.Id.Equal(oidAppleNonce):
  116. data.Nonce = ext.Value
  117. }
  118. }
  119. return data, nil
  120. }