org.bouncycastle.crypto.tls
Class TlsProtocolHandler

java.lang.Object
  extended by org.bouncycastle.crypto.tls.TlsProtocolHandler

public class TlsProtocolHandler
extends java.lang.Object

An implementation of all high level protocols in TLS 1.0.


Field Summary
protected static short AL_fatal
           
protected static short AL_warning
           
private  ByteQueue alertQueue
           
protected static short AP_access_denied
           
protected static short AP_bad_certificate
           
protected static short AP_bad_record_mac
           
protected static short AP_certificate_expired
           
protected static short AP_certificate_revoked
           
protected static short AP_certificate_unknown
           
protected static short AP_close_notify
           
protected static short AP_decode_error
           
protected static short AP_decompression_failure
           
protected static short AP_decrypt_error
           
protected static short AP_decryption_failed
           
protected static short AP_export_restriction
           
protected static short AP_handshake_failure
           
protected static short AP_illegal_parameter
           
protected static short AP_insufficient_security
           
protected static short AP_internal_error
           
protected static short AP_no_renegotiation
           
protected static short AP_protocol_version
           
protected static short AP_record_overflow
           
protected static short AP_unexpected_message
           
protected static short AP_unknown_ca
           
protected static short AP_unsupported_certificate
           
protected static short AP_user_canceled
           
private  boolean appDataReady
           
private  ByteQueue applicationDataQueue
           
private  ByteQueue changeCipherSpecQueue
           
private  TlsCipherSuite choosenCipherSuite
           
private  byte[] clientRandom
           
private  boolean closed
           
private  short connection_state
           
private static short CS_CLIENT_CHANGE_CIPHER_SPEC_SEND
           
private static short CS_CLIENT_FINISHED_SEND
           
private static short CS_CLIENT_HELLO_SEND
           
private static short CS_CLIENT_KEY_EXCHANGE_SEND
           
private static short CS_DONE
           
private static short CS_SERVER_CERTIFICATE_RECEIVED
           
private static short CS_SERVER_CHANGE_CIPHER_SPEC_RECEIVED
           
private static short CS_SERVER_HELLO_DONE_RECEIVED
           
private static short CS_SERVER_HELLO_RECEIVED
           
private static short CS_SERVER_KEY_EXCHANGE_RECEIVED
           
private static byte[] emptybuf
           
private  boolean failedWithError
           
private  ByteQueue handshakeQueue
           
private static short HP_CERTIFICATE
           
private static short HP_CERTIFICATE_REQUEST
           
private static short HP_CERTIFICATE_VERIFY
           
private static short HP_CLIENT_HELLO
           
private static short HP_CLIENT_KEY_EXCHANGE
           
private static short HP_FINISHED
           
private static short HP_HELLO_REQUEST
           
private static short HP_SERVER_HELLO
           
private static short HP_SERVER_HELLO_DONE
           
private static short HP_SERVER_KEY_EXCHANGE
           
private  byte[] ms
           
private  byte[] pms
           
private  java.security.SecureRandom random
           
private static short RL_ALERT
           
private static short RL_APPLICATION_DATA
           
private static short RL_CHANGE_CIPHER_SPEC
           
private static short RL_HANDSHAKE
           
private  RecordStream rs
           
private  byte[] serverRandom
           
private  RSAKeyParameters serverRsaKey
           
private static java.lang.String TLS_ERROR_MESSAGE
           
private  TlsInputStream tlsInputStream
           
private  TlsOuputStream tlsOutputStream
           
private  CertificateVerifyer verifyer
           
private  java.math.BigInteger Yc
           
 
Constructor Summary
TlsProtocolHandler(java.io.InputStream is, java.io.OutputStream os)
           
TlsProtocolHandler(java.io.InputStream is, java.io.OutputStream os, java.security.SecureRandom sr)
           
 
Method Summary
protected  void assertEmpty(java.io.ByteArrayInputStream is)
          Make sure the InputStream is now empty.
 void close()
          Closes this connection.
 void connect(CertificateVerifyer verifyer)
          Connects to the remote system.
protected  void failWithError(short alertLevel, short alertDescription)
          Terminate this connection whith an alert.
protected  void flush()
           
 TlsInputStream getTlsInputStream()
           
 TlsOuputStream getTlsOuputStream()
           
private  void processAlert()
           
private  void processApplicationData()
           
private  void processChangeCipherSpec()
          This method is called, when a change cipher spec message is received.
protected  void processData(short protocol, byte[] buf, int offset, int len)
           
private  void processHandshake()
           
protected  int readApplicationData(byte[] buf, int offset, int len)
          Read data from the network.
protected  void writeData(byte[] buf, int offset, int len)
          Send some application data to the remote system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RL_CHANGE_CIPHER_SPEC

private static final short RL_CHANGE_CIPHER_SPEC
See Also:
Constant Field Values

