decrypter_cas_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package sceptest
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "encoding/json"
  8. "encoding/pem"
  9. "fmt"
  10. "math/big"
  11. "net"
  12. "net/http"
  13. "path/filepath"
  14. "sync"
  15. "testing"
  16. "time"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. "go.step.sm/crypto/keyutil"
  20. "go.step.sm/crypto/minica"
  21. "go.step.sm/crypto/pemutil"
  22. "github.com/smallstep/certificates/authority/config"
  23. "github.com/smallstep/certificates/authority/provisioner"
  24. "github.com/smallstep/certificates/ca"
  25. "github.com/smallstep/certificates/cas/apiv1"
  26. )
  27. func TestIssuesCertificateUsingSCEPWithDecrypterAndUpstreamCAS(t *testing.T) {
  28. signer, err := keyutil.GenerateSigner("EC", "P-256", 0)
  29. require.NoError(t, err)
  30. dir := t.TempDir()
  31. m, err := minica.New(minica.WithName("Step E2E | SCEP Decrypter w/ Upstream CAS"), minica.WithGetSignerFunc(func() (crypto.Signer, error) {
  32. return signer, nil
  33. }))
  34. require.NoError(t, err)
  35. rootFilepath := filepath.Join(dir, "root.crt")
  36. _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
  37. require.NoError(t, err)
  38. intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
  39. _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
  40. require.NoError(t, err)
  41. intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
  42. _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
  43. require.NoError(t, err)
  44. decrypterKey, err := keyutil.GenerateKey("RSA", "", 2048)
  45. require.NoError(t, err)
  46. decrypter, ok := decrypterKey.(crypto.Decrypter)
  47. require.True(t, ok)
  48. decrypterCertifiate, err := m.Sign(&x509.Certificate{
  49. Subject: pkix.Name{CommonName: "decrypter"},
  50. PublicKey: decrypter.Public(),
  51. SerialNumber: big.NewInt(1),
  52. NotBefore: time.Now().Add(-1 * time.Hour),
  53. NotAfter: time.Now().Add(1 * time.Hour),
  54. DNSNames: []string{"decrypter"},
  55. })
  56. require.NoError(t, err)
  57. b, err := pemutil.Serialize(decrypterCertifiate)
  58. require.NoError(t, err)
  59. decrypterCertificatePEMBytes := pem.EncodeToMemory(b)
  60. b, err = pemutil.Serialize(decrypter, pemutil.WithPassword([]byte("1234")))
  61. require.NoError(t, err)
  62. decrypterKeyPEMBytes := pem.EncodeToMemory(b)
  63. // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
  64. // TODO(hs): find/implement a nicer way to expose the CA URL, similar to how e.g. httptest.Server exposes it?
  65. host, port := reservePort(t)
  66. prov := &provisioner.SCEP{
  67. ID: "scep",
  68. Name: "scep",
  69. Type: "SCEP",
  70. ForceCN: false,
  71. ChallengePassword: "",
  72. EncryptionAlgorithmIdentifier: 2,
  73. MinimumPublicKeyLength: 2048,
  74. Claims: &config.GlobalProvisionerClaims,
  75. DecrypterCertificate: decrypterCertificatePEMBytes,
  76. DecrypterKeyPEM: decrypterKeyPEMBytes,
  77. DecrypterKeyPassword: "1234",
  78. }
  79. err = prov.Init(provisioner.Config{})
  80. require.NoError(t, err)
  81. apiv1.Register("test-scep-cas", func(_ context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  82. return &testCAS{
  83. ca: m,
  84. }, nil
  85. })
  86. cfg := &config.Config{
  87. Address: net.JoinHostPort(host, port), // reuse the address that was just "reserved"
  88. DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
  89. AuthorityConfig: &config.AuthConfig{
  90. Options: &apiv1.Options{
  91. AuthorityID: "stepca-test-scep",
  92. Type: "test-scep-cas",
  93. CertificateAuthority: "test-cas",
  94. },
  95. AuthorityID: "stepca-test-scep",
  96. DeploymentType: "standalone-test",
  97. Provisioners: provisioner.List{prov},
  98. },
  99. Logger: json.RawMessage(`{"format": "text"}`),
  100. }
  101. c, err := ca.New(cfg)
  102. require.NoError(t, err)
  103. var wg sync.WaitGroup
  104. wg.Add(1)
  105. go func() {
  106. defer wg.Done()
  107. err = c.Run()
  108. require.ErrorIs(t, err, http.ErrServerClosed)
  109. }()
  110. // instantiate a client for the CA running at the random address
  111. caClient := newCAClient(t, fmt.Sprintf("https://localhost:%s", port), rootFilepath)
  112. requireHealthyCA(t, caClient)
  113. scepClient := createSCEPClient(t, fmt.Sprintf("https://localhost:%s/scep/scep", port), m.Root)
  114. cert, err := scepClient.requestCertificate(t, "test.localhost", []string{"test.localhost"})
  115. assert.NoError(t, err)
  116. require.NotNil(t, cert)
  117. assert.Equal(t, "test.localhost", cert.Subject.CommonName)
  118. assert.Equal(t, "Step E2E | SCEP Decrypter w/ Upstream CAS Intermediate CA", cert.Issuer.CommonName)
  119. // done testing; stop and wait for the server to quit
  120. err = c.Stop()
  121. require.NoError(t, err)
  122. wg.Wait()
  123. }