]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/cms/cms_enc.c
2 * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Simple S/MIME encrypt example */
11 #include <openssl/pem.h>
12 #include <openssl/cms.h>
13 #include <openssl/err.h>
15 int main(int argc
, char **argv
)
17 BIO
*in
= NULL
, *out
= NULL
, *tbio
= NULL
;
19 STACK_OF(X509
) *recips
= NULL
;
20 CMS_ContentInfo
*cms
= NULL
;
21 int ret
= EXIT_FAILURE
;
24 * On OpenSSL 1.0.0 and later only:
25 * for streaming set CMS_STREAM
27 int flags
= CMS_STREAM
;
29 OpenSSL_add_all_algorithms();
30 ERR_load_crypto_strings();
32 /* Read in recipient certificate */
33 tbio
= BIO_new_file("signer.pem", "r");
38 rcert
= PEM_read_bio_X509(tbio
, NULL
, 0, NULL
);
43 /* Create recipient STACK and add recipient cert to it */
44 recips
= sk_X509_new_null();
46 if (!recips
|| !sk_X509_push(recips
, rcert
))
50 * OSSL_STACK_OF_X509_free() will free up recipient STACK and its contents
51 * so set rcert to NULL so it isn't freed up twice.
55 /* Open content being encrypted */
57 in
= BIO_new_file("encr.txt", "r");
63 cms
= CMS_encrypt(recips
, in
, EVP_des_ede3_cbc(), flags
);
68 out
= BIO_new_file("smencr.txt", "w");
72 /* Write out S/MIME message */
73 if (!SMIME_write_CMS(out
, cms
, in
, flags
))
78 if (ret
!= EXIT_SUCCESS
) {
79 fprintf(stderr
, "Error Encrypting Data\n");
80 ERR_print_errors_fp(stderr
);
83 CMS_ContentInfo_free(cms
);
85 OSSL_STACK_OF_X509_free(recips
);