📄 session.cs
字号:
internal byte[] getSessionId()
{
return session_id;
}
private void receive_newkeys(Buffer buf, KeyExchange kex)
{
// send_newkeys();
updateKeys(kex);
in_kex=false;
}
private void updateKeys(KeyExchange kex)
{
byte[] K=kex.getK();
byte[] H=kex.getH();
HASH hash=kex.getHash();
String[] guess=kex._guess;
if(session_id==null)
{
session_id=new byte[H.Length];
Fireball.Ssh.java.System.arraycopy(H, 0, session_id, 0, H.Length);
}
/*
Initial IV client to server: HASH (K || H || "A" || session_id)
Initial IV server to client: HASH (K || H || "B" || session_id)
Encryption key client to server: HASH (K || H || "C" || session_id)
Encryption key server to client: HASH (K || H || "D" || session_id)
Integrity key client to server: HASH (K || H || "E" || session_id)
Integrity key server to client: HASH (K || H || "F" || session_id)
*/
buf.reset();
buf.putMPInt(K);
buf.putByte(H);
buf.putByte((byte)0x41);
buf.putByte(session_id);
hash.update(buf.buffer, 0, buf.index);
IVc2s=hash.digest();
int j=buf.index-session_id.Length-1;
buf.buffer[j]++;
hash.update(buf.buffer, 0, buf.index);
IVs2c=hash.digest();
buf.buffer[j]++;
hash.update(buf.buffer, 0, buf.index);
Ec2s=hash.digest();
buf.buffer[j]++;
hash.update(buf.buffer, 0, buf.index);
Es2c=hash.digest();
buf.buffer[j]++;
hash.update(buf.buffer, 0, buf.index);
MACc2s=hash.digest();
buf.buffer[j]++;
hash.update(buf.buffer, 0, buf.index);
MACs2c=hash.digest();
try
{
Class c;
c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC]));
s2ccipher=(Cipher)(c.newInstance());
while(s2ccipher.getBlockSize()>Es2c.Length)
{
buf.reset();
buf.putMPInt(K);
buf.putByte(H);
buf.putByte(Es2c);
hash.update(buf.buffer, 0, buf.index);
byte[] foo=hash.digest();
byte[] bar=new byte[Es2c.Length+foo.Length];
Fireball.Ssh.java.System.arraycopy(Es2c, 0, bar, 0, Es2c.Length);
Fireball.Ssh.java.System.arraycopy(foo, 0, bar, Es2c.Length, foo.Length);
Es2c=bar;
}
s2ccipher.init(Cipher.DECRYPT_MODE, Es2c, IVs2c);
cipher_size=s2ccipher.getIVSize();
c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC]));
s2cmac=(MAC)(c.newInstance());
s2cmac.init(MACs2c);
mac_buf=new byte[s2cmac.getBlockSize()];
c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS]));
c2scipher=(Cipher)(c.newInstance());
while(c2scipher.getBlockSize()>Ec2s.Length)
{
buf.reset();
buf.putMPInt(K);
buf.putByte(H);
buf.putByte(Ec2s);
hash.update(buf.buffer, 0, buf.index);
byte[] foo=hash.digest();
byte[] bar=new byte[Ec2s.Length+foo.Length];
Fireball.Ssh.java.System.arraycopy(Ec2s, 0, bar, 0, Ec2s.Length);
Fireball.Ssh.java.System.arraycopy(foo, 0, bar, Ec2s.Length, foo.Length);
Ec2s=bar;
}
c2scipher.init(Cipher.ENCRYPT_MODE, Ec2s, IVc2s);
c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS]));
c2smac=(MAC)(c.newInstance());
c2smac.init(MACc2s);
if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS].equals("none"))
{
String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]);
if(foo!=null)
{
try
{
c=Class.forName(foo);
deflater=(Compression)(c.newInstance());
int level=6;
try{ level=Integer.parseInt(getConfig("compression_level"));}
catch(Exception ee){ }
deflater.init(Compression.DEFLATER, level);
}
catch(Exception ee)
{
System.Console.Error.WriteLine(foo+" isn't accessible.");
}
}
}
else
{
if(deflater!=null)
{
deflater=null;
}
}
if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC].equals("none"))
{
String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]);
if(foo!=null)
{
try
{
c=Class.forName(foo);
inflater=(Compression)(c.newInstance());
inflater.init(Compression.INFLATER, 0);
}
catch(Exception ee)
{
System.Console.Error.WriteLine(foo+" isn't accessible.");
}
}
}
else
{
if(inflater!=null)
{
inflater=null;
}
}
}
catch(Exception e){ System.Console.Error.WriteLine("updatekeys: "+e); }
}
/*public*/ /*lock*/public void write(Packet packet, Channel c, int length)
{
while(true)
{
if(in_kex)
{
try{Thread.Sleep(10);}
catch(ThreadInterruptedException e){};
continue;
}
lock(c)
{
if(c.rwsize>=length)
{
c.rwsize-=length;
break;
}
}
if(c._close || !c.isConnected())
{
throw new IOException("channel is broken");
}
bool sendit=false;
int s=0;
byte command=0;
int recipient=-1;
lock(c)
{
if(c.rwsize>0)
{
int len=c.rwsize;
s=packet.shift(len, (c2smac!=null ? c2smac.getBlockSize() : 0));
command=packet.buffer.buffer[5];
recipient=c.getRecipient();
length-=len;
c.rwsize-=len;
sendit=true;
}
}
if(sendit)
{
_write(packet);
packet.unshift(command, recipient, s, length);
}
try{Thread.Sleep(10);}
catch(ThreadInterruptedException e){};
}
_write(packet);
}
/*
public lockpublic void write(Packet packet) {
encode(packet);
if(io!=null){
io.put(packet);
seqo++;
}
}
*/
public void write(Packet packet)
{
// System.Console.WriteLine("in_kex="+in_kex+" "+(packet.buffer.buffer[5]));
while(in_kex)
{
byte command=packet.buffer.buffer[5];
//System.Console.WriteLine("command: "+command);
if(command==SSH_MSG_KEXINIT ||
command==SSH_MSG_NEWKEYS ||
command==SSH_MSG_KEXDH_INIT ||
command==SSH_MSG_KEXDH_REPLY)
{
break;
}
try{Thread.Sleep(10);}
catch(ThreadInterruptedException e){};
}
_write(packet);
}
[System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.Synchronized)]
private void _write(Packet packet)
{
encode(packet);
if(io!=null)
{
io.put(packet);
seqo++;
}
}
Runnable thread;
public void run()
{
thread=this;
byte[] foo;
Buffer buf=new Buffer();
Packet packet=new Packet(buf);
int i=0;
Channel channel;
int[] start=new int[1];
int[] length=new int[1];
KeyExchange kex=null;
try
{
while(_isConnected &&
thread!=null)
{
buf=read(buf);
int msgType=buf.buffer[5]&0xff;
// if(msgType!=94)
//System.Console.WriteLine("read: 94 ? "+msgType);
if(kex!=null && kex.getState()==msgType)
{
bool result=kex.next(buf);
if(!result)
{
throw new JSchException("verify: "+result);
}
continue;
}
switch(msgType)
{
case SSH_MSG_KEXINIT:
//System.Console.WriteLine("KEXINIT");
kex=receive_kexinit(buf);
break;
case SSH_MSG_NEWKEYS:
//System.Console.WriteLine("NEWKEYS");
send_newkeys();
receive_newkeys(buf, kex);
kex=null;
break;
case SSH_MSG_CHANNEL_DATA:
buf.getInt();
buf.getByte();
buf.getByte();
i=buf.getInt();
channel=Channel.getChannel(i, this);
foo=buf.getString(start, length);
if(channel==null)
{
break;
}
try
{
channel.write(foo, start[0], length[0]);
}
catch(Exception e)
{
//System.Console.WriteLine(e);
try{channel.disconnect();}
catch(Exception ee){}
break;
}
int len=length[0];
channel.setLocalWindowSize(channel.lwsize-len);
if(channel.lwsize<channel.lwsize_max/2)
{
packet.reset();
buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
buf.putInt(channel.getRecipient());
buf.putInt(channel.lwsize_max-channel.lwsize);
write(packet);
channel.setLocalWindowSize(channel.lwsize_max);
}
break;
case SSH_MSG_CHANNEL_EXTENDED_DATA:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
buf.getInt(); // data_type_code == 1
foo=buf.getString(start, length);
//System.Console.WriteLine("stderr: "+new String(foo,start[0],length[0]));
if(channel==null)
{
break;
}
//channel.write(foo, start[0], length[0]);
channel.write_ext(foo, start[0], length[0]);
len=length[0];
channel.setLocalWindowSize(channel.lwsize-len);
if(channel.lwsize<channel.lwsize_max/2)
{
packet.reset();
buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
buf.putInt(channel.getRecipient());
buf.putInt(channel.lwsize_max-channel.lwsize);
write(packet);
channel.setLocalWindowSize(channel.lwsize_max);
}
break;
case SSH_MSG_CHANNEL_WINDOW_ADJUST:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
if(channel==null)
{
break;
}
channel.addRemoteWindowSize(buf.getInt());
break;
case SSH_MSG_CHANNEL_EOF:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
if(channel!=null)
{
//channel._eof_remote=true;
//channel.eof();
channel.eof_remote();
}
/*
packet.reset();
buf.putByte((byte)SSH_MSG_CHANNEL_EOF);
buf.putInt(channel.getRecipient());
write(packet);
*/
break;
case SSH_MSG_CHANNEL_CLOSE:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
if(channel!=null)
{
// channel.close();
channel.disconnect();
}
/*
if(Channel.pool.size()==0){
thread=null;
}
*/
break;
case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
if(channel==null)
{
//break;
}
channel.setRecipient(buf.getInt());
channel.setRemoteWindowSize(buf.getInt());
channel.setRemotePacketSize(buf.getInt());
break;
case SSH_MSG_CHANNEL_OPEN_FAILURE:
buf.getInt();
buf.getShort();
i=buf.getInt();
channel=Channel.getChannel(i, this);
if(channel==null)
{
//break;
}
int reason_code=buf.getInt();
//foo=buf.getString(); // additional textual information
//foo=buf.getString(); // language tag
channel.exitstatus=reason_code;
channel._close=true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -