Exemple #1
0
        private byte[] encrypt(byte[] plain, byte[][] _iv)
        {
            if (passphrase == null)
            {
                return(plain);
            }

            if (cipher == null)
            {
                cipher = genCipher();
            }
            byte[] iv = _iv[0] = new byte[cipher.getIVSize()];

            if (random == null)
            {
                random = genRandom();
            }
            random.fill(iv, 0, iv.Length);

            byte[] key     = genKey(passphrase, iv);
            byte[] encoded = plain;
            int    bsize   = cipher.getBlockSize();

            if (encoded.Length % bsize != 0)
            {
                byte[] foo = new byte[(encoded.Length / bsize + 1) * bsize];
                Array.Copy(encoded, 0, foo, 0, encoded.Length);
                encoded = foo;
            }

            try
            {
                cipher.init(Cipher.ENCRYPT_MODE, key, iv);
                cipher.update(encoded, 0, encoded.Length, encoded, 0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(encoded);
        }
		private void updateKeys(KeyExchange kex) 
		{
			byte[] K=kex.getK();
			byte[] H=kex.getH();
			HASH hash=kex.getHash();

			String[] guess=kex._guess;

			if(session_id==null)
			{
				session_id=new byte[H.Length];
				Tamir.SharpSsh.java.System.arraycopy(H, 0, session_id, 0, H.Length);
			}

			/*
			  Initial IV client to server:     HASH (K || H || "A" || session_id)
			  Initial IV server to client:     HASH (K || H || "B" || session_id)
			  Encryption key client to server: HASH (K || H || "C" || session_id)
			  Encryption key server to client: HASH (K || H || "D" || session_id)
			  Integrity key client to server:  HASH (K || H || "E" || session_id)
			  Integrity key server to client:  HASH (K || H || "F" || session_id)
			*/

			buf.reset();
			buf.putMPInt(K);
			buf.putByte(H);
			buf.putByte((byte)0x41);
			buf.putByte(session_id);
			hash.update(buf.buffer, 0, buf.index);
			IVc2s=hash.digest();

			int j=buf.index-session_id.Length-1;

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			IVs2c=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			Ec2s=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			Es2c=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			MACc2s=hash.digest();

			buf.buffer[j]++;
			hash.update(buf.buffer, 0, buf.index);
			MACs2c=hash.digest();

			try
			{
				Class c;

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC]));
				s2ccipher=(Cipher)(c.newInstance());
				while(s2ccipher.getBlockSize()>Es2c.Length)
				{
					buf.reset();
					buf.putMPInt(K);
					buf.putByte(H);
					buf.putByte(Es2c);
					hash.update(buf.buffer, 0, buf.index);
					byte[] foo=hash.digest();
					byte[] bar=new byte[Es2c.Length+foo.Length];
					Tamir.SharpSsh.java.System.arraycopy(Es2c, 0, bar, 0, Es2c.Length);
					Tamir.SharpSsh.java.System.arraycopy(foo, 0, bar, Es2c.Length, foo.Length);
					Es2c=bar;
				}
				s2ccipher.init(Cipher.DECRYPT_MODE, Es2c, IVs2c);
				cipher_size=s2ccipher.getIVSize();
				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC]));
				s2cmac=(MAC)(c.newInstance());
				s2cmac.init(MACs2c);
				mac_buf=new byte[s2cmac.getBlockSize()];

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS]));
				c2scipher=(Cipher)(c.newInstance());
				while(c2scipher.getBlockSize()>Ec2s.Length)
				{
					buf.reset();
					buf.putMPInt(K);
					buf.putByte(H);
					buf.putByte(Ec2s);
					hash.update(buf.buffer, 0, buf.index);
					byte[] foo=hash.digest();
					byte[] bar=new byte[Ec2s.Length+foo.Length];
					Tamir.SharpSsh.java.System.arraycopy(Ec2s, 0, bar, 0, Ec2s.Length);
					Tamir.SharpSsh.java.System.arraycopy(foo, 0, bar, Ec2s.Length, foo.Length);
					Ec2s=bar;
				}
				c2scipher.init(Cipher.ENCRYPT_MODE, Ec2s, IVc2s);

				c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS]));
				c2smac=(MAC)(c.newInstance());
				c2smac.init(MACc2s);

				if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS].equals("none"))
				{
					String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]);
					if(foo!=null)
					{
						try
						{
							c=Class.forName(foo);
							deflater=(Compression)(c.newInstance());
							int level=6;
							try{ level=Integer.parseInt(getConfig("compression_level"));}
							catch(Exception ee){ }
							deflater.init(Compression.DEFLATER, level);
						}
						catch(Exception ee)
						{
							System.Console.Error.WriteLine(foo+" isn't accessible.");
						}
					}
				}
				else
				{
					if(deflater!=null)
					{
						deflater=null;
					}
				}
				if(!guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC].equals("none"))
				{
					String foo=getConfig(guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]);
					if(foo!=null)
					{
						try
						{
							c=Class.forName(foo);
							inflater=(Compression)(c.newInstance());
							inflater.init(Compression.INFLATER, 0);
						}
						catch(Exception ee)
						{
							System.Console.Error.WriteLine(foo+" isn't accessible.");
						}
					}
				}
				else
				{
					if(inflater!=null)
					{
						inflater=null;
					}
				}
			}
			catch(Exception e){ System.Console.Error.WriteLine("updatekeys: "+e); }
		}
Exemple #3
0
        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(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(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] != 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;

                //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);
                    }
                }
                e_array = new byte[Length];
                Array.Copy(plain, index, e_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: E len="+Length);
                 * for(int i=0; i<e_array.Length; i++){
                 * System.out.print(Integer.toHexString(e_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);
                    }
                }
                d_array = new byte[Length];
                Array.Copy(plain, index, d_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: D len="+Length);
                 * for(int i=0; i<d_array.Length; i++){
                 * System.out.print(Integer.toHexString(d_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);
                    }
                }
                dmp1_array = new byte[Length];
                Array.Copy(plain, index, dmp1_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: dmp1 len="+Length);
                 * for(int i=0; i<dmp1_array.Length; i++){
                 * System.out.print(Integer.toHexString(dmp1_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);
                    }
                }
                dmq1_array = new byte[Length];
                Array.Copy(plain, index, dmq1_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: dmq1 len="+Length);
                 * for(int i=0; i<dmq1_array.Length; i++){
                 * System.out.print(Integer.toHexString(dmq1_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);
                    }
                }
                iqmp_array = new byte[Length];
                Array.Copy(plain, index, iqmp_array, 0, Length);
                index += Length;

                /*
                 * System.out.println("int: iqmp len="+Length);
                 * for(int i=0; i<iqmp_array.Length; i++){
                 * System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
                 * }
                 * System.out.println("");
                 */
            }
            catch
            {
                //System.out.println(e);
                return(false);
            }
            return(true);
        }