Guest User

Untitled

a guest
Aug 14th, 2019
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.net.*;
  2. import java.io.*;
  3. import javax.net.ssl.*;
  4. import javax.security.cert.X509Certificate;
  5. import java.security.KeyStore;
  6.  
  7. /*
  8. * This example shows how to set up a key manager to do client
  9. * authentication if required by server.
  10. *
  11. * This program assumes that the client is not inside a firewall.
  12. * The application can be modified to connect to a server outside
  13. * the firewall by following SSLSocketClientWithTunneling.java.
  14. */
  15. public class SSLSocketClientWithClientAuth {
  16.  
  17. public static void main(String[] args) throws Exception {
  18. String host = null;
  19. int port = -1;
  20. String path = null;
  21. for (int i = 0; i < args.length; i++)
  22. System.out.println(args[i]);
  23.  
  24. if (args.length < 3) {
  25. System.out.println(
  26. "USAGE: java SSLSocketClientWithClientAuth " +
  27. "host port requestedfilepath");
  28. System.exit(-1);
  29. }
  30.  
  31. try {
  32. host = args[0];
  33. port = Integer.parseInt(args[1]);
  34. path = args[2];
  35. } catch (IllegalArgumentException e) {
  36. System.out.println("USAGE: java SSLSocketClientWithClientAuth " +
  37. "host port requestedfilepath");
  38. System.exit(-1);
  39. }
  40.  
  41. try {
  42.  
  43. /*
  44. * Set up a key manager for client authentication
  45. * if asked by the server. Use the implementation's
  46. * default TrustStore and secureRandom routines.
  47. */
  48. SSLSocketFactory factory = null;
  49. try {
  50. SSLContext ctx;
  51. KeyManagerFactory kmf;
  52. KeyStore ks;
  53. char[] passphrase = "passphrase".toCharArray();
  54.  
  55. ctx = SSLContext.getInstance("TLS");
  56. kmf = KeyManagerFactory.getInstance("SunX509");
  57. ks = KeyStore.getInstance("JKS");
  58.  
  59. ks.load(new FileInputStream("testkeys"), passphrase);
  60.  
  61. kmf.init(ks, passphrase);
  62. ctx.init(kmf.getKeyManagers(), null, null);
  63.  
  64. factory = ctx.getSocketFactory();
  65. } catch (Exception e) {
  66. throw new IOException(e.getMessage());
  67. }
  68.  
  69. SSLSocket socket = (SSLSocket)factory.createSocket(host, port);
  70.  
  71. /*
  72. * send http request
  73. *
  74. * See SSLSocketClient.java for more information about why
  75. * there is a forced handshake here when using PrintWriters.
  76. */
  77. socket.startHandshake();
  78.  
  79. PrintWriter out = new PrintWriter(
  80. new BufferedWriter(
  81. new OutputStreamWriter(
  82. socket.getOutputStream())));
  83. out.println("GET " + path + " HTTP/1.0");
  84. out.println();
  85. out.flush();
  86.  
  87. /*
  88. * Make sure there were no surprises
  89. */
  90. if (out.checkError())
  91. System.out.println(
  92. "SSLSocketClient: java.io.PrintWriter error");
  93.  
  94. /* read response */
  95. BufferedReader in = new BufferedReader(
  96. new InputStreamReader(
  97. socket.getInputStream()));
  98.  
  99. String inputLine;
  100.  
  101. while ((inputLine = in.readLine()) != null)
  102. System.out.println(inputLine);
  103.  
  104. in.close();
  105. out.close();
  106. socket.close();
  107.  
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
RAW Paste Data