1*569a59c8Sschwarze.\" $OpenBSD: CMS_decrypt.3,v 1.8 2019/11/02 15:39:46 schwarze Exp $ 254c85986Sschwarze.\" full merge up to: OpenSSL e9b77246 Jan 20 19:58:49 2017 +0100 354c85986Sschwarze.\" 444c89e2fSschwarze.\" This file is a derived work. 544c89e2fSschwarze.\" The changes are covered by the following Copyright and license: 644c89e2fSschwarze.\" 744c89e2fSschwarze.\" Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org> 844c89e2fSschwarze.\" 944c89e2fSschwarze.\" Permission to use, copy, modify, and distribute this software for any 1044c89e2fSschwarze.\" purpose with or without fee is hereby granted, provided that the above 1144c89e2fSschwarze.\" copyright notice and this permission notice appear in all copies. 1244c89e2fSschwarze.\" 1344c89e2fSschwarze.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1444c89e2fSschwarze.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1544c89e2fSschwarze.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1644c89e2fSschwarze.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1744c89e2fSschwarze.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1844c89e2fSschwarze.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1944c89e2fSschwarze.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2044c89e2fSschwarze.\" 2144c89e2fSschwarze.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>. 2254c85986Sschwarze.\" Copyright (c) 2008, 2014 The OpenSSL Project. All rights reserved. 2354c85986Sschwarze.\" 2454c85986Sschwarze.\" Redistribution and use in source and binary forms, with or without 2554c85986Sschwarze.\" modification, are permitted provided that the following conditions 2654c85986Sschwarze.\" are met: 2754c85986Sschwarze.\" 2854c85986Sschwarze.\" 1. Redistributions of source code must retain the above copyright 2954c85986Sschwarze.\" notice, this list of conditions and the following disclaimer. 3054c85986Sschwarze.\" 3154c85986Sschwarze.\" 2. Redistributions in binary form must reproduce the above copyright 3254c85986Sschwarze.\" notice, this list of conditions and the following disclaimer in 3354c85986Sschwarze.\" the documentation and/or other materials provided with the 3454c85986Sschwarze.\" distribution. 3554c85986Sschwarze.\" 3654c85986Sschwarze.\" 3. All advertising materials mentioning features or use of this 3754c85986Sschwarze.\" software must display the following acknowledgment: 3854c85986Sschwarze.\" "This product includes software developed by the OpenSSL Project 3954c85986Sschwarze.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 4054c85986Sschwarze.\" 4154c85986Sschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 4254c85986Sschwarze.\" endorse or promote products derived from this software without 4354c85986Sschwarze.\" prior written permission. For written permission, please contact 4454c85986Sschwarze.\" openssl-core@openssl.org. 4554c85986Sschwarze.\" 4654c85986Sschwarze.\" 5. Products derived from this software may not be called "OpenSSL" 4754c85986Sschwarze.\" nor may "OpenSSL" appear in their names without prior written 4854c85986Sschwarze.\" permission of the OpenSSL Project. 4954c85986Sschwarze.\" 5054c85986Sschwarze.\" 6. Redistributions of any form whatsoever must retain the following 5154c85986Sschwarze.\" acknowledgment: 5254c85986Sschwarze.\" "This product includes software developed by the OpenSSL Project 5354c85986Sschwarze.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 5454c85986Sschwarze.\" 5554c85986Sschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 5654c85986Sschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5754c85986Sschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5854c85986Sschwarze.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 5954c85986Sschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 6054c85986Sschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 6154c85986Sschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 6254c85986Sschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6354c85986Sschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 6454c85986Sschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 6554c85986Sschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 6654c85986Sschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE. 6754c85986Sschwarze.\" 68*569a59c8Sschwarze.Dd $Mdocdate: November 2 2019 $ 6954c85986Sschwarze.Dt CMS_DECRYPT 3 7054c85986Sschwarze.Os 7154c85986Sschwarze.Sh NAME 7244c89e2fSschwarze.Nm CMS_decrypt , 7344c89e2fSschwarze.Nm CMS_decrypt_set1_pkey , 7444c89e2fSschwarze.Nm CMS_decrypt_set1_key 7568308cb2Sschwarze.Nd decrypt content from a CMS EnvelopedData structure 7654c85986Sschwarze.Sh SYNOPSIS 7754c85986Sschwarze.In openssl/cms.h 7854c85986Sschwarze.Ft int 7954c85986Sschwarze.Fo CMS_decrypt 8054c85986Sschwarze.Fa "CMS_ContentInfo *cms" 8144c89e2fSschwarze.Fa "EVP_PKEY *private_key" 8244c89e2fSschwarze.Fa "X509 *certificate" 8354c85986Sschwarze.Fa "BIO *dcont" 8454c85986Sschwarze.Fa "BIO *out" 8554c85986Sschwarze.Fa "unsigned int flags" 8654c85986Sschwarze.Fc 8744c89e2fSschwarze.Ft int 8844c89e2fSschwarze.Fo CMS_decrypt_set1_pkey 8944c89e2fSschwarze.Fa "CMS_ContentInfo *cms" 9044c89e2fSschwarze.Fa "EVP_PKEY *private_key" 9144c89e2fSschwarze.Fa "X509 *certificate" 9244c89e2fSschwarze.Fc 9344c89e2fSschwarze.Ft int 9444c89e2fSschwarze.Fo CMS_decrypt_set1_key 9544c89e2fSschwarze.Fa "CMS_ContentInfo *cms" 9644c89e2fSschwarze.Fa "unsigned char *symmetric_key" 9744c89e2fSschwarze.Fa "size_t keylen" 9844c89e2fSschwarze.Fa "const unsigned char *id" 9944c89e2fSschwarze.Fa "size_t idlen" 10044c89e2fSschwarze.Fc 10154c85986Sschwarze.Sh DESCRIPTION 10254c85986Sschwarze.Fn CMS_decrypt 10368308cb2Sschwarzeextracts and decrypts the content from the CMS 10468308cb2Sschwarze.Vt EnvelopedData 10568308cb2Sschwarzestructure 10668308cb2Sschwarze.Fa cms 10744c89e2fSschwarzeusing the 10844c89e2fSschwarze.Fa private_key 10944c89e2fSschwarzeand the 11044c89e2fSschwarze.Fa certificate 11144c89e2fSschwarzeof the recipient. 11244c89e2fSschwarzeIt writes the decrypted content to 11344c89e2fSschwarze.Fa out . 11454c85986Sschwarze.Pp 11568308cb2SschwarzeIn the rare case where the compressed content is detached, pass it in via 11668308cb2Sschwarze.Fa dcont . 11768308cb2SschwarzeFor normal use, set 11854c85986Sschwarze.Fa dcont 11968308cb2Sschwarzeto 12054c85986Sschwarze.Dv NULL . 12154c85986Sschwarze.Pp 12244c89e2fSschwarzeAlthough the recipient's 12344c89e2fSschwarze.Fa certificate 12444c89e2fSschwarzeis not needed to decrypt the data, it is needed to locate the 12568308cb2Sschwarzeappropriate (of possibly several) recipients in the CMS structure. 12654c85986Sschwarze.Pp 12744c89e2fSschwarzeIf the 12844c89e2fSschwarze.Fa certificate 12954c85986Sschwarzeis set to 13054c85986Sschwarze.Dv NULL , 13154c85986Sschwarzeall possible recipients are tried. 13254c85986SschwarzeThis case however is problematic. 13354c85986SschwarzeTo thwart the MMA attack (Bleichenbacher's attack on PKCS #1 v1.5 RSA 13468308cb2Sschwarzepadding), all recipients are tried whether they succeed or not. 13568308cb2SschwarzeIf no recipient succeeds, a random symmetric key is used to decrypt 13654c85986Sschwarzethe content: this will typically output garbage and may (but is not 13754c85986Sschwarzeguaranteed to) ultimately return a padding error only. 13854c85986SschwarzeIf 13954c85986Sschwarze.Fn CMS_decrypt 14054c85986Sschwarzejust returned an error when all recipient encrypted keys failed to 14168308cb2Sschwarzedecrypt, an attacker could use this in a timing attack. 14254c85986SschwarzeIf the special flag 14354c85986Sschwarze.Dv CMS_DEBUG_DECRYPT 14468308cb2Sschwarzeis set, the above behaviour is modified and an error 14554c85986Sschwarze.Em is 14654c85986Sschwarzereturned if no recipient encrypted key can be decrypted 14754c85986Sschwarze.Em without 14854c85986Sschwarzegenerating a random content encryption key. 14954c85986SschwarzeApplications should use this flag with extreme caution 15054c85986Sschwarzeespecially in automated gateways as it can leave them open to attack. 15154c85986Sschwarze.Pp 15254c85986SschwarzeIt is possible to determine the correct recipient key by other means 15368308cb2Sschwarze(for example by looking them up in a database) and setting them in the 15444c89e2fSschwarze.Fa cms 15554c85986Sschwarzestructure in advance using the CMS utility functions such as 15644c89e2fSschwarze.Fn CMS_decrypt_set1_pkey . 15754c85986SschwarzeIn this case both 15844c89e2fSschwarze.Fa certificate 15954c85986Sschwarzeand 16044c89e2fSschwarze.Fa private_key 16154c85986Sschwarzeshould be set to 16244c89e2fSschwarze.Dv NULL 16344c89e2fSschwarzewhen calling 16444c89e2fSschwarze.Fn CMS_decrypt 16544c89e2fSschwarzelater on. 16654c85986Sschwarze.Pp 16768308cb2SschwarzeTo process 16868308cb2Sschwarze.Vt KEKRecipientInfo 16968308cb2Sschwarzetypes, 17044c89e2fSschwarze.Fn CMS_decrypt_set1_key 17154c85986Sschwarzeor 17254c85986Sschwarze.Xr CMS_RecipientInfo_set0_key 3 17354c85986Sschwarzeand 17454c85986Sschwarze.Xr CMS_RecipientInfo_decrypt 3 17554c85986Sschwarzeshould be called before 17654c85986Sschwarze.Fn CMS_decrypt 17754c85986Sschwarzeand 17844c89e2fSschwarze.Fa certificate 17954c85986Sschwarzeand 18044c89e2fSschwarze.Fa private_key 18154c85986Sschwarzeset to 18244c89e2fSschwarze.Dv NULL 18344c89e2fSschwarzewhen calling 18444c89e2fSschwarze.Fn CMS_decrypt 18544c89e2fSschwarzelater on. 18654c85986Sschwarze.Pp 18754c85986SschwarzeIf the 18854c85986Sschwarze.Dv CMS_TEXT 18944c89e2fSschwarzebit is set in 19044c89e2fSschwarze.Fa flags , 19144c89e2fSschwarzeMIME headers for type text/plain are deleted from the content. 19244c89e2fSschwarzeIf the content is not of type text/plain, an error occurs. 19354c85986Sschwarze.Sh RETURN VALUES 19444c89e2fSschwarze.Fn CMS_decrypt , 19544c89e2fSschwarze.Fn CMS_decrypt_set1_pkey , 19644c89e2fSschwarzeand 19744c89e2fSschwarze.Fn CMS_decrypt_set1_key 19844c89e2fSschwarzereturn 1 for success or 0 for failure. 19954c85986SschwarzeThe error can be obtained from 20054c85986Sschwarze.Xr ERR_get_error 3 . 20154c85986Sschwarze.Sh SEE ALSO 20285bbda65Sschwarze.Xr CMS_ContentInfo_new 3 , 20385bbda65Sschwarze.Xr CMS_encrypt 3 , 20485bbda65Sschwarze.Xr CMS_get0_RecipientInfos 3 20568308cb2Sschwarze.Sh STANDARDS 20668308cb2SschwarzeRFC 5652: Cryptographic Message Syntax (CMS) 20768308cb2Sschwarze.Bl -dash -compact -offset indent 20868308cb2Sschwarze.It 20968308cb2Sschwarzesection 6.1: EnvelopedData Type 21068308cb2Sschwarze.It 21168308cb2Sschwarzesection 6.2.3: KEKRecipientInfo Type 21268308cb2Sschwarze.El 21354c85986Sschwarze.Sh HISTORY 21444c89e2fSschwarze.Fn CMS_decrypt , 21544c89e2fSschwarze.Fn CMS_decrypt_set1_pkey , 21644c89e2fSschwarzeand 21744c89e2fSschwarze.Fn CMS_decrypt_set1_key 21866a3cb98Sschwarzefirst appeared in OpenSSL 0.9.8h 21944c89e2fSschwarzeand have been available since 220*569a59c8Sschwarze.Ox 6.7 . 22154c85986Sschwarze.Sh BUGS 22254c85986SschwarzeThe lack of single pass processing and the need to hold all data in 22354c85986Sschwarzememory as mentioned in 22454c85986Sschwarze.Xr CMS_verify 3 22554c85986Sschwarzealso applies to 22654c85986Sschwarze.Fn CMS_decrypt . 227