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

pkcs7

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 24 Imported by: 0

README

go-pkcs7

GitHub Release Go Reference License Tests Supply chain OpenSSF Scorecard

go-pkcs7 is a Go library that parses and builds the parts of the Cryptographic Message Syntax (CMS, RFC 5652) that certificate enrollment protocols use. SCEP (RFC 8894) wraps every request and response in these messages. go-pki-scep is built on this library.

It signs and verifies SignedData, encrypts and decrypts EnvelopedData for RSA recipients and encrypts and decrypts EncryptedData with a pre-shared key. Algorithm choices are made per call through option structs, so two parts of one program can apply different policies. Nothing is configured through package variables.

Contents

Install

go get github.com/misiektoja/go-pkcs7

The module needs Go 1.27.3 or newer and has no dependencies outside the standard library. The package is imported as pkcs7. The API may still change before v1.0.0, as CHANGELOG.md records.

Scope

Content type Coverage
SignedData (RFC 5652 section 5) Signing with RSA PKCS #1 v1.5 or ECDSA keys through crypto.Signer. Verification of RSA PKCS #1 v1.5, RSASSA-PSS (RFC 4055) and ECDSA signatures. Attached and detached content, several signers and messages that carry only certificates or a CRL
EnvelopedData (RFC 5652 section 6) RSA key transport with PKCS #1 v1.5 or RSAES-OAEP (RFC 8017). Decryption through crypto.Decrypter, so the recipient key may live in an HSM
EncryptedData (RFC 5652 section 8) Content encrypted with a pre-shared key

Digests are SHA-256, SHA-384, SHA-512, SHA-224 and SHA-1, plus MD5 on request. Content ciphers are AES-128-CBC, AES-192-CBC, AES-256-CBC, DES-EDE3-CBC and DES-CBC. Input may be BER, including indefinite lengths, and is converted to DER before parsing.

The tests check the library against OpenSSL whenever openssl is on the PATH. OpenSSL reads the signed messages and decrypts the enveloped messages this library builds. This library verifies OpenSSL signatures, including RSASSA-PSS, and decrypts OpenSSL RSAES-OAEP messages.

Getting started

Sign content with a certificate and its key, then verify the result against a trust anchor:

sd, err := pkcs7.NewSignedData(content)
if err != nil {
	return err
}
if err := sd.AddSigner(cert, key, pkcs7.SignerInfoConfig{}); err != nil {
	return err
}
signed, err := sd.Finish()
if err != nil {
	return err
}

p7, err := pkcs7.Parse(signed)
if err != nil {
	return err
}
roots := x509.NewCertPool()
roots.AddCert(caCert)
if err := p7.Verify(pkcs7.VerifyOptions{Roots: roots}); err != nil {
	return err
}

Encrypt for a recipient certificate and decrypt with the recipient's key:

enveloped, err := pkcs7.Encrypt(content, []*x509.Certificate{recipient}, pkcs7.EncryptOptions{
	ContentEncryption: pkcs7.AES256CBC,
	KeyEncryption:     pkcs7.RSAOAEP,
})
if err != nil {
	return err
}

p7, err := pkcs7.Parse(enveloped)
if err != nil {
	return err
}
plaintext, err := p7.Decrypt(recipient, recipientKey, pkcs7.DecryptOptions{})

ExampleSignedData in the package documentation builds a detached signature.

Security defaults

The defaults suit current peers. Each relaxation is an option on the call that needs it.

  • Signing uses SHA-256 unless SetDigestAlgorithm names another digest.
  • Verification checks signer chains at the current time. VerifyOptions.VerifyAtSigningTime uses the signing-time attribute instead. The signer chooses that time, so set it only for archived signatures. Without VerifyOptions.Roots the signatures are checked and no chain is built.
  • MD5 signatures are refused unless VerifyOptions.AllowMD5 is set.
  • Signed attributes must carry exactly one content type, equal to the encapsulated content type, and exactly one message digest. They are verified in the order received.
  • Decryption accepts AES-CBC and DES-EDE3-CBC. Single DES needs DecryptOptions.ContentEncryptionAlgorithms. Every key transport and padding failure returns ErrDecryptionFailed. PKCS #1 v1.5 key transport uses the RFC 3218 countermeasure, so the error does not tell a sender which step failed.
  • Encryption uses AES-128-CBC with PKCS #1 v1.5 key transport by default. EncryptOptions selects AES-256-CBC or RSAES-OAEP for peers that accept them.
  • Embedded certificates are parsed by crypto/x509. ParseOptions.LegacyCertificateParser also accepts certificates that mark the authority key identifier critical, which some Windows SCEP clients send.

Documentation

The package documentation on pkg.go.dev describes every exported type and option. Repository files:

Limitations

  • No AuthEnvelopedData (RFC 5083) and no AES-GCM content encryption.
  • No key agreement or key encryption key recipients. EnvelopedData recipients are RSA certificates.
  • No signing with RSASSA-PSS or Ed25519 and no verification of Ed25519 or DSA signatures.
  • No countersignatures, signed receipts or other CMS content types such as DigestedData and AuthenticatedData.
  • No streaming. Messages are held in memory while they are parsed or built.

Credits

go-pkcs7 continues the work of these projects and their contributors:

All three are MIT licensed. NOTICE records the origins of the code, including the Go standard library code copied under internal/.

Support

SUPPORT.md directs usage questions, bug reports, feature requests and security reports.

License

Licensed under the MIT License. The copies of Go code under internal/ keep the Go BSD-style license, as NOTICE describes.

Documentation

Overview

Package pkcs7 parses and builds the parts of the Cryptographic Message Syntax (CMS, RFC 5652) that certificate enrollment protocols such as SCEP use: SignedData, EnvelopedData with RSA key transport and EncryptedData with a pre-shared key.

Algorithm choices are made per call through option structs. Nothing is configured through package variables.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Signed Data OIDs
	OIDData                   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
	OIDSignedData             = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
	OIDEnvelopedData          = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 3}
	OIDEncryptedData          = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 6}
	OIDAttributeContentType   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 3}
	OIDAttributeMessageDigest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 4}
	OIDAttributeSigningTime   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 5}

	// Digest Algorithms
	OIDDigestAlgorithmMD5    = asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 5}
	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}
	OIDDigestAlgorithmSHA224 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 4}

	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}

	// Signature algorithms from RFC 8017 appendix A.2.4 and RSASSA-PSS from RFC 4055
	OIDEncryptionAlgorithmRSAMD5    = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4}
	OIDEncryptionAlgorithmRSASHA1   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
	OIDEncryptionAlgorithmRSASSAPSS = 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}
	OIDEncryptionAlgorithmRSASHA224 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 14}

	OIDEncryptionAlgorithmECDSAP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
	OIDEncryptionAlgorithmECDSAP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34}
	OIDEncryptionAlgorithmECDSAP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}

	// Asymmetric encryption algorithms and their parameters from RFC 8017 appendices A.2 and B.2
	OIDEncryptionAlgorithmRSA       = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
	OIDEncryptionAlgorithmRSAESOAEP = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 7}
	OIDMaskGenerationFunctionMGF1   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8}
	OIDOAEPSourcePSpecified         = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 9}

	// Symmetric encryption algorithms from RFC 8018 appendix B.2 and RFC 3565 section 4.1
	OIDEncryptionAlgorithmDESCBC     = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 7}
	OIDEncryptionAlgorithmDESEDE3CBC = asn1.ObjectIdentifier{1, 2, 840, 113549, 3, 7}
	OIDEncryptionAlgorithmAES128CBC  = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 2}
	OIDEncryptionAlgorithmAES192CBC  = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 22}
	OIDEncryptionAlgorithmAES256CBC  = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 42}
)
View Source
var DefaultContentDecryptionAlgorithms = []ContentEncryptionAlgorithm{AES128CBC, AES192CBC, AES256CBC, DESEDE3CBC}

DefaultContentDecryptionAlgorithms are the content ciphers accepted when DecryptOptions lists none.

View Source
var ErrAttributeNotFound = errors.New("pkcs7: attribute type not in attributes")

ErrAttributeNotFound is returned when a requested signed attribute is absent.

View Source
var ErrContentEncryptionNotAllowed = errors.New("pkcs7: content encryption algorithm is not allowed")

