1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10c9496f6bSchristos /* S/MIME signing example: 2 signers */
11c9496f6bSchristos #include <openssl/pem.h>
12c9496f6bSchristos #include <openssl/cms.h>
13c9496f6bSchristos #include <openssl/err.h>
14c9496f6bSchristos
main(int argc,char ** argv)15c9496f6bSchristos int main(int argc, char **argv)
16c9496f6bSchristos {
17c9496f6bSchristos BIO *in = NULL, *out = NULL, *tbio = NULL;
18c9496f6bSchristos X509 *scert = NULL, *scert2 = NULL;
19c9496f6bSchristos EVP_PKEY *skey = NULL, *skey2 = NULL;
20c9496f6bSchristos CMS_ContentInfo *cms = NULL;
21c9496f6bSchristos int ret = 1;
22c9496f6bSchristos
23c9496f6bSchristos OpenSSL_add_all_algorithms();
24c9496f6bSchristos ERR_load_crypto_strings();
25c9496f6bSchristos
26c9496f6bSchristos tbio = BIO_new_file("signer.pem", "r");
27c9496f6bSchristos
28c9496f6bSchristos if (!tbio)
29c9496f6bSchristos goto err;
30c9496f6bSchristos
31c9496f6bSchristos scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
32c9496f6bSchristos
33c9496f6bSchristos BIO_reset(tbio);
34c9496f6bSchristos
35c9496f6bSchristos skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
36c9496f6bSchristos
37c9496f6bSchristos BIO_free(tbio);
38c9496f6bSchristos
39c9496f6bSchristos tbio = BIO_new_file("signer2.pem", "r");
40c9496f6bSchristos
41c9496f6bSchristos if (!tbio)
42c9496f6bSchristos goto err;
43c9496f6bSchristos
44c9496f6bSchristos scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
45c9496f6bSchristos
46c9496f6bSchristos BIO_reset(tbio);
47c9496f6bSchristos
48c9496f6bSchristos skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
49c9496f6bSchristos
50c9496f6bSchristos if (!scert2 || !skey2)
51c9496f6bSchristos goto err;
52c9496f6bSchristos
53c9496f6bSchristos in = BIO_new_file("sign.txt", "r");
54c9496f6bSchristos
55c9496f6bSchristos if (!in)
56c9496f6bSchristos goto err;
57c9496f6bSchristos
58c9496f6bSchristos cms = CMS_sign(NULL, NULL, NULL, in, CMS_STREAM | CMS_PARTIAL);
59c9496f6bSchristos
60c9496f6bSchristos if (!cms)
61c9496f6bSchristos goto err;
62c9496f6bSchristos
63c9496f6bSchristos /* Add each signer in turn */
64c9496f6bSchristos
65c9496f6bSchristos if (!CMS_add1_signer(cms, scert, skey, NULL, 0))
66c9496f6bSchristos goto err;
67c9496f6bSchristos
68c9496f6bSchristos if (!CMS_add1_signer(cms, scert2, skey2, NULL, 0))
69c9496f6bSchristos goto err;
70c9496f6bSchristos
71c9496f6bSchristos out = BIO_new_file("smout.txt", "w");
72c9496f6bSchristos if (!out)
73c9496f6bSchristos goto err;
74c9496f6bSchristos
75c9496f6bSchristos /* NB: content included and finalized by SMIME_write_CMS */
76c9496f6bSchristos
77c9496f6bSchristos if (!SMIME_write_CMS(out, cms, in, CMS_STREAM))
78c9496f6bSchristos goto err;
79c9496f6bSchristos
80c9496f6bSchristos ret = 0;
81c9496f6bSchristos
82c9496f6bSchristos err:
83c9496f6bSchristos
84c9496f6bSchristos if (ret) {
85c9496f6bSchristos fprintf(stderr, "Error Signing Data\n");
86c9496f6bSchristos ERR_print_errors_fp(stderr);
87c9496f6bSchristos }
88c9496f6bSchristos
89c9496f6bSchristos CMS_ContentInfo_free(cms);
90c9496f6bSchristos X509_free(scert);
91c9496f6bSchristos EVP_PKEY_free(skey);
92c9496f6bSchristos X509_free(scert2);
93c9496f6bSchristos EVP_PKEY_free(skey2);
94c9496f6bSchristos BIO_free(in);
95c9496f6bSchristos BIO_free(out);
96c9496f6bSchristos BIO_free(tbio);
97c9496f6bSchristos return ret;
98c9496f6bSchristos }
99