Path: blob/main/crypto/openssl/demos/cms/cms_sign2.c
34879 views
/*1* Copyright 2008-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/cms.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;19CMS_ContentInfo *cms = 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;5859cms = CMS_sign(NULL, NULL, NULL, in, CMS_STREAM | CMS_PARTIAL);6061if (!cms)62goto err;6364/* Add each signer in turn */6566if (!CMS_add1_signer(cms, scert, skey, NULL, 0))67goto err;6869if (!CMS_add1_signer(cms, 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_CMS */7778if (!SMIME_write_CMS(out, cms, in, CMS_STREAM))79goto err;8081printf("Signing Successful\n");8283ret = EXIT_SUCCESS;84err:85if (ret != EXIT_SUCCESS) {86fprintf(stderr, "Error Signing Data\n");87ERR_print_errors_fp(stderr);88}8990CMS_ContentInfo_free(cms);91X509_free(scert);92EVP_PKEY_free(skey);93X509_free(scert2);94EVP_PKEY_free(skey2);95BIO_free(in);96BIO_free(out);97BIO_free(tbio);98return ret;99}100101102