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) andpropq(the property query) β these get used when pulling algorithms from providers.dataβ the actual data to be signed is read from thisBIO.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 typetext/plainget prepended to the data. πCMS_NOCERTSβ if set, the signer's certificate will not be included in theCMS_ContentInfostructure. but plot twist: the signer's certificate must still be supplied in thesigncertparameter 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 theCMS_ContentInfostructure, unless this is set, in which case it is omitted. this is used forCMS_ContentInfodetached 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). ifCMS_BINARYis 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β theSignedDatastructure includes several CMS signedAttributes including the signing time, the CMS content type, and the supported list of ciphers in anSMIMECapabilitiesattribute. ifCMS_NOATTRis set then no signedAttributes will be used at all.CMS_NOSMIMECAPβ if set, theSMIMECapabilitieswill 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 returnedCMS_ContentInfostructure 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 thedataparameter. 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 partialCMS_ContentInfostructure is output, to which additional signers and capabilities can be added before finalization. π
β οΈ okay important: if the flagCMS_STREAMis set, the returnedCMS_ContentInfostructure is not complete, and outputting its contents via a function that does not properly finalize theCMS_ContentInfostructure 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_STREAMflag 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()andCMS_sign()ignore any duplicate certificates in theircertsargument and no longer throw an error for them. iconic behaviour honestly. π
π COPYRIGHT π
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
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms