簡體   English   中英

通過 C++ 從 PKCS7 (CMS) 獲取詳細信息

[英]Get details from PKCS7 (CMS) via C++

我有target.cert文件,想要獲取證書 endDate 和 startDate 等詳細信息

openssl pkcs7 -in target.cert -inform DER -print_certs -out cert_pem

openssl x509 -in cert.pem -enddate -startdate -noout

和 output 是開始和結束日期,並且想要做同樣的事情,但來自 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++ 獲取“開始/結束日期”?

開始和結束日期適用於您之前從 PKCS#7 結構中提取的 X.509 證書。 所以你必須對你的命令行做同樣的事情:

  1. 提取證書;
  2. 通過檢索開始/到期日期來獲取有效期。

PKCS#7 是一種容器格式,可能只包含證書。 但是,PKCS#7不是證書,就像 cookie jar 不是 cookie 一樣,即使它只包含一個 cookie。 因此,您的容器名稱target.cert的選擇非常糟糕,甚至連您自己也覺得不妥。 通常我們使用擴展名.p7.pkcs7來代替。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM