예제 #1
0
 private void WriteCreditStruct(SafeMemFile memfile, CreditStruct creditStruct)
 {
     memfile.WriteUInt32(creditStruct.nUploadedLo);      // uploaded TO him
     memfile.WriteUInt32(creditStruct.nDownloadedLo);    // downloaded from him
     memfile.WriteUInt32(creditStruct.nLastSeen);
     memfile.WriteUInt32(creditStruct.nUploadedHi);      // upload high 32
     memfile.WriteUInt32(creditStruct.nDownloadedHi);    // download high 32
     memfile.WriteUInt16(creditStruct.nReserved3);
     memfile.WriteUInt8(creditStruct.nKeySize);
     memfile.Write(creditStruct.abySecureIdent);
 }
예제 #2
0
        private void StartNegotiation(bool bOutgoing)
        {
            if (!bOutgoing)
            {
                negotiatingState_   = NegotiatingStateEnum.ONS_BASIC_CLIENTA_RANDOMPART;
                streamCryptState_   = StreamCryptStateEnum.ECS_NEGOTIATING;
                receiveBytesWanted_ = 4;
            }
            else if (streamCryptState_ == StreamCryptStateEnum.ECS_PENDING)
            {
                SafeMemFile fileRequest = MpdObjectManager.CreateSafeMemFile(29);
                byte        bySemiRandomNotProtocolMarker = GetSemiRandomNotProtocolMarker();
                fileRequest.WriteUInt8(bySemiRandomNotProtocolMarker);
                fileRequest.WriteUInt32(randomKeyPart_);
                fileRequest.WriteUInt32(MuleConstants.MAGICVALUE_SYNC);
                byte bySupportedEncryptionMethod = Convert.ToByte(EncryptionMethodsEnum.ENM_OBFUSCATION); // we do not support any further encryption in this version
                fileRequest.WriteUInt8(bySupportedEncryptionMethod);
                fileRequest.WriteUInt8(bySupportedEncryptionMethod);                                      // so we also prefer this one
                byte byPadding = Convert.ToByte(MpdUtilities.GetRandomUInt8() %
                                                (MuleApplication.Instance.Preference.CryptTCPPaddingLength + 1));
                fileRequest.WriteUInt8(byPadding);
                for (int i = 0; i < byPadding; i++)
                {
                    fileRequest.WriteUInt8(MpdUtilities.GetRandomUInt8());
                }

                negotiatingState_   = NegotiatingStateEnum.ONS_BASIC_CLIENTB_MAGICVALUE;
                streamCryptState_   = StreamCryptStateEnum.ECS_NEGOTIATING;
                receiveBytesWanted_ = 4;

                SendNegotiatingData(fileRequest.Buffer, (uint)fileRequest.Length, 5);
            }
            else if (streamCryptState_ == StreamCryptStateEnum.ECS_PENDING_SERVER)
            {
                SafeMemFile fileRequest = MpdObjectManager.CreateSafeMemFile(113);
                byte        bySemiRandomNotProtocolMarker = GetSemiRandomNotProtocolMarker();
                fileRequest.WriteUInt8(bySemiRandomNotProtocolMarker);

                cryptDHA_.genRandomBits((int)MuleConstants.DHAGREEMENT_A_BITS, new Random()); // our random a
                BigInteger cryptDHPrime =
                    new BigInteger(dh768_p_, (int)MuleConstants.PRIMESIZE_BYTES);             // our fixed prime
                // calculate g^a % p
                BigInteger cryptDHGexpAmodP =
                    new BigInteger(2).modPow(cryptDHA_, cryptDHPrime);

                // put the result into a buffer
                byte[] aBuffer = new byte[MuleConstants.PRIMESIZE_BYTES];
                Array.Copy(cryptDHGexpAmodP.getBytes(), aBuffer, MuleConstants.PRIMESIZE_BYTES);

                fileRequest.Write(aBuffer);
                byte byPadding = (byte)(MpdUtilities.GetRandomUInt8() % 16); // add random padding
                fileRequest.WriteUInt8(byPadding);
                for (int i = 0; i < byPadding; i++)
                {
                    fileRequest.WriteUInt8(MpdUtilities.GetRandomUInt8());
                }

                negotiatingState_   = NegotiatingStateEnum.ONS_BASIC_SERVER_DHANSWER;
                streamCryptState_   = StreamCryptStateEnum.ECS_NEGOTIATING;
                receiveBytesWanted_ = 96;

                SendNegotiatingData(fileRequest.Buffer, (uint)fileRequest.Length, (uint)fileRequest.Length);
            }
            else
            {
                Debug.Assert(false);
                streamCryptState_ = StreamCryptStateEnum.ECS_NONE;
                return;
            }
        }