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

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

<h1>CMS_encrypt(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="#NOTES">NOTES</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_encrypt - create a CMS envelopedData structure
</PRE>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/cms.h&gt;
</PRE>
<PRE> CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, unsigned int flags);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>CMS_encrypt()</CODE> creates and returns a CMS EnvelopedData
structure. <STRONG>certs</STRONG>
is a list of recipient certificates. <STRONG>in</STRONG> is the content to be encrypted.
<STRONG>cipher</STRONG> is the symmetric cipher to use. <STRONG>flags</STRONG> is an optional set of flags.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
Only certificates carrying RSA keys are supported so the recipient
certificates supplied to this function must all contain RSA public keys,
though they do not have to be signed using the RSA algorithm.

</P>
<P>
<CODE>EVP_des_ede3_cbc()</CODE> (triple DES) is the algorithm of choice for
S/MIME use because most clients will support it.

</P>
<P>
The algorithm passed in the <STRONG>cipher</STRONG> parameter must support ASN1 encoding of its parameters. 

</P>
<P>
Many browsers implement a ``sign and encrypt'' option which is simply an
S/MIME envelopedData containing an S/MIME signed message. This can be
readily produced by storing the S/MIME signed message in a memory BIO and
passing it to <CODE>CMS_encrypt().</CODE>

</P>
<P>
The following flags can be passed in the <STRONG>flags</STRONG> parameter.

</P>
<P>
If the <STRONG>CMS_TEXT</STRONG> flag is set MIME headers for type <STRONG>text/plain</STRONG> are prepended to the data.

</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. If <STRONG>CMS_BINARY</STRONG> is set then
<STRONG>CMS_TEXT</STRONG> is ignored.

</P>
<P>
OpenSSL will by default identify recipient 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 all recipient certificates do not have a subject key identifier
extension.

</P>
<P>
If the <STRONG>CMS_STREAM</STRONG> flag is set a partial <STRONG>CMS_ContentInfo</STRONG> structure is returned suitable for streaming I/O: no data is read from the
BIO <STRONG>in</STRONG>.

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

</P>
<P>
The data being encrypted is included in the CMS_ContentInfo structure,
unless
<STRONG>CMS_DETACHED</STRONG> is set in which case it is omitted. This is rarely used in practice and is
not supported by <CODE>SMIME_write_CMS().</CODE>

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<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>
The recipients specified in <STRONG>certs</STRONG> use a CMS KeyTransRecipientInfo info structure. KEKRecipientInfo is also
supported using the flag <STRONG>CMS_PARTIAL</STRONG>
and <CODE>CMS_add0_recipient_key().</CODE>

</P>
<P>
The parameter <STRONG>certs</STRONG> may be NULL if <STRONG>CMS_PARTIAL</STRONG> is set and recipients added later using
<CODE>CMS_add1_recipient_cert()</CODE> or
<CODE>CMS_add0_recipient_key().</CODE>

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>CMS_encrypt()</CODE> returns either a 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_decrypt.html#">CMS_decrypt(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>CMS_decrypt()</CODE> was added to OpenSSL 0.9.8 The <STRONG>CMS_STREAM</STRONG> flag was first supported in OpenSSL 1.0.0.

</P>
:}


