1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5*b077aed3SPierre ProncheryPKCS7_sign_ex, PKCS7_sign 6*b077aed3SPierre Pronchery- create a PKCS#7 signedData structure 7e71b7053SJung-uk Kim 8e71b7053SJung-uk Kim=head1 SYNOPSIS 9e71b7053SJung-uk Kim 10e71b7053SJung-uk Kim #include <openssl/pkcs7.h> 11e71b7053SJung-uk Kim 12*b077aed3SPierre Pronchery PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 13*b077aed3SPierre Pronchery BIO *data, int flags, OSSL_LIB_CTX *libctx, 14*b077aed3SPierre Pronchery const char *propq); 15e71b7053SJung-uk Kim PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 16e71b7053SJung-uk Kim BIO *data, int flags); 17e71b7053SJung-uk Kim 18e71b7053SJung-uk Kim=head1 DESCRIPTION 19e71b7053SJung-uk Kim 20*b077aed3SPierre ProncheryPKCS7_sign_ex() creates and returns a PKCS#7 signedData structure. 21cfc39718SJung-uk KimI<signcert> is the certificate to sign with, I<pkey> is the corresponding 22cfc39718SJung-uk Kimprivate key. I<certs> is an optional set of extra certificates to include 23cfc39718SJung-uk Kimin the PKCS#7 structure (for example any intermediate CAs in the chain). 24*b077aed3SPierre ProncheryThe library context I<libctx> and property query I<propq> are used when 25*b077aed3SPierre Proncheryretrieving algorithms from providers. 26e71b7053SJung-uk Kim 27cfc39718SJung-uk KimThe data to be signed is read from BIO I<data>. 28e71b7053SJung-uk Kim 29cfc39718SJung-uk KimI<flags> is an optional set of flags. 30e71b7053SJung-uk Kim 31cfc39718SJung-uk KimAny of the following flags (ored together) can be passed in the I<flags> 32*b077aed3SPierre Proncheryparameter. 33e71b7053SJung-uk Kim 34e71b7053SJung-uk KimMany S/MIME clients expect the signed content to include valid MIME headers. If 35cfc39718SJung-uk Kimthe B<PKCS7_TEXT> flag is set MIME headers for type C<text/plain> are prepended 36e71b7053SJung-uk Kimto the data. 37e71b7053SJung-uk Kim 38cfc39718SJung-uk KimIf B<PKCS7_NOCERTS> is set the signer's certificate and the extra I<certs> 39cfc39718SJung-uk Kimwill not be included in the PKCS7 structure. 40cfc39718SJung-uk KimThe signer's certificate must still be supplied in the I<signcert> parameter 41cfc39718SJung-uk Kimthough. This can reduce the size of the signatures if the signer's certificates 42cfc39718SJung-uk Kimcan be obtained by other means: for example a previously signed message. 43e71b7053SJung-uk Kim 44e71b7053SJung-uk KimThe data being signed is included in the PKCS7 structure, unless 45e71b7053SJung-uk KimB<PKCS7_DETACHED> is set in which case it is omitted. This is used for PKCS7 46e71b7053SJung-uk Kimdetached signatures which are used in S/MIME plaintext signed messages for 47e71b7053SJung-uk Kimexample. 48e71b7053SJung-uk Kim 49e71b7053SJung-uk KimNormally the supplied content is translated into MIME canonical format (as 50e71b7053SJung-uk Kimrequired by the S/MIME specifications) if B<PKCS7_BINARY> is set no translation 51e71b7053SJung-uk Kimoccurs. This option should be used if the supplied data is in binary format 52e71b7053SJung-uk Kimotherwise the translation will corrupt it. 53e71b7053SJung-uk Kim 54e71b7053SJung-uk KimThe signedData structure includes several PKCS#7 authenticatedAttributes 55e71b7053SJung-uk Kimincluding the signing time, the PKCS#7 content type and the supported list of 56e71b7053SJung-uk Kimciphers in an SMIMECapabilities attribute. If B<PKCS7_NOATTR> is set then no 57e71b7053SJung-uk KimauthenticatedAttributes will be used. If B<PKCS7_NOSMIMECAP> is set then just 58e71b7053SJung-uk Kimthe SMIMECapabilities are omitted. 59e71b7053SJung-uk Kim 60e71b7053SJung-uk KimIf present the SMIMECapabilities attribute indicates support for the following 61e71b7053SJung-uk Kimalgorithms: triple DES, 128 bit RC2, 64 bit RC2, DES and 40 bit RC2. If any of 62e71b7053SJung-uk Kimthese algorithms is disabled then it will not be included. 63e71b7053SJung-uk Kim 64e71b7053SJung-uk KimIf the flags B<PKCS7_STREAM> is set then the returned B<PKCS7> structure is 65e71b7053SJung-uk Kimjust initialized ready to perform the signing operation. The signing is however 66cfc39718SJung-uk KimB<not> performed and the data to be signed is not read from the I<data> 67e71b7053SJung-uk Kimparameter. Signing is deferred until after the data has been written. In this 68e71b7053SJung-uk Kimway data can be signed in a single pass. 69e71b7053SJung-uk Kim 70e71b7053SJung-uk KimIf the B<PKCS7_PARTIAL> flag is set a partial B<PKCS7> structure is output to 71e71b7053SJung-uk Kimwhich additional signers and capabilities can be added before finalization. 72e71b7053SJung-uk Kim 73e71b7053SJung-uk KimIf the flag B<PKCS7_STREAM> is set the returned B<PKCS7> structure is B<not> 74e71b7053SJung-uk Kimcomplete and outputting its contents via a function that does not properly 75e71b7053SJung-uk Kimfinalize the B<PKCS7> structure will give unpredictable results. 76e71b7053SJung-uk Kim 77e71b7053SJung-uk KimSeveral functions including SMIME_write_PKCS7(), i2d_PKCS7_bio_stream(), 78e71b7053SJung-uk KimPEM_write_bio_PKCS7_stream() finalize the structure. Alternatively finalization 79e71b7053SJung-uk Kimcan be performed by obtaining the streaming ASN1 B<BIO> directly using 80e71b7053SJung-uk KimBIO_new_PKCS7(). 81e71b7053SJung-uk Kim 82e71b7053SJung-uk KimIf a signer is specified it will use the default digest for the signing 83e71b7053SJung-uk Kimalgorithm. This is B<SHA1> for both RSA and DSA keys. 84e71b7053SJung-uk Kim 85cfc39718SJung-uk KimThe I<certs>, I<signcert> and I<pkey> parameters can all be 86cfc39718SJung-uk KimNULL if the B<PKCS7_PARTIAL> flag is set. One or more signers can be added 87e71b7053SJung-uk Kimusing the function PKCS7_sign_add_signer(). PKCS7_final() must also be 88e71b7053SJung-uk Kimcalled to finalize the structure if streaming is not enabled. Alternative 89e71b7053SJung-uk Kimsigning digests can also be specified using this method. 90e71b7053SJung-uk Kim 91cfc39718SJung-uk KimIf I<signcert> and I<pkey> are NULL then a certificates only 92e71b7053SJung-uk KimPKCS#7 structure is output. 93e71b7053SJung-uk Kim 94cfc39718SJung-uk KimIn versions of OpenSSL before 1.0.0 the I<signcert> and I<pkey> parameters must 95cfc39718SJung-uk Kimnot be NULL. 96e71b7053SJung-uk Kim 97*b077aed3SPierre ProncheryPKCS7_sign() is like PKCS7_sign_ex() except that it uses default values of 98*b077aed3SPierre ProncheryNULL for the library context I<libctx> and the property query I<propq>. 99*b077aed3SPierre ProncheryThis is retained for API backward compatibility. 100*b077aed3SPierre Pronchery 101e71b7053SJung-uk Kim=head1 BUGS 102e71b7053SJung-uk Kim 103e71b7053SJung-uk KimSome advanced attributes such as counter signatures are not supported. 104e71b7053SJung-uk Kim 105e71b7053SJung-uk Kim=head1 RETURN VALUES 106e71b7053SJung-uk Kim 107*b077aed3SPierre ProncheryPKCS7_sign_ex() and PKCS7_sign() return either a valid PKCS7 structure 108*b077aed3SPierre Proncheryor NULL if an error occurred. The error can be obtained from ERR_get_error(3). 109e71b7053SJung-uk Kim 110e71b7053SJung-uk Kim=head1 SEE ALSO 111e71b7053SJung-uk Kim 112e71b7053SJung-uk KimL<ERR_get_error(3)>, L<PKCS7_verify(3)> 113e71b7053SJung-uk Kim 114e71b7053SJung-uk Kim=head1 HISTORY 115e71b7053SJung-uk Kim 116*b077aed3SPierre ProncheryThe function PKCS7_sign_ex() was added in OpenSSL 3.0. 117*b077aed3SPierre Pronchery 118cfc39718SJung-uk KimThe B<PKCS7_PARTIAL> flag, and the ability for I<certs>, I<signcert>, 119cfc39718SJung-uk Kimand I<pkey> parameters to be NULL were added in OpenSSL 1.0.0. 120e71b7053SJung-uk Kim 1216935a639SJung-uk KimThe B<PKCS7_STREAM> flag was added in OpenSSL 1.0.0. 122e71b7053SJung-uk Kim 123e71b7053SJung-uk Kim=head1 COPYRIGHT 124e71b7053SJung-uk Kim 125*b077aed3SPierre ProncheryCopyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved. 126e71b7053SJung-uk Kim 127*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 128e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 129e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 130e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 131e71b7053SJung-uk Kim 132e71b7053SJung-uk Kim=cut 133