简体   繁体   English

关于Java SSL客户端与服务器连接的问题

[英]Question about Java SSL connection between client and server

So I'm wanting to set up an SSL server/client in Java, my knowledge with.networking is not very good I've created normal servers/clients in java many times and wanted to up it and setup SSL so a user can't just send false packets to my client thinking its the server sending them.所以我想在 Java 中设置一个 SSL 服务器/客户端,我对网络的了解不是很好我已经在 java 中多次创建了普通服务器/客户端,并希望启动它并设置 SSL 这样用户就可以' t 只是向我的客户端发送虚假数据包,认为是服务器发送它们。

I've looked up a few examples etc and came across this GitHub repo which shows a basic server with SSL encryption between the client and server and set it up in my IDE. Here is the repo: https://github.com/AlphaGarden/SSL-Client-Server我查阅了一些示例等,发现了这个 GitHub 存储库,它显示了一个基本服务器,在客户端和服务器之间使用 SSL 加密,并在我的 IDE 中进行了设置。这是存储库: https://github.com/AlphaGarden /SSL-客户端-服务器

My question is why does the client and server use both certificates?我的问题是为什么客户端和服务器同时使用两个证书? Can't a user just get the certificates from the client and use them to decrypt the SSL?用户不能只从客户端获取证书并使用它们来解密 SSL 吗? Also in the client there's 2 strings, password & password2... Am I supposed to hide these from the person using my client too?在客户端中还有 2 个字符串,password 和 password2 ... 我是否也应该对使用我的客户端的人隐藏这些字符串? If not am I supposed to hide anything client sided from the user that could help them decrypt the SSL traffic and feed my client false information?如果不是,我是否应该向用户隐藏任何可以帮助他们解密 SSL 流量并向我的客户提供虚假信息的客户端?

Just some basics to explain a SSL/TLS connection: Wkipedia https://en.wikipedia.org/wiki/Transport_Layer_Security , for TLS 1.2 RFC5246: https://www.rfc-editor.org/rfc/rfc5246 and for TLS 1.3 RFC8446 https://www.rfc-editor.org/rfc/rfc8446 .只是解释 SSL/TLS 连接的一些基础知识:Wkipedia https://en.wikipedia.org/wiki/Transport_Layer_Security ,对于 TLS 1.2 RFC5246: https://www.rfc-editor.org/rfc/rfc5246和 TLS 1.3 RFC8446 https://www.rfc-editor.org/rfc/rfc8446

The basic principle for a secure connection is to use (each) a certificate on server and client's side and exchange them.安全连接的基本原则是在服务器端和客户端使用(每个)证书并交换它们。 The certificate itself is useless as it does not prove that you are whom you say to be.证书本身是无用的,因为它不能证明您就是您所说的那个人。 To get trust in the certificate the usual way is to "buy" a certificate from a Certificate Agency (CA) that checks your identity.要获得对证书的信任,通常的方法是从检查您身份的证书机构 (CA)“购买”证书。

The CA's root certificate usually is known to today browsers and so the server and client certificate can get checked by the browser (client) and server against the CA's root certificate. CA 的根证书通常为当今的浏览器所知,因此浏览器(客户端)和服务器可以根据 CA 的根证书检查服务器和客户端证书。

Let us see the code for the simple SSL Server & Client code.让我们看看简单的 SSL 服务器和客户端代码。 I'm for sure you noticed that the server and client are using "twisted" sources for the keystore and the truststore and therefore they need two (different) passwords to get access to the two stores:我确定您注意到服务器和客户端正在为密钥库信任库使用“扭曲”源,因此他们需要两个(不同的)密码才能访问这两个存储:

SSLServer: SSL服务器:

String password = "abcdefg";
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("server/certificate-server.p12");
// TrustManagerFactory
String password2 = "aabbcc";
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
InputStream inputStream1 = ClassLoader.getSystemClassLoader().getResourceAsStream("client/certificate-client.p12");

SSLClient: SSL客户端:

String password = "aabbcc";
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("client/certificate-client.p12");
// TrustManagerFactory ()
String password2 = "abcdefg";
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
InputStream inputStream1 = ClassLoader.getSystemClassLoader().getResourceAsStream("server/certificate-server.p12");

What are the keystore and the truststore good for?密钥库和信任库有什么用? The keystore holds the (own) private key (of server and client) and in the truststore (all) trusted certificates (usually the CA root certificates) are saved.密钥库保存(自己的)(服务器和客户端的)私钥,并且在信任库中保存(所有)受信任的证书(通常是 CA 根证书)。 To check the received certificate from the other party the server and client prove the certificate chain up to the root certificate from the CA.为了检查从另一方收到的证书,服务器和客户端证明证书链一直到来自 CA 的根证书。

But here is the problem - as the programs are using self signed certificates there is not "real" and saved root certificate available in the trust store.但这就是问题所在——因为程序使用的是自签名证书,所以在信任库中没有“真实的”和保存的根证书可用。 To get the programs to run without an user interaction ("do you trust this certificate?") both provide use a truststore with the "approved" certifcates and all is running.为了让程序在没有用户交互(“你信任这个证书吗?”)的情况下运行,两者都提供了使用带有“已批准”证书的信任库,并且所有程序都在运行。

To your second question "Can't a user just get the certificates from the client and use them to decrypt the SSL" the answer is simple: YES.对于您的第二个问题“用户不能只从客户端获取证书并使用它们来解密 SSL”,答案很简单:是。 But when securing a communication only between "allowed" partners - how should eg the servers knows that the client is the real one and not an attacker?但是,当仅在“允许的”合作伙伴之间保护通信时——例如,服务器应该如何知道客户端是真实的而不是攻击者? For that reason the client is sending a certificate as well that can get checked by the servers truststore.出于这个原因,客户端也会发送一个证书,该证书可以由服务器信任库进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM