cms

package
v0.1.23 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package cms is the Cryptographic Message Syntax (RFC 5652) as S/MIME and Direct secure messaging use it: SignedData (sign a message, verify one and say who signed it) and EnvelopedData (encrypt a message to its recipients' certificates, decrypt one with our key).

Only what the Direct Applicability Statement and its Implementation Guide require: RSA with SHA-256 (SHA-1 accepted for reading only, since older HISPs still send it), RSA key transport with PKCS#1 v1.5 or OAEP, and AES-128 or AES-256 in CBC mode. The cryptography is the standard library's; this package is the ASN.1 around it.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotForUs = errors.New("cms: the message is not encrypted to any certificate this address holds")

ErrNotForUs is an EnvelopedData that names no recipient whose key we hold.

View Source
var ErrTooLarge = errors.New("cms: the compressed content expands past the size limit")

ErrTooLarge is returned when compressed content would expand past the limit given.

Functions

func Compress added in v0.1.17

func Compress(content []byte) ([]byte, error)

Compress wraps content in a CMS CompressedData with zlib, the one algorithm RFC 3274 defines.

func Decompress added in v0.1.17

func Decompress(der []byte, limit int64) ([]byte, error)

Decompress opens a CMS CompressedData. limit bounds what it expands to (a few kilobytes of zlib can claim gigabytes); zero or less means 1 GiB. BER from a streaming encoder is read as well as DER.

func Decrypt

func Decrypt(der []byte, keys []KeyPair) ([]byte, error)

Decrypt opens an EnvelopedData with whichever of the keys is a recipient (cert and its key).

func Encrypt

func Encrypt(content []byte, recipients []*x509.Certificate, oaep bool) ([]byte, error)

Encrypt makes an EnvelopedData of content for each recipient certificate, with AES-256-CBC and RSA key transport (OAEP when oaep, else PKCS#1 v1.5: both are in the Direct Applicability Statement; v1.5 is what most HISPs read).

func IsCompressed added in v0.1.17

func IsCompressed(der []byte) bool

IsCompressed reports whether der is a CMS CompressedData.

func Sign

func Sign(content []byte, cert *x509.Certificate, key crypto.Signer, chain []*x509.Certificate) ([]byte, error)

Sign makes a detached SignedData over content, signed by cert with key and carrying cert and chain: what an S/MIME multipart/signed message's application/pkcs7-signature part holds.

func ToDER added in v0.1.17

func ToDER(b []byte) ([]byte, error)

ToDER rewrites BER as DER where Go's encoding/asn1 needs it: indefinite lengths become definite, and a constructed OCTET STRING (a stream of segments) becomes one primitive OCTET STRING.

BouncyCastle, and so most Java AS2 gateways, write CMS with indefinite lengths ("streaming" BER), and encoding/asn1 reads DER only. The rewrite changes the encoding and not a value, so what decrypts or decompresses afterwards is what the sender put in. It is not used on anything a signature is checked over: signed attributes are DER already, and changing their bytes would break the signature.

Types

type KeyPair

type KeyPair struct {
	Certificate *x509.Certificate
	Key         crypto.PrivateKey
}

KeyPair is a certificate and its private key.

type Signer

type Signer struct {
	Certificate *x509.Certificate
	SigningTime time.Time
}

Signer is who signed a message, after its signature checked out.

func Verify

func Verify(sig, content []byte, roots *x509.CertPool, at time.Time) (*Signer, error)

Verify checks a detached SignedData over content: the content digest, the signature over the signed attributes, and the signer's certificate against roots for the given time (Direct's trust anchors; intermediates come from the message). It returns the signer. A certificate with no path to a root is an error: Direct trust is anchored, never "any signature".

Jump to

Keyboard shortcuts

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