BAEL-2250: Adding files for the article on SSL handshake failure. (#5541)

* BAEL-2250: Adding files for the article on SSL handshake failure.

* BAEL-2250 cleanup formatting

* Applied review feedback on the article.

* Adding cipher suite and protocol selection in server and client

* Corrected some code conventions.

* Revert: BAEL-2250 cleanup formatting
This commit is contained in:
Kumar Chandrakant 2018-10-28 03:22:33 +00:00 committed by Emily Cheyne
parent 901b733ca4
commit 0690a6332e
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.ssl.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class SimpleClient {
static void startClient(String host, int port) throws IOException {
SocketFactory factory = SSLSocketFactory.getDefault();
try (Socket connection = factory.createSocket(host, port)) {
((SSLSocket) connection).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLSocket) connection).setEnabledProtocols(
new String[] { "TLSv1.2"});
BufferedReader input = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
System.out.println(input.readLine());
}
}
public static void main(String[] args) throws IOException {
startClient("localhost", 1234);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.ssl.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
public class SimpleServer {
static void startServer(int port) throws IOException {
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
try (ServerSocket listener = factory.createServerSocket(port)) {
((SSLServerSocket) listener).setNeedClientAuth(true);
((SSLServerSocket) listener).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLServerSocket) listener).setEnabledProtocols(
new String[] { "TLSv1.2"});
while (true) {
try (Socket socket = listener.accept()) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
}
}
}
}
public static void main(String[] args) throws IOException {
startServer(1234);
}
}