我们有一个 PHP 网络应用程序,用于定期向第三方 API 发送数据。通信自然通过 SSL 完成,密码保护 key 保存在我们服务器本地并输入 curl。这是 curl 设置代码:
/**
* @return Http
*/
public function post($url, $content, array $headers = array(), $ssl = true) {
$h = curl_init($this->getUrl($url));
$opts = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $content,
CURLOPT_HTTPHEADER => $headers,
);
if ($ssl) {
$opts[CURLOPT_SSL_VERIFYHOST] = 2;
$opts[CURLOPT_SSL_VERIFYPEER] = true;
}
if ($this->certFile && $ssl) {
$opts[CURLOPT_SSLCERTTYPE] = $this->certType;
$opts[CURLOPT_SSLCERT] = $this->certFile;
if ($this->certKey) {
$opts[CURLOPT_SSLCERTPASSWD] = $this->certKey;
}
if ($this->sslKeyFile) {
$opts[CURLOPT_SSLKEY] = $this->sslKeyFile;
}
else if ($this->certCA) {
$opts[CURLOPT_CAINFO] = $this->certCA;
}
}
curl_setopt_array($h, $opts);
$this->handle = $h;
return $this;
}
Web 应用程序可能会尝试使用上述代码发出 10 个独立的同步请求。我们看到 curl vebose 日志记录返回以下错误的间歇性问题(为了安全而屏蔽主机名和 IP):
* About to connect() to *** port 443 (#59)
* Trying xxx.xxx.xxx.xxx...
* Connected to *** (xxx.xxx.xxx.xxx) port 443 (#59)
* CAfile: /path/to/ca.pem
CApath: none
* Unable to load client key: Incorrect password
* NSS error -8177 (SEC_ERROR_BAD_PASSWORD)
* The security password entered is incorrect.
* Closing connection 59
当我们设法建立成功的连接时,日志输出中会包含额外的一行:
* Initializing NSS with certpath: sql:/etc/pki/nssdb
这一行在所有失败的连接中都丢失了,看起来它可能包含一个线索。该路径在处理请求的服务器上可用且可访问。
提供给 curl 的密码是正确的,并且可以在命令行上毫无问题地打开 key 。
我们的服务器运行 CentOS Linux 版本 7.6.1810(核心),PHP 7.0.33 和 CURL 版本 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.36 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
有没有人见过这个?我们最初认为这是一个文件访问问题,因为我们的私钥存储在高可用性网络存储上。
最佳答案
我们有完全相同的问题。
一个特定的时刻 - 问题出在服务器上,运行 php-fastcgi
。
我们有 12 个 fastcgi
线程。
其中 2 个比其他 10 个晚开始。
这两个线程总是有 SEC_ERROR_BAD_PASSWORD
- 在每个请求上。
其他 10 个线程总是很好。
如果您也在使用 fastcgi,您可以通过添加以下内容来记录线程 pid:
$pid = getmypid();
到你的脚本,找出哪个线程坏了。
在我们的例子中,杀死那两个损坏的线程后,每个请求都正常了。
不知道为什么这两个线程被破坏了——也许它们是在配置错误的那一刻开始的。
关于php - 查找使用加密私钥curl连接第三方时出现间歇性 "Incorrect Password"错误的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55825606/