gpt4 book ai didi

java - 握手后SSL套接字服务器获取证书cn

转载 作者:行者123 更新时间:2023-11-30 10:16:37 24 4
gpt4 key购买 nike

我有一个 SSL 套接字服务器运行,需要 2 方握手(为此我使用自签名证书)。握手成功后,我想在服务器端查看客户端证书的cn。不幸的是,此字段设置为 Unknown

这是我用来确定 cn 字段值的代码:

((SSLSocket) socket).addHandshakeCompletedListener(new HandshakeCompletedListener() {
@Override
public void handshakeCompleted(HandshakeCompletedEvent hce) {
X509Certificate cert = (X509Certificate)hce.getLocalCertificates()[0];
String certName = cert.getSubjectX500Principal().getName().substring(3,cert.getSubjectX500Principal().getName().indexOf(","));
System.out.println(certName);
}
});

打印 Unknown

此外,我使用以下命令检查了客户端的 keystore :

keytool -list -v -keystore clientStore.jks

哪个打印

Keystore-type: JKS
Keystore-provider: SUN

Keystore contains 1 entry

Aliasname: test
creation date: 23.04.2018
entry type: PrivateKeyEntry
certificate length: 1
certificate[1]:
owner: CN=test, OU="Org Unit", O=Org, L=City, ST=State, C=DE
...

如您所见,客户端存储证书的 cn 已设置。然而,我无法解释为什么它似乎没有被传输到服务器。

我很乐意提供各种帮助。

最好的问候,

加尔维斯顿01

最佳答案

After the handshake was successful, I would like to check the client certificate's cn on the server side.

要检查您收到了哪些证书,您需要调用 getPeerCertificates而不是 getLocalCertificates,它用于您发送的证书。

你应该仔细阅读文档:

public X500Principal getSubjectX500Principal()

Returns the subject (subject distinguished name) value from the certificate as an X500Principal. If the subject value is empty, then the getName() method of the returned X500Principal object returns an empty string ("").

因此,不建议在未先检查输入的情况下调用 indexOf() substring()

关于java - 握手后SSL套接字服务器获取证书cn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49982139/

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