
#use wml::openssl-macros area=docs page=CMS_sign

<title>Documents, CMS_sign(3)</title>

<h1>CMS_sign(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<PRE> CMS_sign - create a CMS SignedData structure
</PRE>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/cms.h&gt;
</PRE>
<PRE> CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, BIO *data, unsigned int flags);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>CMS_sign()</CODE> creates and returns a CMS SignedData structure. <STRONG>signcert</STRONG> is the certificate to sign with, <STRONG>pkey</STRONG> is the corresponding private key.
<STRONG>certs</STRONG> is an optional additional set of certificates to include in the CMS
structure (for example any intermediate CAs in the chain). Any or all of
these parameters can be <STRONG>NULL</STRONG>, see <STRONG>NOTES</STRONG> below.

</P>
<P>
The data to be signed is read from BIO <STRONG>data</STRONG>.

</P>
<P>
<STRONG>flags</STRONG> is an optional set of flags.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
Any of the following flags (ored together) can be passed in the <STRONG>flags</STRONG>
parameter.

</P>
<P>
Many S/MIME clients expect the signed content to include valid MIME
headers. If the <STRONG>CMS_TEXT</STRONG> flag is set MIME headers for type <STRONG>text/plain</STRONG> are prepended to the data.

</P>
<P>
If <STRONG>CMS_NOCERTS</STRONG> is set the signer's certificate will not be included in the CMS_ContentInfo
structure, the signer's certificate must still be supplied in the <STRONG>signcert</STRONG> parameter though. This can reduce the size of the signature if the signers
certificate can be obtained by other means: for example a previously signed
message.

</P>
<P>
The data being signed is included in the CMS_ContentInfo structure, unless
<STRONG>CMS_DETACHED</STRONG> is set in which case it is omitted. This is used for CMS_ContentInfo
detached signatures which are used in S/MIME plaintext signed messages for
example.

</P>
<P>
Normally the supplied content is translated into MIME canonical format (as
required by the S/MIME specifications) if <STRONG>CMS_BINARY</STRONG> is set no translation occurs. This option should be used if the supplied
data is in binary format otherwise the translation will corrupt it.

</P>
<P>
The SignedData structure includes several CMS signedAttributes including
the signing time, the CMS content type and the supported list of ciphers in
an SMIMECapabilities attribute. If <STRONG>CMS_NOATTR</STRONG> is set then no signedAttributes will be used. If <STRONG>CMS_NOSMIMECAP</STRONG> is set then just the SMIMECapabilities are omitted.

</P>
<P>
If present the SMIMECapabilities attribute indicates support for the
following algorithms in preference order: 256 bit AES, Gost R3411-94, Gost
28147-89, 192 bit AES, 128 bit AES, triple DES, 128 bit RC2, 64 bit RC2,
DES and 40 bit RC2. If any of these algorithms is not available then it
will not be included: for example the GOST algorithms will not be included
if the GOST ENGINE is not loaded.

</P>
<P>
OpenSSL will by default identify signing certificates using issuer name and
serial number. If <STRONG>CMS_USE_KEYID</STRONG> is set it will use the subject key identifier value instead. An error
occurs if the signing certificate does not have a subject key identifier
extension.

</P>
<P>
If the flags <STRONG>CMS_STREAM</STRONG> is set then the returned <STRONG>CMS_ContentInfo</STRONG>
structure is just initialized ready to perform the signing operation. The
signing is however <STRONG>not</STRONG> performed and the data to be signed is not read from the <STRONG>data</STRONG> parameter. Signing is deferred until after the data has been written. In
this way data can be signed in a single pass.

</P>
<P>
If the <STRONG>CMS_PARTIAL</STRONG> flag is set a partial <STRONG>CMS_ContentInfo</STRONG> structure is output to which additional signers and capabilities can be
added before finalization.

</P>
<P>
If the flag <STRONG>CMS_STREAM</STRONG> is set the returned <STRONG>CMS_ContentInfo</STRONG> structure is
<STRONG>not</STRONG> complete and outputting its contents via a function that does not properly
finalize the <STRONG>CMS_ContentInfo</STRONG> structure will give unpredictable results.

</P>
<P>
Several functions including <CODE>SMIME_write_CMS(),</CODE>
<CODE>i2d_CMS_bio_stream(),</CODE> <CODE>PEM_write_bio_CMS_stream()</CODE>
finalize the structure. Alternatively finalization can be performed by
obtaining the streaming ASN1 <STRONG>BIO</STRONG> directly using <CODE>BIO_new_CMS().</CODE>

</P>
<P>
If a signer is specified it will use the default digest for the signing
algorithm. This is <STRONG>SHA1</STRONG> for both RSA and DSA keys.

</P>
<P>
If <STRONG>signcert</STRONG> and <STRONG>pkey</STRONG> are NULL then a certificates only CMS structure is output.

</P>
<P>
The function <CODE>CMS_sign()</CODE> is a basic CMS signing function whose
output will be suitable for many purposes. For finer control of the output
format the
<STRONG>certs</STRONG>, <STRONG>signcert</STRONG> and <STRONG>pkey</STRONG> parameters can all be <STRONG>NULL</STRONG> and the
<STRONG>CMS_PARTIAL</STRONG> flag set. Then one or more signers can be added using the function
<CODE>CMS_sign_add1_signer(),</CODE> non default digests can be used and
custom attributes added. <STRONG>CMS_final()</STRONG> must then be called to finalize the structure if streaming is not enabled. 

</P>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
Some attributes such as counter signatures are not supported.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>CMS_sign()</CODE> returns either a valid CMS_ContentInfo structure or
NULL if an error occurred. The error can be obtained from
<CODE>ERR_get_error(3).</CODE>

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>, <A HREF="../crypto/CMS_verify.html#">CMS_verify(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>CMS_sign()</CODE> was added to OpenSSL 0.9.8

</P>
<P>
The <STRONG>CMS_STREAM</STRONG> flag is only supported for detached data in OpenSSL 0.9.8, it is supported
for embedded data in OpenSSL 1.0.0 and later.

</P>
:}


