KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > ChannelAgentForwarding


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2006 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 import java.net.*;
33 import java.util.Vector JavaDoc;
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     //wbuf=new Buffer(rmpsize);
67
//packet=new Packet(wbuf);
68
mbuf=new Buffer();
69     connected=true;
70   }
71
72   public void run(){
73     try{
74       sendOpenConfirmation();
75     }
76     catch(Exception JavaDoc e){
77       close=true;
78       disconnect();
79     }
80   }
81
82   void write(byte[] foo, int s, int l) throws java.io.IOException JavaDoc {
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 JavaDoc 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 // if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
140
// datafellows = SSH_BUG_SIGBLOB;
141
// }
142

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 JavaDoc _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 JavaDoc e){
217     }
218   }
219 }
220
Popular Tags