Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion from opaque pkcs7 p7m to detached smime

Hi I couldn't find a way to convert an opaque pkcs#7(p7m) in a clear text deatached smime so that the signed content could be processed by regular mime libraries.

I'd like to take p7m file and convert it to an smime message keping a valid signature.

The steps should be:

  • extract signed content from p7m

  • extract cms structure from p7m

  • pack everything in a new smime structure with detached signature

Is this operation possible ?

I've searched through openssl manuals but I couldn't find a way to do it.

like image 530
zukka Avatar asked Sep 19 '11 19:09

zukka


1 Answers

I was able to convert opaque-signed messages to a detached ones with the following code:

#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/pkcs7.h>

int main(int argc, char **argv)
{
    BIO *data = NULL, *bin = NULL, *bout = NULL;
    PKCS7 *p7, *p7b;

    OpenSSL_add_all_algorithms();

    bin = BIO_new_file("opaque.p7m", "rb");
    p7 = SMIME_read_PKCS7(bin, &data);
    p7b = PKCS7_dup(p7);

    data = PKCS7_dataInit(p7, NULL);

    PKCS7_set_detached(p7b, 1);

    bout = BIO_new_file("detached.p7m", "wb");
    SMIME_write_PKCS7(bout, p7b, data, PKCS7_BINARY | SMIME_DETACHED);
}

To test the program I generate the opaque.p7m with the following command:

$ openssl smime -sign -in foo.txt -signer my.crt -inkey my.key -nodetach -out opaque.p7m

To be terse, the code above has no checks. To accept different input formats, you can change SMIME_read_PKCS7 to PEM_read_bio_PKCS7 (PEM) or d2i_PKCS7_bio (DER).

like image 96
Mathias Brossard Avatar answered Oct 18 '22 19:10

Mathias Brossard