regular_cas_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package sceptest
  2. import (
  3. "context"
  4. "crypto"
  5. "encoding/json"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "path/filepath"
  10. "sync"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "go.step.sm/crypto/keyutil"
  15. "go.step.sm/crypto/minica"
  16. "go.step.sm/crypto/pemutil"
  17. "github.com/smallstep/certificates/authority/config"
  18. "github.com/smallstep/certificates/authority/provisioner"
  19. "github.com/smallstep/certificates/ca"
  20. "github.com/smallstep/certificates/cas/apiv1"
  21. )
  22. func TestFailsIssuingCertificateUsingRegularSCEPWithUpstreamCAS(t *testing.T) {
  23. signer, err := keyutil.GenerateSigner("RSA", "", 2048)
  24. require.NoError(t, err)
  25. dir := t.TempDir()
  26. m, err := minica.New(minica.WithName("Step E2E | SCEP Regular w/ Upstream CAS"), minica.WithGetSignerFunc(func() (crypto.Signer, error) {
  27. return signer, nil
  28. }))
  29. require.NoError(t, err)
  30. rootFilepath := filepath.Join(dir, "root.crt")
  31. _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
  32. require.NoError(t, err)
  33. intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
  34. _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
  35. require.NoError(t, err)
  36. intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
  37. _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
  38. require.NoError(t, err)
  39. // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
  40. // TODO(hs): find/implement a nicer way to expose the CA URL, similar to how e.g. httptest.Server exposes it?
  41. host, port := reservePort(t)
  42. prov := &provisioner.SCEP{
  43. ID: "scep",
  44. Name: "scep",
  45. Type: "SCEP",
  46. ForceCN: false,
  47. ChallengePassword: "",
  48. EncryptionAlgorithmIdentifier: 2,
  49. MinimumPublicKeyLength: 2048,
  50. Claims: &config.GlobalProvisionerClaims,
  51. }
  52. err = prov.Init(provisioner.Config{})
  53. require.NoError(t, err)
  54. apiv1.Register("test-scep-cas", func(_ context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  55. return &testCAS{
  56. ca: m,
  57. }, nil
  58. })
  59. cfg := &config.Config{
  60. Address: net.JoinHostPort(host, port), // reuse the address that was just "reserved"
  61. DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
  62. AuthorityConfig: &config.AuthConfig{
  63. Options: &apiv1.Options{
  64. AuthorityID: "stepca-test-scep",
  65. Type: "test-scep-cas",
  66. CertificateAuthority: "test-cas",
  67. },
  68. AuthorityID: "stepca-test-scep",
  69. DeploymentType: "standalone-test",
  70. Provisioners: provisioner.List{prov},
  71. },
  72. Logger: json.RawMessage(`{"format": "text"}`),
  73. }
  74. c, err := ca.New(cfg)
  75. require.NoError(t, err)
  76. var wg sync.WaitGroup
  77. wg.Add(1)
  78. go func() {
  79. defer wg.Done()
  80. err = c.Run()
  81. require.ErrorIs(t, err, http.ErrServerClosed)
  82. }()
  83. // instantiate a client for the CA running at the random address
  84. caClient := newCAClient(t, fmt.Sprintf("https://localhost:%s", port), rootFilepath)
  85. requireHealthyCA(t, caClient)
  86. // issuance is expected to fail when an upstream CAS is configured, as the current
  87. // CAS interfaces do not support providing a decrypter.
  88. scepClient := createSCEPClient(t, fmt.Sprintf("https://localhost:%s/scep/scep", port), m.Root)
  89. cert, err := scepClient.requestCertificate(t, "test.localhost", []string{"test.localhost"})
  90. assert.Error(t, err)
  91. assert.Nil(t, cert)
  92. // done testing; stop and wait for the server to quit
  93. err = c.Stop()
  94. require.NoError(t, err)
  95. wg.Wait()
  96. }