2012-05-18 450 views
2

我使用這個代碼:SSL證書驗證失敗:使用OpenSSL的

void check_cert(SSL* ssl) 
{ 

//ssl initiation 
SSL_library_init(); 
SSL_load_error_strings(); 
const SSL_METHOD *meth; 
meth = SSLv3_method(); 
SSL_CTX *ctx; 
SSL *_ssl; 
ctx = SSL_CTX_new(meth); 
int result = 0; 
//getting the CA certificate 

result = SSL_CTX_load_verify_locations(ctx1, "cacert.pem", NULL); 
//result = SSL_CTX_load_verify_locations(ctx, NULL, "/home/cdac/Desktop/test/cert"); 
printf("\nCA load result = %d", result); 

//_ssl = SSL_new(ctx); 
SSL_CTX_set_verify(ctx1, SSL_VERIFY_PEER, NULL); 

SSL_CTX_set_verify_depth(ctx1, 1); 
int result_long = SSL_get_verify_result(ssl); 
printf("\nCertificate Check Result: %d", result_long); 
if (SSL_get_verify_result(ssl) != X509_V_OK) 
    { 
     printf("\nCertiticate Verification Failed\n"); 
     //exit(1); 
    } 
    else 
    { 
     printf("\nCertiticate Verification Succeeded"); 
    } 
} 

cacert.pem是CA證書。 當我執行此我得到這個:

CA load result = 1 
Certificate Check Result: 20 
Certiticate Verification Failed 

代碼20級是指:

X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: unable to get local issuer certificate 

the issuer certificate could not be found: this occurs if the issuer certificate of an untrusted certificate cannot be found. 

因此,誰能幫助我?這在哪裏失敗?

CA證書加載正常。 (加載結果返回1)。

+0

這是一個自簽名證書嗎? – Lazylabs

回答

1

如果您從對端獲得根CA並且根CA未加載到您身邊,則可能會發生這種情況。

相關問題