【发布时间】:2013-11-05 10:57:13
【问题描述】:
在我向 API 发出的第二次请求时,我会收到此错误!?
API 的后端是我自己的服务器之一,我自己设置了自签名 SSL 证书
这里发生了什么!?它不能是 SSL 证书,因为它在某些情况下有效
Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in
API 请求代码
$Request = new Request();
$Request->host = $host;
$Request->api_secret = 'asdf39Sf3D';
$Request->send($url, $params);
echo $Request->get_result();
class Request {
public $host;
public $api_secret;
public $boundary;
public $body;
private $response;
private $url;
const SSL = true;
public function send($url, $post_vars=array()){
$this->url = $url;
$crlf = "\r\n";
$host = $this->host;
$port = 80;
if(self::SSL){
$host = 'ssl://'.$this->host;
$port = 443;
}
if($this->body){
$body = $this->body;
}
else{
$post_vars['__api_hash'] = $this->generate_hash($this->url);
$body = http_build_query($post_vars);
}
$content_length = strlen($body);
$max_post = 1024 * 1024 * 20;
if($content_length > $max_post){
throw new Exception("Max post size exceeded");
}
if($fp = fsockopen($host, $port, $errno, $errstr, 20)){
fwrite($fp, 'POST '.substr($this->url, strlen($this->host)).' HTTP/1.1'.$crlf
.'Host: '.$this->host.$crlf
.($this->body ? 'Content-type: multipart/form-data; boundary='.$this->boundary : 'Content-Type: application/x-www-form-urlencoded').$crlf
.'Content-Length: '.$content_length.$crlf
.'Connection: Close'.$crlf.$crlf
.$body);
while($line = fgets($fp)){
if($line !== false){
$this->response .= $line;
}
}
fclose($fp);
}
else{
throw new Exception("$errstr ($errno)");
}
}
public function get_response(){
return $this->response;
}
public function get_result(){
list($header, $content) = explode("\n\n", str_replace("\r\n", "\n", $this->response));
preg_match('/^HTTP\/[\d\.]+ (\d+)/', $header, $matches);
switch($matches[1]){
case 404:
throw new Exception('HTTP 404 '.$this->url);
}
return json_decode($content, true);
}
public function generate_hash(){
return sha1($this->url.$this->api_secret);
}
}
【问题讨论】:
-
我在这里做了一个模糊的猜测...... 2009 年有一个广为人知的 SSL/TLS renegotiation issue。您可能会看到添加代码以防止不安全的重新协商的结果。您可以尝试使用 TLS 而不是 SSL - 请参阅 stackoverflow.com/questions/3153206/…
-
已尝试改用
tls://协议,但在大约一半的请求中仍然出现相同的错误,而后半部分响应没有任何问题.. 嗯 -
在您连接的服务器上,正在使用哪个版本的 OpenSSL?如果对通信的一侧进行了修补以修复 不安全的重新协商 问题,那么这也可能导致您看到的错误。双方都需要有补丁版本的 SSL 或都没有补丁;好吧,我使用 Java 客户端和服务器就是这种情况,尽管有 2 个标志可以在 Java 中用于在必要时覆盖它。从OpenSSL changelog看来,您至少需要
v0.9.8m -
phpinfo()返回此OpenSSL 0.9.8u 12 Mar 2012但无法检查 Apache 上已安装的版本.. 在提示符中运行openssl /?时返回此错误The ordinal 320 could not be located in the dynamic link library SSLEA32.dll.. Wamp 安装在 Windows 上 -
@andyb,创建一个答案 :) 在下载最新版本的 openSSL 并在
C:\wamp\bin\apache\apache2.2.22\bin中替换libeay32.dll、openssl.exe和ssleay32.dll后,它可以工作了!! :D