001: package com.sun.portal.proxylet.crypt.jsse;
002:
003: import com.sun.portal.proxylet.crypt.jsse.NetletJSSEAuthContext;
004:
005: import javax.net.ssl.*;
006: import java.security.GeneralSecurityException;
007: import java.net.Socket;
008: import java.io.*;
009:
010: /**
011: *
012: * $name: com.sun.portal.proxylet.crypt.jsse.NetletJSSEWrapper
013: * $description The com.sun.portal.proxylet.crypt.jsse.NetletJSSEWrapper creates a PKCS12 store or JKS store based on the
014: * store name provided by the user. It then does the handshake with
015: * the server through a proxy, if proxy socket is set. The input and
016: * output stream can then be used to transfer data securely.
017: *
018: */
019: public class NetletJSSEWrapper {
020: /**
021: * The port the client will connect to
022: */
023: private int port = -1;
024: /**
025: * The host the client will connect to
026: */
027: private String host = null;
028: /**
029: * Used to generate a SocketFactory
030: */
031: private SSLContext sslContext = null;
032: /**
033: * Socket tunnel for proxy connections
034: */
035: private Socket tunnelSocket = null;
036: /**
037: * SSL Socket
038: */
039: private SSLSocket sslSocket = null;
040: /**
041: * This implements the functions needed to set the Keystore path and password.
042: */
043: private NetletJSSEAuthContext authContext = null;
044: /**
045: * InputStream to read data from
046: */
047: private InputStream is = null;
048: /**
049: * OutputStream to write data to server
050: */
051: private OutputStream os = null;
052: /**
053: * Indicates if a handshake was performed or not
054: */
055: private boolean handshake = false;
056: /**
057: * SocketFactory object
058: */
059: private SSLSocketFactory sf = null;
060:
061: /**
062: * Constructor that sets the host and port
063: * @param authContext
064: * @throws java.lang.Exception
065: */
066: public NetletJSSEWrapper(NetletJSSEAuthContext authContext)
067: throws Exception {
068: this .authContext = authContext;
069: init();
070: }
071:
072: /**
073: * init
074: * Initializes the context
075: * @throws java.security.GeneralSecurityException
076: * @throws java.io.IOException
077: */
078: public void init() throws GeneralSecurityException, IOException {
079: setupSSLContext();
080: sf = sslContext.getSocketFactory();
081: }
082:
083: /**
084: * Gets the InputStream of the SSLSocket for communication
085: * @return
086: * @throws java.io.IOException
087: */
088: public InputStream getInputStream() throws IOException {
089: return sslSocket.getInputStream();
090: }
091:
092: /**
093: * Gets the outputstream of the SSLSocket for communication
094: * @return
095: * @throws java.io.IOException
096: */
097: public OutputStream getOutputStream() throws IOException {
098: return sslSocket.getOutputStream();
099: }
100:
101: /**
102: * setupSSLContext : Sets the SSL context with the trustmanager and key manager
103: * The trustmanager accepts all certificates by default.
104: * @throws java.security.GeneralSecurityException
105: * @throws java.io.IOException
106: */
107: private void setupSSLContext() throws GeneralSecurityException,
108: IOException {
109: NetletTrustManager tm = new NetletTrustManager();
110: NetletKeyManager km = new NetletKeyManager(authContext);
111:
112: KeyManager[] kmg = { km };
113: TrustManager[] tmg = { tm };
114: sslContext = SSLContext.getInstance("SSL");
115: sslContext.init(kmg, tmg, null);
116: }
117:
118: /**
119: * connect
120: * Sets the SSL context and Initializes the SSL socket.
121: * @param host
122: * @param port
123: * @return
124: * @throws Exception
125: */
126: public SSLSocket connect(String host, int port, Socket ts)
127: throws Exception {
128: this .host = host;
129: this .port = port;
130: this .tunnelSocket = ts;
131: SSLSocket socket = null;
132: if (tunnelSocket != null)
133: socket = (SSLSocket) sf.createSocket(tunnelSocket, host,
134: port, false);
135: else {
136: try {
137: socket = (SSLSocket) sf.createSocket(host, port);
138: } catch (Exception e) {
139: throw e;
140: }
141: }
142:
143: /*socket.addHandshakeCompletedListener(
144: new HandshakeCompletedListener() {
145: public void handshakeCompleted(HandshakeCompletedEvent event) {
146: System.out.println("Handshake finished!");
147: System.out.println(
148: "\t CipherSuite:" + event.getCipherSuite());
149: System.out.println(
150: "\t SessionId " + event.getSession());
151: System.out.println(
152: "\t PeerHost " + event.getSession().getPeerHost());
153: }
154: }
155: );
156:
157: is = socket.getInputStream();
158: os = socket.getOutputStream();*/
159:
160: return socket;
161: }
162:
163: }
|