KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > KeyExchange


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jsch;
31
32 public abstract class KeyExchange{
33
34   static final int PROPOSAL_KEX_ALGS=0;
35   static final int PROPOSAL_SERVER_HOST_KEY_ALGS=1;
36   static final int PROPOSAL_ENC_ALGS_CTOS=2;
37   static final int PROPOSAL_ENC_ALGS_STOC=3;
38   static final int PROPOSAL_MAC_ALGS_CTOS=4;
39   static final int PROPOSAL_MAC_ALGS_STOC=5;
40   static final int PROPOSAL_COMP_ALGS_CTOS=6;
41   static final int PROPOSAL_COMP_ALGS_STOC=7;
42   static final int PROPOSAL_LANG_CTOS=8;
43   static final int PROPOSAL_LANG_STOC=9;
44   static final int PROPOSAL_MAX=10;
45
46   //static String kex_algs="diffie-hellman-group-exchange-sha1"+
47
// ",diffie-hellman-group1-sha1";
48

49 //static String kex="diffie-hellman-group-exchange-sha1";
50
static String JavaDoc kex="diffie-hellman-group1-sha1";
51   static String JavaDoc server_host_key="ssh-rsa,ssh-dss";
52   static String JavaDoc enc_c2s="blowfish-cbc";
53   static String JavaDoc enc_s2c="blowfish-cbc";
54   static String JavaDoc mac_c2s="hmac-md5"; // hmac-md5,hmac-sha1,hmac-ripemd160,
55
// hmac-sha1-96,hmac-md5-96
56
static String JavaDoc mac_s2c="hmac-md5";
57 //static String comp_c2s="none"; // zlib
58
//static String comp_s2c="none";
59
static String JavaDoc lang_c2s="";
60   static String JavaDoc lang_s2c="";
61
62   public static final int STATE_END=0;
63
64   protected Session session=null;
65   protected HASH sha=null;
66   protected byte[] K=null;
67   protected byte[] H=null;
68   protected byte[] K_S=null;
69
70   public abstract void init(Session session,
71                 byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception JavaDoc;
72   public abstract boolean next(Buffer buf) throws Exception JavaDoc;
73   public abstract String JavaDoc getKeyType();
74   public abstract int getState();
75
76   /*
77   void dump(byte[] foo){
78     for(int i=0; i<foo.length; i++){
79       if((foo[i]&0xf0)==0)System.err.print("0");
80       System.err.print(Integer.toHexString(foo[i]&0xff));
81       if(i%16==15){System.err.println(""); continue;}
82       if(i%2==1)System.err.print(" ");
83     }
84   }
85   */

86
87   protected static String JavaDoc[] guess(byte[]I_S, byte[]I_C){
88 //System.err.println("guess: ");
89
String JavaDoc[] guess=new String JavaDoc[PROPOSAL_MAX];
90     Buffer sb=new Buffer(I_S); sb.setOffSet(17);
91     Buffer cb=new Buffer(I_C); cb.setOffSet(17);
92
93     for(int i=0; i<PROPOSAL_MAX; i++){
94       byte[] sp=sb.getString(); // server proposal
95
byte[] cp=cb.getString(); // client proposal
96

97 //System.err.println("server-proposal: |"+new String(sp)+"|");
98
//System.err.println("client-proposal: |"+new String(cp)+"|");
99

100       int j=0;
101       int k=0;
102 //System.err.println(new String(cp));
103
loop:
104       while(j<cp.length){
105     while(j<cp.length && cp[j]!=',')j++;
106     if(k==j) return null;
107     String JavaDoc algorithm=new String JavaDoc(cp, k, j-k);
108 //System.err.println("algorithm: "+algorithm);
109
int l=0;
110     int m=0;
111     while(l<sp.length){
112       while(l<sp.length && sp[l]!=',')l++;
113       if(m==l) return null;
114 //System.err.println(" "+new String(sp, m, l-m));
115
if(algorithm.equals(new String JavaDoc(sp, m, l-m))){
116         guess[i]=algorithm;
117 //System.err.println(" "+algorithm);
118
break loop;
119       }
120       l++;
121       m=l;
122     }
123     j++;
124     k=j;
125       }
126       if(j==0){
127     guess[i]="";
128       }
129       else if(guess[i]==null){
130 //System.err.println(" fail");
131
return null;
132       }
133     }
134
135     if(JSch.getLogger().isEnabled(Logger.INFO)){
136       JSch.getLogger().log(Logger.INFO,
137                            "kex: server->client"+
138                            " "+guess[PROPOSAL_ENC_ALGS_STOC]+
139                            " "+guess[PROPOSAL_MAC_ALGS_STOC]+
140                            " "+guess[PROPOSAL_COMP_ALGS_STOC]);
141       JSch.getLogger().log(Logger.INFO,
142                            "kex: client->server"+
143                            " "+guess[PROPOSAL_ENC_ALGS_CTOS]+
144                            " "+guess[PROPOSAL_MAC_ALGS_CTOS]+
145                            " "+guess[PROPOSAL_COMP_ALGS_CTOS]);
146     }
147
148 // for(int i=0; i<PROPOSAL_MAX; i++){
149
// System.err.println("guess: ["+guess[i]+"]");
150
// }
151

152     return guess;
153   }
154
155   public String JavaDoc getFingerPrint(){
156     HASH hash=null;
157     try{
158       Class JavaDoc c=Class.forName(session.getConfig("md5"));
159       hash=(HASH)(c.newInstance());
160     }
161     catch(Exception JavaDoc e){ System.err.println("getFingerPrint: "+e); }
162     return Util.getFingerPrint(hash, getHostKey());
163   }
164   byte[] getK(){ return K; }
165   byte[] getH(){ return H; }
166   HASH getHash(){ return sha; }
167   byte[] getHostKey(){ return K_S; }
168 }
169
Popular Tags