androidkey_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package androidkey
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/ecdsa"
  6. "crypto/elliptic"
  7. "crypto/rand"
  8. "crypto/sha256"
  9. "crypto/x509"
  10. "crypto/x509/pkix"
  11. "errors"
  12. "testing"
  13. keyattestation "github.com/smallstep/android-attestation"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. "go.step.sm/crypto/keyutil"
  17. "go.step.sm/crypto/minica"
  18. "github.com/smallstep/certificates/attestation"
  19. )
  20. // keyAuth is the key authorization the attestation statements sign.
  21. var keyAuth = []byte("token.thumbprint")
  22. func notRevoked(context.Context, *x509.Certificate) (bool, error) {
  23. return false, nil
  24. }
  25. func TestVerify(t *testing.T) {
  26. ca, err := minica.New()
  27. require.NoError(t, err)
  28. roots := x509.NewCertPool()
  29. roots.AddCert(ca.Root)
  30. signer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  31. require.NoError(t, err)
  32. fingerprint, err := keyutil.Fingerprint(signer.Public())
  33. require.NoError(t, err)
  34. keyDescription, err := keyattestation.CreateKeyDescription(&keyattestation.KeyDescription{
  35. AttestationVersion: 300,
  36. AttestationSecurityLevel: 1,
  37. AttestationChallenge: keyAuth,
  38. TeeEnforced: keyattestation.AuthorizationList{
  39. AttestationIdSerial: []byte("serial-number"),
  40. },
  41. })
  42. require.NoError(t, err)
  43. makeLeaf := func(exts ...pkix.Extension) *x509.Certificate {
  44. leaf, err := ca.Sign(&x509.Certificate{
  45. Subject: pkix.Name{CommonName: "attestation cert"},
  46. PublicKey: signer.Public(),
  47. ExtraExtensions: exts,
  48. })
  49. require.NoError(t, err)
  50. return leaf
  51. }
  52. leaf := makeLeaf(pkix.Extension{Id: oidAndroidAttestation, Value: keyDescription})
  53. leafWithoutExtension := makeLeaf()
  54. leafWithBadExtension := makeLeaf(pkix.Extension{Id: oidAndroidAttestation, Value: []byte("bad")})
  55. keyAuthSum := sha256.Sum256(keyAuth)
  56. sig, err := signer.Sign(rand.Reader, keyAuthSum[:], crypto.SHA256)
  57. require.NoError(t, err)
  58. otherSigner, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  59. require.NoError(t, err)
  60. otherSig, err := otherSigner.Sign(rand.Reader, keyAuthSum[:], crypto.SHA256)
  61. require.NoError(t, err)
  62. otherKeyAuth := []byte("other.thumbprint")
  63. otherKeyAuthSum := sha256.Sum256(otherKeyAuth)
  64. otherKeyAuthSig, err := signer.Sign(rand.Reader, otherKeyAuthSum[:], crypto.SHA256)
  65. require.NoError(t, err)
  66. x5c := []any{leaf.Raw, ca.Intermediate.Raw, ca.Root.Raw}
  67. errCRL := errors.New("crl unavailable")
  68. t.Run("ok", func(t *testing.T) {
  69. got, err := Verify(context.Background(), map[string]any{"x5c": x5c, "sig": sig}, roots, keyAuth, notRevoked)
  70. require.NoError(t, err)
  71. assert.Equal(t, leaf, got.Certificate)
  72. assert.Equal(t, fingerprint, got.Fingerprint)
  73. if assert.NotNil(t, got.KeyDescription) {
  74. assert.Equal(t, keyAuth, got.KeyDescription.AttestationChallenge)
  75. assert.Equal(t, keyattestation.SecurityLevel(1), got.KeyDescription.AttestationSecurityLevel)
  76. assert.Equal(t, []byte("serial-number"), got.KeyDescription.TeeEnforced.AttestationIdSerial)
  77. }
  78. })
  79. revokedRoot := func(_ context.Context, cert *x509.Certificate) (bool, error) {
  80. return cert.SerialNumber.Cmp(ca.Root.SerialNumber) == 0, nil
  81. }
  82. failingCRL := func(context.Context, *x509.Certificate) (bool, error) {
  83. return true, errCRL
  84. }
  85. type args struct {
  86. attStmt map[string]any
  87. roots *x509.CertPool
  88. keyAuth []byte
  89. isRevoked RevocationChecker
  90. }
  91. tests := []struct {
  92. name string
  93. args args
  94. invalid bool
  95. wantErr error
  96. msg string
  97. }{
  98. {"fail no revocation checker", args{map[string]any{"x5c": x5c, "sig": sig}, roots, keyAuth, nil},
  99. false, nil, "requires a revocation checker"},
  100. {"fail google roots", args{map[string]any{"x5c": x5c, "sig": sig}, nil, keyAuth, notRevoked},
  101. true, nil, "root certificate in chain is not trusted"},
  102. {"fail x5c not present", args{map[string]any{"sig": sig}, roots, keyAuth, notRevoked},
  103. true, nil, "x5c not present"},
  104. {"fail x5c empty", args{map[string]any{"x5c": []any{}, "sig": sig}, roots, keyAuth, notRevoked},
  105. true, ErrEmptyX5C, "x5c is empty"},
  106. {"fail x5c element type", args{map[string]any{"x5c": []any{"leaf", ca.Intermediate.Raw, ca.Root.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  107. true, nil, "x5c element is malformed"},
  108. {"fail x5c element parse", args{map[string]any{"x5c": []any{leaf.Raw[:100], ca.Intermediate.Raw, ca.Root.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  109. true, nil, "failed parsing certificate at index 0"},
  110. {"fail revoked", args{map[string]any{"x5c": x5c, "sig": sig}, roots, keyAuth, revokedRoot},
  111. true, nil, "x5c element contains a revoked certificate"},
  112. {"fail revocation check", args{map[string]any{"x5c": x5c, "sig": sig}, roots, keyAuth, failingCRL},
  113. false, errCRL, "failed checking certificate revocation status"},
  114. {"fail missing root", args{map[string]any{"x5c": []any{leaf.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  115. true, nil, "missing root certificate in x5c chain"},
  116. {"fail chain verification", args{map[string]any{"x5c": []any{leaf.Raw, ca.Root.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  117. true, nil, "x5c chain verification failed"},
  118. {"fail sig not present", args{map[string]any{"x5c": x5c}, roots, keyAuth, notRevoked},
  119. true, nil, "sig not present"},
  120. {"fail sig verify", args{map[string]any{"x5c": x5c, "sig": otherSig}, roots, keyAuth, notRevoked},
  121. true, nil, "failed validating signature"},
  122. {"fail no attestation certificate", args{map[string]any{"x5c": []any{leafWithoutExtension.Raw, ca.Intermediate.Raw, ca.Root.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  123. true, nil, "no attestation certificate with OID 1.3.6.1.4.1.11129.2.1.17 found"},
  124. {"fail key description", args{map[string]any{"x5c": []any{leafWithBadExtension.Raw, ca.Intermediate.Raw, ca.Root.Raw}, "sig": sig}, roots, keyAuth, notRevoked},
  125. true, ErrKeyDescription, "error parsing attestation"},
  126. {"fail challenge mismatch", args{map[string]any{"x5c": x5c, "sig": otherKeyAuthSig}, roots, otherKeyAuth, notRevoked},
  127. true, nil, `challenge mismatch; expected "other.thumbprint", got "token.thumbprint"`},
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.name, func(t *testing.T) {
  131. got, err := Verify(context.Background(), tt.args.attStmt, tt.args.roots, tt.args.keyAuth, tt.args.isRevoked)
  132. require.Error(t, err)
  133. assert.ErrorContains(t, err, tt.msg)
  134. assert.Equal(t, tt.invalid, errors.Is(err, attestation.ErrInvalidEvidence), "errors.Is(%v, attestation.ErrInvalidEvidence)", err)
  135. if tt.wantErr != nil {
  136. assert.ErrorIs(t, err, tt.wantErr)
  137. }
  138. assert.Nil(t, got)
  139. })
  140. }
  141. }