gpt4 book ai didi

java - 使用 Bouncy-CaSTLe 库从证书中读取 SubjectAlternativeNames

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

我正在使用 bouncy-caSTLe 库与 Web-Server 进行 TLS-Handshake 并获取公共(public)证书。下面是我的代码

 private org.bouncycastle.asn1.x509.Certificate[] certificateList;

public static void main(String... args) {
new BCMain().testBCTLS();
}

private void testBCTLS() {
try {
Socket s = new Socket(InetAddress.getByName(WEB_SERVER), WEB_SERVER_PORT);
//TlsProtocolHandler tlsHandler = new TlsProtocolHandler(s.getInputStream(), s.getOutputStream());

TlsClientProtocol protocol = new TlsClientProtocol(s.getInputStream(), s.getOutputStream(), new SecureRandom());

TlsClient client = new DefaultTlsClient() {
private Boolean connectionStatus = Boolean.FALSE;

@Override
public TlsAuthentication getAuthentication() throws IOException {


return new ServerOnlyTlsAuthentication() {

public void notifyServerCertificate(Certificate serverCertificate)
throws IOException {

certificateList = serverCertificate.getCertificateList();
}
};
}

@Override
public Hashtable getClientExtensions() throws IOException {
Hashtable clientExtensions = super.getClientExtensions();
clientExtensions = TlsExtensionsUtils.ensureExtensionsInitialised(clientExtensions);
Vector<ServerName> serverNames = new Vector(1);
serverNames.add(new ServerName(NameType.host_name, SNI_HOST_NAME));

TlsExtensionsUtils.addServerNameExtension(clientExtensions, new ServerNameList(serverNames));

return clientExtensions;

}

public Boolean getConnectionStatus() {
return connectionStatus;
}

};

protocol.connect(client);

if (this.certificateList!=null) {
org.bouncycastle.asn1.x509.Certificate certificate = certificateList[0];

System.out.println(certificate.getSubject());
}

InputStream is = protocol.getInputStream();
System.out.println(is);


} catch (Exception e) {
e.printStackTrace();
}


}

我想从该公共(public)证书中提取 Subject Alternative Names

X509Certificate JDK 有提取 SubjectAlternativeNames 的方法。但我想从 bouncy-caSTLe 证书中获得相同的内容。

有人可以帮忙吗?

最佳答案

我能够使用 X509CertificateHolderJcaX509CertificateConverter 类从 BouncyCaSTLe 库中提取 Subject-Alternative-Names。 .继续上面的代码

import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;


if (this.certificateList!=null) {
org.bouncycastle.asn1.x509.Certificate certificate = certificateList[0];
X509CertificateHolder holder = new X509CertificateHolder(certificate.getEncoded());
X509Certificate x509Certificate = new JcaX509CertificateConverter().getCertificate(holder);
Collection<List<?>> sanCollections = x509Certificate.getSubjectAlternativeNames();
}

关于java - 使用 Bouncy-CaSTLe 库从证书中读取 SubjectAlternativeNames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367275/

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