~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/KeyExchange.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
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.
 
13
 
 
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.
 
16
 
 
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.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using NSch;
 
34
using Sharpen;
 
35
 
 
36
namespace NSch
 
37
{
 
38
        public abstract class KeyExchange
 
39
        {
 
40
                internal const int PROPOSAL_KEX_ALGS = 0;
 
41
 
 
42
                internal const int PROPOSAL_SERVER_HOST_KEY_ALGS = 1;
 
43
 
 
44
                internal const int PROPOSAL_ENC_ALGS_CTOS = 2;
 
45
 
 
46
                internal const int PROPOSAL_ENC_ALGS_STOC = 3;
 
47
 
 
48
                internal const int PROPOSAL_MAC_ALGS_CTOS = 4;
 
49
 
 
50
                internal const int PROPOSAL_MAC_ALGS_STOC = 5;
 
51
 
 
52
                internal const int PROPOSAL_COMP_ALGS_CTOS = 6;
 
53
 
 
54
                internal const int PROPOSAL_COMP_ALGS_STOC = 7;
 
55
 
 
56
                internal const int PROPOSAL_LANG_CTOS = 8;
 
57
 
 
58
                internal const int PROPOSAL_LANG_STOC = 9;
 
59
 
 
60
                internal const int PROPOSAL_MAX = 10;
 
61
 
 
62
                internal static string kex = "diffie-hellman-group1-sha1";
 
63
 
 
64
                internal static string server_host_key = "ssh-rsa,ssh-dss";
 
65
 
 
66
                internal static string enc_c2s = "blowfish-cbc";
 
67
 
 
68
                internal static string enc_s2c = "blowfish-cbc";
 
69
 
 
70
                internal static string mac_c2s = "hmac-md5";
 
71
 
 
72
                internal static string mac_s2c = "hmac-md5";
 
73
 
 
74
                internal static string lang_c2s = string.Empty;
 
75
 
 
76
                internal static string lang_s2c = string.Empty;
 
77
 
 
78
                public const int STATE_END = 0;
 
79
 
 
80
                protected internal Session session = null;
 
81
 
 
82
                protected internal HASH sha = null;
 
83
 
 
84
                protected internal byte[] K = null;
 
85
 
 
86
                protected internal byte[] H = null;
 
87
 
 
88
                protected internal byte[] K_S = null;
 
89
 
 
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
 
99
                        [] I_C);
 
100
 
 
101
                /// <exception cref="System.Exception"></exception>
 
102
                public abstract bool Next(Buffer buf);
 
103
 
 
104
                public abstract string GetKeyType();
 
105
 
 
106
                public abstract int GetState();
 
107
 
 
108
                protected internal static string[] Guess(byte[] I_S, byte[] I_C)
 
109
                {
 
110
                        string[] guess = new string[PROPOSAL_MAX];
 
111
                        Buffer sb = new Buffer(I_S);
 
112
                        sb.SetOffSet(17);
 
113
                        Buffer cb = new Buffer(I_C);
 
114
                        cb.SetOffSet(17);
 
115
                        for (int i = 0; i < PROPOSAL_MAX; i++)
 
116
                        {
 
117
                                byte[] sp = sb.GetString();
 
118
                                // server proposal
 
119
                                byte[] cp = cb.GetString();
 
120
                                // client proposal
 
121
                                int j = 0;
 
122
                                int k = 0;
 
123
                                while (j < cp.Length)
 
124
                                {
 
125
                                        while (j < cp.Length && cp[j] != ',')
 
126
                                        {
 
127
                                                j++;
 
128
                                        }
 
129
                                        if (k == j)
 
130
                                        {
 
131
                                                return null;
 
132
                                        }
 
133
                                        string algorithm = Util.Byte2str(cp, k, j - k);
 
134
                                        int l = 0;
 
135
                                        int m = 0;
 
136
                                        while (l < sp.Length)
 
137
                                        {
 
138
                                                while (l < sp.Length && sp[l] != ',')
 
139
                                                {
 
140
                                                        l++;
 
141
                                                }
 
142
                                                if (m == l)
 
143
                                                {
 
144
                                                        return null;
 
145
                                                }
 
146
                                                if (algorithm.Equals(Util.Byte2str(sp, m, l - m)))
 
147
                                                {
 
148
                                                        guess[i] = algorithm;
 
149
                                                        goto loop_break;
 
150
                                                }
 
151
                                                l++;
 
152
                                                m = l;
 
153
                                        }
 
154
                                        j++;
 
155
                                        k = j;
 
156
loop_continue: ;
 
157
                                }
 
158
loop_break: ;
 
159
                                if (j == 0)
 
160
                                {
 
161
                                        guess[i] = string.Empty;
 
162
                                }
 
163
                                else
 
164
                                {
 
165
                                        if (guess[i] == null)
 
166
                                        {
 
167
                                                return null;
 
168
                                        }
 
169
                                }
 
170
                        }
 
171
                        if (JSch.GetLogger().IsEnabled(Logger.INFO))
 
172
                        {
 
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]);
 
177
                        }
 
178
                        //    for(int i=0; i<PROPOSAL_MAX; i++){
 
179
                        //      System.err.println("guess: ["+guess[i]+"]");
 
180
                        //    }
 
181
                        return guess;
 
182
                }
 
183
 
 
184
                public virtual string GetFingerPrint()
 
185
                {
 
186
                        HASH hash = null;
 
187
                        try
 
188
                        {
 
189
                                Type c = Sharpen.Runtime.GetType(session.GetConfig("md5"));
 
190
                                hash = (HASH)(System.Activator.CreateInstance(c));
 
191
                        }
 
192
                        catch (Exception e)
 
193
                        {
 
194
                                System.Console.Error.WriteLine("getFingerPrint: " + e);
 
195
                        }
 
196
                        return Util.GetFingerPrint(hash, GetHostKey());
 
197
                }
 
198
 
 
199
                internal virtual byte[] GetK()
 
200
                {
 
201
                        return K;
 
202
                }
 
203
 
 
204
                internal virtual byte[] GetH()
 
205
                {
 
206
                        return H;
 
207
                }
 
208
 
 
209
                internal virtual HASH GetHash()
 
210
                {
 
211
                        return sha;
 
212
                }
 
213
 
 
214
                internal virtual byte[] GetHostKey()
 
215
                {
 
216
                        return K_S;
 
217
                }
 
218
        }
 
219
}