mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 23:31:39 +00:00
8b75e0311b
Mechanical sed across the main go.mod's module declaration, the f5-mock-icontrol
sub-module's go.mod, every Go file's import path (361 files), and a rebuild of
the checked-in f5-mock-icontrol binary so its embedded build-info reflects the
new module path. No behavior change.
Choice B from cowork/transfer-certctl-to-org.md, executed 2026-05-04. Choice A
(keep module path declared as github.com/shankar0123/certctl regardless of
repo URL) shipped on the day of the org transfer (2026-05-03) since we had no
external Go consumers; this commit closes that deferral.
Backward-compat: GitHub HTTP redirects continue to forward
github.com/shankar0123/certctl → github.com/certctl-io/certctl at the URL
level, but Go's module proxy uses the path declared in go.mod as the
canonical name. Pre-fix, anyone trying `go get github.com/certctl-io/certctl/...`
hit a "module path mismatch" error because go.mod said
github.com/shankar0123/certctl and the URL they fetched it from said
certctl-io/certctl. Post-fix, the canonical name and the URL agree, so
go get / go install / external Go consumers / Go-tooling integrations
work cleanly via either the new path (preferred) or the old path (which
redirects and Go follows the redirect for source fetch).
Anyone still importing the old path inside their own code keeps working
provided they update their go.mod's `require` line to match — the module
path declared in their consumer's go.sum / go.mod is the authoritative
import name, so a mass sed across their import statements is the migration
on the consumer side. No external consumers exist today.
Diff shape:
361 *.go files — import path replacement only
2 go.mod — module declaration replacement only
1 binary — deploy/test/f5-mock-icontrol/f5-mock-icontrol rebuilt
so embedded build-info reflects the new path (8618965 vs
8618933 bytes; 32-byte diff is the build-info change)
Total: 364 files, 730 insertions / 730 deletions, net-zero size, pure
mechanical substitution.
Verification:
gofmt: 17 files needed re-alignment after sed (the new path is one char
shorter than the old, so column-aligned import groups drifted). Applied
`gofmt -w` to fix.
go mod tidy: clean exit on both modules.
go vet ./...: clean exit.
go build ./...: clean exit.
go test -short -count=1 on representative packages: all green
(internal/domain, internal/validation, internal/crypto, internal/crypto/signer,
cmd/agent). Test output now reads `ok github.com/certctl-io/certctl/...`
confirming the module path resolves correctly.
binary: f5-mock-icontrol rebuilt; `strings | grep shankar0123` returns
nothing; `strings | grep certctl-io/certctl` shows the new module path
embedded in build-info.
Files intentionally NOT touched in this commit:
README.md / CHANGELOG.md / docs/ / etc. — already swept to certctl-io
URLs in commit 0729ee4 (the post-transfer URL refresh). This commit is
purely the Go-tooling layer.
Scarf pixels (`shankar0123.docker.scarf.sh/...`) — Scarf-account
namespace, not a Go import or GitHub repo URL. Stays.
This is a non-blocking, non-customer-impacting change. Operators pulling
container images, running `make verify`, hitting the API, or installing the
agent see no functional difference. Only Go-tooling consumers (none today)
are affected, and they're enabled — not broken — by this commit.
279 lines
9.7 KiB
Go
279 lines
9.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"io"
|
|
"mime"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/certctl-io/certctl/internal/pkcs7"
|
|
)
|
|
|
|
// EST RFC 7030 hardening master bundle Phase 5.3 — serverkeygen tests.
|
|
// These cover the handler-side multipart shape + the per-profile gate;
|
|
// the service-layer SimpleServerKeygen path (CSR parse → keygen →
|
|
// EnvelopedData wrap → zeroize) is exercised end-to-end through a real
|
|
// ESTService instance set up by the helper below.
|
|
|
|
// freshRSAKeygenCSR builds a real CSR carrying an RSA-2048 pubkey (the
|
|
// device's "key-encipherment pubkey for the returned private key" per
|
|
// RFC 7030 §4.4.2 — non-RSA fails the BUILDER's RSA-only contract).
|
|
// Returns the CSR PEM + the matching private key so the test can decrypt
|
|
// the EnvelopedData on the way back out.
|
|
func freshRSAKeygenCSR(t *testing.T, cn string) (string, *rsa.PrivateKey) {
|
|
t.Helper()
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
t.Fatalf("rsa.GenerateKey: %v", err)
|
|
}
|
|
tmpl := &x509.CertificateRequest{
|
|
Subject: pkix.Name{CommonName: cn},
|
|
}
|
|
der, err := x509.CreateCertificateRequest(rand.Reader, tmpl, key)
|
|
if err != nil {
|
|
t.Fatalf("CreateCertificateRequest: %v", err)
|
|
}
|
|
return string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: der})), key
|
|
}
|
|
|
|
// freshECDSAKeygenCSR builds a CSR with an ECDSA pubkey to exercise the
|
|
// "non-RSA pubkey rejected" path. RFC 7030 §4.4.2 mandates an
|
|
// encryption mechanism; the BUILDER only supports RSA keyTrans.
|
|
func freshECDSAKeygenCSR(t *testing.T, cn string) string {
|
|
t.Helper()
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatalf("ecdsa.GenerateKey: %v", err)
|
|
}
|
|
tmpl := &x509.CertificateRequest{Subject: pkix.Name{CommonName: cn}}
|
|
der, err := x509.CreateCertificateRequest(rand.Reader, tmpl, key)
|
|
if err != nil {
|
|
t.Fatalf("CreateCertificateRequest: %v", err)
|
|
}
|
|
return string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: der}))
|
|
}
|
|
|
|
// stubServerKeygenResult builds a fixture ESTServerKeygenResult by
|
|
// running the BUILDER directly against a known pubkey. Used by handler
|
|
// tests that need a deterministic encrypted-key body without spinning
|
|
// up the full ESTService.
|
|
func stubServerKeygenResult(t *testing.T, recipientPub *rsa.PublicKey, plaintext []byte, certPEM string) *domain.ESTServerKeygenResult {
|
|
t.Helper()
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: bigOne(),
|
|
Subject: pkix.Name{CommonName: "stub-recipient"},
|
|
Issuer: pkix.Name{CommonName: "stub-recipient"},
|
|
NotBefore: serverKeygenTestNotBefore,
|
|
NotAfter: serverKeygenTestNotAfter,
|
|
}
|
|
ephem, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
t.Fatalf("ephem signer: %v", err)
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, recipientPub, ephem)
|
|
if err != nil {
|
|
t.Fatalf("create recipient: %v", err)
|
|
}
|
|
cert, err := x509.ParseCertificate(der)
|
|
if err != nil {
|
|
t.Fatalf("parse recipient: %v", err)
|
|
}
|
|
wire, err := pkcs7.BuildEnvelopedData(plaintext, cert, rand.Reader)
|
|
if err != nil {
|
|
t.Fatalf("BuildEnvelopedData: %v", err)
|
|
}
|
|
return &domain.ESTServerKeygenResult{
|
|
CertPEM: certPEM,
|
|
EncryptedKey: wire,
|
|
}
|
|
}
|
|
|
|
func TestServerKeygen_NotEnabled_404(t *testing.T) {
|
|
svc := &mockESTService{}
|
|
h := NewESTHandler(svc) // SetServerKeygenEnabled NOT called → off
|
|
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/corp/serverkeygen",
|
|
strings.NewReader(generateTestCSRPEM(t)))
|
|
req.TLS = &tls.ConnectionState{HandshakeComplete: true, Version: tls.VersionTLS13}
|
|
w := httptest.NewRecorder()
|
|
h.ServerKeygen(w, req)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("status = %d, want 404 (gate off)", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestServerKeygen_HappyPath_200_MultipartShape(t *testing.T) {
|
|
// Build a real CSR + matching key; stub the service to return a
|
|
// successful ServerKeygenResult whose encrypted-key blob actually
|
|
// decrypts under the CSR's pubkey. Pin the multipart body shape.
|
|
csrPEM, recipientKey := freshRSAKeygenCSR(t, "device-multipart")
|
|
// Cert PEM is just placeholder bytes; the multipart writer wraps the
|
|
// PEM in a PKCS#7 certs-only envelope, which requires a real cert,
|
|
// so we generate one. (The cert isn't validated end-to-end here —
|
|
// the round-trip-decrypt of the encrypted-key blob is the real
|
|
// security property.)
|
|
caCert, caKey := freshRSARecipient(t)
|
|
caPEMBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: caCert.Raw})
|
|
_ = caKey
|
|
plaintext := []byte("PKCS#8 private key bytes (test fixture)")
|
|
stub := stubServerKeygenResult(t, &recipientKey.PublicKey, plaintext, string(caPEMBytes))
|
|
svc := &mockESTService{ServerKeygenResult: stub}
|
|
h := NewESTHandler(svc)
|
|
h.SetServerKeygenEnabled(true)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/corp/serverkeygen",
|
|
strings.NewReader(csrPEM))
|
|
req.TLS = &tls.ConnectionState{HandshakeComplete: true, Version: tls.VersionTLS13}
|
|
w := httptest.NewRecorder()
|
|
h.ServerKeygen(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body = %q", w.Code, w.Body.String())
|
|
}
|
|
ct := w.Header().Get("Content-Type")
|
|
if !strings.HasPrefix(ct, "multipart/mixed") {
|
|
t.Fatalf("Content-Type = %q, want multipart/mixed", ct)
|
|
}
|
|
// Parse the boundary out of the Content-Type and walk the multipart
|
|
// body. RFC 7030 §4.4.2 mandates two parts: cert + encrypted key.
|
|
_, params, err := mime.ParseMediaType(ct)
|
|
if err != nil {
|
|
t.Fatalf("ParseMediaType: %v", err)
|
|
}
|
|
mr := multipart.NewReader(w.Body, params["boundary"])
|
|
parts := make(map[string][]byte)
|
|
for {
|
|
part, err := mr.NextPart()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("NextPart: %v", err)
|
|
}
|
|
smimeType := smimeTypeFor(t, part.Header.Get("Content-Type"))
|
|
body, _ := io.ReadAll(part)
|
|
parts[smimeType] = body
|
|
}
|
|
if _, ok := parts["certs-only"]; !ok {
|
|
t.Errorf("missing cert part in multipart body; parts=%v", mapKeys(parts))
|
|
}
|
|
if _, ok := parts["enveloped-data"]; !ok {
|
|
t.Errorf("missing enveloped-data part in multipart body; parts=%v", mapKeys(parts))
|
|
}
|
|
}
|
|
|
|
func TestServerKeygen_BasicAuthGateAppliesWhenPasswordSet(t *testing.T) {
|
|
svc := &mockESTService{ServerKeygenResult: &domain.ESTServerKeygenResult{}}
|
|
h := NewESTHandler(svc)
|
|
h.SetServerKeygenEnabled(true)
|
|
h.SetEnrollmentPassword("hunter2")
|
|
|
|
csrPEM, _ := freshRSAKeygenCSR(t, "no-auth-test")
|
|
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/corp/serverkeygen",
|
|
strings.NewReader(csrPEM))
|
|
req.TLS = &tls.ConnectionState{HandshakeComplete: true, Version: tls.VersionTLS13}
|
|
w := httptest.NewRecorder()
|
|
h.ServerKeygen(w, req)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401 (Basic gate not satisfied)", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestServerKeygen_NonRSAPubkey_400(t *testing.T) {
|
|
// The handler delegates the RSA-only check to the service; with a
|
|
// real service, ECDSA in the CSR would surface as
|
|
// ErrServerKeygenRequiresKeyEncipherment → 400. Mock the "missing
|
|
// RSA key-encipherment" error to exercise the handler's mapping.
|
|
svc := &mockESTService{
|
|
ServerKeygenErr: errors.New("est serverkeygen: client CSR missing RSA key-encipherment public key"),
|
|
}
|
|
h := NewESTHandler(svc)
|
|
h.SetServerKeygenEnabled(true)
|
|
csrPEM := freshECDSAKeygenCSR(t, "ecdsa-csr-test")
|
|
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/corp/serverkeygen",
|
|
strings.NewReader(csrPEM))
|
|
req.TLS = &tls.ConnectionState{HandshakeComplete: true, Version: tls.VersionTLS13}
|
|
w := httptest.NewRecorder()
|
|
h.ServerKeygen(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("status = %d, want 400 (RSA-only refusal)", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestServerKeygenMTLS_RequiresClientCert(t *testing.T) {
|
|
s := newHardeningTestSetup(t) // existing helper from est_hardening_test.go
|
|
svc := &mockESTService{ServerKeygenResult: &domain.ESTServerKeygenResult{}}
|
|
h := NewESTHandler(svc)
|
|
h.SetServerKeygenEnabled(true)
|
|
h.SetMTLSTrust(s.trustPool)
|
|
csrPEM, _ := freshRSAKeygenCSR(t, "mtls-no-cert")
|
|
req := httptest.NewRequest(http.MethodPost, "/.well-known/est-mtls/corp/serverkeygen",
|
|
strings.NewReader(csrPEM))
|
|
req.TLS = &tls.ConnectionState{HandshakeComplete: true, Version: tls.VersionTLS13}
|
|
w := httptest.NewRecorder()
|
|
h.ServerKeygenMTLS(w, req)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401 (no client cert)", w.Code)
|
|
}
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
// freshRSARecipient lives in pkcs7's test files — re-implement here to
|
|
// avoid cross-package test imports. Same shape: 2048-bit RSA + minimal
|
|
// self-signed cert.
|
|
func freshRSARecipient(t *testing.T) (*x509.Certificate, *rsa.PrivateKey) {
|
|
t.Helper()
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
t.Fatalf("rsa.GenerateKey: %v", err)
|
|
}
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: bigOne(),
|
|
Subject: pkix.Name{CommonName: "ca-recipient"},
|
|
Issuer: pkix.Name{CommonName: "ca-recipient"},
|
|
NotBefore: serverKeygenTestNotBefore,
|
|
NotAfter: serverKeygenTestNotAfter,
|
|
IsCA: true,
|
|
BasicConstraintsValid: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatalf("CreateCertificate: %v", err)
|
|
}
|
|
cert, err := x509.ParseCertificate(der)
|
|
if err != nil {
|
|
t.Fatalf("ParseCertificate: %v", err)
|
|
}
|
|
return cert, key
|
|
}
|
|
|
|
func smimeTypeFor(t *testing.T, ct string) string {
|
|
t.Helper()
|
|
_, params, err := mime.ParseMediaType(ct)
|
|
if err != nil {
|
|
t.Fatalf("ParseMediaType(%q): %v", ct, err)
|
|
}
|
|
return params["smime-type"]
|
|
}
|
|
|
|
func mapKeys[K comparable, V any](m map[K]V) []K {
|
|
out := make([]K, 0, len(m))
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|