I followed these threads and others with similar examples:
- How to create a secured TCP connection via TLS v.1.2 in Java
- Accept server's self-signed ssl certificate in Java client
I've already achieved a client/server simple Java program using any locally-generated self-signed certificates for both client and server. By the way I see they negotiate TLS 1.3 instead of 1.2. That's good.
But I see they are only using server certificate (client is configured to accept any, OK). I want them to require client to authenticate too. And allow client certificate to be any self-signed one, not signed by any CA. So I tried the server to use sslServerSocket.setNeedClientAuth(true)
or access to client certificate info. Then I get errors like this:
Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
at java.base/sun.security.ssl.SSLSessionImpl.getPeerPrincipal(SSLSessionImpl.java:1122)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
So I've been playing around with these tools to test both sides of my implementation:
ncat --ssl
ncat --ssl --listen
openssl s_client
openssl s_server
My conclusion is that my client is working good, authenticating on other servers when required. But my server needs to either provide the base CA to the client (I don't want this) OR just accept any client with no client authentication (that's what I have now, it's not my goal).
I want my server to require client certificate and accept any. How can I achieve this? Thanks!
Motivation: I want to implement some Syncthing well documented features using Java instead of Go. They use custom-certificate authentication on both sides of the connection.