示例#1
0
    private void PromotePendingEncryptionState(IPEndPoint endPoint, PacketEncryptionLayer.EncryptionState state)
    {
        Dictionary <IPEndPoint, PacketEncryptionLayer.EncryptionState> encryptionStates = this._encryptionStates;

        lock (encryptionStates)
        {
            if (!this._encryptionStates.ContainsKey(endPoint))
            {
                this._encryptionStates[endPoint] = state;
                Dictionary <int, PacketEncryptionLayer.EncryptionState> dictionary;
                if (this._pendingEncryptionStates.TryGetValue(endPoint.Address, out dictionary))
                {
                    if (!dictionary.Remove(endPoint.Port))
                    {
                        int num = -1;
                        foreach (KeyValuePair <int, PacketEncryptionLayer.EncryptionState> keyValuePair in dictionary)
                        {
                            if (keyValuePair.Value == state)
                            {
                                num = keyValuePair.Key;
                            }
                        }
                        if (num != -1)
                        {
                            dictionary.Remove(num);
                        }
                    }
                    if (dictionary.Count == 0)
                    {
                        this._pendingEncryptionStates.Remove(endPoint.Address);
                    }
                }
            }
        }
    }
示例#2
0
    public PacketEncryptionLayer.IEncryptionState AddUnidentifiedEncryptedEndpoint(uint protocolVersion, IPEndPoint endPoint, byte[] preMasterSecret, byte[] serverRandom, byte[] clientRandom, bool isClient)
    {
        PacketEncryptionLayer.EncryptionState encryptionState = new PacketEncryptionLayer.EncryptionState(protocolVersion, preMasterSecret, serverRandom, clientRandom, isClient);
        Dictionary <IPEndPoint, PacketEncryptionLayer.EncryptionState> encryptionStates = this._encryptionStates;

        lock (encryptionStates)
        {
            this._encryptionStates[endPoint] = encryptionState;
        }
        return(encryptionState);
    }
示例#3
0
    public void AddEncryptedEndpoint(uint protocolVersion, IPEndPoint endPoint, string userId, string userName, byte[] preMasterSecret, byte[] serverRandom, byte[] clientRandom, bool isClient)
    {
        PacketEncryptionLayer.EncryptionState encryptionState = new PacketEncryptionLayer.EncryptionState(protocolVersion, preMasterSecret, serverRandom, clientRandom, isClient);
        encryptionState.SetIdentity(userId, userName);
        Dictionary <IPEndPoint, PacketEncryptionLayer.EncryptionState> encryptionStates = this._encryptionStates;

        lock (encryptionStates)
        {
            this._encryptionStates[endPoint] = encryptionState;
        }
    }
示例#4
0
    private bool TryGetEncryptionState(IPEndPoint endPoint, out PacketEncryptionLayer.EncryptionState state, out bool statePending)
    {
        state        = null;
        statePending = false;
        if (endPoint == null)
        {
            return(false);
        }
        Dictionary <IPEndPoint, PacketEncryptionLayer.EncryptionState> encryptionStates = this._encryptionStates;
        bool result;

        lock (encryptionStates)
        {
            Dictionary <int, PacketEncryptionLayer.EncryptionState> dictionary;
            if (this._encryptionStates.TryGetValue(endPoint, out state))
            {
                result = true;
            }
            else if (!this._pendingEncryptionStates.TryGetValue(endPoint.Address, out dictionary))
            {
                result = false;
            }
            else
            {
                statePending = true;
                if (dictionary.TryGetValue(endPoint.Port, out state))
                {
                    result = true;
                }
                else
                {
                    int num = int.MaxValue;
                    foreach (KeyValuePair <int, PacketEncryptionLayer.EncryptionState> keyValuePair in dictionary)
                    {
                        int num2 = Math.Abs(keyValuePair.Key - endPoint.Port);
                        if (num2 < num)
                        {
                            num   = num2;
                            state = keyValuePair.Value;
                        }
                    }
                    result = true;
                }
            }
        }
        return(result);
    }
