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

SSL socket server get certificate cn after handshake

我有一个 SSL 套接字服务器 运行 需要双向握手(为此我使用的是自签名证书)。握手成功后,我想在服务器端查看客户端证书的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

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

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()