无法解析 java 中的 subjectAlternativeNames
Can't pars subjectAlternativeNames in java
我尝试解析 java 中 certificate 的额外数据。我对 subjectAlternativeNames 部分感兴趣。我的代码是:
CertificateFactory certFactory = CertificateFactory.getInstance("X.509")
certFactory.generateCertificate(ByteArrayInputStream(x509EntryChain.leafCertificate))
X509CertInfo
包含 java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
。
subjectAlternativeNames
为空。
如何解析它?
我只对 extra_data
感兴趣,对 leaf_input
不感兴趣。我可以在这里找到证书:http://transparencyreport.google.com/https/certificates/
DNS names
段是空的,好像Google也解析不了
parce 证书的完整代码:
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+MIIFujCCBKKgAwIBAgIDAZZpMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkxVMRYwFAYDVQQKEw1MdXhUcnVzdCBTLkEuMR4wHAYDVQQDExVMdXhUcnVzdCBRdWFsaWZpZWQgQ0EwHhcNMTAwNDE1MDY0MzM0WhcNMTMwNDE1MDY0MzM0WjB8MQswCQYDVQQGEwJMVTERMA8GA1UEBxMITXVuc2JhY2gxFDASBgNVBAoTC0NFVFJFTCBTLkEuMRAwDgYDVQQLEwdJT1AtU1NTMRQwEgYDVQQDFAsqLmNldHJlbC5sdTEcMBoGCSqGSIb3DQEJARYNc3NzQGNldHJlbC5sdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALhf+I7RshQlHMMWq/WPDLNxx+ODMd4Tn9ej14igv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R0cDovL2NybC5sdXh0cnVzdC5sdS9MVFFDQS5jcmwwHQYDVR0OBBYEFLhOdzNJg4CSchxSsHbwKWMVm8xnMA0GCSqGSIb3DQEBBQUAA4IBAQBrQ9bYRA5O8a+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
,它是 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 中 certificate 的额外数据。我对 subjectAlternativeNames 部分感兴趣。我的代码是:
CertificateFactory certFactory = CertificateFactory.getInstance("X.509")
certFactory.generateCertificate(ByteArrayInputStream(x509EntryChain.leafCertificate))
X509CertInfo
包含 java.io.IOException: invalid URI name:DNS:*.cetrel.lu, DNS:mail.cetrel.lu, DNS:www.cetrel.lu
。
subjectAlternativeNames
为空。
如何解析它?
我只对 extra_data
感兴趣,对 leaf_input
不感兴趣。我可以在这里找到证书:http://transparencyreport.google.com/https/certificates/
DNS names
段是空的,好像Google也解析不了
parce 证书的完整代码:
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+MIIFujCCBKKgAwIBAgIDAZZpMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkxVMRYwFAYDVQQKEw1MdXhUcnVzdCBTLkEuMR4wHAYDVQQDExVMdXhUcnVzdCBRdWFsaWZpZWQgQ0EwHhcNMTAwNDE1MDY0MzM0WhcNMTMwNDE1MDY0MzM0WjB8MQswCQYDVQQGEwJMVTERMA8GA1UEBxMITXVuc2JhY2gxFDASBgNVBAoTC0NFVFJFTCBTLkEuMRAwDgYDVQQLEwdJT1AtU1NTMRQwEgYDVQQDFAsqLmNldHJlbC5sdTEcMBoGCSqGSIb3DQEJARYNc3NzQGNldHJlbC5sdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALhf+I7RshQlHMMWq/WPDLNxx+ODMd4Tn9ej14igvMEE+RYEagdMOZoeO8Bqz9qV2atzFHqz0D+Ad+cxuznBGKl4rhS9gUejgoAMox7car0+LSsv1NT4J0gAlnmH3BJlDMd9CihT0D/sRwMNfa8GYAvCuGDtWIvYb497RFy+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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
,它是 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