正如我在 answer 中指出的那样至 Hostname / IP doesn't match certificate's altname当请求的主机名作为证书的公用名 (CN) 给出时,Node.js 会成功验证主机名上的 TLS 证书。
Request : localhost
Common Name: localhost
=> Success
现在,当我尝试对 IP 地址进行相同的验证时,验证失败:
Request : 192.168.178.31
Common Name: 192.168.178.31
=> Fail
仅当我将 IP 地址指定为主题备用名称 (SAN) 时它才有效:
Request : 192.168.178.31
Common Name : 192.168.178.31
Subject Alternative Name: 192.168.178.31
=> Success
如 answer 中所述至 IP address as hostname (CN) when creating a certificate? (HTTPS hostname wrong: should be <ipAddress>) ,
using IP address in certificates is not recommended [...].
作为替代方案,建议在 answer至 How are SSL certificate server names resolved/Can I add alternative names using keytool?将 IP 地址添加为 SAN(这反过来在 Node.js 中有效)。
现在我的问题是:不推荐并不意味着错误或禁止。除了 不推荐 Node.js 不支持 CN 中的 IP 地址之外,是否有任何特定原因,或者如果它支持,为什么会有风险(安全性,等等......)?
最佳答案
实现仅符合 HTTPS specification :
In some cases, the URI is specified as an IP address rather than a hostname. In this case, the iPAddress subjectAltName must be present in the certificate and must exactly match the IP in the URI.
当我在您引用的答案中说“不推荐”时,更多的是关于在使用 HTTPS 时通常使用 IP 地址。有些浏览器可以让你摆脱它,但我也引用了那里的规范,它确实使用了“必须”这个词。不用说,RFC 比您在此处找到的任何答案都更权威。
关于node.js - 为什么 Node.js 只接受 SAN 证书中的 IP 地址,CN 不接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089872/