gpt4 book ai didi

发现 java.security.cert.CertificateException : No subject alternative names matching IP address x. x.x.x

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:10 35 4
gpt4 key购买 nike

我有一个java程序试图从一个IP地址下载一个文件。假设 IP 地址是 10.2.14.35。因此,该文件位于 https://10.2.14.35/path/to/the/file/myfile.txt .我有以下旨在下载该文件的功能:

public static void downloadFile(String uri, String localFileName) {
try {
URL url = new URL(uri);
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(localFileName);

fileOutputStream
.getChannel()
.transferFrom(readableByteChannel, 0 /* position */, Long.MAX_VALUE /* count */);

fileOutputStream.close();
readableByteChannel.close();
} catch (IOException e) {
throw new AgentException(ErrorProto.Type.kIOError, e.getMessage());
}
}

并将函数用作:

downloadFile("https://10.2.14.35/path/to/the/file/myfile.txt", "/tmp/myfile.txt")

我收到错误 java.security.cert.CertificateException:找不到与 IP 地址 10.2.14.35 匹配的主题备用名称。我发现有人在 SO(JAVA - No subject alternative names matching IP addressSSLHandshakeException: No subject alternative names presentHow are SSL certificate server names resolved/Can I add alternative names using keytool?)上提出了一些问题,但没有一个有效(或者我在某处做错了什么。)

我尝试从浏览器下载文件,结果成功了。但是,它表明该站点不安全,因为证书无效。不知道java程序下载文件失败是不是这个原因。在阅读时,我遇到了 cacerts 的概念,我必须让 JRE 明白可以连接到 10.2.14.35。为此,我需要创建证书并将其导入 cacerts。我这样做了,但没有用,因为也许我错过了什么。谁能帮我指出错误?

最佳答案

长见识

您看到的错误与特定的证书扩展有关,称为 Subject Alternative Name , 没有 10.2.14.35指定为有效的 IP 地址。

该错误还表明证书 CA 已被信任。如果 CA 不受信任,则证书验证会提前失败并给出另一个错误,例如

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


由于我无权访问您的特定证书,我将尝试使用 example.com 的服务器证书进行说明。

要查看证书,我将使用 openssl ,但还有多种其他工具可用 - 例如,大多数浏览器都能够显示证书详细信息。

我会删除我认为不需要的输出来说明我想要的内容

$ openssl s_client -connect example.com:443 -showcerts | openssl x509 -noout -text
[...]
Subject: C = US, ST = California, L = Los Angeles, O = Internet Corporation for Assigned Names and Numbers, OU = Technology, CN = www.example.org
[...]
X509v3 Subject Alternative Name:
DNS:www.example.org, DNS:example.com, DNS:example.edu, DNS:example.net, DNS:example.org, DNS:www.example.com, DNS:www.example.edu, DNS:www.example.net

Subject字段包含不同种类的信息,但是 CN (通用名称)值为 www.example.com。以前,大多数或所有浏览器都使用它来验证证书是否是为浏览器连接的服务器颁发的。然而,最近,Chrome(我认为是 Firefox)忽略了 Subject 中的信息。完全代替relies on the Subject Alternative Name .

For Chrome 58 and later, only the subjectAlternativeName extension, not commonName, is used to match the domain name and site certificate. The certificate subject alternative name can be a domain name or IP address. If the certificate doesn’t have the correct subjectAlternativeName extension, users get a NET::ERR_CERT_COMMON_NAME_INVALID error letting them know that the connection isn’t private.

因此,您的错误源于 10.2.14.35 提供的服务器证书这一事实不包含您要连接的 IP 地址。查看使用 openssl 的证书,服务器提供的证书必须至少有一个 Subject Alternative Name。它至少需要包含 IP Address:10.2.14.35 .构造的最小示例看起来像这样。

$ openssl s_client -connect 10.2.14.35:443 -showcerts | openssl x509 -noout -text
[...]
Subject: CN = 10.2.14.35
[...]
X509v3 Subject Alternative Name:
IP Address:10.2.14.35

关于发现 java.security.cert.CertificateException : No subject alternative names matching IP address x. x.x.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55995400/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com