2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in
12
the documentation and/or other materials provided with the distribution.
14
3. The names of the authors may not be used to endorse or promote products
15
derived from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
38
public abstract class KeyExchange
40
internal const int PROPOSAL_KEX_ALGS = 0;
42
internal const int PROPOSAL_SERVER_HOST_KEY_ALGS = 1;
44
internal const int PROPOSAL_ENC_ALGS_CTOS = 2;
46
internal const int PROPOSAL_ENC_ALGS_STOC = 3;
48
internal const int PROPOSAL_MAC_ALGS_CTOS = 4;
50
internal const int PROPOSAL_MAC_ALGS_STOC = 5;
52
internal const int PROPOSAL_COMP_ALGS_CTOS = 6;
54
internal const int PROPOSAL_COMP_ALGS_STOC = 7;
56
internal const int PROPOSAL_LANG_CTOS = 8;
58
internal const int PROPOSAL_LANG_STOC = 9;
60
internal const int PROPOSAL_MAX = 10;
62
internal static string kex = "diffie-hellman-group1-sha1";
64
internal static string server_host_key = "ssh-rsa,ssh-dss";
66
internal static string enc_c2s = "blowfish-cbc";
68
internal static string enc_s2c = "blowfish-cbc";
70
internal static string mac_c2s = "hmac-md5";
72
internal static string mac_s2c = "hmac-md5";
74
internal static string lang_c2s = string.Empty;
76
internal static string lang_s2c = string.Empty;
78
public const int STATE_END = 0;
80
protected internal Session session = null;
82
protected internal HASH sha = null;
84
protected internal byte[] K = null;
86
protected internal byte[] H = null;
88
protected internal byte[] K_S = null;
90
//static String kex_algs="diffie-hellman-group-exchange-sha1"+
91
// ",diffie-hellman-group1-sha1";
92
//static String kex="diffie-hellman-group-exchange-sha1";
93
// hmac-md5,hmac-sha1,hmac-ripemd160,
94
// hmac-sha1-96,hmac-md5-96
95
//static String comp_c2s="none"; // zlib
96
//static String comp_s2c="none";
97
/// <exception cref="System.Exception"></exception>
98
public abstract void Init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte
101
/// <exception cref="System.Exception"></exception>
102
public abstract bool Next(Buffer buf);
104
public abstract string GetKeyType();
106
public abstract int GetState();
108
protected internal static string[] Guess(byte[] I_S, byte[] I_C)
110
string[] guess = new string[PROPOSAL_MAX];
111
Buffer sb = new Buffer(I_S);
113
Buffer cb = new Buffer(I_C);
115
for (int i = 0; i < PROPOSAL_MAX; i++)
117
byte[] sp = sb.GetString();
119
byte[] cp = cb.GetString();
123
while (j < cp.Length)
125
while (j < cp.Length && cp[j] != ',')
133
string algorithm = Util.Byte2str(cp, k, j - k);
136
while (l < sp.Length)
138
while (l < sp.Length && sp[l] != ',')
146
if (algorithm.Equals(Util.Byte2str(sp, m, l - m)))
148
guess[i] = algorithm;
161
guess[i] = string.Empty;
165
if (guess[i] == null)
171
if (JSch.GetLogger().IsEnabled(Logger.INFO))
173
JSch.GetLogger().Log(Logger.INFO, "kex: server->client" + " " + guess[PROPOSAL_ENC_ALGS_STOC
174
] + " " + guess[PROPOSAL_MAC_ALGS_STOC] + " " + guess[PROPOSAL_COMP_ALGS_STOC]);
175
JSch.GetLogger().Log(Logger.INFO, "kex: client->server" + " " + guess[PROPOSAL_ENC_ALGS_CTOS
176
] + " " + guess[PROPOSAL_MAC_ALGS_CTOS] + " " + guess[PROPOSAL_COMP_ALGS_CTOS]);
178
// for(int i=0; i<PROPOSAL_MAX; i++){
179
// System.err.println("guess: ["+guess[i]+"]");
184
public virtual string GetFingerPrint()
189
Type c = Sharpen.Runtime.GetType(session.GetConfig("md5"));
190
hash = (HASH)(System.Activator.CreateInstance(c));
194
System.Console.Error.WriteLine("getFingerPrint: " + e);
196
return Util.GetFingerPrint(hash, GetHostKey());
199
internal virtual byte[] GetK()
204
internal virtual byte[] GetH()
209
internal virtual HASH GetHash()
214
internal virtual byte[] GetHostKey()