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