OpenSSL: update to 1.0.1m
[tomato.git] / release / src-rt-6.x.4708 / router / openssl / demos / smime / smsign2.c
bloba116094341ba9808e4cdd634835a69a12b3f3ce5
1 /* S/MIME signing example: 2 signers. OpenSSL 0.9.9 only */
2 #include <openssl/pem.h>
3 #include <openssl/pkcs7.h>
4 #include <openssl/err.h>
6 int main(int argc, char **argv)
8 BIO *in = NULL, *out = NULL, *tbio = NULL;
9 X509 *scert = NULL, *scert2 = NULL;
10 EVP_PKEY *skey = NULL, *skey2 = NULL;
11 PKCS7 *p7 = NULL;
12 int ret = 1;
14 OpenSSL_add_all_algorithms();
15 ERR_load_crypto_strings();
17 tbio = BIO_new_file("signer.pem", "r");
19 if (!tbio)
20 goto err;
22 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
24 BIO_reset(tbio);
26 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
28 BIO_free(tbio);
30 tbio = BIO_new_file("signer2.pem", "r");
32 if (!tbio)
33 goto err;
35 scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
37 BIO_reset(tbio);
39 skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
41 if (!scert2 || !skey2)
42 goto err;
44 in = BIO_new_file("sign.txt", "r");
46 if (!in)
47 goto err;
49 p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
51 if (!p7)
52 goto err;
54 /* Add each signer in turn */
56 if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
57 goto err;
59 if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
60 goto err;
62 out = BIO_new_file("smout.txt", "w");
63 if (!out)
64 goto err;
66 /* NB: content included and finalized by SMIME_write_PKCS7 */
68 if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
69 goto err;
71 ret = 0;
73 err:
75 if (ret) {
76 fprintf(stderr, "Error Signing Data\n");
77 ERR_print_errors_fp(stderr);
80 if (p7)
81 PKCS7_free(p7);
83 if (scert)
84 X509_free(scert);
85 if (skey)
86 EVP_PKEY_free(skey);
88 if (scert2)
89 X509_free(scert2);
90 if (skey)
91 EVP_PKEY_free(skey2);
93 if (in)
94 BIO_free(in);
95 if (out)
96 BIO_free(out);
97 if (tbio)
98 BIO_free(tbio);
100 return ret;