2 * S/MIME detached data encrypt example: rarely done but should the need
3 * arise this is an example....
5 #include <openssl/pem.h>
6 #include <openssl/cms.h>
7 #include <openssl/err.h>
9 int main(int argc
, char **argv
)
11 BIO
*in
= NULL
, *out
= NULL
, *tbio
= NULL
, *dout
= NULL
;
13 STACK_OF(X509
) *recips
= NULL
;
14 CMS_ContentInfo
*cms
= NULL
;
17 int flags
= CMS_STREAM
| CMS_DETACHED
;
19 OpenSSL_add_all_algorithms();
20 ERR_load_crypto_strings();
22 /* Read in recipient certificate */
23 tbio
= BIO_new_file("signer.pem", "r");
28 rcert
= PEM_read_bio_X509(tbio
, NULL
, 0, NULL
);
33 /* Create recipient STACK and add recipient cert to it */
34 recips
= sk_X509_new_null();
36 if (!recips
|| !sk_X509_push(recips
, rcert
))
40 * sk_X509_pop_free will free up recipient STACK and its contents so set
41 * rcert to NULL so it isn't freed up twice.
45 /* Open content being encrypted */
47 in
= BIO_new_file("encr.txt", "r");
49 dout
= BIO_new_file("smencr.out", "wb");
55 cms
= CMS_encrypt(recips
, in
, EVP_des_ede3_cbc(), flags
);
60 out
= BIO_new_file("smencr.pem", "w");
64 if (!CMS_final(cms
, in
, dout
, flags
))
67 /* Write out CMS structure without content */
68 if (!PEM_write_bio_CMS(out
, cms
))
76 fprintf(stderr
, "Error Encrypting Data\n");
77 ERR_print_errors_fp(stderr
);
81 CMS_ContentInfo_free(cms
);
85 sk_X509_pop_free(recips
, X509_free
);