Example #1
0
        /// <exception cref="NSch.JSchException"></exception>
        internal virtual void SetKnownHosts(InputStream foo)
        {
            pool.Clear();
            StringBuilder sb = new StringBuilder();
            byte          i;
            int           j;
            bool          error = false;

            try
            {
                InputStream fis = foo;
                string      host;
                string      key = null;
                int         type;
                byte[]      buf  = new byte[1024];
                int         bufl = 0;
                while (true)
                {
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            if (bufl == 0)
                            {
                                goto loop_break;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (j == unchecked ((int)(0x0d)))
                        {
                            continue;
                        }
                        if (j == unchecked ((int)(0x0a)))
                        {
                            break;
                        }
                        if (buf.Length <= bufl)
                        {
                            if (bufl > 1024 * 10)
                            {
                                break;
                            }
                            // too long...
                            byte[] newbuf = new byte[buf.Length * 2];
                            System.Array.Copy(buf, 0, newbuf, 0, buf.Length);
                            buf = newbuf;
                        }
                        buf[bufl++] = unchecked ((byte)j);
                    }
                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++;
                            continue;
                        }
                        if (i == '#')
                        {
                            AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                            goto loop_continue;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x20)) || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    type      = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x20)) || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    if (sb.ToString().Equals("ssh-dss"))
                    {
                        type = HostKey.SSHDSS;
                    }
                    else
                    {
                        if (sb.ToString().Equals("ssh-rsa"))
                        {
                            type = HostKey.SSHRSA;
                        }
                        else
                        {
                            j = bufl;
                        }
                    }
                    if (j >= bufl)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x0d)))
                        {
                            continue;
                        }
                        if (i == unchecked ((int)(0x0a)))
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    //System.err.println(host);
                    //System.err.println("|"+key+"|");
                    HostKey hk = null;
                    hk = new KnownHosts.HashedHostKey(this, host, type, Util.FromBase64(Util.Str2byte
                                                                                            (key), 0, key.Length));
                    pool.Add(hk);
                    loop_continue :;
                }
                loop_break :;
                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            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());
            }
        }
Example #2
0
		/// <exception cref="NSch.JSchException"></exception>
		internal virtual void SetKnownHosts(InputStream foo)
		{
			pool.Clear();
			StringBuilder sb = new StringBuilder();
			byte i;
			int j;
			bool error = false;
			try
			{
				InputStream fis = foo;
				string host;
				string key = null;
				int type;
				byte[] buf = new byte[1024];
				int bufl = 0;
				while (true)
				{
					bufl = 0;
					while (true)
					{
						j = fis.Read();
						if (j == -1)
						{
							if (bufl == 0)
							{
								goto loop_break;
							}
							else
							{
								break;
							}
						}
						if (j == unchecked((int)(0x0d)))
						{
							continue;
						}
						if (j == unchecked((int)(0x0a)))
						{
							break;
						}
						if (buf.Length <= bufl)
						{
							if (bufl > 1024 * 10)
							{
								break;
							}
							// too long...
							byte[] newbuf = new byte[buf.Length * 2];
							System.Array.Copy(buf, 0, newbuf, 0, buf.Length);
							buf = newbuf;
						}
						buf[bufl++] = unchecked((byte)j);
					}
					j = 0;
					while (j < bufl)
					{
						i = buf[j];
						if (i == ' ' || i == '\t')
						{
							j++;
							continue;
						}
						if (i == '#')
						{
							AddInvalidLine(Util.Byte2str(buf, 0, bufl));
							goto loop_continue;
						}
						break;
					}
					if (j >= bufl)
					{
						AddInvalidLine(Util.Byte2str(buf, 0, bufl));
						goto loop_continue;
					}
					sb.Length = 0;
					while (j < bufl)
					{
						i = buf[j++];
						if (i == unchecked((int)(0x20)) || i == '\t')
						{
							break;
						}
						sb.Append((char)i);
					}
					host = sb.ToString();
					if (j >= bufl || host.Length == 0)
					{
						AddInvalidLine(Util.Byte2str(buf, 0, bufl));
						goto loop_continue;
					}
					sb.Length = 0;
					type = -1;
					while (j < bufl)
					{
						i = buf[j++];
						if (i == unchecked((int)(0x20)) || i == '\t')
						{
							break;
						}
						sb.Append((char)i);
					}
					if (sb.ToString().Equals("ssh-dss"))
					{
						type = HostKey.SSHDSS;
					}
					else
					{
						if (sb.ToString().Equals("ssh-rsa"))
						{
							type = HostKey.SSHRSA;
						}
						else
						{
							j = bufl;
						}
					}
					if (j >= bufl)
					{
						AddInvalidLine(Util.Byte2str(buf, 0, bufl));
						goto loop_continue;
					}
					sb.Length = 0;
					while (j < bufl)
					{
						i = buf[j++];
						if (i == unchecked((int)(0x0d)))
						{
							continue;
						}
						if (i == unchecked((int)(0x0a)))
						{
							break;
						}
						sb.Append((char)i);
					}
					key = sb.ToString();
					if (key.Length == 0)
					{
						AddInvalidLine(Util.Byte2str(buf, 0, bufl));
						goto loop_continue;
					}
					//System.err.println(host);
					//System.err.println("|"+key+"|");
					HostKey hk = null;
					hk = new KnownHosts.HashedHostKey(this, host, type, Util.FromBase64(Util.Str2byte
						(key), 0, key.Length));
					pool.Add(hk);
loop_continue: ;
				}
loop_break: ;
				fis.Close();
				if (error)
				{
					throw new JSchException("KnownHosts: invalid format");
				}
			}
			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());
			}
		}
Example #3
0
 /// <exception cref="NSch.JSchException"></exception>
 internal virtual HostKey CreateHashedHostKey(string host, byte[] key)
 {
     KnownHosts.HashedHostKey hhk = new KnownHosts.HashedHostKey(this, host, key);
     hhk.Hash();
     return(hhk);
 }
Example #4
0
		/// <exception cref="NSch.JSchException"></exception>
		internal virtual HostKey CreateHashedHostKey(string host, byte[] key)
		{
			KnownHosts.HashedHostKey hhk = new KnownHosts.HashedHostKey(this, host, key);
			hhk.Hash();
			return hhk;
		}