]> git.ipfire.org Git - thirdparty/openssl.git/blame - demos/smime/smsign2.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / demos / smime / smsign2.c
CommitLineData
9e200689 1/*
da1c088f 2 * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
9e200689 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
86db9588 10/* S/MIME signing example: 2 signers */
b2b2dafc
DSH
11#include <openssl/pem.h>
12#include <openssl/pkcs7.h>
13#include <openssl/err.h>
14
15int 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 PKCS7 *p7 = NULL;
09ff84bd 21 int ret = EXIT_FAILURE;
b2b2dafc 22
0f113f3e
MC
23 OpenSSL_add_all_algorithms();
24 ERR_load_crypto_strings();
b2b2dafc 25
0f113f3e 26 tbio = BIO_new_file("signer.pem", "r");
b2b2dafc 27
0f113f3e
MC
28 if (!tbio)
29 goto err;
b2b2dafc 30
0f113f3e 31 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 32
86db9588
JM
33 if (BIO_reset(tbio) < 0)
34 goto err;
b2b2dafc 35
0f113f3e 36 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
b2b2dafc 37
0f113f3e 38 BIO_free(tbio);
b2b2dafc 39
0f113f3e 40 tbio = BIO_new_file("signer2.pem", "r");
b2b2dafc 41
0f113f3e
MC
42 if (!tbio)
43 goto err;
b2b2dafc 44
0f113f3e 45 scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
b2b2dafc 46
86db9588
JM
47 if (BIO_reset(tbio) < 0)
48 goto err;
b2b2dafc 49
0f113f3e 50 skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
b2b2dafc 51
0f113f3e
MC
52 if (!scert2 || !skey2)
53 goto err;
b2b2dafc 54
0f113f3e 55 in = BIO_new_file("sign.txt", "r");
b2b2dafc 56
0f113f3e
MC
57 if (!in)
58 goto err;
b2b2dafc 59
0f113f3e 60 p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
b2b2dafc 61
0f113f3e
MC
62 if (!p7)
63 goto err;
b2b2dafc 64
0f113f3e 65 /* Add each signer in turn */
b2b2dafc 66
0f113f3e
MC
67 if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
68 goto err;
b2b2dafc 69
0f113f3e
MC
70 if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
71 goto err;
b2b2dafc 72
0f113f3e
MC
73 out = BIO_new_file("smout.txt", "w");
74 if (!out)
75 goto err;
b2b2dafc 76
0f113f3e 77 /* NB: content included and finalized by SMIME_write_PKCS7 */
b2b2dafc 78
0f113f3e
MC
79 if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
80 goto err;
b2b2dafc 81
86db9588 82 printf("Success\n");
b2b2dafc 83
86db9588 84 ret = EXIT_SUCCESS;
0f113f3e 85 err:
09ff84bd 86 if (ret != EXIT_SUCCESS) {
0f113f3e
MC
87 fprintf(stderr, "Error Signing Data\n");
88 ERR_print_errors_fp(stderr);
89 }
e0e920b1 90 PKCS7_free(p7);
222561fe 91 X509_free(scert);
c5ba2d99 92 EVP_PKEY_free(skey);
222561fe 93 X509_free(scert2);
c5ba2d99 94 EVP_PKEY_free(skey2);
ca3a82c3
RS
95 BIO_free(in);
96 BIO_free(out);
97 BIO_free(tbio);
0f113f3e 98 return ret;
0f113f3e 99}