blob: ba0adb3974bea95449116a69665106833cd0c569 [file] [log] [blame]
Rich Salz9e200682016-05-18 09:16:36 -04001/*
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
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000010/* 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)
Matt Caswell0f113f32015-01-22 03:40:55 +000016{
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;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000022
Matt Caswell0f113f32015-01-22 03:40:55 +000023 /*
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;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000029
Matt Caswell0f113f32015-01-22 03:40:55 +000030 OpenSSL_add_all_algorithms();
31 ERR_load_crypto_strings();
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000032
Matt Caswell0f113f32015-01-22 03:40:55 +000033 /* Read in signer certificate and private key */
34 tbio = BIO_new_file("signer.pem", "r");
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000035
Matt Caswell0f113f32015-01-22 03:40:55 +000036 if (!tbio)
37 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000038
Matt Caswell0f113f32015-01-22 03:40:55 +000039 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000040
Matt Caswell0f113f32015-01-22 03:40:55 +000041 BIO_reset(tbio);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000042
Matt Caswell0f113f32015-01-22 03:40:55 +000043 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000044
Matt Caswell0f113f32015-01-22 03:40:55 +000045 if (!scert || !skey)
46 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000047
Matt Caswell0f113f32015-01-22 03:40:55 +000048 /* Open content being signed */
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000049
Matt Caswell0f113f32015-01-22 03:40:55 +000050 in = BIO_new_file("sign.txt", "r");
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000051
Matt Caswell0f113f32015-01-22 03:40:55 +000052 if (!in)
53 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000054
Matt Caswell0f113f32015-01-22 03:40:55 +000055 /* Sign content */
56 p7 = PKCS7_sign(scert, skey, NULL, in, flags);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000057
Matt Caswell0f113f32015-01-22 03:40:55 +000058 if (!p7)
59 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000060
Matt Caswell0f113f32015-01-22 03:40:55 +000061 out = BIO_new_file("smout.txt", "w");
62 if (!out)
63 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000064
Matt Caswell0f113f32015-01-22 03:40:55 +000065 if (!(flags & PKCS7_STREAM))
66 BIO_reset(in);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000067
Matt Caswell0f113f32015-01-22 03:40:55 +000068 /* Write out S/MIME message */
69 if (!SMIME_write_PKCS7(out, p7, in, flags))
70 goto err;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000071
Matt Caswell0f113f32015-01-22 03:40:55 +000072 ret = 0;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000073
Matt Caswell0f113f32015-01-22 03:40:55 +000074 err:
Matt Caswell0f113f32015-01-22 03:40:55 +000075 if (ret) {
76 fprintf(stderr, "Error Signing Data\n");
77 ERR_print_errors_fp(stderr);
78 }
Rich Salze0e920b2015-04-11 16:32:54 -040079 PKCS7_free(p7);
Rich Salz222561f2015-04-30 17:33:59 -040080 X509_free(scert);
Rich Salzc5ba2d92015-03-28 10:54:15 -040081 EVP_PKEY_free(skey);
Rich Salzca3a82c2015-03-25 11:31:18 -040082 BIO_free(in);
83 BIO_free(out);
84 BIO_free(tbio);
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000085
Matt Caswell0f113f32015-01-22 03:40:55 +000086 return ret;
Dr. Stephen Hensonb2b2daf2007-04-13 20:40:47 +000087
Matt Caswell0f113f32015-01-22 03:40:55 +000088}