1 1.1 christos /* 2 1.1.1.2 christos * Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos /* 11 1.1 christos * S/MIME detached data encrypt example: rarely done but should the need 12 1.1 christos * arise this is an example.... 13 1.1 christos */ 14 1.1 christos #include <openssl/pem.h> 15 1.1 christos #include <openssl/cms.h> 16 1.1 christos #include <openssl/err.h> 17 1.1 christos 18 1.1 christos int main(int argc, char **argv) 19 1.1 christos { 20 1.1 christos BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL; 21 1.1 christos X509 *rcert = NULL; 22 1.1 christos STACK_OF(X509) *recips = NULL; 23 1.1 christos CMS_ContentInfo *cms = NULL; 24 1.1 christos int ret = EXIT_FAILURE; 25 1.1 christos 26 1.1 christos int flags = CMS_STREAM | CMS_DETACHED; 27 1.1 christos 28 1.1 christos OpenSSL_add_all_algorithms(); 29 1.1 christos ERR_load_crypto_strings(); 30 1.1 christos 31 1.1 christos /* Read in recipient certificate */ 32 1.1 christos tbio = BIO_new_file("signer.pem", "r"); 33 1.1 christos 34 1.1 christos if (!tbio) 35 1.1 christos goto err; 36 1.1 christos 37 1.1 christos rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); 38 1.1 christos 39 1.1 christos if (!rcert) 40 1.1 christos goto err; 41 1.1 christos 42 1.1 christos /* Create recipient STACK and add recipient cert to it */ 43 1.1 christos recips = sk_X509_new_null(); 44 1.1 christos 45 1.1 christos if (!recips || !sk_X509_push(recips, rcert)) 46 1.1 christos goto err; 47 1.1 christos 48 1.1 christos /* 49 1.1 christos * OSSL_STACK_OF_X509_free() free up recipient STACK and its contents 50 1.1 christos * so set rcert to NULL so it isn't freed up twice. 51 1.1 christos */ 52 1.1 christos rcert = NULL; 53 1.1 christos 54 1.1 christos /* Open content being encrypted */ 55 1.1 christos 56 1.1 christos in = BIO_new_file("encr.txt", "r"); 57 1.1 christos 58 1.1 christos dout = BIO_new_file("smencr.out", "wb"); 59 1.1 christos 60 1.1.1.2 christos if (in == NULL || dout == NULL) 61 1.1 christos goto err; 62 1.1 christos 63 1.1 christos /* encrypt content */ 64 1.1 christos cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); 65 1.1 christos 66 1.1 christos if (!cms) 67 1.1 christos goto err; 68 1.1 christos 69 1.1 christos out = BIO_new_file("smencr.pem", "w"); 70 1.1 christos if (!out) 71 1.1 christos goto err; 72 1.1 christos 73 1.1 christos if (!CMS_final(cms, in, dout, flags)) 74 1.1 christos goto err; 75 1.1 christos 76 1.1 christos /* Write out CMS structure without content */ 77 1.1 christos if (!PEM_write_bio_CMS(out, cms)) 78 1.1 christos goto err; 79 1.1 christos 80 1.1 christos ret = EXIT_SUCCESS; 81 1.1.1.2 christos err: 82 1.1 christos if (ret != EXIT_SUCCESS) { 83 1.1 christos fprintf(stderr, "Error Encrypting Data\n"); 84 1.1 christos ERR_print_errors_fp(stderr); 85 1.1 christos } 86 1.1 christos 87 1.1 christos CMS_ContentInfo_free(cms); 88 1.1 christos X509_free(rcert); 89 1.1 christos OSSL_STACK_OF_X509_free(recips); 90 1.1 christos BIO_free(in); 91 1.1 christos BIO_free(out); 92 1.1 christos BIO_free(dout); 93 1.1 christos BIO_free(tbio); 94 1.1 christos return ret; 95 1.1 christos } 96