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