| | | 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.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.Dispatcher; |
| | | 12 | | using CoreWCF.IdentityModel; |
| | | 13 | | using CoreWCF.IdentityModel.Claims; |
| | | 14 | | using CoreWCF.IdentityModel.Policy; |
| | | 15 | | using CoreWCF.Runtime; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Security.Tokens |
| | | 18 | | { |
| | | 19 | | internal struct SecurityContextCookieSerializer |
| | | 20 | | { |
| | | 21 | | private const int SupportedPersistanceVersion = 1; |
| | | 22 | | private readonly SecurityStateEncoder _securityStateEncoder; |
| | | 23 | | private readonly IList<Type> _knownTypes; |
| | | 24 | | |
| | | 25 | | public SecurityContextCookieSerializer(SecurityStateEncoder securityStateEncoder, IList<Type> knownTypes) |
| | | 26 | | { |
| | 133 | 27 | | _securityStateEncoder = securityStateEncoder ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument |
| | 133 | 28 | | _knownTypes = knownTypes ?? new List<Type>(); |
| | 133 | 29 | | } |
| | | 30 | | |
| | | 31 | | private SecurityContextSecurityToken DeserializeContext(byte[] serializedContext, byte[] cookieBlob, string id, |
| | | 32 | | { |
| | 0 | 33 | | SctClaimDictionary dictionary = SctClaimDictionary.Instance; |
| | 0 | 34 | | XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(serializedContext, 0, serializedContext. |
| | 0 | 35 | | int cookieVersion = -1; |
| | 0 | 36 | | UniqueId cookieContextId = null; |
| | 0 | 37 | | DateTime effectiveTime = SecurityUtils.MinUtcDateTime; |
| | 0 | 38 | | DateTime expiryTime = SecurityUtils.MaxUtcDateTime; |
| | 0 | 39 | | byte[] key = null; |
| | 0 | 40 | | string localId = null; |
| | 0 | 41 | | UniqueId keyGeneration = null; |
| | 0 | 42 | | DateTime keyEffectiveTime = SecurityUtils.MinUtcDateTime; |
| | 0 | 43 | | DateTime keyExpirationTime = SecurityUtils.MaxUtcDateTime; |
| | 0 | 44 | | List<ClaimSet> claimSets = null; |
| | 0 | 45 | | IList<IIdentity> identities = null; |
| | 0 | 46 | | bool isCookie = true; |
| | | 47 | | |
| | 0 | 48 | | reader.ReadFullStartElement(dictionary.SecurityContextSecurityToken, dictionary.EmptyString); |
| | | 49 | | |
| | 0 | 50 | | while (reader.IsStartElement()) |
| | | 51 | | { |
| | 0 | 52 | | if (reader.IsStartElement(dictionary.Version, dictionary.EmptyString)) |
| | | 53 | | { |
| | 0 | 54 | | cookieVersion = reader.ReadElementContentAsInt(); |
| | | 55 | | } |
| | 0 | 56 | | else if (reader.IsStartElement(dictionary.ContextId, dictionary.EmptyString)) |
| | | 57 | | { |
| | 0 | 58 | | cookieContextId = reader.ReadElementContentAsUniqueId(); |
| | | 59 | | } |
| | 0 | 60 | | else if (reader.IsStartElement(dictionary.Id, dictionary.EmptyString)) |
| | | 61 | | { |
| | 0 | 62 | | localId = reader.ReadElementContentAsString(); |
| | | 63 | | } |
| | 0 | 64 | | else if (reader.IsStartElement(dictionary.EffectiveTime, dictionary.EmptyString)) |
| | | 65 | | { |
| | 0 | 66 | | effectiveTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 67 | | } |
| | 0 | 68 | | else if (reader.IsStartElement(dictionary.ExpiryTime, dictionary.EmptyString)) |
| | | 69 | | { |
| | 0 | 70 | | expiryTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 71 | | } |
| | 0 | 72 | | else if (reader.IsStartElement(dictionary.Key, dictionary.EmptyString)) |
| | | 73 | | { |
| | 0 | 74 | | key = reader.ReadElementContentAsBase64(); |
| | | 75 | | } |
| | 0 | 76 | | else if (reader.IsStartElement(dictionary.KeyGeneration, dictionary.EmptyString)) |
| | | 77 | | { |
| | 0 | 78 | | keyGeneration = reader.ReadElementContentAsUniqueId(); |
| | | 79 | | } |
| | 0 | 80 | | else if (reader.IsStartElement(dictionary.KeyEffectiveTime, dictionary.EmptyString)) |
| | | 81 | | { |
| | 0 | 82 | | keyEffectiveTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 83 | | } |
| | 0 | 84 | | else if (reader.IsStartElement(dictionary.KeyExpiryTime, dictionary.EmptyString)) |
| | | 85 | | { |
| | 0 | 86 | | keyExpirationTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc); |
| | | 87 | | } |
| | 0 | 88 | | else if (reader.IsStartElement(dictionary.Identities, dictionary.EmptyString)) |
| | | 89 | | { |
| | 0 | 90 | | identities = SctClaimSerializer.DeserializeIdentities(reader, dictionary, DataContractSerializerDefa |
| | | 91 | | } |
| | 0 | 92 | | else if (reader.IsStartElement(dictionary.ClaimSets, dictionary.EmptyString)) |
| | | 93 | | { |
| | 0 | 94 | | reader.ReadStartElement(); |
| | | 95 | | |
| | 0 | 96 | | DataContractSerializer claimSetSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(C |
| | 0 | 97 | | DataContractSerializer claimSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(Clai |
| | 0 | 98 | | claimSets = new List<ClaimSet>(1); |
| | 0 | 99 | | while (reader.IsStartElement()) |
| | | 100 | | { |
| | 0 | 101 | | claimSets.Add(SctClaimSerializer.DeserializeClaimSet(reader, dictionary, claimSetSerializer, cla |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | reader.ReadEndElement(); |
| | | 105 | | } |
| | 0 | 106 | | else if (reader.IsStartElement(dictionary.IsCookieMode, dictionary.EmptyString)) |
| | | 107 | | { |
| | 0 | 108 | | isCookie = reader.ReadElementString() == "1" ? true : false; |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 0 | 112 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieXmlParseError)); |
| | | 113 | | } |
| | | 114 | | } |
| | 0 | 115 | | reader.ReadEndElement(); |
| | 0 | 116 | | if (cookieVersion != SupportedPersistanceVersion) |
| | | 117 | | { |
| | 0 | 118 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Seriali |
| | | 119 | | } |
| | 0 | 120 | | if (cookieContextId == null) |
| | | 121 | | { |
| | 0 | 122 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieValueMissingOrIncorrect, nameof(cookieContextId))); |
| | | 123 | | } |
| | 0 | 124 | | if (key == null || key.Length == 0) |
| | | 125 | | { |
| | 0 | 126 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieValueMissingOrIncorrect, nameof(key))); |
| | | 127 | | } |
| | 0 | 128 | | if (localId != id) |
| | | 129 | | { |
| | 0 | 130 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieValueMissingOrIncorrect, nameof(id))); |
| | | 131 | | } |
| | | 132 | | List<IAuthorizationPolicy> authorizationPolicies; |
| | 0 | 133 | | if (claimSets != null) |
| | | 134 | | { |
| | 0 | 135 | | authorizationPolicies = new List<IAuthorizationPolicy>(1) |
| | 0 | 136 | | { |
| | 0 | 137 | | new SctUnconditionalPolicy(identities, claimSets, expiryTime) |
| | 0 | 138 | | }; |
| | | 139 | | } |
| | | 140 | | else |
| | | 141 | | { |
| | 0 | 142 | | authorizationPolicies = null; |
| | | 143 | | } |
| | 0 | 144 | | return new SecurityContextSecurityToken(cookieContextId, localId, key, effectiveTime, expiryTime, |
| | 0 | 145 | | authorizationPolicies?.AsReadOnly(), isCookie, cookieBlob, keyGeneration, keyEffectiveTime, keyExpiratio |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public byte[] CreateCookieFromSecurityContext(UniqueId contextId, string id, byte[] key, DateTime tokenEffective |
| | | 149 | | DateTime tokenExpirationTime, UniqueId keyGeneration, DateTime keyEffectiveTime, DateTime keyExpirationTime, |
| | | 150 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies) |
| | | 151 | | { |
| | 0 | 152 | | if (contextId == null) |
| | | 153 | | { |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)); |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | if (key == null) |
| | | 158 | | { |
| | 0 | 159 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | MemoryStream stream = new MemoryStream(); |
| | 0 | 163 | | XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream, SctClaimDictionary.Instance, nul |
| | | 164 | | |
| | 0 | 165 | | SctClaimDictionary dictionary = SctClaimDictionary.Instance; |
| | 0 | 166 | | writer.WriteStartElement(dictionary.SecurityContextSecurityToken, dictionary.EmptyString); |
| | 0 | 167 | | writer.WriteStartElement(dictionary.Version, dictionary.EmptyString); |
| | 0 | 168 | | writer.WriteValue(SupportedPersistanceVersion); |
| | 0 | 169 | | writer.WriteEndElement(); |
| | 0 | 170 | | if (id != null) |
| | | 171 | | { |
| | 0 | 172 | | writer.WriteElementString(dictionary.Id, dictionary.EmptyString, id); |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | XmlHelper.WriteElementStringAsUniqueId(writer, dictionary.ContextId, dictionary.EmptyString, contextId); |
| | | 176 | | |
| | 0 | 177 | | writer.WriteStartElement(dictionary.Key, dictionary.EmptyString); |
| | 0 | 178 | | writer.WriteBase64(key, 0, key.Length); |
| | 0 | 179 | | writer.WriteEndElement(); |
| | | 180 | | |
| | 0 | 181 | | if (keyGeneration != null) |
| | | 182 | | { |
| | 0 | 183 | | XmlHelper.WriteElementStringAsUniqueId(writer, dictionary.KeyGeneration, dictionary.EmptyString, keyGene |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | XmlHelper.WriteElementContentAsInt64(writer, dictionary.EffectiveTime, dictionary.EmptyString, tokenEffectiv |
| | 0 | 187 | | XmlHelper.WriteElementContentAsInt64(writer, dictionary.ExpiryTime, dictionary.EmptyString, tokenExpirationT |
| | 0 | 188 | | XmlHelper.WriteElementContentAsInt64(writer, dictionary.KeyEffectiveTime, dictionary.EmptyString, keyEffecti |
| | 0 | 189 | | XmlHelper.WriteElementContentAsInt64(writer, dictionary.KeyExpiryTime, dictionary.EmptyString, keyExpiration |
| | | 190 | | |
| | 0 | 191 | | AuthorizationContext authContext = null; |
| | 0 | 192 | | if (authorizationPolicies != null) |
| | | 193 | | { |
| | 0 | 194 | | authContext = AuthorizationContext.CreateDefaultAuthorizationContext(authorizationPolicies); |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | if (authContext != null && authContext.ClaimSets.Count != 0) |
| | | 198 | | { |
| | 0 | 199 | | DataContractSerializer identitySerializer = DataContractSerializerDefaults.CreateSerializer(typeof(IIden |
| | 0 | 200 | | DataContractSerializer claimSetSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(Claim |
| | 0 | 201 | | DataContractSerializer claimSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(Claim), |
| | 0 | 202 | | SctClaimSerializer.SerializeIdentities(authContext, dictionary, writer, identitySerializer); |
| | | 203 | | |
| | 0 | 204 | | writer.WriteStartElement(dictionary.ClaimSets, dictionary.EmptyString); |
| | 0 | 205 | | for (int i = 0; i < authContext.ClaimSets.Count; i++) |
| | | 206 | | { |
| | 0 | 207 | | SctClaimSerializer.SerializeClaimSet(authContext.ClaimSets[i], dictionary, writer, claimSetSerialize |
| | | 208 | | } |
| | 0 | 209 | | writer.WriteEndElement(); |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | writer.WriteEndElement(); |
| | 0 | 213 | | writer.Flush(); |
| | | 214 | | |
| | 0 | 215 | | byte[] serializedContext = stream.ToArray(); |
| | 0 | 216 | | return _securityStateEncoder.EncodeSecurityState(serializedContext); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | public SecurityContextSecurityToken CreateSecurityContextFromCookie(byte[] encodedCookie, UniqueId contextId, Un |
| | | 220 | | { |
| | 0 | 221 | | byte[] cookie = null; |
| | | 222 | | |
| | | 223 | | try |
| | | 224 | | { |
| | 0 | 225 | | cookie = _securityStateEncoder.DecodeSecurityState(encodedCookie); |
| | 0 | 226 | | } |
| | 0 | 227 | | catch (Exception e) |
| | | 228 | | { |
| | 0 | 229 | | if (Fx.IsFatal(e)) |
| | | 230 | | { |
| | 0 | 231 | | throw; |
| | | 232 | | } |
| | 0 | 233 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieBlobDecodeFailure), e); |
| | 0 | 234 | | } |
| | 0 | 235 | | SecurityContextSecurityToken sct = DeserializeContext(cookie, encodedCookie, id, quotas); |
| | 0 | 236 | | if (sct.ContextId != contextId) |
| | | 237 | | { |
| | 0 | 238 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieValueMissingOrIncorrect, nameof(contextId))); |
| | | 239 | | } |
| | 0 | 240 | | if (sct.KeyGeneration != generation) |
| | | 241 | | { |
| | 0 | 242 | | OnInvalidCookieFailure(SR.Format(SR.SctCookieValueMissingOrIncorrect, nameof(sct.KeyGeneration))); |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return sct; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | internal static void OnInvalidCookieFailure(string reason) |
| | | 249 | | { |
| | 0 | 250 | | OnInvalidCookieFailure(reason, null); |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | internal static void OnInvalidCookieFailure(string reason, Exception e) |
| | | 254 | | { |
| | 0 | 255 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.InvalidS |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | internal class SctUnconditionalPolicy : IAuthorizationPolicy |
| | | 259 | | { |
| | 0 | 260 | | private readonly SecurityUniqueId _id = SecurityUniqueId.Create(); |
| | | 261 | | private readonly IList<IIdentity> _identities; |
| | | 262 | | private readonly IList<ClaimSet> _claimSets; |
| | | 263 | | private readonly DateTime _expirationTime; |
| | | 264 | | |
| | 0 | 265 | | public SctUnconditionalPolicy(IList<IIdentity> identities, IList<ClaimSet> claimSets, DateTime expirationTim |
| | | 266 | | { |
| | 0 | 267 | | _identities = identities; |
| | 0 | 268 | | _claimSets = claimSets; |
| | 0 | 269 | | _expirationTime = expirationTime; |
| | 0 | 270 | | } |
| | | 271 | | |
| | | 272 | | public string Id |
| | | 273 | | { |
| | 0 | 274 | | get { return _id.Value; } |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | public ClaimSet Issuer |
| | | 278 | | { |
| | 0 | 279 | | get { return ClaimSet.System; } |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | public bool Evaluate(EvaluationContext evaluationContext, ref object state) |
| | | 283 | | { |
| | 0 | 284 | | for (int i = 0; i < _claimSets.Count; ++i) |
| | | 285 | | { |
| | 0 | 286 | | evaluationContext.AddClaimSet(this, _claimSets[i]); |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | if (_identities != null) |
| | | 290 | | { |
| | 0 | 291 | | if (!evaluationContext.Properties.TryGetValue(SecurityUtils.Identities, out object obj)) |
| | | 292 | | { |
| | 0 | 293 | | evaluationContext.Properties.Add(SecurityUtils.Identities, _identities); |
| | | 294 | | } |
| | | 295 | | else |
| | | 296 | | { |
| | | 297 | | // null if other overrides the property with something else |
| | 0 | 298 | | if (obj is List<IIdentity> dstIdentities) |
| | | 299 | | { |
| | 0 | 300 | | dstIdentities.AddRange(_identities); |
| | | 301 | | } |
| | | 302 | | } |
| | | 303 | | } |
| | 0 | 304 | | evaluationContext.RecordExpirationTime(_expirationTime); |
| | 0 | 305 | | return true; |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | } |