]>
Commit | Line | Data |
---|---|---|
9e200689 | 1 | /* |
da1c088f | 2 | * Copyright 2008-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 | ||
0f113f3e MC |
10 | /* |
11 | * S/MIME detached data encrypt example: rarely done but should the need | |
12 | * arise this is an example.... | |
17287562 DSH |
13 | */ |
14 | #include <openssl/pem.h> | |
15 | #include <openssl/cms.h> | |
16 | #include <openssl/err.h> | |
17 | ||
18 | int main(int argc, char **argv) | |
0f113f3e MC |
19 | { |
20 | BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL; | |
21 | X509 *rcert = NULL; | |
22 | STACK_OF(X509) *recips = NULL; | |
23 | CMS_ContentInfo *cms = NULL; | |
09ff84bd | 24 | int ret = EXIT_FAILURE; |
17287562 | 25 | |
0f113f3e | 26 | int flags = CMS_STREAM | CMS_DETACHED; |
17287562 | 27 | |
0f113f3e MC |
28 | OpenSSL_add_all_algorithms(); |
29 | ERR_load_crypto_strings(); | |
17287562 | 30 | |
0f113f3e MC |
31 | /* Read in recipient certificate */ |
32 | tbio = BIO_new_file("signer.pem", "r"); | |
17287562 | 33 | |
0f113f3e MC |
34 | if (!tbio) |
35 | goto err; | |
17287562 | 36 | |
0f113f3e | 37 | rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); |
17287562 | 38 | |
0f113f3e MC |
39 | if (!rcert) |
40 | goto err; | |
17287562 | 41 | |
0f113f3e MC |
42 | /* Create recipient STACK and add recipient cert to it */ |
43 | recips = sk_X509_new_null(); | |
17287562 | 44 | |
0f113f3e MC |
45 | if (!recips || !sk_X509_push(recips, rcert)) |
46 | goto err; | |
17287562 | 47 | |
0f113f3e | 48 | /* |
79b2a2f2 DDO |
49 | * OSSL_STACK_OF_X509_free() free up recipient STACK and its contents |
50 | * so set rcert to NULL so it isn't freed up twice. | |
0f113f3e MC |
51 | */ |
52 | rcert = NULL; | |
17287562 | 53 | |
0f113f3e | 54 | /* Open content being encrypted */ |
17287562 | 55 | |
0f113f3e | 56 | in = BIO_new_file("encr.txt", "r"); |
17287562 | 57 | |
0f113f3e | 58 | dout = BIO_new_file("smencr.out", "wb"); |
17287562 | 59 | |
0f113f3e MC |
60 | if (!in) |
61 | goto err; | |
17287562 | 62 | |
0f113f3e MC |
63 | /* encrypt content */ |
64 | cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); | |
17287562 | 65 | |
0f113f3e MC |
66 | if (!cms) |
67 | goto err; | |
17287562 | 68 | |
0f113f3e MC |
69 | out = BIO_new_file("smencr.pem", "w"); |
70 | if (!out) | |
71 | goto err; | |
17287562 | 72 | |
0f113f3e MC |
73 | if (!CMS_final(cms, in, dout, flags)) |
74 | goto err; | |
17287562 | 75 | |
0f113f3e MC |
76 | /* Write out CMS structure without content */ |
77 | if (!PEM_write_bio_CMS(out, cms)) | |
78 | goto err; | |
17287562 | 79 | |
09ff84bd | 80 | ret = EXIT_SUCCESS; |
0f113f3e | 81 | err: |
09ff84bd | 82 | if (ret != EXIT_SUCCESS) { |
0f113f3e MC |
83 | fprintf(stderr, "Error Encrypting Data\n"); |
84 | ERR_print_errors_fp(stderr); | |
85 | } | |
17287562 | 86 | |
25aaa98a | 87 | CMS_ContentInfo_free(cms); |
222561fe | 88 | X509_free(rcert); |
79b2a2f2 | 89 | OSSL_STACK_OF_X509_free(recips); |
ca3a82c3 RS |
90 | BIO_free(in); |
91 | BIO_free(out); | |
92 | BIO_free(dout); | |
93 | BIO_free(tbio); | |
0f113f3e | 94 | return ret; |
0f113f3e | 95 | } |