Help us shape the future of Go! Take the 2026 Go Developer Survey

pkcs7

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0, MIT Imports: 19 Imported by: 0

Documentation

Overview

Package pkcs7 implements parsing and generation of some PKCS#7 structures.

Index

Constants

This section is empty.

Variables

View Source
var (
	// OIDData and OIDSignedData identify signed-data content types.
	OIDData       = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
	OIDSignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}

	// OIDDigestAlgorithmSHA1 and the following values identify digest algorithms.
	OIDDigestAlgorithmSHA1   = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
	OIDDigestAlgorithmSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
	OIDDigestAlgorithmSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
	OIDDigestAlgorithmSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}

	OIDDigestAlgorithmDSA     = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
	OIDDigestAlgorithmDSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}

	OIDDigestAlgorithmECDSASHA1   = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
	OIDDigestAlgorithmECDSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
	OIDDigestAlgorithmECDSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
	OIDDigestAlgorithmECDSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}

	// OIDEncryptionAlgorithmRSA and the following values identify signature algorithms and elliptic curves.
	OIDEncryptionAlgorithmRSA       = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
	OIDEncryptionAlgorithmRSASHA1   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
	OIDEncryptionAlgorithmRSAPSS    = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10}
	OIDEncryptionAlgorithmRSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
	OIDEncryptionAlgorithmRSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
	OIDEncryptionAlgorithmRSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}

	OIDEncryptionAlgorithmECDSAP256   = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7} // 256 bit elliptic curve
	OIDEncryptionAlgorithmECDSAP384   = asn1.ObjectIdentifier{1, 3, 132, 0, 34}          // 384-bit elliptic curve
	OIDEncryptionAlgorithmECDSAP521   = asn1.ObjectIdentifier{1, 3, 132, 0, 35}          // 512-bit elliptic curve!
	OIDEncryptionAlgorithmECPUBLICKEY = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}    // ecPublicKey

	OIDEncryptionAlgorithmEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112}
)

PKCS#7 content, attribute, digest, signature and curve object identifiers.

View Source
var ErrAlgorithmMismatch = errors.New("pkcs7: algorithm mismatch")

ErrAlgorithmMismatch identifies inconsistent CMS digest and signature algorithms.

View Source
var ErrCertificateParse = errors.New("pkcs7: certificate parse error")

ErrCertificateParse identifies malformed certificate data embedded in a PKCS#7 message.

View Source
var ErrEmptyInput = errors.New("pkcs7: input data is empty")

ErrEmptyInput identifies missing PKCS#7 input data.

View Source
var ErrInvalidPSSParameters = errors.New("pkcs7: invalid RSASSA-PSS parameters")

ErrInvalidPSSParameters identifies malformed or unsupported RSASSA-PSS parameters.

View Source
var ErrMalformedAttribute = errors.New("pkcs7: malformed attribute")

ErrMalformedAttribute identifies a missing or malformed signed attribute.

View Source
var ErrMissingSignerIdentifier = errors.New("pkcs7: missing signer identifier")

ErrMissingSignerIdentifier identifies an incomplete signer issuer-and-serial identifier.

View Source
var ErrSignatureMismatch = errors.New("pkcs7: cryptographic signature mismatch")

ErrSignatureMismatch identifies an actual cryptographic signature mismatch.

View Source
var ErrUnsupportedAlgorithm = errors.New("pkcs7: unsupported algorithm")

ErrUnsupportedAlgorithm identifies a cryptographic algorithm unsupported by this package.

View Source
var ErrUnsupportedContentType = errors.New("pkcs7: cannot parse data: unimplemented content type")

ErrUnsupportedContentType is returned when a PKCS7 content is not supported. Currently only Signed Data (1.2.840.113549.1.7.2) is supported.

Functions

func CheckSignature

func CheckSignature(
	cert *x509.Certificate,
	signer SignerInfo,
	content []byte,
) error

CheckSignature verifies signer's content binding and encrypted digest using cert. Signed attributes, when present, are checked against id-data content.

func CheckSignatureWithContentType

func CheckSignatureWithContentType(
	cert *x509.Certificate,
	signer SignerInfo,
	content []byte,
	contentType asn1.ObjectIdentifier,
) error

CheckSignatureWithContentType verifies signer's content binding and encrypted digest using cert and the encapsulated content type.

func GetCertFromCertsByIssuerAndSerial

func GetCertFromCertsByIssuerAndSerial(
	certs []*x509.Certificate,
	ias issuerAndSerial,
) (*x509.Certificate, error)

GetCertFromCertsByIssuerAndSerial returns the certificate matching ias. It returns ErrMissingSignerIdentifier when ias is incomplete.

func HashForOID

func HashForOID(oid asn1.ObjectIdentifier) (crypto.Hash, error)

HashForOID returns the Go hash corresponding to oid.

func OIDForEncryptionAlgorithm

func OIDForEncryptionAlgorithm(pkey crypto.PrivateKey, OIDDigestAlg asn1.ObjectIdentifier) (asn1.ObjectIdentifier, error)

OIDForEncryptionAlgorithm takes the private key type of the signer and the OID of a digest algorithm to return the appropriate signerInfo.DigestEncryptionAlgorithm

func VerifyCertChain

