1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
33 import java.util.Vector;
35 class ChannelAgentForwarding extends Channel{
37 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
38 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
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;
51 private Buffer rbuf=null;
52 private Buffer wbuf=null;
53 private Packet packet=null;
54 private Buffer mbuf=null;
56 ChannelAgentForwarding(){
59 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
60 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
61 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
63 type=Util.str2byte("auth-agent@openssh.com");
66 //wbuf=new Buffer(rmpsize);
67 //packet=new Packet(wbuf);
74 sendOpenConfirmation();
82 void write(byte[] foo, int s, int l) throws java.io.IOException {
85 wbuf=new Buffer(rmpsize);
86 packet=new Packet(wbuf);
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);
96 rbuf.putByte(foo, s, l);
98 int mlen=rbuf.getInt();
99 if(mlen>rbuf.getLength()){
104 int typ=rbuf.getByte();
106 Session _session=null;
108 _session=getSession();
110 catch(JSchException e){
111 throw new java.io.IOException(e.toString());
114 Vector identities=_session.jsch.identities;
115 UserInfo userinfo=_session.getUserInfo();
117 if(typ==SSH2_AGENTC_REQUEST_IDENTITIES){
119 mbuf.putByte((byte)SSH2_AGENT_IDENTITIES_ANSWER);
120 synchronized(identities){
122 for(int i=0; i<identities.size(); i++){
123 Identity identity=(Identity)(identities.elementAt(i));
124 if(identity.getPublicKeyBlob()!=null)
128 for(int i=0; i<identities.size(); i++){
129 Identity identity=(Identity)(identities.elementAt(i));
130 byte[] pubkeyblob=identity.getPublicKeyBlob();
133 mbuf.putString(pubkeyblob);
134 mbuf.putString(Util.empty);
137 byte[] bar=new byte[mbuf.getLength()];
142 else if(typ==SSH2_AGENTC_SIGN_REQUEST){
143 byte[] blob=rbuf.getString();
144 byte[] data=rbuf.getString();
145 int flags=rbuf.getInt();
147 // if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
148 // datafellows = SSH_BUG_SIGBLOB;
151 Identity identity=null;
152 synchronized(identities){
153 for(int i=0; i<identities.size(); i++){
154 Identity _identity=(Identity)(identities.elementAt(i));
155 if(_identity.getPublicKeyBlob()==null)
157 if(!Util.array_equals(blob, _identity.getPublicKeyBlob())){
160 if(_identity.isEncrypted()){
163 while(_identity.isEncrypted()){
164 if(!userinfo.promptPassphrase("Passphrase for "+_identity.getName())){
168 String _passphrase=userinfo.getPassphrase();
169 if(_passphrase==null){
173 byte[] passphrase=Util.str2byte(_passphrase);
175 if(_identity.setPassphrase(passphrase)){
179 catch(JSchException e){
185 if(!_identity.isEncrypted()){
192 byte[] signature=null;
195 signature=identity.getSignature(data);
200 mbuf.putByte((byte)SSH2_AGENT_FAILURE);
203 mbuf.putByte((byte)SSH2_AGENT_SIGN_RESPONSE);
204 mbuf.putString(signature);
207 byte[] bar=new byte[mbuf.getLength()];
214 private void send(byte[] message){
216 wbuf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
217 wbuf.putInt(recipient);
218 wbuf.putInt(4+message.length);
219 wbuf.putString(message);
222 getSession().write(packet, this, 4+message.length);