Ejemplo n.º 1
0
        public override byte[] getPublicKeyBlob()
        {
            byte[] foo = base.getPublicKeyBlob();
            if (foo != null)
            {
                return(foo);
            }

            if (P_array == null)
            {
                return(null);
            }

            LibSterileSSH.SecureShell.Buffer buf = new LibSterileSSH.SecureShell.Buffer(sshdss.Length + 4 +
                                                                                        P_array.Length + 4 +
                                                                                        Q_array.Length + 4 +
                                                                                        G_array.Length + 4 +
                                                                                        pub_array.Length + 4);
            buf.putString(sshdss);
            buf.putString(P_array);
            buf.putString(Q_array);
            buf.putString(G_array);
            buf.putString(pub_array);
            return(buf.buffer);
        }
Ejemplo n.º 2
0
        public override void init(Session session,
                                  byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            this.session = session;
            this.V_S     = V_S;
            this.V_C     = V_C;
            this.I_S     = I_S;
            this.I_C     = I_C;

            //    sha=new SHA1();
            //    sha.init();
            try {
                Type t = Type.GetType(session.getConfig("sha-1"));
                sha = (IHASH)(Activator.CreateInstance(t));
                sha.Init();
            }
            catch (Exception ee) {
                Console.WriteLine(ee);
            }

            buf    = new LibSterileSSH.SecureShell.Buffer();
            packet = new Packet(buf);

            try {
                Type t = Type.GetType(session.getConfig("dh"));
                dh = (IDH)(Activator.CreateInstance(t));
                dh.init();
            }
            catch (Exception ee) {
                throw ee;
            }

            dh.setP(p);
            dh.setG(g);

            // The client responds with:
            // byte  SSH_MSG_KEXDH_INIT(30)
            // mpint e <- g^x mod p
            //         x is a random number (1 < x < (p-1)/2)

            e = dh.getE();

            packet.reset();
            buf.putByte((byte)SSH_MSG_KEXDH_INIT);
            buf.putMPInt(e);
            session.write(packet);

            state = SSH_MSG_KEXDH_REPLY;
        }
Ejemplo n.º 3
0
        //private byte[] f;

        public override void init(Session session,
                                  byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            this.session = session;
            this.V_S     = V_S;
            this.V_C     = V_C;
            this.I_S     = I_S;
            this.I_C     = I_C;

            //    sha=new SHA1();
            //    sha.init();

            try {
                Type t = Type.GetType(session.getConfig("sha-1"));
                sha = (IHASH)(Activator.CreateInstance(t));
                sha.Init();
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }

            buf    = new LibSterileSSH.SecureShell.Buffer();
            packet = new Packet(buf);

            try {
                Type t = Type.GetType(session.getConfig("dh"));
                dh = (IDH)(Activator.CreateInstance(t));
                dh.init();
            }
            catch (Exception e) {
                throw e;
            }

            packet.reset();
            buf.putByte((byte)0x22);
            buf.putInt(min);
            buf.putInt(preferred);
            buf.putInt(max);
            session.write(packet);

            state = SSH_MSG_KEX_DH_GEX_GROUP;
        }
