apple_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package apple
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "crypto/x509"
  7. "crypto/x509/pkix"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.step.sm/crypto/keyutil"
  12. "go.step.sm/crypto/minica"
  13. "github.com/smallstep/certificates/attestation"
  14. )
  15. func TestVerify(t *testing.T) {
  16. ca, err := minica.New()
  17. require.NoError(t, err)
  18. roots := x509.NewCertPool()
  19. roots.AddCert(ca.Root)
  20. signer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  21. require.NoError(t, err)
  22. leaf, err := ca.Sign(&x509.Certificate{
  23. Subject: pkix.Name{CommonName: "attestation cert"},
  24. PublicKey: signer.Public(),
  25. ExtraExtensions: []pkix.Extension{
  26. {Id: oidAppleSerialNumber, Value: []byte("serial-number")},
  27. {Id: oidAppleUniqueDeviceIdentifier, Value: []byte("udid")},
  28. {Id: oidAppleSecureEnclaveProcessorOSVersion, Value: []byte("16.0")},
  29. {Id: oidAppleNonce, Value: []byte("nonce")},
  30. },
  31. })
  32. require.NoError(t, err)
  33. fingerprint, err := keyutil.Fingerprint(signer.Public())
  34. require.NoError(t, err)
  35. tests := []struct {
  36. name string
  37. attStmt map[string]any
  38. roots *x509.CertPool
  39. want *Data
  40. wantErr bool
  41. }{
  42. {"ok", map[string]any{
  43. "x5c": []any{leaf.Raw, ca.Intermediate.Raw},
  44. }, roots, &Data{
  45. Nonce: []byte("nonce"),
  46. SerialNumber: "serial-number",
  47. UDID: "udid",
  48. SEPVersion: "16.0",
  49. Certificate: leaf,
  50. Fingerprint: fingerprint,
  51. }, false},
  52. {"fail apple issuer", map[string]any{
  53. "x5c": []any{leaf.Raw, ca.Intermediate.Raw},
  54. }, nil, nil, true},
  55. {"fail missing x5c", map[string]any{
  56. "foo": "bar",
  57. }, roots, nil, true},
  58. {"fail empty issuer", map[string]any{
  59. "x5c": []any{},
  60. }, roots, nil, true},
  61. {"fail leaf type", map[string]any{
  62. "x5c": []any{"leaf", ca.Intermediate.Raw},
  63. }, roots, nil, true},
  64. {"fail leaf parse", map[string]any{
  65. "x5c": []any{leaf.Raw[:100], ca.Intermediate.Raw},
  66. }, roots, nil, true},
  67. {"fail intermediate type", map[string]any{
  68. "x5c": []any{leaf.Raw, "intermediate"},
  69. }, roots, nil, true},
  70. {"fail intermediate parse", map[string]any{
  71. "x5c": []any{leaf.Raw, ca.Intermediate.Raw[:100]},
  72. }, roots, nil, true},
  73. {"fail verify", map[string]any{
  74. "x5c": []any{leaf.Raw},
  75. }, roots, nil, true},
  76. }
  77. for _, tt := range tests {
  78. t.Run(tt.name, func(t *testing.T) {
  79. got, err := Verify(tt.attStmt, tt.roots)
  80. if tt.wantErr {
  81. assert.ErrorIs(t, err, attestation.ErrInvalidEvidence)
  82. assert.Nil(t, got)
  83. return
  84. }
  85. require.NoError(t, err)
  86. assert.Equal(t, tt.want, got)
  87. })
  88. }
  89. }