PKCS7_ENCRYPT(3)
π PKCS7_ENCRYPT(3) β the envelopedData glow-up π
okay bestie, welcome to the libcrypto manual page for PKCS7_encrypt(). yes, that libcrypto. the crypto one. buckle up π
β¨ NAME β¨
PKCS7_encrypt β create a PKCS#7 envelopedData structure π
(yes that's the whole job, and she does it gorgeously)
πΈ SYNOPSIS πΈ
library βlibcryptoβ #include <openssl/pkcs7.h>
PKCS7 *
PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
int flags);
π¦ DESCRIPTION π¦
So PKCS7_encrypt() literally creates and returns a PKCS#7 envelopedData structure. that's it, that's the vibe β¨ here's the cast:
certsβ the list of recipient certificates π«Άinβ the content that's getting encrypted πcipherβ the symmetric cipher to use π§flagsβ an optional set of flags π©
β οΈ important!! Only RSA keys are supported in PKCS#7 and envelopedData. so every single recipient certificate you hand this function must contain an RSA public key. ngl they don't have to be signed using the RSA algorithm though β just gotta have the RSA public key. iykyk π
And the algorithm you pass in cipher? it has to support ASN.1 encoding of its parameters. no exceptions bestie β
okay so here's a fun one: many browsers implement a "sign and encrypt" option which is honestly just an S/MIME envelopedData containing an S/MIME signed message. main character energy honestly π you can produce that easily by storing the S/MIME signed message in a memory BIO and passing it to PKCS7_encrypt(). slay π
π the flags, one by one π
these go in the flags parameter, bestie:
PKCS7_TEXTβ if set, MIME headers for typetext/plainget prepended to the data πPKCS7_BINARYβ normally the supplied content gets translated into MIME canonical format (as required by the S/MIME specifications, periodt). if this flag is set, no translation occurs. π¨ use this if your supplied data is in binary format β otherwise the translation will literally corrupt it π. and heads up: ifPKCS7_BINARYis set, thenPKCS7_TEXTis ignored.PKCS7_STREAMβ if set, a partial PKCS7 structure is output suitable for streaming I/O: no data is read fromin. iconic for streaming, but see the warning below π
π BIG WARNING: 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. like, actually unpredictable. not cute unpredictable. π
so how do you finalize it? several functions do the job:
alternatively, finalization can be performed by obtaining the streaming ASN.1 BIO directly using BIO_new_PKCS7(). options, we love options π«Ά
β RETURN VALUES β
PKCS7_encrypt() returns either a PKCS7 structure, or NULL if an error occurred π if it's NULL, you can grab the error from ERR_get_error(3). go get your diagnostics bestie π
π SEE ALSO π
(the whole PKCS7 friend group honestly π)
π HISTORY π
PKCS7_encrypt() first appeared in OpenSSL 0.9.5 and has been available since OpenBSD 2.7. a true veteran, we stan π«‘
The PKCS7_STREAM flag was first supported in OpenSSL 1.0.0. she's newer but she's that girl π
kisses and good luck with your envelopedData π β this page came straight from the Debian libcrypto docs, dated June 8, 2025. bye bestie, go encrypt something gorgeous πβ¨
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2002, 2006, 2007, 2008, 2009 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms