问题描述
我有一个检查 URL 可用性的 PHP 脚本(基本上,当 URL 可以在浏览器中打开时,脚本应该为给定的 URL 返回 true,反之亦然).我偶然发现了一个 URL:https://thepiratebay.gd/.这个 URL 可以在浏览器中正确打开,但 fsockopen() 只是失败并出现 SSL 握手错误.在 PHP 中调试 fsockopen() 的选项并不多,但是在深入研究它的同时,我发现我也无法连接到 https://thepiratebay.gd/ 使用控制台 openssl 客户端:
openssl s_client -connect thepiratebay.gd:443已连接(00000003)39613:错误:14077438:SSL 例程:SSL23_GET_SERVER_HELLO:tlsv1 警报内部错误:/SourceCache/OpenSSL098/OpenSSL098-50/src/ssl/s23_clnt.c:602:
该网站似乎可以使用 Web 浏览器或 curl 正常打开,但是,我无法找到通过 openssl 连接到它的方法.显然,服务器使用带有 ECDHE-ECDSA-AES128-GCM-SHA256 密码的 TLS 1.2,但即使我为 openssl 强制使用这些密码,它仍然失败:
openssl s_client -cipher ECDHE-ECDSA-AES128-GCM-SHA256 -connect thepiratebay.gd:443 -tls1_2已连接(00000003)140735195829088:错误:14094438:SSL 例程:SSL3_READ_BYTES:tlsv1 警报内部错误:s3_pkt.c:1256:SSL 警报编号 80140735195829088:错误:1409E0E5:SSL 例程:SSL3_WRITE_BYTES:ssl 握手失败:s3_pkt.c:596:---没有可用的对等证书---未发送客户端证书 CA 名称---SSL 握手已读取 7 个字节并写入 0 个字节---新的,(无),密码是(无)不支持安全重新协商压缩:无扩展:无SSL 会话:协议:TLSv1.2密码:0000会话 ID:会话 ID-ctx:主密钥:键参数:无PSK 身份:无PSK 身份提示:无SRP 用户名:无开始时间:1432931347超时:7200(秒)验证返回码:0(确定)---
我尝试过各种 openssl 版本:0.9.8y、1.0.1g,以及最新的 0.9.8zf 和 1.0.2a.我还尝试在至少 5 台服务器(CentOS、Debian、OSX)上运行它,但没有成功.
所有其他网站似乎都处理得很好,这是一个成功的握手输出示例:
openssl s_client -connect stackoverflow.com:443 -tls1已连接(00000003)depth=1/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 高保证服务器 CA验证错误:num=20:无法获得本地颁发者证书验证返回:0---证书链0 s:/C=US/ST=NY/L=纽约/O=Stack Exchange, Inc./CN=*.stackexchange.comi:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 高保证服务器 CA1 s:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 高保证服务器 CAi:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV Root CA---服务器证书-----开始认证-----MIIGajCCBVKgAwIBAgIQCn1PE//Ffo4Be8tPBlsAZDANBgkqhkiG9w0BAQsFADBwMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMS8wLQYDVQQDEyZEaWdpQ2VydCBTSEEyIEhpZ2ggQXNzdXJhbmNlIFNlcnZlciBDQTAeFw0xMzEwMjIxMjAwMDFaFw0xNjA3MDYxMjAwMDBaMGoxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOWTERMA8GA1UEBxMITmV3IFlvcmsx<---跳过了几行--->qOHCjaUIx7vKszN4cqbvyry/NdxYkPCC7S8Eks8NjSyppzRL79tU0Yr1MUhVEd6hGjB2qDwvAGqyWmLz1Q/l82lZbXyBF26DVTJ3RFRUzzieyzKucaVgohI7HC2yyJ9YAsE7wvVK4odQI3fRjOsLRaXjFtpiaor0rERUxM4mg7jj05leRBkSazNjv2xvCL5/Qqm5PN666tREQwvgvXZgg+ZlKWkFyOq6X3THstM6CC8DTGED0cb94WPQA4YTp9OQrS3+OedQN+Nlu80Sk8Y=-----结束证书-----主题=/C=US/ST=NY/L=纽约/O=Stack Exchange, Inc./CN=*.stackexchange.comissuer=/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 高保证服务器 CA---未发送客户端证书 CA 名称---SSL 握手已读取 3956 个字节并写入 426 个字节---新的,TLSv1/SSLv3,密码是 DHE-RSA-AES128-SHA服务器公钥为 2048 位支持安全重新协商压缩:无扩展:无SSL 会话:协议:TLSv1密码:DHE-RSA-AES128-SHA会话 ID:2E38670F2CABEF3D65FAC67DB6D2E00DBACA4519E50B463D57FCFF8410640BF5会话 ID-ctx:主密钥:4C63E5502FF7DD36853048E775435A76CB1FDEB37104D6714B1C37D89482D8111B93574D2B3D7F38A1EEFF85D69F9F54键参数:无TLS 会话票证生命周期提示:300(秒)TLS 会话票证:0000 - 39 a8 c2 5f c5 15 04 b3-20 34 af fe 20 8e 4d 6c 9.._.... 4.. .Ml0010 - 6e 63 f1 e3 45 fd 2a 2c-d9 3c 0d ac 11 ab c0 c9 nc..E.*,.<......0020 - ce 51 19 89 13 49 53 a0-af 87 89 b0 5d e2 c5 92 .Q...IS.....]...0030 - af e5 84 28 03 4e 1e 98-4c a7 03 d5 5f fc 15 69 ...(.N..L..._..i0040 - 7c 83 d2 98 7d 42 50 31-30 00 d7 a8 3c 85 88 a7 |...}BP10...<...0050 - cd c0 bb 45 c8 12 b1 c8-4b 76 3c 41 5e 47 04 b5 ...E....Kv
很难相信所有这些 openssl 版本都有相同的错误,所以我认为我做错了什么.
谁能建议如何使用 openssl 连接到这个特定的网站?
这两个组合不好:
<块引用>-cipher ECDHE-ECDSA-AES128-GCM-SHA256
还有:
<块引用>错误:/SourceCache/OpenSSL098/OpenSSL098-50/src/ssl/s23_clnt.c
OpenSSL 0.9.8 不完全支持 EC.它不支持 TLS 1.1 或 1.2.要获得 AEAD 密码套件,您需要使用 TLS 1.2.这意味着您需要 OpenSSL 1.0.0 或更高版本 (IIRC).
OpenSSL 1.0.1 和 1.0.2 有它们,所以最好使用这些版本.
<小时><块引用> openssl s_client -connect thepiratebay.gd:443 ...
您要查找的命令是:openssl s_client -connect thepiratebay.gd:443 -tls1_2 -servername thepiratebay.gd -CAfile XXX
.-servername
注册 SNI.
当我访问该站点时,服务器已通过 AddTrust External CA Root 认证.当您访问该站点时,它已获得DigiCert High Assurance EV Root CA 的认证.当您再次访问该网站时,它已获得COMODO ECC 认证机构的认证.
不同的 CA 和配置与负载均衡器后面的分布式站点通信,每个参与的 Web 服务器的配置略有不同.
<小时>除了多个 Web 服务器和配置之外,一些 Web 服务器本身也配置错误.它们配置错误,因为它们没有发送构建验证路径所需的链.
链应该包括(1)服务器证书;(2) 形成根"链的从属 CA 或中间体.对于(2),可能有一个或多个中间体.
链应该不包括根.您必须拥有 root,并且必须信任它.
<小时><块引用>这个网站似乎可以使用网络浏览器或 curl 正常打开,但是,我无法找到通过 openssl 连接到它的方法...
这是因为由于 Web 服务器配置错误,浏览器携带了数百个根 CA 和从属 CA 的列表:) 该列表包括 AddTrust External CA Root、DigiCert High Assurance EV RootCA 和 COMODO ECC 根证书颁发机构.
<小时><块引用>谁能建议如何使用 openssl 连接到这个特定的网站?
好的,对于 OpenSSL 命令,您应该使用 -CAfile
.通常,您只需使用类似 openssl s_client -connect ... -CAfile DigiCertHighAssuranceEVRootCA.crt
之类的东西(用于通过 DigiCert High Assurance EV Root CA 认证的服务器).但这在这种情况下不起作用.
您必须使用所需的根 CA 和从属 CA 创建单个文件.该文件应该是构建验证服务器证书的路径所需的 PEM 格式的根 CA 和从属 CA 的串联.看来至少需要三四个证书.
或者,您可以放弃构建自己的文件,而使用cacert.pem
.但是使用 CA Zoo 存在一些风险(我对它们的亲切称呼).对于一些风险,请参阅cacert.pem 是我的计算机独有的吗?.
以编程方式,您将使用 SSL_CTX_load_verify_locations
开放式SSL.连接的 PEM 文件通过 CAfile
传递.
我不确定您会在 PHP 中使用什么.
<小时>相关,cacert.pem
有 155 个根和下属.大多数不需要认证网站thepiratebay.gd
:
$ cat cacert.pem |grep 开始 |wc -l155
因此,您希望将 CAfile
限制为仅用于认证站点所需的文件.
(评论)不确定这是要问的正确线程,但现在我想知道是否有办法以编程方式跳过其中一些检查以减少误报的数量...
我可能不会放弃检查.现在您已了解发生了什么,应该更容易使用该系统而不是放弃它.
重申,要么:
仅使用必要的根和从属 CA
- 您构建它,将 PEM 证书串联起来
- 创建文件
piratebay-certs.pem
- 添加必要的 CA
使用具有预定义可信根和从属 CA 的 CA Zoo
- 你下载它
cacert.pem
第三个选项是让站点修复其 Web 服务器配置.但如果它现在没有发生,它可能不会发生.(这可能是一个设计决策——该站点可能使用多个 CA 来确保没有一个 CA 可以对该站点进行 DoS.但这并不能解决不完整的链.
<小时>还有更一般的观察:
<块引用>我有一个检查 URL 可用性的 PHP 脚本(基本上,当 URL 可以在浏览器中打开时,脚本应该为给定的 URL 返回 true,反之亦然
远离 piratebay.gd
,特别是检查随机 URL,您可能不得不使用 cacert.pem
.这是因为 100 万个网站的随机样本可能会使用所有这些网站.
如果 piratebay.gd
仍然失败,那么找出 cacert.pem
中缺少什么,然后:
cat cacert.pem >我的扩展cacert.pemcat missing-cert.pem >>我的扩展cacert.pem
I have a PHP script that checks URLs availability (basically, the script should return true for a given URL when the URL could be opened in browser and vice versa). There is an URL I stumbled upon: https://thepiratebay.gd/. This URL could be correctly opened in browser, but fsockopen() just fails with the SSL handshake errors. There are not many options for debugging fsockopen() in PHP, but while digging more into it, I found that I am also not able to connect to https://thepiratebay.gd/ using console openssl client:
openssl s_client -connect thepiratebay.gd:443
CONNECTED(00000003)
39613:error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error:/SourceCache/OpenSSL098/OpenSSL098-50/src/ssl/s23_clnt.c:602:
This website seem to open fine using web browser or curl, however, I was not able to find a way to connect to it via openssl. Apparently, the server uses TLS 1.2 with ECDHE-ECDSA-AES128-GCM-SHA256 cipher, but even when I force those for openssl, it still fails:
openssl s_client -cipher ECDHE-ECDSA-AES128-GCM-SHA256 -connect thepiratebay.gd:443 -tls1_2
CONNECTED(00000003)
140735195829088:error:14094438:SSL routines:SSL3_READ_BYTES:tlsv1 alert internal error:s3_pkt.c:1256:SSL alert number 80
140735195829088:error:1409E0E5:SSL routines:SSL3_WRITE_BYTES:ssl handshake failure:s3_pkt.c:596:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 7 bytes and written 0 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1.2
Cipher : 0000
Session-ID:
Session-ID-ctx:
Master-Key:
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: 1432931347
Timeout : 7200 (sec)
Verify return code: 0 (ok)
---
I've tried various openssl versions: 0.9.8y, 1.0.1g, and the most recent 0.9.8zf and 1.0.2a. I've also tried to run this on at least 5 servers (CentOS, Debian, OSX) with no luck.
Every other website seem to be handled fine, here is an example of a successful handshake output:
openssl s_client -connect stackoverflow.com:443 -tls1
CONNECTED(00000003)
depth=1 /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=US/ST=NY/L=New York/O=Stack Exchange, Inc./CN=*.stackexchange.com
i:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA
1 s:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA
i:/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIGajCCBVKgAwIBAgIQCn1PE//Ffo4Be8tPBlsAZDANBgkqhkiG9w0BAQsFADBw
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMS8wLQYDVQQDEyZEaWdpQ2VydCBTSEEyIEhpZ2ggQXNz
dXJhbmNlIFNlcnZlciBDQTAeFw0xMzEwMjIxMjAwMDFaFw0xNjA3MDYxMjAwMDBa
MGoxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOWTERMA8GA1UEBxMITmV3IFlvcmsx
<---skipped few lines--->
qOHCjaUIx7vKszN4cqbvyry/NdxYkPCC7S8Eks8NjSyppzRL79tU0Yr1MUhVEd6h
GjB2qDwvAGqyWmLz1Q/l82lZbXyBF26DVTJ3RFRUzzieyzKucaVgohI7HC2yyJ9Y
AsE7wvVK4odQI3fRjOsLRaXjFtpiaor0rERUxM4mg7jj05leRBkSazNjv2xvCL5/
Qqm5PN666tREQwvgvXZgg+ZlKWkFyOq6X3THstM6CC8DTGED0cb94WPQA4YTp9OQ
rS3+OedQN+Nlu80Sk8Y=
-----END CERTIFICATE-----
subject=/C=US/ST=NY/L=New York/O=Stack Exchange, Inc./CN=*.stackexchange.com
issuer=/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 High Assurance Server CA
---
No client certificate CA names sent
---
SSL handshake has read 3956 bytes and written 426 bytes
---
New, TLSv1/SSLv3, Cipher is DHE-RSA-AES128-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : DHE-RSA-AES128-SHA
Session-ID: 2E38670F2CABEF3D65FAC67DB6D2E00DBACA4519E50B463D57FCFF8410640BF5
Session-ID-ctx:
Master-Key: 4C63E5502FF7DD36853048E775435A76CB1FDEB37104D6714B1C37D89482D8111B93574D2B3D7F38A1EEFF85D69F9F54
Key-Arg : None
TLS session ticket lifetime hint: 300 (seconds)
TLS session ticket:
0000 - 39 a8 c2 5f c5 15 04 b3-20 34 af fe 20 8e 4d 6c 9.._.... 4.. .Ml
0010 - 6e 63 f1 e3 45 fd 2a 2c-d9 3c 0d ac 11 ab c0 c9 nc..E.*,.<......
0020 - ce 51 19 89 13 49 53 a0-af 87 89 b0 5d e2 c5 92 .Q...IS.....]...
0030 - af e5 84 28 03 4e 1e 98-4c a7 03 d5 5f fc 15 69 ...(.N..L..._..i
0040 - 7c 83 d2 98 7d 42 50 31-30 00 d7 a8 3c 85 88 a7 |...}BP10...<...
0050 - cd c0 bb 45 c8 12 b1 c8-4b 76 3c 41 5e 47 04 b5 ...E....Kv<A^G..
0060 - 60 67 22 76 60 bb 44 f3-4b 3d 3d 99 af 0e dd 0d `g"v`.D.K==.....
0070 - 13 95 db 94 90 c2 0f 47-26 04 65 6b 71 b2 f8 1c .......G&.ekq...
0080 - 31 95 82 8b 00 38 59 08-1e 84 80 dc da 04 5c f0 1....8Y........
0090 - ae cc 2b ac 55 0f 39 59-0b 39 7d c7 16 b9 60 ef ..+.U.9Y.9}...`.
Start Time: 1432930782
Timeout : 7200 (sec)
Verify return code: 0 (ok)
It is hard to believe that all these openssl versions have the same bug, so I'm thinking that I'm doing something wrong.
Can anyone advice how to connect to this particular website using openssl?
These two are a bad combination:
-cipher ECDHE-ECDSA-AES128-GCM-SHA256
And:
error:/SourceCache/OpenSSL098/OpenSSL098-50/src/ssl/s23_clnt.c
OpenSSL 0.9.8 does not have full EC support. And it does not support TLS 1.1 or 1.2. To get the AEAD cipher suites, you need to use TLS 1.2. That means you need OpenSSL 1.0.0 or above (IIRC).
OpenSSL 1.0.1 and 1.0.2 have them, so its probably better to use those versions.
openssl s_client -connect thepiratebay.gd:443 ...
The command you are looking for is: openssl s_client -connect thepiratebay.gd:443 -tls1_2 -servername thepiratebay.gd -CAfile XXX
. -servername
enlists SNI.
When I hit the site, the server was certified by AddTrust External CA Root. When you hit the site, it was certified by DigiCert High Assurance EV Root CA. And when you hit the site again, it was certified by COMODO ECC Certification Authority.
The different CAs and configurations speak to a distributed site behind a load balancer, with each participating web server in a slightly different configuration.
In addition to multiple web servers and configurations, some of the web servers themselves are misconfigured. They are misconfigured because they do not send the chain required to build a path for validation.
The chain should include (1) the server certificate; (2) Subordinate CAs or intermediates that form the chain to the "root". For (2), there may be one or more intermediates.
The chain should not include the root. You have to have the root, and it must be trusted.
This website seem to open fine using web browser or curl, however, I was not able to find a way to connect to it via openssl...
This is because the browsers carry around a list of hundreds of Root CAs and Subordinate CAs due to web server misconfigurations :) The list includes AddTrust External CA Root, DigiCert High Assurance EV Root CA, and COMODO ECC Root Certificate Authority.
Can anyone advice how to connect to this particular website using openssl?
OK, for the OpenSSL command, you should use -CAfile
. Usually, you just use something like openssl s_client -connect ... -CAfile DigiCertHighAssuranceEVRootCA.crt
(for the server certified with DigiCert High Assurance EV Root CA). But that won't work in this case.
You have to create a single file with the required Root and Subordinate CAs. The file should be a concatenation of the Root CAs and Subordinate CAs in PEM format required to build a path to validate the server certificate. It looks like it will need at least 3 or 4 certificates.
Or, you could forgo building you own file, and use something like cacert.pem
. But there is some risk in using the CA Zoo (my affectionate term for them). For some of the risks, see Is cacert.pem unique to my computer?.
Programmatically, you would use SSL_CTX_load_verify_locations
in OpenSSL. The concatenated PEM file is passed though CAfile
.
I'm not sure what you would use in PHP.
Related, cacert.pem
has 155 roots and subordinates. Most of them are not needed to certify the site thepiratebay.gd
:
$ cat cacert.pem | grep BEGIN | wc -l
155
Hence the reason you want to restrict your CAfile
to only those necessary to certify the site.
(comment) Not sure this is the correct thread to ask, but now I wonder if there is a way to skip some of these checks programmatically to reduce number of false negatives...
I would probably not forgo the checks. Now that you understand what's going on, it should be easier to work with the system rather than abandoning it.
To reiterate, either:
Use only necessary Root and Subordinate CAs
- You build it, concatenation of PEM certificates
- Create file
piratebay-certs.pem
- Add necessary CAs
Use a CA Zoo with predifined trusted Root and Subordinate CAs
- You download it
cacert.pem
The third option is to get the site to fix its web server configurations. But if it has not happened by now, it probably won't happen. (And it could be a design decision - the site may be using multiple CAs to ensure no one CA can DoS the site. But that does not address the incomplete chain).
And the more general observation:
I have a PHP script that checks URLs availability (basically, the script should return true for a given URL when the URL could be opened in browser and vice versa
Moving away from piratebay.gd
in particular to checking random URLs, you will probably have to use cacert.pem
. That's because a random sample of 1 million sites will likely use all of them.
If piratebay.gd
still fails, then find out what is missing from cacert.pem
, and then:
cat cacert.pem > my-expanded-cacert.pem
cat missing-cert.pem >> my-expanded-cacert.pem
这篇关于“tlsv1 警报内部错误"握手时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!