Decrypting s/mime messages in p7m format with OpenSSL

不羁的心 提交于 2019-12-13 00:23:17

问题


I'm trying to decrypt p7m with OpenSSL but I cannot go through the error in the following part of the code:

PKCS7 *p7 = NULL;

in = BIO_new_file(convertedResourcePath, "r");

    if (in) {
        NSLog(@"opening p7m file");
    }
    else
        NSLog(@"cannot found p7m file");

    out = BIO_new_file(convertedDecrFilePath, "w");

    if (out) {
        NSLog(@"file for decription has been created");
    }
    else
        NSLog(@"failed to create decription file");


    p7 = SMIME_read_PKCS7(in, NULL);

if (p7) {
        NSLog(@"start reading p7m file");

    }
    else {
        NSLog(@"cannot read p7m file");
        ERR_print_errors_fp(stderr);
    }

 if (PKCS7_decrypt(p7, pkey, cert, out, 0)) {
        NSLog(@"file decrypted sucessfully!");
    }
    else
        NSLog(@"cannot decrypt file");

I got the following in output:

opening p7m file 2013-07-22 12:45:22.951 smimePrototype[10827:c07] file for decription has been created 2013-07-22 12:45:22.952 smimePrototype[10827:c07] cannot read p7m file 2900150892:error:0D0D40D1:asn1 encoding routines:SMIME_read_ASN1:no content type:asn_mime.c:451: 2013-07-22 12:45:22.953 smimePrototype[10827:c07] cannot decrypt file

Looking for your help, maybe p7 variable can be initialized in other way?


回答1:


I tried to use

p7=d2i_PKCS7_bio(in,NULL);

instead of

p7 = SMIME_read_PKCS7(in, NULL);

and it works great.

I hope it will help someone.



来源:https://stackoverflow.com/questions/17784735/decrypting-s-mime-messages-in-p7m-format-with-openssl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!