113d40330Schristos=pod 213d40330Schristos 313d40330Schristos=head1 NAME 413d40330Schristos 5b0d17251SchristosCMS_sign, CMS_sign_ex - create a CMS SignedData structure 613d40330Schristos 713d40330Schristos=head1 SYNOPSIS 813d40330Schristos 913d40330Schristos #include <openssl/cms.h> 1013d40330Schristos 11b0d17251Schristos CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey, 12b0d17251Schristos STACK_OF(X509) *certs, BIO *data, 13b0d17251Schristos unsigned int flags, OSSL_LIB_CTX *ctx, 14b0d17251Schristos const char *propq); 1513d40330Schristos CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 1613d40330Schristos BIO *data, unsigned int flags); 1713d40330Schristos 1813d40330Schristos=head1 DESCRIPTION 1913d40330Schristos 20b0d17251SchristosCMS_sign_ex() creates and returns a CMS SignedData structure. 21b0d17251SchristosI<signcert> is the certificate to sign with, I<pkey> is the corresponding 22b0d17251Schristosprivate key. I<certs> is an optional additional set of certificates to include 23b0d17251Schristosin the CMS structure (for example any intermediate CAs in the chain). The 24b0d17251Schristoslibrary context I<libctx> and the property query I<propq> are used when 25b0d17251Schristosretrieving algorithms from providers. Any or all of these parameters can be 26b0d17251SchristosB<NULL>, see B<NOTES> below. 2713d40330Schristos 2813d40330SchristosThe data to be signed is read from BIO B<data>. 2913d40330Schristos 3013d40330SchristosB<flags> is an optional set of flags. 3113d40330Schristos 32b0d17251SchristosCMS_sign() is similar to CMS_sign_ex() but uses default values of NULL 33b0d17251Schristosfor the library context I<libctx> and the property query I<propq>. 34b0d17251Schristos 3513d40330Schristos=head1 NOTES 3613d40330Schristos 3713d40330SchristosAny of the following flags (ored together) can be passed in the B<flags> 3813d40330Schristosparameter. 3913d40330Schristos 4013d40330SchristosMany S/MIME clients expect the signed content to include valid MIME headers. If 4113d40330Schristosthe B<CMS_TEXT> flag is set MIME headers for type B<text/plain> are prepended 4213d40330Schristosto the data. 4313d40330Schristos 4413d40330SchristosIf B<CMS_NOCERTS> is set the signer's certificate will not be included in the 4513d40330SchristosCMS_ContentInfo structure, the signer's certificate must still be supplied in 4613d40330Schristosthe B<signcert> parameter though. This can reduce the size of the signature if 4713d40330Schristosthe signers certificate can be obtained by other means: for example a 4813d40330Schristospreviously signed message. 4913d40330Schristos 5013d40330SchristosThe data being signed is included in the CMS_ContentInfo structure, unless 5113d40330SchristosB<CMS_DETACHED> is set in which case it is omitted. This is used for 5213d40330SchristosCMS_ContentInfo detached signatures which are used in S/MIME plaintext signed 5313d40330Schristosmessages for example. 5413d40330Schristos 5513d40330SchristosNormally the supplied content is translated into MIME canonical format (as 5613d40330Schristosrequired by the S/MIME specifications) if B<CMS_BINARY> is set no translation 5713d40330Schristosoccurs. This option should be used if the supplied data is in binary format 5813d40330Schristosotherwise the translation will corrupt it. 5913d40330Schristos 6013d40330SchristosThe SignedData structure includes several CMS signedAttributes including the 6113d40330Schristossigning time, the CMS content type and the supported list of ciphers in an 6213d40330SchristosSMIMECapabilities attribute. If B<CMS_NOATTR> is set then no signedAttributes 6313d40330Schristoswill be used. If B<CMS_NOSMIMECAP> is set then just the SMIMECapabilities are 6413d40330Schristosomitted. 6513d40330Schristos 6613d40330SchristosIf present the SMIMECapabilities attribute indicates support for the following 6713d40330Schristosalgorithms in preference order: 256 bit AES, Gost R3411-94, Gost 28147-89, 192 6813d40330Schristosbit AES, 128 bit AES, triple DES, 128 bit RC2, 64 bit RC2, DES and 40 bit RC2. 69b0d17251SchristosIf any of these algorithms is not available then it will not be included: 70b0d17251Schristosfor example the GOST algorithms will not be included if the GOST ENGINE is 7113d40330Schristosnot loaded. 7213d40330Schristos 7313d40330SchristosOpenSSL will by default identify signing certificates using issuer name 7413d40330Schristosand serial number. If B<CMS_USE_KEYID> is set it will use the subject key 7513d40330Schristosidentifier value instead. An error occurs if the signing certificate does not 7613d40330Schristoshave a subject key identifier extension. 7713d40330Schristos 7813d40330SchristosIf the flags B<CMS_STREAM> is set then the returned B<CMS_ContentInfo> 7913d40330Schristosstructure is just initialized ready to perform the signing operation. The 8013d40330Schristossigning is however B<not> performed and the data to be signed is not read from 8113d40330Schristosthe B<data> parameter. Signing is deferred until after the data has been 8213d40330Schristoswritten. In this way data can be signed in a single pass. 8313d40330Schristos 8413d40330SchristosIf the B<CMS_PARTIAL> flag is set a partial B<CMS_ContentInfo> structure is 8513d40330Schristosoutput to which additional signers and capabilities can be added before 8613d40330Schristosfinalization. 8713d40330Schristos 8813d40330SchristosIf the flag B<CMS_STREAM> is set the returned B<CMS_ContentInfo> structure is 8913d40330SchristosB<not> complete and outputting its contents via a function that does not 9013d40330Schristosproperly finalize the B<CMS_ContentInfo> structure will give unpredictable 9113d40330Schristosresults. 9213d40330Schristos 9313d40330SchristosSeveral functions including SMIME_write_CMS(), i2d_CMS_bio_stream(), 9413d40330SchristosPEM_write_bio_CMS_stream() finalize the structure. Alternatively finalization 9513d40330Schristoscan be performed by obtaining the streaming ASN1 B<BIO> directly using 9613d40330SchristosBIO_new_CMS(). 9713d40330Schristos 9813d40330SchristosIf a signer is specified it will use the default digest for the signing 9913d40330Schristosalgorithm. This is B<SHA1> for both RSA and DSA keys. 10013d40330Schristos 10113d40330SchristosIf B<signcert> and B<pkey> are NULL then a certificates only CMS structure is 10213d40330Schristosoutput. 10313d40330Schristos 10413d40330SchristosThe function CMS_sign() is a basic CMS signing function whose output will be 10513d40330Schristossuitable for many purposes. For finer control of the output format the 10613d40330SchristosB<certs>, B<signcert> and B<pkey> parameters can all be B<NULL> and the 10713d40330SchristosB<CMS_PARTIAL> flag set. Then one or more signers can be added using the 108*4778aedeSchristosfunction CMS_add1_signer(), non default digests can be used and custom 10913d40330Schristosattributes added. CMS_final() must then be called to finalize the 11013d40330Schristosstructure if streaming is not enabled. 11113d40330Schristos 11213d40330Schristos=head1 BUGS 11313d40330Schristos 11413d40330SchristosSome attributes such as counter signatures are not supported. 11513d40330Schristos 11613d40330Schristos=head1 RETURN VALUES 11713d40330Schristos 118b0d17251SchristosCMS_sign_ex() and CMS_sign() return either a valid CMS_ContentInfo 119b0d17251Schristosstructure or NULL if an error occurred. The error can be obtained from 120b0d17251SchristosERR_get_error(3). 12113d40330Schristos 12213d40330Schristos=head1 SEE ALSO 12313d40330Schristos 12413d40330SchristosL<ERR_get_error(3)>, L<CMS_verify(3)> 12513d40330Schristos 12613d40330Schristos=head1 HISTORY 12713d40330Schristos 12813d40330SchristosThe B<CMS_STREAM> flag is only supported for detached data in OpenSSL 0.9.8, 12913d40330Schristosit is supported for embedded data in OpenSSL 1.0.0 and later. 13013d40330Schristos 131b0d17251SchristosThe CMS_sign_ex() method was added in OpenSSL 3.0. 132b0d17251Schristos 13313d40330Schristos=head1 COPYRIGHT 13413d40330Schristos 135*4778aedeSchristosCopyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. 13613d40330Schristos 137b0d17251SchristosLicensed under the Apache License 2.0 (the "License"). You may not use 13813d40330Schristosthis file except in compliance with the License. You can obtain a copy 13913d40330Schristosin the file LICENSE in the source distribution or at 14013d40330SchristosL<https://www.openssl.org/source/license.html>. 14113d40330Schristos 14213d40330Schristos=cut 143