]>
Commit | Line | Data |
---|---|---|
0763e16d JW |
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
2 | /* | |
3 | Copyright (c) 2002-2010 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 kex="diffie-hellman-group1-sha1"; | |
51 | static String server_host_key="ssh-rsa,ssh-dss"; | |
52 | static String enc_c2s="blowfish-cbc"; | |
53 | static String enc_s2c="blowfish-cbc"; | |
54 | static String mac_c2s="hmac-md5"; // hmac-md5,hmac-sha1,hmac-ripemd160, | |
55 | // hmac-sha1-96,hmac-md5-96 | |
56 | static String mac_s2c="hmac-md5"; | |
57 | //static String comp_c2s="none"; // zlib | |
58 | //static String comp_s2c="none"; | |
59 | static String lang_c2s=""; | |
60 | static String 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; | |
72 | public abstract boolean next(Buffer buf) throws Exception; | |
73 | public abstract String 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[] guess(byte[]I_S, byte[]I_C){ | |
88 | String[] guess=new String[PROPOSAL_MAX]; | |
89 | Buffer sb=new Buffer(I_S); sb.setOffSet(17); | |
90 | Buffer cb=new Buffer(I_C); cb.setOffSet(17); | |
91 | ||
92 | for(int i=0; i<PROPOSAL_MAX; i++){ | |
93 | byte[] sp=sb.getString(); // server proposal | |
94 | byte[] cp=cb.getString(); // client proposal | |
95 | int j=0; | |
96 | int k=0; | |
97 | ||
98 | loop: | |
99 | while(j<cp.length){ | |
100 | while(j<cp.length && cp[j]!=',')j++; | |
101 | if(k==j) return null; | |
102 | String algorithm=Util.byte2str(cp, k, j-k); | |
103 | int l=0; | |
104 | int m=0; | |
105 | while(l<sp.length){ | |
106 | while(l<sp.length && sp[l]!=',')l++; | |
107 | if(m==l) return null; | |
108 | if(algorithm.equals(Util.byte2str(sp, m, l-m))){ | |
109 | guess[i]=algorithm; | |
110 | break loop; | |
111 | } | |
112 | l++; | |
113 | m=l; | |
114 | } | |
115 | j++; | |
116 | k=j; | |
117 | } | |
118 | if(j==0){ | |
119 | guess[i]=""; | |
120 | } | |
121 | else if(guess[i]==null){ | |
122 | return null; | |
123 | } | |
124 | } | |
125 | ||
126 | if(JSch.getLogger().isEnabled(Logger.INFO)){ | |
127 | JSch.getLogger().log(Logger.INFO, | |
128 | "kex: server->client"+ | |
129 | " "+guess[PROPOSAL_ENC_ALGS_STOC]+ | |
130 | " "+guess[PROPOSAL_MAC_ALGS_STOC]+ | |
131 | " "+guess[PROPOSAL_COMP_ALGS_STOC]); | |
132 | JSch.getLogger().log(Logger.INFO, | |
133 | "kex: client->server"+ | |
134 | " "+guess[PROPOSAL_ENC_ALGS_CTOS]+ | |
135 | " "+guess[PROPOSAL_MAC_ALGS_CTOS]+ | |
136 | " "+guess[PROPOSAL_COMP_ALGS_CTOS]); | |
137 | } | |
138 | ||
139 | // for(int i=0; i<PROPOSAL_MAX; i++){ | |
140 | // System.err.println("guess: ["+guess[i]+"]"); | |
141 | // } | |
142 | ||
143 | return guess; | |
144 | } | |
145 | ||
146 | public String getFingerPrint(){ | |
147 | HASH hash=null; | |
148 | try{ | |
149 | Class c=Class.forName(session.getConfig("md5")); | |
150 | hash=(HASH)(c.newInstance()); | |
151 | } | |
152 | catch(Exception e){ System.err.println("getFingerPrint: "+e); } | |
153 | return Util.getFingerPrint(hash, getHostKey()); | |
154 | } | |
155 | byte[] getK(){ return K; } | |
156 | byte[] getH(){ return H; } | |
157 | HASH getHash(){ return sha; } | |
158 | byte[] getHostKey(){ return K_S; } | |
159 | } |