FileDocCategorySizeDatePackage
SecureMessageServiceClientHelper.javaAPI DocAzureus 3.0.3.45319Thu Sep 28 08:02:36 BST 2006com.aelitis.azureus.core.clientmessageservice.secure.impl

SecureMessageServiceClientHelper

public class SecureMessageServiceClientHelper extends Object implements com.aelitis.azureus.core.clientmessageservice.ClientMessageService

Fields Summary
private com.aelitis.azureus.core.clientmessageservice.ClientMessageService
delegate
private SecretKey
session_key
private byte[]
encryped_session_key
Constructors Summary
protected SecureMessageServiceClientHelper(String server_address, int server_port, int timeout_secs, String msg_type_id, RSAPublicKey public_key)

		try{
			KeyGenerator secret_key_gen = KeyGenerator.getInstance("DESede");
		
			session_key = secret_key_gen.generateKey();
				
			byte[] secret_bytes = session_key.getEncoded();
			
			try{
				Cipher	rsa_cipher = Cipher.getInstance( "RSA" );
		    
				rsa_cipher.init( Cipher.ENCRYPT_MODE, public_key );
		    
				encryped_session_key = rsa_cipher.doFinal( secret_bytes );
				
			}catch( Throwable e ){
				
					// fallback to the BC implementation for jdk1.4.2 as JCE RSA not available
				
				RSAEngine	eng = new RSAEngine();
				
				PKCS1Encoding	padded_eng = new PKCS1Encoding( eng );
				
	            CipherParameters param = RSAUtil.generatePublicKeyParameter(public_key);
	            
	            param = new ParametersWithRandom(param, new SecureRandom());
	            
	            padded_eng.init( true, param );
				
				encryped_session_key = padded_eng.processBlock(secret_bytes, 0, secret_bytes.length);
			}

		}catch( Throwable e ){
			
			e.printStackTrace();
			
			throw( new IOException( "Secure client message service initialisation fails - " + Debug.getNestedExceptionMessage(e)));
		}
		
		delegate = ClientMessageServiceClient.getServerService( server_address, server_port, msg_type_id );
	
Methods Summary
public voidclose()

		delegate.close();
	
public static com.aelitis.azureus.core.clientmessageservice.ClientMessageServicegetServerService(java.lang.String server_address, int server_port, int timeout_secs, java.lang.String msg_type_id, java.security.interfaces.RSAPublicKey public_key)

		return new SecureMessageServiceClientHelper( server_address, server_port, timeout_secs, msg_type_id, public_key );
	
public java.util.MapreceiveMessage()

		Map	secure_payload = delegate.receiveMessage();
		
		byte[]	encrypted_message	= (byte[])secure_payload.get( "content" );
		
		try{
			Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			
			cipher.init(Cipher.DECRYPT_MODE, session_key ); 
	
			byte[]	message_bytes = cipher.doFinal( encrypted_message );
	
			Map plain_payload = StaticUtilities.getFormatters().bDecode( message_bytes );

			return( plain_payload );
			
		}catch( Throwable e ){
			
			throw( new IOException( "send message failed - " + Debug.getNestedExceptionMessage(e)));
		}	
	
public voidsendMessage(java.util.Map plain_payload)

		Map	secure_payload = new HashMap();
		
		try{
		    byte[]	message_bytes = StaticUtilities.getFormatters().bEncode( plain_payload );
		    
			Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			
			cipher.init(Cipher.ENCRYPT_MODE, session_key ); 
	
			byte[]	encrypted_message = cipher.doFinal( message_bytes );
	
			secure_payload.put( "ver", "1" );
			secure_payload.put( "alg", "DESede" );
			secure_payload.put( "key", encryped_session_key );
			secure_payload.put( "content", encrypted_message );
			
		}catch( Throwable e ){
			
			throw( new IOException( "send message failed - " + Debug.getNestedExceptionMessage(e)));
		}
		
		delegate.sendMessage( secure_payload );
	
public voidsetMaximumMessageSize(int max_bytes)

		delegate.setMaximumMessageSize( max_bytes );