openssl3.2 - 官方demo学习 - smime - smver.c
概述
对于签名文件(不管是单独签名, 还是联合签名), 都要用顶层证书进行验签(靠近根CA的证书)文章来源:https://www.toymoban.com/news/detail-804744.html
读证书文件, 得到x509*, 添加到证书容器
读取签名密文, 得到pkcs7*和密文的bio
进行pkcs7验签, 并将验签得到的签名的明文写到文件.文章来源地址https://www.toymoban.com/news/detail-804744.html
笔记
/*!
\file smver.c
\note
openssl3.2 - 官方demo学习 - smime - smver.c
对于签名文件(不管是单独签名, 还是联合签名), 都要用顶层证书进行验签(靠近根CA的证书)
读证书文件, 得到x509*, 添加到证书容器
读取签名密文, 得到pkcs7*和密文的bio
进行pkcs7验签, 并将验签得到的签名的明文写到文件.
*/
/*
* Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/* Simple S/MIME verification example */
#include <openssl/pem.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
#include "my_openSSL_lib.h"
int main(int argc, char **argv)
{
BIO *_bio_in = NULL, *_bio_out = NULL, *_bio_t = NULL, *_bio_c = NULL;
X509_STORE *_x509_store = NULL;
X509 *_x509 = NULL;
PKCS7 *_pkcs7 = NULL;
int ret = EXIT_FAILURE;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Set up trusted CA certificate store */
_x509_store = X509_STORE_new();
if (_x509_store == NULL)
goto err;
/* Read in signer certificate and private key */
_bio_t = BIO_new_file("cacert.pem", "r");
if (_bio_t == NULL)
goto err;
_x509 = PEM_read_bio_X509(_bio_t, NULL, 0, NULL);
if (_x509 == NULL)
goto err;
if (!X509_STORE_add_cert(_x509_store, _x509))
goto err;
/* Open content being signed */
_bio_in = BIO_new_file("smout.txt", "r");
if (_bio_in == NULL)
goto err;
/* Sign content */
_pkcs7 = SMIME_read_PKCS7(_bio_in, &_bio_c);
if (_pkcs7 == NULL)
goto err;
/* File to output verified content to */
_bio_out = BIO_new_file("smver.txt", "w");
if (_bio_out == NULL)
goto err;
if (!PKCS7_verify(_pkcs7, NULL, _x509_store, _bio_c, _bio_out, 0)) {
fprintf(stderr, "Verification Failure\n");
goto err;
}
fprintf(stderr, "Verification Successful\n");
ret = EXIT_SUCCESS;
err:
if (ret != EXIT_SUCCESS) {
fprintf(stderr, "Error Verifying Data\n");
ERR_print_errors_fp(stderr);
}
X509_STORE_free(_x509_store);
PKCS7_free(_pkcs7);
X509_free(_x509);
BIO_free(_bio_in);
BIO_free(_bio_out);
BIO_free(_bio_t);
return ret;
}
END
到了这里,关于openssl3.2 - 官方demo学习 - smime - smver.c的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!