读取和验证证书
Reading and Verifying Certificates
你好,我有两个证书,即mycert。CRT和root.crt。我需要验证我的证书是否由根证书签名。我使用以下代码使用以下代码这样做,但我得到一个错误段错误(core dump)
static int verifyCerti (BYTE *cert1, BYTE *cert2, int certlenght1, int certlenght2);
int main (int ac, char **av)
{
FILE *f_in, *f_in2;
BYTE *certBuf, *certBuf2;
UINT32 certBufLen,certBufLen2;
UINT32 certLen,certLen2;
int result;
//////////// Reading first certificate/////
certBufLen = 0;
certBuf = malloc (1);
//for (i=0; i<nCerts; i++) {
if ((f_in = fopen (av[1], "rb")) == NULL) {
fprintf (stderr, "Unable to open file %s for inputn", av[1]);
exit (1);
}
fseek (f_in, 0, SEEK_END);
certLen = ftell (f_in);
fseek (f_in, 0, SEEK_SET);
certBuf = realloc (certBuf, certBufLen + certLen);
if (fread (certBuf+certBufLen, 1, certLen, f_in) != certLen) {
fprintf (stderr, "Failed to read file %sn", av[1]);
exit (1);
}
if (certBuf[certBufLen] != 0x30) {
fprintf (stderr, "Certificate file %s not in binary formatn", av[1]);
exit (1);
}
fclose (f_in);
printf ("we reach here %s n", av[1]);
////////////////Reading second certificate/////////////////////////////////////////////////
certBufLen2 = 0;
certBuf2 = malloc (1);
if ((f_in2 = fopen (av[2], "rb")) == NULL) {
fprintf (stderr, "Unable to open file %s for inputn", av[2]);
exit (1);
}
fseek (f_in2, 0, SEEK_END);
certLen2 = ftell (f_in2);
fseek (f_in2, 0, SEEK_SET);
certBuf2 = realloc (certBuf2, certBufLen2 + certLen2);
if (fread (certBuf2+certBufLen2, 1, certLen2, f_in2) != certLen2) {
fprintf (stderr, "Failed to read file %sn", av[2]);
exit (1);
}
if (certBuf2[certBufLen2] != 0x30) {
fprintf (stderr, "Certificate file %s not in binary formatn", av[2]);
exit (1);
}
fclose (f_in2);
printf ("we reach here %s n", av[2]);
if (verifyCerti (certBuf, certBuf2, certBufLen, certBufLen2) < 0) {
fprintf (stderr, "Certificate chain is incorrectn");
exit (1);
}
}
static int verifyCerti (BYTE *cert1, BYTE *cert2, int certLen1, int certLen2)
{
X509 *root;
X509 *mycert;
root = d2i_X509 (NULL, (unsigned char const **)&cert2, certLen2);
mycert = d2i_X509 (NULL, (unsigned char const **)&cert1, certLen1);
//Get root certificate into root
//Get mycert into mycert.
//Get the public key.
EVP_PKEY *pubkey = X509_get_pubkey(root);
//verify. result less than or 0 means not verified or some error.
int result = X509_verify(mycert, pubkey);
//free the public key.
EVP_PKEY_free(pubkey);
return result;
}
错误是我认为因为X509_verify(),但我不确定。
该函数的目的是验证pkey(公钥)是否与用相应私钥签名的serverCert进行了验证。
实际上应该作为第二个参数传递的是公钥,其对应的私钥已经对parameter1中传递的证书进行了签名。我认为你没有传递正确的公钥。
尝试使用。
获取失败的错误代码。unsigned int errCode = ERR_get_error();
printf("nError: %sn", ERR_error_string(errCode, NULL));
printf("nLib: %sn", ERR_lib_error_string(errCode));
printf("nFunc: %sn", ERR_func_error_string(errCode));
printf("nReason: %sn", ERR_reason_error_string(errCode));
相关文章:
- 如何使用OPENSL使用OCSP验证证书
- 如何使用相应的 X509 证书验证专用 RSA 签名签名
- 如何读取证书以使用 openssl 验证签名
- 使用 WinInet 的客户端身份验证(证书 + 私钥)
- 证书验证在客户端服务器通信中使用boost :: asio和openssl失败
- 如何验证视频卡驱动程序证书
- 验证 x509 证书
- 无法使用OpenSSL验证服务器证书
- 使用libcurl的CA证书身份验证
- Boost、asio、https 和主机/证书验证
- Casablanca C++Rest-如何禁用证书验证
- 获取POCO HTTPSClientSession发送请求时出错-证书验证失败
- 如何在Windows上禁用LDAP认证的证书验证
- 如何在C++中使用openssl创建证书验证的信任链
- 服务器证书验证失败.CAfile: ca.pem CRLfile:无
- Casablanca c++ Rest SDK -如何在websocket上禁用证书验证
- 使用SSL通配符证书验证主机
- OpenSSL在证书验证错误时自行终止
- 是否有任何库支持具有EMSA-PSS编码技术和证书验证的RSASSA-PSS.
- ssl证书验证上的多线程c++应用程序内存泄漏