Search code examples
copenssllibcrypto

Decrypt the content of a signed CMS_ContentInfo*


I am having problems accessing the EnvelopedData within a signed CMS_ContentInfo* object when using a memory BIO.

Using the following code, everything works fine :

BIO* output = BIO_new_file("/absolute/path/test.txt", "r+");
                    if (CMS_verify(cms, stack, store, dcont, output, CMS_NOINTERN)) {
    BIO_flush(output);
    BIO_reset(output);
    CMS_ContentInfo* cms2 = SMIME_read_CMS(output, nullptr);
}

cms2 is instantiated properly and I am able to decrypt its content. Although, I don't want the file to be written to disk so I am trying to make this work in memory like so :

BIO* output = BIO_new(BIO_s_mem());
if (CMS_verify(cms, stack, store, dcont, output, CMS_NOINTERN)) {
    BIO_flush(output);
    BIO_seek(output, 0);
    CMS_ContentInfo* cms2 = SMIME_read_CMS(output, nullptr);
}

For some reason, it seems the SMIME_read_CMS function can never read from the BIO. Can anyone help me get this working?


Solution

  • I have found my solution. Here is the piece of code I have used :

    BIO* output = BIO_new(BIO_s_mem());
    if (CMS_verify(cms, stack, store, dcont, nullptr, CMS_NOINTERN)) {
        CMS_ContentInfo* cms2 = SMIME_read_CMS(dcont, nullptr);
    }
    

    Apparently, it is the dcont BIO that holds the SignedData and I can properly decrypt it afterwards using the cms2 variable.