]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/smime/smsign2.c
free NULL cleanup 8
[thirdparty/openssl.git] / demos / smime / smsign2.c
CommitLineData
b2b2dafc
DSH
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>
5
6int main(int argc, char **argv)
0f113f3e
MC
7{
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;
b2b2dafc 13
0f113f3e
MC
14 OpenSSL_add_all_algorithms();
15 ERR_load_crypto_strings();
b2b2dafc 16
0f113f3e 17 tbio = BIO_new_file("signer.pem", "r");
b2b2dafc 18
0f113f3e
MC
19 if (!tbio)
20 goto err;
b2b2dafc 21
0f113f3e 22 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 23
0f113f3e 24 BIO_reset(tbio);
b2b2dafc 25
0f113f3e 26 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
b2b2dafc 27
0f113f3e 28 BIO_free(tbio);
b2b2dafc 29
0f113f3e 30 tbio = BIO_new_file("signer2.pem", "r");
b2b2dafc 31
0f113f3e
MC
32 if (!tbio)
33 goto err;
b2b2dafc 34
0f113f3e 35 scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 36
0f113f3e 37 BIO_reset(tbio);
b2b2dafc 38
0f113f3e 39 skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
b2b2dafc 40
0f113f3e
MC
41 if (!scert2 || !skey2)
42 goto err;
b2b2dafc 43
0f113f3e 44 in = BIO_new_file("sign.txt", "r");
b2b2dafc 45
0f113f3e
MC
46 if (!in)
47 goto err;
b2b2dafc 48
0f113f3e 49 p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
b2b2dafc 50
0f113f3e
MC
51 if (!p7)
52 goto err;
b2b2dafc 53
0f113f3e 54 /* Add each signer in turn */
b2b2dafc 55
0f113f3e
MC
56 if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
57 goto err;
b2b2dafc 58
0f113f3e
MC
59 if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
60 goto err;
b2b2dafc 61
0f113f3e
MC
62 out = BIO_new_file("smout.txt", "w");
63 if (!out)
64 goto err;
b2b2dafc 65
0f113f3e 66 /* NB: content included and finalized by SMIME_write_PKCS7 */
b2b2dafc 67
0f113f3e
MC
68 if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
69 goto err;
b2b2dafc 70
0f113f3e 71 ret = 0;
b2b2dafc 72
0f113f3e 73 err:
0f113f3e
MC
74 if (ret) {
75 fprintf(stderr, "Error Signing Data\n");
76 ERR_print_errors_fp(stderr);
77 }
e0e920b1 78 PKCS7_free(p7);
0f113f3e
MC
79 if (scert)
80 X509_free(scert);
c5ba2d99 81 EVP_PKEY_free(skey);
0f113f3e
MC
82 if (scert2)
83 X509_free(scert2);
c5ba2d99 84 EVP_PKEY_free(skey2);
ca3a82c3
RS
85 BIO_free(in);
86 BIO_free(out);
87 BIO_free(tbio);
0f113f3e 88 return ret;
0f113f3e 89}