KeyExchange.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00043 public abstract class KeyExchange{
00044 
00045   static final int PROPOSAL_KEX_ALGS=0;
00046   static final int PROPOSAL_SERVER_HOST_KEY_ALGS=1;
00047   static final int PROPOSAL_ENC_ALGS_CTOS=2;
00048   static final int PROPOSAL_ENC_ALGS_STOC=3;
00049   static final int PROPOSAL_MAC_ALGS_CTOS=4;
00050   static final int PROPOSAL_MAC_ALGS_STOC=5;
00051   static final int PROPOSAL_COMP_ALGS_CTOS=6;
00052   static final int PROPOSAL_COMP_ALGS_STOC=7;
00053   static final int PROPOSAL_LANG_CTOS=8;
00054   static final int PROPOSAL_LANG_STOC=9;
00055   static final int PROPOSAL_MAX=10;
00056 
00057   //static String kex_algs="diffie-hellman-group-exchange-sha1"+
00058   //                       ",diffie-hellman-group1-sha1";
00059 
00060 //static String kex="diffie-hellman-group-exchange-sha1";
00061   static String kex="diffie-hellman-group1-sha1";
00062   static String server_host_key="ssh-rsa,ssh-dss";
00063   static String enc_c2s="blowfish-cbc";
00064   static String enc_s2c="blowfish-cbc";
00065   static String mac_c2s="hmac-md5";     // hmac-md5,hmac-sha1,hmac-ripemd160,
00066                                         // hmac-sha1-96,hmac-md5-96
00067   static String mac_s2c="hmac-md5";
00068 //static String comp_c2s="none";        // zlib
00069 //static String comp_s2c="none";
00070   static String lang_c2s="";
00071   static String lang_s2c="";
00072 
00077   public static final int STATE_END=0;
00078 
00079   protected Session session=null;
00080   protected HASH sha=null;
00081   protected byte[] K=null;
00082   protected byte[] H=null;
00083   protected byte[] K_S=null;
00084 
00085 
00102   public abstract void init(Session session, 
00103                 byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception;
00104 
00112   public abstract boolean next(Buffer buf) throws Exception;
00113 
00118   public abstract String getKeyType();
00119 
00125   public abstract int getState();
00126 
00127   /*
00128   void dump(byte[] foo){
00129     for(int i=0; i<foo.length; i++){
00130       if((foo[i]&0xf0)==0)System.err.print("0");
00131       System.err.print(Integer.toHexString(foo[i]&0xff));
00132       if(i%16==15){System.err.println(""); continue;}
00133       if(i%2==1)System.err.print(" ");
00134     }
00135   } 
00136   */
00137 
00138   protected static String[] guess(byte[]I_S, byte[]I_C){
00139     String[] guess=new String[PROPOSAL_MAX];
00140     Buffer sb=new Buffer(I_S); sb.setOffSet(17);
00141     Buffer cb=new Buffer(I_C); cb.setOffSet(17);
00142 
00143     for(int i=0; i<PROPOSAL_MAX; i++){
00144       byte[] sp=sb.getString();  // server proposal
00145       byte[] cp=cb.getString();  // client proposal
00146       int j=0;
00147       int k=0;
00148 
00149       loop:
00150       while(j<cp.length){
00151     while(j<cp.length && cp[j]!=',')j++; 
00152     if(k==j) return null;
00153     String algorithm=Util.byte2str(cp, k, j-k);
00154     int l=0;
00155     int m=0;
00156     while(l<sp.length){
00157       while(l<sp.length && sp[l]!=',')l++; 
00158       if(m==l) return null;
00159       if(algorithm.equals(Util.byte2str(sp, m, l-m))){
00160         guess[i]=algorithm;
00161         break loop;
00162       }
00163       l++;
00164       m=l;
00165     }   
00166     j++;
00167     k=j;
00168       }
00169       if(j==0){
00170     guess[i]="";
00171       }
00172       else if(guess[i]==null){
00173     return null;
00174       }
00175     }
00176 
00177     if(JSch.getLogger().isEnabled(Logger.INFO)){
00178       JSch.getLogger().log(Logger.INFO, 
00179                            "kex: server->client"+
00180                            " "+guess[PROPOSAL_ENC_ALGS_STOC]+
00181                            " "+guess[PROPOSAL_MAC_ALGS_STOC]+
00182                            " "+guess[PROPOSAL_COMP_ALGS_STOC]);
00183       JSch.getLogger().log(Logger.INFO, 
00184                            "kex: client->server"+
00185                            " "+guess[PROPOSAL_ENC_ALGS_CTOS]+
00186                            " "+guess[PROPOSAL_MAC_ALGS_CTOS]+
00187                            " "+guess[PROPOSAL_COMP_ALGS_CTOS]);
00188     }
00189 
00190 //    for(int i=0; i<PROPOSAL_MAX; i++){
00191 //      System.err.println("guess: ["+guess[i]+"]");
00192 //    }
00193 
00194     return guess;
00195   }
00196 
00205   public String getFingerPrint(){
00206     HASH hash=null;
00207     try{
00208       Class c=Class.forName(session.getConfig("md5"));
00209       hash=(HASH)(c.newInstance());
00210     }
00211     catch(Exception e){ System.err.println("getFingerPrint: "+e); }
00212     return Util.getFingerPrint(hash, getHostKey());
00213   }
00214   byte[] getK(){ return K; }
00215   byte[] getH(){ return H; }
00216   HASH getHash(){ return sha; }
00217   byte[] getHostKey(){ return K_S; }
00218 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1