Path: blob/main/crypto/openssl/demos/cms/cms_ddec.c
34879 views
/*1* Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* S/MIME detached data decrypt example: rarely done but should the need11* arise this is an example....12*/13#include <openssl/pem.h>14#include <openssl/cms.h>15#include <openssl/err.h>1617int main(int argc, char **argv)18{19BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;20X509 *rcert = NULL;21EVP_PKEY *rkey = NULL;22CMS_ContentInfo *cms = NULL;23int ret = EXIT_FAILURE;2425OpenSSL_add_all_algorithms();26ERR_load_crypto_strings();2728/* Read in recipient certificate and private key */29tbio = BIO_new_file("signer.pem", "r");3031if (!tbio)32goto err;3334rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);3536if (BIO_reset(tbio) < 0)37goto err;3839rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);4041if (!rcert || !rkey)42goto err;4344/* Open PEM file containing enveloped data */4546in = BIO_new_file("smencr.pem", "r");4748if (!in)49goto err;5051/* Parse PEM content */52cms = PEM_read_bio_CMS(in, NULL, 0, NULL);5354if (!cms)55goto err;5657/* Open file containing detached content */58dcont = BIO_new_file("smencr.out", "rb");5960if (!in)61goto err;6263out = BIO_new_file("encrout.txt", "w");64if (!out)65goto err;6667/* Decrypt S/MIME message */68if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))69goto err;7071ret = EXIT_SUCCESS;7273err:7475if (ret != EXIT_SUCCESS) {76fprintf(stderr, "Error Decrypting Data\n");77ERR_print_errors_fp(stderr);78}7980CMS_ContentInfo_free(cms);81X509_free(rcert);82EVP_PKEY_free(rkey);83BIO_free(in);84BIO_free(out);85BIO_free(tbio);86BIO_free(dcont);87return ret;88}899091