Issue with Java SSLSocket



我目前正在处理一个客户端-服务器项目。
出于安全原因,我决定应该使用 SSL。

我尝试将所有SocketServerSocket对象转换为它们的 SSL 变体,但无济于事。
当我使用SSLServerSocket并连接到它时,服务器端的套接字输出在outputstream.write(byte[])中间冻结。

以下是我用来创建SSL套接字的函数,ContextController.CONTEXTSSLContextConstants.DEBUG是我切换SSL的快速方法:

public static ServerSocket server(int port, int backlog) throws IOException {
return Constants.DEBUG ? new ServerSocket(port, backlog) : CONTEXT.getServerSocketFactory().createServerSocket(port, backlog);
}
public static Socket client(InetSocketAddress address) throws IOException {
return Constants.DEBUG ? new Socket(address.getHostName(), address.getPort())
: ContextController.CONTEXT.getSocketFactory().createSocket(address.getHostName(), address.getPort());
}
public static Socket client() throws IOException {
return Constants.DEBUG ? new Socket() : ContextController.CONTEXT.getSocketFactory().createSocket();
}

Constants.DEBUG = true(即SSL关闭(时它可以工作,但是当SSL处于打开状态时,它会在无限期发送一些数据后如上所述冻结。我该如何解决这个问题?

编辑
这是ContextController.CONTEXT的来源:
(注意,我知道我应该使用实际的TrustManager但首先我希望它起作用(

if (server) {// load the keystore
try (FileInputStream fis = new FileInputStream(ks)) {
CONTEXT = SSLContext.getInstance("SSL");
// load the keystore from the file
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(fis, PASSWORD);
// setup the KeyManagerFactory (used by the server)
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keystore, PASSWORD);
CONTEXT.init(kmf.getKeyManagers(), null, null);
}
catch (Exception e) {
}
}
else {
CONTEXT = SSLContext.getInstance("SSL");
CONTEXT.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} }, null);
}

编辑2
这是它冻结位置的堆栈跟踪,请注意它不会崩溃,它会挂起:

Thread [ClientExecThread #0] (Suspended)    
owns: AppOutputStream  (id=54)  
owns: ByteArrayOutputStream  (id=55)    
SocketOutputStream.socketWrite0(FileDescriptor, byte[], int, int) line: not available [native method]   
SocketOutputStream.socketWrite(byte[], int, int) line: 111  
SocketOutputStream.write(byte[], int, int) line: 155    
OutputRecord.writeBuffer(OutputStream, byte[], int, int, int) line: 431 
OutputRecord.write(OutputStream, boolean, ByteArrayOutputStream) line: 417  
SSLSocketImpl.writeRecordInternal(OutputRecord, boolean) line: 876  
SSLSocketImpl.writeRecord(OutputRecord, boolean) line: 847  
AppOutputStream.write(byte[], int, int) line: 123   
ByteArrayOutputStream.writeTo(OutputStream) line: 167   
SocketStream.streamPacket(Packet) line: 181 
ClientThread.lambda$8(Group) line: 150  
1427646530.run(Object) line: not available  
ClientThread.execute() line: 444    
ClientThread$ClientExecThread.run() line: 261

我在SocketStream.streamPacket(Packet) 181之后的行中放置了一个断点,但它永远不会到达它。

我真的不知道为什么会发生这种情况,我不需要SSL的所有方面(只有加密部分,这就是为什么我没有实现TrustManager(,所以我决定实现一个Diffie Hellman。

这按预期工作,并将DH与AES相结合,我根据我的要求加密了流量。

谢谢大家的帮助!

相关内容

  • 没有找到相关文章

最新更新