---
name: "libressl-CMS_decrypt"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-CMS_decrypt"
---

# libressl-CMS\_decrypt(3)

decrypt content from a CMS EnvelopedData structure

## NAME

**CMS\_decrypt**, **CMS\_decrypt\_set1\_pkey**, **CMS\_decrypt\_set1\_key** – decrypt content from a CMS EnvelopedData structure

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/cms.h\>**

*int*\

**CMS\_decrypt**(*CMS\_ContentInfo \*cms*, *EVP\_PKEY \*private\_key*, *X509 \*certificate*, *BIO \*dcont*, *BIO \*out*, *unsigned int flags*);

*int*\

**CMS\_decrypt\_set1\_pkey**(*CMS\_ContentInfo \*cms*, *EVP\_PKEY \*private\_key*, *X509 \*certificate*);

*int*\

**CMS\_decrypt\_set1\_key**(*CMS\_ContentInfo \*cms*, *unsigned char \*symmetric\_key*, *size\_t keylen*, *const unsigned char \*id*, *size\_t idlen*);

## DESCRIPTION

**CMS\_decrypt**() extracts and decrypts the content from the CMS *EnvelopedData* structure *cms* using the *private\_key* and the *certificate* of the recipient. It writes the decrypted content to *out*.

In the rare case where the compressed content is detached, pass it in via *dcont*. For normal use, set *dcont* to NULL.

Although the recipient's *certificate* is not needed to decrypt the data, it is needed to locate the appropriate (of possibly several) recipients in the CMS structure.

If the *certificate* is set to NULL, all possible recipients are tried. This case however is problematic. To thwart the MMA attack (Bleichenbacher's attack on PKCS #1 v1.5 RSA padding), all recipients are tried whether they succeed or not. If no recipient succeeds, a random symmetric key is used to decrypt the content: this will typically output garbage and may (but is not guaranteed to) ultimately return a padding error only. If **CMS\_decrypt**() just returned an error when all recipient encrypted keys failed to decrypt, an attacker could use this in a timing attack. If the special flag CMS\_DEBUG\_DECRYPT is set, the above behaviour is modified and an error *is* returned if no recipient encrypted key can be decrypted *without* generating a random content encryption key. Applications should use this flag with extreme caution especially in automated gateways as it can leave them open to attack.

It is possible to determine the correct recipient key by other means (for example by looking them up in a database) and setting them in the *cms* structure in advance using the CMS utility functions such as **CMS\_decrypt\_set1\_pkey**(). In this case both *certificate* and *private\_key* should be set to NULL when calling **CMS\_decrypt**() later on.

To process *KEKRecipientInfo* types, **CMS\_decrypt\_set1\_key**() or CMS\_RecipientInfo\_set0\_key(3) and CMS\_RecipientInfo\_decrypt(3) should be called before **CMS\_decrypt**() and *certificate* and *private\_key* set to NULL when calling **CMS\_decrypt**() later on.

If the CMS\_TEXT bit is set in *flags*, MIME headers for type text/plain are deleted from the content. If the content is not of type text/plain, an error occurs.

## RETURN VALUES

**CMS\_decrypt**(), **CMS\_decrypt\_set1\_pkey**(), and **CMS\_decrypt\_set1\_key**() return 1 for success or 0 for failure. The error can be obtained from ERR\_get\_error(3).

## SEE ALSO

CMS\_ContentInfo\_new(3), CMS\_encrypt(3), CMS\_get0\_RecipientInfos(3)

## STANDARDS

RFC 5652: Cryptographic Message Syntax (CMS)

- section 6.1: EnvelopedData Type

- section 6.2.3: KEKRecipientInfo Type

## HISTORY

**CMS\_decrypt**(), **CMS\_decrypt\_set1\_pkey**(), and **CMS\_decrypt\_set1\_key**() first appeared in OpenSSL 0.9.8h and have been available since OpenBSD 6.7.

## BUGS

The lack of single pass processing and the need to hold all data in memory as mentioned in CMS\_verify(3) also applies to **CMS\_decrypt**().
