如何使用openssl从PKCS7.p7b证书文件读取证书文件
How to Read the certificates file from the PKCS7.p7b certificate file using openssl?
我正在获取PKCS7文件(p7b)。我想读取文件的内容并提取X509结构中的证书。
如何使用openssl库从PKCS容器访问单个证书?
我使用了以下程序:
#include <stdio.h>
#include <openssl/pkcs7.h>
#include <openssl/x509.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
int main(int argc, char **argv)
{
PKCS7 *p7 = NULL;
BIO *in = BIO_new(BIO_s_file());
BIO *out = BIO_new(BIO_s_file());
int der = 0; /* Input from DER or PEM ? */
int text = 0; /* Dump text or output PEM ? */
STACK_OF(X509) *certs = NULL;
int i;
CRYPTO_malloc_init();
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
BIO_set_fp(out, stdout, BIO_NOCLOSE);
BIO_read_filename(in, argv[1]);
p7 = der ?
d2i_PKCS7_bio(in, NULL) :
PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
i = OBJ_obj2nid(p7->type);
if(i == NID_pkcs7_signed) {
certs = p7->d.sign->cert;
} else if(i == NID_pkcs7_signedAndEnveloped) {
certs = p7->d.signed_and_enveloped->cert;
}
for (i = 0; certs && i < sk_X509_num(certs); i++) {
X509 *x = sk_X509_value(certs,i);
if(text) {
X509_print(out, x);
} else {
PEM_write_bio_X509(out,x);
}
}
}
它基于openssl-1.0.0d/apps/pkcs7.c
,并且很容易用编译(在Linux或Mac OS X上)(前提是您将其保存为readp7.c):
gcc -o readp7 readp7.c -lcrypto
您可以使用openssl创建文件,并像这样读取:
openssl crl2pkcs7 -nocrl -certfile a.crt -certfile b.crt -out test.p7b
./readp7 test.p7b
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- C++项目需要什么才能使用证书 (RSA-SHA256) 对文件进行签名
- 如何在 c++ 中从证书文件导出指纹
- 将证书文件复制到其他位置
- 如何以编程方式从签名文件安装数字证书
- 从可执行文件中读取并验证证书
- 如何使用openssl从PKCS7.p7b证书文件读取证书文件