2016-05-18 21:16:36 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-18 21:16:36 +08:00
|
|
|
*
|
2018-12-06 21:08:15 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 21:16:36 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2008-04-12 07:52:26 +08:00
|
|
|
/*
|
|
|
|
* S/MIME detached data decrypt example: rarely done but should the need
|
|
|
|
* arise this is an example....
|
|
|
|
*/
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/cms.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;
|
|
|
|
X509 *rcert = NULL;
|
|
|
|
EVP_PKEY *rkey = NULL;
|
|
|
|
CMS_ContentInfo *cms = NULL;
|
2023-03-20 12:48:33 +08:00
|
|
|
int ret = EXIT_FAILURE;
|
2008-04-12 07:52:26 +08:00
|
|
|
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
|
|
|
/* Read in recipient certificate and private key */
|
|
|
|
tbio = BIO_new_file("signer.pem", "r");
|
|
|
|
|
|
|
|
if (!tbio)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
|
|
|
|
|
2023-11-11 03:02:00 +08:00
|
|
|
if (BIO_reset(tbio) < 0)
|
|
|
|
goto err;
|
2008-04-12 07:52:26 +08:00
|
|
|
|
|
|
|
rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
|
|
|
|
|
|
|
|
if (!rcert || !rkey)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Open PEM file containing enveloped data */
|
|
|
|
|
|
|
|
in = BIO_new_file("smencr.pem", "r");
|
|
|
|
|
|
|
|
if (!in)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Parse PEM content */
|
|
|
|
cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
|
|
|
|
|
|
|
|
if (!cms)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Open file containing detached content */
|
|
|
|
dcont = BIO_new_file("smencr.out", "rb");
|
|
|
|
|
|
|
|
if (!in)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
out = BIO_new_file("encrout.txt", "w");
|
|
|
|
if (!out)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Decrypt S/MIME message */
|
|
|
|
if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
|
|
|
|
goto err;
|
|
|
|
|
2023-03-20 12:48:33 +08:00
|
|
|
ret = EXIT_SUCCESS;
|
2008-04-12 07:52:26 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
|
2023-03-20 12:48:33 +08:00
|
|
|
if (ret != EXIT_SUCCESS) {
|
2008-04-12 07:52:26 +08:00
|
|
|
fprintf(stderr, "Error Decrypting Data\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:37:16 +08:00
|
|
|
CMS_ContentInfo_free(cms);
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_free(rcert);
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_free(rkey);
|
2015-03-25 23:31:18 +08:00
|
|
|
BIO_free(in);
|
|
|
|
BIO_free(out);
|
|
|
|
BIO_free(tbio);
|
|
|
|
BIO_free(dcont);
|
2008-04-12 07:52:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|