VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/demos/cms/cms_enc.c@ 101665

Last change on this file since 101665 was 101211, checked in by vboxsync, 12 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

File size: 2.0 KB
Line 
1/*
2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10/* Simple S/MIME encrypt example */
11#include <openssl/pem.h>
12#include <openssl/cms.h>
13#include <openssl/err.h>
14
15int main(int argc, char **argv)
16{
17 BIO *in = NULL, *out = NULL, *tbio = NULL;
18 X509 *rcert = NULL;
19 STACK_OF(X509) *recips = NULL;
20 CMS_ContentInfo *cms = NULL;
21 int ret = 1;
22
23 /*
24 * On OpenSSL 1.0.0 and later only:
25 * for streaming set CMS_STREAM
26 */
27 int flags = CMS_STREAM;
28
29 OpenSSL_add_all_algorithms();
30 ERR_load_crypto_strings();
31
32 /* Read in recipient certificate */
33 tbio = BIO_new_file("signer.pem", "r");
34
35 if (!tbio)
36 goto err;
37
38 rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
39
40 if (!rcert)
41 goto err;
42
43 /* Create recipient STACK and add recipient cert to it */
44 recips = sk_X509_new_null();
45
46 if (!recips || !sk_X509_push(recips, rcert))
47 goto err;
48
49 /*
50 * sk_X509_pop_free will free up recipient STACK and its contents so set
51 * rcert to NULL so it isn't freed up twice.
52 */
53 rcert = NULL;
54
55 /* Open content being encrypted */
56
57 in = BIO_new_file("encr.txt", "r");
58
59 if (!in)
60 goto err;
61
62 /* encrypt content */
63 cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
64
65 if (!cms)
66 goto err;
67
68 out = BIO_new_file("smencr.txt", "w");
69 if (!out)
70 goto err;
71
72 /* Write out S/MIME message */
73 if (!SMIME_write_CMS(out, cms, in, flags))
74 goto err;
75
76 ret = 0;
77
78 err:
79
80 if (ret) {
81 fprintf(stderr, "Error Encrypting Data\n");
82 ERR_print_errors_fp(stderr);
83 }
84
85 CMS_ContentInfo_free(cms);
86 X509_free(rcert);
87 sk_X509_pop_free(recips, X509_free);
88 BIO_free(in);
89 BIO_free(out);
90 BIO_free(tbio);
91 return ret;
92}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette