1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1.1.3 christos CMS_decrypt, CMS_decrypt_set1_pkey_and_peer, 6 1.1.1.3 christos CMS_decrypt_set1_pkey, CMS_decrypt_set1_password 7 1.1.1.3 christos - decrypt content from a CMS envelopedData structure 8 1.1 christos 9 1.1 christos =head1 SYNOPSIS 10 1.1 christos 11 1.1 christos #include <openssl/cms.h> 12 1.1 christos 13 1.1 christos int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, 14 1.1 christos BIO *dcont, BIO *out, unsigned int flags); 15 1.1.1.2 christos int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, 16 1.1.1.2 christos EVP_PKEY *pk, X509 *cert, X509 *peer); 17 1.1.1.2 christos int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert); 18 1.1.1.3 christos int CMS_decrypt_set1_password(CMS_ContentInfo *cms, 19 1.1.1.3 christos unsigned char *pass, ossl_ssize_t passlen); 20 1.1 christos 21 1.1 christos =head1 DESCRIPTION 22 1.1 christos 23 1.1.1.3 christos CMS_decrypt() extracts the decrypted content from a CMS EnvelopedData 24 1.1.1.3 christos or AuthEnvelopedData structure. 25 1.1.1.3 christos It uses CMS_decrypt_set1_pkey() to decrypt the content 26 1.1.1.3 christos with the recipient private key I<pkey> if I<pkey> is not NULL. 27 1.1.1.3 christos In this case, it is recommended to provide the associated certificate 28 1.1.1.3 christos in I<cert> - see the NOTES below. 29 1.1.1.3 christos I<out> is a BIO to write the content to and 30 1.1.1.3 christos I<flags> is an optional set of flags. 31 1.1.1.3 christos If I<pkey> is NULL the function assumes that decryption was already done 32 1.1.1.3 christos (e.g., using CMS_decrypt_set1_pkey() or CMS_decrypt_set1_password()) and just 33 1.1.1.3 christos provides the content unless I<cert>, I<dcont>, and I<out> are NULL as well. 34 1.1.1.3 christos The I<dcont> parameter is used in the rare case where the encrypted content 35 1.1 christos is detached. It will normally be set to NULL. 36 1.1 christos 37 1.1.1.3 christos CMS_decrypt_set1_pkey_and_peer() decrypts the CMS_ContentInfo structure I<cms> 38 1.1.1.3 christos using the private key I<pkey>, the corresponding certificate I<cert>, which is 39 1.1.1.3 christos recommended to be supplied but may be NULL, 40 1.1.1.3 christos and the (optional) originator certificate I<peer>. 41 1.1.1.3 christos On success, it also records in I<cms> the decryption key I<pkey>, and this 42 1.1.1.3 christos should be followed by C<CMS_decrypt(cms, NULL, NULL, dcont, out, flags)>. 43 1.1.1.3 christos This call deallocates any decryption key stored in I<cms>. 44 1.1.1.3 christos 45 1.1.1.3 christos CMS_decrypt_set1_pkey() is the same as 46 1.1.1.3 christos CMS_decrypt_set1_pkey_and_peer() with I<peer> being NULL. 47 1.1.1.3 christos 48 1.1.1.3 christos CMS_decrypt_set1_password() decrypts the CMS_ContentInfo structure I<cms> 49 1.1.1.3 christos using the secret I<pass> of length I<passlen>. 50 1.1.1.3 christos On success, it also records in I<cms> the decryption key used, and this 51 1.1.1.3 christos should be followed by C<CMS_decrypt(cms, NULL, NULL, dcont, out, flags)>. 52 1.1.1.3 christos This call deallocates any decryption key stored in I<cms>. 53 1.1.1.2 christos 54 1.1 christos =head1 NOTES 55 1.1 christos 56 1.1 christos Although the recipients certificate is not needed to decrypt the data it is 57 1.1 christos needed to locate the appropriate (of possible several) recipients in the CMS 58 1.1 christos structure. 59 1.1 christos 60 1.1.1.3 christos If I<cert> is set to NULL all possible recipients are tried. This case however 61 1.1 christos is problematic. To thwart the MMA attack (Bleichenbacher's attack on 62 1.1 christos PKCS #1 v1.5 RSA padding) all recipients are tried whether they succeed or 63 1.1 christos not. If no recipient succeeds then a random symmetric key is used to decrypt 64 1.1 christos the content: this will typically output garbage and may (but is not guaranteed 65 1.1 christos to) ultimately return a padding error only. If CMS_decrypt() just returned an 66 1.1 christos error when all recipient encrypted keys failed to decrypt an attacker could 67 1.1 christos use this in a timing attack. If the special flag B<CMS_DEBUG_DECRYPT> is set 68 1.1 christos then the above behaviour is modified and an error B<is> returned if no 69 1.1 christos recipient encrypted key can be decrypted B<without> generating a random 70 1.1 christos content encryption key. Applications should use this flag with 71 1.1 christos B<extreme caution> especially in automated gateways as it can leave them 72 1.1 christos open to attack. 73 1.1 christos 74 1.1 christos It is possible to determine the correct recipient key by other means (for 75 1.1 christos example looking them up in a database) and setting them in the CMS structure 76 1.1.1.3 christos in advance using the CMS utility functions such as CMS_set1_pkey(), 77 1.1.1.3 christos or use CMS_decrypt_set1_password() if the recipient has a symmetric key. 78 1.1.1.3 christos In these cases both I<cert> and I<pkey> should be set to NULL. 79 1.1 christos 80 1.1 christos To process KEKRecipientInfo types CMS_set1_key() or CMS_RecipientInfo_set0_key() 81 1.1 christos and CMS_RecipientInfo_decrypt() should be called before CMS_decrypt() and 82 1.1.1.3 christos I<cert> and I<pkey> set to NULL. 83 1.1 christos 84 1.1.1.3 christos The following flags can be passed in the I<flags> parameter. 85 1.1 christos 86 1.1.1.3 christos If the B<CMS_TEXT> flag is set MIME headers for type C<text/plain> are deleted 87 1.1.1.3 christos from the content. If the content is not of type C<text/plain> then an error is 88 1.1 christos returned. 89 1.1 christos 90 1.1 christos =head1 RETURN VALUES 91 1.1 christos 92 1.1.1.3 christos CMS_decrypt(), CMS_decrypt_set1_pkey_and_peer(), 93 1.1.1.3 christos CMS_decrypt_set1_pkey(), and CMS_decrypt_set1_password() 94 1.1.1.3 christos return either 1 for success or 0 for failure. 95 1.1.1.3 christos The error can be obtained from ERR_get_error(3). 96 1.1 christos 97 1.1 christos =head1 BUGS 98 1.1 christos 99 1.1.1.3 christos The B<set1_> part of these function names is misleading 100 1.1.1.3 christos and should better read: B<with_>. 101 1.1.1.3 christos 102 1.1 christos The lack of single pass processing and the need to hold all data in memory as 103 1.1 christos mentioned in CMS_verify() also applies to CMS_decrypt(). 104 1.1 christos 105 1.1 christos =head1 SEE ALSO 106 1.1 christos 107 1.1 christos L<ERR_get_error(3)>, L<CMS_encrypt(3)> 108 1.1 christos 109 1.1.1.2 christos =head1 HISTORY 110 1.1.1.2 christos 111 1.1.1.3 christos CMS_decrypt_set1_pkey_and_peer() and CMS_decrypt_set1_password() 112 1.1.1.3 christos were added in OpenSSL 3.0. 113 1.1.1.2 christos 114 1.1 christos =head1 COPYRIGHT 115 1.1 christos 116 1.1.1.3 christos Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved. 117 1.1 christos 118 1.1.1.2 christos Licensed under the Apache License 2.0 (the "License"). You may not use 119 1.1 christos this file except in compliance with the License. You can obtain a copy 120 1.1 christos in the file LICENSE in the source distribution or at 121 1.1 christos L<https://www.openssl.org/source/license.html>. 122 1.1 christos 123 1.1 christos =cut 124