c++ - 通过 C++ 从 PKCS7 (CMS) 获取详细信息
问题描述
我有target.cert
文件,想获取证书 endDate 和 startDate 等详细信息
openssl pkcs7 -in target.cert -inform DER -print_certs -out cert_pem
openssl x509 -in cert.pem -enddate -startdate -noout
并且输出是开始和结束日期,并且想要做同样的事情,但来自 C++ 代码。
FILE* fp;
if (!(fp = fopen("target.cert", "rb"))) {
fprintf(stderr, "Error reading input pkcs7 file\n" );
exit(1);
}
PKCS7 *p7;
p7 = d2i_PKCS7_fp(fp, NULL);
但p7
没有像“startDate”这样的字段或解析字段的能力。
如何通过 C++ 获取“开始/结束日期”?
解决方案
PKCS7 没有开始日期/结束日期。它里面的证书可以。您需要先提取它们并询问他们的日期。像这样的东西应该可以解决问题:
STACK_OF(X509) *certs = NULL;
// find out where the certs stack is located
int nid = OBJ_obj2nid(p7->type);
if(nid == NID_pkcs7_signed) {
certs = p7->d.sign->cert;
} else if(nid == NID_pkcs7_signedAndEnveloped) {
certs = p7->d.signed_and_enveloped->cert;
}
// go over all the certs in the chain (you can skip this and look only at the first
// certificate if you don't care for the root CA's certificate expiration date)
for (int i = 0; certs && i < sk_X509_num(certs); i++) {
X509 *cert = sk_X509_value(certs, i);
const ASN1_TIME *start = X509_get0_notBefore(cert);
const ASN1_TIME *end = X509_get0_notAfter(cert);
// do whatever you will with the dates
}
推荐阅读
- javascript - redux thunk 函数是如何运作的?当一个 redux thunk 函数被调用时会发生什么?
- javascript - 更新失败的 Knexjs
- azure - 适用于 Java 的 Azure Application Insights 代理导致无法在 JBoss 18 上找到或加载主类 %JAVA_OPTS
- c# - 如何比较 SpecFlow 中的多行参数?
- c# - 在c#中使用/不使用正则表达式清除不需要的十六进制字符
- c++ - 为什么在三元中返回静态大小的数组时 MSVC 2019 会失败?
- javascript - 如何使用 JavaScript 和 Github API 更新托管在 GitHub 存储库中的 JSON 文件
- google-api - API 密钥对谷歌云翻译无效
- sql - Azure SQL 数据库中的 SQL 作业中的语法错误
- python - django中的url可以传入一个空字符串吗?