1 2 29 30 package com.jcraft.jsch; 31 32 public class DHGEX extends KeyExchange{ 33 34 private static final int SSH_MSG_KEX_DH_GEX_GROUP= 31; 35 private static final int SSH_MSG_KEX_DH_GEX_INIT= 32; 36 private static final int SSH_MSG_KEX_DH_GEX_REPLY= 33; 37 private static final int SSH_MSG_KEX_DH_GEX_REQUEST= 34; 38 39 static int min=1024; 40 41 static int preferred=1024; 43 static int max=1024; 44 45 48 static final int RSA=0; 49 static final int DSS=1; 50 private int type=0; 51 52 private int state; 53 54 DH dh; 56 57 byte[] V_S; 58 byte[] V_C; 59 byte[] I_S; 60 byte[] I_C; 61 62 private Buffer buf; 63 private Packet packet; 64 65 private byte[] p; 66 private byte[] g; 67 private byte[] e; 68 70 public void init(Session session, 71 byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception { 72 this.session=session; 73 this.V_S=V_S; 74 this.V_C=V_C; 75 this.I_S=I_S; 76 this.I_C=I_C; 77 78 try{ 79 Class c=Class.forName(session.getConfig("sha-1")); 80 sha=(HASH)(c.newInstance()); 81 sha.init(); 82 } 83 catch(Exception e){ 84 System.err.println(e); 85 } 86 87 buf=new Buffer(); 88 packet=new Packet(buf); 89 90 try{ 91 Class c=Class.forName(session.getConfig("dh")); 92 dh=(com.jcraft.jsch.DH)(c.newInstance()); 93 dh.init(); 94 } 95 catch(Exception e){ 96 throw e; 98 } 99 100 packet.reset(); 101 buf.putByte((byte)SSH_MSG_KEX_DH_GEX_REQUEST); 102 buf.putInt(min); 103 buf.putInt(preferred); 104 buf.putInt(max); 105 session.write(packet); 106 107 if(JSch.getLogger().isEnabled(Logger.INFO)){ 108 JSch.getLogger().log(Logger.INFO, 109 "SSH_MSG_KEX_DH_GEX_REQUEST("+min+"<"+preferred+"<"+max+") sent"); 110 JSch.getLogger().log(Logger.INFO, 111 "expecting SSH_MSG_KEX_DH_GEX_GROUP"); 112 } 113 114 state=SSH_MSG_KEX_DH_GEX_GROUP; 115 } 116 117 public boolean next(Buffer _buf) throws Exception { 118 int i,j; 119 switch(state){ 120 case SSH_MSG_KEX_DH_GEX_GROUP: 121 _buf.getInt(); 125 _buf.getByte(); 126 j=_buf.getByte(); 127 if(j!=SSH_MSG_KEX_DH_GEX_GROUP){ 128 System.err.println("type: must be SSH_MSG_KEX_DH_GEX_GROUP "+j); 129 return false; 130 } 131 132 p=_buf.getMPInt(); 133 g=_buf.getMPInt(); 134 143 dh.setP(p); 144 dh.setG(g); 145 146 151 e=dh.getE(); 152 153 packet.reset(); 154 buf.putByte((byte)SSH_MSG_KEX_DH_GEX_INIT); 155 buf.putMPInt(e); 156 session.write(packet); 157 158 if(JSch.getLogger().isEnabled(Logger.INFO)){ 159 JSch.getLogger().log(Logger.INFO, 160 "SSH_MSG_KEX_DH_GEX_INIT sent"); 161 JSch.getLogger().log(Logger.INFO, 162 "expecting SSH_MSG_KEX_DH_GEX_REPLY"); 163 } 164 165 state=SSH_MSG_KEX_DH_GEX_REPLY; 166 return true; 167 169 case SSH_MSG_KEX_DH_GEX_REPLY: 170 j=_buf.getInt(); 176 j=_buf.getByte(); 177 j=_buf.getByte(); 178 if(j!=SSH_MSG_KEX_DH_GEX_REPLY){ 179 System.err.println("type: must be SSH_MSG_KEX_DH_GEX_REPLY "+j); 180 return false; 181 } 182 183 K_S=_buf.getString(); 184 192 byte[] f=_buf.getMPInt(); 193 byte[] sig_of_H=_buf.getString(); 194 195 dh.setF(f); 196 K=dh.getK(); 197 198 216 buf.reset(); 217 buf.putString(V_C); buf.putString(V_S); 218 buf.putString(I_C); buf.putString(I_S); 219 buf.putString(K_S); 220 buf.putInt(min); buf.putInt(preferred); buf.putInt(max); 221 buf.putMPInt(p); buf.putMPInt(g); buf.putMPInt(e); buf.putMPInt(f); 222 buf.putMPInt(K); 223 224 byte[] foo=new byte[buf.getLength()]; 225 buf.getByte(foo); 226 sha.update(foo, 0, foo.length); 227 228 H=sha.digest(); 229 230 232 i=0; 233 j=0; 234 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 235 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 236 String alg=new String (K_S, i, j); 237 i+=j; 238 239 boolean result=false; 240 if(alg.equals("ssh-rsa")){ 241 byte[] tmp; 242 byte[] ee; 243 byte[] n; 244 245 type=RSA; 246 247 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 248 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 249 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 250 ee=tmp; 251 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 252 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 253 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 254 n=tmp; 255 256 259 SignatureRSA sig=null; 260 try{ 261 Class c=Class.forName(session.getConfig("signature.rsa")); 262 sig=(SignatureRSA)(c.newInstance()); 263 sig.init(); 264 } 265 catch(Exception e){ 266 System.err.println(e); 267 } 268 269 sig.setPubKey(ee, n); 270 sig.update(H); 271 result=sig.verify(sig_of_H); 272 273 if(JSch.getLogger().isEnabled(Logger.INFO)){ 274 JSch.getLogger().log(Logger.INFO, 275 "ssh_rsa_verify: signature "+result); 276 } 277 278 } 279 else if(alg.equals("ssh-dss")){ 280 byte[] q=null; 281 byte[] tmp; 282 283 type=DSS; 284 285 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 286 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 287 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 288 p=tmp; 289 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 290 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 291 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 292 q=tmp; 293 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 294 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 295 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 296 g=tmp; 297 j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| 298 ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); 299 tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; 300 f=tmp; 301 302 305 SignatureDSA sig=null; 306 try{ 307 Class c=Class.forName(session.getConfig("signature.dss")); 308 sig=(SignatureDSA)(c.newInstance()); 309 sig.init(); 310 } 311 catch(Exception e){ 312 System.err.println(e); 313 } 314 315 sig.setPubKey(f, p, q, g); 316 sig.update(H); 317 result=sig.verify(sig_of_H); 318 319 if(JSch.getLogger().isEnabled(Logger.INFO)){ 320 JSch.getLogger().log(Logger.INFO, 321 "ssh_dss_verify: signature "+result); 322 } 323 324 } 325 else{ 326 System.err.println("unknown alg"); 327 } 328 state=STATE_END; 329 return result; 330 } 331 return false; 332 } 333 334 public String getKeyType(){ 335 if(type==DSS) return "DSA"; 336 return "RSA"; 337 } 338 339 public int getState(){return state; } 340 } 341 | Popular Tags |