]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/cms/cms_sign2.c
free NULL cleanup
[thirdparty/openssl.git] / demos / cms / cms_sign2.c
CommitLineData
f3eba36c 1/* S/MIME signing example: 2 signers */
3b28bc99
DSH
2#include <openssl/pem.h>
3#include <openssl/cms.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 CMS_ContentInfo *cms = NULL;
12 int ret = 1;
3b28bc99 13
0f113f3e
MC
14 OpenSSL_add_all_algorithms();
15 ERR_load_crypto_strings();
3b28bc99 16
0f113f3e 17 tbio = BIO_new_file("signer.pem", "r");
3b28bc99 18
0f113f3e
MC
19 if (!tbio)
20 goto err;
3b28bc99 21
0f113f3e 22 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
3b28bc99 23
0f113f3e 24 BIO_reset(tbio);
3b28bc99 25
0f113f3e 26 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
3b28bc99 27
0f113f3e 28 BIO_free(tbio);
3b28bc99 29
0f113f3e 30 tbio = BIO_new_file("signer2.pem", "r");
3b28bc99 31
0f113f3e
MC
32 if (!tbio)
33 goto err;
3b28bc99 34
0f113f3e 35 scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
3b28bc99 36
0f113f3e 37 BIO_reset(tbio);
3b28bc99 38
0f113f3e 39 skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
3b28bc99 40
0f113f3e
MC
41 if (!scert2 || !skey2)
42 goto err;
3b28bc99 43
0f113f3e 44 in = BIO_new_file("sign.txt", "r");
3b28bc99 45
0f113f3e
MC
46 if (!in)
47 goto err;
3b28bc99 48
0f113f3e 49 cms = CMS_sign(NULL, NULL, NULL, in, CMS_STREAM | CMS_PARTIAL);
3b28bc99 50
0f113f3e
MC
51 if (!cms)
52 goto err;
3b28bc99 53
0f113f3e 54 /* Add each signer in turn */
3b28bc99 55
0f113f3e
MC
56 if (!CMS_add1_signer(cms, scert, skey, NULL, 0))
57 goto err;
3b28bc99 58
0f113f3e
MC
59 if (!CMS_add1_signer(cms, scert2, skey2, NULL, 0))
60 goto err;
3b28bc99 61
0f113f3e
MC
62 out = BIO_new_file("smout.txt", "w");
63 if (!out)
64 goto err;
3b28bc99 65
0f113f3e 66 /* NB: content included and finalized by SMIME_write_CMS */
3b28bc99 67
0f113f3e
MC
68 if (!SMIME_write_CMS(out, cms, in, CMS_STREAM))
69 goto err;
3b28bc99 70
0f113f3e 71 ret = 0;
3b28bc99 72
0f113f3e 73 err:
3b28bc99 74
0f113f3e
MC
75 if (ret) {
76 fprintf(stderr, "Error Signing Data\n");
77 ERR_print_errors_fp(stderr);
78 }
3b28bc99 79
0f113f3e
MC
80 if (cms)
81 CMS_ContentInfo_free(cms);
3b28bc99 82
0f113f3e
MC
83 if (scert)
84 X509_free(scert);
c5ba2d99 85 EVP_PKEY_free(skey);
3b28bc99 86
0f113f3e
MC
87 if (scert2)
88 X509_free(scert2);
c5ba2d99 89 EVP_PKEY_free(skey2);
3b28bc99 90
ca3a82c3
RS
91 BIO_free(in);
92 BIO_free(out);
93 BIO_free(tbio);
3b28bc99 94
0f113f3e 95 return ret;
3b28bc99 96
0f113f3e 97}