1 /*
2  * Created on Jan 18, 2006
3  * Created by Alon Rohter
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 package com.aelitis.azureus.core.networkmanager.impl;
20 
21 import java.nio.ByteBuffer;
22 
23 
24 /**
25  *
26  */
27 public class TransportCryptoManager {
28 
29 	private static final TransportCryptoManager instance = new TransportCryptoManager();
30 
31 
getSingleton()32 	public static TransportCryptoManager getSingleton() {  return instance;  }
33 
34 
35 
36 	public void
manageCrypto( TransportHelper transport, byte[][] shared_secrets, boolean is_incoming, ByteBuffer initial_data, final HandshakeListener listener )37 	manageCrypto(
38 		TransportHelper				transport,
39 		byte[][]					shared_secrets,
40 		boolean 					is_incoming,
41 		ByteBuffer					initial_data,
42 		final HandshakeListener 	listener )
43 	{
44 			try{
45 				new ProtocolDecoderInitial(
46 						transport,
47 						shared_secrets,
48 						!is_incoming,
49 						initial_data,
50 						new ProtocolDecoderAdapter()
51 						{
52 							public int
53 							getMaximumPlainHeaderLength()
54 							{
55 								return( listener.getMaximumPlainHeaderLength());
56 							}
57 
58 							public int
59 							matchPlainHeader(
60 								ByteBuffer			buffer )
61 							{
62 								return( listener.matchPlainHeader( buffer ));
63 							}
64 
65 							public void
66 							gotSecret(
67 								byte[]				session_secret )
68 							{
69 								listener.gotSecret( session_secret );
70 							}
71 
72 							public void
73 							decodeComplete(
74 								ProtocolDecoder	decoder,
75 								ByteBuffer		remaining_initial_data )
76 							{
77 								listener.handshakeSuccess( decoder, remaining_initial_data );
78 							}
79 
80 							public void
81 							decodeFailed(
82 								ProtocolDecoder	decoder,
83 								Throwable			cause )
84 							{
85 								listener.handshakeFailure( cause );
86 							}
87 						});
88 			}catch( Throwable e ){
89 
90 				listener.handshakeFailure( e );
91 			}
92 
93 	}
94 
95 
96 
97 
98 	public interface HandshakeListener {
99 		public static final int MATCH_NONE						= ProtocolDecoderAdapter.MATCH_NONE;
100 		public static final int MATCH_CRYPTO_NO_AUTO_FALLBACK	= ProtocolDecoderAdapter.MATCH_CRYPTO_NO_AUTO_FALLBACK;
101 		public static final int MATCH_CRYPTO_AUTO_FALLBACK		= ProtocolDecoderAdapter.MATCH_CRYPTO_AUTO_FALLBACK;
102 
handshakeSuccess( ProtocolDecoder decoder, ByteBuffer remaining_initial_data )103 		public void handshakeSuccess( ProtocolDecoder decoder, ByteBuffer remaining_initial_data );
104 
handshakeFailure( Throwable failure_msg )105 		public void handshakeFailure( Throwable failure_msg );
106 
gotSecret( byte[] session_secret )107 		public void gotSecret( byte[] session_secret );
108 
getMaximumPlainHeaderLength()109 		public int getMaximumPlainHeaderLength();
110 
matchPlainHeader( ByteBuffer buffer )111 		public int matchPlainHeader( ByteBuffer buffer );
112   }
113 }
114