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 encrypt 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, *dout = NULL; 21e1051a39Sopenharmony_ci X509 *rcert = NULL; 22e1051a39Sopenharmony_ci STACK_OF(X509) *recips = NULL; 23e1051a39Sopenharmony_ci CMS_ContentInfo *cms = NULL; 24e1051a39Sopenharmony_ci int ret = 1; 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ci int flags = CMS_STREAM | CMS_DETACHED; 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ci OpenSSL_add_all_algorithms(); 29e1051a39Sopenharmony_ci ERR_load_crypto_strings(); 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci /* Read in recipient certificate */ 32e1051a39Sopenharmony_ci tbio = BIO_new_file("signer.pem", "r"); 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci if (!tbio) 35e1051a39Sopenharmony_ci goto err; 36e1051a39Sopenharmony_ci 37e1051a39Sopenharmony_ci rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ci if (!rcert) 40e1051a39Sopenharmony_ci goto err; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci /* Create recipient STACK and add recipient cert to it */ 43e1051a39Sopenharmony_ci recips = sk_X509_new_null(); 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci if (!recips || !sk_X509_push(recips, rcert)) 46e1051a39Sopenharmony_ci goto err; 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci /* 49e1051a39Sopenharmony_ci * sk_X509_pop_free will free up recipient STACK and its contents so set 50e1051a39Sopenharmony_ci * rcert to NULL so it isn't freed up twice. 51e1051a39Sopenharmony_ci */ 52e1051a39Sopenharmony_ci rcert = NULL; 53e1051a39Sopenharmony_ci 54e1051a39Sopenharmony_ci /* Open content being encrypted */ 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ci in = BIO_new_file("encr.txt", "r"); 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci dout = BIO_new_file("smencr.out", "wb"); 59e1051a39Sopenharmony_ci 60e1051a39Sopenharmony_ci if (!in) 61e1051a39Sopenharmony_ci goto err; 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci /* encrypt content */ 64e1051a39Sopenharmony_ci cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); 65e1051a39Sopenharmony_ci 66e1051a39Sopenharmony_ci if (!cms) 67e1051a39Sopenharmony_ci goto err; 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ci out = BIO_new_file("smencr.pem", "w"); 70e1051a39Sopenharmony_ci if (!out) 71e1051a39Sopenharmony_ci goto err; 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_ci if (!CMS_final(cms, in, dout, flags)) 74e1051a39Sopenharmony_ci goto err; 75e1051a39Sopenharmony_ci 76e1051a39Sopenharmony_ci /* Write out CMS structure without content */ 77e1051a39Sopenharmony_ci if (!PEM_write_bio_CMS(out, cms)) 78e1051a39Sopenharmony_ci goto err; 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci ret = 0; 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ci err: 83e1051a39Sopenharmony_ci 84e1051a39Sopenharmony_ci if (ret) { 85e1051a39Sopenharmony_ci fprintf(stderr, "Error Encrypting Data\n"); 86e1051a39Sopenharmony_ci ERR_print_errors_fp(stderr); 87e1051a39Sopenharmony_ci } 88e1051a39Sopenharmony_ci 89e1051a39Sopenharmony_ci CMS_ContentInfo_free(cms); 90e1051a39Sopenharmony_ci X509_free(rcert); 91e1051a39Sopenharmony_ci sk_X509_pop_free(recips, X509_free); 92e1051a39Sopenharmony_ci BIO_free(in); 93e1051a39Sopenharmony_ci BIO_free(out); 94e1051a39Sopenharmony_ci BIO_free(dout); 95e1051a39Sopenharmony_ci BIO_free(tbio); 96e1051a39Sopenharmony_ci return ret; 97e1051a39Sopenharmony_ci} 98