1 2 29 30 package com.jcraft.jsch; 31 32 import java.net.*; 33 import java.util.Vector ; 34 35 class ChannelAgentForwarding extends Channel{ 36 37 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000; 38 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000; 39 40 private final int SSH2_AGENTC_REQUEST_IDENTITIES=11; 41 private final int SSH2_AGENT_IDENTITIES_ANSWER=12; 42 private final int SSH2_AGENTC_SIGN_REQUEST=13; 43 private final int SSH2_AGENT_SIGN_RESPONSE=14; 44 private final int SSH2_AGENTC_ADD_IDENTITY=17; 45 private final int SSH2_AGENTC_REMOVE_IDENTITY=18; 46 private final int SSH2_AGENTC_REMOVE_ALL_IDENTITIES=19; 47 private final int SSH2_AGENT_FAILURE=30; 48 49 boolean init=true; 50 51 private Buffer rbuf=null; 52 private Buffer wbuf=null; 53 private Packet packet=null; 54 private Buffer mbuf=null; 55 56 ChannelAgentForwarding(){ 57 super(); 58 59 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX); 60 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX); 61 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE); 62 63 type="auth-agent@openssh.com".getBytes(); 64 rbuf=new Buffer(); 65 rbuf.reset(); 66 mbuf=new Buffer(); 69 connected=true; 70 } 71 72 public void run(){ 73 try{ 74 sendOpenConfirmation(); 75 } 76 catch(Exception e){ 77 close=true; 78 disconnect(); 79 } 80 } 81 82 void write(byte[] foo, int s, int l) throws java.io.IOException { 83 84 if(packet==null){ 85 wbuf=new Buffer(rmpsize); 86 packet=new Packet(wbuf); 87 } 88 89 rbuf.shift(); 90 if(rbuf.buffer.length<rbuf.index+l){ 91 byte[] newbuf=new byte[rbuf.s+l]; 92 System.arraycopy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.length); 93 rbuf.buffer=newbuf; 94 } 95 96 rbuf.putByte(foo, s, l); 97 98 int mlen=rbuf.getInt(); 99 if(mlen>rbuf.getLength()){ 100 rbuf.s-=4; 101 return; 102 } 103 104 int typ=rbuf.getByte(); 105 106 Vector identities=getSession().jsch.identities; 107 UserInfo userinfo=getSession().getUserInfo(); 108 109 if(typ==SSH2_AGENTC_REQUEST_IDENTITIES){ 110 mbuf.reset(); 111 mbuf.putByte((byte)SSH2_AGENT_IDENTITIES_ANSWER); 112 synchronized(identities){ 113 int count=0; 114 for(int i=0; i<identities.size(); i++){ 115 Identity identity=(Identity)(identities.elementAt(i)); 116 if(identity.getPublicKeyBlob()!=null) 117 count++; 118 } 119 mbuf.putInt(count); 120 for(int i=0; i<identities.size(); i++){ 121 Identity identity=(Identity)(identities.elementAt(i)); 122 byte[] pubkeyblob=identity.getPublicKeyBlob(); 123 if(pubkeyblob==null) 124 continue; 125 mbuf.putString(pubkeyblob); 126 mbuf.putString("".getBytes()); 127 } 128 } 129 byte[] bar=new byte[mbuf.getLength()]; 130 mbuf.getByte(bar); 131 132 send(bar); 133 } 134 else if(typ==SSH2_AGENTC_SIGN_REQUEST){ 135 byte[] blob=rbuf.getString(); 136 byte[] data=rbuf.getString(); 137 int flags=rbuf.getInt(); 138 139 143 Identity identity=null; 144 synchronized(identities){ 145 for(int i=0; i<identities.size(); i++){ 146 Identity _identity=(Identity)(identities.elementAt(i)); 147 if(_identity.getPublicKeyBlob()==null) 148 continue; 149 if(!Util.array_equals(blob, _identity.getPublicKeyBlob())){ 150 continue; 151 } 152 if(_identity.isEncrypted()){ 153 if(userinfo==null) 154 continue; 155 while(_identity.isEncrypted()){ 156 if(!userinfo.promptPassphrase("Passphrase for "+_identity.getName())){ 157 break; 158 } 159 160 String _passphrase=userinfo.getPassphrase(); 161 if(_passphrase==null){ 162 break; 163 } 164 165 byte[] passphrase=Util.str2byte(_passphrase); 166 try{ 167 if(_identity.setPassphrase(passphrase)){ 168 break; 169 } 170 } 171 catch(JSchException e){ 172 break; 173 } 174 } 175 } 176 177 if(!_identity.isEncrypted()){ 178 identity=_identity; 179 break; 180 } 181 } 182 } 183 184 byte[] signature=null; 185 186 if(identity!=null){ 187 signature=identity.getSignature(data); 188 } 189 190 mbuf.reset(); 191 if(signature==null){ 192 mbuf.putByte((byte)SSH2_AGENT_FAILURE); 193 } 194 else{ 195 mbuf.putByte((byte)SSH2_AGENT_SIGN_RESPONSE); 196 mbuf.putString(signature); 197 } 198 199 byte[] bar=new byte[mbuf.getLength()]; 200 mbuf.getByte(bar); 201 202 send(bar); 203 } 204 } 205 206 private void send(byte[] message){ 207 packet.reset(); 208 wbuf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); 209 wbuf.putInt(recipient); 210 wbuf.putInt(4+message.length); 211 wbuf.putString(message); 212 213 try{ 214 session.write(packet, this, 4+message.length); 215 } 216 catch(Exception e){ 217 } 218 } 219 } 220 | Popular Tags |