]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/smime/smsign.c
Consolidate copyright for demos
[thirdparty/openssl.git] / demos / smime / smsign.c
CommitLineData
9e200689
RS
1/*
2 * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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
b2b2dafc
DSH
10/* Simple S/MIME signing example */
11#include <openssl/pem.h>
12#include <openssl/pkcs7.h>
13#include <openssl/err.h>
14
15int main(int argc, char **argv)
0f113f3e
MC
16{
17 BIO *in = NULL, *out = NULL, *tbio = NULL;
18 X509 *scert = NULL;
19 EVP_PKEY *skey = NULL;
20 PKCS7 *p7 = NULL;
21 int ret = 1;
b2b2dafc 22
0f113f3e
MC
23 /*
24 * For simple S/MIME signing use PKCS7_DETACHED. On OpenSSL 0.9.9 only:
25 * for streaming detached set PKCS7_DETACHED|PKCS7_STREAM for streaming
26 * non-detached set PKCS7_STREAM
27 */
28 int flags = PKCS7_DETACHED | PKCS7_STREAM;
b2b2dafc 29
0f113f3e
MC
30 OpenSSL_add_all_algorithms();
31 ERR_load_crypto_strings();
b2b2dafc 32
0f113f3e
MC
33 /* Read in signer certificate and private key */
34 tbio = BIO_new_file("signer.pem", "r");
b2b2dafc 35
0f113f3e
MC
36 if (!tbio)
37 goto err;
b2b2dafc 38
0f113f3e 39 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 40
0f113f3e 41 BIO_reset(tbio);
b2b2dafc 42
0f113f3e 43 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
b2b2dafc 44
0f113f3e
MC
45 if (!scert || !skey)
46 goto err;
b2b2dafc 47
0f113f3e 48 /* Open content being signed */
b2b2dafc 49
0f113f3e 50 in = BIO_new_file("sign.txt", "r");
b2b2dafc 51
0f113f3e
MC
52 if (!in)
53 goto err;
b2b2dafc 54
0f113f3e
MC
55 /* Sign content */
56 p7 = PKCS7_sign(scert, skey, NULL, in, flags);
b2b2dafc 57
0f113f3e
MC
58 if (!p7)
59 goto err;
b2b2dafc 60
0f113f3e
MC
61 out = BIO_new_file("smout.txt", "w");
62 if (!out)
63 goto err;
b2b2dafc 64
0f113f3e
MC
65 if (!(flags & PKCS7_STREAM))
66 BIO_reset(in);
b2b2dafc 67
0f113f3e
MC
68 /* Write out S/MIME message */
69 if (!SMIME_write_PKCS7(out, p7, in, flags))
70 goto err;
b2b2dafc 71
0f113f3e 72 ret = 0;
b2b2dafc 73
0f113f3e 74 err:
0f113f3e
MC
75 if (ret) {
76 fprintf(stderr, "Error Signing Data\n");
77 ERR_print_errors_fp(stderr);
78 }
e0e920b1 79 PKCS7_free(p7);
222561fe 80 X509_free(scert);
c5ba2d99 81 EVP_PKEY_free(skey);
ca3a82c3
RS
82 BIO_free(in);
83 BIO_free(out);
84 BIO_free(tbio);
b2b2dafc 85
0f113f3e 86 return ret;
b2b2dafc 87
0f113f3e 88}