RL_ALERT

private static final short RL_ALERT
See Also:
Constant Field Values

RL_HANDSHAKE

private static final short RL_HANDSHAKE
See Also:
Constant Field Values

RL_APPLICATION_DATA

private static final short RL_APPLICATION_DATA
See Also:
Constant Field Values

HP_HELLO_REQUEST

private static final short HP_HELLO_REQUEST
See Also:
Constant Field Values

HP_CLIENT_HELLO

private static final short HP_CLIENT_HELLO
See Also:
Constant Field Values

HP_SERVER_HELLO

private static final short HP_SERVER_HELLO
See Also:
Constant Field Values

HP_CERTIFICATE

private static final short HP_CERTIFICATE
See Also:
Constant Field Values

HP_SERVER_KEY_EXCHANGE

private static final short HP_SERVER_KEY_EXCHANGE
See Also:
Constant Field Values

HP_CERTIFICATE_REQUEST

private static final short HP_CERTIFICATE_REQUEST
See Also:
Constant Field Values

HP_SERVER_HELLO_DONE

private static final short HP_SERVER_HELLO_DONE
See Also:
Constant Field Values

HP_CERTIFICATE_VERIFY

private static final short HP_CERTIFICATE_VERIFY
See Also:
Constant Field Values

HP_CLIENT_KEY_EXCHANGE

private static final short HP_CLIENT_KEY_EXCHANGE
See Also:
Constant Field Values

HP_FINISHED

private static final short HP_FINISHED
See Also:
Constant Field Values

CS_CLIENT_HELLO_SEND

private static final short CS_CLIENT_HELLO_SEND
See Also:
Constant Field Values

CS_SERVER_HELLO_RECEIVED

private static final short CS_SERVER_HELLO_RECEIVED
See Also:
Constant Field Values

CS_SERVER_CERTIFICATE_RECEIVED

private static final short CS_SERVER_CERTIFICATE_RECEIVED
See Also:
Constant Field Values

CS_SERVER_KEY_EXCHANGE_RECEIVED

private static final short CS_SERVER_KEY_EXCHANGE_RECEIVED
See Also:
Constant Field Values

CS_SERVER_HELLO_DONE_RECEIVED

private static final short CS_SERVER_HELLO_DONE_RECEIVED
See Also:
Constant Field Values

CS_CLIENT_KEY_EXCHANGE_SEND

private static final short CS_CLIENT_KEY_EXCHANGE_SEND
See Also:
Constant Field Values

CS_CLIENT_CHANGE_CIPHER_SPEC_SEND

private static final short CS_CLIENT_CHANGE_CIPHER_SPEC_SEND
See Also:
Constant Field Values

CS_CLIENT_FINISHED_SEND

private static final short CS_CLIENT_FINISHED_SEND
See Also:
Constant Field Values

CS_SERVER_CHANGE_CIPHER_SPEC_RECEIVED

private static final short CS_SERVER_CHANGE_CIPHER_SPEC_RECEIVED
See Also:
Constant Field Values

CS_DONE

private static final short CS_DONE
See Also:
Constant Field Values

AP_close_notify

protected static final short AP_close_notify
See Also:
Constant Field Values

AP_unexpected_message

protected static final short AP_unexpected_message
See Also:
Constant Field Values

AP_bad_record_mac

protected static final short AP_bad_record_mac
See Also:
Constant Field Values

AP_decryption_failed

protected static final short AP_decryption_failed
See Also:
Constant Field Values

AP_record_overflow

protected static final short AP_record_overflow
See Also:
Constant Field Values

AP_decompression_failure

protected static final short AP_decompression_failure
See Also:
Constant Field Values

AP_handshake_failure

protected static final short AP_handshake_failure
See Also:
Constant Field Values

AP_bad_certificate

protected static final short AP_bad_certificate
See Also:
Constant Field Values

AP_unsupported_certificate

protected static final short AP_unsupported_certificate
See Also:
Constant Field Values

AP_certificate_revoked

protected static final short AP_certificate_revoked
See Also:
Constant Field Values

AP_certificate_expired

protected static final short AP_certificate_expired
See Also:
Constant Field Values

AP_certificate_unknown

protected static final short AP_certificate_unknown
See Also:
Constant Field Values

AP_illegal_parameter

protected static final short AP_illegal_parameter
See Also:
Constant Field Values

AP_unknown_ca

protected static final short AP_unknown_ca
See Also:
Constant Field Values

AP_access_denied

protected static final short AP_access_denied
See Also:
Constant Field Values

AP_decode_error

protected static final short AP_decode_error
See Also:
Constant Field Values

AP_decrypt_error

protected static final short AP_decrypt_error
See Also:
Constant Field Values

AP_export_restriction

protected static final short AP_export_restriction
See Also:
Constant Field Values

AP_protocol_version

