最终解决了所有问题,所以我来回答我自己的问题。这些是我用来解决我的特定问题的设置/文件;
客户端的密钥存储库是一个PKCS#12格式文件,包含
客户端的公共证书(在本例中由自签名CA签名)
客户端的私钥
例如,我使用OpenSSL的pkcs12命令来生成它;
openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12 -name "Whatever"
提示:确保你得到最新的OpenSSL,而不是0.9.8h版本,因为它似乎有一个错误,不允许你正确生成PKCS#12文件。
当服务器显式地请求客户端进行身份验证时,Java客户端将使用这个PKCS#12文件向服务器提供客户端证书。请参阅维基百科上关于TLS的文章,了解用于客户端证书身份验证的协议实际上是如何工作的(还解释了为什么这里需要客户端私钥)。
客户端的信任库是一个直接的JKS格式文件,其中包含根证书或中间CA证书。这些CA证书将决定允许您与哪些端点通信,在这种情况下,它将允许您的客户端连接到提供由信任存储库的CA之一签名的证书的任何服务器。
例如,你可以使用标准的Java keytool来生成它;
keytool -genkey -dname "cn=CLIENT" -alias truststorekey -keyalg RSA -keystore ./client-truststore.jks -keypass whatever -storepass whatever
keytool -import -keystore ./client-truststore.jks -file myca.crt -alias myca
使用这个信任库,您的客户端将尝试与所有提供由myca.crt标识的CA签名的证书的服务器进行一次完整的SSL握手。
上面的文件严格地只针对客户端。当您还想设置服务器时,服务器需要自己的密钥和信任库文件。在这个网站上可以找到关于为Java客户端和服务器(使用Tomcat)设置一个完全工作的示例的很好的演练。
问题/评论/建议
Client certificate authentication can only be enforced by the server.
(Important!) When the server requests a client certificate (as part of the TLS handshake), it will also provide a list of trusted CA's as part of the certificate request. When the client certificate you wish to present for authentication is not signed by one of these CA's, it won't be presented at all (in my opinion, this is weird behaviour, but I'm sure there's a reason for it). This was the main cause of my issues, as the other party had not configured their server properly to accept my self-signed client certificate and we assumed that the problem was at my end for not properly providing the client certificate in the request.
Get Wireshark. It has great SSL/HTTPS packet analysis and will be a tremendous help debugging and finding the problem. It's similar to -Djavax.net.debug=ssl but is more structured and (arguably) easier to interpret if you're uncomfortable with the Java SSL debug output.
It's perfectly possible to use the Apache httpclient library. If you want to use httpclient, just replace the destination URL with the HTTPS equivalent and add the following JVM arguments (which are the same for any other client, regardless of the library you want to use to send/receive data over HTTP/HTTPS):
-Djavax.net.debug=ssl
-Djavax.net.ssl.keyStoreType=pkcs12
-Djavax.net.ssl.keyStore=client.p12
-Djavax.net.ssl.keyStorePassword=whatever
-Djavax.net.ssl.trustStoreType=jks
-Djavax.net.ssl.trustStore=client-truststore.jks
-Djavax.net.ssl.trustStorePassword=whatever