Ejemplo n.º 4
0
        public override bool next(LibSterileSSH.SecureShell.Buffer _buf)
        {
            int  i, j;
            bool result = false;

            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)
                {
                    Console.WriteLine("type: must be 31 " + j);
                    result = false;
                    break;
                }

                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.out.print("K_S: "); //dump(K_S, 0, K_S.length);
                byte[] f        = _buf.getMPInt();
                byte[] sig_of_H = _buf.getString();

                /*
                 * for(int ii=0; ii<sig_of_H.length;ii++){
                 * System.out.print(Integer.toHexString(sig_of_H[ii]&0xff));
                 * System.out.print(": ");
                 * }
                 * Console.WriteLine("");
                 */

                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.out.print("H -> "); //dump(H, 0, H.length);

                i = 0;
                j = 0;
                j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                    ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                String alg = StringAux.getString(K_S, i, j);
                i += j;

                result = false;

                if (alg.Equals("ssh-rsa"))
                {
                    byte[] tmp;
                    byte[] ee;
                    byte[] n;

                    type = RSA;

                    j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                        ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    ee = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    n  = tmp;

                    //	SignatureRSA sig=new SignatureRSA();
                    //	sig.init();

                    ISignatureRSA sig = null;
                    try {
                        Type t = Type.GetType(session.getConfig("signature.rsa"));
                        sig = (ISignatureRSA)(Activator.CreateInstance(t));
                        sig.init();
                    }
                    catch (Exception eee) {
                        Console.WriteLine(eee);
                    }

                    sig.setPubKey(ee, n);
                    sig.update(H);
                    result = sig.verify(sig_of_H);
                    //MainClass.dump(ee, n, sig_of_H, H);
                }
                else if (alg.Equals("ssh-dss"))
                {
                    byte[] q = null;
                    byte[] tmp;
                    byte[] p;
                    byte[] g;

                    type = DSS;

                    j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                        ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    p  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    q  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    g  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    f  = tmp;
                    //	SignatureDSA sig=new SignatureDSA();
                    //	sig.init();
                    ISignatureDSA sig = null;
                    try {
                        Type t = Type.GetType(session.getConfig("signature.dss"));
                        sig = (ISignatureDSA)(Activator.CreateInstance(t));
                        sig.init();
                    }
                    catch (Exception ee) {
                        Console.WriteLine(ee);
                    }
                    sig.setPubKey(f, p, q, g);
                    sig.update(H);
                    result = sig.verify(sig_of_H);
                }
                else
                {
                    Console.WriteLine("unknow alg");
                }
                state = STATE_END;
                break;
            }
            return(result);
        }
Ejemplo n.º 5
0
        public static AKeyPair load(SshClient 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 {
                //File file=new File(prvkey);
                FileStream fis = File.OpenRead(prvkey);
                byte[]     buf = new byte[(int)(fis.Length)];
                int        len = fis.Read(buf, 0, buf.Length);
                fis.Close();

                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 = DSA;
                        }
                        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;
                            vendor = VENDOR_FSECURE;
                        }
                        else
                        {
                            //System.outs.println("invalid format: "+identity);
                            throw new SshClientException("invaid privatekey: " + prvkey);
                        }
                        i += 3;
                        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 SshClientException("invaid privatekey: " + prvkey);
                }

                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++;
                }
                data = StringAux.fromBase64(buf, start, i - start);

                if (data.Length > 4 &&                            // FSecure
                    data[0] == (byte)0x3f &&
                    data[1] == (byte)0x6f &&
                    data[2] == (byte)0xf9 &&
                    data[3] == (byte)0xeb)
                {
                    LibSterileSSH.SecureShell.Buffer _buf = new LibSterileSSH.SecureShell.Buffer(data);
                    _buf.getInt();                      // 0x3f6ff9be
                    _buf.getInt();
                    byte[] _type = _buf.getString();
                    //System.outs.println("type: "+new String(_type));
                    byte[] _cipher = _buf.getString();
                    String cipher  = StringAux.getString(_cipher);
                    //System.outs.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 SshClientException("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 File(pubkey);
                        fis = File.OpenRead(pubkey);
                        buf = new byte[(int)(fis.Length)];
                        len = fis.Read(buf, 0, buf.Length);
                        fis.Close();

                        if (buf.Length > 4 &&                                     // FSecure's public key
                            buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-')
                        {
                            bool valid = true;
                            i = 0;
                            do
                            {
                                i++;
                            } while (buf.Length > i && buf[i] != 0x0a);
                            if (buf.Length <= i)
                            {
                                valid = false;
                            }

                            while (valid)
                            {
                                if (buf[i] == 0x0a)
                                {
                                    bool inheader = false;
                                    for (int j = i + 1; j < buf.Length; j++)
                                    {
                                        if (buf[j] == 0x0a)
                                        {
                                            break;
                                        }
                                        if (buf[j] == ':')
                                        {
                                            inheader = true;
                                            break;
                                        }
                                    }
                                    if (!inheader)
                                    {
                                        i++;
                                        break;
                                    }
                                }
                                i++;
                            }
                            if (buf.Length <= i)
                            {
                                valid = false;
                            }

                            start = i;
                            while (valid && i < len)
                            {
                                if (buf[i] == 0x0a)
                                {
                                    Array.Copy(buf, i + 1, buf, i, len - i - 1);
                                    len--;
                                    continue;
                                }
                                if (buf[i] == '-')
                                {
                                    break;
                                }
                                i++;
                            }
                            if (valid)
                            {
                                publickeyblob = StringAux.fromBase64(buf, start, i - 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 = 0;
                                while (i < len)
                                {
                                    if (buf[i] == ' ')
                                    {
                                        break;
                                    }
                                    i++;
                                }
                                i++;
                                if (i < len)
                                {
                                    start = i;
                                    while (i < len)
                                    {
                                        if (buf[i] == ' ')
                                        {
                                            break;
                                        }
                                        i++;
                                    }
                                    publickeyblob = StringAux.fromBase64(buf, start, i - start);
                                }
                            }
                        }
                    }
                    catch                    //(Exception ee)
                    {
                    }
                }
            }
            catch (Exception e) {
                if (e is SshClientException)
                {
                    throw (SshClientException)e;
                }
                throw new SshClientException(e.ToString());
            }

            AKeyPair 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 SshClientException("invaid privatekey: " + prvkey);
                    }
                }
            }

            return(kpair);
        }
