Exemple #1
0
 public RSAKeyPair(BigInteger e, BigInteger d, BigInteger n, BigInteger u, BigInteger p, BigInteger q)
 {
     _publickey = new RSAPublicKey(e, n);
     _d = d;
     _u = u;
     _p = p;
     _q = q;
 }
        private void VerifyHostKeyByRSA(SSH2DataReader pubkey, byte[] sigbody, byte[] hash)
        {
            BigInteger exp = pubkey.ReadMPInt();
            BigInteger mod = pubkey.ReadMPInt();
            Debug.Assert(pubkey.Rest==0);

            //Debug.WriteLine(exp.ToHexString());
            //Debug.WriteLine(mod.ToHexString());

            RSAPublicKey pk = new RSAPublicKey(exp, mod);
            pk.VerifyWithSHA1(sigbody, new SHA1CryptoServiceProvider().ComputeHash(hash));
            _cInfo._hostkey = pk;
        }
        private void SendSessionKey(byte[] session_key)
        {
            try
            {
                //step1 XOR with session_id
                byte[] working_data = new byte[session_key.Length];
                byte[] session_id = CalcSessionID();
                Array.Copy(session_key, 0, working_data, 0, session_key.Length);
                for(int i=0; i<session_id.Length; i++) working_data[i] ^= session_id[i];

                //step2 decrypts with RSA
                RSAPublicKey first_encryption;
                RSAPublicKey second_encryption;
                SSHServerInfo si = _cInfo._serverinfo;
                int first_key_bytelen, second_key_bytelen;
                if(si.server_key_bits < si.host_key_bits)
                {
                    first_encryption  = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    second_encryption = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    first_key_bytelen = (si.server_key_bits+7)/8;
                    second_key_bytelen = (si.host_key_bits+7)/8;
                }
                else
                {
                    first_encryption  = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus);
                    second_encryption = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus);
                    first_key_bytelen = (si.host_key_bits+7)/8;
                    second_key_bytelen = (si.server_key_bits+7)/8;
                }

                BigInteger first_result = RSAUtil.PKCS1PadType2(new BigInteger(working_data), first_key_bytelen, _param.Random).modPow(first_encryption.Exponent, first_encryption.Modulus);
                BigInteger second_result = RSAUtil.PKCS1PadType2(first_result, second_key_bytelen, _param.Random).modPow(second_encryption.Exponent, second_encryption.Modulus);

                //output
                SSH1DataWriter writer = new SSH1DataWriter();
                writer.Write((byte)_cInfo._algorithmForTransmittion);
                writer.Write(si.anti_spoofing_cookie);
                writer.Write(second_result);
                writer.Write(0); //protocol flags

                //send
                SSH1Packet SSH1Packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_SESSION_KEY, writer.ToByteArray());
                SSH1Packet.WriteTo(_stream);

                _sessionID = session_id;

            }
            catch(Exception e)
            {
                if(e is IOException)
                    throw (IOException)e;
                else
                {
                    string t = e.StackTrace;
                    throw new SSHException(e.Message); //IOException�ȊO�݂͂�SSHException�ɂ��Ă��܂�
                }
            }
        }