PKCS7_DATAINIT(3)
π PKCS7_dataInit β PKCS7_DATAINIT(3) π
πΈ NAME πΈ
PKCS7_dataInit β construct a BIO chain for adding or retrieving content π
β¨ SYNOPSIS β¨
library βlibcryptoβ
#include <openssl/pkcs7.h>
BIO *
PKCS7_dataInit(PKCS7 *p7, BIO *indata);
π DESCRIPTION π
Okay bestie, so PKCS7_dataInit() constructs a BIO chain in preparation for putting data into or retrieving data out of p7. Depending on the contentType of p7, the created chain starts with:
- for
SignedData: one or moreBIO_f_md(3)message digest filters β¨ - for
EnvelopedData: oneBIO_f_cipher(3)encryption filter π₯ - for
SignedAndEnvelopedData: one or moreBIO_f_md(3)message digest filters followed by oneBIO_f_cipher(3)encryption filter π - for
DigestedData: oneBIO_f_md(3)message digest filter π - for arbitrary data: no filter BIO π¦
One additional BIO is appended to the end of the chain, depending on the first condition that holds in the following list:
indataβ if theindataargument is notNULL. This only makes sense while verifying a detached signature, in which caseindatais expected to supply the content associated with the detached signature.BIO_s_null(3)β if thecontentTypeofp7isSignedDataand it is configured to contain a detached signature. This only makes sense while creating the detached signature.BIO_new_mem_buf(3)β when reading from aSignedDataorDigestedDataobject.PKCS7_dataInit()attaches the end of the chain to the nested content ofp7.BIO_s_mem(3)β otherwise. This is the most common case while writing data top7.PKCS7_dataFinal(3)can later be used to transfer the data from the memory BIO intop7.
π«Ά Adding content π«Ά
Before calling PKCS7_dataInit() in order to add content, PKCS7_new(3), PKCS7_set_type(3), and PKCS7_content_new(3) are typically required to create p7, to choose its desired type, and to allocate the nested ContentInfo structure. Alternatively, for SignedData, PKCS7_sign(3) can be used with the PKCS7_PARTIAL or PKCS7_STREAM flags or for EnvelopedData, PKCS7_encrypt(3) with the PKCS7_STREAM flag. π
After calling PKCS7_dataInit(), the desired data can be written into the returned BIO, BIO_flush(3) can be called on it, PKCS7_dataFinal(3) can be used to transfer the processed data from the returned memory BIO to the p7 structure, and the chain can finally be destroyed with BIO_free_all(3). π«Ά
While PKCS7_dataInit() does support the EnvelopedData and SignedAndEnvelopedData types, using it for these types is awkward and error prone except when using PKCS7_encrypt(3) for the setup because PKCS7_content_new(3) does not support these two types. So in addition to creating p7 itself and setting its type, the nested ContentInfo structure also needs to be constructed with PKCS7_new(3) and PKCS7_set_type(3) and manually inserted into the correct field of the respective sub-structure of p7. π
π§ Retrieving content π§
PKCS7_dataInit() can also be called on a fully populated object of type SignedData or DigestedData. After that, BIO_read(3) can be used to retrieve data from it. In this use case, do not call PKCS7_dataFinal(3); simply proceed directly to BIO_free_all(3) after reading the data. β
π¦ RETURN VALUES π¦
PKCS7_dataInit() returns a BIO chain on success or NULL on failure. It fails if p7 is NULL, if the content field of p7 is empty, if the contentType of p7 is unsupported, if a cipher is required but none is configured, or if any required operation fails, for example due to lack of memory or for various other reasons. π
π SEE ALSO π
BIO_new(3)BIO_read(3)PKCS7_content_new(3)PKCS7_dataFinal(3)PKCS7_encrypt(3)PKCS7_final(3)PKCS7_new(3)PKCS7_set_type(3)PKCS7_sign(3)
β HISTORY β
PKCS7_dataInit() first appeared in SSLeay 0.8.1 and has been available since OpenBSD 2.4. π
π CAVEATS π
β οΈ This function does not support EncryptedData. Periodt.
π BUGS π
π Ifp7is a fully populated structure containingEnvelopedData,SignedAndEnvelopedData, or arbitrary data,PKCS7_dataInit()returns a BIO chain that ultimately reads from an empty memory BIO, so reading from it will instantly return an end-of-file indication rather than reading the actual data contained inp7. Not me crying over this behavior π
π Big cute sign-off bestie: this page is from Debian, dated June 8, 2025, and it's the PKCS7_DATAINIT(3) manual. Slay. πβ¨
π¦ 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) 2020 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms