Documentation
¶
Overview ¶
Package cms reads, verifies and signs CMS SignedData (RFC 5652): the container a PDF signature, a UEFI authenticated variable and an S/MIME message carry. Sign produces one; see SignOptions.
Parse reads the structure; (*SignedData).Verify checks every signer's signature against the signed content and returns the certificate each one used. Whether that certificate should be TRUSTED — its chain, its validity period, its revocation — is a separate question that RFC 5652 §5.6 places "beyond the scope of this document", and so does this package: hand the returned certificates to crypto/x509's Verify, and to go-authn/revocation for CRLs.
⛔ DER ONLY, for now. RFC 5652 lets the outer structure be BER, and some producers write indefinite lengths; encoding/asn1 does not read them, and a structure that cannot be read is REFUSED with ErrMalformed rather than guessed at. The signed attributes themselves must be DER in every case (§5.3), so this limit never weakens what is verified — it only refuses some well-formed inputs that a BER-to-DER pass would accept.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMalformed: the bytes are not a SignedData this package can read. ErrMalformed = errors.New("cms: malformed") // ErrUnsupported: well-formed, but uses something this package does not // implement (an algorithm, SHA-1, a certificate it cannot find). ErrUnsupported = errors.New("cms: unsupported") // ErrInvalid: readable and supported, and the signature does not hold. ErrInvalid = errors.New("cms: signature invalid") )
The errors every failure wraps, so a caller can tell "this is not a signature" from "this is a signature I cannot check" from "this signature is wrong" — three answers RFC 5652 §5.1 asks an implementation to keep apart ("MUST gracefully handle unimplemented signature algorithms").
var ErrOptions = errors.New("cms: invalid signing options")
ErrOptions: the signing request is not one this package will carry out — a missing key, a certificate that is not the key's, a combination the RFCs forbid. Algorithms it does not implement wrap ErrUnsupported instead, as they do when reading.
var ErrSigner = errors.New("cms: signer failed")
ErrSigner: the crypto.Signer failed, or returned a signature that does not verify under the signer's certificate (another key, ignored PSS options, an empty signature). The signer's own error stays in the chain, so errors.Is finds it as well.
Functions ¶
func Sign ¶ added in v0.2.0
func Sign(content []byte, o SignOptions) ([]byte, error)
Sign returns a DER ContentInfo holding a SignedData with ONE signer.
⛔ What it will not emit: SHA-1 or any digest outside SHA-2 256/384/512; RSASSA-PSS parameters other than "this hash, MGF1 with it, salt of its length"; Ed25519 with another digest; a certificate whose key is not the signer's; a signature that omits the attributes yet claims another content type. Everything Sign writes is DER — signedAttrs in particular is a SET OF sorted by encoding (§5.4 signs those bytes, and the reader here is strict about them) — and every product is meant to pass this package's Verify and OpenSSL's `cms -verify`, which the tests assert.
Types ¶
type Attribute ¶
type Attribute struct {
Type asn1.ObjectIdentifier
Values [][]byte
}
Attribute is one signed attribute: a type and its values, each value kept as its complete DER encoding.
type IssuerAndSerial ¶
IssuerAndSerial identifies a certificate by its issuer's name, as encoded, and its serial number.
type SignOptions ¶ added in v0.2.0
type SignOptions struct {
// Signer holds the private key: an *rsa.PrivateKey, an *ecdsa.PrivateKey
// or an ed25519.PrivateKey, or any crypto.Signer whose Public key is one
// of those types (a smart card, an HSM, a remote signer).
Signer crypto.Signer
// Certificate is the signer's certificate. It is embedded, and the
// SignerInfo names it by issuer and serial number (RFC 5652 §5.3). Its
// public key must be Signer's.
Certificate *x509.Certificate
// Chain is further certificates to embed (intermediates).
Chain []*x509.Certificate
// Hash is the digest: crypto.SHA256 (the default), SHA384 or SHA512.
// Ed25519 signs with SHA-512 (RFC 8419 §3.1) and refuses any other.
// SHA-1 is refused by name.
Hash crypto.Hash
// PSS selects RSASSA-PSS (MGF1 with Hash, salt as long as the hash)
// instead of RSASSA-PKCS1-v1_5. RSA keys only.
PSS bool
// Rand is the entropy for RSA-PSS and ECDSA; crypto/rand when nil.
Rand io.Reader
// ContentType is the eContentType; id-data when nil.
ContentType asn1.ObjectIdentifier
// Detached leaves the content out of the container, as a PDF signature
// does: the verifier is handed the signed bytes (Verify's argument).
Detached bool
// NoSignedAttrs signs the content itself, with no signedAttrs — the
// PKCS#7 shape UEFI authenticated variables use. It needs id-data
// (§5.3) and excludes SigningTime and SigningCertificateV2, which are
// attributes.
NoSignedAttrs bool
// SigningTime, when not zero, adds the signing-time attribute (§11.3),
// to the second. PAdES carries the time elsewhere and CAdES leaves it
// optional, so it is opt-in.
SigningTime time.Time
// SigningCertificateV2 adds the signing-certificate-v2 attribute of
// RFC 5035 §5.4.1, which ETSI CAdES / PAdES baseline signatures require:
// the hash of the signer's certificate, and its issuer and serial.
SigningCertificateV2 bool
}
SignOptions says what Sign emits. The zero value, apart from Signer and Certificate, is a SignedData over id-data with signed attributes (content-type and message-digest), SHA-256, the content ATTACHED.
type SignedData ¶
type SignedData struct {
Version int
DigestAlgorithms []pkix.AlgorithmIdentifier
// ContentType is encapContentInfo's eContentType: id-data for a PDF
// signature and for most others.
ContentType asn1.ObjectIdentifier
// Content is the encapsulated content, or nil when the signature is
// DETACHED — as a PDF signature always is: the signed bytes are the
// file's /ByteRange, and Verify takes them as its argument.
Content []byte
// Certificates are those the SignedData carries. Certificate choices
// other than an X.509 certificate (attribute certificates, "other") are
// skipped and counted in SkippedCertificates.
Certificates []*x509.Certificate
SkippedCertificates int
Signers []Signer
}
SignedData is a parsed CMS SignedData.
func Parse ¶
func Parse(der []byte) (*SignedData, error)
Parse reads a DER-encoded ContentInfo holding a SignedData.
func (*SignedData) Verify ¶
func (sd *SignedData) Verify(detached []byte) ([]*x509.Certificate, error)
Verify checks every signer's signature and returns the certificate each signer used, in signer order.
detached is the signed content when the SignedData does not carry it — for a PDF signature, the concatenation of the file's /ByteRange. It must be nil when the content is attached: supplying content that the container also carries is refused rather than silently preferring one of the two.
⛔ EVERY signer must verify. RFC 5652 §5.1 lets an application treat one good signature from a given signer as enough when that signer signed several times; a caller that wants that can verify signers itself. A container with no signers at all is refused: it signs nothing.
The returned certificates are only those that SIGNED. Whether to trust them — chain, validity, revocation — is the caller's, per §5.6.
type Signer ¶
type Signer struct {
Version int
// Exactly one of IssuerAndSerial and SubjectKeyID is set: RFC 5652 §5.3
// requires a recipient to accept both forms of SignerIdentifier.
IssuerAndSerial *IssuerAndSerial
SubjectKeyID []byte
DigestAlgorithm pkix.AlgorithmIdentifier
// SignedAttrs is nil when the signer signed the content directly.
SignedAttrs []Attribute
SignatureAlgorithm pkix.AlgorithmIdentifier
Signature []byte
// contains filtered or unexported fields
}
Signer is one SignerInfo.