Ejemplo n.º 6
0
        internal override bool parse(byte[] plain)
        {
            /*
             * byte[] p_array;
             * byte[] q_array;
             * byte[] dmp1_array;
             * byte[] dmq1_array;
             * byte[] iqmp_array;
             */
            try {
                int index  = 0;
                int Length = 0;

                if (vendor == VENDOR_FSECURE)
                {
                    if (plain[index] != 0x30)                                        // FSecure
                    {
                        LibSterileSSH.SecureShell.Buffer buf = new LibSterileSSH.SecureShell.Buffer(plain);
                        pub_array = buf.getMPIntBits();
                        prv_array = buf.getMPIntBits();
                        n_array   = buf.getMPIntBits();
                        byte[] u_array = buf.getMPIntBits();
                        p_array = buf.getMPIntBits();
                        q_array = buf.getMPIntBits();
                        return(true);
                    }
                    return(false);
                }

                index++;                 // SEQUENCE
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }

                if (plain[index] != 0x02)
                {
                    return(false);
                }
                index++;                 // INTEGER
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                index += Length;

                //System.out.println("int: len="+Length);
                //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
                //System.out.println("");

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                n_array = new byte[Length];
                Array.Copy(plain, index, n_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: N len="+Length);
                 * for(int i=0; i<n_array.Length; i++){
                 * System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                pub_array = new byte[Length];
                Array.Copy(plain, index, pub_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: E len="+Length);
                 * for(int i=0; i<pub_array.Length; i++){
                 * System.out.print(Integer.toHexString(pub_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                prv_array = new byte[Length];
                Array.Copy(plain, index, prv_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: prv len="+Length);
                 * for(int i=0; i<prv_array.Length; i++){
                 * System.out.print(Integer.toHexString(prv_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                p_array = new byte[Length];
                Array.Copy(plain, index, p_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: P len="+Length);
                 * for(int i=0; i<p_array.Length; i++){
                 * System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                q_array = new byte[Length];
                Array.Copy(plain, index, q_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: q len="+Length);
                 * for(int i=0; i<q_array.Length; i++){
                 * System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                ep_array = new byte[Length];
                Array.Copy(plain, index, ep_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: ep len="+Length);
                 * for(int i=0; i<ep_array.Length; i++){
                 * System.out.print(Integer.toHexString(ep_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                eq_array = new byte[Length];
                Array.Copy(plain, index, eq_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: eq len="+Length);
                 * for(int i=0; i<eq_array.Length; i++){
                 * System.out.print(Integer.toHexString(eq_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                c_array = new byte[Length];
                Array.Copy(plain, index, c_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: c len="+Length);
                 * for(int i=0; i<c_array.Length; i++){
                 * System.out.print(Integer.toHexString(c_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
            }
            catch            //(Exception e)
            {
                //System.out.println(e);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 7
0
        internal override bool parse(byte[] plain)
        {
            try {
                if (vendor == VENDOR_FSECURE)
                {
                    if (plain[0] != 0x30)                                    // FSecure
                    {
                        LibSterileSSH.SecureShell.Buffer buf = new LibSterileSSH.SecureShell.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] != 0x30)
                {
                    return(false);
                }
                index++;                 // SEQUENCE
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }

                if (plain[index] != 0x02)
                {
                    return(false);
                }
                index++;                 // INTEGER
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                index += Length;

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                P_array = new byte[Length];
                Array.Copy(plain, index, P_array, 0, Length);
                index += Length;

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                Q_array = new byte[Length];
                Array.Copy(plain, index, Q_array, 0, Length);
                index += Length;

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                G_array = new byte[Length];
                Array.Copy(plain, index, G_array, 0, Length);
                index += Length;

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                pub_array = new byte[Length];
                Array.Copy(plain, index, pub_array, 0, Length);
                index += Length;

                index++;
                Length = plain[index++] & 0xff;
                if ((Length & 0x80) != 0)
                {
                    int foo = Length & 0x7f;
                    Length = 0;
                    while (foo-- > 0)
                    {
                        Length = (Length << 8) + (plain[index++] & 0xff);
                    }
                }
                prv_array = new byte[Length];
                Array.Copy(plain, index, prv_array, 0, Length);
                index += Length;
            }
            catch            //(Exception e)
            {
                //System.out.println(e);
                //e.printStackTrace();
                return(false);
            }
            return(true);
        }
Ejemplo n.º 8
0
        public override bool next(LibSterileSSH.SecureShell.Buffer _buf)
        {
            int  i, j;
            bool result = false;

            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 != 31)
                {
                    Console.WriteLine("type: must be 31 " + j);
                    result = false;
                }

                p = _buf.getMPInt();
                g = _buf.getMPInt();

                /*
                 * for(int iii=0; iii<p.length; iii++){
                 * System.out.println("0x"+Integer.toHexString(p[iii]&0xff)+",");
                 * }
                 * System.out.println("");
                 * for(int iii=0; iii<g.length; iii++){
                 * System.out.println("0x"+Integer.toHexString(g[iii]&0xff)+",");
                 * }
                 */
                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((byte)0x20);
                buf.putMPInt(e);
                session.write(packet);

                state  = SSH_MSG_KEX_DH_GEX_REPLY;
                result = true;
                break;

            case SSH_MSG_KEX_DH_GEX_REPLY:
                // 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 != 33)
                {
                    Console.WriteLine("type: must be 33 " + j);
                    result = 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.out.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.out.print("H -> "); dump(H, 0, H.length);

                i = 0;
                j = 0;
                j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                    ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                String alg = StringAux.getString(K_S, i, j);
                i += j;


                if (alg.Equals("ssh-rsa"))
                {
                    byte[] tmp;
                    byte[] ee;
                    byte[] n;

                    type = RSA;

                    j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                        ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    ee = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    n  = tmp;

                    //	SignatureRSA sig=new SignatureRSA();
                    //	sig.init();

                    ISignatureRSA sig = null;
                    try {
                        Type t = Type.GetType(session.getConfig("signature.rsa"));
                        sig = (ISignatureRSA)(Activator.CreateInstance(t));
                        sig.init();
                    }
                    catch (Exception eee) {
                        Console.WriteLine(eee);
                    }

                    sig.setPubKey(ee, n);
                    sig.update(H);
                    result = sig.verify(sig_of_H);
                }
                else if (alg.Equals("ssh-dss"))
                {
                    byte[] q = null;
                    byte[] tmp;

                    type = DSS;

                    j = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                        ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    p  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    q  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    g  = tmp;
                    j  = (int)((K_S[i++] << 24) & 0xff000000) | ((K_S[i++] << 16) & 0x00ff0000) |
                         ((K_S[i++] << 8) & 0x0000ff00) | ((K_S[i++]) & 0x000000ff);
                    tmp = new byte[j];
                    Array.Copy(K_S, i, tmp, 0, j);
                    i += j;
                    f  = tmp;

                    //	SignatureDSA sig=new SignatureDSA();
                    //	sig.init();

                    ISignatureDSA sig = null;
                    try {
                        Type t = Type.GetType(session.getConfig("signature.dss"));
                        sig = (ISignatureDSA)(Activator.CreateInstance(t));
                        sig.init();
                    }
                    catch (Exception ee) {
                        Console.WriteLine(ee);
                    }

                    sig.setPubKey(f, p, q, g);
                    sig.update(H);
                    result = sig.verify(sig_of_H);
                }
                else
                {
                    Console.WriteLine("unknow alg");
                }
                state = STATE_END;
                break;
            }
            return(result);
        }
Ejemplo n.º 9
0
        /*
         * void dump(byte[] foo){
         * for(int i=0; i<foo.length; i++){
         *      if((foo[i]&0xf0)==0)System.out.print("0");
         *      System.out.print(Integer.toHexString(foo[i]&0xff));
         *      if(i%16==15){System.out.println(""); continue;}
         *      if(i%2==1)System.out.print(" ");
         * }
         * }
         */

        internal static String[] guess(byte[] I_S, byte[] I_C)
        {
            //System.out.println("guess: ");
            String[] guess = new String[PROPOSAL_MAX];
            LibSterileSSH.SecureShell.Buffer sb = new LibSterileSSH.SecureShell.Buffer(I_S);
            sb.setOffSet(17);
            LibSterileSSH.SecureShell.Buffer cb = new LibSterileSSH.SecureShell.Buffer(I_C);
            cb.setOffSet(17);

            for (int i = 0; i < PROPOSAL_MAX; i++)
            {
                byte[] sp = sb.getString();                  // server proposal
                byte[] cp = cb.getString();                  // client proposal

                //System.out.println("server-proposal: |"+new String(sp)+"|");
                //System.out.println("client-proposal: |"+new String(cp)+"|");

                int j = 0;
                int k = 0;
                //System.out.println(new String(cp));
                //loop(using BREAK instead):
                while (j < cp.Length)
                {
                    while (j < cp.Length && cp[j] != ',')
                    {
                        j++;
                    }
                    if (k == j)
                    {
                        return(null);
                    }
                    String algorithm = StringAux.getString(cp, k, j - k);
                    //System.out.println("algorithm: "+algorithm);
                    int l = 0;
                    int m = 0;
                    while (l < sp.Length)
                    {
                        while (l < sp.Length && sp[l] != ',')
                        {
                            l++;
                        }
                        if (m == l)
                        {
                            return(null);
                        }
                        //System.out.println("  "+new String(sp, m, l-m));
                        if (algorithm.Equals(StringAux.getString(sp, m, l - m)))
                        {
                            guess[i] = algorithm;
                            //System.out.println("  "+algorithm);
                            goto BREAK;
                        }
                        l++;
                        m = l;
                    }
                    j++;
                    k = j;
                }
BREAK:
                if (j == 0)
                {
                    guess[i] = "";
                }
                else if (guess[i] == null)
                {
                    //System.out.println("  fail");
                    return(null);
                }
            }

            //    for(int i=0; i<PROPOSAL_MAX; i++){
            //      System.out.println("guess: ["+guess[i]+"]");
            //    }

            return(guess);
        }
Ejemplo n.º 10
0
 public abstract bool next(LibSterileSSH.SecureShell.Buffer buf);