func VerifyCertChain(
	ee *x509.Certificate,
	certs []*x509.Certificate,
	truststore *x509.CertPool,
	currentTime time.Time,
) ([][]*x509.Certificate, error)

VerifyCertChain builds all chains from ee through certs to a root in truststore.

When verifying chains that may have expired, currentTime can be set to a past date to allow the verification to pass. If unset, currentTime is set to the current UTC time.

func VerifyMessageDigestDetached

func VerifyMessageDigestDetached(signer SignerInfo, signedData []byte) error

VerifyMessageDigestDetached verifies signer's authenticated message digest against signedData.

func VerifyMessageDigestEmbedded

func VerifyMessageDigestEmbedded(digest, signedData []byte) error

VerifyMessageDigestEmbedded verifies the SHA-1 document digest mandated by the legacy adbe.pkcs7.sha1 profile. It supports validation of existing PDFs only.

func VerifyMessageDigestTSToken

func VerifyMessageDigestTSToken(oidHashAlg asn1.ObjectIdentifier, digest, signedData []byte) error

VerifyMessageDigestTSToken verifies a timestamp-token digest against signedData using oidHashAlg.

Types

type Attribute

type Attribute struct {
	Type  asn1.ObjectIdentifier
	Value interface{}
}

Attribute represents a key-value attribute whose value must be marshalable by encoding/asn1.

type CertificateParseError

type CertificateParseError struct {
	// contains filtered or unexported fields
}

CertificateParseError reports malformed certificate data embedded in a PKCS#7 message while preserving the underlying ASN.1 or X.509 parse error.

func (*CertificateParseError) Error

func (e *CertificateParseError) Error() string

Error implements error.

func (*CertificateParseError) Is

func (e *CertificateParseError) Is(target error) bool

Is classifies this error as ErrCertificateParse.

func (*CertificateParseError) Unwrap

func (e *CertificateParseError) Unwrap() error

Unwrap returns the underlying ASN.1 or X.509 parse error.

type MessageDigestMismatchError

type MessageDigestMismatchError struct {
	ExpectedDigest []byte
	ActualDigest   []byte
}

MessageDigestMismatchError is returned when the signer data digest does not match the computed digest for the contained content.

func (*MessageDigestMismatchError) Error

func (err *MessageDigestMismatchError) Error() string

Error returns the message-digest mismatch details.

type PKCS7

type PKCS7 struct {
	Content      []byte
	ContentType  asn1.ObjectIdentifier
	Certificates []*x509.Certificate
	CRLs         []*x509.RevocationList
	Signers      []SignerInfo
}

PKCS7 represents a parsed PKCS#7 structure.

func Parse

func Parse(data []byte) (*PKCS7, error)

Parse decodes a BER- or DER-encoded PKCS#7 package.

type SignedData

type SignedData struct {
	// contains filtered or unexported fields
}

SignedData is an opaque data structure for creating signed data payloads

func NewSignedData

func NewSignedData() (*SignedData, error)

NewSignedData initializes a PKCS7 SignedData struct that is ready to be signed via AddSigner.

func (*SignedData) AddCertificate

func (sd *SignedData) AddCertificate(cert *x509.Certificate) error

AddCertificate adds cert to the payload.

func (*SignedData) AddSigner

func (sd *SignedData) AddSigner(cert *x509.Certificate, pkey crypto.PrivateKey, messageDigest []byte, digestOid asn1.ObjectIdentifier, config SignerInfoConfig) error

AddSigner is a wrapper around AddSignerChain that adds a signer without a parent.

func (*SignedData) AddSignerChain

func (sd *SignedData) AddSignerChain(cert *x509.Certificate, pkey crypto.PrivateKey, messageDigest []byte, digestOid asn1.ObjectIdentifier, parents []*x509.Certificate, config SignerInfoConfig) error

AddSignerChain signs attributes about the content and adds certificates and signers infos to the Signed Data. The certificate and private key of the end-entity signer are used to issue the signature, and any parent of that end-entity that need to be added to the list of certifications can be specified in the parents slice.

The signature algorithm used to hash the data is the one of the end-entity certificate aka the cert.

func (*SignedData) Finish

func (sd *SignedData) Finish() ([]byte, error)

Finish encodes sd as PKCS#7 signed data.

type SignerInfo

type SignerInfo struct {
	Version                   int `asn1:"default:1"`
	IssuerAndSerialNumber     issuerAndSerial
	DigestAlgorithm           pkix.AlgorithmIdentifier
	AuthenticatedAttributes   []attribute `asn1:"optional,omitempty,tag:0"` // RFC5652: signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL
	DigestEncryptionAlgorithm pkix.AlgorithmIdentifier
	EncryptedDigest           []byte      `asn1:"octet"`
	UnauthenticatedAttributes []attribute `asn1:"optional,omitempty,tag:1"` // RFC5652: unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL
}

SignerInfo contains the algorithms, attributes, identity and encrypted digest for one PKCS#7 signer.

type SignerInfoConfig

type SignerInfoConfig struct {
	ExtraSignedAttributes   []Attribute
	ExtraUnsignedAttributes []Attribute
}

SignerInfoConfig are optional values to include when adding a signer

Jump to

Keyboard shortcuts

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