001 package examples.security.sslclient;
002
003 import java.io.*;
004 import java.security.KeyStore;
005 import java.security.PrivateKey;
006 import java.security.cert.Certificate;
007 import javax.net.ssl.HandshakeCompletedListener;
008 import javax.net.ssl.SSLSocket;
009 import weblogic.security.SSL.HostnameVerifier;
010 import weblogic.security.SSL.SSLContext;
011 import weblogic.security.SSL.SSLSocketFactory;
012 import weblogic.security.SSL.TrustManager;
013
014 /**
015 * A Java client demonstrates connecting to a JSP served by WebLogic Server
016 * using the secure port and displays the results of the connection.
017 *
018 * @author Copyright (c) 1999,2009, Oracle and/or its affiliates. All Rights Reserved.
019 */
020 public class SSLSocketClient {
021
022 public void SSLSocketClient() {}
023
024 public static void main (String [] argv)
025 throws Exception {
026 if ((argv.length < 2) || (argv.length > 3)) {
027 System.out.println("usage: java SSLSocketClient host sslport <HostnameVerifier>");
028 System.out.println("example: java SSLSocketClient server2.weblogic.com 443 MyHVClassName");
029 System.exit(-1);
030 }
031
032 /////////////////////////////////
033
034 try {
035 System.out.println("\nhttps://" + argv[0] + ":" + argv[1]);
036 System.out.println(" Creating the SSLContext");
037 SSLContext sslCtx = SSLContext.getInstance("https");
038
039 File KeyStoreFile = new File ("mykeystore");
040 if (!KeyStoreFile.exists())
041 {
042 System.out.println("Keystore Error : mykeystore is not present in this directory.");
043 System.out.println("To create it run - ant createmykeystore.");
044 System.exit(0);
045 }
046
047 System.out.println(" Initializing the SSLContext with client\n" +
048 " identity (certificates and private key),\n" +
049 " HostnameVerifier, AND NulledTrustManager");
050
051
052 // Open the keystore, retrieve the private key, and certificate chain
053 KeyStore ks = KeyStore.getInstance("jks");
054 ks.load(new FileInputStream("mykeystore"), null);
055 PrivateKey key = (PrivateKey)ks.getKey("mykey", "testkey".toCharArray());
056 Certificate [] certChain = ks.getCertificateChain("mykey");
057 sslCtx.loadLocalIdentity(certChain, key);
058
059 HostnameVerifier hVerifier = null;
060 if (argv.length < 3)
061 hVerifier = new NulledHostnameVerifier();
062 else
063 hVerifier = (HostnameVerifier) Class.forName(argv[2]).newInstance();
064
065 sslCtx.setHostnameVerifier(hVerifier);
066
067 TrustManager tManager = new NulledTrustManager();
068
069 sslCtx.setTrustManager(tManager);
070
071 System.out.println(" Creating new SSLSocketFactory with SSLContext");
072 SSLSocketFactory sslSF = sslCtx.getSocketFactory();
073
074 System.out.println(" Creating and opening new SSLSocket with SSLSocketFactory");
075 // using createSocket(String hostname, int port)
076 SSLSocket sslSock = (SSLSocket) sslSF.createSocket(argv[0],
077 new Integer(argv[1]).intValue());
078 System.out.println(" SSLSocket created");
079
080
081 HandshakeCompletedListener mListener = null;
082 mListener = new MyListener();
083
084 sslSock.addHandshakeCompletedListener(mListener);
085
086 OutputStream out = sslSock.getOutputStream();
087
088 // Send a simple HTTP request
089 String req = "GET /examplesWebApp/SnoopServlet.jsp HTTP/1.0\r\n\r\n";
090 out.write(req.getBytes());
091
092 // Retrieve the InputStream and read the HTTP result, displaying
093 // it on the console
094 InputStream in = sslSock.getInputStream();
095 byte buf[] = new byte[1024];
096
097 try
098 {
099 while (true)
100 {
101 int amt = in.read(buf);
102 if (amt == -1) break;
103 System.out.write(buf, 0, amt);
104 }
105 }
106 catch (IOException e)
107 {
108 return;
109 }
110
111 sslSock.close();
112 System.out.println(" SSLSocket closed");
113
114 } catch (Exception e) {
115 System.err.println("An exception occurred: "+e.getMessage());
116 }
117 }
118
119
120
121 }
|