使用“openssl s_client”获取服务器的 SSL/TLS 证书

使用“openssl s_client”获取服务器的 SSL/TLS 证书

我正在尝试使用以下方法获取我们的一个负载均衡器 (Netscaler) 的 SSL/TLS 证书:

openssl s_client -showcerts -connect lb.example.com:443

但它不会向我显示证书:

CONNECTED(00000003)
write:errno=54

使用-servername lb.example.com没有帮助,我们的系统管理员告诉我我们的负载均衡器不使用 SNI。

编辑:该服务器位于我们的内部网中,不接受来自公共互联网的连接。这是 openssl 的输出-debug

CONNECTED(00000003)
write to 0x7fec7af0abf0 [0x7fec7b803a00] (130 bytes => 130 (0x82))
0000 - 80 80 01 03 01 00 57 00-00 00 20 00 00 39 00 00   ......W... ..9..
0010 - 38 00 00 35 00 00 16 00-00 13 00 00 0a 07 00 c0   8..5............
0020 - 00 00 33 00 00 32 00 00-2f 00 00 9a 00 00 99 00   ..3..2../.......
0030 - 00 96 03 00 80 00 00 05-00 00 04 01 00 80 00 00   ................
0040 - 15 00 00 12 00 00 09 06-00 40 00 00 14 00 00 11   .........@......
0050 - 00 00 08 00 00 06 04 00-80 00 00 03 02 00 80 00   ................
0060 - 00 ff a6 f7 27 1a a7 18-85 cf b2 03 22 fc 48 3d   ....'.......".H=
0070 - dd a9 2c b7 76 67 62 80-df 85 ed 48 35 c7 d4 87   ..,.vgb....H5...
0080 - 8d d3                                             ..
read from 0x7fec7af0abf0 [0x7fec7b809000] (7 bytes => -1 (0xFFFFFFFFFFFFFFFF))
write:errno=54

这是相关输出curl -v https://lb.example.com/

$ curl -vI https://lb.exmple.com/
*   Trying 1.2.3.4...
* Connected to lb.exmple.com (10.1.2.3) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_CBC_SHA
* Server certificate: lb.example.com
* Server certificate: RapidSSL SHA256 CA - G2
* Server certificate: GeoTrust Primary Certification Authority - G3
> HEAD / HTTP/1.1
> Host: lb.exmple.com
> User-Agent: curl/7.43.0
> Accept: */*
>

关于如何使用获取证书,有什么建议吗openssl s_client

答案1

过了一会儿,我搞明白了:这个特定的负载均衡器配置为仅使用 TLSv1.2,而 OS X (0.9.8) 中包含的 openssl 版本无法识别该版本。我使用 homebrew 安装了较新版本的 openssl (>= 1.0.1),因此可以正常工作:

/usr/local/opt/openssl/bin/openssl s_client -showcerts -connect lb.example.com:443

答案2

我正在尝试使用以下方法获取我们的一个负载均衡器 (Netscaler) 的 SSL/TLS 证书:

 openssl s_client -showcerts -connect lb.example.com:443

如果它是一个现代配置(有些人不明白这意味着什么),请使用:

openssl s_client -connect lb.example.com:443 -tls1 -servername lb.example.com | \
openssl x509 -text -noout

CONNECTED(00000003)
write to 0x7fec7af0abf0 [0x7fec7b803a00] (130 bytes => 130 (0x82))
0000 - 80 80 01 03 01 00 57 00-00 00 20 00 00 39 00 00
...

看起来字节 0 和 1 处有一些额外的前导码。字节 2 处应该有一个记录类型。字节 3 和 4 处应该有一个版本号。字节 5 和 6 应该是有效载荷的 16 位长度。

这是一个有效的例子:

$ openssl s_client -connect www.googl.com:443 -tls1 -servername www.googl.com -debug
CONNECTED(00000005)
write to 0x7f7fe1c1fa30 [0x7f7fe2022000] (132 bytes => 132 (0x84))
0000 - 16 03 01 00 7f 01 00 00-7b 03 01 71 c0 12 35 98
...

从上面可以看出,记录类型在位置 0,其值为 0x16。0x16 是握手类型。记录层版本是位置 2 和 3 的接下来两个字节。它们的值为0x03 0x01。有效载荷的长度为0x007f

另请参阅RFC 5246,传输层安全 (TLS) 协议版本 1.2,第 18 页:

6.2.1.  Fragmentation

   The record layer fragments information blocks into TLSPlaintext
   records carrying data in chunks of 2^14 bytes or less.  Client
   message boundaries are not preserved in the record layer (i.e.,
   multiple client messages of the same ContentType MAY be coalesced
   into a single TLSPlaintext record, or a single message MAY be
   fragmented across several records).

      struct {
          uint8 major;
          uint8 minor;
      } ProtocolVersion;

      enum {
          change_cipher_spec(20), alert(21), handshake(22),
          application_data(23), (255)
      } ContentType;

      struct {
          ContentType type;
          ProtocolVersion version;
          uint16 length;
          opaque fragment[TLSPlaintext.length];
      } TLSPlaintext;

您的问题可能是与旧 SSLv2 兼容的记录类型。或者可能是 OpenSSL 的低版本,例如 0.9.5 或 0.9.8。很难说,我们可能需要更多信息。

更多信息包括操作系统;OpenSSL 版本;您是否尝试用您自己的 OpenSSL 版本替换平台的 OpenSSL 版本;是否有防火墙或“网络检查”框或其他中间件正在运行;以及服务器接收的内容。


使用-servername lb.example.com没有帮助,我们的系统管理员告诉我我们的负载均衡器不使用 SNI。

这听起来有点不寻常。但它是 TLS 的扩展,因此如果不使用,它会被忽略(并且不会产生致命警报)。

2016 年的经验法则:始终使用 TLS 1.0 或更高版本,并且始终使用 SNI。

相关内容