1=pod 2 3=head1 NAME 4 5CMS_sign, CMS_sign_ex - create a CMS SignedData structure 6 7=head1 SYNOPSIS 8 9 #include <openssl/cms.h> 10 11 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey, 12 STACK_OF(X509) *certs, BIO *data, 13 unsigned int flags, OSSL_LIB_CTX *ctx, 14 const char *propq); 15 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 16 BIO *data, unsigned int flags); 17 18=head1 DESCRIPTION 19 20CMS_sign_ex() creates and returns a CMS SignedData structure. 21I<signcert> is the certificate to sign with, I<pkey> is the corresponding 22private key. I<certs> is an optional additional set of certificates to include 23in the CMS structure (for example any intermediate CAs in the chain). The 24library context I<libctx> and the property query I<propq> are used when 25retrieving algorithms from providers. Any or all of these parameters can be 26B<NULL>, see B<NOTES> below. 27 28The data to be signed is read from BIO B<data>. 29 30B<flags> is an optional set of flags. 31 32CMS_sign() is similar to CMS_sign_ex() but uses default values of NULL 33for the library context I<libctx> and the property query I<propq>. 34 35=head1 NOTES 36 37Any of the following flags (ored together) can be passed in the B<flags> 38parameter. 39 40Many S/MIME clients expect the signed content to include valid MIME headers. If 41the B<CMS_TEXT> flag is set MIME headers for type B<text/plain> are prepended 42to the data. 43 44If B<CMS_NOCERTS> is set the signer's certificate will not be included in the 45CMS_ContentInfo structure, the signer's certificate must still be supplied in 46the B<signcert> parameter though. This can reduce the size of the signature if 47the signers certificate can be obtained by other means: for example a 48previously signed message. 49 50The data being signed is included in the CMS_ContentInfo structure, unless 51B<CMS_DETACHED> is set in which case it is omitted. This is used for 52CMS_ContentInfo detached signatures which are used in S/MIME plaintext signed 53messages for example. 54 55Normally the supplied content is translated into MIME canonical format (as 56required by the S/MIME specifications) if B<CMS_BINARY> is set no translation 57occurs. This option should be used if the supplied data is in binary format 58otherwise the translation will corrupt it. 59 60The SignedData structure includes several CMS signedAttributes including the 61signing time, the CMS content type and the supported list of ciphers in an 62SMIMECapabilities attribute. If B<CMS_NOATTR> is set then no signedAttributes 63will be used at all. If B<CMS_NOSMIMECAP> is set then the SMIMECapabilities 64will be omitted. If B<CMS_NO_SIGNING_TIME> is set then the signing time will be 65omitted. 66 67If present the SMIMECapabilities attribute indicates support for the following 68algorithms in preference order: 256 bit AES, Gost R3411-94, Gost 28147-89, 192 69bit AES, 128 bit AES, triple DES, 128 bit RC2, 64 bit RC2, DES and 40 bit RC2. 70If any of these algorithms is not available then it will not be included: 71for example the GOST algorithms will not be included if the GOST ENGINE is 72not loaded. 73 74OpenSSL will by default identify signing certificates using issuer name 75and serial number. If B<CMS_USE_KEYID> is set it will use the subject key 76identifier value instead. An error occurs if the signing certificate does not 77have a subject key identifier extension. 78 79If the flags B<CMS_STREAM> is set then the returned B<CMS_ContentInfo> 80structure is just initialized ready to perform the signing operation. The 81signing is however B<not> performed and the data to be signed is not read from 82the B<data> parameter. Signing is deferred until after the data has been 83written. In this way data can be signed in a single pass. 84 85If the B<CMS_PARTIAL> flag is set a partial B<CMS_ContentInfo> structure is 86output to which additional signers and capabilities can be added before 87finalization. 88 89If the flag B<CMS_STREAM> is set the returned B<CMS_ContentInfo> structure is 90B<not> complete and outputting its contents via a function that does not 91properly finalize the B<CMS_ContentInfo> structure will give unpredictable 92results. 93 94Several functions including SMIME_write_CMS(), i2d_CMS_bio_stream(), 95PEM_write_bio_CMS_stream() finalize the structure. Alternatively finalization 96can be performed by obtaining the streaming ASN1 B<BIO> directly using 97BIO_new_CMS(). 98 99If a signer is specified it will use the default digest for the signing 100algorithm. This is B<SHA256> for both RSA and DSA keys. 101 102If B<signcert> and B<pkey> are NULL then a certificates only CMS structure is 103output. 104 105The function CMS_sign() is a basic CMS signing function whose output will be 106suitable for many purposes. For finer control of the output format the 107B<certs>, B<signcert> and B<pkey> parameters can all be B<NULL> and the 108B<CMS_PARTIAL> flag set. Then one or more signers can be added using the 109function CMS_add1_signer(), non default digests can be used and custom 110attributes added. CMS_final() must then be called to finalize the 111structure if streaming is not enabled. 112 113=head1 BUGS 114 115Some attributes such as counter signatures are not supported. 116 117=head1 RETURN VALUES 118 119CMS_sign_ex() and CMS_sign() return either a valid CMS_ContentInfo 120structure or NULL if an error occurred. The error can be obtained from 121ERR_get_error(3). 122 123=head1 SEE ALSO 124 125L<ERR_get_error(3)>, L<CMS_verify(3)> 126 127=head1 HISTORY 128 129The B<CMS_STREAM> flag is only supported for detached data in OpenSSL 0.9.8, 130it is supported for embedded data in OpenSSL 1.0.0 and later. 131 132The CMS_sign_ex() method was added in OpenSSL 3.0. 133 134Since OpenSSL 3.2, CMS_sign_ex() and CMS_sign() ignore any duplicate 135certificates in their I<certs> argument and no longer throw an error for them. 136 137=head1 COPYRIGHT 138 139Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved. 140 141Licensed under the Apache License 2.0 (the "License"). You may not use 142this file except in compliance with the License. You can obtain a copy 143in the file LICENSE in the source distribution or at 144L<https://www.openssl.org/source/license.html>. 145 146=cut 147