etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / openssl / dist / demos / smime / smenc.c
blob18bf7b1618b79d9a1f9a6595cbf75ec4c38e6778
1 /* Simple S/MIME encrypt example */
2 #include <openssl/pem.h>
3 #include <openssl/pkcs7.h>
4 #include <openssl/err.h>
6 int main(int argc, char **argv)
8 BIO *in = NULL, *out = NULL, *tbio = NULL;
9 X509 *rcert = NULL;
10 STACK_OF(X509) *recips = NULL;
11 PKCS7 *p7 = NULL;
12 int ret = 1;
15 * On OpenSSL 0.9.9 only:
16 * for streaming set PKCS7_STREAM
18 int flags = PKCS7_STREAM;
20 OpenSSL_add_all_algorithms();
21 ERR_load_crypto_strings();
23 /* Read in recipient certificate */
24 tbio = BIO_new_file("signer.pem", "r");
26 if (!tbio)
27 goto err;
29 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
31 if (!rcert)
32 goto err;
34 /* Create recipient STACK and add recipient cert to it */
35 recips = sk_X509_new_null();
37 if (!recips || !sk_X509_push(recips, rcert))
38 goto err;
41 * sk_X509_pop_free will free up recipient STACK and its contents so set
42 * rcert to NULL so it isn't freed up twice.
44 rcert = NULL;
46 /* Open content being encrypted */
48 in = BIO_new_file("encr.txt", "r");
50 if (!in)
51 goto err;
53 /* encrypt content */
54 p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
56 if (!p7)
57 goto err;
59 out = BIO_new_file("smencr.txt", "w");
60 if (!out)
61 goto err;
63 /* Write out S/MIME message */
64 if (!SMIME_write_PKCS7(out, p7, in, flags))
65 goto err;
67 ret = 0;
69 err:
71 if (ret) {
72 fprintf(stderr, "Error Encrypting Data\n");
73 ERR_print_errors_fp(stderr);
76 if (p7)
77 PKCS7_free(p7);
78 if (rcert)
79 X509_free(rcert);
80 if (recips)
81 sk_X509_pop_free(recips, X509_free);
83 if (in)
84 BIO_free(in);
85 if (out)
86 BIO_free(out);
87 if (tbio)
88 BIO_free(tbio);
90 return ret;