113d40330Schristos=pod 213d40330Schristos 313d40330Schristos=head1 NAME 413d40330Schristos 5*4170684fSchristosCMS_decrypt, CMS_decrypt_set1_pkey_and_peer, 6*4170684fSchristosCMS_decrypt_set1_pkey, CMS_decrypt_set1_password 7*4170684fSchristos- decrypt content from a CMS envelopedData structure 813d40330Schristos 913d40330Schristos=head1 SYNOPSIS 1013d40330Schristos 1113d40330Schristos #include <openssl/cms.h> 1213d40330Schristos 1313d40330Schristos int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, 1413d40330Schristos BIO *dcont, BIO *out, unsigned int flags); 15b0d17251Schristos int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, 16b0d17251Schristos EVP_PKEY *pk, X509 *cert, X509 *peer); 17b0d17251Schristos int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert); 18*4170684fSchristos int CMS_decrypt_set1_password(CMS_ContentInfo *cms, 19*4170684fSchristos unsigned char *pass, ossl_ssize_t passlen); 2013d40330Schristos 2113d40330Schristos=head1 DESCRIPTION 2213d40330Schristos 23*4170684fSchristosCMS_decrypt() extracts the decrypted content from a CMS EnvelopedData 24*4170684fSchristosor AuthEnvelopedData structure. 25*4170684fSchristosIt uses CMS_decrypt_set1_pkey() to decrypt the content 26*4170684fSchristoswith the recipient private key I<pkey> if I<pkey> is not NULL. 27*4170684fSchristosIn this case, it is recommended to provide the associated certificate 28*4170684fSchristosin I<cert> - see the NOTES below. 29*4170684fSchristosI<out> is a BIO to write the content to and 30*4170684fSchristosI<flags> is an optional set of flags. 31*4170684fSchristosIf I<pkey> is NULL the function assumes that decryption was already done 32*4170684fSchristos(e.g., using CMS_decrypt_set1_pkey() or CMS_decrypt_set1_password()) and just 33*4170684fSchristosprovides the content unless I<cert>, I<dcont>, and I<out> are NULL as well. 34*4170684fSchristosThe I<dcont> parameter is used in the rare case where the encrypted content 3513d40330Schristosis detached. It will normally be set to NULL. 3613d40330Schristos 37*4170684fSchristosCMS_decrypt_set1_pkey_and_peer() decrypts the CMS_ContentInfo structure I<cms> 38*4170684fSchristosusing the private key I<pkey>, the corresponding certificate I<cert>, which is 39*4170684fSchristosrecommended to be supplied but may be NULL, 40*4170684fSchristosand the (optional) originator certificate I<peer>. 41*4170684fSchristosOn success, it also records in I<cms> the decryption key I<pkey>, and this 42*4170684fSchristosshould be followed by C<CMS_decrypt(cms, NULL, NULL, dcont, out, flags)>. 43*4170684fSchristosThis call deallocates any decryption key stored in I<cms>. 44b0d17251Schristos 45*4170684fSchristosCMS_decrypt_set1_pkey() is the same as 46*4170684fSchristosCMS_decrypt_set1_pkey_and_peer() with I<peer> being NULL. 47*4170684fSchristos 48*4170684fSchristosCMS_decrypt_set1_password() decrypts the CMS_ContentInfo structure I<cms> 49*4170684fSchristosusing the secret I<pass> of length I<passlen>. 50*4170684fSchristosOn success, it also records in I<cms> the decryption key used, and this 51*4170684fSchristosshould be followed by C<CMS_decrypt(cms, NULL, NULL, dcont, out, flags)>. 52*4170684fSchristosThis call deallocates any decryption key stored in I<cms>. 53b0d17251Schristos 5413d40330Schristos=head1 NOTES 5513d40330Schristos 5613d40330SchristosAlthough the recipients certificate is not needed to decrypt the data it is 5713d40330Schristosneeded to locate the appropriate (of possible several) recipients in the CMS 5813d40330Schristosstructure. 5913d40330Schristos 60*4170684fSchristosIf I<cert> is set to NULL all possible recipients are tried. This case however 6113d40330Schristosis problematic. To thwart the MMA attack (Bleichenbacher's attack on 6213d40330SchristosPKCS #1 v1.5 RSA padding) all recipients are tried whether they succeed or 6313d40330Schristosnot. If no recipient succeeds then a random symmetric key is used to decrypt 6413d40330Schristosthe content: this will typically output garbage and may (but is not guaranteed 6513d40330Schristosto) ultimately return a padding error only. If CMS_decrypt() just returned an 6613d40330Schristoserror when all recipient encrypted keys failed to decrypt an attacker could 6713d40330Schristosuse this in a timing attack. If the special flag B<CMS_DEBUG_DECRYPT> is set 6813d40330Schristosthen the above behaviour is modified and an error B<is> returned if no 6913d40330Schristosrecipient encrypted key can be decrypted B<without> generating a random 7013d40330Schristoscontent encryption key. Applications should use this flag with 7113d40330SchristosB<extreme caution> especially in automated gateways as it can leave them 7213d40330Schristosopen to attack. 7313d40330Schristos 7413d40330SchristosIt is possible to determine the correct recipient key by other means (for 7513d40330Schristosexample looking them up in a database) and setting them in the CMS structure 76*4170684fSchristosin advance using the CMS utility functions such as CMS_set1_pkey(), 77*4170684fSchristosor use CMS_decrypt_set1_password() if the recipient has a symmetric key. 78*4170684fSchristosIn these cases both I<cert> and I<pkey> should be set to NULL. 7913d40330Schristos 8013d40330SchristosTo process KEKRecipientInfo types CMS_set1_key() or CMS_RecipientInfo_set0_key() 8113d40330Schristosand CMS_RecipientInfo_decrypt() should be called before CMS_decrypt() and 82*4170684fSchristosI<cert> and I<pkey> set to NULL. 8313d40330Schristos 84*4170684fSchristosThe following flags can be passed in the I<flags> parameter. 8513d40330Schristos 86*4170684fSchristosIf the B<CMS_TEXT> flag is set MIME headers for type C<text/plain> are deleted 87*4170684fSchristosfrom the content. If the content is not of type C<text/plain> then an error is 8813d40330Schristosreturned. 8913d40330Schristos 9013d40330Schristos=head1 RETURN VALUES 9113d40330Schristos 92*4170684fSchristosCMS_decrypt(), CMS_decrypt_set1_pkey_and_peer(), 93*4170684fSchristosCMS_decrypt_set1_pkey(), and CMS_decrypt_set1_password() 94*4170684fSchristosreturn either 1 for success or 0 for failure. 95*4170684fSchristosThe error can be obtained from ERR_get_error(3). 9613d40330Schristos 9713d40330Schristos=head1 BUGS 9813d40330Schristos 99*4170684fSchristosThe B<set1_> part of these function names is misleading 100*4170684fSchristosand should better read: B<with_>. 101*4170684fSchristos 10213d40330SchristosThe lack of single pass processing and the need to hold all data in memory as 10313d40330Schristosmentioned in CMS_verify() also applies to CMS_decrypt(). 10413d40330Schristos 10513d40330Schristos=head1 SEE ALSO 10613d40330Schristos 10713d40330SchristosL<ERR_get_error(3)>, L<CMS_encrypt(3)> 10813d40330Schristos 109b0d17251Schristos=head1 HISTORY 110b0d17251Schristos 111*4170684fSchristosCMS_decrypt_set1_pkey_and_peer() and CMS_decrypt_set1_password() 112*4170684fSchristoswere added in OpenSSL 3.0. 113b0d17251Schristos 11413d40330Schristos=head1 COPYRIGHT 11513d40330Schristos 116*4170684fSchristosCopyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. 11713d40330Schristos 118b0d17251SchristosLicensed under the Apache License 2.0 (the "License"). You may not use 11913d40330Schristosthis file except in compliance with the License. You can obtain a copy 12013d40330Schristosin the file LICENSE in the source distribution or at 12113d40330SchristosL<https://www.openssl.org/source/license.html>. 12213d40330Schristos 12313d40330Schristos=cut 124