1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /* Simple S/MIME signing example */
11*e0c4386eSCy Schubert #include <openssl/pem.h>
12*e0c4386eSCy Schubert #include <openssl/cms.h>
13*e0c4386eSCy Schubert #include <openssl/err.h>
14*e0c4386eSCy Schubert
main(int argc,char ** argv)15*e0c4386eSCy Schubert int main(int argc, char **argv)
16*e0c4386eSCy Schubert {
17*e0c4386eSCy Schubert BIO *in = NULL, *out = NULL, *tbio = NULL;
18*e0c4386eSCy Schubert X509 *scert = NULL;
19*e0c4386eSCy Schubert EVP_PKEY *skey = NULL;
20*e0c4386eSCy Schubert CMS_ContentInfo *cms = NULL;
21*e0c4386eSCy Schubert int ret = 1;
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert /*
24*e0c4386eSCy Schubert * For simple S/MIME signing use CMS_DETACHED. On OpenSSL 1.0.0 only: for
25*e0c4386eSCy Schubert * streaming detached set CMS_DETACHED|CMS_STREAM for streaming
26*e0c4386eSCy Schubert * non-detached set CMS_STREAM
27*e0c4386eSCy Schubert */
28*e0c4386eSCy Schubert int flags = CMS_DETACHED | CMS_STREAM;
29*e0c4386eSCy Schubert
30*e0c4386eSCy Schubert OpenSSL_add_all_algorithms();
31*e0c4386eSCy Schubert ERR_load_crypto_strings();
32*e0c4386eSCy Schubert
33*e0c4386eSCy Schubert /* Read in signer certificate and private key */
34*e0c4386eSCy Schubert tbio = BIO_new_file("signer.pem", "r");
35*e0c4386eSCy Schubert
36*e0c4386eSCy Schubert if (!tbio)
37*e0c4386eSCy Schubert goto err;
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
40*e0c4386eSCy Schubert
41*e0c4386eSCy Schubert BIO_reset(tbio);
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert if (!scert || !skey)
46*e0c4386eSCy Schubert goto err;
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert /* Open content being signed */
49*e0c4386eSCy Schubert
50*e0c4386eSCy Schubert in = BIO_new_file("sign.txt", "r");
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert if (!in)
53*e0c4386eSCy Schubert goto err;
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert /* Sign content */
56*e0c4386eSCy Schubert cms = CMS_sign(scert, skey, NULL, in, flags);
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert if (!cms)
59*e0c4386eSCy Schubert goto err;
60*e0c4386eSCy Schubert
61*e0c4386eSCy Schubert out = BIO_new_file("smout.txt", "w");
62*e0c4386eSCy Schubert if (!out)
63*e0c4386eSCy Schubert goto err;
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert if (!(flags & CMS_STREAM))
66*e0c4386eSCy Schubert BIO_reset(in);
67*e0c4386eSCy Schubert
68*e0c4386eSCy Schubert /* Write out S/MIME message */
69*e0c4386eSCy Schubert if (!SMIME_write_CMS(out, cms, in, flags))
70*e0c4386eSCy Schubert goto err;
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubert ret = 0;
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert err:
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert if (ret) {
77*e0c4386eSCy Schubert fprintf(stderr, "Error Signing Data\n");
78*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
79*e0c4386eSCy Schubert }
80*e0c4386eSCy Schubert
81*e0c4386eSCy Schubert CMS_ContentInfo_free(cms);
82*e0c4386eSCy Schubert X509_free(scert);
83*e0c4386eSCy Schubert EVP_PKEY_free(skey);
84*e0c4386eSCy Schubert BIO_free(in);
85*e0c4386eSCy Schubert BIO_free(out);
86*e0c4386eSCy Schubert BIO_free(tbio);
87*e0c4386eSCy Schubert return ret;
88*e0c4386eSCy Schubert }
89