- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试解析 certificate 的额外数据在Java中。我对 subjectAlternativeNames 部分感兴趣。我的代码是:
CertificateFactory certFactory = CertificateFactory.getInstance("X.509")
certFactory.generateCertificate(ByteArrayInputStream(x509EntryChain.leafCertificate))
X509CertInfo
包含 java.io.IOException:无效的 URI 名称:DNS:*.cetrel.lu、DNS:mail.cetrel.lu、DNS:www.cetrel.lu
.并且 subjectAlternativeNames
为空。
如何解析它?
我只对 extra_data
感兴趣,对 leaf_input
不感兴趣。我可以在这里找到证书:http://transparencyreport.google.com/https/certificates/
DNS 名称
部分为空,Google 似乎也无法解析它。
解析证书的完整代码:
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.security.x509.CertificateExtensions;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
public class CertParser {
public static void main(String[] args) throws CertificateException, IOException {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(
"AAAAAAFXsGq8HwAAAAW+MIIFujCCBKKgAwIBAgIDAZZpMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkxVMRYwFAYDVQQKEw1MdXhUcnVzdCBTLkEuMR4wHAYDVQQDExVMdXhUcnVzdCBRdWFsaWZpZWQgQ0EwHhcNMTAwNDE1MDY0MzM0WhcNMTMwNDE1MDY0MzM0WjB8MQswCQYDVQQGEwJMVTERMA8GA1UEBxMITXVuc2JhY2gxFDASBgNVBAoTC0NFVFJFTCBTLkEuMRAwDgYDVQQLEwdJT1AtU1NTMRQwEgYDVQQDFAsqLmNldHJlbC5sdTEcMBoGCSqGSIb3DQEJ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BBggrBgEFBQcDAgYIKwYBBQUHAwQwHwYDVR0jBBgwFoAUjZCjB90aE3eZTJKrTUPeP80pZAUwMQYDVR0fBCowKDAmoCSgIoYgaHR0cDovL2NybC5sdXh0cnVzdC5sdS9MVFFDQS5jcmwwHQYDVR0OBBYEFLhOdzNJg4CSchxSsHbwKWMVm8xnMA0GCSqGSIb3DQEBBQUAA4IBAQBrQ9bYRA5O8a+4vIfVx5izH5x9yieKuQjpF4Etc4YUxfk3h5yZieWJuZGHygUjV5TCYDdhtVf6FFWkJ4FT5l6zDQeOLrEWqL12qcT4hGFN61mwjZO7kca8IHlqPPeqtYVg/Ssbpun+bjPOsGdvrvMulqNNTz5UeQuovc/VFaoHpYCQhezrQ6E6uQ684f6LFVIbsah6pT58wEnrf6xE1aRdSk27e3bF8wns3zOVsWE2wKck5pMS5DkGwjWli27Aqt6QQCyCKC7xqqxwL8GfnmXZNdn2iYYfSyr0I7rdqxa7FsuNFkEb8/PdZlyMxQP867YnucRCyLzzjCnRy1bOLnUeAAA="
));
readNumber(in, 1); // version
readNumber(in, 1); // type
readNumber(in, 8); // timestamp
readNumber(in, 2); // entry type
int length = (int) readNumber(in, 3);
byte[] x509 = readFixedLength(in, length);
X509CertImpl cert = (X509CertImpl) certFactory.generateCertificate(new ByteArrayInputStream(x509));
((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions(); // <-- want too be empty, but it's not!!!
}
static byte[] readFixedLength(InputStream inputStream, int dataLength) throws IOException {
byte[] toReturn = new byte[dataLength];
int bytesRead = inputStream.read(toReturn);
if (bytesRead < dataLength) {
throw new RuntimeException();
}
return toReturn;
}
static long readNumber(InputStream inputStream, int numBytes) throws IOException {
long toReturn = 0;
for (int i = 0; i < numBytes; i++) {
int valRead = inputStream.read();
if (valRead < 0) {
throw new RuntimeException();
}
toReturn = (toReturn << 8) | valRead;
}
return toReturn;
}
}
最佳答案
在您共享访问证书的代码后,我可以安全地检查它。
正如问题中已经明确的那样,有问题的扩展是主题备用名称。它的值被指定(RFC 5280)为 GeneralNames
的实例,它是 GeneralName
的 SEQUENCE
,而 GeneralName
是 CHOICE
>:
SubjectAltName ::= GeneralNames
GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
GeneralName ::= CHOICE {
otherName [0] AnotherName,
rfc822Name [1] IA5String,
dNSName [2] IA5String,
x400Address [3] ORAddress,
directoryName [4] Name,
ediPartyName [5] EDIPartyName,
uniformResourceIdentifier [6] IA5String,
iPAddress [7] OCTET STRING,
registeredID [8] OBJECT IDENTIFIER }
这是与您的证书相关的扩展的转储:
<30 41>
676 65: . . . . SEQUENCE {
<06 03>
678 3: . . . . . OBJECT IDENTIFIER subjectAltName (2 5 29 17)
: . . . . . . (X.509 extension)
<04 3A>
683 58: . . . . . OCTET STRING, encapsulates {
<30 38>
685 56: . . . . . . SEQUENCE {
<86 36>
687 54: . . . . . . . [6]
: . . . . . . . . 'DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cet'
: . . . . . . . . 'rel.lu'
: . . . . . . . }
: . . . . . . }
: . . . . . }
该值标有 6 ([6]
)。因此,使用了uniformResourceIdentifier选项。 RFC 5280 在主题备用名称的上下文中要求此选择的值:
The name MUST NOT be a relative URI, and it MUST follow the URI syntax and encoding rules specified in [RFC3986]. The name MUST include both a scheme (e.g., "http" or "ftp") and a scheme-specific-part.
因此,sun.security.x509.X509CertImpl
尝试将该值解析为 URI。这显然一定会失败,因为该值根本就不是 URI:
java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
at sun.security.x509.URIName.<init>(URIName.java:109)
at sun.security.x509.URIName.<init>(URIName.java:96)
at sun.security.x509.GeneralName.<init>(GeneralName.java:122)
at sun.security.x509.GeneralName.<init>(GeneralName.java:76)
at sun.security.x509.GeneralNames.<init>(GeneralNames.java:68)
at sun.security.x509.SubjectAlternativeNameExtension.<init>(SubjectAlternativeNameExtension.java:141)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.security.x509.CertificateExtensions.parseExtension(CertificateExtensions.java:113)
at sun.security.x509.CertificateExtensions.init(CertificateExtensions.java:88)
at sun.security.x509.CertificateExtensions.<init>(CertificateExtensions.java:78)
at sun.security.x509.X509CertInfo.parse(X509CertInfo.java:702)
at sun.security.x509.X509CertInfo.<init>(X509CertInfo.java:167)
at sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1804)
at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:195)
at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:102)
at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 16: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parse(URI.java:3058)
at java.net.URI.<init>(URI.java:588)
at sun.security.x509.URIName.<init>(URIName.java:107)
... 46 more
如果您仍然想访问该值,只需从您拥有的 UnparseableExtensions
映射中检索并解析它,如下所示:
Map<String, Extension> unparseables = ((CertificateExtensions)((X509CertInfo)cert.get("x509.info")).get("extensions")).getUnparseableExtensions();
Extension extension = unparseables.get("2.5.29.17");
byte[] value = extension.getValue();
DerValue derValue = new DerValue(value);
while (derValue.data.available() > 0) {
DerValue encName = derValue.data.getDerValue();
if ((encName.tag & 0x1f) == 6) {
encName.resetTag(DerValue.tag_IA5String);
System.out.printf("IA5String value from URI GeneralName value: %s\n", encName.getIA5String());
}
}
对于sun.security.x509.Extension
和sun.security.util.DerValue
。
输出:
IA5String value from URI GeneralName value: DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
关于java - 无法解析 java 中的 subjectAlternativeNames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60261855/
我尝试解析 certificate 的额外数据在Java中。我对 subjectAlternativeNames 部分感兴趣。我的代码是: CertificateFactory certFactory
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在使用 bouncy-caSTLe 库与 Web-Server 进行 TLS-Handshake 并获取公共(public)证书。下面是我的代码 private org.bouncycastle
我正在从 Java 中的 X509 证书中提取 SubjectAlternativeName,它似乎返回类似的内容 [2, domain.example.com ] 这是标准吗?值中的 2 代表什么?
有一个非面向公众的应用程序。我正在尝试确保没有与 HTTPS 相关的警告/错误。 我在将证书与 SAN 字段(由受信任的 CA 签名)放在一起后收到的错误是 Web 应用程序根本不会加载,即专门 Go
我有一个基于用java编写的“commons-httpclient”(v3.*)的httpclient。客户端可以与至少两个不同的产品(服务器)正常工作。 但是与我希望它能够使用的新服务器(产品)的s
我是一名优秀的程序员,十分优秀!