ErrContentEncryptionNotAllowed is returned when DecryptOptions do not accept the content cipher of a message.

View Source
var ErrDecryptionFailed = errors.New("pkcs7: decryption failed")

ErrDecryptionFailed is returned when the content key or the content cannot be decrypted. It does not say which step failed, so that a sender cannot use the error as a padding oracle.

View Source
var ErrNotEncryptedContent = errors.New("pkcs7: content data is not an encrypted data type")

ErrNotEncryptedContent is returned when attempting to Decrypt data that is not encrypted data

View Source
var ErrPSKNotProvided = errors.New("pkcs7: cannot encrypt content: PSK not provided")

ErrPSKNotProvided is returned when attempting to encrypt using a PSK without actually providing the PSK.

View Source
var ErrUnsupportedAlgorithm = errors.New(
	"pkcs7: cannot decrypt data: only AES-CBC, DES-EDE3-CBC and DES-CBC are supported",
)

ErrUnsupportedAlgorithm is returned when the content cipher of a message is not implemented.

View Source
var ErrUnsupportedAsymmetricEncryptionAlgorithm = errors.New(
	"pkcs7: cannot decrypt data: only RSA PKCS#1 v1.5 and RSA OAEP are supported",
)

ErrUnsupportedAsymmetricEncryptionAlgorithm is returned when a recipient uses an unknown key encryption algorithm.

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

ErrUnsupportedContentType is returned when a PKCS7 content type is not supported. Only SignedData, EnvelopedData and EncryptedData are supported.

View Source
var ErrUnsupportedEncryptionAlgorithm = errors.New(
	"pkcs7: cannot encrypt content: only AES-CBC, DES-EDE3-CBC and DES-CBC are supported",
)

ErrUnsupportedEncryptionAlgorithm is returned when attempting to encrypt content with an unsupported algorithm.

View Source
var ErrUnsupportedKeyEncryptionAlgorithm = errors.New("pkcs7: unsupported key encryption algorithm provided")

ErrUnsupportedKeyEncryptionAlgorithm is returned when an unsupported key encryption algorithm is requested.

View Source
var ErrUnsupportedKeyEncryptionHash = errors.New("pkcs7: unsupported key encryption hash provided")

ErrUnsupportedKeyEncryptionHash is returned when an unsupported key encryption hash is provided.

View Source
var ErrUnsupportedKeyType = errors.New("pkcs7: only RSA keys are supported")

ErrUnsupportedKeyType is returned when attempting to encrypting keys using a key that's not an RSA key

Functions

func DegenerateCRL

func DegenerateCRL(crl []byte) ([]byte, error)

DegenerateCRL creates a signed data structure with no signers and no certificates that carries one DER encoded CRL, the form of a SCEP GetCRL response (RFC 8894).

func DegenerateCertificate

func DegenerateCertificate(cert []byte) ([]byte, error)

DegenerateCertificate creates a signed data structure containing only the provided certificate or certificate chain.

func Encrypt

func Encrypt(content []byte, recipients []*x509.Certificate, opts EncryptOptions) ([]byte, error)

Encrypt creates an EnvelopedData message whose content key is encrypted for each recipient certificate. Recipients must hold RSA keys.

func EncryptUsingPSK

func EncryptUsingPSK(content []byte, key []byte, opts EncryptOptions) ([]byte, error)

EncryptUsingPSK creates an EncryptedData message whose content is encrypted with the pre-shared key. The key length must match the content cipher.

Types

type Attribute

type Attribute struct {
	Type  asn1.ObjectIdentifier
	Value any
}

Attribute represents a key value pair attribute. Value must be marshalable by `encoding/asn1`.

type ContentEncryptionAlgorithm

type ContentEncryptionAlgorithm int

ContentEncryptionAlgorithm identifies the symmetric cipher that encrypts EnvelopedData and EncryptedData content. The zero value selects AES128CBC.

const (
	// AES128CBC is AES with a 128-bit key in CBC mode. SCEP (RFC 8894) requires it.
	AES128CBC ContentEncryptionAlgorithm = iota + 1
	// AES192CBC is AES with a 192-bit key in CBC mode.
	AES192CBC
	// AES256CBC is AES with a 256-bit key in CBC mode.
	AES256CBC
	// DESEDE3CBC is three-key triple DES in CBC mode, for clients that predate AES.
	DESEDE3CBC
	// DESCBC is single DES in CBC mode. Its 56-bit key is broken. Decryption refuses it unless
	// DecryptOptions lists it.
	DESCBC
)

