I am trying to create a SSL TCP Connection from Java to a Vala Server. Everything works fine until I send a second package to the server. (also the first package sends fine). The server only receives the first byte (in this case the "1") of the second package, nothing else, but if I connect to the server without SSL everything works fine. I think that the server isn't the problem, because every other connection from another Vala client works pretty well.
I'm using an unstrusted Certificate, so I created a custom TrustManager and I'm using OpenJDK 7 (Elementary OS - Linux). Here's my code:
//Main:
SSLHandler handler = new SSLHandler();
handler.createSecureSocket("localhost", 7431);
byte[] data = {1,4,1,1,1,1};
handler.getOutputStream().write(data);
handler.getOutputStream().write(data);
// SSLHandler
public class SSLHandler {
// SSL Socket erstellen
SSLSocket sslSocket;
public void createSecureSocket(String ip, int port) throws UnknownHostException, IOException, KeyManagementException, NoSuchAlgorithmException {
SSLSocketFactory factory = (SSLSocketFactory) new DefaultTrustManager().createSSLFactory("TLS");
sslSocket = (SSLSocket) factory.createSocket(ip, port);
}
public OutputStream getOutputStream() throws IOException {
return sslSocket.getOutputStream();
}
public InputStream getInputStream() throws IOException {
return sslSocket.getInputStream();
}
}
//Custom Trust Manager
public class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public SSLSocketFactory createSSLFactory(String protocol) throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = SSLContext.getInstance(protocol);
TrustManager[] byPassTrustManager = new TrustManager[] {this};
sslContext.init(null, byPassTrustManager, new SecureRandom());
return sslContext.getSocketFactory();
}
}
Does anybody know a solution for this problem?