PKCS7_ENCRYPT(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’Œ PKCS7_ENCRYPT(3ossl) β€” the OpenSSL manual, bestie edition

okay so this is the PKCS7_ENCRYPT page from the OpenSSL manual (yes, the 3ossl one) and it's literally all about making envelopedData structures. stay with me, it's cute πŸ’–


✨ NAME ✨

PKCS7_encrypt_ex, PKCS7_encrypt β€” these two create a PKCS#7 envelopedData structure πŸŽ€


🌸 SYNOPSIS 🌸

#include <openssl/pkcs7.h>

PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
                        const EVP_CIPHER *cipher, int flags,
                        OSSL_LIB_CTX *libctx, const char *propq);
PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
                     int flags);

πŸ¦‹ DESCRIPTION πŸ¦‹

So PKCS7_encrypt_ex() creates and returns a PKCS#7 envelopedData structure. That's the whole vibe. πŸ’…

Here's the cast of characters, iykyk:

  • certs β€” a list of recipient certificates πŸ‘‘
  • in β€” the content you wanna encrypt
  • cipher β€” the symmetric cipher to use πŸ”
  • flags β€” an optional set of flags
  • libctx and propq β€” the library context and the property query, used when retrieving algorithms from providers
⚠️ important caveat bestie: only RSA keys are supported in PKCS#7 and envelopedData, so every recipient certificate you hand to this function must contain an RSA public key. They do not have to be signed using the RSA algorithm though β€” that part's flexible 🫢

EVP_des_ede3_cbc() (triple DES) is the algorithm of choice for S/MIME use, because most clients will support it. it's giving "everyone's already got it installed" energy.

Some old "export grade" clients may only support weak encryption using 40 or 64 bit RC2 😭 and you can use those by passing EVP_rc2_40_cbc() and EVP_rc2_64_cbc() respectively. (yes, weak. we said what we said.)

The algorithm passed in the cipher parameter must support ASN1 encoding of its parameters. no ASN1 params, no entry, periodt.

Okay so many browsers implement a "sign and encrypt" option which is simply an S/MIME envelopedData containing an S/MIME signed message. The way to produce that here? store the S/MIME signed message in a memory BIO and pass it to PKCS7_encrypt(). iconic, honestly.

πŸŽ€ the flags

The following flags can be passed in the flags parameter. let's go:

  • PKCS7_TEXT β€” if this is set, MIME headers for type text/plain get prepended to the data
  • PKCS7_BINARY β€” normally the supplied content is translated into MIME canonical format (as required by the S/MIME specifications). if this flag is set, no translation occurs. use it when your supplied data is in binary format, otherwise the translation will corrupt it πŸ’€ also, if PKCS7_BINARY is set then PKCS7_TEXT is ignored
  • PKCS7_STREAM β€” if set, a partial PKCS7 structure is output, suitable for streaming I/O: no data is read from the BIO in
🚨 okay real talk, dramatic warning time: if the flag PKCS7_STREAM is set, the returned PKCS7 structure is not complete, and outputting its contents via a function that does not properly finalize the PKCS7 structure will give unpredictable results. not a vibe. not slay.

Several functions finalize the structure, including SMIME_write_PKCS7(), i2d_PKCS7_bio_stream(), PEM_write_bio_PKCS7_stream(). Alternatively, finalization can be performed by obtaining the streaming ASN1 BIO directly using BIO_new_PKCS7(). 🧚

PKCS7_encrypt() is similar to PKCS7_encrypt_ex() but uses default values of NULL for the library context libctx and the property query propq. same energy, fewer knobs.


⭐ RETURN VALUES ⭐

PKCS7_encrypt_ex() and PKCS7_encrypt() return either a PKCS7 structure or NULL if an error occurred 😭 and you can grab the error from ERR_get_error(3).


🌈 SEE ALSO 🌈

ERR_get_error(3), PKCS7_decrypt(3)


πŸ’œ HISTORY πŸ’œ

  • The function PKCS7_encrypt_ex() was added in OpenSSL 3.0
  • The PKCS7_STREAM flag was added in OpenSSL 1.0.0

Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page bestie 🫢 this one's from OpenSSL 3.5.0, dated 2026-09-25, and it was literally so fun. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from