| | | 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.Xml; |
| | | 7 | | using CoreWCF.IdentityModel; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using CoreWCF.IdentityModel.Tokens; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | using CoreWCF.Security.Tokens; |
| | | 12 | | using TokenEntry = CoreWCF.Security.WSSecurityTokenSerializer.TokenEntry; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Security |
| | | 15 | | { |
| | | 16 | | internal abstract class WSSecureConversation : WSSecurityTokenSerializer.SerializerEntries |
| | | 17 | | { |
| | | 18 | | private readonly DerivedKeyTokenEntry _derivedKeyEntry; |
| | | 19 | | |
| | 132 | 20 | | protected WSSecureConversation(WSSecurityTokenSerializer tokenSerializer, int maxKeyDerivationOffset, int maxKey |
| | | 21 | | { |
| | 132 | 22 | | WSSecurityTokenSerializer = tokenSerializer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentN |
| | 132 | 23 | | _derivedKeyEntry = new DerivedKeyTokenEntry(this, maxKeyDerivationOffset, maxKeyDerivationLabelLength, maxKe |
| | 132 | 24 | | } |
| | | 25 | | |
| | | 26 | | public abstract SecureConversationDictionary SerializerDictionary |
| | | 27 | | { |
| | | 28 | | get; |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public WSSecurityTokenSerializer WSSecurityTokenSerializer { get; } |
| | | 32 | | |
| | | 33 | | public override void PopulateTokenEntries(IList<TokenEntry> tokenEntryList) |
| | | 34 | | { |
| | 132 | 35 | | if (tokenEntryList == null) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenEntryList)); |
| | | 38 | | } |
| | 132 | 39 | | tokenEntryList.Add(_derivedKeyEntry); |
| | 132 | 40 | | } |
| | | 41 | | |
| | | 42 | | public virtual bool IsAtDerivedKeyToken(XmlDictionaryReader reader) |
| | | 43 | | { |
| | 0 | 44 | | return _derivedKeyEntry.CanReadTokenCore(reader); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public virtual void ReadDerivedKeyTokenParameters(XmlDictionaryReader reader, SecurityTokenResolver tokenResolve |
| | | 48 | | { |
| | 0 | 49 | | _derivedKeyEntry.ReadDerivedKeyTokenParameters(reader, tokenResolver, out id, out derivationAlgorithm, out l |
| | 0 | 50 | | out length, out nonce, out offset, out generation, out tokenToDeriveIdentifier, out tokenToDerive); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public virtual SecurityToken CreateDerivedKeyToken(string id, string derivationAlgorithm, string label, int leng |
| | | 54 | | { |
| | 0 | 55 | | return _derivedKeyEntry.CreateDerivedKeyToken(id, derivationAlgorithm, label, length, nonce, offset, generat |
| | 0 | 56 | | tokenToDeriveIdentifier, tokenToDerive); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | public virtual string DerivationAlgorithm => SecurityAlgorithms.Psha1KeyDerivation; |
| | | 60 | | |
| | | 61 | | protected class DerivedKeyTokenEntry : WSSecurityTokenSerializer.TokenEntry |
| | | 62 | | { |
| | | 63 | | public const string DefaultLabel = "WS-SecureConversation"; |
| | | 64 | | private readonly WSSecureConversation _parent; |
| | | 65 | | private readonly int _maxKeyDerivationOffset; |
| | | 66 | | private readonly int _maxKeyDerivationLabelLength; |
| | | 67 | | private readonly int _maxKeyDerivationNonceLength; |
| | | 68 | | |
| | 132 | 69 | | public DerivedKeyTokenEntry(WSSecureConversation parent, int maxKeyDerivationOffset, int maxKeyDerivationLab |
| | | 70 | | { |
| | 132 | 71 | | _parent = parent ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parent)); |
| | 132 | 72 | | _maxKeyDerivationOffset = maxKeyDerivationOffset; |
| | 132 | 73 | | _maxKeyDerivationLabelLength = maxKeyDerivationLabelLength; |
| | 132 | 74 | | _maxKeyDerivationNonceLength = maxKeyDerivationNonceLength; |
| | 132 | 75 | | } |
| | | 76 | | |
| | 30 | 77 | | protected override XmlDictionaryString LocalName => _parent.SerializerDictionary.DerivedKeyToken; |
| | 30 | 78 | | protected override XmlDictionaryString NamespaceUri => _parent.SerializerDictionary.Namespace; |
| | 10 | 79 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(DerivedKeySecurityToken) }; } |
| | 0 | 80 | | public override string TokenTypeUri => _parent.SerializerDictionary.DerivedKeyTokenType.Value; |
| | 0 | 81 | | protected override string ValueTypeUri => null; |
| | | 82 | | |
| | | 83 | | public override SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedToken |
| | | 84 | | SecurityTokenReferenceStyle tokenReferenceStyle) |
| | | 85 | | { |
| | 0 | 86 | | TokenReferenceStyleHelper.Validate(tokenReferenceStyle); |
| | | 87 | | |
| | | 88 | | switch (tokenReferenceStyle) |
| | | 89 | | { |
| | | 90 | | case SecurityTokenReferenceStyle.Internal: |
| | 0 | 91 | | return CreateDirectReference(issuedTokenXml, UtilityStrings.IdAttribute, UtilityStrings.Namespac |
| | | 92 | | case SecurityTokenReferenceStyle.External: |
| | | 93 | | // DerivedKeys aren't referred to externally |
| | 0 | 94 | | return null; |
| | | 95 | | default: |
| | 0 | 96 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | // xml format |
| | | 101 | | //<DerivedKeyToken wsu:Id="..." wsse:Algorithm="..."> id required, alg optional (curr disallowed) |
| | | 102 | | // <SecurityTokenReference>...</SecurityTokenReference> - required |
| | | 103 | | // <Properties>...</Properties> - disallowed (optional in spec, but we disallow it) |
| | | 104 | | // choice begin - (schema requires a choice - we allow neither on read - we always write one) |
| | | 105 | | // <Generation>...</Generation> - optional |
| | | 106 | | // <Offset>...</Offset> - optional |
| | | 107 | | // choice end |
| | | 108 | | // <Length>...</Length> - optional - default 32 on read (default specified in spec, not in schema - we alwa |
| | | 109 | | // <Label>...</Label> - optional |
| | | 110 | | // <Nonce>...</Nonce> - required (optional in spec, but we require it) |
| | | 111 | | //</DerivedKeyToken> |
| | | 112 | | public virtual void ReadDerivedKeyTokenParameters(XmlDictionaryReader reader, SecurityTokenResolver tokenRes |
| | | 113 | | { |
| | 0 | 114 | | if (tokenResolver == null) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenResolver)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace); |
| | | 120 | | |
| | 0 | 121 | | derivationAlgorithm = reader.GetAttribute(XD.XmlSignatureDictionary.Algorithm, null); |
| | 0 | 122 | | if (derivationAlgorithm == null) |
| | | 123 | | { |
| | 0 | 124 | | derivationAlgorithm = _parent.DerivationAlgorithm; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | reader.ReadStartElement(); |
| | | 128 | | |
| | 0 | 129 | | tokenToDeriveIdentifier = null; |
| | 0 | 130 | | tokenToDerive = null; |
| | | 131 | | |
| | 0 | 132 | | if (reader.IsStartElement(XD.SecurityJan2004Dictionary.SecurityTokenReference, XD.SecurityJan2004Diction |
| | | 133 | | { |
| | 0 | 134 | | tokenToDeriveIdentifier = _parent.WSSecurityTokenSerializer.ReadKeyIdentifierClause(reader); |
| | 0 | 135 | | tokenResolver.TryResolveToken(tokenToDeriveIdentifier, out tokenToDerive); |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 0 | 139 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.DerivedKeyTo |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // no support for properties |
| | | 143 | | |
| | 0 | 144 | | generation = -1; |
| | 0 | 145 | | if (reader.IsStartElement(_parent.SerializerDictionary.Generation, _parent.SerializerDictionary.Namespac |
| | | 146 | | { |
| | 0 | 147 | | reader.ReadStartElement(); |
| | 0 | 148 | | generation = reader.ReadContentAsInt(); |
| | 0 | 149 | | reader.ReadEndElement(); |
| | 0 | 150 | | if (generation < 0) |
| | | 151 | | { |
| | 0 | 152 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.DerivedK |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | offset = -1; |
| | 0 | 157 | | if (reader.IsStartElement(_parent.SerializerDictionary.Offset, _parent.SerializerDictionary.Namespace)) |
| | | 158 | | { |
| | 0 | 159 | | reader.ReadStartElement(); |
| | 0 | 160 | | offset = reader.ReadContentAsInt(); |
| | 0 | 161 | | reader.ReadEndElement(); |
| | 0 | 162 | | if (offset < 0) |
| | | 163 | | { |
| | 0 | 164 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.DerivedK |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | length = DerivedKeySecurityToken.DefaultDerivedKeyLength; |
| | 0 | 169 | | if (reader.IsStartElement(_parent.SerializerDictionary.Length, _parent.SerializerDictionary.Namespace)) |
| | | 170 | | { |
| | 0 | 171 | | reader.ReadStartElement(); |
| | 0 | 172 | | length = reader.ReadContentAsInt(); |
| | 0 | 173 | | reader.ReadEndElement(); |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | if ((offset == -1) && (generation == -1)) |
| | | 177 | | { |
| | 0 | 178 | | offset = 0; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | // verify that the offset is not larger than the max allowed |
| | 0 | 182 | | DerivedKeySecurityToken.EnsureAcceptableOffset(offset, generation, length, _maxKeyDerivationOffset); |
| | | 183 | | |
| | 0 | 184 | | label = null; |
| | 0 | 185 | | if (reader.IsStartElement(_parent.SerializerDictionary.Label, _parent.SerializerDictionary.Namespace)) |
| | | 186 | | { |
| | 0 | 187 | | reader.ReadStartElement(); |
| | 0 | 188 | | label = reader.ReadString(); |
| | 0 | 189 | | reader.ReadEndElement(); |
| | | 190 | | } |
| | 0 | 191 | | if (label != null && label.Length > _maxKeyDerivationLabelLength) |
| | | 192 | | { |
| | 0 | 193 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | reader.ReadStartElement(_parent.SerializerDictionary.Nonce, _parent.SerializerDictionary.Namespace); |
| | 0 | 197 | | nonce = reader.ReadContentAsBase64(); |
| | 0 | 198 | | reader.ReadEndElement(); |
| | | 199 | | |
| | 0 | 200 | | if (nonce != null && nonce.Length > _maxKeyDerivationNonceLength) |
| | | 201 | | { |
| | 0 | 202 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | reader.ReadEndElement(); |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | public virtual SecurityToken CreateDerivedKeyToken(string id, string derivationAlgorithm, string label, int |
| | | 209 | | { |
| | 0 | 210 | | if (tokenToDerive == null) |
| | | 211 | | { |
| | 0 | 212 | | return new DerivedKeySecurityTokenStub(generation, offset, length, |
| | 0 | 213 | | label, nonce, tokenToDeriveIdentifier, derivationAlgorithm, id); |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 0 | 217 | | return new DerivedKeySecurityToken(generation, offset, length, |
| | 0 | 218 | | label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationAlgorithm, id); |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver) |
| | | 223 | | { |
| | 0 | 224 | | ReadDerivedKeyTokenParameters(reader, tokenResolver, out string id, out string derivationAlgorithm, out |
| | 0 | 225 | | out byte[] nonce, out int offset, out int generation, out SecurityKeyIdentifierClause tokenToDeriveI |
| | | 226 | | |
| | 0 | 227 | | return CreateDerivedKeyToken(id, derivationAlgorithm, label, length, nonce, offset, generation, |
| | 0 | 228 | | tokenToDeriveIdentifier, tokenToDerive); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token) |
| | | 232 | | { |
| | 0 | 233 | | DerivedKeySecurityToken derivedKeyToken = token as DerivedKeySecurityToken; |
| | 0 | 234 | | string serializerPrefix = _parent.SerializerDictionary.Prefix.Value; |
| | | 235 | | |
| | 0 | 236 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.DerivedKeyToken, _parent.Seriali |
| | 0 | 237 | | if (derivedKeyToken.Id != null) |
| | | 238 | | { |
| | 0 | 239 | | writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD. |
| | | 240 | | } |
| | 0 | 241 | | if (derivedKeyToken.KeyDerivationAlgorithm != _parent.DerivationAlgorithm) |
| | | 242 | | { |
| | 0 | 243 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR. |
| | | 244 | | } |
| | 0 | 245 | | _parent.WSSecurityTokenSerializer.WriteKeyIdentifierClause(writer, derivedKeyToken.TokenToDeriveIdentifi |
| | | 246 | | |
| | | 247 | | // Don't support Properties element |
| | 0 | 248 | | if (derivedKeyToken.Generation > 0 || derivedKeyToken.Offset > 0 || derivedKeyToken.Length != 32) |
| | | 249 | | { |
| | | 250 | | // this means they're both specified (offset must be gen * length) - we'll write generation |
| | 0 | 251 | | if (derivedKeyToken.Generation >= 0 && derivedKeyToken.Offset >= 0) |
| | | 252 | | { |
| | 0 | 253 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Generation, _parent.Seri |
| | 0 | 254 | | writer.WriteValue(derivedKeyToken.Generation); |
| | 0 | 255 | | writer.WriteEndElement(); |
| | | 256 | | } |
| | 0 | 257 | | else if (derivedKeyToken.Generation != -1) |
| | | 258 | | { |
| | 0 | 259 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Generation, _parent.Seri |
| | 0 | 260 | | writer.WriteValue(derivedKeyToken.Generation); |
| | 0 | 261 | | writer.WriteEndElement(); |
| | | 262 | | } |
| | 0 | 263 | | else if (derivedKeyToken.Offset != -1) |
| | | 264 | | { |
| | 0 | 265 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Offset, _parent.Serializ |
| | 0 | 266 | | writer.WriteValue(derivedKeyToken.Offset); |
| | 0 | 267 | | writer.WriteEndElement(); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | if (derivedKeyToken.Length != 32) |
| | | 271 | | { |
| | 0 | 272 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Length, _parent.Serializ |
| | 0 | 273 | | writer.WriteValue(derivedKeyToken.Length); |
| | 0 | 274 | | writer.WriteEndElement(); |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | if (derivedKeyToken.Label != null) |
| | | 279 | | { |
| | 0 | 280 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Generation, _parent.Serializ |
| | 0 | 281 | | writer.WriteString(derivedKeyToken.Label); |
| | 0 | 282 | | writer.WriteEndElement(); |
| | | 283 | | } |
| | 0 | 284 | | writer.WriteStartElement(serializerPrefix, _parent.SerializerDictionary.Nonce, _parent.SerializerDiction |
| | 0 | 285 | | writer.WriteBase64(derivedKeyToken.Nonce, 0, derivedKeyToken.Nonce.Length); |
| | 0 | 286 | | writer.WriteEndElement(); |
| | 0 | 287 | | writer.WriteEndElement(); |
| | 0 | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | protected abstract class SecurityContextTokenEntry : WSSecurityTokenSerializer.TokenEntry |
| | | 292 | | { |
| | | 293 | | private SecurityContextCookieSerializer _cookieSerializer; |
| | | 294 | | |
| | 132 | 295 | | public SecurityContextTokenEntry(WSSecureConversation parent, SecurityStateEncoder securityStateEncoder, ILi |
| | | 296 | | { |
| | 132 | 297 | | Parent = parent; |
| | 132 | 298 | | _cookieSerializer = new SecurityContextCookieSerializer(securityStateEncoder, knownClaimTypes); |
| | 132 | 299 | | } |
| | | 300 | | |
| | 180 | 301 | | protected WSSecureConversation Parent { get; } |
| | | 302 | | |
| | 30 | 303 | | protected override XmlDictionaryString LocalName => Parent.SerializerDictionary.SecurityContextToken; |
| | 30 | 304 | | protected override XmlDictionaryString NamespaceUri => Parent.SerializerDictionary.Namespace; |
| | 10 | 305 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(SecurityContextSecurityToken) }; |
| | 0 | 306 | | public override string TokenTypeUri => Parent.SerializerDictionary.SecurityContextTokenType.Value; |
| | 20 | 307 | | protected override string ValueTypeUri => null; |
| | | 308 | | |
| | | 309 | | public override SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedToken |
| | | 310 | | SecurityTokenReferenceStyle tokenReferenceStyle) |
| | | 311 | | { |
| | 0 | 312 | | TokenReferenceStyleHelper.Validate(tokenReferenceStyle); |
| | | 313 | | |
| | | 314 | | switch (tokenReferenceStyle) |
| | | 315 | | { |
| | | 316 | | case SecurityTokenReferenceStyle.Internal: |
| | 0 | 317 | | return CreateDirectReference(issuedTokenXml, UtilityStrings.IdAttribute, UtilityStrings.Namespac |
| | | 318 | | case SecurityTokenReferenceStyle.External: |
| | 0 | 319 | | UniqueId contextId = null; |
| | 0 | 320 | | UniqueId generation = null; |
| | 0 | 321 | | foreach (XmlNode node in issuedTokenXml.ChildNodes) |
| | | 322 | | { |
| | 0 | 323 | | if (node is XmlElement element) |
| | | 324 | | { |
| | 0 | 325 | | if (element.LocalName == Parent.SerializerDictionary.Identifier.Value && element.Namespa |
| | | 326 | | { |
| | 0 | 327 | | contextId = XmlHelper.ReadTextElementAsUniqueId(element); |
| | | 328 | | } |
| | 0 | 329 | | else if (CanReadGeneration(element)) |
| | | 330 | | { |
| | 0 | 331 | | generation = ReadGeneration(element); |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | } |
| | 0 | 335 | | return new SecurityContextKeyIdentifierClause(contextId, generation); |
| | | 336 | | default: |
| | 0 | 337 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof |
| | | 338 | | } |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | protected abstract bool CanReadGeneration(XmlDictionaryReader reader); |
| | | 342 | | protected abstract bool CanReadGeneration(XmlElement element); |
| | | 343 | | protected abstract UniqueId ReadGeneration(XmlDictionaryReader reader); |
| | | 344 | | protected abstract UniqueId ReadGeneration(XmlElement element); |
| | | 345 | | |
| | | 346 | | private SecurityContextSecurityToken TryResolveSecurityContextToken(UniqueId contextId, UniqueId generation, |
| | | 347 | | { |
| | 20 | 348 | | SecurityContextSecurityToken cachedSct = null; |
| | 20 | 349 | | sctCache = null; |
| | 20 | 350 | | if (tokenResolver is ISecurityContextSecurityTokenCache) |
| | | 351 | | { |
| | 0 | 352 | | sctCache = ((ISecurityContextSecurityTokenCache)tokenResolver); |
| | 0 | 353 | | cachedSct = sctCache.GetContext(contextId, generation); |
| | | 354 | | } |
| | 20 | 355 | | else if (tokenResolver is AggregateSecurityHeaderTokenResolver) |
| | | 356 | | { |
| | | 357 | | // We will see if we have a ISecurityContextSecurityTokenCache in the |
| | | 358 | | // AggregateTokenResolver. We will hold the reference to the first sctCache |
| | | 359 | | // we find. |
| | 20 | 360 | | AggregateSecurityHeaderTokenResolver aggregateTokenResolve = tokenResolver as AggregateSecurityHeade |
| | 40 | 361 | | for (int i = 0; i < aggregateTokenResolve.TokenResolvers.Count; ++i) |
| | | 362 | | { |
| | 20 | 363 | | if (!(aggregateTokenResolve.TokenResolvers[i] is ISecurityContextSecurityTokenCache oobTokenReso |
| | | 364 | | { |
| | | 365 | | continue; |
| | | 366 | | } |
| | 20 | 367 | | if (sctCache == null) |
| | | 368 | | { |
| | 20 | 369 | | sctCache = oobTokenResolver; |
| | | 370 | | } |
| | 20 | 371 | | cachedSct = oobTokenResolver.GetContext(contextId, generation); |
| | 20 | 372 | | if (cachedSct != null) |
| | | 373 | | { |
| | | 374 | | break; |
| | | 375 | | } |
| | | 376 | | } |
| | | 377 | | } |
| | 20 | 378 | | if (cachedSct == null) |
| | | 379 | | { |
| | 0 | 380 | | return null; |
| | | 381 | | } |
| | 20 | 382 | | else if (cachedSct.Id == id) |
| | | 383 | | { |
| | 20 | 384 | | return cachedSct; |
| | | 385 | | } |
| | | 386 | | else |
| | | 387 | | { |
| | 0 | 388 | | return new SecurityContextSecurityToken(cachedSct, id); |
| | | 389 | | } |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver) |
| | | 393 | | { |
| | 20 | 394 | | UniqueId generation = null; |
| | 20 | 395 | | bool isCookieMode = false; |
| | | 396 | | |
| | | 397 | | Fx.Assert(reader.NodeType == XmlNodeType.Element, ""); |
| | | 398 | | |
| | | 399 | | // check if there is an id |
| | 20 | 400 | | string id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace); |
| | | 401 | | |
| | 20 | 402 | | SecurityContextSecurityToken sct = null; |
| | | 403 | | |
| | | 404 | | // There needs to be at least a contextId in here. |
| | 20 | 405 | | reader.ReadFullStartElement(); |
| | 20 | 406 | | reader.MoveToStartElement(Parent.SerializerDictionary.Identifier, Parent.SerializerDictionary.Namespace) |
| | 20 | 407 | | UniqueId contextId = reader.ReadElementContentAsUniqueId(); |
| | 20 | 408 | | if (CanReadGeneration(reader)) |
| | | 409 | | { |
| | 0 | 410 | | generation = ReadGeneration(reader); |
| | | 411 | | } |
| | 20 | 412 | | if (reader.IsStartElement(Parent.SerializerDictionary.Cookie, XD.DotNetSecurityDictionary.Namespace)) |
| | | 413 | | { |
| | 0 | 414 | | isCookieMode = true; |
| | 0 | 415 | | sct = TryResolveSecurityContextToken(contextId, generation, id, tokenResolver, out ISecurityContextS |
| | 0 | 416 | | if (sct == null) |
| | | 417 | | { |
| | 0 | 418 | | byte[] encodedCookie = reader.ReadElementContentAsBase64(); |
| | 0 | 419 | | if (encodedCookie != null) |
| | | 420 | | { |
| | 0 | 421 | | sct = _cookieSerializer.CreateSecurityContextFromCookie(encodedCookie, contextId, generation |
| | 0 | 422 | | if (sctCache != null) |
| | | 423 | | { |
| | 0 | 424 | | sctCache.AddContext(sct); |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | } |
| | | 428 | | else |
| | | 429 | | { |
| | 0 | 430 | | reader.Skip(); |
| | | 431 | | } |
| | | 432 | | } |
| | 20 | 433 | | reader.ReadEndElement(); |
| | | 434 | | |
| | 20 | 435 | | if (contextId == null) |
| | | 436 | | { |
| | 0 | 437 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR. |
| | | 438 | | } |
| | | 439 | | |
| | 20 | 440 | | if (sct == null && !isCookieMode) |
| | | 441 | | { |
| | 20 | 442 | | sct = TryResolveSecurityContextToken(contextId, generation, id, tokenResolver, out ISecurityContextS |
| | | 443 | | } |
| | 20 | 444 | | if (sct == null) |
| | | 445 | | { |
| | 0 | 446 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new Exception(SR.Format(SR.SecurityConte |
| | | 447 | | } |
| | 20 | 448 | | return sct; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | protected virtual void WriteGeneration(XmlDictionaryWriter writer, SecurityContextSecurityToken sct) |
| | | 452 | | { |
| | 0 | 453 | | } |
| | | 454 | | |
| | | 455 | | public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token) |
| | | 456 | | { |
| | 10 | 457 | | SecurityContextSecurityToken sct = (token as SecurityContextSecurityToken); |
| | | 458 | | |
| | | 459 | | // serialize the name and any wsu:Id attribute |
| | 10 | 460 | | writer.WriteStartElement(Parent.SerializerDictionary.Prefix.Value, Parent.SerializerDictionary.SecurityC |
| | 10 | 461 | | if (sct.Id != null) |
| | | 462 | | { |
| | 10 | 463 | | writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD. |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | // serialize the context id |
| | 10 | 467 | | writer.WriteStartElement(Parent.SerializerDictionary.Prefix.Value, Parent.SerializerDictionary.Identifie |
| | 10 | 468 | | XmlHelper.WriteStringAsUniqueId(writer, sct.ContextId); |
| | 10 | 469 | | writer.WriteEndElement(); |
| | | 470 | | |
| | 10 | 471 | | WriteGeneration(writer, sct); |
| | | 472 | | |
| | | 473 | | // if cookie-mode, then it must have a cookie |
| | 10 | 474 | | if (sct.IsCookieMode) |
| | | 475 | | { |
| | 0 | 476 | | if (sct.CookieBlob == null) |
| | | 477 | | { |
| | 0 | 478 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | // if the token has a cookie, write it out |
| | 0 | 482 | | writer.WriteStartElement(XD.DotNetSecurityDictionary.Prefix.Value, Parent.SerializerDictionary.Cooki |
| | 0 | 483 | | writer.WriteBase64(sct.CookieBlob, 0, sct.CookieBlob.Length); |
| | 0 | 484 | | writer.WriteEndElement(); |
| | | 485 | | } |
| | | 486 | | |
| | 10 | 487 | | writer.WriteEndElement(); |
| | 10 | 488 | | } |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | public abstract class Driver : SecureConversationDriver |
| | | 492 | | { |
| | 131 | 493 | | public Driver() |
| | | 494 | | { |
| | 131 | 495 | | } |
| | | 496 | | |
| | | 497 | | protected abstract SecureConversationDictionary DriverDictionary |
| | | 498 | | { |
| | | 499 | | get; |
| | | 500 | | } |
| | | 501 | | |
| | 32 | 502 | | public override XmlDictionaryString IssueAction => DriverDictionary.RequestSecurityContextIssuance; |
| | | 503 | | |
| | 10 | 504 | | public override XmlDictionaryString IssueResponseAction => DriverDictionary.RequestSecurityContextIssuanceRe |
| | | 505 | | |
| | 0 | 506 | | public override XmlDictionaryString RenewNeededFaultCode => DriverDictionary.RenewNeededFaultCode; |
| | | 507 | | |
| | 0 | 508 | | public override XmlDictionaryString BadContextTokenFaultCode => DriverDictionary.BadContextTokenFaultCode; |
| | | 509 | | |
| | | 510 | | public override UniqueId GetSecurityContextTokenId(XmlDictionaryReader reader) |
| | | 511 | | { |
| | 20 | 512 | | if (reader == null) |
| | | 513 | | { |
| | 0 | 514 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 515 | | } |
| | | 516 | | |
| | 20 | 517 | | reader.ReadStartElement(DriverDictionary.SecurityContextToken, DriverDictionary.Namespace); |
| | 20 | 518 | | UniqueId contextId = XmlHelper.ReadElementStringAsUniqueId(reader, DriverDictionary.Identifier, DriverDi |
| | 20 | 519 | | while (reader.IsStartElement()) |
| | | 520 | | { |
| | 0 | 521 | | reader.Skip(); |
| | | 522 | | } |
| | 20 | 523 | | reader.ReadEndElement(); |
| | 20 | 524 | | return contextId; |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | public override bool IsAtSecurityContextToken(XmlDictionaryReader reader) |
| | | 528 | | { |
| | 40 | 529 | | if (reader == null) |
| | | 530 | | { |
| | 0 | 531 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 532 | | } |
| | | 533 | | |
| | 40 | 534 | | return reader.IsStartElement(DriverDictionary.SecurityContextToken, DriverDictionary.Namespace); |
| | | 535 | | } |
| | | 536 | | } |
| | | 537 | | } |
| | | 538 | | } |