tls.checkServerIdentity(hostname, cert)
hostname<string> 用于验证证书的主机名或 IP 地址。cert<Object> 表示对端证书的 证书对象。- 返回值: <Error> | <undefined>
验证证书 cert 是否颁发给 hostname。
【Verifies the certificate cert is issued to hostname.】
返回 <Error> 对象,在失败时用 reason、host 和 cert 填充它。成功时,返回 <undefined>。
【Returns <Error> object, populating it with reason, host, and cert on
failure. On success, returns <undefined>.】
此函数旨在与可以传递给 tls.connect() 的 checkServerIdentity 选项结合使用,因此它作用于 证书对象。对于其他用途,请考虑使用 x509.checkHost()。
【This function is intended to be used in combination with the
checkServerIdentity option that can be passed to tls.connect() and as
such operates on a certificate object. For other purposes, consider using
x509.checkHost() instead.】
可以通过提供一个替代函数作为传递给 tls.connect() 的 options.checkServerIdentity 选项来重写此函数。当然,重写的函数可以调用 tls.checkServerIdentity(),以在现有检查的基础上增加额外的验证。
【This function can be overwritten by providing an alternative function as the
options.checkServerIdentity option that is passed to tls.connect(). The
overwriting function can call tls.checkServerIdentity() of course, to augment
the checks done with additional verification.】
只有在证书通过了所有其他检查后才会调用此函数,例如由受信任的 CA (options.ca) 颁发。
【This function is only called if the certificate passed all other checks, such as
being issued by trusted CA (options.ca).】
早期版本的 Node.js 如果存在匹配的 uniformResourceIdentifier 主题备用名称,会错误地接受给定 hostname 的证书(参见 CVE-2021-44531)。希望接受 uniformResourceIdentifier 主题备用名称的应用可以使用自定义的 options.checkServerIdentity 函数来实现所需的行为。
【Earlier versions of Node.js incorrectly accepted certificates for a given
hostname if a matching uniformResourceIdentifier subject alternative name
was present (see CVE-2021-44531). Applications that wish to accept
uniformResourceIdentifier subject alternative names can use a custom
options.checkServerIdentity function that implements the desired behavior.】