FileDocCategorySizeDatePackage
SSLStreamConnection.javaAPI DocphoneME MR2 API (J2ME)13244Wed May 02 18:00:00 BST 2007com.sun.midp.ssl

SSLStreamConnection

public class SSLStreamConnection extends Object implements javax.microedition.io.StreamConnection
The SSLStreamConnection class implements the StreamConnection interface. Data exchanged through a SSLStreamConnection is automatically protected by SSL. Currently, only SSL version 3.0 is supported and the list of cipher suites proposed by the client is hardcoded to {SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_EXPORT_WITH_RC4_40_MD5}. This version of the implementation does not support client authentication at the SSL layer -- a feature that is rarely used. Typical usage of this class by an application would be along the following lines:
// create a TCP connection
StreamConnection t = Connector.open("socket://www.server.com:443");

// Create an SSL connection
SSLStreamConnection s = new SSLStreamConnection("www.server.com", 443,
t.openInputStream(), t.openOutputStream());
t.close();

// obtain the associated input/output streams
OutputStream sout = s.openOutputStream();
InputStream sin = s.openInputStream();
...
// send SSL-protected data by writing to sout and
// receive SSL-protected by reading from sin
...
sin.close();
sout.close();
s.close(); // close the SSL connection when done

Fields Summary
static final int
READY
Indicates that a is ready to be opened.
static final int
OPEN
Indicates that a stream is opened.
static final int
CLOSED
Indicates that a stream is closed.
private Record
rec
Current record being processed.
private In
uin
Input stream for buffered records.
private Out
uout
Output stream for buffered records.
private InputStream
sin
Raw encrypted input stream.
private OutputStream
sout
Raw encrypted output stream.
private String
host
Current host name.
private int
port
Current port number.
private boolean
copen
Flag indicating the underlying TCP connection is open.
private X509Certificate
serverCert
Server certificate from a successful handshake.
private String
cipherSuite
Cipher suite from a successful handshake.
int
inputStreamState
State of the input stream given out by getInputStream.
int
outputStreamState
State of the output stream given out by getOutputStream.
Constructors Summary
public SSLStreamConnection(String host, int port, InputStream in, OutputStream out, CertStore cs)
Establish and SSL session over a reliable stream. This connection will forward the input and output stream close methods to the given connection. If the caller wants to have the given connection closed with this connection, the caller can close given connection after constructing this connection, but leaving the closing of the streams to this connection.

param
host hostname of the SSL server
param
port port number of the SSL server
param
in InputStream associated with the StreamConnection
param
out OutputStream associated with the StreamConnection
param
cs trusted certificate store to be used for this connection
exception
IOException if there is a problem initializing the SSL data structures or the SSL handshake fails

    
                                                                                                                               
        
                                  
                                
              

        if (cs == null) {
            throw new IllegalArgumentException(
                "SSLStreamConnection: no trusted certificate store given");
        }

        if ((in == null) || (out == null)) {
            throw new IllegalArgumentException(
                "SSLStreamConnection: stream missing");
        }

        this.host = host;
        this.port = port;
        this.sin = in;
        this.sout = out;

        this.rec = new Record(sin, sout);

        uin = new In(rec, this);
        uout = new Out(rec, this);
        
        try {
            Handshake hndshk = new Handshake(host, port, rec, cs);

            hndshk.doHandShake(Record.CLIENT);
            serverCert = hndshk.sCert;
            cipherSuite = hndshk.negSuiteName;
        } catch (IOException e) {
            cleanupIfNeeded();
            throw e;
        }

        copen = true;
    
Methods Summary
voidcleanupIfNeeded()
Closes the SSL connection. The underlying TCP socket, over which SSL is layered, is also closed unless the latter was opened by an external application and its input/output streams were passed as argument to the SSLStreamConnection constructor.

exception
IOException if the SSL connection could not be terminated cleanly

        if (copen || inputStreamState == OPEN || outputStreamState == OPEN ||
               rec == null) {
            // we do not need to cleanup
            return;
        }

        rec.shutdownConnection();
        rec = null;
    
public synchronized voidclose()
Closes the SSL connection. The underlying TCP socket, over which SSL is layered, is also closed unless the latter was opened by an external application and its input/output streams were passed as argument to the SSLStreamConnection constructor.

exception
IOException if the SSL connection could not be terminated cleanly

        if (copen) {
            copen = false;
            cleanupIfNeeded();
        }
    
java.lang.StringgetCipherSuite()
Returns the cipher suite in use for the connection. The value returned is one of the CipherSuite definitions in Appendix C of RFC 2246. The cipher suite string should be used to represent the actual parameters used to establish the connection regardless of whether the secure connection uses SSL V3 or TLS 1.0 or WTLS.

return
a String containing the cipher suite in use

        return cipherSuite;
    
public javax.microedition.io.SecurityInfogetSecurityInfo()
Returns the security information associated with this connection.

return
the security information associated with this open connection
exception
IOException if the connection is closed

        if (!copen) {
            throw new IOException("Connection closed");
        }

        return new SSLSecurityInfo(this);
    
public X509CertificategetServerCertificate()
Returns the server certificate associated with this connection.

return
the server certificate associated with this connection

        return serverCert;
    
public java.io.DataInputStreamopenDataInputStream()
Returns the DataInputStream associated with this SSLStreamConnection.

exception
IOException if the connection is not open or the stream was already open
return
a DataInputStream object

	return (new DataInputStream(openInputStream()));
    
public java.io.DataOutputStreamopenDataOutputStream()
Returns the DataOutputStream associated with this SSLStreamConnection.

exception
IOException if the connection is not open or the stream was already open
return
a DataOutputStream object

          return (new DataOutputStream(openOutputStream()));
    
public synchronized java.io.InputStreamopenInputStream()
Returns the InputStream associated with this SSLStreamConnection.

return
InputStream object from which SSL protected bytes can be read
exception
IOException if the connection is not open or the stream was already open

        if (!copen) {
            throw new IOException("Connection closed");
        }

        if (inputStreamState != READY) {
            throw new IOException("Input stream already opened");
        }

        inputStreamState = OPEN;
        return (uin);
    
public synchronized java.io.OutputStreamopenOutputStream()
Returns the OutputStream associated with this SSLStreamConnection.

return
OutputStream object such that bytes written to this stream are sent over an SSL secured channel
exception
IOException if the connection is not open or the stream was already open

        if (!copen) {
            throw new IOException("Connection closed");
        }

        if (outputStreamState != READY) {
            throw new IOException("Output stream already opened");
        }

        outputStreamState = OPEN;
        return (uout);