openssl API を使用して復号化したい DER でエンコードされた CMS ファイルがあります。
復号化用のAPIを見つけました。
CMS_decrypt(cms_content, pkey, cert, NULL, out, NULL);
pkey および cert PEM ファイルを読み取り、出力 BIO を設定する例は見つかりましたが、cms ファイルを読み取る方法がわかりません。
質問cms_content
: ASN.1 DER でエンコードされたファイルを、型を持つ変数に読み込むにはどうすればよいCMS_ContentInfo
ですか?
編集:カミーユの答えのおかげで、私はそれをうまく動かすことができました:
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/cms.h>
int main (int argc, char **argv)
{
char pkeypath[] = "recipient_prvkey.pem";
char certpath[] = "sender_cert.pem";
char cmspath[] = "encrypted.der";
char decpath[] = "decrypted.zip";
BIO *in = NULL, *out = NULL, *tbio = NULL;
CMS_ContentInfo *cms = NULL;
EVP_PKEY *pkey;
X509 *cert;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
tbio = BIO_new_file(pkeypath, "r");
pkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
if (pkey == NULL) {
printf("error reading private key");
return EXIT_FAILURE;
}
BIO_free(tbio);
tbio = BIO_new_file(certpath, "r");
cert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
if (cert == NULL) {
printf("error reading private key");
return EXIT_FAILURE;
}
in = BIO_new_file(cmspath, "r");
cms = d2i_CMS_bio(in, NULL);
BIO_free(in);
out = BIO_new_file(decpath, "w");
if (!CMS_decrypt_set1_pkey(cms, pkey, cert))
{
fprintf(stderr, "set1_pkey error\n");
return EXIT_FAILURE;
}
if (!CMS_decrypt(cms, NULL, NULL, NULL, out, CMS_BINARY))
{
int error = ERR_get_error();
fprintf(stderr, "error: %s :: %s :: %s\n",
ERR_reason_error_string(error),
ERR_func_error_string(error),
ERR_lib_error_string(error)
);
}
BIO_free(out);
return 0;
}