pkcs7

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

varwof-pkcs7

Pure Go PKCS#7 / CMS SignedData signing and verification library. Zero external dependencies.

⚠️ Preview — Not for production use. APIs and features may change before official release.

License Go Reference

中文

What is varwof-pkcs7?

Pure standard-library PKCS#7 / CMS SignedData signing and verification. Zero external dependencies. Supports ECDSA/RSA/Ed25519, CAdES-T timestamps, and detached signature verification. Used by varwof-core for CA certificate chain signing and code signing.

Quick Start

import "github.com/varwof/pkcs7"

// Sign
der, err := pkcs7.BuildSignedData(
    pkcs7.OIDData, data, cert, signer, nil,
)

// Verify
signerCert, err := pkcs7.VerifyDetached(der, data)

// Attach CAdES-T timestamp
tsDER, _ := requestTimestamp(der)
signed, _ := pkcs7.AddCAdESTimestamp(der, tsDER)

Installation

go get github.com/varwof/pkcs7@v0.1.0

Features

  • Zero external dependencies, pure standard library
  • PKCS#7 SignedData signing (BuildSignedData / BuildSignedDataWithHash / BuildSignedDataWithDigest)
  • CAdES-T timestamp attachment (AddCAdESTimestamp)
  • Detached signature verification (VerifyDetached)
  • Supports ECDSA (P-256/P-384/P-521) / RSA / Ed25519
  • Automatic hash algorithm selection based on certificate public key

Ecosystem

graph TB
    subgraph varwof["varwof Ecosystem"]
        core["core<br/>PKI CA"]
        gw["gateway<br/>TCP/HTTP/UDP"]
        client["client<br/>CLI"]
        gwcore["gateway-core<br/>Security Engine"]
        pkcs7["pkcs7<br/>CMS Signing"]
        types["types<br/>Shared Types"]
        cap["capability<br/>JSON Data"]
        reg["register<br/>Capability Registry"]
    end
    core --> gwcore
    gw --> gwcore
    client -->|mTLS| core
    gwcore --> pkcs7
    gwcore --> types
    reg --> cap
    core --> reg

pkcs7 provides CMS signing primitives used by core for code signing and CA certificate chain signing.

This project is a member of the Open Invention Network.

Homepage https://varwof.com
Community https://varwof.org
IETF Draft draft-wei-aic-identity-cert
License Apache-2.0
Member Open Invention Network

Documentation

Index

Constants

View Source
const Version = "0.1.1"

Version is the current pkcs7 library version.

Variables

View Source
var (
	OIDSignedData      = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
	OIDData            = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
	OIDSHA256          = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
	OIDSHA384          = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
	OIDSHA512          = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
	OIDEcdsaWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
	OIDEcdsaWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
	OIDEcdsaWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
	OIDRSAWithSHA256   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
	OIDRSAWithSHA384   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
	OIDRSAWithSHA512   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
	OIDEd25519         = asn1.ObjectIdentifier{1, 3, 101, 112}
)

Content type and signature algorithm OIDs (RFC 5652 / RFC 5754).

View Source
var OIDSignatureTimeStamp = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 16, 2, 14}

OIDSignatureTimeStamp is the attribute type for CAdES signature timestamps (RFC 5126).

Functions

func AddCAdESTimestamp

func AddCAdESTimestamp(pkcs7DER []byte, tstTokenDER []byte) ([]byte, error)

AddCAdESTimestamp appends a CAdES signature timestamp (RFC 5126) to a PKCS#7 SignedData.

func BuildSignedData

func BuildSignedData(eContentType asn1.ObjectIdentifier, eContent []byte, cert *x509.Certificate, signer crypto.Signer, chain []*x509.Certificate) ([]byte, error)

BuildSignedData builds a PKCS#7 SignedData with signing certificate attributes.

func BuildSignedDataWithDigest

func BuildSignedDataWithDigest(eContentType asn1.ObjectIdentifier, eContent, digest []byte, cert *x509.Certificate, signer crypto.Signer, chain []*x509.Certificate, hash crypto.Hash) ([]byte, error)

