1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci * S/MIME detached data decrypt example: rarely done but should the need 12e1051a39Sopenharmony_ci * arise this is an example.... 13e1051a39Sopenharmony_ci */ 14e1051a39Sopenharmony_ci#include <openssl/pem.h> 15e1051a39Sopenharmony_ci#include <openssl/cms.h> 16e1051a39Sopenharmony_ci#include <openssl/err.h> 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ciint main(int argc, char **argv) 19e1051a39Sopenharmony_ci{ 20e1051a39Sopenharmony_ci BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL; 21e1051a39Sopenharmony_ci X509 *rcert = NULL; 22e1051a39Sopenharmony_ci EVP_PKEY *rkey = NULL; 23e1051a39Sopenharmony_ci CMS_ContentInfo *cms = NULL; 24e1051a39Sopenharmony_ci int ret = 1; 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ci OpenSSL_add_all_algorithms(); 27e1051a39Sopenharmony_ci ERR_load_crypto_strings(); 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci /* Read in recipient certificate and private key */ 30e1051a39Sopenharmony_ci tbio = BIO_new_file("signer.pem", "r"); 31e1051a39Sopenharmony_ci 32e1051a39Sopenharmony_ci if (!tbio) 33e1051a39Sopenharmony_ci goto err; 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ci rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); 36e1051a39Sopenharmony_ci 37e1051a39Sopenharmony_ci BIO_reset(tbio); 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ci rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ci if (!rcert || !rkey) 42e1051a39Sopenharmony_ci goto err; 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_ci /* Open PEM file containing enveloped data */ 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci in = BIO_new_file("smencr.pem", "r"); 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci if (!in) 49e1051a39Sopenharmony_ci goto err; 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci /* Parse PEM content */ 52e1051a39Sopenharmony_ci cms = PEM_read_bio_CMS(in, NULL, 0, NULL); 53e1051a39Sopenharmony_ci 54e1051a39Sopenharmony_ci if (!cms) 55e1051a39Sopenharmony_ci goto err; 56e1051a39Sopenharmony_ci 57e1051a39Sopenharmony_ci /* Open file containing detached content */ 58e1051a39Sopenharmony_ci dcont = BIO_new_file("smencr.out", "rb"); 59e1051a39Sopenharmony_ci 60e1051a39Sopenharmony_ci if (!in) 61e1051a39Sopenharmony_ci goto err; 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci out = BIO_new_file("encrout.txt", "w"); 64e1051a39Sopenharmony_ci if (!out) 65e1051a39Sopenharmony_ci goto err; 66e1051a39Sopenharmony_ci 67e1051a39Sopenharmony_ci /* Decrypt S/MIME message */ 68e1051a39Sopenharmony_ci if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0)) 69e1051a39Sopenharmony_ci goto err; 70e1051a39Sopenharmony_ci 71e1051a39Sopenharmony_ci ret = 0; 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_ci err: 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ci if (ret) { 76e1051a39Sopenharmony_ci fprintf(stderr, "Error Decrypting Data\n"); 77e1051a39Sopenharmony_ci ERR_print_errors_fp(stderr); 78e1051a39Sopenharmony_ci } 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci CMS_ContentInfo_free(cms); 81e1051a39Sopenharmony_ci X509_free(rcert); 82e1051a39Sopenharmony_ci EVP_PKEY_free(rkey); 83e1051a39Sopenharmony_ci BIO_free(in); 84e1051a39Sopenharmony_ci BIO_free(out); 85e1051a39Sopenharmony_ci BIO_free(tbio); 86e1051a39Sopenharmony_ci BIO_free(dcont); 87e1051a39Sopenharmony_ci return ret; 88e1051a39Sopenharmony_ci} 89