1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2007-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 /* S/MIME signing example: 2 signers. OpenSSL 0.9.9 only */
11*e0c4386eSCy Schubert #include <openssl/pem.h>
12*e0c4386eSCy Schubert #include <openssl/pkcs7.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, *scert2 = NULL;
19*e0c4386eSCy Schubert EVP_PKEY *skey = NULL, *skey2 = NULL;
20*e0c4386eSCy Schubert PKCS7 *p7 = NULL;
21*e0c4386eSCy Schubert int ret = 1;
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert OpenSSL_add_all_algorithms();
24*e0c4386eSCy Schubert ERR_load_crypto_strings();
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert tbio = BIO_new_file("signer.pem", "r");
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert if (!tbio)
29*e0c4386eSCy Schubert goto err;
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
32*e0c4386eSCy Schubert
33*e0c4386eSCy Schubert BIO_reset(tbio);
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert BIO_free(tbio);
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert tbio = BIO_new_file("signer2.pem", "r");
40*e0c4386eSCy Schubert
41*e0c4386eSCy Schubert if (!tbio)
42*e0c4386eSCy Schubert goto err;
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubert scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubert BIO_reset(tbio);
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
49*e0c4386eSCy Schubert
50*e0c4386eSCy Schubert if (!scert2 || !skey2)
51*e0c4386eSCy Schubert goto err;
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert in = BIO_new_file("sign.txt", "r");
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert if (!in)
56*e0c4386eSCy Schubert goto err;
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
59*e0c4386eSCy Schubert
60*e0c4386eSCy Schubert if (!p7)
61*e0c4386eSCy Schubert goto err;
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubert /* Add each signer in turn */
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
66*e0c4386eSCy Schubert goto err;
67*e0c4386eSCy Schubert
68*e0c4386eSCy Schubert if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
69*e0c4386eSCy Schubert goto err;
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert out = BIO_new_file("smout.txt", "w");
72*e0c4386eSCy Schubert if (!out)
73*e0c4386eSCy Schubert goto err;
74*e0c4386eSCy Schubert
75*e0c4386eSCy Schubert /* NB: content included and finalized by SMIME_write_PKCS7 */
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
78*e0c4386eSCy Schubert goto err;
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert ret = 0;
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert err:
83*e0c4386eSCy Schubert if (ret) {
84*e0c4386eSCy Schubert fprintf(stderr, "Error Signing Data\n");
85*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert PKCS7_free(p7);
88*e0c4386eSCy Schubert X509_free(scert);
89*e0c4386eSCy Schubert EVP_PKEY_free(skey);
90*e0c4386eSCy Schubert X509_free(scert2);
91*e0c4386eSCy Schubert EVP_PKEY_free(skey2);
92*e0c4386eSCy Schubert BIO_free(in);
93*e0c4386eSCy Schubert BIO_free(out);
94*e0c4386eSCy Schubert BIO_free(tbio);
95*e0c4386eSCy Schubert return ret;
96*e0c4386eSCy Schubert }
97