Example #1
0
        public bool IsDeadSource(UpDownClient pToCheck)
        {
            if (!pToCheck.HasLowID || pToCheck.ServerIP != 0)
            {
                DeadSource ds =
                    new DeadSource(pToCheck.UserIDHybrid, pToCheck.UserPort,
                                   pToCheck.ServerIP, pToCheck.KadPort);

                if (deadSources_.ContainsKey(ds))
                {
                    if (deadSources_[ds] > MpdUtilities.GetTickCount())
                    {
                        return(true);
                    }
                }
            }
            if (((pToCheck.HasValidBuddyID ||
                  pToCheck.SupportsDirectUDPCallback) &&
                 !MpdUtilities.IsNullMd4(pToCheck.UserHash)) ||
                (pToCheck.HasLowID && pToCheck.ServerIP == 0))
            {
                DeadSource ds = new DeadSource(pToCheck.UserHash);
                if (deadSources_.ContainsKey(ds))
                {
                    if (deadSources_[ds] > MpdUtilities.GetTickCount())
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #2
0
 public static bool operator !=(DeadSource ds1, DeadSource ds2)
 {
     return(!(
                // lowid ed2k and highid kad + ed2k check
                ((ds1.id_ != 0 && ds1.id_ == ds2.id_) &&
                 ((ds1.port_ != 0 && ds1.port_ == ds2.port_) ||
                  (ds1.kadPort_ != 0 && ds1.kadPort_ == ds2.kadPort_)) &&
                 (ds1.serverIP_ == ds2.serverIP_ ||
                  !MuleUtilities.IsLowID(ds1.id_)))
                // lowid kad check
                || (MuleUtilities.IsLowID(ds1.id_) &&
                    MpdUtilities.IsNullMd4(ds1.hash_) == false &&
                    MpdUtilities.Md4Cmp(ds1.hash_, ds2.hash_) == 0)));
 }
        public virtual int EncryptSendClient(ref byte[] ppbyBuf, int nBufLen, byte[] pachClientHashOrKadID, bool bKad, uint nReceiverVerifyKey, uint nSenderVerifyKey)
        {
            Debug.Assert(MuleApplication.Instance.PublicIP != 0 || bKad);
            Debug.Assert(MuleApplication.Instance.Preference.IsClientCryptLayerSupported);
            Debug.Assert(pachClientHashOrKadID != null || nReceiverVerifyKey != 0);
            Debug.Assert((nReceiverVerifyKey == 0 && nSenderVerifyKey == 0) || bKad);

            byte byPadLen        = 0;           // padding disabled for UDP currently
            int  nCryptHeaderLen = byPadLen + CRYPT_HEADER_WITHOUTPADDING + (bKad ? 8 : 0);

            int nCryptedLen = nBufLen + nCryptHeaderLen;

            byte[] pachCryptedBuffer = new byte[nCryptedLen];
            bool   bKadRecKeyUsed    = false;

            ushort nRandomKeyPart = MpdUtilities.GetRandomUInt16();
            MD5    md5            = MD5.Create();

            byte[] rawHash = null;
            if (bKad)
            {
                if ((pachClientHashOrKadID == null ||
                     MpdUtilities.IsNullMd4(pachClientHashOrKadID)) &&
                    nReceiverVerifyKey != 0)
                {
                    bKadRecKeyUsed = true;
                    byte[] achKeyData = new byte[6];
                    Array.Copy(BitConverter.GetBytes(nReceiverVerifyKey), achKeyData, 4);
                    Array.Copy(BitConverter.GetBytes(nRandomKeyPart), 0,
                               achKeyData, 4, 2);
                    rawHash = md5.ComputeHash(achKeyData);
                    //DEBUG_ONLY( DebugLog(_T("Creating obfuscated Kad packet encrypted by ReceiverKey (%u)"), nReceiverVerifyKey) );
                }
                else if (pachClientHashOrKadID != null && !MpdUtilities.IsNullMd4(pachClientHashOrKadID))
                {
                    byte[] achKeyData = new byte[18];
                    MpdUtilities.Md4Cpy(achKeyData, pachClientHashOrKadID);
                    Array.Copy(BitConverter.GetBytes(nRandomKeyPart), 0,
                               achKeyData, 16, 2);
                    rawHash = md5.ComputeHash(achKeyData);
                    //DEBUG_ONLY( DebugLog(_T("Creating obfuscated Kad packet encrypted by Hash/NodeID %s"), md4str(pachClientHashOrKadID)) );
                }
                else
                {
                    ppbyBuf = null;
                    Debug.Assert(false);
                    return(nBufLen);
                }
            }
            else
            {
                byte[] achKeyData = new byte[23];
                MpdUtilities.Md4Cpy(achKeyData, pachClientHashOrKadID);
                uint dwIP = (uint)MuleApplication.Instance.PublicIP;
                Array.Copy(BitConverter.GetBytes(dwIP), 0, achKeyData, 16, 4);
                Array.Copy(BitConverter.GetBytes(nRandomKeyPart), 0, achKeyData, 21, 2);
                achKeyData[20] = MAGICVALUE_UDP;
                rawHash        = md5.ComputeHash(achKeyData);
            }
            RC4Key keySendKey = null;

            MuleUtilities.RC4CreateKey(rawHash, 16, ref keySendKey, true);

            // create the semi random byte encryption header
            byte bySemiRandomNotProtocolMarker = 0;
            int  i;

            for (i = 0; i < 128; i++)
            {
                bySemiRandomNotProtocolMarker = MpdUtilities.GetRandomUInt8();
                bySemiRandomNotProtocolMarker =
                    (byte)(bKad ? (bySemiRandomNotProtocolMarker & 0xFE) :
                           (bySemiRandomNotProtocolMarker | 0x01)); // set the ed2k/kad marker bit
                if (bKad)
                {
                    bySemiRandomNotProtocolMarker =
                        (byte)(bKadRecKeyUsed ? ((bySemiRandomNotProtocolMarker & 0xFE) | 0x02) :
                               (bySemiRandomNotProtocolMarker & 0xFC)); // set the ed2k/kad and nodeid/reckey markerbit
                }
                else
                {
                    bySemiRandomNotProtocolMarker = (byte)(bySemiRandomNotProtocolMarker | 0x01); // set the ed2k/kad marker bit
                }
                bool bOk = false;
                switch (bySemiRandomNotProtocolMarker)
                { // not allowed values
                case MuleConstants.PROTOCOL_EMULEPROT:
                case MuleConstants.PROTOCOL_KADEMLIAPACKEDPROT:
                case MuleConstants.PROTOCOL_KADEMLIAHEADER:
                case MuleConstants.PROTOCOL_UDPRESERVEDPROT1:
                case MuleConstants.PROTOCOL_UDPRESERVEDPROT2:
                case MuleConstants.PROTOCOL_PACKEDPROT:
                    break;

                default:
                    bOk = true;
                    break;
                }
                if (bOk)
                {
                    break;
                }
            }
            if (i >= 128)
            {
                // either we have _really_ bad luck or the randomgenerator is a bit messed up
                Debug.Assert(false);
                bySemiRandomNotProtocolMarker = 0x01;
            }

            uint dwMagicValue = MAGICVALUE_UDP_SYNC_CLIENT;

            pachCryptedBuffer[0] = bySemiRandomNotProtocolMarker;
            Array.Copy(BitConverter.GetBytes(nRandomKeyPart), 0, pachCryptedBuffer, 1, 2);
            MuleUtilities.RC4Crypt(BitConverter.GetBytes(dwMagicValue), 0, pachCryptedBuffer, 3, 4, keySendKey);
            MuleUtilities.RC4Crypt(BitConverter.GetBytes(byPadLen), 0, pachCryptedBuffer, 7, 1, keySendKey);

            Random rand = new Random();

            for (int j = 0; j < byPadLen; j++)
            {
                byte byRand = (byte)rand.Next(255);     // they actually dont really need to be random, but it doesn't hurts either
                MuleUtilities.RC4Crypt(BitConverter.GetBytes(byRand), 0,
                                       pachCryptedBuffer, CRYPT_HEADER_WITHOUTPADDING + j, 1, keySendKey);
            }

            if (bKad)
            {
                MuleUtilities.RC4Crypt(BitConverter.GetBytes(nReceiverVerifyKey), 0,
                                       pachCryptedBuffer, CRYPT_HEADER_WITHOUTPADDING + byPadLen, 4, keySendKey);
                MuleUtilities.RC4Crypt(BitConverter.GetBytes(nSenderVerifyKey), 0,
                                       pachCryptedBuffer, CRYPT_HEADER_WITHOUTPADDING + byPadLen + 4, 4, keySendKey);
            }

            MuleUtilities.RC4Crypt(ppbyBuf, 0,
                                   pachCryptedBuffer, nCryptHeaderLen, (uint)nBufLen, keySendKey);
            ppbyBuf = pachCryptedBuffer;

            MuleApplication.Instance.Statistics.AddUpDataOverheadCrypt((uint)(nCryptedLen - nBufLen));
            return(nCryptedLen);
        }