CMS_SIGN(3ossl)

✨ CMS_SIGN β€” create a CMS SignedData structure ✨

okay bestie, pull up a chair, we're doing CMS signing today and honestly? it's giving ✨cryptographic main character energy✨. this is CMS_sign and CMS_sign_ex from the OpenSSL fam, living in <openssl/cms.h>. πŸ’–


🌸 SYNOPSIS 🌸

#include <openssl/cms.h>

CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
                             STACK_OF(X509) *certs, BIO *data,
                             unsigned int flags, OSSL_LIB_CTX *ctx,
                             const char *propq);
CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
                          BIO *data, unsigned int flags);

πŸ’… DESCRIPTION πŸ’…

so CMS_sign_ex() creates and returns a CMS SignedData structure. literally builds it and hands it to you. 🫢

  • signcert β€” the certificate you're signing with, the moment. πŸ‘‘
  • pkey β€” the matching private key, its ride-or-die.
  • certs β€” an optional extra set of certificates to tuck into the CMS structure (think any intermediate CAs chillin' in the chain). πŸŽ€
  • libctx (the library context) and propq (the property query) β€” these get used when pulling algorithms from providers.
  • data β€” the actual data to be signed is read from this BIO.
  • flags β€” an optional lil' set of flags.

and here's the tea: any or all of these parameters can be NULL, see NOTES below for the deets. πŸ™ˆ

CMS_sign() is basically CMS_sign_ex()'s twin but it uses default values of NULL for the library context libctx and the property query propq. same vibe, fewer knobs. πŸ’œ


✨ NOTES ✨

any of these flags (ored together, no cap) can be passed in the flags parameter:

  • CMS_TEXT β€” many S/MIME clients expect the signed content to include valid MIME headers, so if this flag is set, MIME headers for type text/plain get prepended to the data. πŸŽ€
  • CMS_NOCERTS β€” if set, the signer's certificate will not be included in the CMS_ContentInfo structure. but plot twist: the signer's certificate must still be supplied in the signcert parameter though. 😭 this can shrink the size of the signature if the signer's cert can be obtained some other way β€” for example, from a previously signed message.
  • CMS_DETACHED β€” the data being signed is normally included in the CMS_ContentInfo structure, unless this is set, in which case it is omitted. this is used for CMS_ContentInfo detached signatures, which are used in S/MIME plaintext signed messages for example.
  • CMS_BINARY β€” normally the supplied content gets translated into MIME canonical format (as required by the S/MIME specifications). if CMS_BINARY is set, no translation occurs. use this option if the supplied data is in binary format, otherwise the translation will corrupt it. πŸ’€ for real, don't skip this one.
  • CMS_NOATTR β€” the SignedData structure includes several CMS signedAttributes including the signing time, the CMS content type, and the supported list of ciphers in an SMIMECapabilities attribute. if CMS_NOATTR is set then no signedAttributes will be used at all.
  • CMS_NOSMIMECAP β€” if set, the SMIMECapabilities will be omitted.
  • CMS_NO_SIGNING_TIME β€” if set, the signing time will be omitted.

okay so when the SMIMECapabilities attribute is present, it indicates support for the following algorithms in preference order: 256 bit AES, Gost R3411-94, Gost 28147-89, 192 bit AES, 128 bit AES, triple DES, 128 bit RC2, 64 bit RC2, DES and 40 bit RC2. ⭐ and if any of these algorithms isn't available, it just won't be included β€” for example the GOST algorithms will not be included if the GOST ENGINE is not loaded. slay, adaptive.

  • CMS_USE_KEYID β€” OpenSSL will by default identify signing certificates using issuer name and serial number. if this flag is set, it'll use the subject key identifier value instead. ⚠️ an error occurs if the signing certificate does not have a subject key identifier extension. so, vibe check your cert first.
  • CMS_STREAM β€” if this flag is set, the returned CMS_ContentInfo structure is just initialized, ready to perform the signing operation. the signing is however not performed and the data to be signed is not read from the data parameter. signing is deferred until after the data has been written. in this way, data can be signed in a single pass. πŸ”₯
  • CMS_PARTIAL β€” if set, a partial CMS_ContentInfo structure is output, to which additional signers and capabilities can be added before finalization. 🌈
⚠️ okay important: if the flag CMS_STREAM is set, the returned CMS_ContentInfo structure is not complete, and outputting its contents via a function that does not properly finalize the CMS_ContentInfo structure will give unpredictable results. not the good kind of unpredictable. 😭

several functions do finalize the structure, including SMIME_write_CMS(), i2d_CMS_bio_stream(), and PEM_write_bio_CMS_stream(). alternatively, finalization can be performed by obtaining the streaming ASN1 BIO directly using BIO_new_CMS(). πŸ¦‹

if a signer is specified, it will use the default digest for the signing algorithm. this is SHA256 for both RSA and DSA keys. periodt. πŸ’…

if signcert and pkey are NULL, then a certificates-only CMS structure is output. 🀷

and finally β€” the function CMS_sign() is a basic CMS signing function whose output will be suitable for many purposes. for finer control of the output format, the certs, signcert and pkey parameters can all be NULL and the CMS_PARTIAL flag set. then one or more signers can be added using the function CMS_add1_signer(), non-default digests can be used, and custom attributes added. CMS_final() must then be called to finalize the structure if streaming is not enabled. πŸ’–


πŸ’€ BUGS πŸ’€

some attributes such as counter signatures are not supported. that's it, that's the bug list. πŸ™ˆ


🫢 RETURN VALUES 🫢

CMS_sign_ex() and CMS_sign() return either a valid CMS_ContentInfo structure, or NULL if an error occurred. 😭 the error can be obtained from ERR_get_error(3).


🌈 SEE ALSO 🌈

ERR_get_error(3), CMS_verify(3)


🧚 HISTORY 🧚

  • the CMS_STREAM flag is only supported for detached data in OpenSSL 0.9.8, it is supported for embedded data in OpenSSL 1.0.0 and later.
  • the CMS_sign_ex() method was added in OpenSSL 3.0.
  • since OpenSSL 3.2, CMS_sign_ex() and CMS_sign() ignore any duplicate certificates in their certs argument and no longer throw an error for them. iconic behaviour honestly. πŸ‘‘

Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


okay that's the whole page bestie, you ate it up πŸ’…πŸ’– this was OpenSSL 3.5.9, dated 2026-09-29, and it's the CMS_SIGN(3ossl) page β€” now go forth and sign some data, no cap. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from