Esempio n. 1
0
        internal HostKey createHashedHostKey(string host, byte[] key)
        {
            HashedHostKey hhk = new HashedHostKey(this, host, key);

            hhk.Hash();
            return(hhk);
        }
Esempio n. 2
0
        internal void setKnownHosts(Stream foo)
        {
            pool.Clear();
            StringBuilder sb = new StringBuilder();
            byte i;
            int j;
            bool error = false;
            try
            {
                Stream fis = foo;
                string host;
                string key = null;
                int type;
                byte[] buf = new byte[1024];
                int bufl = 0;

                while (true)
                {
                loop:
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            if (bufl == 0) { goto outloop; }
                            else { break; }
                        }
                        if (j == 0x0d) { continue; }
                        if (j == 0x0a) { break; }
                        if (buf.Length <= bufl)
                        {
                            if (bufl > 1024 * 10) break;   // too long...
                            byte[] newbuf = new byte[buf.Length * 2];
                            Array.Copy(buf, 0, newbuf, 0, buf.Length);
                            buf = newbuf;
                        }
                        buf[bufl++] = (byte)j;
                    }

                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t') { j++; continue; }
                        if (i == '#')
                        {
                            addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                            goto loop;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length=0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x20 || i == '\t') { break; }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length=0;
                    type = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 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(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length=0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x0d) { continue; }
                        if (i == 0x0a) { break; }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    //Console.Error.WriteLine(host);
                    //Console.Error.WriteLine("|"+key+"|");

                    HostKey hk = null;
                    hk = new HashedHostKey(this, host, type,
                                           Util.fromBase64(key.getBytes(), 0,
                                                           key.Length));
                    pool.Add(hk);
                }
            outloop:
                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                    throw (JSchException)e;
                throw new JSchException(e.Message,e);
            }
        }
Esempio n. 3
0
        internal void setKnownHosts(Stream foo)
        {
            pool.Clear();
            StringBuilder sb = new StringBuilder();
            byte          i;
            int           j;
            bool          error = false;

            try
            {
                Stream fis = foo;
                string host;
                string key = null;
                int    type;
                byte[] buf  = new byte[1024];
                int    bufl = 0;

                while (true)
                {
loop:
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            if (bufl == 0)
                            {
                                goto outloop;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (j == 0x0d)
                        {
                            continue;
                        }
                        if (j == 0x0a)
                        {
                            break;
                        }
                        if (buf.Length <= bufl)
                        {
                            if (bufl > 1024 * 10)
                            {
                                break;                     // too long...
                            }
                            byte[] newbuf = new byte[buf.Length * 2];
                            Array.Copy(buf, 0, newbuf, 0, buf.Length);
                            buf = newbuf;
                        }
                        buf[bufl++] = (byte)j;
                    }

                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++; continue;
                        }
                        if (i == '#')
                        {
                            addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                            goto loop;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x20 || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    type      = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 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(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x0d)
                        {
                            continue;
                        }
                        if (i == 0x0a)
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    //Console.Error.WriteLine(host);
                    //Console.Error.WriteLine("|"+key+"|");

                    HostKey hk = null;
                    hk = new HashedHostKey(this, host, type,
                                           Util.fromBase64(key.getBytes(), 0,
                                                           key.Length));
                    pool.Add(hk);
                }
outloop:
                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.Message, e);
            }
        }
Esempio n. 4
0
 internal HostKey createHashedHostKey(string host, byte[] key)
 {
     HashedHostKey hhk = new HashedHostKey(this,host, key);
     hhk.Hash();
     return hhk;
 }