]>
Commit | Line | Data |
---|---|---|
9e200689 | 1 | /* |
da1c088f | 2 | * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. |
9e200689 | 3 | * |
5e73e6ba | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
9e200689 RS |
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 | |
8 | */ | |
9 | ||
b2b2dafc DSH |
10 | /* Simple S/MIME encrypt example */ |
11 | #include <openssl/pem.h> | |
12 | #include <openssl/pkcs7.h> | |
13 | #include <openssl/err.h> | |
14 | ||
15 | int main(int argc, char **argv) | |
0f113f3e MC |
16 | { |
17 | BIO *in = NULL, *out = NULL, *tbio = NULL; | |
18 | X509 *rcert = NULL; | |
19 | STACK_OF(X509) *recips = NULL; | |
20 | PKCS7 *p7 = NULL; | |
09ff84bd | 21 | int ret = EXIT_FAILURE; |
b2b2dafc | 22 | |
0f113f3e MC |
23 | /* |
24 | * On OpenSSL 0.9.9 only: | |
25 | * for streaming set PKCS7_STREAM | |
26 | */ | |
27 | int flags = PKCS7_STREAM; | |
b2b2dafc | 28 | |
0f113f3e MC |
29 | OpenSSL_add_all_algorithms(); |
30 | ERR_load_crypto_strings(); | |
b2b2dafc | 31 | |
0f113f3e MC |
32 | /* Read in recipient certificate */ |
33 | tbio = BIO_new_file("signer.pem", "r"); | |
b2b2dafc | 34 | |
0f113f3e MC |
35 | if (!tbio) |
36 | goto err; | |
b2b2dafc | 37 | |
0f113f3e | 38 | rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); |
b2b2dafc | 39 | |
0f113f3e MC |
40 | if (!rcert) |
41 | goto err; | |
b2b2dafc | 42 | |
0f113f3e MC |
43 | /* Create recipient STACK and add recipient cert to it */ |
44 | recips = sk_X509_new_null(); | |
b2b2dafc | 45 | |
0f113f3e MC |
46 | if (!recips || !sk_X509_push(recips, rcert)) |
47 | goto err; | |
b2b2dafc | 48 | |
0f113f3e | 49 | /* |
79b2a2f2 DDO |
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. | |
0f113f3e MC |
52 | */ |
53 | rcert = NULL; | |
b2b2dafc | 54 | |
0f113f3e | 55 | /* Open content being encrypted */ |
b2b2dafc | 56 | |
0f113f3e | 57 | in = BIO_new_file("encr.txt", "r"); |
b2b2dafc | 58 | |
0f113f3e MC |
59 | if (!in) |
60 | goto err; | |
b2b2dafc | 61 | |
0f113f3e MC |
62 | /* encrypt content */ |
63 | p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags); | |
b2b2dafc | 64 | |
0f113f3e MC |
65 | if (!p7) |
66 | goto err; | |
b2b2dafc | 67 | |
0f113f3e MC |
68 | out = BIO_new_file("smencr.txt", "w"); |
69 | if (!out) | |
70 | goto err; | |
b2b2dafc | 71 | |
0f113f3e MC |
72 | /* Write out S/MIME message */ |
73 | if (!SMIME_write_PKCS7(out, p7, in, flags)) | |
74 | goto err; | |
b2b2dafc | 75 | |
09ff84bd | 76 | ret = EXIT_SUCCESS; |
b2b2dafc | 77 | |
0f113f3e | 78 | err: |
09ff84bd | 79 | if (ret != EXIT_SUCCESS) { |
0f113f3e MC |
80 | fprintf(stderr, "Error Encrypting Data\n"); |
81 | ERR_print_errors_fp(stderr); | |
82 | } | |
e0e920b1 | 83 | PKCS7_free(p7); |
222561fe | 84 | X509_free(rcert); |
79b2a2f2 | 85 | OSSL_STACK_OF_X509_free(recips); |
ca3a82c3 RS |
86 | BIO_free(in); |
87 | BIO_free(out); | |
88 | BIO_free(tbio); | |
0f113f3e | 89 | return ret; |
b2b2dafc | 90 | |
0f113f3e | 91 | } |