func (ContentEncryptionAlgorithm) String

String returns the OpenSSL-style name of the algorithm.

type DecryptOptions

type DecryptOptions struct {
	// ContentEncryptionAlgorithms lists the content ciphers to accept. When empty,
	// DefaultContentDecryptionAlgorithms applies, which leaves out single DES.
	ContentEncryptionAlgorithms []ContentEncryptionAlgorithm
}

DecryptOptions controls which algorithms Decrypt and DecryptUsingPSK accept.

type EncryptOptions

type EncryptOptions struct {
	// ContentEncryption is the content cipher. The zero value selects AES128CBC.
	ContentEncryption ContentEncryptionAlgorithm

	// KeyEncryption is the RSA key transport for each recipient. The zero value selects RSAPKCS1v15.
	// EncryptUsingPSK ignores it.
	KeyEncryption KeyEncryptionAlgorithm

	// OAEPHash is the RSAES-OAEP hash and MGF1 hash. The zero value selects SHA-256.
	OAEPHash crypto.Hash
}

EncryptOptions selects the algorithms Encrypt and EncryptUsingPSK use.

type Hasher

type Hasher interface {
	Hash(crypto.Hash, io.Reader) ([]byte, error)
}

Hasher is an interface defining a custom hash calculator.

type KeyEncryptionAlgorithm

type KeyEncryptionAlgorithm int

KeyEncryptionAlgorithm identifies how the content key is encrypted for an RSA recipient. The zero value selects RSAPKCS1v15.

const (
	// RSAPKCS1v15 is RSAES-PKCS1-v1_5 key transport. SCEP clients expect it.
	RSAPKCS1v15 KeyEncryptionAlgorithm = iota + 1
	// RSAOAEP is RSAES-OAEP key transport (RFC 8017) with MGF1 over EncryptOptions.OAEPHash.
	RSAOAEP
)

func (KeyEncryptionAlgorithm) String

func (a KeyEncryptionAlgorithm) String() string

String returns the name of the algorithm.

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 implements error.

type PKCS7

type PKCS7 struct {
	Content      []byte
	Certificates []*x509.Certificate
	CRLs         []pkix.CertificateList
	Signers      []signerInfo
	Hasher       Hasher
	// contains filtered or unexported fields
}

PKCS7 is a parsed CMS message.

func Parse

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

Parse decodes a BER or DER encoded CMS ContentInfo with the default ParseOptions.

func ParseWithOptions

func ParseWithOptions(data []byte, opts ParseOptions) (*PKCS7, error)

ParseWithOptions decodes a BER or DER encoded CMS ContentInfo. The input must hold exactly one ContentInfo.

func (*PKCS7) ContentEncryptionAlgorithm

func (p7 *PKCS7) ContentEncryptionAlgorithm() (ContentEncryptionAlgorithm, error)

ContentEncryptionAlgorithm returns the content cipher of an EnvelopedData or EncryptedData message. A responder can use it to answer with the cipher the requester chose.

func (*PKCS7) Decrypt

func (p7 *PKCS7) Decrypt(cert *x509.Certificate, key crypto.Decrypter, opts DecryptOptions) ([]byte, error)

Decrypt decrypts EnvelopedData content for the recipient certificate with its RSA private key.

func (*PKCS7) DecryptUsingPSK

func (p7 *PKCS7) DecryptUsingPSK(key []byte, opts DecryptOptions) ([]byte, error)

DecryptUsingPSK decrypts EncryptedData content with a pre-shared key.

func (*PKCS7) GetOnlySigner

func (p7 *PKCS7) GetOnlySigner() *x509.Certificate

GetOnlySigner returns an x509.Certificate for the first signer of the signed data payload. If there are more or less than one signer, nil is returned

func (*PKCS7) Recipients

func (p7 *PKCS7) Recipients() ([]Recipient, error)

