2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Simple S/MIME signing example */
11 #include <openssl/pem.h>
12 #include <openssl/cms.h>
13 #include <openssl/err.h>
15 int main(int argc, char **argv)
17 BIO *in = NULL, *out = NULL, *tbio = NULL;
19 EVP_PKEY *skey = NULL;
20 CMS_ContentInfo *cms = NULL;
24 * For simple S/MIME signing use CMS_DETACHED. On OpenSSL 1.0.0 only: for
25 * streaming detached set CMS_DETACHED|CMS_STREAM for streaming
26 * non-detached set CMS_STREAM
28 int flags = CMS_DETACHED | CMS_STREAM;
30 OpenSSL_add_all_algorithms();
31 ERR_load_crypto_strings();
33 /* Read in signer certificate and private key */
34 tbio = BIO_new_file("signer.pem", "r");
39 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
43 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
48 /* Open content being signed */
50 in = BIO_new_file("sign.txt", "r");
56 cms = CMS_sign(scert, skey, NULL, in, flags);
61 out = BIO_new_file("smout.txt", "w");
65 if (!(flags & CMS_STREAM))
68 /* Write out S/MIME message */
69 if (!SMIME_write_CMS(out, cms, in, flags))
77 fprintf(stderr, "Error Signing Data\n");
78 ERR_print_errors_fp(stderr);
81 CMS_ContentInfo_free(cms);