WSSecurityTokenSerializer.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Collections.Generic;
  7. using System.IdentityModel.Selectors;
  8. using System.IdentityModel.Tokens;
  9. using System.Runtime;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Security.Tokens;
  12. using System.Xml;
  13. using System.ServiceModel.Diagnostics;
  14. using System.Diagnostics;
  15. public class WSSecurityTokenSerializer : SecurityTokenSerializer
  16. {
  17. const int DefaultMaximumKeyDerivationOffset = 64; // bytes
  18. const int DefaultMaximumKeyDerivationLabelLength = 128; // bytes
  19. const int DefaultMaximumKeyDerivationNonceLength = 128; // bytes
  20. static WSSecurityTokenSerializer instance;
  21. readonly bool emitBspRequiredAttributes;
  22. readonly SecurityVersion securityVersion;
  23. readonly List<SerializerEntries> serializerEntries;
  24. WSSecureConversation secureConversation;
  25. readonly List<TokenEntry> tokenEntries;
  26. int maximumKeyDerivationOffset;
  27. int maximumKeyDerivationLabelLength;
  28. int maximumKeyDerivationNonceLength;
  29. KeyInfoSerializer keyInfoSerializer;
  30. public WSSecurityTokenSerializer()
  31. : this(SecurityVersion.WSSecurity11)
  32. {
  33. }
  34. public WSSecurityTokenSerializer(bool emitBspRequiredAttributes)
  35. : this(SecurityVersion.WSSecurity11, emitBspRequiredAttributes)
  36. {
  37. }
  38. public WSSecurityTokenSerializer(SecurityVersion securityVersion)
  39. : this(securityVersion, false)
  40. {
  41. }
  42. public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes)
  43. : this(securityVersion, emitBspRequiredAttributes, null)
  44. {
  45. }
  46. public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer samlSerializer)
  47. : this(securityVersion, emitBspRequiredAttributes, samlSerializer, null, null)
  48. {
  49. }
  50. public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer samlSerializer, SecurityStateEncoder securityStateEncoder, IEnumerable<Type> knownTypes)
  51. : this(securityVersion, emitBspRequiredAttributes, samlSerializer, securityStateEncoder, knownTypes, DefaultMaximumKeyDerivationOffset, DefaultMaximumKeyDerivationLabelLength, DefaultMaximumKeyDerivationNonceLength)
  52. {
  53. }
  54. public WSSecurityTokenSerializer(SecurityVersion securityVersion, TrustVersion trustVersion, SecureConversationVersion secureConversationVersion, bool emitBspRequiredAttributes, SamlSerializer samlSerializer, SecurityStateEncoder securityStateEncoder, IEnumerable<Type> knownTypes)
  55. : this(securityVersion, trustVersion, secureConversationVersion, emitBspRequiredAttributes, samlSerializer, securityStateEncoder, knownTypes, DefaultMaximumKeyDerivationOffset, DefaultMaximumKeyDerivationLabelLength, DefaultMaximumKeyDerivationNonceLength)
  56. {
  57. }
  58. public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer samlSerializer, SecurityStateEncoder securityStateEncoder, IEnumerable<Type> knownTypes,
  59. int maximumKeyDerivationOffset, int maximumKeyDerivationLabelLength, int maximumKeyDerivationNonceLength)
  60. : this(securityVersion, TrustVersion.Default, SecureConversationVersion.Default, emitBspRequiredAttributes, samlSerializer, securityStateEncoder, knownTypes, maximumKeyDerivationOffset, maximumKeyDerivationLabelLength, maximumKeyDerivationNonceLength)
  61. {
  62. }
  63. public WSSecurityTokenSerializer(SecurityVersion securityVersion, TrustVersion trustVersion, SecureConversationVersion secureConversationVersion, bool emitBspRequiredAttributes, SamlSerializer samlSerializer, SecurityStateEncoder securityStateEncoder, IEnumerable<Type> knownTypes,
  64. int maximumKeyDerivationOffset, int maximumKeyDerivationLabelLength, int maximumKeyDerivationNonceLength)
  65. {
  66. if (securityVersion == null)
  67. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("securityVersion"));
  68. if (maximumKeyDerivationOffset < 0)
  69. {
  70. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maximumKeyDerivationOffset", SR.GetString(SR.ValueMustBeNonNegative)));
  71. }
  72. if (maximumKeyDerivationLabelLength < 0)
  73. {
  74. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maximumKeyDerivationLabelLength", SR.GetString(SR.ValueMustBeNonNegative)));
  75. }
  76. if (maximumKeyDerivationNonceLength <= 0)
  77. {
  78. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maximumKeyDerivationNonceLength", SR.GetString(SR.ValueMustBeGreaterThanZero)));
  79. }
  80. this.securityVersion = securityVersion;
  81. this.emitBspRequiredAttributes = emitBspRequiredAttributes;
  82. this.maximumKeyDerivationOffset = maximumKeyDerivationOffset;
  83. this.maximumKeyDerivationNonceLength = maximumKeyDerivationNonceLength;
  84. this.maximumKeyDerivationLabelLength = maximumKeyDerivationLabelLength;
  85. this.serializerEntries = new List<SerializerEntries>();
  86. if (secureConversationVersion == SecureConversationVersion.WSSecureConversationFeb2005)
  87. {
  88. this.secureConversation = new WSSecureConversationFeb2005(this, securityStateEncoder, knownTypes, maximumKeyDerivationOffset, maximumKeyDerivationLabelLength, maximumKeyDerivationNonceLength);
  89. }
  90. else if (secureConversationVersion == SecureConversationVersion.WSSecureConversation13)
  91. {
  92. this.secureConversation = new WSSecureConversationDec2005(this, securityStateEncoder, knownTypes, maximumKeyDerivationOffset, maximumKeyDerivationLabelLength, maximumKeyDerivationNonceLength);
  93. }
  94. else
  95. {
  96. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  97. }
  98. if (securityVersion == SecurityVersion.WSSecurity10)
  99. {
  100. this.serializerEntries.Add(new WSSecurityJan2004(this, samlSerializer));
  101. }
  102. else if (securityVersion == SecurityVersion.WSSecurity11)
  103. {
  104. this.serializerEntries.Add(new WSSecurityXXX2005(this, samlSerializer));
  105. }
  106. else
  107. {
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("securityVersion", SR.GetString(SR.MessageSecurityVersionOutOfRange)));
  109. }
  110. this.serializerEntries.Add(this.secureConversation);
  111. IdentityModel.TrustDictionary trustDictionary;
  112. if (trustVersion == TrustVersion.WSTrustFeb2005)
  113. {
  114. this.serializerEntries.Add(new WSTrustFeb2005(this));
  115. trustDictionary = new IdentityModel.TrustFeb2005Dictionary(new CollectionDictionary(DXD.TrustDec2005Dictionary.Feb2005DictionaryStrings));
  116. }
  117. else if (trustVersion == TrustVersion.WSTrust13)
  118. {
  119. this.serializerEntries.Add(new WSTrustDec2005(this));
  120. trustDictionary = new IdentityModel.TrustDec2005Dictionary(new CollectionDictionary(DXD.TrustDec2005Dictionary.Dec2005DictionaryString));
  121. }
  122. else
  123. {
  124. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  125. }
  126. this.tokenEntries = new List<TokenEntry>();
  127. for (int i = 0; i < this.serializerEntries.Count; ++i)
  128. {
  129. SerializerEntries serializerEntry = this.serializerEntries[i];
  130. serializerEntry.PopulateTokenEntries(this.tokenEntries);
  131. }
  132. IdentityModel.DictionaryManager dictionaryManager = new IdentityModel.DictionaryManager(ServiceModelDictionary.CurrentVersion);
  133. dictionaryManager.SecureConversationDec2005Dictionary = new IdentityModel.SecureConversationDec2005Dictionary(new CollectionDictionary(DXD.SecureConversationDec2005Dictionary.SecureConversationDictionaryStrings));
  134. dictionaryManager.SecurityAlgorithmDec2005Dictionary = new IdentityModel.SecurityAlgorithmDec2005Dictionary(new CollectionDictionary(DXD.SecurityAlgorithmDec2005Dictionary.SecurityAlgorithmDictionaryStrings));
  135. this.keyInfoSerializer = new WSKeyInfoSerializer(this.emitBspRequiredAttributes, dictionaryManager, trustDictionary, this, securityVersion, secureConversationVersion);
  136. }
  137. public static WSSecurityTokenSerializer DefaultInstance
  138. {
  139. get
  140. {
  141. if (instance == null)
  142. instance = new WSSecurityTokenSerializer();
  143. return instance;
  144. }
  145. }
  146. public bool EmitBspRequiredAttributes
  147. {
  148. get { return this.emitBspRequiredAttributes; }
  149. }
  150. public SecurityVersion SecurityVersion
  151. {
  152. get { return this.securityVersion; }
  153. }
  154. public int MaximumKeyDerivationOffset
  155. {
  156. get { return this.maximumKeyDerivationOffset; }
  157. }
  158. public int MaximumKeyDerivationLabelLength
  159. {
  160. get { return this.maximumKeyDerivationLabelLength; }
  161. }
  162. public int MaximumKeyDerivationNonceLength
  163. {
  164. get { return this.maximumKeyDerivationNonceLength; }
  165. }
  166. internal WSSecureConversation SecureConversation
  167. {
  168. get { return this.secureConversation; }
  169. }
  170. bool ShouldWrapException(Exception e)
  171. {
  172. if (Fx.IsFatal(e))
  173. {
  174. return false;
  175. }
  176. return ((e is ArgumentException) || (e is FormatException) || (e is InvalidOperationException));
  177. }
  178. protected override bool CanReadTokenCore(XmlReader reader)
  179. {
  180. XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
  181. for (int i = 0; i < this.tokenEntries.Count; i++)
  182. {
  183. TokenEntry tokenEntry = this.tokenEntries[i];
  184. if (tokenEntry.CanReadTokenCore(localReader))
  185. return true;
  186. }
  187. return false;
  188. }
  189. protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver)
  190. {
  191. XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
  192. for (int i = 0; i < this.tokenEntries.Count; i++)
  193. {
  194. TokenEntry tokenEntry = this.tokenEntries[i];
  195. if (tokenEntry.CanReadTokenCore(localReader))
  196. {
  197. try
  198. {
  199. return tokenEntry.ReadTokenCore(localReader, tokenResolver);
  200. }
  201. #pragma warning suppress 56500 // covered by FxCOP
  202. catch (Exception e)
  203. {
  204. if (!ShouldWrapException(e))
  205. {
  206. throw;
  207. }
  208. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ErrorDeserializingTokenXml), e));
  209. }
  210. }
  211. }
  212. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.CannotReadToken, reader.LocalName, reader.NamespaceURI, localReader.GetAttribute(XD.SecurityJan2004Dictionary.ValueType, null))));
  213. }
  214. protected override bool CanWriteTokenCore(SecurityToken token)
  215. {
  216. for (int i = 0; i < this.tokenEntries.Count; i++)
  217. {
  218. TokenEntry tokenEntry = this.tokenEntries[i];
  219. if (tokenEntry.SupportsCore(token.GetType()))
  220. return true;
  221. }
  222. return false;
  223. }
  224. protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
  225. {
  226. bool wroteToken = false;
  227. XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
  228. if (token.GetType() == typeof(ProviderBackedSecurityToken))
  229. {
  230. token = (token as ProviderBackedSecurityToken).Token;
  231. }
  232. for (int i = 0; i < this.tokenEntries.Count; i++)
  233. {
  234. TokenEntry tokenEntry = this.tokenEntries[i];
  235. if (tokenEntry.SupportsCore(token.GetType()))
  236. {
  237. try
  238. {
  239. tokenEntry.WriteTokenCore(localWriter, token);
  240. }
  241. #pragma warning suppress 56500 // covered by FxCOP
  242. catch (Exception e)
  243. {
  244. if (!ShouldWrapException(e))
  245. {
  246. throw;
  247. }
  248. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ErrorSerializingSecurityToken), e));
  249. }
  250. wroteToken = true;
  251. break;
  252. }
  253. }
  254. if (!wroteToken)
  255. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.StandardsManagerCannotWriteObject, token.GetType())));
  256. localWriter.Flush();
  257. }
  258. protected override bool CanReadKeyIdentifierCore(XmlReader reader)
  259. {
  260. try
  261. {
  262. return this.keyInfoSerializer.CanReadKeyIdentifier(reader);
  263. }
  264. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  265. {
  266. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  267. }
  268. }
  269. protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader)
  270. {
  271. try
  272. {
  273. return this.keyInfoSerializer.ReadKeyIdentifier(reader);
  274. }
  275. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  276. {
  277. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  278. }
  279. }
  280. protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier)
  281. {
  282. try
  283. {
  284. return this.keyInfoSerializer.CanWriteKeyIdentifier(keyIdentifier);
  285. }
  286. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  287. {
  288. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  289. }
  290. }
  291. protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier)
  292. {
  293. try
  294. {
  295. this.keyInfoSerializer.WriteKeyIdentifier(writer, keyIdentifier);
  296. }
  297. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  298. {
  299. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  300. }
  301. }
  302. protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader)
  303. {
  304. try
  305. {
  306. return this.keyInfoSerializer.CanReadKeyIdentifierClause(reader);
  307. }
  308. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  309. {
  310. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  311. }
  312. }
  313. protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader)
  314. {
  315. try
  316. {
  317. return this.keyInfoSerializer.ReadKeyIdentifierClause(reader);
  318. }
  319. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  320. {
  321. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  322. }
  323. }
  324. protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause)
  325. {
  326. try
  327. {
  328. return this.keyInfoSerializer.CanWriteKeyIdentifierClause(keyIdentifierClause);
  329. }
  330. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  331. {
  332. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  333. }
  334. }
  335. protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierClause)
  336. {
  337. try
  338. {
  339. this.keyInfoSerializer.WriteKeyIdentifierClause(writer, keyIdentifierClause);
  340. }
  341. catch (System.IdentityModel.SecurityMessageSerializationException ex)
  342. {
  343. throw FxTrace.Exception.AsError(new MessageSecurityException(ex.Message));
  344. }
  345. }
  346. internal Type[] GetTokenTypes(string tokenTypeUri)
  347. {
  348. if (tokenTypeUri != null)
  349. {
  350. for (int i = 0; i < this.tokenEntries.Count; i++)
  351. {
  352. TokenEntry tokenEntry = this.tokenEntries[i];
  353. if (tokenEntry.SupportsTokenTypeUri(tokenTypeUri))
  354. {
  355. return tokenEntry.GetTokenTypes();
  356. }
  357. }
  358. }
  359. return null;
  360. }
  361. protected internal virtual string GetTokenTypeUri(Type tokenType)
  362. {
  363. if (tokenType != null)
  364. {
  365. for (int i = 0; i < this.tokenEntries.Count; i++)
  366. {
  367. TokenEntry tokenEntry = this.tokenEntries[i];
  368. if (tokenEntry.SupportsCore(tokenType))
  369. {
  370. return tokenEntry.TokenTypeUri;
  371. }
  372. }
  373. }
  374. return null;
  375. }
  376. public virtual bool TryCreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tokenReferenceStyle, out SecurityKeyIdentifierClause securityKeyIdentifierClause)
  377. {
  378. if (element == null)
  379. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  380. securityKeyIdentifierClause = null;
  381. try
  382. {
  383. securityKeyIdentifierClause = CreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle);
  384. }
  385. catch (XmlException e)
  386. {
  387. if (DiagnosticUtility.ShouldTraceError)
  388. {
  389. TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.Security, SR.GetString(SR.TraceCodeSecurity), null, e);
  390. }
  391. return false;
  392. }
  393. return true;
  394. }
  395. public virtual SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tokenReferenceStyle)
  396. {
  397. if (element == null)
  398. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  399. for (int i = 0; i < this.tokenEntries.Count; i++)
  400. {
  401. TokenEntry tokenEntry = this.tokenEntries[i];
  402. if (tokenEntry.CanReadTokenCore(element))
  403. {
  404. try
  405. {
  406. return tokenEntry.CreateKeyIdentifierClauseFromTokenXmlCore(element, tokenReferenceStyle);
  407. }
  408. #pragma warning suppress 56500 // covered by FxCOP
  409. catch (Exception e)
  410. {
  411. if (!ShouldWrapException(e))
  412. {
  413. throw;
  414. }
  415. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ErrorDeserializingKeyIdentifierClauseFromTokenXml), e));
  416. }
  417. }
  418. }
  419. // PreSharp Bug: Parameter 'element' to this public method must be validated: A null-dereference can occur here.
  420. #pragma warning suppress 56506
  421. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.CannotReadToken, element.LocalName, element.NamespaceURI, element.GetAttribute(SecurityJan2004Strings.ValueType, null))));
  422. }
  423. internal abstract new class TokenEntry
  424. {
  425. Type[] tokenTypes = null;
  426. public virtual IAsyncResult BeginReadTokenCore(XmlDictionaryReader reader,
  427. SecurityTokenResolver tokenResolver, AsyncCallback callback, object state)
  428. {
  429. SecurityToken result = this.ReadTokenCore(reader, tokenResolver);
  430. return new CompletedAsyncResult<SecurityToken>(result, callback, state);
  431. }
  432. protected abstract XmlDictionaryString LocalName { get; }
  433. protected abstract XmlDictionaryString NamespaceUri { get; }
  434. public Type TokenType { get { return GetTokenTypes()[0]; } }
  435. public abstract string TokenTypeUri { get; }
  436. protected abstract string ValueTypeUri { get; }
  437. protected abstract Type[] GetTokenTypesCore();
  438. public Type[] GetTokenTypes()
  439. {
  440. if (this.tokenTypes == null)
  441. this.tokenTypes = GetTokenTypesCore();
  442. return this.tokenTypes;
  443. }
  444. public bool SupportsCore(Type tokenType)
  445. {
  446. Type[] tokenTypes = GetTokenTypes();
  447. for (int i = 0; i < tokenTypes.Length; ++i)
  448. {
  449. if (tokenTypes[i].IsAssignableFrom(tokenType))
  450. return true;
  451. }
  452. return false;
  453. }
  454. public virtual bool SupportsTokenTypeUri(string tokenTypeUri)
  455. {
  456. return (this.TokenTypeUri == tokenTypeUri);
  457. }
  458. protected static SecurityKeyIdentifierClause CreateDirectReference(XmlElement issuedTokenXml, string idAttributeLocalName, string idAttributeNamespace, Type tokenType)
  459. {
  460. string id = issuedTokenXml.GetAttribute(idAttributeLocalName, idAttributeNamespace);
  461. if (id == null)
  462. {
  463. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.RequiredAttributeMissing, idAttributeLocalName, issuedTokenXml.LocalName)));
  464. }
  465. return new LocalIdKeyIdentifierClause(id, tokenType);
  466. }
  467. public virtual bool CanReadTokenCore(XmlElement element)
  468. {
  469. string valueTypeUri = null;
  470. if (element.HasAttribute(SecurityJan2004Strings.ValueType, null))
  471. {
  472. valueTypeUri = element.GetAttribute(SecurityJan2004Strings.ValueType, null);
  473. }
  474. return element.LocalName == LocalName.Value && element.NamespaceURI == NamespaceUri.Value && valueTypeUri == this.ValueTypeUri;
  475. }
  476. public virtual bool CanReadTokenCore(XmlDictionaryReader reader)
  477. {
  478. return reader.IsStartElement(this.LocalName, this.NamespaceUri) &&
  479. reader.GetAttribute(XD.SecurityJan2004Dictionary.ValueType, null) == this.ValueTypeUri;
  480. }
  481. public virtual SecurityToken EndReadTokenCore(IAsyncResult result)
  482. {
  483. return CompletedAsyncResult<SecurityToken>.End(result);
  484. }
  485. public abstract SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedTokenXml, SecurityTokenReferenceStyle tokenReferenceStyle);
  486. public abstract SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver);
  487. public abstract void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token);
  488. }
  489. internal abstract new class SerializerEntries
  490. {
  491. public virtual void PopulateTokenEntries(IList<TokenEntry> tokenEntries) { }
  492. }
  493. internal class CollectionDictionary : IXmlDictionary
  494. {
  495. List<XmlDictionaryString> dictionaryStrings;
  496. public CollectionDictionary(List<XmlDictionaryString> dictionaryStrings)
  497. {
  498. if (dictionaryStrings == null)
  499. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dictionaryStrings"));
  500. this.dictionaryStrings = dictionaryStrings;
  501. }
  502. public bool TryLookup(string value, out XmlDictionaryString result)
  503. {
  504. if (value == null)
  505. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  506. for (int i = 0; i < this.dictionaryStrings.Count; ++i)
  507. {
  508. if (this.dictionaryStrings[i].Value.Equals(value))
  509. {
  510. result = this.dictionaryStrings[i];
  511. return true;
  512. }
  513. }
  514. result = null;
  515. return false;
  516. }
  517. public bool TryLookup(int key, out XmlDictionaryString result)
  518. {
  519. for (int i = 0; i < this.dictionaryStrings.Count; ++i)
  520. {
  521. if (this.dictionaryStrings[i].Key == key)
  522. {
  523. result = this.dictionaryStrings[i];
  524. return true;
  525. }
  526. }
  527. result = null;
  528. return false;
  529. }
  530. public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result)
  531. {
  532. if (value == null)
  533. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  534. for (int i = 0; i < this.dictionaryStrings.Count; ++i)
  535. {
  536. if ((this.dictionaryStrings[i].Key == value.Key) &&
  537. (this.dictionaryStrings[i].Value.Equals(value.Value)))
  538. {
  539. result = this.dictionaryStrings[i];
  540. return true;
  541. }
  542. }
  543. result = null;
  544. return false;
  545. }
  546. }
  547. }
  548. }