| 1 |
import java.security.cert.CertificateFactory; |
| 2 |
import java.security.cert.X509Certificate; |
| 3 |
import java.util.Arrays; |
| 4 |
import java.security.KeyStore; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.io.OutputStream; |
| 7 |
import java.io.BufferedInputStream; |
| 8 |
import java.io.BufferedOutputStream; |
| 9 |
import javax.net.ssl.SSLContext; |
| 10 |
import javax.net.ssl.TrustManagerFactory; |
| 11 |
import javax.net.ssl.SSLSocketFactory; |
| 12 |
import javax.net.ssl.SSLSocket; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class SSLSocketClient { |
| 19 |
public static void main(String[] args) throws Exception { |
| 20 |
int port = Integer.parseInt(args[0]); |
| 21 |
String certificatePath = args[1]; |
| 22 |
String[] cipher = new String[] {args[2]}; |
| 23 |
|
| 24 |
|
| 25 |
String[] protocolList = Arrays.copyOfRange(args, 3, args.length); |
| 26 |
for (int i = 0; i < protocolList.length; i++) { |
| 27 |
protocolList[i] = sslProtocols(protocolList[i]); |
| 28 |
} |
| 29 |
|
| 30 |
String host = "localhost"; |
| 31 |
byte[] buffer = new byte[100]; |
| 32 |
|
| 33 |
SSLSocketFactory socketFactory = createSocketFactory(certificatePath, protocolList[0]); |
| 34 |
|
| 35 |
try ( |
| 36 |
SSLSocket socket = (SSLSocket)socketFactory.createSocket(host, port); |
| 37 |
OutputStream out = new BufferedOutputStream(socket.getOutputStream()); |
| 38 |
BufferedInputStream stdIn = new BufferedInputStream(System.in); |
| 39 |
) { |
| 40 |
socket.setEnabledProtocols(protocolList); |
| 41 |
socket.setEnabledCipherSuites(cipher); |
| 42 |
socket.startHandshake(); |
| 43 |
System.out.println("Starting handshake"); |
| 44 |
|
| 45 |
while (true) { |
| 46 |
int read = stdIn.read(buffer); |
| 47 |
if (read == -1) |
| 48 |
break; |
| 49 |
out.write(buffer, 0, read); |
| 50 |
} |
| 51 |
out.flush(); |
| 52 |
|
| 53 |
out.close(); |
| 54 |
socket.close(); |
| 55 |
|
| 56 |
} catch (Exception e) { |
| 57 |
e.printStackTrace(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public static SSLSocketFactory createSocketFactory(String certificatePath, String protocol) { |
| 62 |
|
| 63 |
try { |
| 64 |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 65 |
|
| 66 |
FileInputStream is = new FileInputStream(certificatePath); |
| 67 |
|
| 68 |
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(is); |
| 69 |
is.close(); |
| 70 |
|
| 71 |
KeyStore caKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 72 |
caKeyStore.load(null, null); |
| 73 |
caKeyStore.setCertificateEntry("ca-certificate", cert); |
| 74 |
|
| 75 |
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( |
| 76 |
TrustManagerFactory.getDefaultAlgorithm()); |
| 77 |
trustManagerFactory.init(caKeyStore); |
| 78 |
|
| 79 |
SSLContext context = SSLContext.getInstance(protocol); |
| 80 |
context.init(null, trustManagerFactory.getTrustManagers(), null); |
| 81 |
|
| 82 |
return context.getSocketFactory(); |
| 83 |
|
| 84 |
} catch(Exception e) { |
| 85 |
e.printStackTrace(); |
| 86 |
} |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
public static String sslProtocols(String s2nProtocol) { |
| 91 |
switch (s2nProtocol) { |
| 92 |
case "TLS1.3": |
| 93 |
return "TLSv1.3"; |
| 94 |
case "TLS1.2": |
| 95 |
return "TLSv1.2"; |
| 96 |
case "TLS1.1": |
| 97 |
return "TLSv1.1"; |
| 98 |
case "TLS1.0": |
| 99 |
return "TLSv1.0"; |
| 100 |
|
| 101 |
case "SSLv2Hello": |
| 102 |
return "SSLv2Hello"; |
| 103 |
} |
| 104 |
|
| 105 |
throw new RuntimeException("unrecognized protocol version:" + s2nProtocol); |
| 106 |
} |
| 107 |
} |