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