| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Security.Authentication.ExtendedProtection; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Configuration |
| | | 13 | | { |
| | | 14 | | internal static class ConfigurationChannelBindingUtility |
| | | 15 | | { |
| | 0 | 16 | | private static readonly ExtendedProtectionPolicy s_disabledPolicy = new ExtendedProtectionPolicy(PolicyEnforceme |
| | 0 | 17 | | private static readonly ExtendedProtectionPolicy s_defaultPolicy = s_disabledPolicy; |
| | | 18 | | |
| | | 19 | | public static bool IsDefaultPolicy(ExtendedProtectionPolicy policy) |
| | | 20 | | { |
| | 0 | 21 | | return ReferenceEquals(policy, s_defaultPolicy); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static void CopyFrom(ExtendedProtectionPolicyElement source, ExtendedProtectionPolicyElement destination) |
| | | 25 | | { |
| | 0 | 26 | | destination.PolicyEnforcement = source.PolicyEnforcement; |
| | 0 | 27 | | destination.ProtectionScenario = source.ProtectionScenario; |
| | 0 | 28 | | destination.CustomServiceNames.Clear(); |
| | 0 | 29 | | foreach (ServiceNameElement sourceEntry in source.CustomServiceNames) |
| | | 30 | | { |
| | 0 | 31 | | ServiceNameElement entry = new ServiceNameElement(); |
| | 0 | 32 | | entry.Name = sourceEntry.Name; |
| | 0 | 33 | | destination.CustomServiceNames.Add(entry); |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static void InitializeFrom(ExtendedProtectionPolicy source, ExtendedProtectionPolicyElement destination) |
| | | 38 | | { |
| | 0 | 39 | | if (!IsDefaultPolicy(source)) |
| | | 40 | | { |
| | 0 | 41 | | destination.PolicyEnforcement = source.PolicyEnforcement; |
| | 0 | 42 | | destination.ProtectionScenario = source.ProtectionScenario; |
| | 0 | 43 | | destination.CustomServiceNames.Clear(); |
| | | 44 | | |
| | 0 | 45 | | if (source.CustomServiceNames != null) |
| | | 46 | | { |
| | 0 | 47 | | foreach (string name in source.CustomServiceNames) |
| | | 48 | | { |
| | 0 | 49 | | ServiceNameElement entry = new ServiceNameElement(); |
| | 0 | 50 | | entry.Name = name; |
| | 0 | 51 | | destination.CustomServiceNames.Add(entry); |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public static ExtendedProtectionPolicy BuildPolicy(ExtendedProtectionPolicyElement configurationPolicy) |
| | | 58 | | { |
| | | 59 | | //using this pattern allows us to have a different default policy |
| | | 60 | | //than the NCL team chooses. |
| | 6 | 61 | | if (configurationPolicy.ElementInformation.IsPresent) |
| | | 62 | | { |
| | 1 | 63 | | return configurationPolicy.BuildPolicy(); |
| | | 64 | | } |
| | | 65 | | else |
| | | 66 | | { |
| | 5 | 67 | | return ChannelBindingUtility.DefaultPolicy; |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public static ChannelBinding GetToken(SslStream stream) |
| | | 72 | | { |
| | 0 | 73 | | return GetToken(stream.TransportContext); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public static ChannelBinding GetToken(TransportContext context) |
| | | 77 | | { |
| | 0 | 78 | | ChannelBinding token = null; |
| | 0 | 79 | | if (context != null) |
| | | 80 | | { |
| | 0 | 81 | | token = context.GetChannelBinding(ChannelBindingKind.Endpoint); |
| | | 82 | | } |
| | 0 | 83 | | return token; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public static ChannelBinding DuplicateToken(ChannelBinding source) |
| | | 87 | | { |
| | 0 | 88 | | if (source == null) |
| | | 89 | | { |
| | 0 | 90 | | return null; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | return DuplicatedChannelBinding.CreateCopy(source); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public static void TryAddToMessage(ChannelBinding channelBindingToken, Message message, bool messagePropertyOwns |
| | | 97 | | { |
| | 0 | 98 | | if (channelBindingToken != null) |
| | | 99 | | { |
| | 0 | 100 | | ChannelBindingMessageProperty property = new ChannelBindingMessageProperty(channelBindingToken, messageP |
| | 0 | 101 | | property.AddTo(message); |
| | 0 | 102 | | ((IDisposable)property).Dispose(); //message.Properties.Add() creates a copy... |
| | | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | //does not validate the ExtendedProtectionPolicy.CustomServiceNames collections on the policies |
| | | 107 | | public static bool AreEqual(ExtendedProtectionPolicy policy1, ExtendedProtectionPolicy policy2) |
| | | 108 | | { |
| | 0 | 109 | | if (policy1 == null) |
| | | 110 | | { |
| | 0 | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policy1)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (policy2 == null) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policy2)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (policy1.PolicyEnforcement == PolicyEnforcement.Never && policy2.PolicyEnforcement == PolicyEnforcement.N |
| | | 120 | | { |
| | 0 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (policy1.PolicyEnforcement != policy2.PolicyEnforcement) |
| | | 125 | | { |
| | 0 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | if (policy1.ProtectionScenario != policy2.ProtectionScenario) |
| | | 130 | | { |
| | 0 | 131 | | return false; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if (policy1.CustomChannelBinding != policy2.CustomChannelBinding) |
| | | 135 | | { |
| | 0 | 136 | | return false; |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | public static bool IsSubset(ServiceNameCollection primaryList, ServiceNameCollection subset) |
| | | 143 | | { |
| | 0 | 144 | | bool result = false; |
| | 0 | 145 | | if (subset == null || subset.Count == 0) |
| | | 146 | | { |
| | 0 | 147 | | result = true; |
| | | 148 | | } |
| | 0 | 149 | | else if (primaryList == null || primaryList.Count < subset.Count) |
| | | 150 | | { |
| | 0 | 151 | | result = false; |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | ServiceNameCollection merged = primaryList.Merge(subset); |
| | | 156 | | |
| | | 157 | | //The merge routine only adds an entry if it is unique. |
| | 0 | 158 | | result = (merged.Count == primaryList.Count); |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | return result; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | public static void Dispose(ref ChannelBinding channelBinding) |
| | | 165 | | { |
| | | 166 | | // Explicitly cast to IDisposable to avoid the SecurityException. |
| | 0 | 167 | | IDisposable disposable = (IDisposable)channelBinding; |
| | 0 | 168 | | channelBinding = null; |
| | 0 | 169 | | disposable?.Dispose(); |
| | | 170 | | |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | private class DuplicatedChannelBinding : ChannelBinding |
| | | 174 | | { |
| | | 175 | | private int _size; |
| | | 176 | | |
| | 0 | 177 | | private DuplicatedChannelBinding() |
| | | 178 | | { |
| | | 179 | | |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | public override int Size |
| | | 183 | | { |
| | | 184 | | |
| | 0 | 185 | | get { return _size; } |
| | | 186 | | } |
| | | 187 | | internal static ChannelBinding CreateCopy(ChannelBinding source) |
| | | 188 | | { |
| | | 189 | | Fx.Assert(source != null, "source ChannelBinding should have been checked for null previously"); |
| | | 190 | | |
| | 0 | 191 | | if (source.IsInvalid || source.IsClosed) |
| | | 192 | | { |
| | 0 | 193 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(source.GetType |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | if (source.Size <= 0) |
| | | 197 | | { |
| | 0 | 198 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("source.Si |
| | 0 | 199 | | SR.Format(SRCommon.ValueMustBePositive))); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | //Instantiate the SafeHandle before trying to allocate the native memory |
| | 0 | 203 | | DuplicatedChannelBinding duplicate = new DuplicatedChannelBinding(); |
| | | 204 | | |
| | | 205 | | //allocate the native memory and make a deep copy of the original. |
| | 0 | 206 | | duplicate.Initialize(source); |
| | | 207 | | |
| | 0 | 208 | | return duplicate; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private unsafe void Initialize(ChannelBinding source) |
| | | 212 | | { |
| | | 213 | | //allocates the memory pointed to by this.handle |
| | | 214 | | //and sets this.size after allocation succeeds. |
| | 0 | 215 | | AllocateMemory(source.Size); |
| | | 216 | | |
| | 0 | 217 | | byte* sourceBuffer = (byte*)source.DangerousGetHandle().ToPointer(); |
| | 0 | 218 | | byte* destinationBuffer = (byte*)handle.ToPointer(); |
| | | 219 | | |
| | 0 | 220 | | for (int i = 0; i < source.Size; i++) |
| | | 221 | | { |
| | 0 | 222 | | destinationBuffer[i] = sourceBuffer[i]; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | _size = source.Size; |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | private void AllocateMemory(int bytesToAllocate) |
| | | 229 | | { |
| | | 230 | | Fx.Assert(bytesToAllocate > 0, "bytesToAllocate must be positive"); |
| | | 231 | | |
| | 0 | 232 | | base.SetHandle(Marshal.AllocHGlobal(bytesToAllocate)); |
| | | 233 | | |
| | 0 | 234 | | } |
| | | 235 | | |
| | | 236 | | protected override bool ReleaseHandle() |
| | | 237 | | { |
| | 0 | 238 | | Marshal.FreeHGlobal(handle); |
| | 0 | 239 | | base.SetHandle(IntPtr.Zero); |
| | 0 | 240 | | return true; |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | } |