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