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