ComputeAsymetricKeys() public static method

public static ComputeAsymetricKeys ( byte sharedSecret, byte initiatorNonce, byte responderNonce, byte &requestKey, byte &responseKey ) : void
sharedSecret byte
initiatorNonce byte
responderNonce byte
requestKey byte
responseKey byte
return void
Example #1
0
        public void Run()
        {
            if (DH == null)
            {
                DH = RtmfpUtils.BeginDiffieHellman(ref Nonce);
                return;
            }
            // Compute Diffie-Hellman secret
            SharedSecret = DH.CreateSharedKey(InitiatorKey);
            // Compute Keys
            RtmfpUtils.ComputeAsymetricKeys(SharedSecret, InitiatorNonce, Nonce, out DecryptKey, out EncryptKey);
            var pSession = _handshake?.CreateSession(Value);

            if (pSession != null)
            {
                pSession.Peer.UserState = this;
            }
        }
        public override void PacketHandler(N2HBinaryReader reader)
        {
            if (Checked)
            {
                lock (Writer)
                {
                    base.PacketHandler(reader);
                }
                return;
            }
            var marker = reader.ReadByte();

            if (marker != 0x0b)
            {
                Logger.FATAL("Marker hand shake wrong:should be 0b and not {0:X}", marker);
                return;
            }
            var time   = reader.ReadUInt16();
            var type   = reader.ReadByte();
            var length = reader.ReadUInt16();

            byte[] tag;
            Logger.Debug("handshake {0:X} len:{1}", type, length);
            Debug.WriteLine("handshake {0:X} len:{1}", type, length);
            switch (type)
            {
            case 0x70:

                tag = reader.ReadBytes(reader.ReadByte());
                var cookieBytes      = reader.ReadBytes(reader.ReadByte());
                var targetCertificat = reader.ReadBytes((int)reader.BaseStream.GetAvaliableByteCounts());
                var nonce            = new byte[0];
                _dh     = RtmfpUtils.BeginDiffieHellman(ref nonce, true);
                Peer.Id = Target.Sha256.ComputeHash(nonce, 0, nonce.Length);
                HandShake38(cookieBytes, nonce);
                _handshake = () => HandShake38(cookieBytes, nonce);
                break;

            case 0x71:
                tag = reader.ReadBytes(reader.ReadByte());
                var flag    = reader.ReadByte();
                var address = new IPEndPoint(new IPAddress(reader.ReadBytes(4)), reader.ReadInt16());

                Target.Address.Port = address.Port;
                Logger.Debug("redirect to {0}", address.ToString());
                Handler.FarProtocol.IOHandler.Socket.Connect(Target.Address);
                _handshake();
                break;

            case 0x78:

                FarId = reader.ReadUInt32();
                var targetNonce = reader.ReadBytes((int)reader.Read7BitLongValue());
                var must58      = reader.ReadByte();
                Debug.WriteLineIf(must58 != 0x58, $"must58!{must58}");
                var key = new byte[RtmfpUtils.KEY_SIZE];
                Buffer.BlockCopy(targetNonce, targetNonce.Length - RtmfpUtils.KEY_SIZE, key, 0, RtmfpUtils.KEY_SIZE);
                var    sharedSecret = _dh.CreateSharedKey(key);
                byte[] decryptKey;
                byte[] encryptKey;
                RtmfpUtils.ComputeAsymetricKeys(sharedSecret, _certificat, targetNonce, out encryptKey, out decryptKey);
                Checked = true;
                _handshakeTimeoutTimer.Stop();
                AesEncrypt  = new AESEngine(encryptKey, AESEngine.Direction.ENCRYPT);
                AesDecrypt  = new AESEngine(decryptKey);
                PrevAesType = AESEngine.AESType.DEFAULT;
                Application = Handler.Application;
                Handler.CreateSession(Peer, null);

                break;

            default:

                break;
            }
        }