我確實擁有DER和PEM格式的證書,我的目標是檢索頒發者和Subject的字段,並使用CA公鑰驗證證書,同時使用根公鑰驗證CA證書。 我能夠檢索發行人和主題的所有細節,但無法驗證證書。
所使用的API:x509證書驗證C
x509 = d2i_X509_fp (fp, &x509); //READING DER Format
x509 = PEM_read_X509 (fp, &x509, NULL, NULL); //READING PEM Format
//to retrieve the Subject:
X509_NAME_oneline(X509_get_subject_name(x509), subject, sizeof (subject));
//to retrieve the Issuer:
X509_NAME_oneline(X509_get_issuer_name(x509), issuer, sizeof (issuer));
//To store the CA public key (in unsigned char *key) that will be used to verify the
//certificate (in my case always sha1WithRSAEncryption):
RSA *x = X509_get_pubkey(x509)->pkey.rsa;
bn = x->n;
//extracts the bytes from public key & convert into unsigned char buffer
buf_len = (size_t) BN_num_bytes (bn);
stored_CA_pubKey = (unsigned char *)malloc (buf_len);
i_n = BN_bn2bin (bn, (unsigned char *)stored_CA_pubKey);
if (i_n != buf_len)
LOG(ERROR," : key error\n");
if (key[0] & 0x80)
LOG(DEBUG, "00\n");
stored_CA_pubKeyLen = EVP_PKEY_size(X509_get_pubkey(x509));
爲了驗證我通過不同的方法去,但我無法驗證:
一)
i_x509_verify = X509_verify(cert_x509, ca_pubkey);
B)
/* verify the signature */
int iRet1, iRet2, iReason;
iRet1 = EVP_VerifyInit(&md_ctx, EVP_sha1());
iRet2 = EVP_VerifyUpdate(&md_ctx, cert_code, cert_code_len);
rv = EVP_VerifyFinal(&md_ctx, (const unsigned char *)stored_CA_pubKey,
stored_CA_pubKeyLen, cert_pubkey);
注意:cert_code和stored_CA_pubKey是無符號字符緩衝區。
在init證書存儲2行llookup被使用,但在第5行中聲明...該代碼如何工作..?請給出完整的代碼 – Balamurugan 2011-07-19 15:22:16
thx Balamurugan,我只複製相關的代碼片段,完整的代碼將複雜。 :) 查找的第二個init是ca文件存儲在散列目錄中的替代方法。 – pHagi 2013-12-09 10:02:46
您應該使用'X509_STORE_CTX_get_error(storeCtx)'而不是'storeCtx-> error';後者由於ABI變化而更脆弱。 – 2016-11-16 21:46:01