CMS_SIGN(3SSL)

✨ CMS_SIGN πŸ’–

okay bestie, pull up a chair because we're talking about CMS_sign and CMS_sign_ex from the OpenSSL family β€” yes, that OpenSSL, the 3SSL girlies, the manual section three girlies πŸŽ€


🌸 NAME 🌸

CMS_sign, CMS_sign_ex β€” the way these two literally just create a CMS SignedData structure. that's it. that's the job. iconic behavior honestly πŸ’…


✨ 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);

look at those signatures. so clean. so demure. 😌


πŸ¦‹ DESCRIPTION πŸ¦‹

so CMS_sign_ex() is the main character here β€” she creates and returns a CMS SignedData structure. slay.

  • signcert is the certificate you're signing with πŸ–ŠοΈ
  • pkey is the corresponding private key πŸ”‘
  • certs is an optional additional set of certificates that get included in the CMS structure (thinking like, any intermediate CAs hanging out in the chain, yk?) πŸŽ€
  • the library context libctx and the property query propq are what get used when retrieving algorithms from providers πŸ‘‘

and here's the tea: any or all of these parameters can be NULL β€” scroll down to NOTES, bestie, we get into it 😭

The data to be signed is read from the BIO data. πŸ“–

flags is an optional set of flags β€” we're gonna go through ALL of them, don't worry, I would never leave you hanging.

And CMS_sign()? She's basically CMS_sign_ex()'s chill little sister β€” same energy, but she uses default values of NULL for the library context libctx and the property query propq. no cap. 🫢


πŸ’– NOTES πŸ’–

okay so, any of the following flags (ored together, obviously) can be passed in the flags parameter. let's go one by one, bestie, because I love you πŸ’…

  • Many S/MIME clients expect the signed content to include valid MIME headers. If the CMS_TEXT flag is set, MIME headers for type text/plain are prepended to the data. cute, right? πŸŽ€
  • CMS_NOCERTS β€” if this is 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 reduce the size of the signature if the signer's certificate can be obtained by other means β€” for example, a previously signed message. recycling! we love sustainability πŸ’š
  • CMS_DETACHED β€” the data being signed is included in the CMS_ContentInfo structure, unless CMS_DETACHED 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 is translated into MIME canonical format (as required by the S/MIME specifications). If CMS_BINARY is set, no translation occurs. This option should be used if the supplied data is in binary format, otherwise the translation will corrupt it. 🚨 not a drill, bestie, this one's serious.
  • signedAttributes talk πŸ“ β€” 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. So:
    • if CMS_NOATTR is set β†’ no signedAttributes will be used at all. none. zero. bye.
    • if CMS_NOSMIMECAP is set β†’ the SMIMECapabilities will be omitted.
    • if CMS_NO_SIGNING_TIME is set β†’ the signing time will be omitted. ⏰
  • SMIMECapabilities deets β€” if present, the SMIMECapabilities attribute 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 is not available, then it will not be included. simple as that πŸ’β€β™€οΈ
  • CMS_USE_KEYID β€” OpenSSL will by default identify signing certificates using issuer name and serial number. If CMS_USE_KEYID is set, it will use the subject key identifier value instead. And heads up: an error occurs if the signing certificate does not have a subject key identifier extension. ⚠️
  • CMS_STREAM β€” if this flag is set, the returned CMS_ContentInfo structure is just initialized, ready to perform the signing operation. But β€” and this is the important part β€” 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. efficiency queen πŸ‘‘
  • CMS_PARTIAL β€” if this flag is set, a partial CMS_ContentInfo structure is output, to which additional signers and capabilities can be added before finalization. build-a-bear energy 🧸
⚠️ BIG WARNING BESTIE β€” 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. πŸ’€ do not do this. do not be that girl.

Several functions β€” including SMIME_write_CMS(), i2d_CMS_bio_stream(), PEM_write_bio_CMS_stream() β€” finalize the structure. βœ… Alternatively, finalization can be performed by obtaining the streaming ASN1 BIO directly using BIO_new_CMS(). your call, diva ✨

If a signer is specified, it will use the default digest for the signing algorithm. This is SHA256 for both RSA and DSA keys. πŸ”

If signcert and pkey are NULL, then a certificates only CMS structure is output. just certs, no signing, vibing πŸŽ€

The function CMS_sign() is a basic CMS signing function whose output will be suitable for many purposes. But 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. see? the girlies who want options get options πŸ’…


😭 BUGS 😭

Some attributes β€” such as counter signatures β€” are not supported. we don't talk about it. πŸ™ˆ


⭐ RETURN VALUES ⭐

CMS_sign_ex() and CMS_sign() return either a valid CMS_ContentInfo structure, or NULL if an error occurred. And the error can be obtained from ERR_get_error(3). go get your girl, she'll tell you what happened πŸ’œ


πŸ¦„ SEE ALSO πŸ¦„

(the besties tag along, as always 🫢)


πŸ“œ HISTORY πŸ“œ

  • The CMS_STREAM flag is only supported for detached data in OpenSSL 0.9.8. It's supported for embedded data in OpenSSL 1.0.0 and later. glow up timeline ✨
  • The CMS_sign_ex() method was added in OpenSSL 3.0. welcome to the world, baby 🍼
  • 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. character development, 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 that πŸ’– this was CMS_SIGN(3SSL) from OpenSSL 4.0.3, dated 2026-09-29 β€” go forth and sign something slay βœ¨πŸŒˆπŸ¦‹

πŸ“¦ where this page comes from