示例#5
0
    public void AddPendingEncryptedEndpoint(uint protocolVersion, IPEndPoint endPoint, string userId, string userName, byte[] preMasterSecret, byte[] serverRandom, byte[] clientRandom, bool isClient)
    {
        PacketEncryptionLayer.EncryptionState encryptionState = new PacketEncryptionLayer.EncryptionState(protocolVersion, preMasterSecret, serverRandom, clientRandom, isClient);
        encryptionState.SetIdentity(userId, userName);
        Dictionary <IPEndPoint, PacketEncryptionLayer.EncryptionState> encryptionStates = this._encryptionStates;

        lock (encryptionStates)
        {
            Dictionary <int, PacketEncryptionLayer.EncryptionState> dictionary;
            if (!this._pendingEncryptionStates.TryGetValue(endPoint.Address, out dictionary))
            {
                dictionary = new Dictionary <int, PacketEncryptionLayer.EncryptionState>();
                this._pendingEncryptionStates[endPoint.Address] = dictionary;
            }
            dictionary[endPoint.Port] = encryptionState;
        }
    }
示例#6
0
    private bool TryDecryptData(byte[] data, PacketEncryptionLayer.EncryptionState state, int startingOffset, ref int offset, ref int length)
    {
        object receiveMutex = state.receiveMutex;
        bool   result;

        lock (receiveMutex)
        {
            uint num = BitConverter.ToUInt32(data, offset);
            offset += 4;
            length -= 4;
            if (!state.IsValidSequenceNum(num))
            {
                result = false;
            }
            else
            {
                if (PacketEncryptionLayer._tempIV == null)
                {
                    PacketEncryptionLayer._tempIV = new byte[16];
                }
                if (PacketEncryptionLayer._tempHash == null)
                {
                    PacketEncryptionLayer._tempHash = new byte[10];
                }
                PacketEncryptionLayer.FastCopyBlock(data, offset, PacketEncryptionLayer._tempIV, 0);
                offset += PacketEncryptionLayer._tempIV.Length;
                length -= PacketEncryptionLayer._tempIV.Length;
                using (ICryptoTransform cryptoTransform = this._aes.CreateDecryptor(state.receiveKey, PacketEncryptionLayer._tempIV))
                {
                    int num2 = startingOffset;
                    int num3;
                    for (int i = length; i >= cryptoTransform.InputBlockSize; i -= num3)
                    {
                        int inputCount = cryptoTransform.CanTransformMultipleBlocks ? (i / cryptoTransform.InputBlockSize * cryptoTransform.InputBlockSize) : cryptoTransform.InputBlockSize;
                        num3    = cryptoTransform.TransformBlock(data, offset, inputCount, data, num2);
                        offset += num3;
                        num2   += num3;
                    }
                    offset = startingOffset;
                    length = num2 - offset;
                }
                int  num4  = (int)data[offset + length - 1];
                bool flag2 = true;
                if (num4 + 10 + 1 > length)
                {
                    num4  = 0;
                    flag2 = false;
                }
                length -= num4 + 10 + 1;
                PacketEncryptionLayer.FastCopyMac(data, offset + length, PacketEncryptionLayer._tempHash, 0);
                FastBitConverter.GetBytes(data, offset + length, num);
                byte[] array = state.receiveMac.ComputeHash(data, offset, length + 4);
                if (!flag2)
                {
                    result = false;
                }
                else
                {
                    for (int j = 0; j < 10; j++)
                    {
                        if (PacketEncryptionLayer._tempHash[j] != array[j])
                        {
                            return(false);
                        }
                    }
                    if (!state.PutSequenceNum(num))
                    {
                        result = false;
                    }
                    else
                    {
                        result = true;
                    }
                }
            }
        }
        return(result);
    }