protected static final short AP_protocol_version
See Also:
Constant Field Values

AP_insufficient_security

protected static final short AP_insufficient_security
See Also:
Constant Field Values

AP_internal_error

protected static final short AP_internal_error
See Also:
Constant Field Values

AP_user_canceled

protected static final short AP_user_canceled
See Also:
Constant Field Values

AP_no_renegotiation

protected static final short AP_no_renegotiation
See Also:
Constant Field Values

AL_warning

protected static final short AL_warning
See Also:
Constant Field Values

AL_fatal

protected static final short AL_fatal
See Also:
Constant Field Values

emptybuf

private static final byte[] emptybuf

TLS_ERROR_MESSAGE

private static final java.lang.String TLS_ERROR_MESSAGE
See Also:
Constant Field Values

applicationDataQueue

private ByteQueue applicationDataQueue

changeCipherSpecQueue

private ByteQueue changeCipherSpecQueue

alertQueue

private ByteQueue alertQueue

handshakeQueue

private ByteQueue handshakeQueue

rs

private RecordStream rs

random

private java.security.SecureRandom random

serverRsaKey

private RSAKeyParameters serverRsaKey

tlsInputStream

private TlsInputStream tlsInputStream

tlsOutputStream

private TlsOuputStream tlsOutputStream

closed

private boolean closed

failedWithError

private boolean failedWithError

appDataReady

private boolean appDataReady

clientRandom

private byte[] clientRandom

serverRandom

private byte[] serverRandom

ms

private byte[] ms

choosenCipherSuite

private TlsCipherSuite choosenCipherSuite

Yc

private java.math.BigInteger Yc

pms

private byte[] pms

verifyer

private CertificateVerifyer verifyer

connection_state

private short connection_state
Constructor Detail

TlsProtocolHandler

public TlsProtocolHandler(java.io.InputStream is,
                          java.io.OutputStream os)

TlsProtocolHandler

public TlsProtocolHandler(java.io.InputStream is,
                          java.io.OutputStream os,
                          java.security.SecureRandom sr)
Method Detail

processData

protected void processData(short protocol,
                           byte[] buf,
                           int offset,
                           int len)
                    throws java.io.IOException
Throws:
java.io.IOException

processHandshake

private void processHandshake()
                       throws java.io.IOException
Throws:
java.io.IOException

processApplicationData

private void processApplicationData()

processAlert

private void processAlert()
                   throws java.io.IOException
Throws:
java.io.IOException

processChangeCipherSpec

private void processChangeCipherSpec()
                              throws java.io.IOException
This method is called, when a change cipher spec message is received.

Throws:
java.io.IOException - If the message has an invalid content or the handshake is not in the correct state.

connect

public void connect(CertificateVerifyer verifyer)
             throws java.io.IOException
Connects to the remote system.

Parameters:
verifyer - Will be used when a certificate is received to verify that this certificate is accepted by the client.
Throws:
java.io.IOException - If handshake was not successfull.

readApplicationData

protected int readApplicationData(byte[] buf,
                                  int offset,
                                  int len)
                           throws java.io.IOException
Read data from the network. The method will return immed, if there is still some data left in the buffer, or block untill some application data has been read from the network.

Parameters:
buf - The buffer where the data will be copied to.
offset - The position where the data will be placed in the buffer.
len - The maximum number of bytes to read.
Returns:
The number of bytes read.
Throws:
java.io.IOException - If something goes wrong during reading data.

writeData

protected void writeData(byte[] buf,
                         int offset,
                         int len)
                  throws java.io.IOException
Send some application data to the remote system.

The method will handle fragmentation internally.

Parameters:
buf - The buffer with the data.
offset - The position in the buffer where the data is placed.
len - The length of the data.
Throws:
java.io.IOException - If something goes wrong during sending.

getTlsOuputStream

public TlsOuputStream getTlsOuputStream()
Returns:
An OutputStream which can be used to send data.

getTlsInputStream

public TlsInputStream getTlsInputStream()
Returns:
An InputStream which can be used to read data.

failWithError

protected void failWithError(short alertLevel,
                             short alertDescription)
                      throws java.io.IOException
Terminate this connection whith an alert.

Can be used for normal closure too.

Parameters:
alertLevel - The level of the alert, an be AL_fatal or AL_warning.
alertDescription - The exact alert message.
Throws:
java.io.IOException - If alert was fatal.

close

public void close()
           throws java.io.IOException
Closes this connection.

Throws:
java.io.IOException - If something goes wrong during closing.

assertEmpty

protected void assertEmpty(java.io.ByteArrayInputStream is)
                    throws java.io.IOException
Make sure the InputStream is now empty. Fail otherwise.

Parameters:
is - The InputStream to check.
Throws:
java.io.IOException - If is is not empty.

flush

protected void flush()
              throws java.io.IOException
Throws:
java.io.IOException