internal override void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); byte[] addr = buf.GetString(); int port = buf.GetInt(); byte[] orgaddr = buf.GetString(); int orgport = buf.GetInt(); Session _session = null; try { _session = GetSession(); } catch (JSchException) { } // session has been already down. lock (pool) { for (int i = 0; i < pool.Count; i++) { object[] foo = (object[])(pool[i]); if (foo[0] != _session) { continue; } if (((int)foo[1]) != port) { continue; } this.rport = port; this.target = (string)foo[2]; if (foo[3] == null || (foo[3] is object[])) { this.lport = -1; } else { this.lport = ((int)foo[3]); } if (foo.Length >= 6) { this.factory = ((SocketFactory)foo[5]); } break; } if (target == null) { } } }
internal static NSch.SftpATTRS GetATTR(Buffer buf) { NSch.SftpATTRS attr = new NSch.SftpATTRS(); attr.flags = buf.GetInt(); if ((attr.flags & SSH_FILEXFER_ATTR_SIZE) != 0) { attr.size = buf.GetLong(); } if ((attr.flags & SSH_FILEXFER_ATTR_UIDGID) != 0) { attr.uid = buf.GetInt(); attr.gid = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { attr.permissions = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { attr.atime = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { attr.mtime = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) { int count = buf.GetInt(); if (count > 0) { attr.extended = new string[count * 2]; for (int i = 0; i < count; i++) { attr.extended[i * 2] = Util.Byte2str(buf.GetString()); attr.extended[i * 2 + 1] = Util.Byte2str(buf.GetString()); } } } return(attr); }
internal virtual void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); }
/// <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="System.Exception"></exception> public override bool Next(Buffer _buf) { int i; int j; switch (state) { case SSH_MSG_KEXDH_REPLY: { // The server responds with: // byte SSH_MSG_KEXDH_REPLY(31) // 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 != 31) { System.Console.Error.WriteLine("type: must be 31 " + 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 // 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.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; 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; byte[] p; byte[] g; 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; 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="System.Exception"></exception> private KeyExchange Receive_kexinit(Buffer buf) { int j = buf.GetInt(); if (j != buf.GetLength()) { // packet was compressed and buf.GetByte(); // j is the size of deflated packet. I_S = new byte[buf.index - 5]; } else { I_S = new byte[j - 1 - buf.GetByte()]; } System.Array.Copy(buf.buffer, buf.s, I_S, 0, I_S.Length); if (!in_kex) { // We are in rekeying activated by the remote! Send_kexinit(); } guess = KeyExchange.Guess(I_S, I_C); if (guess == null) { throw new JSchException("Algorithm negotiation fail"); } if (!isAuthed && (guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS].Equals("none") || (guess [KeyExchange.PROPOSAL_ENC_ALGS_STOC].Equals("none")))) { throw new JSchException("NONE Cipher should not be chosen before authentification is successed." ); } KeyExchange kex = null; try { Type c = Sharpen.Runtime.GetType(GetConfig(guess[KeyExchange.PROPOSAL_KEX_ALGS])); kex = (KeyExchange)(System.Activator.CreateInstance(c)); } catch (Exception e) { throw new JSchException(e.ToString(), e); } kex.Init(this, V_S, V_C, I_S, I_C); return kex; }
internal override bool Parse(byte[] plain) { try { if (vendor == VENDOR_FSECURE) { if (plain[0] != unchecked ((int)(0x30))) { // FSecure Buffer buf = new Buffer(plain); buf.GetInt(); P_array = buf.GetMPIntBits(); G_array = buf.GetMPIntBits(); Q_array = buf.GetMPIntBits(); pub_array = buf.GetMPIntBits(); prv_array = buf.GetMPIntBits(); return(true); } return(false); } int index = 0; int length = 0; if (plain[index] != unchecked ((int)(0x30))) { return(false); } index++; // SEQUENCE length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } if (plain[index] != unchecked ((int)(0x02))) { return(false); } index++; // INTEGER length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } P_array = new byte[length]; System.Array.Copy(plain, index, P_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } Q_array = new byte[length]; System.Array.Copy(plain, index, Q_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } G_array = new byte[length]; System.Array.Copy(plain, index, G_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } pub_array = new byte[length]; System.Array.Copy(plain, index, pub_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } prv_array = new byte[length]; System.Array.Copy(plain, index, prv_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); //e.printStackTrace(); return(false); } return(true); }
/// <exception cref="System.IO.IOException"></exception> internal override void Write(byte[] foo, int s, int l) { if (packet == null) { wbuf = new Buffer(rmpsize); packet = new Packet(wbuf); } rbuf.Shift(); if (rbuf.buffer.Length < rbuf.index + l) { byte[] newbuf = new byte[rbuf.s + l]; System.Array.Copy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.Length); rbuf.buffer = newbuf; } rbuf.PutByte(foo, s, l); int mlen = rbuf.GetInt(); if (mlen > rbuf.GetLength()) { rbuf.s -= 4; return; } int typ = rbuf.GetByte(); Session _session = null; try { _session = GetSession(); } catch (JSchException e) { throw new IOException(e.ToString()); } IdentityRepository irepo = _session.jsch.GetIdentityRepository(); UserInfo userinfo = _session.GetUserInfo(); mbuf.Reset(); if (typ == SSH2_AGENTC_REQUEST_IDENTITIES) { mbuf.PutByte(SSH2_AGENT_IDENTITIES_ANSWER); ArrayList identities = irepo.GetIdentities(); lock (identities) { int count = 0; for (int i = 0; i < identities.Count; i++) { Identity identity = (Identity)(identities[i]); if (identity.GetPublicKeyBlob() != null) { count++; } } mbuf.PutInt(count); for (int i_1 = 0; i_1 < identities.Count; i_1++) { Identity identity = (Identity)(identities[i_1]); byte[] pubkeyblob = identity.GetPublicKeyBlob(); if (pubkeyblob == null) { continue; } mbuf.PutString(pubkeyblob); mbuf.PutString(Util.empty); } } } else { if (typ == SSH_AGENTC_REQUEST_RSA_IDENTITIES) { mbuf.PutByte(SSH_AGENT_RSA_IDENTITIES_ANSWER); mbuf.PutInt(0); } else { if (typ == SSH2_AGENTC_SIGN_REQUEST) { byte[] blob = rbuf.GetString(); byte[] data = rbuf.GetString(); int flags = rbuf.GetInt(); // if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1 // datafellows = SSH_BUG_SIGBLOB; // } ArrayList identities = irepo.GetIdentities(); Identity identity = null; lock (identities) { for (int i = 0; i < identities.Count; i++) { Identity _identity = (Identity)(identities[i]); if (_identity.GetPublicKeyBlob() == null) { continue; } if (!Util.Array_equals(blob, _identity.GetPublicKeyBlob())) { continue; } if (_identity.IsEncrypted()) { if (userinfo == null) { continue; } while (_identity.IsEncrypted()) { if (!userinfo.PromptPassphrase("Passphrase for " + _identity.GetName())) { break; } string _passphrase = userinfo.GetPassphrase(); if (_passphrase == null) { break; } byte[] passphrase = Util.Str2byte(_passphrase); try { if (_identity.SetPassphrase(passphrase)) { break; } } catch (JSchException) { break; } } } if (!_identity.IsEncrypted()) { identity = _identity; break; } } } byte[] signature = null; if (identity != null) { signature = identity.GetSignature(data); } if (signature == null) { mbuf.PutByte(SSH2_AGENT_FAILURE); } else { mbuf.PutByte(SSH2_AGENT_SIGN_RESPONSE); mbuf.PutString(signature); } } else { if (typ == SSH2_AGENTC_REMOVE_IDENTITY) { byte[] blob = rbuf.GetString(); irepo.Remove(blob); mbuf.PutByte(SSH_AGENT_SUCCESS); } else { if (typ == SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES) { mbuf.PutByte(SSH_AGENT_SUCCESS); } else { if (typ == SSH2_AGENTC_REMOVE_ALL_IDENTITIES) { irepo.RemoveAll(); mbuf.PutByte(SSH_AGENT_SUCCESS); } else { if (typ == SSH2_AGENTC_ADD_IDENTITY) { int fooo = rbuf.GetLength(); byte[] tmp = new byte[fooo]; rbuf.GetByte(tmp); bool result = irepo.Add(tmp); mbuf.PutByte(result ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); } else { rbuf.Skip(rbuf.GetLength() - 1); mbuf.PutByte(SSH_AGENT_FAILURE); } } } } } } } byte[] response = new byte[mbuf.GetLength()]; mbuf.GetByte(response); Send(response); }
internal virtual bool Decrypt_rsa() { byte[] p_array; byte[] q_array; byte[] dmp1_array; byte[] dmq1_array; byte[] iqmp_array; try { byte[] plain; if (encrypted) { if (keytype == OPENSSH) { cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { if (keytype == FSECURE) { for (int i = 0; i < iv.Length; i++) { iv[i] = 0; } cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { return false; } } } else { if (n_array != null) { return true; } plain = encoded_data; } if (keytype == FSECURE) { // FSecure Buffer buf = new Buffer(plain); int foo = buf.GetInt(); if (plain.Length != foo + 4) { return false; } e_array = buf.GetMPIntBits(); d_array = buf.GetMPIntBits(); n_array = buf.GetMPIntBits(); byte[] u_array = buf.GetMPIntBits(); p_array = buf.GetMPIntBits(); q_array = buf.GetMPIntBits(); return true; } int index = 0; int length = 0; if (plain[index] != unchecked((int)(0x30))) { return false; } index++; // SEQUENCE length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } if (plain[index] != unchecked((int)(0x02))) { return false; } index++; // INTEGER length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } index += length; //System.err.println("int: len="+length); //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":"); //System.err.println(""); index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } n_array = new byte[length]; System.Array.Copy(plain, index, n_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } e_array = new byte[length]; System.Array.Copy(plain, index, e_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } d_array = new byte[length]; System.Array.Copy(plain, index, d_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } p_array = new byte[length]; System.Array.Copy(plain, index, p_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } q_array = new byte[length]; System.Array.Copy(plain, index, q_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } dmp1_array = new byte[length]; System.Array.Copy(plain, index, dmp1_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } dmq1_array = new byte[length]; System.Array.Copy(plain, index, dmq1_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } iqmp_array = new byte[length]; System.Array.Copy(plain, index, iqmp_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); return false; } return true; }
/// <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 override void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); byte[] addr = buf.GetString(); int port = buf.GetInt(); byte[] orgaddr = buf.GetString(); int orgport = buf.GetInt(); Session _session = null; try { _session = GetSession(); } catch (JSchException) { } // session has been already down. lock (pool) { for (int i = 0; i < pool.Count; i++) { object[] foo = (object[])(pool[i]); if (foo[0] != _session) { continue; } if (((int)foo[1]) != port) { continue; } this.rport = port; this.target = (string)foo[2]; if (foo[3] == null || (foo[3] is object[])) { this.lport = -1; } else { this.lport = ((int)foo[3]); } if (foo.Length >= 6) { this.factory = ((SocketFactory)foo[5]); } break; } if (target == null) { } } }
internal virtual void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); }
internal static NSch.SftpATTRS GetATTR(Buffer buf) { NSch.SftpATTRS attr = new NSch.SftpATTRS(); attr.flags = buf.GetInt(); if ((attr.flags & SSH_FILEXFER_ATTR_SIZE) != 0) { attr.size = buf.GetLong(); } if ((attr.flags & SSH_FILEXFER_ATTR_UIDGID) != 0) { attr.uid = buf.GetInt(); attr.gid = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { attr.permissions = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { attr.atime = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) { attr.mtime = buf.GetInt(); } if ((attr.flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) { int count = buf.GetInt(); if (count > 0) { attr.extended = new string[count * 2]; for (int i = 0; i < count; i++) { attr.extended[i * 2] = Util.Byte2str(buf.GetString()); attr.extended[i * 2 + 1] = Util.Byte2str(buf.GetString()); } } } return attr; }
/// <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="System.IO.IOException"></exception> private ChannelHeader Header(Buffer buf, ChannelHeader header) { buf.Rewind(); int i = Fill(buf.buffer, 0, 9); header.length = buf.GetInt() - 5; header.type = buf.GetByte() & unchecked((int)(0xff)); header.rid = buf.GetInt(); return header; }
internal virtual bool Decrypt_dss() { try { byte[] plain; if (encrypted) { if (keytype == OPENSSH) { cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { if (keytype == FSECURE) { for (int i = 0; i < iv.Length; i++) { iv[i] = 0; } cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { return false; } } } else { if (P_array != null) { return true; } plain = encoded_data; } if (keytype == FSECURE) { // FSecure Buffer buf = new Buffer(plain); int foo = buf.GetInt(); if (plain.Length != foo + 4) { return false; } P_array = buf.GetMPIntBits(); G_array = buf.GetMPIntBits(); Q_array = buf.GetMPIntBits(); pub_array = buf.GetMPIntBits(); prv_array = buf.GetMPIntBits(); return true; } int index = 0; int length = 0; if (plain[index] != unchecked((int)(0x30))) { return false; } index++; // SEQUENCE length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } if (plain[index] != unchecked((int)(0x02))) { return false; } index++; // INTEGER length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } P_array = new byte[length]; System.Array.Copy(plain, index, P_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } Q_array = new byte[length]; System.Array.Copy(plain, index, Q_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } G_array = new byte[length]; System.Array.Copy(plain, index, G_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } pub_array = new byte[length]; System.Array.Copy(plain, index, pub_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } prv_array = new byte[length]; System.Array.Copy(plain, index, prv_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); //e.printStackTrace(); return false; } return true; }
/// <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; }
internal virtual bool Decrypt_dss() { try { byte[] plain; if (encrypted) { if (keytype == OPENSSH) { cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { if (keytype == FSECURE) { for (int i = 0; i < iv.Length; i++) { iv[i] = 0; } cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { return(false); } } } else { if (P_array != null) { return(true); } plain = encoded_data; } if (keytype == FSECURE) { // FSecure Buffer buf = new Buffer(plain); int foo = buf.GetInt(); if (plain.Length != foo + 4) { return(false); } P_array = buf.GetMPIntBits(); G_array = buf.GetMPIntBits(); Q_array = buf.GetMPIntBits(); pub_array = buf.GetMPIntBits(); prv_array = buf.GetMPIntBits(); return(true); } int index = 0; int length = 0; if (plain[index] != unchecked ((int)(0x30))) { return(false); } index++; // SEQUENCE length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } if (plain[index] != unchecked ((int)(0x02))) { return(false); } index++; // INTEGER length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } P_array = new byte[length]; System.Array.Copy(plain, index, P_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } Q_array = new byte[length]; System.Array.Copy(plain, index, Q_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } G_array = new byte[length]; System.Array.Copy(plain, index, G_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } pub_array = new byte[length]; System.Array.Copy(plain, index, pub_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } prv_array = new byte[length]; System.Array.Copy(plain, index, prv_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); //e.printStackTrace(); return(false); } return(true); }
/// <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); }
/// <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 virtual 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; int stimeout = 0; try { while (isConnected && thread != null) { try { buf = Read(buf); stimeout = 0; } catch (ThreadInterruptedException ee) { if (!in_kex && stimeout < serverAliveCountMax) { SendKeepAliveMsg(); stimeout++; continue; } throw; } int msgType = buf.GetCommand() & unchecked((int)(0xff)); if (kex != null && kex.GetState() == msgType) { kex_start_time = Runtime.CurrentTimeMillis(); bool result = kex.Next(buf); if (!result) { throw new JSchException("verify: " + result); } continue; } switch (msgType) { case SSH_MSG_KEXINIT: { //System.err.println("KEXINIT"); kex = Receive_kexinit(buf); break; } case SSH_MSG_NEWKEYS: { //System.err.println("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; } if (length[0] == 0) { break; } try { channel.Write(foo, start[0], length[0]); } catch (Exception) { //System.err.println(e); try { channel.Disconnect(); } catch (Exception) { } break; } int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((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.err.println("stderr: "+new String(foo,start[0],length[0])); if (channel == null) { break; } if (length[0] == 0) { break; } channel.Write_ext(foo, start[0], length[0]); int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((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(); } 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(); } break; } case SSH_MSG_CHANNEL_OPEN_CONFIRMATION: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { } //break; int r = buf.GetInt(); long rws = buf.GetUInt(); int rps = buf.GetInt(); channel.SetRemoteWindowSize(rws); channel.SetRemotePacketSize(rps); channel.SetRecipient(r); 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; channel.eof_remote = true; channel.SetRecipient(0); break; } case SSH_MSG_CHANNEL_REQUEST: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); foo = buf.GetString(); bool reply = (buf.GetByte() != 0); channel = Channel.GetChannel(i, this); if (channel != null) { byte reply_type = unchecked((byte)SSH_MSG_CHANNEL_FAILURE); if ((Util.Byte2str(foo)).Equals("exit-status")) { i = buf.GetInt(); // exit-status channel.SetExitStatus(i); reply_type = unchecked((byte)SSH_MSG_CHANNEL_SUCCESS); } if (reply) { packet.Reset(); buf.PutByte(reply_type); buf.PutInt(channel.GetRecipient()); Write(packet); } } break; } case SSH_MSG_CHANNEL_OPEN: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); string ctyp = Util.Byte2str(foo); if (!"forwarded-tcpip".Equals(ctyp) && !("x11".Equals(ctyp) && x11_forwarding) && !("*****@*****.**".Equals(ctyp) && agent_forwarding)) { //System.err.println("Session.run: CHANNEL OPEN "+ctyp); //throw new IOException("Session.run: CHANNEL OPEN "+ctyp); packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE)); buf.PutInt(buf.GetInt()); buf.PutInt(Channel.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); buf.PutString(Util.empty); buf.PutString(Util.empty); Write(packet); } else { channel = Channel.GetChannel(ctyp); AddChannel(channel); channel.GetData(buf); channel.Init(); Sharpen.Thread tmp = new Sharpen.Thread(channel); tmp.SetName("Channel " + ctyp + " " + host); if (daemon_thread) { tmp.SetDaemon(daemon_thread); } tmp.Start(); break; } goto case SSH_MSG_CHANNEL_SUCCESS; } case SSH_MSG_CHANNEL_SUCCESS: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 1; break; } case SSH_MSG_CHANNEL_FAILURE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 0; break; } case SSH_MSG_GLOBAL_REQUEST: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); // request name bool reply = (buf.GetByte() != 0); if (reply) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_REQUEST_FAILURE)); Write(packet); } break; } case SSH_MSG_REQUEST_FAILURE: case SSH_MSG_REQUEST_SUCCESS: { Sharpen.Thread t = grr.GetThread(); if (t != null) { grr.SetReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0); t.Interrupt(); } break; } default: { //System.err.println("Session.run: unsupported type "+msgType); throw new IOException("Unknown SSH message type " + msgType); } } } } catch (Exception e) { in_kex = false; if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Caught an exception, leaving main loop due to " + e.Message); } } //System.err.println("# Session.run"); //e.printStackTrace(); try { Disconnect(); } catch (ArgumentNullException) { } catch (Exception) { } //System.err.println("@1"); //e.printStackTrace(); //System.err.println("@2"); //e.printStackTrace(); isConnected = false; }
internal virtual bool Decrypt_rsa() { byte[] p_array; byte[] q_array; byte[] dmp1_array; byte[] dmq1_array; byte[] iqmp_array; try { byte[] plain; if (encrypted) { if (keytype == OPENSSH) { cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { if (keytype == FSECURE) { for (int i = 0; i < iv.Length; i++) { iv[i] = 0; } cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv); plain = new byte[encoded_data.Length]; cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0); } else { return(false); } } } else { if (n_array != null) { return(true); } plain = encoded_data; } if (keytype == FSECURE) { // FSecure Buffer buf = new Buffer(plain); int foo = buf.GetInt(); if (plain.Length != foo + 4) { return(false); } e_array = buf.GetMPIntBits(); d_array = buf.GetMPIntBits(); n_array = buf.GetMPIntBits(); byte[] u_array = buf.GetMPIntBits(); p_array = buf.GetMPIntBits(); q_array = buf.GetMPIntBits(); return(true); } int index = 0; int length = 0; if (plain[index] != unchecked ((int)(0x30))) { return(false); } index++; // SEQUENCE length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } if (plain[index] != unchecked ((int)(0x02))) { return(false); } index++; // INTEGER length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } index += length; //System.err.println("int: len="+length); //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":"); //System.err.println(""); index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } n_array = new byte[length]; System.Array.Copy(plain, index, n_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } e_array = new byte[length]; System.Array.Copy(plain, index, e_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } d_array = new byte[length]; System.Array.Copy(plain, index, d_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } p_array = new byte[length]; System.Array.Copy(plain, index, p_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } q_array = new byte[length]; System.Array.Copy(plain, index, q_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } dmp1_array = new byte[length]; System.Array.Copy(plain, index, dmp1_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } dmq1_array = new byte[length]; System.Array.Copy(plain, index, dmq1_array, 0, length); index += length; index++; length = plain[index++] & unchecked ((int)(0xff)); if ((length & unchecked ((int)(0x80))) != 0) { int foo = length & unchecked ((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked ((int)(0xff))); } } iqmp_array = new byte[length]; System.Array.Copy(plain, index, iqmp_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); return(false); } return(true); }
/// <exception cref="System.Exception"></exception> public virtual Buffer Read(Buffer buf) { int j = 0; while (true) { buf.Reset(); io.GetByte(buf.buffer, buf.index, s2ccipher_size); buf.index += s2ccipher_size; if (s2ccipher != null) { s2ccipher.Update(buf.buffer, 0, s2ccipher_size, buf.buffer, 0); } j = ((buf.buffer[0] << 24) & unchecked((int)(0xff000000))) | ((buf.buffer[1] << 16 ) & unchecked((int)(0x00ff0000))) | ((buf.buffer[2] << 8) & unchecked((int)(0x0000ff00 ))) | ((buf.buffer[3]) & unchecked((int)(0x000000ff))); // RFC 4253 6.1. Maximum Packet Length if (j < 5 || j > PACKET_MAX_SIZE) { Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE); } int need = j + 4 - s2ccipher_size; //if(need<0){ // throw new IOException("invalid data"); //} if ((buf.index + need) > buf.buffer.Length) { byte[] foo = new byte[buf.index + need]; System.Array.Copy(buf.buffer, 0, foo, 0, buf.index); buf.buffer = foo; } if ((need % s2ccipher_size) != 0) { string message = "Bad packet length " + need; if (JSch.GetLogger().IsEnabled(Logger.FATAL)) { JSch.GetLogger().Log(Logger.FATAL, message); } Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE - s2ccipher_size); } if (need > 0) { io.GetByte(buf.buffer, buf.index, need); buf.index += (need); if (s2ccipher != null) { s2ccipher.Update(buf.buffer, s2ccipher_size, need, buf.buffer, s2ccipher_size); } } if (s2cmac != null) { s2cmac.Update(seqi); s2cmac.Update(buf.buffer, 0, buf.index); s2cmac.DoFinal(s2cmac_result1, 0); io.GetByte(s2cmac_result2, 0, s2cmac_result2.Length); if (!Arrays.Equals(s2cmac_result1, s2cmac_result2)) { if (need > PACKET_MAX_SIZE) { throw new IOException("MAC Error"); } Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE - need); continue; } } seqi++; if (inflater != null) { //inflater.uncompress(buf); int pad = buf.buffer[4]; uncompress_len[0] = buf.index - 5 - pad; byte[] foo = inflater.Uncompress(buf.buffer, 5, uncompress_len); if (foo != null) { buf.buffer = foo; buf.index = 5 + uncompress_len[0]; } else { System.Console.Error.WriteLine("fail in inflater"); break; } } int type = buf.GetCommand() & unchecked((int)(0xff)); //System.err.println("read: "+type); if (type == SSH_MSG_DISCONNECT) { buf.Rewind(); buf.GetInt(); buf.GetShort(); int reason_code = buf.GetInt(); byte[] description = buf.GetString(); byte[] language_tag = buf.GetString(); throw new JSchException("SSH_MSG_DISCONNECT: " + reason_code + " " + Util.Byte2str (description) + " " + Util.Byte2str(language_tag)); } else { //break; if (type == SSH_MSG_IGNORE) { } else { if (type == SSH_MSG_UNIMPLEMENTED) { buf.Rewind(); buf.GetInt(); buf.GetShort(); int reason_id = buf.GetInt(); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Received SSH_MSG_UNIMPLEMENTED for " + reason_id ); } } else { if (type == SSH_MSG_DEBUG) { buf.Rewind(); buf.GetInt(); buf.GetShort(); } else { if (type == SSH_MSG_CHANNEL_WINDOW_ADJUST) { buf.Rewind(); buf.GetInt(); buf.GetShort(); Channel c = Channel.GetChannel(buf.GetInt(), this); if (c == null) { } else { c.AddRemoteWindowSize(buf.GetInt()); } } else { if (type == UserAuth.SSH_MSG_USERAUTH_SUCCESS) { isAuthed = true; if (inflater == null && deflater == null) { string method; method = guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]; InitDeflater(method); method = guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]; InitInflater(method); } break; } else { break; } } } } } } } buf.Rewind(); return buf; }
internal override bool Parse(byte[] plain) { try { if (vendor == VENDOR_FSECURE) { if (plain[0] != unchecked((int)(0x30))) { // FSecure Buffer buf = new Buffer(plain); buf.GetInt(); P_array = buf.GetMPIntBits(); G_array = buf.GetMPIntBits(); Q_array = buf.GetMPIntBits(); pub_array = buf.GetMPIntBits(); prv_array = buf.GetMPIntBits(); return true; } return false; } int index = 0; int length = 0; if (plain[index] != unchecked((int)(0x30))) { return false; } index++; // SEQUENCE length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } if (plain[index] != unchecked((int)(0x02))) { return false; } index++; // INTEGER length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } P_array = new byte[length]; System.Array.Copy(plain, index, P_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } Q_array = new byte[length]; System.Array.Copy(plain, index, Q_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } G_array = new byte[length]; System.Array.Copy(plain, index, G_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } pub_array = new byte[length]; System.Array.Copy(plain, index, pub_array, 0, length); index += length; index++; length = plain[index++] & unchecked((int)(0xff)); if ((length & unchecked((int)(0x80))) != 0) { int foo = length & unchecked((int)(0x7f)); length = 0; while (foo-- > 0) { length = (length << 8) + (plain[index++] & unchecked((int)(0xff))); } } prv_array = new byte[length]; System.Array.Copy(plain, index, prv_array, 0, length); index += length; } catch (Exception) { //System.err.println(e); //e.printStackTrace(); return false; } return true; }