简体   繁体   English

从命令行确定TLS / SSL证书是否“可信”?

[英]Determining if a TLS/SSL certificate is 'trusted' from the command line?

I would like to be able to determine if a remote domain's TLS/SSL certificate is 'trusted' from the command line. 我希望能够从命令行确定远程域的TLS / SSL证书是否“可信”。

Here is an openssl example I was playing with a few weeks back, here I use openssl to acquire the certificate and then pipe it to openssl's 'verify' command. 这是我几周前玩的一个openssl示例,在这里我使用openssl获取证书,然后将其传递给openssl的'verify'命令。 I assumed that the 'verify' command would verify the certificate, however, how I understand it now is that the 'verify' command just verifies the certificate chain (I think). 我假设'verify'命令会验证证书,但是,我现在理解的是'verify'命令只是验证证书链(我认为)。 (cdn.pubnub.com is just a domain I found from a quick Twitter search as an example to use) (cdn.pubnub.com只是我从快速Twitter搜索中找到的一个域名作为使用示例)

echo "GET /" | openssl s_client -connect cdn.pubnub.com:443 | openssl x509 -text | openssl verify

As you can see from the cdn.pubnub.com domain (at the time of writing), the browser (Chrome at least) does not trust the certificate (because the certificate domain doesn't match), however, the openssl 'verify' command does not output 'trusted' or 'not trusted' or something else we can deduct that information from. 正如您在cdn.pubnub.com域中所看到的(在撰写本文时),浏览器(至少Chrome)不信任证书(因为证书域不匹配),但是,openssl'verify'命令不输出'trusted'或'not trusted'或我们可以从中扣除该信息的其他内容。

Another way I thought of doing this, is by using a headless browser (such as PhantomJS ) and parsing any errors they return. 我想到的另一种方法是使用无头浏览器(例如PhantomJS )并解析它们返回的任何错误。 It turns out that PhantomJS just errors but does not give any details, so this can not be used as the error could have been caused by something else. 事实证明,PhantomJS只是错误但没有提供任何细节,所以这不能用,因为错误可能是由其他东西引起的。

I didn't think it would be this hard to find out that a certificate was trusted or not from the command line, without having to parse and check all the data that makes a certificate trusted myself which I don't think would be wise. 我不认为在命令行中发现证书是否受信任是很难的,而不必解析和检查使证书成为我自己信任的所有数据,我认为这些数据并不明智。

Is there a library or some other way I can tell if a remote domain's certificate is trusted from the command line? 是否有一个库或其他方式我可以告诉我是否从命令行信任远程域的证书?

curl (and libcurl ) uses OpenSSL for https URLs, and checks certificate validity unless -k, --insecure option is enabled. curl (和libcurl )使用OpenSSL进行https URL,并检查证书有效性,除非启用了-k, --insecure选项。

zsh 29354 % curl https://cdn.pubnub.com/
curl: (51) SSL peer certificate or SSH remote key was not OK

As you see, it doesn't give much details on why the certificate is invalid, but otherwise it should be as good as a headless browser, and much lighter. 如你所见,它没有详细说明证书无效的原因,但是它应该像无头浏览器一样好,而且要轻得多。

It depends on what you consider "trusted". 这取决于你认为“值得信赖”的东西。 Beside the core cryptographic checks (eg checking the digital signature) the client usually does the following: 除了核心加密检查(例如检查数字签名),客户端通常会执行以下操作:

  1. Check that the certificate chains to a trusted root 检查证书是否链接到受信任的根

  2. Verify that the current time is between the notValidBefore and not validAfter attributes. 验证当前时间是否在notValidBefore和not validAfter属性之间。

  3. The certificate is not revoked. 证书未被撤销。

  4. keyUsage and other certificate constraints match. keyUsage和其他证书约束匹配。

  5. The entity we are communicating is somehow found in the subject of the certificate (for servers this usually means the hostname is listed as CN or subjectAlternativeName). 我们正在通信的实体以某种方式在证书的主题中找到(对于服务器,这通常意味着主机名被列为CN或subjectAlternativeName)。

In your case the information to verify step 5 (namely the hostname) is missing, so it cannot be checked. 在您的情况下,缺少验证步骤5(即主机名)的信息,因此无法检查。 You would have to do this step yourself. 你必须自己做这个步骤。 Please note that different clients perform different checks to see if a certificate is trusted, so one answer may not apply to all possible clients. 请注意,不同的客户端执行不同的检查以查看证书是否可信,因此一个答案可能不适用于所有可能的客户端。 If you want to check your installation deeply, consider using the check from ssl labs https://www.ssllabs.com/ssltest / 如果您想深入检查安装,请考虑使用ssl labs中的检查https://www.ssllabs.com/ssltest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM