Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/demos/cms/cms_ddec.c
34879 views
1
/*
2
* Copyright 2008-2023 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
/*
11
* S/MIME detached data decrypt example: rarely done but should the need
12
* arise this is an example....
13
*/
14
#include <openssl/pem.h>
15
#include <openssl/cms.h>
16
#include <openssl/err.h>
17
18
int main(int argc, char **argv)
19
{
20
BIO *in = NULL, *out = NULL, *tbio = NULL, *dcont = NULL;
21
X509 *rcert = NULL;
22
EVP_PKEY *rkey = NULL;
23
CMS_ContentInfo *cms = NULL;
24
int ret = EXIT_FAILURE;
25
26
OpenSSL_add_all_algorithms();
27
ERR_load_crypto_strings();
28
29
/* Read in recipient certificate and private key */
30
tbio = BIO_new_file("signer.pem", "r");
31
32
if (!tbio)
33
goto err;
34
35
rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
36
37
if (BIO_reset(tbio) < 0)
38
goto err;
39
40
rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
41
42
if (!rcert || !rkey)
43
goto err;
44
45
/* Open PEM file containing enveloped data */
46
47
in = BIO_new_file("smencr.pem", "r");
48
49
if (!in)
50
goto err;
51
52
/* Parse PEM content */
53
cms = PEM_read_bio_CMS(in, NULL, 0, NULL);
54
55
if (!cms)
56
goto err;
57
58
/* Open file containing detached content */
59
dcont = BIO_new_file("smencr.out", "rb");
60
61
if (!in)
62
goto err;
63
64
out = BIO_new_file("encrout.txt", "w");
65
if (!out)
66
goto err;
67
68
/* Decrypt S/MIME message */
69
if (!CMS_decrypt(cms, rkey, rcert, dcont, out, 0))
70
goto err;
71
72
ret = EXIT_SUCCESS;
73
74
err:
75
76
if (ret != EXIT_SUCCESS) {
77
fprintf(stderr, "Error Decrypting Data\n");
78
ERR_print_errors_fp(stderr);
79
}
80
81
CMS_ContentInfo_free(cms);
82
X509_free(rcert);
83
EVP_PKEY_free(rkey);
84
BIO_free(in);
85
BIO_free(out);
86
BIO_free(tbio);
87
BIO_free(dcont);
88
return ret;
89
}
90
91