pkcs7

package module
v0.1.0 Latest Latest
Warning

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

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

README

Varwof PKCS#7

Pure standard-library PKCS#7 / CMS SignedData signing/verification implementation. Zero external dependencies.

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

Install

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

Usage

import "github.com/varwof/pkcs7"

// Sign
der, err := pkcs7.BuildSignedData(
    pkcs7.OIDData,    // content type
    data,             // data to sign
    cert,             // signing certificate
    signer,           // signing private key
    nil,              // certificate chain (optional)
)

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

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

License

Apache-2.0

Documentation

Index

Constants

View Source
const Version = "0.1.0"

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 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