2011-05-27 186 views
0

我知道這將是一個巨大的職位,但我想提出一個問題,我面臨的基本上是給它的所有細節。SSL證書驗證編程

背景 我有其觸發的Firefox抓取URL數據並呈現所有組件的各個組件載入時間在網頁(例如螢火蟲)的應用程序。但是,應用程序不會自動驗證ssl證書(即,如果存在錯誤的證書,因爲沒有用戶手動接受/拒絕證書並且全部以編程方式完成,它會卡住)。我需要通過在firefox進程啓動之前驗證站點的證書來解決這個問題。

我的解決方案

,我發現這個位C代碼,做SSL證書的驗證編程在C.我給它的一個簡要概述。這是main()方法:

SSL_library_init(); 
ERR_load_BIO_strings(); 
SSL_load_error_strings(); 
OpenSSL_add_all_algorithms(); 

/* Set up the SSL context */ 
ctx = SSL_CTX_new(SSLv23_client_method()); 

/* Load the trust store - in this case, it's just a single 
* certificate that has been created for testing purposes. 
*/ 

if(! SSL_CTX_load_verify_locations(ctx,"certificate.pem",NULL)) 
{ 
    fprintf(stderr, "Error loading trust store\n"); 
    //ERR_print_errors_fp(stderr); 
    SSL_CTX_free(ctx); 
    return 0; 
} 

/* Setup the connection */ 
bio = BIO_new_ssl_connect(ctx); 

/* Set the SSL_MODE_AUTO_RETRY flag */ 

BIO_get_ssl(bio, & ssl); 
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 

/* Create and setup the connection */ 

BIO_set_conn_hostname(bio, "mail.google.com:https"); 

fprintf(stderr, "Connecting to host ...\n"); 

if(BIO_do_connect(bio) <= 0) 
{ 
    fprintf(stderr, "Error attempting to connect: %d\n",BIO_do_connect(bio)); 
    //ERR_print_errors_fp(stderr); 
    BIO_free_all(bio); 
    SSL_CTX_free(ctx); 
    return 0; 
} 
/* Retrieve the peer certificate */ 

fprintf(stderr, "Retrieving peer certificate\n"); 
if(getPeerCert(ssl, & peerCert) != X509_V_OK) 
{ 
    /* Can be changed to better handle a suspect certificate. However, 
    * for the purposes of this demonstration, we're aborting. 
    */ 
    fprintf(stderr, "Certificate verification error: %i\n",SSL_get_verify_result(ssl)); 
    BIO_free_all(bio); 
    SSL_CTX_free(ctx); 
    return 0; 
} 

我離開了getPeerCert(),因爲它得到了同行和證書使用OpenSSL的方法驗證方法的defenition。

另外certificate.pem是一個pem文件,通過按照解決方案的步驟獲得this的問題。

但是當我嘗試運行此我得到

Connecting to host ... 
Retrieving peer certificate 
Certificate verification error: 20 

我無法明白爲什麼發生這種情況的核查應該成功。我會很感激並樂意得到任何幫助。提前致謝。

問候, 哈日

更新1

我試着用開放SSL命令,並試圖從代碼中調用命令即

opensssl verify -CAfile ./ca-bundle.crt cert1... 

不過,我發現,它證明了內部和外部證書,它似乎也驗證了實際上應該是壞的證書(內部)(特別是壞域證書)。我非常感謝對此的任何見解。提前致謝。

+1

你是否努力使兩個連接?首先用你的應用程序來驗證證書,然後用Firefox?在這種情況下,從技術上講,您並不知道Firefox是否提供了相同的證書,因此驗證第一個證書沒有多大意義。我不太確定你爲什麼要使用Firefox來獲取你的頁面。這裏有很多HTTP客戶端庫,支持SSL/TLS,這樣做更合適。 – Bruno 2011-05-27 19:13:29

+0

嗨Firefox的部分是模擬什麼真正的用戶有/無主緩存將得到看到。我不確定我是否理解你關於不同的生態環境的問題。我首先驗證了這一點,如果失敗,我根本沒有啓動firefox。 – Hari 2011-05-27 19:21:04

+0

我的真正擔心,即使在獲得所需的證書後。這失敗了,我不知道爲什麼 – Hari 2011-05-27 19:33:46

回答

1

specific error你越來越是

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. 

嘗試把Gmail的發佈者,而不是Gmail的證書,到certificate.pem。

此外,請確保您瞭解布魯諾對您的問題的第一條評論。

+0

謝謝。這聽起來很愚蠢。但我不是這方面的專家。即使在發行人證書後,我收到此錯誤。我不知道! – Hari 2011-05-27 20:50:55

+0

你是否得到相同的錯誤,或不同的? – Jumbogram 2011-05-28 13:02:08

+0

我收到了同樣的錯誤 – Hari 2011-05-31 04:41:04