2011-10-04 192 views
3

我需要創建一個PHP,它將充當客戶端並使用https下的一些Web服務。 我的問題是,我也想驗證服務器證書。我需要知道我有合適的服務器,並且沒有人擔任服務器。 有人可以幫我嗎?PHP客戶端驗證https證書

謝謝!

+0

好的一步,但沒有用於檢查中間人攻擊。谷歌的「Diginotar」,看看爲什麼整個SSL CA系統基本上被打破。 –

+3

@Marc B:在任何基本意義上都不會有任何不同意見,我想指出的是,如果注意保持可信CA的列表儘可能短,則可以在實踐中避免許多問題。例如,在這種情況下,除了您要連接的網站使用的特定CA之外,實際上不需要信任任何CA. –

回答

6

如果您有curl擴展名,它可以配置爲驗證連接上的證書。

http://php.net/manual/en/function.curl-setopt.php

// As of writing this, Twitter uses Verisign, Google uses Eqifax 
$exampleUrl = 'https://twitter.com/'; // Success 
$exampleUrl = 'https://google.com/'; // Fails 

// create a new CURL resource 
$ch = curl_init($exampleUrl); 

// enable verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 

// list of CAs to trust 
// If the remote site has a specific CA, they usually have a .crt 
// file on their site you can download. Or you can export key items from 
// some browsers. 
// In this example, using: Verisign [1] 
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/ca_bundle.crt'); 
// - or - 
curl_setopt($ch, CURLOPT_CAPATH, __DIR__ . '/ca_certs/); 

// If the remote site uses basic auth: 
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); 

// And a helpful option to enable while debugging 
//curl_setopt($ch, CURLOPT_VERBOSE, true); 

// defaults to stdout, don't want that for this case. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$page = curl_exec($ch); 

[1] http://www.verisign.com/support/verisign-intermediate-ca/extended-validation/apache/

+0

謝謝!對於愚蠢的問題感到抱歉。但我不知道如何測試Url或將其用作Web服務客戶端。我真的很新的PHP。 :) –

+0

如果服務器未提供由ca_bundle.crt中的一個CA簽名的有效證書,則連接將失敗 – rrehbein

+0

感謝您的幫助!但是,第一次和第二次通話都不會發出任何警告。當我做'var_dump($頁)'我得到布爾假。請幫助... :) 之後,我該如何調用服務器功能? (例如,像肥皂客戶端?) –

0

它看起來就像是捲曲7.10,這是所有設置現在默認檢查:
http://php.net/manual/en/function.curl-setopt.php


CURLOPT_SSL_VERIFYPEER

FALSE停止cURL驗證對等方的證書。可以使用CURLOPT_CAINFO選項指定要驗證的替代證書,也可以使用CURLOPT_CAPATH選項指定證書目錄。

默認爲TRUE,默認爲cURL 7.10。從cURL 7.10開始安裝默認軟件包。


CURLOPT_SSL_VERIFYHOST

1來檢查一個共同的名字的存在SSL對等證書中。 2檢查是否存在通用名稱,並驗證它是否與提供的主機名相匹配。在生產環境中,此選項的值應保持爲2(默認值)。