Path: blob/main/crypto/openssl/demos/smime/smdec.c
34907 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/* Simple S/MIME signing example */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 *rcert = NULL;18EVP_PKEY *rkey = NULL;19PKCS7 *p7 = NULL;20int ret = EXIT_FAILURE;2122OpenSSL_add_all_algorithms();23ERR_load_crypto_strings();2425/* Read in recipient certificate and private key */26tbio = BIO_new_file("signer.pem", "r");2728if (!tbio)29goto err;3031rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);3233if (BIO_reset(tbio) < 0)34goto err;3536rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);3738if (!rcert || !rkey)39goto err;4041/* Open content being signed */4243in = BIO_new_file("smencr.txt", "r");4445if (!in)46goto err;4748/* Sign content */49p7 = SMIME_read_PKCS7(in, NULL);5051if (!p7)52goto err;5354out = BIO_new_file("encrout.txt", "w");55if (!out)56goto err;5758/* Decrypt S/MIME message */59if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))60goto err;6162printf("Success\n");6364ret = EXIT_SUCCESS;65err:66if (ret != EXIT_SUCCESS) {67fprintf(stderr, "Error Signing Data\n");68ERR_print_errors_fp(stderr);69}70PKCS7_free(p7);71X509_free(rcert);72EVP_PKEY_free(rkey);73BIO_free(in);74BIO_free(out);75BIO_free(tbio);7677return ret;78}798081