Recipients lists the recipients of an EnvelopedData message, so a receiver that holds several keys can choose the one that decrypts it. They come in encoded order. DER sorts the recipient set, so the order need not match the certificates given to Encrypt.

func (*PKCS7) UnmarshalSignedAttribute

func (p7 *PKCS7) UnmarshalSignedAttribute(attributeType asn1.ObjectIdentifier, out any) error

UnmarshalSignedAttribute decodes a signed attribute of the first signer into out. The attribute must appear once with a single value. It returns ErrAttributeNotFound when it is absent.

func (*PKCS7) Verify

func (p7 *PKCS7) Verify(opts VerifyOptions) error

Verify checks every signer of a SignedData message: the message digest, the signed attributes, the signature and, when opts.Roots is set, the signer certificate chain. For a detached signature, set p7.Content to the signed data first.

type ParseOptions

type ParseOptions struct {
	// LegacyCertificateParser retries embedded certificates that crypto/x509 refuses because their
	// authority key identifier extension is marked critical, using a copy of the Go 1.23 parser that
	// skips that check. Windows SCEP clients send such certificates. RFC 5280 forbids the critical
	// flag, so enable this only where those clients must be served.
	// See https://go-review.googlesource.com/c/go/+/562341.
	LegacyCertificateParser bool
}

ParseOptions controls how ParseWithOptions decodes a message.

type Recipient

type Recipient struct {
	// Issuer is the DER encoded issuer name.
	Issuer []byte
	// SerialNumber is the certificate serial number.
	SerialNumber *big.Int
}

Recipient identifies a recipient of an EnvelopedData message by the issuer name and serial number of its certificate.

type SignedData

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

SignedData is an opaque data structure for creating signed data payloads

Example

ExampleSignedData shows how to produce a detached signature.

// generate a signing cert or load a key pair
cert, err := createTestCertificate(x509.SHA256WithRSA)
if err != nil {
	fmt.Printf("Cannot create test certificates: %s", err)
}

// Initialize a SignedData struct with content to be signed
signedData, err := NewSignedData([]byte("Example data to be signed"))
if err != nil {
	fmt.Printf("Cannot initialize signed data: %s", err)
}

// Add the signing cert and private key
if err := signedData.AddSigner(cert.Certificate, cert.PrivateKey, SignerInfoConfig{}); err != nil {
	fmt.Printf("Cannot add signer: %s", err)
}

// Call Detach() is you want to remove content from the signature
// and generate an S/MIME detached signature
signedData.Detach()

// Finish() to obtain the signature bytes
detachedSignature, err := signedData.Finish()
if err != nil {
	fmt.Printf("Cannot finish signing data: %s", err)
}
_ = pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: detachedSignature})

func NewSignedData

func NewSignedData(data []byte) (*SignedData, error)

NewSignedData starts a SignedData message over data. Signers use SHA-256 unless SetDigestAlgorithm is called before they are added.

func (*SignedData) AddCertificate

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

AddCertificate adds the certificate to the payload. Useful for parent certificates

func (*SignedData) AddSigner

func (sd *SignedData) AddSigner(ee *x509.Certificate, key crypto.Signer, config SignerInfoConfig) error

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

func (*SignedData) AddSignerChain

func (sd *SignedData) AddSignerChain(ee *x509.Certificate, key crypto.Signer, parents []*x509.Certificate, config SignerInfoConfig) error

AddSignerChain signs attributes about the content and adds certificates and signer infos to the SignedData. The end-entity certificate and key issue the signature. Parents that should travel with the message are listed in order, the issuer of ee first.

The signed attributes are content-type, message-digest and signing-time, followed by config.ExtraSignedAttributes.

func (*SignedData) Detach

func (sd *SignedData) Detach()

Detach removes content from the signed data struct to make it a detached signature. This must be called right before Finish()

func (*SignedData) Finish

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

Finish marshals the content and its signers

func (*SignedData) GetSignedData

func (sd *SignedData) GetSignedData() *signedData

GetSignedData returns the private Signed Data

func (*SignedData) RemoveAuthenticatedAttributes

func (sd *SignedData) RemoveAuthenticatedAttributes()

RemoveAuthenticatedAttributes removes authenticated attributes from signedData similar to OpenSSL's PKCS7_NOATTR or -noattr flags

