Files
certctl/internal/pkcs7/signedinfo_test.go
T
shankar0123 8b75e0311b chore: rename Go module path to github.com/certctl-io/certctl
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.
2026-05-04 00:30:29 +00:00

361 lines
13 KiB
Go

package pkcs7
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"math/big"
"testing"
"time"
"github.com/certctl-io/certctl/internal/domain"
)
// SCEP RFC 8894 Phase 2.2: round-trip tests for ParseSignedData +
// SignerInfo.VerifySignature + auth-attr extractors.
//
// Each test materialises a real signing cert + signs auth-attrs over a
// known content, then re-parses and verifies. Catches drift between the
// signing-side encoding and the verification-side re-serialisation
// (RFC 5652 §5.4 SET OF Attribute quirk).
func TestSignerInfo_RoundTrip_RSAWithSHA256(t *testing.T) {
signer, signerCert := genTestRSASigner(t)
signedData := buildTestSignedData(t, signer, signerCert,
domain.SCEPMessageTypePKCSReq, "txn-12345", []byte("0123456789abcdef"),
[]byte("encapsulated content (typically EnvelopedData bytes)"))
parsed, err := ParseSignedData(signedData)
if err != nil {
t.Fatalf("ParseSignedData: %v", err)
}
if len(parsed.SignerInfos) != 1 {
t.Fatalf("len(SignerInfos) = %d, want 1", len(parsed.SignerInfos))
}
si := parsed.SignerInfos[0]
if err := si.VerifySignature(); err != nil {
t.Fatalf("VerifySignature: %v", err)
}
// Auth-attr extractors.
mt, err := si.GetMessageType()
if err != nil {
t.Fatalf("GetMessageType: %v", err)
}
if mt != domain.SCEPMessageTypePKCSReq {
t.Errorf("GetMessageType = %d, want %d", mt, domain.SCEPMessageTypePKCSReq)
}
tid, err := si.GetTransactionID()
if err != nil {
t.Fatalf("GetTransactionID: %v", err)
}
if tid != "txn-12345" {
t.Errorf("GetTransactionID = %q, want %q", tid, "txn-12345")
}
nonce, err := si.GetSenderNonce()
if err != nil {
t.Fatalf("GetSenderNonce: %v", err)
}
if string(nonce) != "0123456789abcdef" {
t.Errorf("GetSenderNonce = %q, want %q", nonce, "0123456789abcdef")
}
}
func TestSignerInfo_RoundTrip_ECDSAWithSHA256(t *testing.T) {
signer, signerCert := genTestECDSASigner(t)
signedData := buildTestSignedData(t, signer, signerCert,
domain.SCEPMessageTypeRenewalReq, "txn-ec-1", []byte("nonce-ec-aaaa-bbbb"),
[]byte("encap content"))
parsed, err := ParseSignedData(signedData)
if err != nil {
t.Fatalf("ParseSignedData: %v", err)
}
si := parsed.SignerInfos[0]
if err := si.VerifySignature(); err != nil {
t.Fatalf("VerifySignature (ECDSA): %v", err)
}
mt, err := si.GetMessageType()
if err != nil {
t.Fatalf("GetMessageType: %v", err)
}
if mt != domain.SCEPMessageTypeRenewalReq {
t.Errorf("GetMessageType = %d, want RenewalReq (17)", mt)
}
}
func TestSignerInfo_VerifySignature_TamperedAttrs_Refuses(t *testing.T) {
signer, signerCert := genTestRSASigner(t)
signedData := buildTestSignedData(t, signer, signerCert,
domain.SCEPMessageTypePKCSReq, "txn-tamper", []byte("nonce-aaaa-bbbb"),
[]byte("content"))
parsed, err := ParseSignedData(signedData)
if err != nil {
t.Fatalf("ParseSignedData: %v", err)
}
si := parsed.SignerInfos[0]
// Tamper with rawSignedAttrs by flipping the last byte. Re-verification
// must reject — proves the signature is bound to the auth-attr bytes.
si.rawSignedAttrs[len(si.rawSignedAttrs)-1] ^= 0x01
if err := si.VerifySignature(); !errors.Is(err, ErrSignerInfoVerify) {
t.Errorf("VerifySignature(tampered attrs) = %v, want ErrSignerInfoVerify", err)
}
}
func TestParseSignedData_Empty_Refuses(t *testing.T) {
if _, err := ParseSignedData(nil); err == nil {
t.Error("ParseSignedData(nil) = nil, want error")
}
if _, err := ParseSignedData([]byte{}); err == nil {
t.Error("ParseSignedData(empty) = nil, want error")
}
}
func TestParseSignedData_Garbage_Refuses(t *testing.T) {
garbage := []byte{0x30, 0x82, 0x05, 0x01, 0x02, 0x03}
if _, err := ParseSignedData(garbage); err == nil {
t.Error("ParseSignedData(garbage) = nil, want error")
}
}
// --- helpers -------------------------------------------------------------
type testSigner interface {
Sign(data []byte) ([]byte, error)
DigestOID() asn1.ObjectIdentifier
SignatureOID() asn1.ObjectIdentifier
}
type rsaTestSigner struct{ k *rsa.PrivateKey }
func (s *rsaTestSigner) Sign(data []byte) ([]byte, error) {
h := sha256.Sum256(data)
return rsa.SignPKCS1v15(rand.Reader, s.k, 0+5, h[:]) // 5 == crypto.SHA256 in crypto.Hash enum
}
func (s *rsaTestSigner) DigestOID() asn1.ObjectIdentifier { return OIDSHA256 }
func (s *rsaTestSigner) SignatureOID() asn1.ObjectIdentifier { return OIDRSAWithSHA256 }
type ecdsaTestSigner struct{ k *ecdsa.PrivateKey }
func (s *ecdsaTestSigner) Sign(data []byte) ([]byte, error) {
h := sha256.Sum256(data)
return ecdsa.SignASN1(rand.Reader, s.k, h[:])
}
func (s *ecdsaTestSigner) DigestOID() asn1.ObjectIdentifier { return OIDSHA256 }
func (s *ecdsaTestSigner) SignatureOID() asn1.ObjectIdentifier { return OIDECDSAWithSHA256 }
func genTestRSASigner(t *testing.T) (testSigner, *x509.Certificate) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("rsa.GenerateKey: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano() ^ 0xDEAD),
Subject: pkix.Name{CommonName: "device-rsa"},
Issuer: pkix.Name{CommonName: "device-rsa"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
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 &rsaTestSigner{k: key}, cert
}
func genTestECDSASigner(t *testing.T) (testSigner, *x509.Certificate) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("ecdsa.GenerateKey: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano() ^ 0xBEEF),
Subject: pkix.Name{CommonName: "device-ec"},
Issuer: pkix.Name{CommonName: "device-ec"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
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 &ecdsaTestSigner{k: key}, cert
}
// buildTestSignedData hand-constructs a CMS SignedData with one SignerInfo
// carrying SCEP authenticated attributes (messageType, transactionID,
// senderNonce, plus the standard CMS contentType + messageDigest).
//
// The signing pipeline mirrors what micromdm/scep + the ChromeOS SCEP
// client emit: the device hashes the encap content into messageDigest,
// the auth-attrs are SET-OF re-serialised, hashed, and signed.
//
// Implementation note: built directly with ASN1Wrap helpers rather than
// relying on asn1.Marshal of structs containing asn1.RawValue fields —
// asn1.Marshal of nested RawValues with mixed Class/Tag has been finicky
// and the helpers give us byte-level control that matches what's on the wire.
func buildTestSignedData(t *testing.T, signer testSigner, signerCert *x509.Certificate, messageType domain.SCEPMessageType, transactionID string, senderNonce, encapContent []byte) []byte {
t.Helper()
// 1. messageDigest auth-attr: SHA-256 of the encap content.
contentDigest := sha256.Sum256(encapContent)
// 2. Build each auth-attr as Attribute ::= SEQUENCE { OID, SET OF Value }
// using the helpers. Marshal each value individually then wrap.
attrSetBody := buildSCEPAuthAttrs(t, contentDigest[:], messageType, transactionID, senderNonce)
// 3. Compute the signature over SET OF Attribute.
signedAttrsForSig := ASN1Wrap(0x31, attrSetBody)
sig, err := signer.Sign(signedAttrsForSig)
if err != nil {
t.Fatalf("signer.Sign: %v", err)
}
// 4. Build the SignerInfo SEQUENCE byte-by-byte.
versionBytes := []byte{0x02, 0x01, 0x01} // INTEGER 1
// SID is IssuerAndSerialNumber: SEQUENCE { Issuer (RDN), SerialNumber INTEGER }
serialDER, err := asn1.Marshal(signerCert.SerialNumber)
if err != nil {
t.Fatalf("marshal serial: %v", err)
}
sidBody := append([]byte{}, signerCert.RawIssuer...) // already in DER
sidBody = append(sidBody, serialDER...)
sidBytes := ASN1Wrap(0x30, sidBody)
// DigestAlgorithm: AlgorithmIdentifier — encode via stdlib (small struct, no nested RawValue issues).
digestAlgBytes := mustMarshal(t, pkix.AlgorithmIdentifier{Algorithm: signer.DigestOID(), Parameters: asn1.NullRawValue})
// SignedAttrs as [0] IMPLICIT SET OF — tag 0xA0 wraps the SET body.
signedAttrsImplicitBytes := ASN1Wrap(0xa0, attrSetBody)
// SignatureAlgorithm.
sigAlg := pkix.AlgorithmIdentifier{Algorithm: signer.SignatureOID()}
if signer.SignatureOID().Equal(OIDRSAWithSHA256) {
sigAlg.Parameters = asn1.NullRawValue
}
sigAlgBytes := mustMarshal(t, sigAlg)
// Signature: OCTET STRING.
sigOctetBytes := ASN1Wrap(0x04, sig)
siBody := append([]byte{}, versionBytes...)
siBody = append(siBody, sidBytes...)
siBody = append(siBody, digestAlgBytes...)
siBody = append(siBody, signedAttrsImplicitBytes...)
siBody = append(siBody, sigAlgBytes...)
siBody = append(siBody, sigOctetBytes...)
siBytes := ASN1Wrap(0x30, siBody)
// 5. Build encapContentInfo SEQUENCE { OID data, [0] EXPLICIT OCTET STRING }.
octetBytes := ASN1Wrap(0x04, encapContent) // OCTET STRING
encapContentExplicit := ASN1Wrap(0xa0, octetBytes) // [0] EXPLICIT
oidDataBytes := mustMarshal(t, OIDDataContent)
encapBody := append([]byte{}, oidDataBytes...)
encapBody = append(encapBody, encapContentExplicit...)
encapBytes := ASN1Wrap(0x30, encapBody)
// 6. certificates [0] IMPLICIT SET OF Certificate — body is one cert DER.
certsBytes := ASN1Wrap(0xa0, signerCert.Raw)
// 7. digestAlgorithms SET OF AlgorithmIdentifier (one entry).
digestAlgsBytes := ASN1Wrap(0x31, digestAlgBytes)
// 8. signerInfos SET OF SignerInfo (one entry).
signerInfosBytes := ASN1Wrap(0x31, siBytes)
// 9. Assemble SignedData SEQUENCE.
sdBody := append([]byte{}, []byte{0x02, 0x01, 0x01}...) // version
sdBody = append(sdBody, digestAlgsBytes...)
sdBody = append(sdBody, encapBytes...)
sdBody = append(sdBody, certsBytes...)
sdBody = append(sdBody, signerInfosBytes...)
sdSeq := ASN1Wrap(0x30, sdBody)
// 10. Wrap as ContentInfo SEQUENCE { OID signedData, [0] EXPLICIT SignedData }.
contentField := ASN1Wrap(0xa0, sdSeq)
oidSignedDataDER := []byte{0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02}
ciBody := append([]byte{}, oidSignedDataDER...)
ciBody = append(ciBody, contentField...)
return ASN1Wrap(0x30, ciBody)
}
// buildSCEPAuthAttrs builds the SET-OF body of SCEP auth-attrs (the bytes
// inside the [0] IMPLICIT SignedAttrs wrapper). Each Attribute is a SEQUENCE
// of (OID, SET OF Value); we build them with ASN1Wrap to avoid asn1.Marshal
// nuances with nested RawValues.
func buildSCEPAuthAttrs(t *testing.T, msgDigest []byte, messageType domain.SCEPMessageType, transactionID string, senderNonce []byte) []byte {
t.Helper()
var out []byte
// contentType: SET OF OID = SET { OID data }
out = append(out, attrSeq(t, OIDContentType, ASN1Wrap(0x06, []byte{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01}))...)
// messageDigest: SET OF OCTET STRING
out = append(out, attrSeq(t, OIDMessageDigest, ASN1Wrap(0x04, msgDigest))...)
// SCEP messageType: SET OF PrintableString (decimal ASCII)
out = append(out, attrSeq(t, OIDSCEPMessageType, ASN1Wrap(0x13, []byte(intToAscii(int(messageType)))))...)
// SCEP transactionID: SET OF PrintableString
out = append(out, attrSeq(t, OIDSCEPTransactionID, ASN1Wrap(0x13, []byte(transactionID)))...)
// SCEP senderNonce: SET OF OCTET STRING
out = append(out, attrSeq(t, OIDSCEPSenderNonce, ASN1Wrap(0x04, senderNonce))...)
return out
}
// attrSeq builds one Attribute SEQUENCE: SEQUENCE { OID, SET OF value }.
// The `value` arg is one already-encoded TLV (e.g. an OCTET STRING or
// PrintableString); attrSeq wraps it in a SET and prefixes the OID.
func attrSeq(t *testing.T, oid asn1.ObjectIdentifier, value []byte) []byte {
t.Helper()
oidBytes := mustMarshal(t, oid)
setOfValue := ASN1Wrap(0x31, value)
body := append([]byte{}, oidBytes...)
body = append(body, setOfValue...)
return ASN1Wrap(0x30, body)
}
func mustMarshal(t *testing.T, v interface{}) []byte {
t.Helper()
out, err := asn1.Marshal(v)
if err != nil {
t.Fatalf("marshal %T: %v", v, err)
}
return out
}
func intToAscii(i int) string {
if i == 0 {
return "0"
}
neg := i < 0
if neg {
i = -i
}
var b []byte
for i > 0 {
b = append([]byte{byte('0' + i%10)}, b...)
i /= 10
}
if neg {
b = append([]byte{'-'}, b...)
}
return string(b)
}