ChannelAgentForwarding.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2006-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 
00032 import java.net.*;
00033 import java.util.Vector;
00034 
00035 class ChannelAgentForwarding extends Channel{
00036 
00037   static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
00038   static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00039 
00040   private final int SSH2_AGENTC_REQUEST_IDENTITIES=11;
00041   private final int SSH2_AGENT_IDENTITIES_ANSWER=12;
00042   private final int SSH2_AGENTC_SIGN_REQUEST=13;
00043   private final int SSH2_AGENT_SIGN_RESPONSE=14;
00044   private final int SSH2_AGENTC_ADD_IDENTITY=17;
00045   private final int SSH2_AGENTC_REMOVE_IDENTITY=18;
00046   private final int SSH2_AGENTC_REMOVE_ALL_IDENTITIES=19;
00047   private final int SSH2_AGENT_FAILURE=30;
00048 
00049   boolean init=true;
00050 
00051   private Buffer rbuf=null;
00052   private Buffer wbuf=null;
00053   private Packet packet=null;
00054   private Buffer mbuf=null;
00055 
00056   ChannelAgentForwarding(){
00057     super();
00058 
00059     setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00060     setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00061     setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00062 
00063     type=Util.str2byte("auth-agent@openssh.com");
00064     rbuf=new Buffer();
00065     rbuf.reset();
00066     //wbuf=new Buffer(rmpsize);
00067     //packet=new Packet(wbuf);
00068     mbuf=new Buffer();
00069     connected=true;
00070   }
00071 
00072   public void run(){
00073     try{
00074       sendOpenConfirmation();
00075     }
00076     catch(Exception e){
00077       close=true;
00078       disconnect();
00079     }
00080   }
00081 
00082   void write(byte[] foo, int s, int l) throws java.io.IOException {
00083 
00084     if(packet==null){
00085       wbuf=new Buffer(rmpsize);
00086       packet=new Packet(wbuf);
00087     }
00088 
00089     rbuf.shift();
00090     if(rbuf.buffer.length<rbuf.index+l){
00091       byte[] newbuf=new byte[rbuf.s+l];
00092       System.arraycopy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.length);
00093       rbuf.buffer=newbuf;
00094     }
00095 
00096     rbuf.putByte(foo, s, l);
00097 
00098     int mlen=rbuf.getInt();
00099     if(mlen>rbuf.getLength()){
00100       rbuf.s-=4;
00101       return;
00102     }
00103 
00104     int typ=rbuf.getByte();
00105 
00106     Session _session=null;
00107     try{
00108       _session=getSession();
00109     }
00110     catch(JSchException e){
00111       throw new java.io.IOException(e.toString());
00112     }
00113 
00114     Vector identities=_session.jsch.identities;
00115     UserInfo userinfo=_session.getUserInfo();
00116 
00117     if(typ==SSH2_AGENTC_REQUEST_IDENTITIES){ 
00118       mbuf.reset();
00119       mbuf.putByte((byte)SSH2_AGENT_IDENTITIES_ANSWER);
00120       synchronized(identities){
00121         int count=0;
00122         for(int i=0; i<identities.size(); i++){
00123           Identity identity=(Identity)(identities.elementAt(i));
00124           if(identity.getPublicKeyBlob()!=null)
00125             count++;
00126         }
00127         mbuf.putInt(count);
00128         for(int i=0; i<identities.size(); i++){
00129           Identity identity=(Identity)(identities.elementAt(i));
00130           byte[] pubkeyblob=identity.getPublicKeyBlob();
00131           if(pubkeyblob==null)
00132             continue;
00133           mbuf.putString(pubkeyblob);
00134           mbuf.putString(Util.empty);
00135         }
00136       }
00137       byte[] bar=new byte[mbuf.getLength()];
00138       mbuf.getByte(bar);
00139 
00140       send(bar);
00141     }
00142     else if(typ==SSH2_AGENTC_SIGN_REQUEST){
00143       byte[] blob=rbuf.getString();
00144       byte[] data=rbuf.getString();
00145       int flags=rbuf.getInt();
00146 
00147 //      if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
00148 //        datafellows = SSH_BUG_SIGBLOB;
00149 //      }
00150 
00151       Identity identity=null;
00152       synchronized(identities){
00153         for(int i=0; i<identities.size(); i++){
00154           Identity _identity=(Identity)(identities.elementAt(i));
00155           if(_identity.getPublicKeyBlob()==null)
00156             continue;
00157           if(!Util.array_equals(blob, _identity.getPublicKeyBlob())){
00158             continue;
00159           }
00160           if(_identity.isEncrypted()){
00161             if(userinfo==null)
00162               continue;
00163             while(_identity.isEncrypted()){
00164               if(!userinfo.promptPassphrase("Passphrase for "+_identity.getName())){
00165                 break;
00166               }
00167 
00168               String _passphrase=userinfo.getPassphrase();
00169               if(_passphrase==null){
00170                 break;
00171               }
00172 
00173               byte[] passphrase=Util.str2byte(_passphrase);
00174               try{
00175                 if(_identity.setPassphrase(passphrase)){
00176                   break;
00177                 }
00178               }
00179               catch(JSchException e){
00180                 break;
00181               }
00182             }
00183           }
00184 
00185           if(!_identity.isEncrypted()){
00186             identity=_identity;
00187             break;
00188           }
00189         }
00190       }
00191 
00192       byte[] signature=null;
00193 
00194       if(identity!=null){
00195         signature=identity.getSignature(data);
00196       }
00197 
00198       mbuf.reset();
00199       if(signature==null){
00200         mbuf.putByte((byte)SSH2_AGENT_FAILURE);
00201       }
00202       else{
00203         mbuf.putByte((byte)SSH2_AGENT_SIGN_RESPONSE);
00204         mbuf.putString(signature);
00205       }
00206 
00207       byte[] bar=new byte[mbuf.getLength()];
00208       mbuf.getByte(bar);
00209 
00210       send(bar);
00211     }
00212   }
00213 
00214   private void send(byte[] message){
00215     packet.reset();
00216     wbuf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00217     wbuf.putInt(recipient);
00218     wbuf.putInt(4+message.length);
00219     wbuf.putString(message);
00220 
00221     try{
00222       getSession().write(packet, this, 4+message.length);
00223     }
00224     catch(Exception e){
00225     }
00226   }
00227 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1