gpt4 book ai didi

c++ - 阅读和验证证书

转载 作者:行者123 更新时间:2023-11-30 04:18:20 26 4
gpt4 key购买 nike

您好,我有两个证书,即 mycert.crt 和 root.crt。我需要验证我的证书是否由根证书签名。我正在使用以下代码使用以下代码来执行此操作,但出现错误段错误(核心已转储)

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 input\n", 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 %s\n", av[1]);
exit (1);
}
if (certBuf[certBufLen] != 0x30) {
fprintf (stderr, "Certificate file %s not in binary format\n", 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 input\n", 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 %s\n", av[2]);
exit (1);
}
if (certBuf2[certBufLen2] != 0x30) {
fprintf (stderr, "Certificate file %s not in binary format\n", 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 incorrect\n");
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 验证..

您实际上应该作为第二个参数传递的是公钥,其对应的私钥已经签署了参数 1 中传递的证书。我认为您没有传递正确的公钥。

尝试获取失败的错误代码。

unsigned int errCode = ERR_get_error();

printf("\nError: %s\n", ERR_error_string(errCode, NULL));
printf("\nLib: %s\n", ERR_lib_error_string(errCode));
printf("\nFunc: %s\n", ERR_func_error_string(errCode));
printf("\nReason: %s\n", ERR_reason_error_string(errCode));

关于c++ - 阅读和验证证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com