Path: blob/main/crypto/openssl/demos/cms/cms_dec.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/* Simple S/MIME decryption example */10#include <openssl/pem.h>11#include <openssl/cms.h>12#include <openssl/err.h>1314int main(int argc, char **argv)15{16BIO *in = NULL, *out = NULL, *tbio = NULL;17X509 *rcert = NULL;18EVP_PKEY *rkey = NULL;19CMS_ContentInfo *cms = NULL;20int ret = EXIT_FAILURE;2122OpenSSL_add_all_algorithms();23ERR_load_crypto_strings();2425/* Read in recipient certificate and private key */26tbio = BIO_new_file("signer.pem", "r");2728if (!tbio)29goto err;3031rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);3233if (BIO_reset(tbio) < 0)34goto err;3536rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);3738if (!rcert || !rkey)39goto err;4041/* Open S/MIME message to decrypt */4243in = BIO_new_file("smencr.txt", "r");4445if (!in)46goto err;4748/* Parse message */49cms = SMIME_read_CMS(in, NULL);5051if (!cms)52goto err;5354out = BIO_new_file("decout.txt", "w");55if (!out)56goto err;5758/* Decrypt S/MIME message */59if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0))60goto err;6162printf("Decryption Successful\n");6364ret = EXIT_SUCCESS;6566err:67if (ret != EXIT_SUCCESS) {68fprintf(stderr, "Error Decrypting Data\n");69ERR_print_errors_fp(stderr);70}7172CMS_ContentInfo_free(cms);73X509_free(rcert);74EVP_PKEY_free(rkey);75BIO_free(in);76BIO_free(out);77BIO_free(tbio);78return ret;79}808182