file_get_contents:无法设置本地证书链文件

时间:2017-07-20 07:28:47

标签: php ssl zend-framework composer-php satis

我们正在使用composer来升级与Satis的依赖关系。在最近的服务器升级后,我们无法这样做。我们发现,缩小可能的原因,在尝试建立ssl连接时,file_get_contents php函数失败。

我们使用以下脚本来测试我们的ssl:

<?php
$url = 'https://satis.work.com/packages.json';
$contextOptions = [
    'ssl' => [
        'verify_peer'      => false,
        'verify_peer_name' => false,
        'local_cert'       => '/home/work/.ssl/deployer.pem',
    ]
];
$sslContext = stream_context_create($contextOptions);
$result = file_get_contents($url, false, $sslContext);
echo $result, "\n"; 

抛出:

  

PHP警告:file_get_contents():无法设置本地证书链文件`/home/work/.ssl/deployer.pem';在第12行的/home/omlook/test-ssl.php中检查您的cafile / capath设置是否包含证书及其颁发者的详细信息   PHP警告:file_get_contents():无法在第12行的/home/work/test-ssl.php中启用加密   PHP警告:file_get_contents(https://satis.work.com/packages.json):无法打开流:第12行/home/work/test-ssl.php中的操作失败

这绝对不是权利或文件所有权的问题,脚本可以读取.pem就好了。令人困惑的是,完全相同的脚本和.pem键如何在我的本地环境中正常工作,并且看起来版本差异并不显着。

当地环境:

  

PHP 7.0.18-0ubuntu0.16.04.1(cli)(NTS)   版权所有(c)1997-2017 PHP小组   Zend Engine v3.0.0,版权所有(c)1998-2017 Zend Technologies       与Zend OPcache v7.0.18-0ubuntu0.16.04.1,版权所有(c)1999-2017,作者:Zend Technologies

     

OpenSSL 1.0.2g 2016年3月1日

服务器:

  

PHP 7.1.7-1 + ubuntu14.04.1 + deb.sury.org + 1(cli)(建于:2017年7月7日10:07:42)(NTS)   版权所有(c)1997-2017 PHP小组   Zend Engine v3.1.0,版权所有(c)1998-2017 Zend Technologies       与Zend OPcache v7.1.7-1 + ubuntu14.04.1 + deb.sury.org + 1,版权所有(c)1999-2017,作者:Zend Technologies

     

OpenSSL 1.1.0f 2017年5月25日

2 个答案:

答案 0 :(得分:0)

只有在PEM文件中的“BEGIN CERTIFICATE”部分之前省略了明文元数据(Issuer,Validity等)时,才会发生这种情况。对于较新的PHP版本(包括7.0),这部分似乎现在是强制性的。到目前为止我还没有发现更多,但我的猜测是,这是一个开放性问题。 更新证书,包括openssl生成的元数据部分,为我解决了问题。

答案 1 :(得分:0)

我为我解决了同样的问题。看来,明文元数据并不重要。 类似的代码在使用openssl 1.1.0j的php 7.0上对我有用,并在使用openssl 1.1.1c的php 7.3上被破坏了-我有相同的错误文本。 添加明文元数据对我没有帮助。在当前的ca证书中添加cafile上下文参数对我也没有帮助。

当我尝试使用curl发出相同的请求时,出现错误:

curl -k --cert cert.pem https://myservice.com/soap/ShopService/
curl: (58) could not load PEM client certificate, OpenSSL error error:140AB18E:SSL routines:SSL_CTX_use_certificate:ca md too weak, (no key found, wrong pass phrase, or wrong file format?)

因此,我发现现有的旧客户端证书已使用弱sha1WithRsaEncryption算法签名。 私钥为2048位长度-没关系(如果您有1024位-现在不安全,还需要一个新的更长密钥)

我重新发布了具有实际sha256哈希值的客户证书(openssl选项-sha256)。我的CA证书具有相同的弱哈希值sha1,但不必更改它,只需更改客户端证书即可。命令:

# here: 
# cert.pem - my old client certificate with private key
# ca.pem - service's current CA certificate for signing client certificates with it's private key
# cert2.pem - my new working client certificate with the same old private key
#
# make new certificate request from current client certificate
openssl x509 -x509toreq -in cert.pem -out cert2.csr -signkey cert.pem -sha256

# make new certificate
openssl x509 -req -in cert2.csr -out cert2.pem -CA ca.pem -sha256 -days 730 -set_serial 0x51ca170d

# append private key
openssl rsa -in cert.pem >> cert2.pem

数小时的痛苦,现在可以了) 似乎php为此错误发送了错误的错误消息。

相关问题