2013-05-10 2 views
1

안녕하세요. mycert.crt 및 root.crt와 같은 두 개의 인증서가 있습니다. 내 인증서가 루트 인증서로 서명 된 경우 verfiy해야합니다. 다음 코드를 사용하여이를 수행하려면 다음 코드를 사용하고 있지만 오류 분할 오류 (코어 덤프)인증서 읽기 및 확인

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()의 생각이다를 얻고 있지만, 나는 확실하지 않다.

+0

디버거에서 실행하는 데 방해가 되셨습니까? 그리고 파일로드에'X509 * d2i_X509_fp (FILE * fp, X509 ** x)'를 사용하지 않는 특별한 이유가 있습니까? – WhozCraig

+0

X509_verify에 전달하기 전에 root 및 mycert의 값을 확인 했습니까? 유효한 인증서 여야합니다. – doptimusprime

+0

d2i 기능의 경우 인증서의 형식은 DER 여야합니다. 인증서 파일의 형식이 DER인지 확인하십시오. 이 것이 아닌 경우 DER로 변환하십시오. – doptimusprime

답변

2

함수의 목적은 실제로 두 번째 매개 변수로 전달해야한다 무엇

는 공개 키 있습니다 .. serverCert는 해당 개인 키로 서명으로 PKEY (공개 키)를 확인하면 확인하는 것입니다 해당 개인 키가 parameter1에 전달 된 인증서에 서명했습니다. 나는 당신이 올바른 공개 키를 전달하고 있다고 생각하지 않는다.

을 사용하여 오류에 대한 오류 코드를 가져 오십시오.

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)); 
+0

네, 맞습니다. X509_verify()는 인증서 자체의 일관성을 확인하는 데 사용됩니다. 나는. 인증서 데이터의 서명이 인증서 자신의 공개 키와 일치합니까? OP는 다른 기능이 필요한 인증서의 체인을 확인하려고합니다. – jcoffland