BuildSignedDataWithDigest builds a PKCS#7 SignedData using a precomputed digest. Unlike BuildSignedDataWithHash which hashes eContent internally, this function uses the provided digest directly for the messageDigest attribute. If eContent is nil, the EncapContentInfo.Content is omitted (detached signature).

func BuildSignedDataWithHash

func BuildSignedDataWithHash(eContentType asn1.ObjectIdentifier, eContent []byte, cert *x509.Certificate, signer crypto.Signer, chain []*x509.Certificate, hash crypto.Hash) ([]byte, error)

BuildSignedDataWithHash builds a PKCS#7 SignedData using the specified hash algorithm.

func BuildSignedDataWithoutCertificates added in v0.1.1

func BuildSignedDataWithoutCertificates(eContentType asn1.ObjectIdentifier, eContent []byte, cert *x509.Certificate, signer crypto.Signer, chain []*x509.Certificate) ([]byte, error)

BuildSignedDataWithoutCertificates is like BuildSignedData but omits the signing certificate (and any chain certificates) from SignedData.certificates. RFC 3161 §2.4.1: a TimeStampReq whose certReq flag is FALSE (the default) MUST result in a TimeStampToken whose SignedData.certificates field is empty. The ESSCertID signing attribute still carries the certificate hash so a verifier can identify the signer.

func HasCAdESUnsigned

func HasCAdESUnsigned(pkcs7DER []byte) bool

HasCAdESUnsigned reports whether any SignerInfo has unsigned attributes.

func SelectHash

func SelectHash(cert *x509.Certificate) crypto.Hash

SelectHash selects the signature hash algorithm based on the certificate's public key type.

func SignatureValue

func SignatureValue(pkcs7DER []byte) ([]byte, error)

SignatureValue extracts the first signature value from a PKCS#7 SignedData.

func VerifyDetached

func VerifyDetached(der []byte, content []byte) (*x509.Certificate, error)

Types

type AlgorithmIdentifier

type AlgorithmIdentifier struct {
	Algorithm  asn1.ObjectIdentifier
	Parameters asn1.RawValue `asn1:"optional"`
}

AlgorithmIdentifier is the signature/digest algorithm identifier (RFC 5652 §10.1).

type Attribute

type Attribute struct {
	Type   asn1.ObjectIdentifier
	Values []asn1.RawValue `asn1:"set"`
}

Attribute is the CMS attribute structure (RFC 5652 §5.3).

type ContentInfo

type ContentInfo struct {
	ContentType asn1.ObjectIdentifier
	Content     asn1.RawValue `asn1:"explicit,tag:0"`
}

ContentInfo is the CMS SignedData content information wrapper (RFC 5652 §5.2).

type EncapsulatedContentInfo

type EncapsulatedContentInfo struct {
	ContentType asn1.ObjectIdentifier
	Content     asn1.RawValue `asn1:"optional"`
}

EncapsulatedContentInfo is the signed content encapsulated within SignedData (RFC 5652 §5.2).

type IssuerAndSerial

type IssuerAndSerial struct {
	Issuer       asn1.RawValue `asn1:"set"`
	SerialNumber asn1.RawValue
}

IssuerAndSerial identifies the signer certificate by issuer and serial number (RFC 5652 §5.3).

type SignedData

type SignedData struct {
	Version          int
	DigestAlgorithms []AlgorithmIdentifier `asn1:"set"`
	EncapContentInfo EncapsulatedContentInfo
	Certificates     []asn1.RawValue `asn1:"optional,implicit,tag:0"`
	SignerInfos      []SignerInfo    `asn1:"set"`
}

SignedData is the CMS SignedData structure (RFC 5652 §5.1).

type SignerInfo

type SignerInfo struct {
	Version            int
	IssuerAndSerial    IssuerAndSerial
	DigestAlgorithm    AlgorithmIdentifier
	SignedAttributes   []Attribute `asn1:"optional,implicit,tag:0"`
	SignatureAlgorithm AlgorithmIdentifier
	Signature          []byte
	UnsignedAttributes []Attribute `asn1:"optional,implicit,tag:1"`
}

SignerInfo is the information for a single signer (RFC 5652 §5.3).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL