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

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/JSch.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 System.Collections;
 
34
using System.Collections.Generic;
 
35
using NSch;
 
36
using Sharpen;
 
37
 
 
38
namespace NSch
 
39
{
 
40
        public class JSch
 
41
        {
 
42
                internal static Hashtable config = new Hashtable();
 
43
 
 
44
                static JSch()
 
45
                {
 
46
                        //  config.put("kex", "diffie-hellman-group-exchange-sha1");
 
47
                        config.Put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1"
 
48
                                );
 
49
                        config.Put("server_host_key", "ssh-rsa,ssh-dss");
 
50
                        //    config.put("server_host_key", "ssh-dss,ssh-rsa");
 
51
                        config.Put("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc"
 
52
                                );
 
53
                        config.Put("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc"
 
54
                                );
 
55
                        config.Put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
 
56
                        config.Put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
 
57
                        config.Put("compression.s2c", "none");
 
58
                        // config.put("compression.s2c", "zlib@openssh.com,zlib,none");
 
59
                        config.Put("compression.c2s", "none");
 
60
                        // config.put("compression.c2s", "zlib@openssh.com,zlib,none");
 
61
                        config.Put("lang.s2c", string.Empty);
 
62
                        config.Put("lang.c2s", string.Empty);
 
63
                        config.Put("compression_level", "6");
 
64
                        config.Put("diffie-hellman-group-exchange-sha1", "com.jcraft.jsch.DHGEX");
 
65
                        config.Put("diffie-hellman-group1-sha1", "com.jcraft.jsch.DHG1");
 
66
                        config.Put("dh", "com.jcraft.jsch.jce.DH");
 
67
                        config.Put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
 
68
                        config.Put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
 
69
                        config.Put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
 
70
                        config.Put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
 
71
                        config.Put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
 
72
                        config.Put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
 
73
                        config.Put("sha-1", "com.jcraft.jsch.jce.SHA1");
 
74
                        config.Put("md5", "com.jcraft.jsch.jce.MD5");
 
75
                        config.Put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
 
76
                        config.Put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
 
77
                        config.Put("keypairgen.dsa", "com.jcraft.jsch.jce.KeyPairGenDSA");
 
78
                        config.Put("keypairgen.rsa", "com.jcraft.jsch.jce.KeyPairGenRSA");
 
79
                        config.Put("random", "com.jcraft.jsch.jce.Random");
 
80
                        config.Put("none", "com.jcraft.jsch.CipherNone");
 
81
                        config.Put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
 
82
                        config.Put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
 
83
                        config.Put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
 
84
                        config.Put("aes128-ctr", "com.jcraft.jsch.jce.AES128CTR");
 
85
                        config.Put("aes192-ctr", "com.jcraft.jsch.jce.AES192CTR");
 
86
                        config.Put("aes256-ctr", "com.jcraft.jsch.jce.AES256CTR");
 
87
                        config.Put("3des-ctr", "com.jcraft.jsch.jce.TripleDESCTR");
 
88
                        config.Put("arcfour", "com.jcraft.jsch.jce.ARCFOUR");
 
89
                        config.Put("arcfour128", "com.jcraft.jsch.jce.ARCFOUR128");
 
90
                        config.Put("arcfour256", "com.jcraft.jsch.jce.ARCFOUR256");
 
91
                        config.Put("userauth.none", "com.jcraft.jsch.UserAuthNone");
 
92
                        config.Put("userauth.password", "com.jcraft.jsch.UserAuthPassword");
 
93
                        config.Put("userauth.keyboard-interactive", "com.jcraft.jsch.UserAuthKeyboardInteractive"
 
94
                                );
 
95
                        config.Put("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey");
 
96
                        config.Put("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
 
97
                        config.Put("gssapi-with-mic.krb5", "com.jcraft.jsch.jgss.GSSContextKrb5");
 
98
                        config.Put("zlib", "com.jcraft.jsch.jcraft.Compression");
 
99
                        config.Put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
 
100
                        config.Put("StrictHostKeyChecking", "ask");
 
101
                        config.Put("HashKnownHosts", "no");
 
102
                        //config.put("HashKnownHosts",  "yes");
 
103
                        config.Put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password"
 
104
                                );
 
105
                        config.Put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256"
 
106
                                );
 
107
                }
 
108
 
 
109
                internal ArrayList pool = new ArrayList();
 
110
 
 
111
                internal ArrayList identities = new ArrayList();
 
112
 
 
113
                private HostKeyRepository known_hosts = null;
 
114
 
 
115
                private sealed class _Logger_115 : Logger
 
116
                {
 
117
                        public _Logger_115()
 
118
                        {
 
119
                        }
 
120
 
 
121
                        public override bool IsEnabled(int level)
 
122
                        {
 
123
                                return false;
 
124
                        }
 
125
 
 
126
                        public override void Log(int level, string message)
 
127
                        {
 
128
                        }
 
129
                }
 
130
 
 
131
                private static readonly Logger DEVNULL = new _Logger_115();
 
132
 
 
133
                internal static Logger logger = DEVNULL;
 
134
 
 
135
                public JSch()
 
136
                {
 
137
                        try
 
138
                        {
 
139
                                string osname = (string)(Runtime.GetProperties()["os.name"]);
 
140
                                if (osname != null && osname.Equals("Mac OS X"))
 
141
                                {
 
142
                                        config.Put("hmac-sha1", "com.jcraft.jsch.jcraft.HMACSHA1");
 
143
                                        config.Put("hmac-md5", "com.jcraft.jsch.jcraft.HMACMD5");
 
144
                                        config.Put("hmac-md5-96", "com.jcraft.jsch.jcraft.HMACMD596");
 
145
                                        config.Put("hmac-sha1-96", "com.jcraft.jsch.jcraft.HMACSHA196");
 
146
                                }
 
147
                        }
 
148
                        catch (Exception)
 
149
                        {
 
150
                        }
 
151
                }
 
152
 
 
153
                /// <exception cref="NSch.JSchException"></exception>
 
154
                public virtual Session GetSession(string username, string host)
 
155
                {
 
156
                        return GetSession(username, host, 22);
 
157
                }
 
158
 
 
159
                /// <exception cref="NSch.JSchException"></exception>
 
160
                public virtual Session GetSession(string username, string host, int port)
 
161
                {
 
162
                        if (username == null)
 
163
                        {
 
164
                                throw new JSchException("username must not be null.");
 
165
                        }
 
166
                        if (host == null)
 
167
                        {
 
168
                                throw new JSchException("host must not be null.");
 
169
                        }
 
170
                        Session s = new Session(this);
 
171
                        s.SetUserName(username);
 
172
                        s.SetHost(host);
 
173
                        s.SetPort(port);
 
174
                        //pool.addElement(s);
 
175
                        return s;
 
176
                }
 
177
 
 
178
                protected internal virtual void AddSession(Session session)
 
179
                {
 
180
                        lock (pool)
 
181
                        {
 
182
                                pool.Add(session);
 
183
                        }
 
184
                }
 
185
 
 
186
                protected internal virtual bool RemoveSession(Session session)
 
187
                {
 
188
                        lock (pool)
 
189
                        {
 
190
                                return pool.RemoveElement(session);
 
191
                        }
 
192
                }
 
193
 
 
194
                public virtual void SetHostKeyRepository(HostKeyRepository hkrepo)
 
195
                {
 
196
                        known_hosts = hkrepo;
 
197
                }
 
198
 
 
199
                /// <exception cref="NSch.JSchException"></exception>
 
200
                public virtual void SetKnownHosts(string filename)
 
201
                {
 
202
                        if (known_hosts == null)
 
203
                        {
 
204
                                known_hosts = new KnownHosts(this);
 
205
                        }
 
206
                        if (known_hosts is KnownHosts)
 
207
                        {
 
208
                                lock (known_hosts)
 
209
                                {
 
210
                                        ((KnownHosts)known_hosts).SetKnownHosts(filename);
 
211
                                }
 
212
                        }
 
213
                }
 
214
 
 
215
                /// <exception cref="NSch.JSchException"></exception>
 
216
                public virtual void SetKnownHosts(InputStream stream)
 
217
                {
 
218
                        if (known_hosts == null)
 
219
                        {
 
220
                                known_hosts = new KnownHosts(this);
 
221
                        }
 
222
                        if (known_hosts is KnownHosts)
 
223
                        {
 
224
                                lock (known_hosts)
 
225
                                {
 
226
                                        ((KnownHosts)known_hosts).SetKnownHosts(stream);
 
227
                                }
 
228
                        }
 
229
                }
 
230
 
 
231
                public virtual HostKeyRepository GetHostKeyRepository()
 
232
                {
 
233
                        if (known_hosts == null)
 
234
                        {
 
235
                                known_hosts = new KnownHosts(this);
 
236
                        }
 
237
                        return known_hosts;
 
238
                }
 
239
 
 
240
                /// <exception cref="NSch.JSchException"></exception>
 
241
                public virtual void AddIdentity(string prvkey)
 
242
                {
 
243
                        AddIdentity(prvkey, (byte[])null);
 
244
                }
 
245
 
 
246
                /// <exception cref="NSch.JSchException"></exception>
 
247
                public virtual void AddIdentity(string prvkey, string passphrase)
 
248
                {
 
249
                        byte[] _passphrase = null;
 
250
                        if (passphrase != null)
 
251
                        {
 
252
                                _passphrase = Util.Str2byte(passphrase);
 
253
                        }
 
254
                        AddIdentity(prvkey, _passphrase);
 
255
                        if (_passphrase != null)
 
256
                        {
 
257
                                Util.Bzero(_passphrase);
 
258
                        }
 
259
                }
 
260
 
 
261
                /// <exception cref="NSch.JSchException"></exception>
 
262
                public virtual void AddIdentity(string prvkey, byte[] passphrase)
 
263
                {
 
264
                        Identity identity = IdentityFile.NewInstance(prvkey, null, this);
 
265
                        AddIdentity(identity, passphrase);
 
266
                }
 
267
 
 
268
                /// <exception cref="NSch.JSchException"></exception>
 
269
                public virtual void AddIdentity(string prvkey, string pubkey, byte[] passphrase)
 
270
                {
 
271
                        Identity identity = IdentityFile.NewInstance(prvkey, pubkey, this);
 
272
                        AddIdentity(identity, passphrase);
 
273
                }
 
274
 
 
275
                /// <exception cref="NSch.JSchException"></exception>
 
276
                public virtual void AddIdentity(string name, byte[] prvkey, byte[] pubkey, byte[]
 
277
                         passphrase)
 
278
                {
 
279
                        Identity identity = IdentityFile.NewInstance(name, prvkey, pubkey, this);
 
280
                        AddIdentity(identity, passphrase);
 
281
                }
 
282
 
 
283
                /// <exception cref="NSch.JSchException"></exception>
 
284
                public virtual void AddIdentity(Identity identity, byte[] passphrase)
 
285
                {
 
286
                        if (passphrase != null)
 
287
                        {
 
288
                                try
 
289
                                {
 
290
                                        byte[] goo = new byte[passphrase.Length];
 
291
                                        System.Array.Copy(passphrase, 0, goo, 0, passphrase.Length);
 
292
                                        passphrase = goo;
 
293
                                        identity.SetPassphrase(passphrase);
 
294
                                }
 
295
                                finally
 
296
                                {
 
297
                                        Util.Bzero(passphrase);
 
298
                                }
 
299
                        }
 
300
                        lock (identities)
 
301
                        {
 
302
                                if (!identities.Contains(identity))
 
303
                                {
 
304
                                        identities.Add(identity);
 
305
                                }
 
306
                        }
 
307
                }
 
308
 
 
309
                /// <exception cref="NSch.JSchException"></exception>
 
310
                public virtual void RemoveIdentity(string name)
 
311
                {
 
312
                        lock (identities)
 
313
                        {
 
314
                                for (int i = 0; i < identities.Count; i++)
 
315
                                {
 
316
                                        Identity identity = (Identity)(identities[i]);
 
317
                                        if (!identity.GetName().Equals(name))
 
318
                                        {
 
319
                                                continue;
 
320
                                        }
 
321
                                        identities.RemoveElement(identity);
 
322
                                        identity.Clear();
 
323
                                        break;
 
324
                                }
 
325
                        }
 
326
                }
 
327
 
 
328
                /// <exception cref="NSch.JSchException"></exception>
 
329
                public virtual ArrayList GetIdentityNames()
 
330
                {
 
331
                        ArrayList foo = new ArrayList();
 
332
                        lock (identities)
 
333
                        {
 
334
                                for (int i = 0; i < identities.Count; i++)
 
335
                                {
 
336
                                        Identity identity = (Identity)(identities[i]);
 
337
                                        foo.Add(identity.GetName());
 
338
                                }
 
339
                        }
 
340
                        return foo;
 
341
                }
 
342
 
 
343
                /// <exception cref="NSch.JSchException"></exception>
 
344
                public virtual void RemoveAllIdentity()
 
345
                {
 
346
                        lock (identities)
 
347
                        {
 
348
                                ArrayList foo = GetIdentityNames();
 
349
                                for (int i = 0; i < foo.Count; i++)
 
350
                                {
 
351
                                        string name = ((string)foo[i]);
 
352
                                        RemoveIdentity(name);
 
353
                                }
 
354
                        }
 
355
                }
 
356
 
 
357
                public static string GetConfig(string key)
 
358
                {
 
359
                        lock (config)
 
360
                        {
 
361
                                string s = (string)(config[key]);
 
362
                                s = s.Replace ("com.jcraft.jsch.jce","NSch.Jce");
 
363
                                s = s.Replace ("com.jcraft.jsch.jcraft","NSch.Jcraft");
 
364
                                s = s.Replace ("com.jcraft.jsch.jgss","NSch.Jgss");
 
365
                                s = s.Replace ("com.jcraft.jsch","NSch");
 
366
                                return s;
 
367
                        }
 
368
                }
 
369
 
 
370
                public static void SetConfig(Hashtable newconf)
 
371
                {
 
372
                        lock (config)
 
373
                        {
 
374
                                for (IEnumerator e = newconf.Keys.GetEnumerator (); e.MoveNext(); )
 
375
                                {
 
376
                                        string key = (string)(e.Current);
 
377
                                        config.Put(key, (string)(newconf[key]));
 
378
                                }
 
379
                        }
 
380
                }
 
381
 
 
382
                public static void SetConfig(string key, string value)
 
383
                {
 
384
                        config.Put(key, value);
 
385
                }
 
386
 
 
387
                public static void SetLogger(Logger logger)
 
388
                {
 
389
                        if (logger == null)
 
390
                        {
 
391
                                NSch.JSch.logger = DEVNULL;
 
392
                        }
 
393
                        NSch.JSch.logger = logger;
 
394
                }
 
395
 
 
396
                internal static Logger GetLogger()
 
397
                {
 
398
                        return logger;
 
399
                }
 
400
        }
 
401
}