/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair Load(JSch jsch, string prvkey) { string pubkey = prvkey + ".pub"; if (!new FilePath(pubkey).Exists()) { pubkey = null; } return(Load(jsch, prvkey, pubkey)); }
/// <exception cref="NSch.JSchException"></exception> internal static NSch.IdentityFile NewInstance(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { try { return(new NSch.IdentityFile(name, prvkey, pubkey, jsch)); } finally { Util.Bzero(prvkey); } }
private NSch.Cipher GenCipher() { try { Type c; c = Sharpen.Runtime.GetType(JSch.GetConfig("3des-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); } catch (Exception) { } return(cipher); }
private HASH GenHash() { try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); } catch (Exception) { } return(hash); }
public virtual string GetFingerPrint(JSch jsch) { HASH hash = null; try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); } catch (Exception e) { System.Console.Error.WriteLine("getFingerPrint: " + e); } return(Util.GetFingerPrint(hash, key)); }
private Random GenRandom() { if (random == null) { try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("random")); random = (Random)(System.Activator.CreateInstance(c)); } catch (Exception e) { System.Console.Error.WriteLine("connect: random " + e); } } return(random); }
// static int min=512; // static int preferred=1024; // static int max=2000; // com.jcraft.jsch.DH dh; //private byte[] f; /// <exception cref="System.Exception"></exception> public override void Init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte [] I_C) { this.session = session; this.V_S = V_S; this.V_C = V_C; this.I_S = I_S; this.I_C = I_C; try { Type c = Sharpen.Runtime.GetType(session.GetConfig("sha-1")); sha = (HASH)(System.Activator.CreateInstance(c)); sha.Init(); } catch (Exception e) { System.Console.Error.WriteLine(e); } buf = new Buffer(); packet = new Packet(buf); try { Type c = Sharpen.Runtime.GetType(session.GetConfig("dh")); dh = (NSch.DH)(System.Activator.CreateInstance(c)); dh.Init(); } catch (Exception e) { // System.err.println(e); throw; } packet.Reset(); buf.PutByte(unchecked ((byte)SSH_MSG_KEX_DH_GEX_REQUEST)); buf.PutInt(min); buf.PutInt(preferred); buf.PutInt(max); session.Write(packet); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEX_DH_GEX_REQUEST(" + min + "<" + preferred + "<" + max + ") sent"); JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEX_DH_GEX_GROUP"); } state = SSH_MSG_KEX_DH_GEX_GROUP; }
private MAC GetHMACSHA1() { lock (this) { if (hmacsha1 == null) { try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("hmac-sha1")); hmacsha1 = (MAC)(System.Activator.CreateInstance(c)); } catch (Exception e) { System.Console.Error.WriteLine("hmacsha1: " + e); } } return(hmacsha1); } }
internal virtual byte[] GetSignature_rsa(byte[] data) { try { Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("signature.rsa")); NSch.SignatureRSA rsa = (NSch.SignatureRSA)(System.Activator.CreateInstance(c)); rsa.Init(); rsa.SetPrvKey(d_array, n_array, e_array); rsa.Update(data); byte[] sig = rsa.Sign(); Buffer buf = new Buffer("ssh-rsa".Length + 4 + sig.Length + 4); buf.PutString(Util.Str2byte("ssh-rsa")); buf.PutString(sig); return(buf.buffer); } catch (Exception) { } return(null); }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair GenKeyPair(JSch jsch, int type, int key_size) { NSch.KeyPair kpair = null; if (type == DSA) { kpair = new KeyPairDSA(jsch); } else { if (type == RSA) { kpair = new KeyPairRSA(jsch); } } if (kpair != null) { kpair.Generate(key_size); } return kpair; }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair GenKeyPair(JSch jsch, int type, int key_size) { NSch.KeyPair kpair = null; if (type == DSA) { kpair = new KeyPairDSA(jsch); } else { if (type == RSA) { kpair = new KeyPairRSA(jsch); } } if (kpair != null) { kpair.Generate(key_size); } return(kpair); }
internal virtual byte[] GetSignature_dss(byte[] data) { try { Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("signature.dss")); NSch.SignatureDSA dsa = (NSch.SignatureDSA)(System.Activator.CreateInstance(c)); dsa.Init(); dsa.SetPrvKey(prv_array, P_array, Q_array, G_array); dsa.Update(data); byte[] sig = dsa.Sign(); Buffer buf = new Buffer("ssh-dss".Length + 4 + sig.Length + 4); buf.PutString(Util.Str2byte("ssh-dss")); buf.PutString(sig); return(buf.buffer); } catch (Exception) { } //System.err.println("e "+e); return(null); }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair Load(JSch jsch, string prvkey, string pubkey) { byte[] iv = new byte[8]; // 8 bool encrypted = true; byte[] data = null; byte[] publickeyblob = null; int type = ERROR; int vendor = VENDOR_OPENSSH; try { FilePath file = new FilePath(prvkey); FileInputStream fis = new FileInputStream(prvkey); byte[] buf = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(buf, len, buf.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); int i_1 = 0; while (i_1 < len) { if (buf[i_1] == 'B' && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'G' && buf[i_1 + 3] == 'I') { i_1 += 6; if (buf[i_1] == 'D' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A') { type = DSA; } else { if (buf[i_1] == 'R' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A') { type = RSA; } else { if (buf[i_1] == 'S' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'H') { // FSecure type = UNKNOWN; vendor = VENDOR_FSECURE; } else { //System.err.println("invalid format: "+identity); throw new JSchException("invalid privatekey: " + prvkey); } } } i_1 += 3; continue; } if (buf[i_1] == 'C' && buf[i_1 + 1] == 'B' && buf[i_1 + 2] == 'C' && buf[i_1 + 3] == ',') { i_1 += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked((byte)(((A2b(buf[i_1++]) << 4) & unchecked((int)(0xf0))) + (A2b (buf[i_1++]) & unchecked((int)(0xf))))); } continue; } if (buf[i_1] == unchecked((int)(0x0d)) && i_1 + 1 < buf.Length && buf[i_1 + 1] == unchecked((int)(0x0a))) { i_1++; continue; } if (buf[i_1] == unchecked((int)(0x0a)) && i_1 + 1 < buf.Length) { if (buf[i_1 + 1] == unchecked((int)(0x0a))) { i_1 += 2; break; } if (buf[i_1 + 1] == unchecked((int)(0x0d)) && i_1 + 2 < buf.Length && buf[i_1 + 2 ] == unchecked((int)(0x0a))) { i_1 += 3; break; } bool inheader = false; for (int j = i_1 + 1; j < buf.Length; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i_1++; encrypted = false; // no passphrase break; } } i_1++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + prvkey); } int start = i_1; while (i_1 < len) { if (buf[i_1] == unchecked((int)(0x0a))) { bool xd = (buf[i_1 - 1] == unchecked((int)(0x0d))); System.Array.Copy(buf, i_1 + 1, buf, i_1 - (xd ? 1 : 0), len - i_1 - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i_1] == '-') { break; } i_1++; } data = Util.FromBase64(buf, start, i_1 - start); if (data.Length > 4 && data[0] == unchecked((byte)unchecked((int)(0x3f))) && data [1] == unchecked((byte)unchecked((int)(0x6f))) && data[2] == unchecked((byte)unchecked( (int)(0xf9))) && data[3] == unchecked((byte)unchecked((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); byte[] _cipher = _buf.GetString(); string cipher = Util.Byte2str(_cipher); //System.err.println("cipher: "+cipher); if (cipher.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + prvkey); } else { if (cipher.Equals("none")) { _buf.GetInt(); _buf.GetInt(); encrypted = false; byte[] foo = new byte[data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); data = foo; } } } if (pubkey != null) { try { file = new FilePath(pubkey); fis = new FileInputStream(pubkey); buf = new byte[(int)(file.Length())]; len = 0; while (true) { i_1 = fis.Read(buf, len, buf.Length - len); if (i_1 <= 0) { break; } len += i_1; } fis.Close(); if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key bool valid = true; i_1 = 0; do { i_1++; } while (buf.Length > i_1 && buf[i_1] != unchecked((int)(0x0a))); if (buf.Length <= i_1) { valid = false; } while (valid) { if (buf[i_1] == unchecked((int)(0x0a))) { bool inheader = false; for (int j = i_1 + 1; j < buf.Length; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i_1++; break; } } i_1++; } if (buf.Length <= i_1) { valid = false; } start = i_1; while (valid && i_1 < len) { if (buf[i_1] == unchecked((int)(0x0a))) { System.Array.Copy(buf, i_1 + 1, buf, i_1, len - i_1 - 1); len--; continue; } if (buf[i_1] == '-') { break; } i_1++; } if (valid) { publickeyblob = Util.FromBase64(buf, start, i_1 - start); if (type == UNKNOWN) { if (publickeyblob[8] == 'd') { type = DSA; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } } else { if (buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-') { i_1 = 0; while (i_1 < len) { if (buf[i_1] == ' ') { break; } i_1++; } i_1++; if (i_1 < len) { start = i_1; while (i_1 < len) { if (buf[i_1] == ' ') { break; } i_1++; } publickeyblob = Util.FromBase64(buf, start, i_1 - start); } } } } catch (Exception) { } } } catch (Exception e) { if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } NSch.KeyPair kpair = null; if (type == DSA) { kpair = new KeyPairDSA(jsch); } else { if (type == RSA) { kpair = new KeyPairRSA(jsch); } } if (kpair != null) { kpair.encrypted = encrypted; kpair.publickeyblob = publickeyblob; kpair.vendor = vendor; if (encrypted) { kpair.iv = iv; kpair.data = data; } else { if (kpair.Parse(data)) { return kpair; } else { throw new JSchException("invalid privatekey: " + prvkey); } } } return kpair; }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair Load(JSch jsch, string prvkey) { string pubkey = prvkey + ".pub"; if (!new FilePath(pubkey).Exists()) { pubkey = null; } return Load(jsch, prvkey, pubkey); }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair GenKeyPair(JSch jsch, int type) { return GenKeyPair(jsch, type, 1024); }
private static void LoadIdentity(JSch sch, FilePath priv) { if (priv.IsFile()) { try { sch.AddIdentity(priv.GetAbsolutePath()); } catch (JSchException) { } } }
public KeyPair(JSch jsch) { this.jsch = jsch; }
// Oh well. They don't have a known hosts in home. // Oh well. They don't have a known hosts in home. private static void Identities(JSch sch, FS fs) { FilePath home = fs.UserHome(); if (home == null) { return; } FilePath sshdir = new FilePath(home, ".ssh"); if (sshdir.IsDirectory()) { LoadIdentity(sch, new FilePath(sshdir, "identity")); LoadIdentity(sch, new FilePath(sshdir, "id_rsa")); LoadIdentity(sch, new FilePath(sshdir, "id_dsa")); } }
/// <exception cref="NSch.JSchException"></exception> private static void KnownHosts(JSch sch, FS fs) { FilePath home = fs.UserHome(); if (home == null) { return; } FilePath known_hosts = new FilePath(new FilePath(home, ".ssh"), "known_hosts"); try { FileInputStream @in = new FileInputStream(known_hosts); try { sch.SetKnownHosts(@in); } finally { @in.Close(); } } catch (FileNotFoundException) { } catch (IOException) { } }
/// <exception cref="System.Exception"></exception> public override bool Next(Buffer _buf) { int i; int j; switch (state) { case SSH_MSG_KEX_DH_GEX_GROUP: { // byte SSH_MSG_KEX_DH_GEX_GROUP(31) // mpint p, safe prime // mpint g, generator for subgroup in GF (p) _buf.GetInt(); _buf.GetByte(); j = _buf.GetByte(); if (j != SSH_MSG_KEX_DH_GEX_GROUP) { System.Console.Error.WriteLine("type: must be SSH_MSG_KEX_DH_GEX_GROUP " + j); return(false); } p = _buf.GetMPInt(); g = _buf.GetMPInt(); dh.SetP(p); dh.SetG(g); // The client responds with: // byte SSH_MSG_KEX_DH_GEX_INIT(32) // mpint e <- g^x mod p // x is a random number (1 < x < (p-1)/2) e = dh.GetE(); packet.Reset(); buf.PutByte(unchecked ((byte)SSH_MSG_KEX_DH_GEX_INIT)); buf.PutMPInt(e); session.Write(packet); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEX_DH_GEX_INIT sent"); JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEX_DH_GEX_REPLY"); } state = SSH_MSG_KEX_DH_GEX_REPLY; return(true); } case SSH_MSG_KEX_DH_GEX_REPLY: { //break; // The server responds with: // byte SSH_MSG_KEX_DH_GEX_REPLY(33) // string server public host key and certificates (K_S) // mpint f // string signature of H j = _buf.GetInt(); j = _buf.GetByte(); j = _buf.GetByte(); if (j != SSH_MSG_KEX_DH_GEX_REPLY) { System.Console.Error.WriteLine("type: must be SSH_MSG_KEX_DH_GEX_REPLY " + j); return(false); } K_S = _buf.GetString(); // K_S is server_key_blob, which includes .... // string ssh-dss // impint p of dsa // impint q of dsa // impint g of dsa // impint pub_key of dsa //System.err.print("K_S: "); dump(K_S, 0, K_S.length); byte[] f = _buf.GetMPInt(); byte[] sig_of_H = _buf.GetString(); dh.SetF(f); K = dh.GetK(); //The hash H is computed as the HASH hash of the concatenation of the //following: // string V_C, the client's version string (CR and NL excluded) // string V_S, the server's version string (CR and NL excluded) // string I_C, the payload of the client's SSH_MSG_KEXINIT // string I_S, the payload of the server's SSH_MSG_KEXINIT // string K_S, the host key // uint32 min, minimal size in bits of an acceptable group // uint32 n, preferred size in bits of the group the server should send // uint32 max, maximal size in bits of an acceptable group // mpint p, safe prime // mpint g, generator for subgroup // mpint e, exchange value sent by the client // mpint f, exchange value sent by the server // mpint K, the shared secret // This value is called the exchange hash, and it is used to authenti- // cate the key exchange. buf.Reset(); buf.PutString(V_C); buf.PutString(V_S); buf.PutString(I_C); buf.PutString(I_S); buf.PutString(K_S); buf.PutInt(min); buf.PutInt(preferred); buf.PutInt(max); buf.PutMPInt(p); buf.PutMPInt(g); buf.PutMPInt(e); buf.PutMPInt(f); buf.PutMPInt(K); byte[] foo = new byte[buf.GetLength()]; buf.GetByte(foo); sha.Update(foo, 0, foo.Length); H = sha.Digest(); // System.err.print("H -> "); dump(H, 0, H.length); i = 0; j = 0; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); string alg = Util.Byte2str(K_S, i, j); i += j; bool result = false; if (alg.Equals("ssh-rsa")) { byte[] tmp; byte[] ee; byte[] n; type = RSA; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; ee = tmp; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; n = tmp; // SignatureRSA sig=new SignatureRSA(); // sig.init(); NSch.SignatureRSA sig = null; try { Type c = Sharpen.Runtime.GetType(session.GetConfig("signature.rsa")); sig = (NSch.SignatureRSA)(System.Activator.CreateInstance(c)); sig.Init(); } catch (Exception ex) { System.Console.Error.WriteLine(ex); } sig.SetPubKey(ee, n); sig.Update(H); result = sig.Verify(sig_of_H); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "ssh_rsa_verify: signature " + result); } } else { if (alg.Equals("ssh-dss")) { byte[] q = null; byte[] tmp; type = DSS; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; p = tmp; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; q = tmp; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; g = tmp; j = ((K_S[i++] << 24) & unchecked ((int)(0xff000000))) | ((K_S[i++] << 16) & unchecked ( (int)(0x00ff0000))) | ((K_S[i++] << 8) & unchecked ((int)(0x0000ff00))) | ((K_S[i ++]) & unchecked ((int)(0x000000ff))); tmp = new byte[j]; System.Array.Copy(K_S, i, tmp, 0, j); i += j; f = tmp; // SignatureDSA sig=new SignatureDSA(); // sig.init(); NSch.SignatureDSA sig = null; try { Type c = Sharpen.Runtime.GetType(session.GetConfig("signature.dss")); sig = (NSch.SignatureDSA)(System.Activator.CreateInstance(c)); sig.Init(); } catch (Exception ex) { System.Console.Error.WriteLine(ex); } sig.SetPubKey(f, p, q, g); sig.Update(H); result = sig.Verify(sig_of_H); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "ssh_dss_verify: signature " + result); } } else { System.Console.Error.WriteLine("unknown alg"); } } state = STATE_END; return(result); } } return(false); }
/// <exception cref="NSch.JSchException"></exception> internal static NSch.IdentityFile NewInstance(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { try { return new NSch.IdentityFile(name, prvkey, pubkey, jsch); } finally { Util.Bzero(prvkey); } }
internal LocalIdentityRepository(JSch jsch) { this.jsch = jsch; }
/// <exception cref="NSch.JSchException"></exception> private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { this.identity = name; this.jsch = jsch; try { Type c; c = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; // 24 iv = new byte[cipher.GetIVSize()]; // 8 c = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); byte[] buf = prvkey; int len = buf.Length; int i = 0; while (i < len) { if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf [i + 3] == '-' && buf[i + 4] == '-') { break; } i++; } while (i < len) { if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf [i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSS; } else { if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else { if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; keytype = FSECURE; } else { //System.err.println("invalid format: "+identity); throw new JSchException("invalid privatekey: " + identity); } } } i += 3; continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + identity); } continue; } if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf [i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked((byte)(((A2b(buf[i++]) << 4) & unchecked((int)(0xf0))) + (A2b( buf[i++]) & unchecked((int)(0xf))))); } continue; } if (buf[i] == unchecked((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked((int )(0x0a))) { i++; continue; } if (buf[i] == unchecked((int)(0x0a)) && i + 1 < len) { if (buf[i + 1] == unchecked((int)(0x0a))) { i += 2; break; } if (buf[i + 1] == unchecked((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked( (int)(0x0a))) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + identity); } int start = i; while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { bool xd = (buf[i - 1] == unchecked((int)(0x0d))); System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i] == '-') { break; } i++; } encoded_data = Util.FromBase64(buf, start, i - start); if (encoded_data.Length > 4 && encoded_data[0] == unchecked((byte)unchecked((int) (0x3f))) && encoded_data[1] == unchecked((byte)unchecked((int)(0x6f))) && encoded_data [2] == unchecked((byte)unchecked((int)(0xf9))) && encoded_data[3] == unchecked(( byte)unchecked((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(encoded_data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); byte[] _cipher = _buf.GetString(); string cipher2 = Util.Byte2str(_cipher); //System.err.println("cipher: "+cipher); if (cipher2.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else { if (cipher2.Equals("none")) { _buf.GetInt(); //_buf.getInt(); encrypted = false; byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; } } } if (pubkey == null) { return; } buf = pubkey; len = buf.Length; if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key i = 0; do { i++; } while (len > i && buf[i] != unchecked((int)(0x0a))); if (len <= i) { return; } while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (len <= i) { return; } start = i; while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { System.Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (type == UNKNOWN && publickeyblob.Length > 8) { if (publickeyblob[8] == 'd') { type = DSS; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') { return; } i = 0; while (i < len) { if (buf[i] == ' ') { break; } i++; } i++; if (i >= len) { return; } start = i; while (i < len) { if (buf[i] == ' ' || buf[i] == '\n') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (publickeyblob.Length < 4 + 7) { // It must start with "ssh-XXX". if (JSch.GetLogger().IsEnabled(Logger.WARN)) { JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key"); } publickeyblob = null; } } } catch (Exception e) { //System.err.println("IdentityFile: "+e); if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } }
internal KnownHosts(JSch jsch) : base() { this.jsch = jsch; pool = new ArrayList(); }
/// <exception cref="NSch.JSchException"></exception> private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { this.identity = name; this.jsch = jsch; // prvkey from "ssh-add" command on the remote. if (pubkey == null && prvkey != null && (prvkey.Length > 11 && prvkey[0] == 0 && prvkey[1] == 0 && prvkey[2] == 0 && prvkey[3] == 7)) { Buffer buf = new Buffer(prvkey); string _type = Sharpen.Runtime.GetStringForBytes(buf.GetString()); // ssh-rsa if (_type.Equals("ssh-rsa")) { type = RSA; n_array = buf.GetString(); e_array = buf.GetString(); d_array = buf.GetString(); buf.GetString(); buf.GetString(); buf.GetString(); this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString()); } else { if (_type.Equals("ssh-dss")) { type = DSS; P_array = buf.GetString(); Q_array = buf.GetString(); G_array = buf.GetString(); pub_array = buf.GetString(); prv_array = buf.GetString(); this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString()); } else { throw new JSchException("privatekey: invalid key " + Sharpen.Runtime.GetStringForBytes (prvkey, 4, 7)); } } encoded_data = prvkey; encrypted = false; keytype = OPENSSH; return; } try { Type c; c = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; // 24 iv = new byte[cipher.GetIVSize()]; // 8 c = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); byte[] buf = prvkey; int len = buf.Length; int i = 0; while (i < len) { if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf [i + 3] == '-' && buf[i + 4] == '-') { break; } i++; } while (i < len) { if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf [i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSS; } else { if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else { if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; keytype = FSECURE; } else { //System.err.println("invalid format: "+identity); throw new JSchException("invalid privatekey: " + identity); } } } i += 3; continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + identity); } continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '9' && buf[i + 6] == '2' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes192-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes192-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes192-cbc is not available " + identity); } continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '2' && buf[i + 6] == '8' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes128-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes128-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes128-cbc is not available " + identity); } continue; } if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf [i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked ((byte)(((A2b(buf[i++]) << 4) & unchecked ((int)(0xf0))) + (A2b( buf[i++]) & unchecked ((int)(0xf))))); } continue; } if (buf[i] == unchecked ((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked ((int )(0x0a))) { i++; continue; } if (buf[i] == unchecked ((int)(0x0a)) && i + 1 < len) { if (buf[i + 1] == unchecked ((int)(0x0a))) { i += 2; break; } if (buf[i + 1] == unchecked ((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked ( (int)(0x0a))) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + identity); } int start = i; while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { bool xd = (buf[i - 1] == unchecked ((int)(0x0d))); System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i] == '-') { break; } i++; } encoded_data = Util.FromBase64(buf, start, i - start); if (encoded_data.Length > 4 && encoded_data[0] == unchecked ((byte)unchecked ((int) (0x3f))) && encoded_data[1] == unchecked ((byte)unchecked ((int)(0x6f))) && encoded_data [2] == unchecked ((byte)unchecked ((int)(0xf9))) && encoded_data[3] == unchecked (( byte)unchecked ((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(encoded_data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); byte[] _cipher = _buf.GetString(); string cipherStr = Util.Byte2str(_cipher); //System.err.println("cipher: "+cipher); if (cipherStr.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else { if (cipherStr.Equals("none")) { _buf.GetInt(); //_buf.getInt(); encrypted = false; byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; } } } if (pubkey == null) { return; } buf = pubkey; len = buf.Length; if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key i = 0; do { i++; }while (len > i && buf[i] != unchecked ((int)(0x0a))); if (len <= i) { return; } while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (len <= i) { return; } start = i; while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { System.Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (type == UNKNOWN && publickeyblob.Length > 8) { if (publickeyblob[8] == 'd') { type = DSS; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') { return; } i = 0; while (i < len) { if (buf[i] == ' ') { break; } i++; } i++; if (i >= len) { return; } start = i; while (i < len) { if (buf[i] == ' ' || buf[i] == '\n') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (publickeyblob.Length < 4 + 7) { // It must start with "ssh-XXX". if (JSch.GetLogger().IsEnabled(Logger.WARN)) { JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key"); } publickeyblob = null; } } } catch (Exception e) { //System.err.println("IdentityFile: "+e); if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } }
public KeyPairRSA(JSch jsch) : base(jsch) { }
// DSA // RSA // modulus // public exponent // private exponent // private String algname="ssh-dss"; /// <exception cref="NSch.JSchException"></exception> internal static NSch.IdentityFile NewInstance(string prvfile, string pubfile, JSch jsch) { byte[] prvkey = null; byte[] pubkey = null; FilePath file = null; FileInputStream fis = null; try { file = new FilePath(prvfile); fis = new FileInputStream(prvfile); prvkey = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(prvkey, len, prvkey.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); } catch (Exception e) { try { if (fis != null) { fis.Close(); } } catch (Exception) { } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } string _pubfile = pubfile; if (pubfile == null) { _pubfile = prvfile + ".pub"; } try { file = new FilePath(_pubfile); fis = new FileInputStream(_pubfile); pubkey = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(pubkey, len, pubkey.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); } catch (Exception e) { try { if (fis != null) { fis.Close(); } } catch (Exception) { } if (pubfile != null) { // The pubfile is explicitry given, but not accessible. if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } } return(NewInstance(prvfile, prvkey, pubkey, jsch)); }
protected internal static string[] Guess(byte[] I_S, byte[] I_C) { string[] guess = new string[PROPOSAL_MAX]; Buffer sb = new Buffer(I_S); sb.SetOffSet(17); Buffer cb = new Buffer(I_C); cb.SetOffSet(17); for (int i = 0; i < PROPOSAL_MAX; i++) { byte[] sp = sb.GetString(); // server proposal byte[] cp = cb.GetString(); // client proposal int j = 0; int k = 0; while (j < cp.Length) { while (j < cp.Length && cp[j] != ',') { j++; } if (k == j) { return(null); } string algorithm = Util.Byte2str(cp, k, j - k); int l = 0; int m = 0; while (l < sp.Length) { while (l < sp.Length && sp[l] != ',') { l++; } if (m == l) { return(null); } if (algorithm.Equals(Util.Byte2str(sp, m, l - m))) { guess[i] = algorithm; goto loop_break; } l++; m = l; } j++; k = j; loop_continue :; } loop_break :; if (j == 0) { guess[i] = string.Empty; } else { if (guess[i] == null) { return(null); } } } if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "kex: server->client" + " " + guess[PROPOSAL_ENC_ALGS_STOC ] + " " + guess[PROPOSAL_MAC_ALGS_STOC] + " " + guess[PROPOSAL_COMP_ALGS_STOC]); JSch.GetLogger().Log(Logger.INFO, "kex: client->server" + " " + guess[PROPOSAL_ENC_ALGS_CTOS ] + " " + guess[PROPOSAL_MAC_ALGS_CTOS] + " " + guess[PROPOSAL_COMP_ALGS_CTOS]); } // for(int i=0; i<PROPOSAL_MAX; i++){ // System.err.println("guess: ["+guess[i]+"]"); // } return(guess); }
public KeyPairDSA(JSch jsch) : base(jsch) { }
// DSA // RSA // modulus // public exponent // private exponent // private String algname="ssh-dss"; /// <exception cref="NSch.JSchException"></exception> internal static NSch.IdentityFile NewInstance(string prvfile, string pubfile, JSch jsch) { byte[] prvkey = null; byte[] pubkey = null; FilePath file = null; FileInputStream fis = null; try { file = new FilePath(prvfile); fis = new FileInputStream(prvfile); prvkey = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(prvkey, len, prvkey.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); } catch (Exception e) { try { if (fis != null) { fis.Close(); } } catch (Exception) { } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } string _pubfile = pubfile; if (pubfile == null) { _pubfile = prvfile + ".pub"; } try { file = new FilePath(_pubfile); fis = new FileInputStream(_pubfile); pubkey = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(pubkey, len, pubkey.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); } catch (Exception e) { try { if (fis != null) { fis.Close(); } } catch (Exception) { } if (pubfile != null) { // The pubfile is explicitry given, but not accessible. if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } } return NewInstance(prvfile, prvkey, pubkey, jsch); }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair GenKeyPair(JSch jsch, int type) { return(GenKeyPair(jsch, type, 1024)); }
public virtual string GetFingerPrint(JSch jsch) { HASH hash = null; try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); } catch (Exception e) { System.Console.Error.WriteLine("getFingerPrint: " + e); } return Util.GetFingerPrint(hash, key); }
/// <exception cref="NSch.JSchException"></exception> public static NSch.KeyPair Load(JSch jsch, string prvkey, string pubkey) { byte[] iv = new byte[8]; // 8 bool encrypted = true; byte[] data = null; byte[] publickeyblob = null; int type = ERROR; int vendor = VENDOR_OPENSSH; string publicKeyComment = string.Empty; NSch.Cipher cipher = null; try { FilePath file = new FilePath(prvkey); FileInputStream fis = new FileInputStream(prvkey); byte[] buf = new byte[(int)(file.Length())]; int len = 0; while (true) { int i = fis.Read(buf, len, buf.Length - len); if (i <= 0) { break; } len += i; } fis.Close(); int i_1 = 0; while (i_1 < len) { if (buf[i_1] == '-' && i_1 + 4 < len && buf[i_1 + 1] == '-' && buf[i_1 + 2] == '-' && buf[i_1 + 3] == '-' && buf[i_1 + 4] == '-') { break; } i_1++; } while (i_1 < len) { if (buf[i_1] == 'B' && i_1 + 3 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'G' && buf[i_1 + 3] == 'I') { i_1 += 6; if (buf[i_1] == 'D' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A') { type = DSA; } else { if (buf[i_1] == 'R' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A') { type = RSA; } else { if (buf[i_1] == 'S' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'H') { // FSecure type = UNKNOWN; vendor = VENDOR_FSECURE; } else { throw new JSchException("invalid privatekey: " + prvkey); } } } i_1 += 3; continue; } if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' && buf[i_1 + 3] == '-' && buf[i_1 + 4] == '2' && buf[i_1 + 5] == '5' && buf[i_1 + 6] == '6' && buf[i_1 + 7] == '-') { i_1 += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc"))) { Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); // key=new byte[cipher.getBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + prvkey); } continue; } if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' && buf[i_1 + 3] == '-' && buf[i_1 + 4] == '1' && buf[i_1 + 5] == '9' && buf[i_1 + 6] == '2' && buf[i_1 + 7] == '-') { i_1 += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes192-cbc"))) { Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes192-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); // key=new byte[cipher.getBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes192-cbc is not available " + prvkey); } continue; } if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' && buf[i_1 + 3] == '-' && buf[i_1 + 4] == '1' && buf[i_1 + 5] == '2' && buf[i_1 + 6] == '8' && buf[i_1 + 7] == '-') { i_1 += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes128-cbc"))) { Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes128-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); // key=new byte[cipher.getBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes128-cbc is not available " + prvkey); } continue; } if (buf[i_1] == 'C' && i_1 + 3 < len && buf[i_1 + 1] == 'B' && buf[i_1 + 2] == 'C' && buf[i_1 + 3] == ',') { i_1 += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked ((byte)(((A2b(buf[i_1++]) << 4) & unchecked ((int)(0xf0))) + (A2b (buf[i_1++]) & unchecked ((int)(0xf))))); } continue; } if (buf[i_1] == unchecked ((int)(0x0d)) && i_1 + 1 < buf.Length && buf[i_1 + 1] == unchecked ((int)(0x0a))) { i_1++; continue; } if (buf[i_1] == unchecked ((int)(0x0a)) && i_1 + 1 < buf.Length) { if (buf[i_1 + 1] == unchecked ((int)(0x0a))) { i_1 += 2; break; } if (buf[i_1 + 1] == unchecked ((int)(0x0d)) && i_1 + 2 < buf.Length && buf[i_1 + 2 ] == unchecked ((int)(0x0a))) { i_1 += 3; break; } bool inheader = false; for (int j = i_1 + 1; j < buf.Length; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i_1++; encrypted = false; // no passphrase break; } } i_1++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + prvkey); } int start = i_1; while (i_1 < len) { if (buf[i_1] == unchecked ((int)(0x0a))) { bool xd = (buf[i_1 - 1] == unchecked ((int)(0x0d))); System.Array.Copy(buf, i_1 + 1, buf, i_1 - (xd ? 1 : 0), len - i_1 - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i_1] == '-') { break; } i_1++; } data = Util.FromBase64(buf, start, i_1 - start); if (data.Length > 4 && data[0] == unchecked ((byte)unchecked ((int)(0x3f))) && data [1] == unchecked ((byte)unchecked ((int)(0x6f))) && data[2] == unchecked ((byte)unchecked ( (int)(0xf9))) && data[3] == unchecked ((byte)unchecked ((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); string _cipher = Util.Byte2str(_buf.GetString()); //System.err.println("cipher: "+_cipher); if (_cipher.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + prvkey); } else { if (_cipher.Equals("none")) { _buf.GetInt(); _buf.GetInt(); encrypted = false; byte[] foo = new byte[data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); data = foo; } } } if (pubkey != null) { try { file = new FilePath(pubkey); fis = new FileInputStream(pubkey); buf = new byte[(int)(file.Length())]; len = 0; while (true) { i_1 = fis.Read(buf, len, buf.Length - len); if (i_1 <= 0) { break; } len += i_1; } fis.Close(); if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key bool valid = true; i_1 = 0; do { i_1++; }while (buf.Length > i_1 && buf[i_1] != unchecked ((int)(0x0a))); if (buf.Length <= i_1) { valid = false; } while (valid) { if (buf[i_1] == unchecked ((int)(0x0a))) { bool inheader = false; for (int j = i_1 + 1; j < buf.Length; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i_1++; break; } } i_1++; } if (buf.Length <= i_1) { valid = false; } start = i_1; while (valid && i_1 < len) { if (buf[i_1] == unchecked ((int)(0x0a))) { System.Array.Copy(buf, i_1 + 1, buf, i_1, len - i_1 - 1); len--; continue; } if (buf[i_1] == '-') { break; } i_1++; } if (valid) { publickeyblob = Util.FromBase64(buf, start, i_1 - start); if (type == UNKNOWN) { if (publickeyblob[8] == 'd') { type = DSA; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } } else { if (buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-') { i_1 = 0; while (i_1 < len) { if (buf[i_1] == ' ') { break; } i_1++; } i_1++; if (i_1 < len) { start = i_1; while (i_1 < len) { if (buf[i_1] == ' ') { break; } i_1++; } publickeyblob = Util.FromBase64(buf, start, i_1 - start); } if (i_1++ < len) { int s = i_1; while (i_1 < len) { if (buf[i_1] == '\n') { break; } i_1++; } if (i_1 < len) { publicKeyComment = Sharpen.Runtime.GetStringForBytes(buf, s, i_1 - s); } } } } } catch (Exception) { } } } catch (Exception e) { if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } NSch.KeyPair kpair = null; if (type == DSA) { kpair = new KeyPairDSA(jsch); } else { if (type == RSA) { kpair = new KeyPairRSA(jsch); } } if (kpair != null) { kpair.encrypted = encrypted; kpair.publickeyblob = publickeyblob; kpair.vendor = vendor; kpair.publicKeyComment = publicKeyComment; kpair.cipher = cipher; if (encrypted) { kpair.iv = iv; kpair.data = data; } else { if (kpair.Parse(data)) { return(kpair); } else { throw new JSchException("invalid privatekey: " + prvkey); } } } return(kpair); }
/// <param name="fs"> /// the file system abstraction which will be necessary to /// perform certain file system operations. /// </param> /// <returns>the new default JSch implementation.</returns> /// <exception cref="NSch.JSchException">known host keys cannot be loaded.</exception> protected internal virtual JSch CreateDefaultJSch(FS fs) { JSch jsch = new JSch(); KnownHosts(jsch, fs); Identities(jsch, fs); return jsch; }
/// <exception cref="System.Exception"></exception> public override bool Start(Session session) { base.Start(session); // send // byte SSH_MSG_SERVICE_REQUEST(5) // string service name "ssh-userauth" packet.Reset(); buf.PutByte(unchecked ((byte)Session.SSH_MSG_SERVICE_REQUEST)); buf.PutString(Util.Str2byte("ssh-userauth")); session.Write(packet); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_SERVICE_REQUEST sent"); } // receive // byte SSH_MSG_SERVICE_ACCEPT(6) // string service name buf = session.Read(buf); int command = buf.GetCommand(); bool result = (command == SSH_MSG_SERVICE_ACCEPT); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_SERVICE_ACCEPT received"); } if (!result) { return(false); } byte[] _username = null; _username = Util.Str2byte(username); // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name // string service name ("ssh-connection") // string "none" packet.Reset(); buf.PutByte(unchecked ((byte)SSH_MSG_USERAUTH_REQUEST)); buf.PutString(_username); buf.PutString(Util.Str2byte("ssh-connection")); buf.PutString(Util.Str2byte("none")); session.Write(packet); while (true) { buf = session.Read(buf); command = buf.GetCommand() & unchecked ((int)(0xff)); if (command == SSH_MSG_USERAUTH_SUCCESS) { return(true); } if (command == SSH_MSG_USERAUTH_BANNER) { buf.GetInt(); buf.GetByte(); buf.GetByte(); byte[] _message = buf.GetString(); byte[] lang = buf.GetString(); string message = Util.Byte2str(_message); if (userinfo != null) { try { userinfo.ShowMessage(message); } catch (RuntimeException) { } } goto loop_continue; } if (command == SSH_MSG_USERAUTH_FAILURE) { buf.GetInt(); buf.GetByte(); buf.GetByte(); byte[] foo = buf.GetString(); int partial_success = buf.GetByte(); methods = Util.Byte2str(foo); //System.err.println("UserAuthNONE: "+methods+ // " partial_success:"+(partial_success!=0)); // if(partial_success!=0){ // throw new JSchPartialAuthException(new String(foo)); // } break; } else { // System.err.println("USERAUTH fail ("+command+")"); throw new JSchException("USERAUTH fail (" + command + ")"); } loop_continue :; } loop_break :; //throw new JSchException("USERAUTH fail"); return(false); }
/// <summary>Obtain the JSch used to create new sessions.</summary> /// <remarks>Obtain the JSch used to create new sessions.</remarks> /// <param name="hc">host configuration</param> /// <param name="fs"> /// the file system abstraction which will be necessary to /// perform certain file system operations. /// </param> /// <returns>the JSch instance to use.</returns> /// <exception cref="NSch.JSchException">the user configuration could not be created. /// </exception> protected internal virtual JSch GetJSch(OpenSshConfig.Host hc, FS fs) { if (defaultJSch == null) { defaultJSch = CreateDefaultJSch(fs); foreach (object name in defaultJSch.GetIdentityNames()) { byIdentityFile.Put((string)name, defaultJSch); } } FilePath identityFile = hc.GetIdentityFile(); if (identityFile == null) { return defaultJSch; } string identityKey = identityFile.GetAbsolutePath(); JSch jsch = byIdentityFile.Get(identityKey); if (jsch == null) { jsch = new JSch(); jsch.SetHostKeyRepository(defaultJSch.GetHostKeyRepository()); jsch.AddIdentity(identityKey); byIdentityFile.Put(identityKey, jsch); } return jsch; }
/// <exception cref="NSch.JSchException"></exception> internal Session(JSch jsch) : base() { grr = new Session.GlobalRequestReply(this); // http://ietf.org/internet-drafts/draft-ietf-secsh-assignednumbers-01.txt // server version // client version // the payload of the client's SSH_MSG_KEXINIT // the payload of the server's SSH_MSG_KEXINIT // the host key //private byte[] mac_buf; this.jsch = jsch; buf = new Buffer(); packet = new Packet(buf); }