public void CheckChannelBinding(ChannelBinding channelBinding)
 {
     if (channelBinding != null)
     {
         Assert.True(!channelBinding.IsInvalid, "Channel binding token should be marked as a valid SafeHandle.");
         Assert.True(channelBinding.Size > 0, "Number of bytes in a valid channel binding token should be greater than zero.");
         var bytes = new byte[channelBinding.Size];
         Marshal.Copy(channelBinding.DangerousGetHandle(), bytes, 0, channelBinding.Size);
         Assert.Equal(channelBinding.Size, bytes.Length);
     }
 }
Example #2
0
        protected ExtendedProtectionPolicy(SerializationInfo info, StreamingContext context)
        {
            policyEnforcement  = (PolicyEnforcement)info.GetInt32(policyEnforcementName);
            protectionScenario = (ProtectionScenario)info.GetInt32(protectionScenarioName);
            customServiceNames = (ServiceNameCollection)info.GetValue(customServiceNamesName, typeof(ServiceNameCollection));

            byte[] channelBindingData = (byte[])info.GetValue(customChannelBindingName, typeof(byte[]));
            if (channelBindingData != null)
            {
                customChannelBinding = SafeLocalFreeChannelBinding.LocalAlloc(channelBindingData.Length);
                Marshal.Copy(channelBindingData, 0, customChannelBinding.DangerousGetHandle(), channelBindingData.Length);
            }
        }
Example #3
0
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(policyEnforcementName, (int)policyEnforcement);
            info.AddValue(protectionScenarioName, (int)protectionScenario);
            info.AddValue(customServiceNamesName, customServiceNames, typeof(ServiceNameCollection));

            if (customChannelBinding == null)
            {
                info.AddValue(customChannelBindingName, null, typeof(byte[]));
            }
            else
            {
                byte[] channelBindingData = new byte[customChannelBinding.Size];
                Marshal.Copy(customChannelBinding.DangerousGetHandle(), channelBindingData, 0, customChannelBinding.Size);
                info.AddValue(customChannelBindingName, channelBindingData, typeof(byte[]));
            }
        }
Example #4
0
        //
        // Adapted from ComputeGssBindHash() in ds\security\protocols\sspcommon\sspbindings.cxx
        //
        // The formatted binding is:
        //   1. the initiator type and length
        //   2. the initiator data, if any
        //   3. the acceptor type and length
        //   4. the acceptor data, if any
        //   5. the application data length
        //   6. the application data, if any
        //
        private static byte[] formatChannelBindingForHash(ChannelBinding binding)
        {
            int initiatorType = Marshal.ReadInt32(binding.DangerousGetHandle(), InitiatorTypeOffset);
            int initiatorLength = Marshal.ReadInt32(binding.DangerousGetHandle(), InitiatorLengthOffset);
            int acceptorType = Marshal.ReadInt32(binding.DangerousGetHandle(), AcceptorTypeOffset);
            int acceptorLength = Marshal.ReadInt32(binding.DangerousGetHandle(), AcceptorLengthOffset);
            int applicationDataLength = Marshal.ReadInt32(binding.DangerousGetHandle(), ApplicationDataLengthOffset);

            byte[] formattedData = new byte[MinimumFormattedBindingLength + initiatorLength + acceptorLength + applicationDataLength];

            BitConverter.GetBytes(initiatorType).CopyTo(formattedData, 0);
            BitConverter.GetBytes(initiatorLength).CopyTo(formattedData, SizeOfInt);

            int offset = 2 * SizeOfInt;
            if (initiatorLength > 0)
            {
                int initiatorOffset = Marshal.ReadInt32(binding.DangerousGetHandle(), InitiatorOffsetOffset);
                Marshal.Copy(IntPtrHelper.Add(binding.DangerousGetHandle(), initiatorOffset), formattedData, offset, initiatorLength);
                offset += initiatorLength;
            }

            BitConverter.GetBytes(acceptorType).CopyTo(formattedData, offset);
            BitConverter.GetBytes(acceptorLength).CopyTo(formattedData, offset + SizeOfInt);

            offset += 2 * SizeOfInt;
            if (acceptorLength > 0)
            {
                int acceptorOffset = Marshal.ReadInt32(binding.DangerousGetHandle(), AcceptorOffsetOffset);
                Marshal.Copy(IntPtrHelper.Add(binding.DangerousGetHandle(), acceptorOffset), formattedData, offset, acceptorLength);
                offset += acceptorLength;
            }

            BitConverter.GetBytes(applicationDataLength).CopyTo(formattedData, offset);

            offset += SizeOfInt;
            if (applicationDataLength > 0)
            {
                int applicationDataOffset = Marshal.ReadInt32(binding.DangerousGetHandle(), ApplicationDataOffsetOffset);
                Marshal.Copy(IntPtrHelper.Add(binding.DangerousGetHandle(), applicationDataOffset), formattedData, offset, applicationDataLength);
            }

            return formattedData;
        }
 private unsafe void Initialize(ChannelBinding source)
 {
     this.AllocateMemory(source.Size);
     byte* numPtr = (byte*) source.DangerousGetHandle().ToPointer();
     byte* numPtr2 = (byte*) this.handle.ToPointer();
     for (int i = 0; i < source.Size; i++)
     {
         numPtr2[i] = numPtr[i];
     }
     this.size = source.Size;
 }
        protected ExtendedProtectionPolicy(SerializationInfo info, StreamingContext context)
        {
            policyEnforcement = (PolicyEnforcement)info.GetInt32(policyEnforcementName);
            protectionScenario = (ProtectionScenario)info.GetInt32(protectionScenarioName);
            customServiceNames = (ServiceNameCollection)info.GetValue(customServiceNamesName, typeof(ServiceNameCollection));

            byte[] channelBindingData = (byte[])info.GetValue(customChannelBindingName, typeof(byte[]));
            if (channelBindingData != null)
            {
                customChannelBinding = SafeLocalFreeChannelBinding.LocalAlloc(channelBindingData.Length);
                Marshal.Copy(channelBindingData, 0, customChannelBinding.DangerousGetHandle(), channelBindingData.Length);
            }
        }
            unsafe void Initialize(ChannelBinding source)
            {
                //allocates the memory pointed to by this.handle
                //and sets this.size after allocation succeeds.
                AllocateMemory(source.Size);

                byte* sourceBuffer = (byte*)source.DangerousGetHandle().ToPointer();
                byte* destinationBuffer = (byte*)this.handle.ToPointer();

                for (int i = 0; i < source.Size; i++)
                {
                    destinationBuffer[i] = sourceBuffer[i];
                }

                this.size = source.Size;
            }