Created
October 3, 2016 16:30
-
-
Save VenkataRaju/01c18914e6027bd7a7068e4985e7351b to your computer and use it in GitHub Desktop.
TLS Without Certificates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.Socket; | |
import java.util.Arrays; | |
import java.util.LinkedHashSet; | |
import java.util.Set; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLServerSocket; | |
import javax.net.ssl.SSLServerSocketFactory; | |
import javax.net.ssl.SSLSocket; | |
import javax.net.ssl.SSLSocketFactory; | |
final class Server | |
{ | |
public static void main(String[] arstring) throws Throwable | |
{ | |
SSLContext sslContext = SSLContext.getDefault(); | |
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory(); | |
try (SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(9999)) | |
{ | |
String[] supportedAnonCipherSuites = Util | |
.getSupportedAnonCipherSuites(sslServerSocket.getSupportedCipherSuites()); | |
sslServerSocket.setEnabledCipherSuites(supportedAnonCipherSuites); | |
try (Socket socket = sslServerSocket.accept()) | |
{ | |
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
for (String line; (line = br.readLine()) != null;) | |
System.out.println(line); | |
} | |
} | |
} | |
} | |
final class Client | |
{ | |
public static void main(String[] arstring) throws Throwable | |
{ | |
SSLContext sslContext = SSLContext.getDefault(); | |
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | |
try (SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 9999)) | |
{ | |
String[] supportedAnonCipherSuites = Util | |
.getSupportedAnonCipherSuites(sslSocket.getSupportedCipherSuites()); | |
sslSocket.setEnabledCipherSuites(supportedAnonCipherSuites); | |
System.out.println("Connected to server"); | |
System.out.printf("Enter your input: "); | |
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bufferedwriter = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream())); | |
for (String input; (input = userInput.readLine()) != null;) | |
{ | |
System.out.println("Sending input: " + input); | |
bufferedwriter.append(input).append('\n').flush(); | |
System.out.println("Your input is sent"); | |
System.out.printf("Enter your input: "); | |
} | |
} | |
} | |
} | |
final class Util | |
{ | |
private static final Set<String> ANON_CIPHER_SUITES = new LinkedHashSet<>(Arrays.asList( | |
"TLS_DH_anon_WITH_AES_256_GCM_SHA384", | |
"TLS_DH_anon_WITH_AES_128_GCM_SHA256", | |
"TLS_DH_anon_WITH_AES_256_CBC_SHA256", | |
"TLS_DH_anon_WITH_AES_128_CBC_SHA256", | |
"TLS_ECDH_anon_WITH_AES_128_CBC_SHA", | |
"TLS_DH_anon_WITH_AES_128_CBC_SHA")); | |
static String[] getSupportedAnonCipherSuites(String[] supportedCipherSuites) | |
{ | |
Set<String> cipherSuites = new LinkedHashSet<>(Arrays.asList(supportedCipherSuites)); | |
cipherSuites.retainAll(ANON_CIPHER_SUITES); | |
if (cipherSuites.isEmpty()) | |
throw new RuntimeException("No predefined anon Cipher Suiters are supported"); | |
return cipherSuites.toArray(new String[cipherSuites.size()]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment