2011-10-04 195 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(默认值)。