Java 中自签名证书的 SSL 如何工作

How SSL works in case of self signed certificates in Java

我正在开发一个 HTTPS 服务,它将部署在一个带有自签名证书的服务器上,一个客户端将接受所有 certificates.I 我是 SSL 的新手。

我已经完成 this post and this post 并了解如何配置信任管理器以接受所有证书。

如果我理解正确,java 客户端将使用服务器的 public 密钥(通过使用服务器的 public 证书安装)来加密要发送到 server.And 的数据然后服务器使用它的私钥来解密数据。

我的问题是:

1.In 一个常见的场景,我们在客户端机器的信任库中安装 server.cer(java 中的 cacerts)。 java 客户端代码如何链接到此已安装的 public 密钥以在与此类服务器通信时加密数据?谁在这里进行SSL加密?是 Java API 执行此操作,还是我必须在将数据发送到服务器之前在我的客户端代码中处理加密?

2.As在上面提到的post之一中给出,我们可以让客户端接受所有certificates.How在这种情况下加密和解密是否有效?由于我们没有在客户端的信任库中安装任何特定于服务器的密钥,因此客户端如何知道要使用哪个 public 密钥进行加密?

我正在寻找更多技术细节。

I am working on a HTTPS service which will be deployed on a server with a self signed certificate and a client which will accept all certificates. I am new to SSL.

很明显。您正在使用不安全的系统。这是浪费时间。您也可以根本不使用 SSL。客户端应该接受 this 证书和所有 CA 证书,而不是所有证书。如前所述,您的系统容易受到中间人攻击。

If I understand correctly a java client will use server's public key(installed by using server's public certificate) to encrypt data to be sent to a server.

你不知道。它没有。证书用于验证服务器的身份,但 SSL 加密是通过秘密协商的会话密钥对称的,与证书中的 public 密钥无关。

And a server then uses it's private key to decrypt the data.

没有

How does the java client code links to this installed public key to encrypt data while communicating to such server?

通常通过 javax.net.ssl.trustStore 属性,但同样它不使用它进行加密,仅用于身份验证。

Who does the SSL encryption here? Is it Java APIs that do it

是的。这是由 HttpsURLConnection 使用 javax.net.ssl.SSLSocket.

完成的

or do i have to handle encryption in my client code before sending data to server?

没有

As given in one of the above mentioned post ,we can make client accept all certificates.

你可以,但它非常不安全,永远不应该那样做。

How will the encryption and decryption work in this scenario? How will the client know which public key to use for encryption as we are not installing any server specific key in client's truststore?

看到上面,你在这里有一个误解,但是当你接受所有证书时你不知道你在和谁说话,所以任何加密都是毫无意义的。不要这样做。