xref: /netbsd-src/crypto/external/bsd/openssl/dist/demos/smime/smdec.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
2c7da899bSchristos  * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8c7da899bSchristos  */
9c7da899bSchristos 
10a89c9211Schristos /* Simple S/MIME signing example */
11a89c9211Schristos #include <openssl/pem.h>
12a89c9211Schristos #include <openssl/pkcs7.h>
13a89c9211Schristos #include <openssl/err.h>
14a89c9211Schristos 
main(int argc,char ** argv)15a89c9211Schristos int main(int argc, char **argv)
16a89c9211Schristos {
17a89c9211Schristos     BIO *in = NULL, *out = NULL, *tbio = NULL;
18a89c9211Schristos     X509 *rcert = NULL;
19a89c9211Schristos     EVP_PKEY *rkey = NULL;
20a89c9211Schristos     PKCS7 *p7 = NULL;
21a89c9211Schristos     int ret = 1;
22a89c9211Schristos 
23a89c9211Schristos     OpenSSL_add_all_algorithms();
24a89c9211Schristos     ERR_load_crypto_strings();
25a89c9211Schristos 
26a89c9211Schristos     /* Read in recipient certificate and private key */
27a89c9211Schristos     tbio = BIO_new_file("signer.pem", "r");
28a89c9211Schristos 
29a89c9211Schristos     if (!tbio)
30a89c9211Schristos         goto err;
31a89c9211Schristos 
32a89c9211Schristos     rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
33a89c9211Schristos 
34a89c9211Schristos     BIO_reset(tbio);
35a89c9211Schristos 
36a89c9211Schristos     rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
37a89c9211Schristos 
38a89c9211Schristos     if (!rcert || !rkey)
39a89c9211Schristos         goto err;
40a89c9211Schristos 
41a89c9211Schristos     /* Open content being signed */
42a89c9211Schristos 
43a89c9211Schristos     in = BIO_new_file("smencr.txt", "r");
44a89c9211Schristos 
45a89c9211Schristos     if (!in)
46a89c9211Schristos         goto err;
47a89c9211Schristos 
48a89c9211Schristos     /* Sign content */
49a89c9211Schristos     p7 = SMIME_read_PKCS7(in, NULL);
50a89c9211Schristos 
51a89c9211Schristos     if (!p7)
52a89c9211Schristos         goto err;
53a89c9211Schristos 
54a89c9211Schristos     out = BIO_new_file("encrout.txt", "w");
55a89c9211Schristos     if (!out)
56a89c9211Schristos         goto err;
57a89c9211Schristos 
58a89c9211Schristos     /* Decrypt S/MIME message */
59a89c9211Schristos     if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))
60a89c9211Schristos         goto err;
61a89c9211Schristos 
62a89c9211Schristos     ret = 0;
63a89c9211Schristos 
64a89c9211Schristos  err:
65635165faSspz     if (ret) {
66a89c9211Schristos         fprintf(stderr, "Error Signing Data\n");
67a89c9211Schristos         ERR_print_errors_fp(stderr);
68a89c9211Schristos     }
69a89c9211Schristos     PKCS7_free(p7);
70a89c9211Schristos     X509_free(rcert);
71a89c9211Schristos     EVP_PKEY_free(rkey);
72a89c9211Schristos     BIO_free(in);
73a89c9211Schristos     BIO_free(out);
74a89c9211Schristos     BIO_free(tbio);
75a89c9211Schristos 
76a89c9211Schristos     return ret;
77a89c9211Schristos 
78a89c9211Schristos }
79