Beispiel #1
0
 public static string getFingerPrint(HASH hash, byte[] data)
 {
     try
     {
         hash.init();
         hash.update(data, 0, data.Length);
         byte[]        foo = hash.digest();
         StringBuilder sb  = new StringBuilder();
         int           bar;
         for (int i = 0; i < foo.Length; i++)
         {
             bar = foo[i] & 0xff;
             sb.Append(chars[(((uint)bar) >> 4) & 0xf]);
             sb.Append(chars[(bar) & 0xf]);
             if (i + 1 < foo.Length)
             {
                 sb.Append(":");
             }
         }
         return(sb.ToString());
     }
     catch //(Exception e)
     {
         return("???");
     }
 }
Beispiel #2
0
 private HASH genHash()
 {
     try
     {
         Type c = Type.GetType(JSch.getConfig("md5"));
         hash = (HASH)(c.newInstance());
         hash.init();
     }
     catch //(Exception e)
     {
     }
     return(hash);
 }
Beispiel #3
0
 private HASH genHash()
 {
     try
     {
         Type c = Type.GetType(JSch.getConfig("md5"));
         hash = (HASH)(c.newInstance());
         hash.init();
     }
     catch //(Exception e)
     {
     }
     return hash;
 }
        private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch)
        {
            this.identity = name;
            this.jsch     = jsch;
            try
            {
                Type c;
                c      = Type.GetType((string)JSch.getConfig("3des-cbc"));
                cipher = (Cipher)(c.newInstance());
                key    = new byte[cipher.getBlockSize()]; // 24
                iv     = new byte[cipher.getIVSize()];    // 8
                c      = Type.GetType((string)JSch.getConfig("md5"));
                hash   = (HASH)(c.newInstance());
                hash.init();

                byte[] buf = prvkey;
                int    len = buf.Length;

                int i = 0;
                while (i < len)
                {
                    if (buf[i] == 'B' && 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
                        {
                            //Console.Error.WriteLine("invalid format: "+identity);
                            throw new JSchException("invalid privatekey: " + identity);
                        }
                        i += 3;
                        continue;
                    }
                    if (buf[i] == 'A' && 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      = Type.GetType((string)JSch.getConfig("aes256-cbc"));
                            cipher = (Cipher)(c.newInstance());
                            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' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',')
                    {
                        i += 4;
                        for (int ii = 0; ii < iv.Length; ii++)
                        {
                            iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) +
                                            (a2b(buf[i++]) & 0xf));
                        }
                        continue;
                    }
                    if (buf[i] == 0x0d &&
                        i + 1 < buf.Length && buf[i + 1] == 0x0a)
                    {
                        i++;
                        continue;
                    }
                    if (buf[i] == 0x0a && i + 1 < buf.Length)
                    {
                        if (buf[i + 1] == 0x0a)
                        {
                            i += 2; break;
                        }
                        if (buf[i + 1] == 0x0d &&
                            i + 2 < buf.Length && buf[i + 2] == 0x0a)
                        {
                            i += 3; break;
                        }
                        bool inheader = false;
                        for (int j = i + 1; j < buf.Length; j++)
                        {
                            if (buf[j] == 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] == 0x0a)
                    {
                        bool xd = (buf[i - 1] == 0x0d);
                        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 &&            // FSecure
                    encoded_data[0] == (byte)0x3f &&
                    encoded_data[1] == (byte)0x6f &&
                    encoded_data[2] == (byte)0xf9 &&
                    encoded_data[3] == (byte)0xeb)
                {
                    Buffer _buf = new Buffer(encoded_data);
                    _buf.getInt();  // 0x3f6ff9be
                    _buf.getInt();
                    byte[] _type = _buf.getString();
                    //Console.Error.WriteLine("type: "+Encoding.UTF8.GetString(_type));
                    byte[] _cipher = _buf.getString();
                    string scipher = Encoding.UTF8.GetString(_cipher);
                    //Console.Error.WriteLine("cipher: "+cipher);
                    if (scipher.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 (scipher.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 &&             // FSecure's public key
                    buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-')
                {
                    i = 0;
                    do
                    {
                        i++;
                    } while (len > i && buf[i] != 0x0a);
                    if (len <= i)
                    {
                        return;
                    }
                    while (i < len)
                    {
                        if (buf[i] == 0x0a)
                        {
                            bool inheader = false;
                            for (int j = i + 1; j < len; j++)
                            {
                                if (buf[j] == 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] == 0x0a)
                        {
                            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)
            {
                //Console.Error.WriteLine("IdentityFile: "+e);
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.Message, e);
            }
        }
Beispiel #5
0
 public static string getFingerPrint(HASH hash, byte[] data)
 {
     try
     {
         hash.init();
         hash.update(data, 0, data.Length);
         byte[] foo = hash.digest();
         StringBuilder sb = new StringBuilder();
         int bar;
         for (int i = 0; i < foo.Length; i++)
         {
             bar = foo[i] & 0xff;
             sb.Append(chars[(((uint)bar) >> 4) & 0xf]);
             sb.Append(chars[(bar) & 0xf]);
             if (i + 1 < foo.Length)
                 sb.Append(":");
         }
         return sb.ToString();
     }
     catch //(Exception e)
     {
         return "???";
     }
 }