Path: blob/main/crypto/openssl/demos/smime/smsign2.c
34889 views
/*1* Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* S/MIME signing example: 2 signers */10#include <openssl/pem.h>11#include <openssl/pkcs7.h>12#include <openssl/err.h>1314int main(int argc, char **argv)15{16BIO *in = NULL, *out = NULL, *tbio = NULL;17X509 *scert = NULL, *scert2 = NULL;18EVP_PKEY *skey = NULL, *skey2 = NULL;19PKCS7 *p7 = NULL;20int ret = EXIT_FAILURE;2122OpenSSL_add_all_algorithms();23ERR_load_crypto_strings();2425tbio = BIO_new_file("signer.pem", "r");2627if (!tbio)28goto err;2930scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);3132if (BIO_reset(tbio) < 0)33goto err;3435skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);3637BIO_free(tbio);3839tbio = BIO_new_file("signer2.pem", "r");4041if (!tbio)42goto err;4344scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);4546if (BIO_reset(tbio) < 0)47goto err;4849skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);5051if (!scert2 || !skey2)52goto err;5354in = BIO_new_file("sign.txt", "r");5556if (!in)57goto err;5859p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);6061if (!p7)62goto err;6364/* Add each signer in turn */6566if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))67goto err;6869if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))70goto err;7172out = BIO_new_file("smout.txt", "w");73if (!out)74goto err;7576/* NB: content included and finalized by SMIME_write_PKCS7 */7778if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))79goto err;8081printf("Success\n");8283ret = EXIT_SUCCESS;84err:85if (ret != EXIT_SUCCESS) {86fprintf(stderr, "Error Signing Data\n");87ERR_print_errors_fp(stderr);88}89PKCS7_free(p7);90X509_free(scert);91EVP_PKEY_free(skey);92X509_free(scert2);93EVP_PKEY_free(skey2);94BIO_free(in);95BIO_free(out);96BIO_free(tbio);97return ret;98}99100101