|
|
@@ -2,6 +2,7 @@ package sceptest
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "crypto"
|
|
|
"crypto/rand"
|
|
|
"crypto/rsa"
|
|
|
"crypto/sha1"
|
|
|
@@ -10,6 +11,7 @@ import (
|
|
|
"crypto/x509/pkix"
|
|
|
"encoding/asn1"
|
|
|
"encoding/base64"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
@@ -17,6 +19,7 @@ import (
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
+ "path/filepath"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
@@ -25,14 +28,19 @@ import (
|
|
|
|
|
|
"github.com/smallstep/pkcs7"
|
|
|
"github.com/smallstep/scep"
|
|
|
+ "go.step.sm/crypto/keyutil"
|
|
|
"go.step.sm/crypto/minica"
|
|
|
+ "go.step.sm/crypto/pemutil"
|
|
|
"go.step.sm/crypto/x509util"
|
|
|
|
|
|
+ "github.com/smallstep/certificates/authority/config"
|
|
|
+ "github.com/smallstep/certificates/authority/provisioner"
|
|
|
"github.com/smallstep/certificates/ca"
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
|
|
)
|
|
|
|
|
|
func newCAClient(t *testing.T, caURL, rootFilepath string) *ca.Client {
|
|
|
+ t.Helper()
|
|
|
caClient, err := ca.NewClient(
|
|
|
caURL,
|
|
|
ca.WithRootFile(rootFilepath),
|
|
|
@@ -72,6 +80,86 @@ func reservePort(t *testing.T) (host, port string) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+type testCA struct {
|
|
|
+ ca *ca.CA
|
|
|
+ caURL string
|
|
|
+ rootFilepath string
|
|
|
+ root *x509.Certificate
|
|
|
+}
|
|
|
+
|
|
|
+func (t *testCA) run() error {
|
|
|
+ return t.ca.Run()
|
|
|
+}
|
|
|
+
|
|
|
+func (t *testCA) stop() error {
|
|
|
+ return t.ca.Stop()
|
|
|
+}
|
|
|
+
|
|
|
+func newTestCA(t *testing.T, name string) *testCA {
|
|
|
+ t.Helper()
|
|
|
+
|
|
|
+ signer, err := keyutil.GenerateSigner("RSA", "", 2048)
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ dir := t.TempDir()
|
|
|
+ m, err := minica.New(minica.WithName(name), minica.WithGetSignerFunc(func() (crypto.Signer, error) {
|
|
|
+ return signer, nil
|
|
|
+ }))
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ rootFilepath := filepath.Join(dir, "root.crt")
|
|
|
+ _, err = pemutil.Serialize(m.Root, pemutil.WithFilename(rootFilepath))
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ intermediateCertFilepath := filepath.Join(dir, "intermediate.crt")
|
|
|
+ _, err = pemutil.Serialize(m.Intermediate, pemutil.WithFilename(intermediateCertFilepath))
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ intermediateKeyFilepath := filepath.Join(dir, "intermediate.key")
|
|
|
+ _, err = pemutil.Serialize(m.Signer, pemutil.WithFilename(intermediateKeyFilepath))
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ // get a random address to listen on and connect to; currently no nicer way to get one before starting the server
|
|
|
+ host, port := reservePort(t)
|
|
|
+
|
|
|
+ prov := &provisioner.SCEP{
|
|
|
+ ID: "scep",
|
|
|
+ Name: "scep",
|
|
|
+ Type: "SCEP",
|
|
|
+ ForceCN: false,
|
|
|
+ ChallengePassword: "",
|
|
|
+ EncryptionAlgorithmIdentifier: 2,
|
|
|
+ MinimumPublicKeyLength: 2048,
|
|
|
+ Claims: &config.GlobalProvisionerClaims,
|
|
|
+ }
|
|
|
+
|
|
|
+ err = prov.Init(provisioner.Config{})
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ cfg := &config.Config{
|
|
|
+ Root: []string{rootFilepath},
|
|
|
+ IntermediateCert: intermediateCertFilepath,
|
|
|
+ IntermediateKey: intermediateKeyFilepath,
|
|
|
+ Address: net.JoinHostPort(host, port), // reuse the address that was just "reserved"
|
|
|
+ DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
|
|
|
+ AuthorityConfig: &config.AuthConfig{
|
|
|
+ AuthorityID: "stepca-test-scep",
|
|
|
+ DeploymentType: "standalone-test",
|
|
|
+ Provisioners: provisioner.List{prov},
|
|
|
+ },
|
|
|
+ Logger: json.RawMessage(`{"format": "text"}`),
|
|
|
+ }
|
|
|
+ c, err := ca.New(cfg)
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ return &testCA{
|
|
|
+ ca: c,
|
|
|
+ caURL: fmt.Sprintf("https://localhost:%s", port),
|
|
|
+ rootFilepath: rootFilepath,
|
|
|
+ root: m.Root,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
type client struct {
|
|
|
caURL string
|
|
|
caCert *x509.Certificate
|
|
|
@@ -90,7 +178,7 @@ func createSCEPClient(t *testing.T, caURL string, root *x509.Certificate) *clien
|
|
|
Transport: transport,
|
|
|
}
|
|
|
return &client{
|
|
|
- caURL: caURL,
|
|
|
+ caURL: fmt.Sprintf("%s/scep/scep", caURL),
|
|
|
httpClient: httpClient,
|
|
|
}
|
|
|
}
|
|
|
@@ -301,7 +389,7 @@ func (c *client) requestCertificateEmulatingWindowsClient(t *testing.T, commonNa
|
|
|
h := sha1.Sum(publicKeyBytes)
|
|
|
subjectKeyId := h[:]
|
|
|
|
|
|
- // add subject key ID extension
|
|
|
+ // create subject key ID extension
|
|
|
value, err = asn1.Marshal(subjectKeyId)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("failed marshaling subject key ID: %w", err)
|