func (*SignedData) RemoveCertificates

func (sd *SignedData) RemoveCertificates()

RemoveCertificates leaves every certificate out of the message, including the signer certificates. RFC 5652 allows this when recipients already hold the signer certificate, as SCEP clients do from GetCACert.

func (*SignedData) RemoveUnauthenticatedAttributes

func (sd *SignedData) RemoveUnauthenticatedAttributes()

RemoveUnauthenticatedAttributes removes unauthenticated attributes from signedData

func (*SignedData) SetDigestAlgorithm

func (sd *SignedData) SetDigestAlgorithm(d asn1.ObjectIdentifier)

SetDigestAlgorithm sets the digest algorithm, such as OIDDigestAlgorithmSHA256, for signers added afterwards.

func (*SignedData) SetEncryptionAlgorithm

func (sd *SignedData) SetEncryptionAlgorithm(d asn1.ObjectIdentifier)

SetEncryptionAlgorithm sets the signature algorithm identifier for signers added afterwards. It must suit the signer key and digest. For RSA keys, OIDEncryptionAlgorithmRSA is accepted with any digest. When unset, the identifier is derived from the key and digest.

func (*SignedData) SignWithoutAttr

func (sd *SignedData) SignWithoutAttr(ee *x509.Certificate, key crypto.Signer, _ SignerInfoConfig) error

SignWithoutAttr issues a signature on the content of the pkcs7 SignedData. Unlike AddSigner/AddSignerChain, it calculates the digest on the data alone and does not include any signed attributes like timestamp and so on.

This function is needed to sign old Android APKs, something you probably shouldn't do unless you're maintaining backward compatibility for old applications.

type SignerInfoConfig

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

	// SigningTime is the value of the signing-time attribute. When zero, the current time is used.
	SigningTime time.Time
}

SignerInfoConfig are optional values to include when adding a signer

type SigningTimeNotValidError

type SigningTimeNotValidError struct {
	SigningTime time.Time
	NotBefore   time.Time // NotBefore of signer
	NotAfter    time.Time // NotAfter of signer
}

SigningTimeNotValidError is returned when the signing time attribute falls outside of the signer certificate validity.

func (*SigningTimeNotValidError) Error

func (e *SigningTimeNotValidError) Error() string

Error implements error.

type VerifyOptions

type VerifyOptions struct {
	// Roots holds the trust anchors for signer certificate chains. When nil, Verify checks the
	// signatures but builds no chain, so the caller must decide whether to trust the signer.
	Roots *x509.CertPool

	// Intermediates holds certificates that may complete a signer chain, in addition to the
	// certificates carried in the message.
	Intermediates []*x509.Certificate

	// SignerCertificates holds candidate signer certificates for messages that do not carry their
	// signer certificate. Some SCEP servers send responses without one.
	SignerCertificates []*x509.Certificate

	// CurrentTime is the time at which chains are checked. When zero, the current time is used,
	// unless VerifyAtSigningTime applies.
	CurrentTime time.Time

	// VerifyAtSigningTime checks chains at the time in the signing-time attribute when CurrentTime
	// is zero. The signer chooses that time, so set it only for archived signatures whose signing
	// time is trusted.
	VerifyAtSigningTime bool

	// KeyUsages lists the extended key usages a signer chain must allow. When empty, any usage is
	// accepted.
	KeyUsages []x509.ExtKeyUsage

	// AllowMD5 accepts signers that use the MD5 digest. Some old SCEP clients still do.
	AllowMD5 bool
}

VerifyOptions controls the checks Verify makes.

Directories

Path Synopsis
internal
cryptobyte
Package cryptobyte contains types that help with parsing and constructing length-prefixed, binary messages, including ASN.1 DER.
Package cryptobyte contains types that help with parsing and constructing length-prefixed, binary messages, including ASN.1 DER.
cryptobyte/asn1
Package asn1 contains supporting types for parsing and building ASN.1 messages with the cryptobyte package.
Package asn1 contains supporting types for parsing and building ASN.1 messages with the cryptobyte package.
legacy/x509
Package x509 implements a subset of the X.509 standard.
Package x509 implements a subset of the X.509 standard.

Jump to

Keyboard shortcuts

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