SendSecurityHeader.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. //----------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.Collections.ObjectModel;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.IdentityModel.Tokens;
  14. using System.IdentityModel.Selectors;
  15. using System.Security.Cryptography;
  16. using System.ServiceModel.Security.Tokens;
  17. using System.Xml;
  18. using System.ServiceModel.Diagnostics;
  19. using DictionaryManager = System.IdentityModel.DictionaryManager;
  20. using ISecurityElement = System.IdentityModel.ISecurityElement;
  21. using ISignatureValueSecurityElement = System.IdentityModel.ISignatureValueSecurityElement;
  22. using IPrefixGenerator = System.IdentityModel.IPrefixGenerator;
  23. abstract class SendSecurityHeader : SecurityHeader, IMessageHeaderWithSharedNamespace
  24. {
  25. bool basicTokenEncrypted;
  26. SendSecurityHeaderElementContainer elementContainer;
  27. bool primarySignatureDone;
  28. bool encryptSignature;
  29. SignatureConfirmations signatureValuesGenerated;
  30. SignatureConfirmations signatureConfirmationsToSend;
  31. int idCounter;
  32. string idPrefix;
  33. bool hasSignedTokens;
  34. bool hasEncryptedTokens;
  35. MessagePartSpecification signatureParts;
  36. MessagePartSpecification encryptionParts;
  37. SecurityTokenParameters signingTokenParameters;
  38. SecurityTokenParameters encryptingTokenParameters;
  39. List<SecurityToken> basicTokens = null;
  40. List<SecurityTokenParameters> basicSupportingTokenParameters = null;
  41. List<SecurityTokenParameters> endorsingTokenParameters = null;
  42. List<SecurityTokenParameters> signedEndorsingTokenParameters = null;
  43. List<SecurityTokenParameters> signedTokenParameters = null;
  44. SecurityToken encryptingToken;
  45. bool skipKeyInfoForEncryption;
  46. byte[] primarySignatureValue = null;
  47. bool shouldProtectTokens;
  48. BufferManager bufferManager;
  49. bool shouldSignToHeader = false;
  50. SecurityProtocolCorrelationState correlationState;
  51. bool signThenEncrypt = true;
  52. static readonly string[] ids = new string[] { "_0", "_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_9" };
  53. protected SendSecurityHeader(Message message, string actor, bool mustUnderstand, bool relay,
  54. SecurityStandardsManager standardsManager,
  55. SecurityAlgorithmSuite algorithmSuite,
  56. MessageDirection transferDirection)
  57. : base(message, actor, mustUnderstand, relay, standardsManager, algorithmSuite, transferDirection)
  58. {
  59. this.elementContainer = new SendSecurityHeaderElementContainer();
  60. }
  61. public SendSecurityHeaderElementContainer ElementContainer
  62. {
  63. get { return this.elementContainer; }
  64. }
  65. public SecurityProtocolCorrelationState CorrelationState
  66. {
  67. get { return this.correlationState; }
  68. set
  69. {
  70. ThrowIfProcessingStarted();
  71. this.correlationState = value;
  72. }
  73. }
  74. public BufferManager StreamBufferManager
  75. {
  76. get
  77. {
  78. if (this.bufferManager == null)
  79. {
  80. this.bufferManager = BufferManager.CreateBufferManager(0, int.MaxValue);
  81. }
  82. return this.bufferManager;
  83. }
  84. set
  85. {
  86. this.bufferManager = value;
  87. }
  88. }
  89. public MessagePartSpecification EncryptionParts
  90. {
  91. get { return this.encryptionParts; }
  92. set
  93. {
  94. ThrowIfProcessingStarted();
  95. if (value == null)
  96. {
  97. throw TraceUtility.ThrowHelperError(new ArgumentNullException("value"), this.Message);
  98. }
  99. if (!value.IsReadOnly)
  100. {
  101. throw TraceUtility.ThrowHelperError(new InvalidOperationException(
  102. SR.GetString(SR.MessagePartSpecificationMustBeImmutable)), this.Message);
  103. }
  104. this.encryptionParts = value;
  105. }
  106. }
  107. public bool EncryptPrimarySignature
  108. {
  109. get { return this.encryptSignature; }
  110. set
  111. {
  112. ThrowIfProcessingStarted();
  113. this.encryptSignature = value;
  114. }
  115. }
  116. internal byte[] PrimarySignatureValue
  117. {
  118. get { return this.primarySignatureValue; }
  119. }
  120. protected internal SecurityTokenParameters SigningTokenParameters
  121. {
  122. get { return this.signingTokenParameters; }
  123. }
  124. protected bool ShouldSignToHeader
  125. {
  126. get { return this.shouldSignToHeader; }
  127. }
  128. public string IdPrefix
  129. {
  130. get { return this.idPrefix; }
  131. set
  132. {
  133. ThrowIfProcessingStarted();
  134. this.idPrefix = string.IsNullOrEmpty(value) || value == "_" ? null : value;
  135. }
  136. }
  137. public override string Name
  138. {
  139. get { return this.StandardsManager.SecurityVersion.HeaderName.Value; }
  140. }
  141. public override string Namespace
  142. {
  143. get { return this.StandardsManager.SecurityVersion.HeaderNamespace.Value; }
  144. }
  145. protected SecurityAppliedMessage SecurityAppliedMessage
  146. {
  147. get { return (SecurityAppliedMessage) this.Message; }
  148. }
  149. public bool SignThenEncrypt
  150. {
  151. get { return this.signThenEncrypt; }
  152. set
  153. {
  154. ThrowIfProcessingStarted();
  155. this.signThenEncrypt = value;
  156. }
  157. }
  158. public bool ShouldProtectTokens
  159. {
  160. get { return this.shouldProtectTokens; }
  161. set
  162. {
  163. ThrowIfProcessingStarted();
  164. this.shouldProtectTokens = value;
  165. }
  166. }
  167. public MessagePartSpecification SignatureParts
  168. {
  169. get { return this.signatureParts; }
  170. set
  171. {
  172. ThrowIfProcessingStarted();
  173. if (value == null)
  174. {
  175. throw TraceUtility.ThrowHelperError(new ArgumentNullException("value"), this.Message);
  176. }
  177. if (!value.IsReadOnly)
  178. {
  179. throw TraceUtility.ThrowHelperError(new InvalidOperationException(
  180. SR.GetString(SR.MessagePartSpecificationMustBeImmutable)), this.Message);
  181. }
  182. this.signatureParts = value;
  183. }
  184. }
  185. public SecurityTimestamp Timestamp
  186. {
  187. get { return this.elementContainer.Timestamp; }
  188. }
  189. public bool HasSignedTokens
  190. {
  191. get
  192. {
  193. return this.hasSignedTokens;
  194. }
  195. }
  196. public bool HasEncryptedTokens
  197. {
  198. get
  199. {
  200. return this.hasEncryptedTokens;
  201. }
  202. }
  203. public void AddPrerequisiteToken(SecurityToken token)
  204. {
  205. ThrowIfProcessingStarted();
  206. if (token == null)
  207. {
  208. throw TraceUtility.ThrowHelperArgumentNull("token", this.Message);
  209. }
  210. this.elementContainer.PrerequisiteToken = token;
  211. }
  212. void AddParameters(ref List<SecurityTokenParameters> list, SecurityTokenParameters item)
  213. {
  214. if (list == null)
  215. {
  216. list = new List<SecurityTokenParameters>();
  217. }
  218. list.Add(item);
  219. }
  220. public abstract void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator);
  221. public abstract void ApplySecurityAndWriteHeaders(MessageHeaders headers, XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator);
  222. protected virtual bool HasSignedEncryptedMessagePart
  223. {
  224. get { return false; }
  225. }
  226. public void SetSigningToken(SecurityToken token, SecurityTokenParameters tokenParameters)
  227. {
  228. ThrowIfProcessingStarted();
  229. if ((token == null && tokenParameters != null) || (token != null && tokenParameters == null))
  230. {
  231. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.TokenMustBeNullWhenTokenParametersAre)));
  232. }
  233. this.elementContainer.SourceSigningToken = token;
  234. this.signingTokenParameters = tokenParameters;
  235. }
  236. public void SetEncryptionToken(SecurityToken token, SecurityTokenParameters tokenParameters)
  237. {
  238. ThrowIfProcessingStarted();
  239. if ((token == null && tokenParameters != null) || (token != null && tokenParameters == null))
  240. {
  241. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.TokenMustBeNullWhenTokenParametersAre)));
  242. }
  243. this.elementContainer.SourceEncryptionToken = token;
  244. this.encryptingTokenParameters = tokenParameters;
  245. }
  246. public void AddBasicSupportingToken(SecurityToken token, SecurityTokenParameters parameters)
  247. {
  248. if (token == null)
  249. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  250. if (parameters == null)
  251. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  252. ThrowIfProcessingStarted();
  253. SendSecurityHeaderElement tokenElement = new SendSecurityHeaderElement(token.Id, new TokenElement(token, this.StandardsManager));
  254. tokenElement.MarkedForEncryption = true;
  255. this.elementContainer.AddBasicSupportingToken(tokenElement);
  256. hasEncryptedTokens = true;
  257. hasSignedTokens = true;
  258. this.AddParameters(ref this.basicSupportingTokenParameters, parameters);
  259. if (this.basicTokens == null)
  260. {
  261. this.basicTokens = new List<SecurityToken>();
  262. }
  263. // We maintain a list of the basic tokens for the SignThenEncrypt case as we will
  264. // need this token to write STR entry on OnWriteHeaderContents.
  265. this.basicTokens.Add(token);
  266. }
  267. public void AddEndorsingSupportingToken(SecurityToken token, SecurityTokenParameters parameters)
  268. {
  269. if (token == null)
  270. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  271. if (parameters == null)
  272. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  273. ThrowIfProcessingStarted();
  274. this.elementContainer.AddEndorsingSupportingToken(token);
  275. // The ProviderBackedSecurityToken was added for the ChannelBindingToken (CBT) effort for win7.
  276. // We can assume the key is of type symmetric key.
  277. //
  278. // Asking for the key type from the token will cause the ProviderBackedSecurityToken
  279. // to attempt to resolve the token and the nego will start.
  280. //
  281. // We don't want that.
  282. // We want to defer the nego until after the CBT is available in SecurityAppliedMessage.OnWriteMessage.
  283. if (!(token is ProviderBackedSecurityToken))
  284. {
  285. this.shouldSignToHeader |= (!this.RequireMessageProtection) && (SecurityUtils.GetSecurityKey<AsymmetricSecurityKey>(token) != null);
  286. }
  287. this.AddParameters(ref this.endorsingTokenParameters, parameters);
  288. }
  289. public void AddSignedEndorsingSupportingToken(SecurityToken token, SecurityTokenParameters parameters)
  290. {
  291. if (token == null)
  292. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  293. if (parameters == null)
  294. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  295. ThrowIfProcessingStarted();
  296. this.elementContainer.AddSignedEndorsingSupportingToken(token);
  297. hasSignedTokens = true;
  298. this.shouldSignToHeader |= (!this.RequireMessageProtection) && (SecurityUtils.GetSecurityKey<AsymmetricSecurityKey>(token) != null);
  299. this.AddParameters(ref this.signedEndorsingTokenParameters, parameters);
  300. }
  301. public void AddSignedSupportingToken(SecurityToken token, SecurityTokenParameters parameters)
  302. {
  303. if (token == null)
  304. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  305. if (parameters == null)
  306. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  307. ThrowIfProcessingStarted();
  308. this.elementContainer.AddSignedSupportingToken(token);
  309. hasSignedTokens = true;
  310. this.AddParameters(ref this.signedTokenParameters, parameters);
  311. }
  312. public void AddSignatureConfirmations(SignatureConfirmations confirmations)
  313. {
  314. ThrowIfProcessingStarted();
  315. this.signatureConfirmationsToSend = confirmations;
  316. }
  317. public void AddTimestamp(TimeSpan timestampValidityDuration)
  318. {
  319. DateTime now = DateTime.UtcNow;
  320. string id = this.RequireMessageProtection ? SecurityUtils.GenerateId() : GenerateId();
  321. AddTimestamp(new SecurityTimestamp(now, now + timestampValidityDuration, id));
  322. }
  323. public void AddTimestamp(SecurityTimestamp timestamp)
  324. {
  325. ThrowIfProcessingStarted();
  326. if (this.elementContainer.Timestamp != null)
  327. {
  328. throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TimestampAlreadySetForSecurityHeader)), this.Message);
  329. }
  330. if (timestamp == null)
  331. {
  332. throw TraceUtility.ThrowHelperArgumentNull("timestamp", this.Message);
  333. }
  334. this.elementContainer.Timestamp = timestamp;
  335. }
  336. protected virtual ISignatureValueSecurityElement[] CreateSignatureConfirmationElements(SignatureConfirmations signatureConfirmations)
  337. {
  338. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  339. SR.GetString(SR.SignatureConfirmationNotSupported)));
  340. }
  341. void StartEncryption()
  342. {
  343. if (this.elementContainer.SourceEncryptionToken == null)
  344. {
  345. return;
  346. }
  347. // determine the key identifier clause to use for the source
  348. SecurityTokenReferenceStyle sourceEncryptingKeyReferenceStyle = GetTokenReferenceStyle(this.encryptingTokenParameters);
  349. bool encryptionTokenSerialized = sourceEncryptingKeyReferenceStyle == SecurityTokenReferenceStyle.Internal;
  350. SecurityKeyIdentifierClause sourceEncryptingKeyIdentifierClause = this.encryptingTokenParameters.CreateKeyIdentifierClause(this.elementContainer.SourceEncryptionToken, sourceEncryptingKeyReferenceStyle);
  351. if (sourceEncryptingKeyIdentifierClause == null)
  352. {
  353. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  354. }
  355. SecurityToken sourceToken;
  356. SecurityKeyIdentifierClause sourceTokenIdentifierClause;
  357. // if the source token cannot do symmetric crypto, create a wrapped key
  358. if (!SecurityUtils.HasSymmetricSecurityKey(elementContainer.SourceEncryptionToken))
  359. {
  360. int keyLength = Math.Max(128, this.AlgorithmSuite.DefaultSymmetricKeyLength);
  361. CryptoHelper.ValidateSymmetricKeyLength(keyLength, this.AlgorithmSuite);
  362. byte[] key = new byte[keyLength / 8];
  363. CryptoHelper.FillRandomBytes(key);
  364. string keyWrapAlgorithm;
  365. XmlDictionaryString keyWrapAlgorithmDictionaryString;
  366. this.AlgorithmSuite.GetKeyWrapAlgorithm(elementContainer.SourceEncryptionToken, out keyWrapAlgorithm, out keyWrapAlgorithmDictionaryString);
  367. WrappedKeySecurityToken wrappedKey = new WrappedKeySecurityToken(GenerateId(), key, keyWrapAlgorithm, keyWrapAlgorithmDictionaryString,
  368. elementContainer.SourceEncryptionToken, new SecurityKeyIdentifier(sourceEncryptingKeyIdentifierClause));
  369. elementContainer.WrappedEncryptionToken = wrappedKey;
  370. sourceToken = wrappedKey;
  371. sourceTokenIdentifierClause = new LocalIdKeyIdentifierClause(wrappedKey.Id, wrappedKey.GetType());
  372. encryptionTokenSerialized = true;
  373. }
  374. else
  375. {
  376. sourceToken = elementContainer.SourceEncryptionToken;
  377. sourceTokenIdentifierClause = sourceEncryptingKeyIdentifierClause;
  378. }
  379. // determine if a key needs to be derived
  380. SecurityKeyIdentifierClause encryptingKeyIdentifierClause;
  381. // determine if a token needs to be derived
  382. if (this.encryptingTokenParameters.RequireDerivedKeys)
  383. {
  384. string derivationAlgorithm = this.AlgorithmSuite.GetEncryptionKeyDerivationAlgorithm(sourceToken, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  385. string expectedDerivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  386. if (derivationAlgorithm == expectedDerivationAlgorithm)
  387. {
  388. DerivedKeySecurityToken derivedEncryptingToken = new DerivedKeySecurityToken(-1, 0,
  389. this.AlgorithmSuite.GetEncryptionKeyDerivationLength(sourceToken, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion), null, DerivedKeySecurityToken.DefaultNonceLength, sourceToken, sourceTokenIdentifierClause, derivationAlgorithm, GenerateId());
  390. this.encryptingToken = this.elementContainer.DerivedEncryptionToken = derivedEncryptingToken;
  391. encryptingKeyIdentifierClause = new LocalIdKeyIdentifierClause(derivedEncryptingToken.Id, derivedEncryptingToken.GetType());
  392. }
  393. else
  394. {
  395. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedCryptoAlgorithm, derivationAlgorithm)));
  396. }
  397. }
  398. else
  399. {
  400. this.encryptingToken = sourceToken;
  401. encryptingKeyIdentifierClause = sourceTokenIdentifierClause;
  402. }
  403. this.skipKeyInfoForEncryption = encryptionTokenSerialized && this.EncryptedKeyContainsReferenceList && (this.encryptingToken is WrappedKeySecurityToken) && this.signThenEncrypt;
  404. SecurityKeyIdentifier identifier;
  405. if (this.skipKeyInfoForEncryption)
  406. {
  407. identifier = null;
  408. }
  409. else
  410. {
  411. identifier = new SecurityKeyIdentifier(encryptingKeyIdentifierClause);
  412. }
  413. StartEncryptionCore(this.encryptingToken, identifier);
  414. }
  415. void CompleteEncryption()
  416. {
  417. ISecurityElement referenceList = CompleteEncryptionCore(
  418. elementContainer.PrimarySignature,
  419. elementContainer.GetBasicSupportingTokens(),
  420. elementContainer.GetSignatureConfirmations(),
  421. elementContainer.GetEndorsingSignatures());
  422. if (referenceList == null)
  423. {
  424. // null out all the encryption fields since there is no encryption needed
  425. this.elementContainer.SourceEncryptionToken = null;
  426. this.elementContainer.WrappedEncryptionToken = null;
  427. this.elementContainer.DerivedEncryptionToken = null;
  428. return;
  429. }
  430. if (this.skipKeyInfoForEncryption)
  431. {
  432. WrappedKeySecurityToken wrappedKeyToken = this.encryptingToken as WrappedKeySecurityToken;
  433. wrappedKeyToken.EnsureEncryptedKeySetUp();
  434. wrappedKeyToken.EncryptedKey.ReferenceList = (ReferenceList) referenceList;
  435. }
  436. else
  437. {
  438. this.elementContainer.ReferenceList = referenceList;
  439. }
  440. basicTokenEncrypted = true;
  441. }
  442. internal void StartSecurityApplication()
  443. {
  444. if (this.SignThenEncrypt)
  445. {
  446. StartSignature();
  447. StartEncryption();
  448. }
  449. else
  450. {
  451. StartEncryption();
  452. StartSignature();
  453. }
  454. }
  455. internal void CompleteSecurityApplication()
  456. {
  457. if (this.SignThenEncrypt)
  458. {
  459. CompleteSignature();
  460. SignWithSupportingTokens();
  461. CompleteEncryption();
  462. }
  463. else
  464. {
  465. CompleteEncryption();
  466. CompleteSignature();
  467. SignWithSupportingTokens();
  468. }
  469. if (this.correlationState != null)
  470. {
  471. this.correlationState.SignatureConfirmations = GetSignatureValues();
  472. }
  473. }
  474. public void RemoveSignatureEncryptionIfAppropriate()
  475. {
  476. if (this.SignThenEncrypt &&
  477. this.EncryptPrimarySignature &&
  478. (this.SecurityAppliedMessage.BodyProtectionMode != MessagePartProtectionMode.SignThenEncrypt) &&
  479. (this.basicSupportingTokenParameters == null || this.basicSupportingTokenParameters.Count == 0) &&
  480. (this.signatureConfirmationsToSend == null || this.signatureConfirmationsToSend.Count == 0 || !this.signatureConfirmationsToSend.IsMarkedForEncryption) &&
  481. !this.HasSignedEncryptedMessagePart)
  482. {
  483. this.encryptSignature = false;
  484. }
  485. }
  486. public string GenerateId()
  487. {
  488. int id = this.idCounter++;
  489. if (this.idPrefix != null)
  490. {
  491. return this.idPrefix + id;
  492. }
  493. if (id < ids.Length)
  494. {
  495. return ids[id];
  496. }
  497. else
  498. {
  499. return "_" + id;
  500. }
  501. }
  502. SignatureConfirmations GetSignatureValues()
  503. {
  504. return this.signatureValuesGenerated;
  505. }
  506. protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
  507. {
  508. this.StandardsManager.SecurityVersion.WriteStartHeader(writer);
  509. WriteHeaderAttributes(writer, messageVersion);
  510. }
  511. internal static bool ShouldSerializeToken(SecurityTokenParameters parameters, MessageDirection transferDirection)
  512. {
  513. switch (parameters.InclusionMode)
  514. {
  515. case SecurityTokenInclusionMode.AlwaysToInitiator:
  516. return (transferDirection == MessageDirection.Output);
  517. case SecurityTokenInclusionMode.Once:
  518. case SecurityTokenInclusionMode.AlwaysToRecipient:
  519. return (transferDirection == MessageDirection.Input);
  520. case SecurityTokenInclusionMode.Never:
  521. return false;
  522. default:
  523. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedTokenInclusionMode, parameters.InclusionMode)));
  524. }
  525. }
  526. protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
  527. {
  528. if (this.basicSupportingTokenParameters != null && this.basicSupportingTokenParameters.Count > 0
  529. && this.RequireMessageProtection && !basicTokenEncrypted)
  530. {
  531. throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BasicTokenCannotBeWrittenWithoutEncryption)), this.Message);
  532. }
  533. if (this.elementContainer.Timestamp != null && this.Layout != SecurityHeaderLayout.LaxTimestampLast)
  534. {
  535. this.StandardsManager.WSUtilitySpecificationVersion.WriteTimestamp(writer, this.elementContainer.Timestamp);
  536. }
  537. if (elementContainer.PrerequisiteToken != null)
  538. {
  539. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.PrerequisiteToken);
  540. }
  541. if (elementContainer.SourceSigningToken != null)
  542. {
  543. if (ShouldSerializeToken(this.signingTokenParameters, this.MessageDirection))
  544. {
  545. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.SourceSigningToken);
  546. // Implement Protect token
  547. // NOTE: The spec says sign the primary token if it is not included in the message. But we currently are not supporting it
  548. // as we do not support STR-Transform for external references. Hence we can not sign the token which is external ie not in the message.
  549. // This only affects the messages from service to client where
  550. // 1. allowSerializedSigningTokenOnReply is false.
  551. // 2. SymmetricSecurityBindingElement with IssuedTokens binding where the issued token has a symmetric key.
  552. if (this.ShouldProtectTokens)
  553. {
  554. this.WriteSecurityTokenReferencyEntry(writer, elementContainer.SourceSigningToken, this.signingTokenParameters);
  555. }
  556. }
  557. }
  558. if (elementContainer.DerivedSigningToken != null)
  559. {
  560. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.DerivedSigningToken);
  561. }
  562. if (elementContainer.SourceEncryptionToken != null && elementContainer.SourceEncryptionToken != elementContainer.SourceSigningToken && ShouldSerializeToken(encryptingTokenParameters, this.MessageDirection))
  563. {
  564. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.SourceEncryptionToken);
  565. }
  566. if (elementContainer.WrappedEncryptionToken != null)
  567. {
  568. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.WrappedEncryptionToken);
  569. }
  570. if (elementContainer.DerivedEncryptionToken != null)
  571. {
  572. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, elementContainer.DerivedEncryptionToken);
  573. }
  574. if (this.SignThenEncrypt)
  575. {
  576. if (elementContainer.ReferenceList != null)
  577. {
  578. elementContainer.ReferenceList.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  579. }
  580. }
  581. SecurityToken[] signedTokens = elementContainer.GetSignedSupportingTokens();
  582. if (signedTokens != null)
  583. {
  584. for (int i = 0; i < signedTokens.Length; ++i)
  585. {
  586. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedTokens[i]);
  587. this.WriteSecurityTokenReferencyEntry(writer, signedTokens[i], this.signedTokenParameters[i]);
  588. }
  589. }
  590. SendSecurityHeaderElement[] basicTokensXml = elementContainer.GetBasicSupportingTokens();
  591. if (basicTokensXml != null)
  592. {
  593. for (int i = 0; i < basicTokensXml.Length; ++i)
  594. {
  595. basicTokensXml[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  596. if (this.SignThenEncrypt)
  597. {
  598. this.WriteSecurityTokenReferencyEntry(writer, this.basicTokens[i], this.basicSupportingTokenParameters[i]);
  599. }
  600. }
  601. }
  602. SecurityToken[] endorsingTokens = elementContainer.GetEndorsingSupportingTokens();
  603. if (endorsingTokens != null)
  604. {
  605. for (int i = 0; i < endorsingTokens.Length; ++i)
  606. {
  607. if (ShouldSerializeToken(endorsingTokenParameters[i], this.MessageDirection))
  608. {
  609. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, endorsingTokens[i]);
  610. }
  611. }
  612. }
  613. SecurityToken[] endorsingDerivedTokens = elementContainer.GetEndorsingDerivedSupportingTokens();
  614. if (endorsingDerivedTokens != null)
  615. {
  616. for (int i = 0; i < endorsingDerivedTokens.Length; ++i)
  617. {
  618. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, endorsingDerivedTokens[i]);
  619. }
  620. }
  621. SecurityToken[] signedEndorsingTokens = elementContainer.GetSignedEndorsingSupportingTokens();
  622. if (signedEndorsingTokens != null)
  623. {
  624. for (int i = 0; i < signedEndorsingTokens.Length; ++i)
  625. {
  626. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedEndorsingTokens[i]);
  627. this.WriteSecurityTokenReferencyEntry(writer, signedEndorsingTokens[i], this.signedEndorsingTokenParameters[i]);
  628. }
  629. }
  630. SecurityToken[] signedEndorsingDerivedTokens = elementContainer.GetSignedEndorsingDerivedSupportingTokens();
  631. if (signedEndorsingDerivedTokens != null)
  632. {
  633. for (int i = 0; i < signedEndorsingDerivedTokens.Length; ++i)
  634. {
  635. this.StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedEndorsingDerivedTokens[i]);
  636. }
  637. }
  638. SendSecurityHeaderElement[] signatureConfirmations = elementContainer.GetSignatureConfirmations();
  639. if (signatureConfirmations != null)
  640. {
  641. for (int i = 0; i < signatureConfirmations.Length; ++i)
  642. {
  643. signatureConfirmations[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  644. }
  645. }
  646. if (elementContainer.PrimarySignature != null && elementContainer.PrimarySignature.Item != null)
  647. {
  648. elementContainer.PrimarySignature.Item.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  649. }
  650. SendSecurityHeaderElement[] endorsingSignatures = elementContainer.GetEndorsingSignatures();
  651. if (endorsingSignatures != null)
  652. {
  653. for (int i = 0; i < endorsingSignatures.Length; ++i)
  654. {
  655. endorsingSignatures[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  656. }
  657. }
  658. if (!this.SignThenEncrypt)
  659. {
  660. if (elementContainer.ReferenceList != null)
  661. {
  662. elementContainer.ReferenceList.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  663. }
  664. }
  665. if (this.elementContainer.Timestamp != null && this.Layout == SecurityHeaderLayout.LaxTimestampLast)
  666. {
  667. this.StandardsManager.WSUtilitySpecificationVersion.WriteTimestamp(writer, this.elementContainer.Timestamp);
  668. }
  669. }
  670. protected abstract void WriteSecurityTokenReferencyEntry(XmlDictionaryWriter writer, SecurityToken securityToken, SecurityTokenParameters securityTokenParameters);
  671. public Message SetupExecution()
  672. {
  673. ThrowIfProcessingStarted();
  674. SetProcessingStarted();
  675. bool signBody = false;
  676. if (this.elementContainer.SourceSigningToken != null)
  677. {
  678. if (this.signatureParts == null)
  679. {
  680. throw TraceUtility.ThrowHelperError(new ArgumentNullException("SignatureParts"), this.Message);
  681. }
  682. signBody = this.signatureParts.IsBodyIncluded;
  683. }
  684. bool encryptBody = false;
  685. if (this.elementContainer.SourceEncryptionToken != null)
  686. {
  687. if (this.encryptionParts == null)
  688. {
  689. throw TraceUtility.ThrowHelperError(new ArgumentNullException("EncryptionParts"), this.Message);
  690. }
  691. encryptBody = this.encryptionParts.IsBodyIncluded;
  692. }
  693. SecurityAppliedMessage message = new SecurityAppliedMessage(this.Message, this, signBody, encryptBody);
  694. this.Message = message;
  695. return message;
  696. }
  697. protected internal SecurityTokenReferenceStyle GetTokenReferenceStyle(SecurityTokenParameters parameters)
  698. {
  699. return (ShouldSerializeToken(parameters, this.MessageDirection)) ? SecurityTokenReferenceStyle.Internal : SecurityTokenReferenceStyle.External;
  700. }
  701. void StartSignature()
  702. {
  703. if (this.elementContainer.SourceSigningToken == null)
  704. {
  705. return;
  706. }
  707. // determine the key identifier clause to use for the source
  708. SecurityTokenReferenceStyle sourceSigningKeyReferenceStyle = GetTokenReferenceStyle(this.signingTokenParameters);
  709. SecurityKeyIdentifierClause sourceSigningKeyIdentifierClause = this.signingTokenParameters.CreateKeyIdentifierClause(this.elementContainer.SourceSigningToken, sourceSigningKeyReferenceStyle);
  710. if (sourceSigningKeyIdentifierClause == null)
  711. {
  712. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  713. }
  714. SecurityToken signingToken;
  715. SecurityKeyIdentifierClause signingKeyIdentifierClause;
  716. // determine if a token needs to be derived
  717. if (this.signingTokenParameters.RequireDerivedKeys && !this.signingTokenParameters.HasAsymmetricKey)
  718. {
  719. string derivationAlgorithm = this.AlgorithmSuite.GetSignatureKeyDerivationAlgorithm(this.elementContainer.SourceSigningToken, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  720. string expectedDerivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  721. if (derivationAlgorithm == expectedDerivationAlgorithm)
  722. {
  723. DerivedKeySecurityToken derivedSigningToken = new DerivedKeySecurityToken(-1, 0, this.AlgorithmSuite.GetSignatureKeyDerivationLength(this.elementContainer.SourceSigningToken, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion), null, DerivedKeySecurityToken.DefaultNonceLength, this.elementContainer.SourceSigningToken,
  724. sourceSigningKeyIdentifierClause, derivationAlgorithm, GenerateId());
  725. signingToken = this.elementContainer.DerivedSigningToken = derivedSigningToken;
  726. signingKeyIdentifierClause = new LocalIdKeyIdentifierClause(signingToken.Id, signingToken.GetType());
  727. }
  728. else
  729. {
  730. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedCryptoAlgorithm, derivationAlgorithm)));
  731. }
  732. }
  733. else
  734. {
  735. signingToken = elementContainer.SourceSigningToken;
  736. signingKeyIdentifierClause = sourceSigningKeyIdentifierClause;
  737. }
  738. SecurityKeyIdentifier signingKeyIdentifier = new SecurityKeyIdentifier(signingKeyIdentifierClause);
  739. if (signatureConfirmationsToSend != null && signatureConfirmationsToSend.Count > 0)
  740. {
  741. ISecurityElement[] signatureConfirmationElements;
  742. signatureConfirmationElements = CreateSignatureConfirmationElements(signatureConfirmationsToSend);
  743. for (int i = 0; i < signatureConfirmationElements.Length; ++i)
  744. {
  745. SendSecurityHeaderElement sigConfElement = new SendSecurityHeaderElement(signatureConfirmationElements[i].Id, signatureConfirmationElements[i]);
  746. sigConfElement.MarkedForEncryption = signatureConfirmationsToSend.IsMarkedForEncryption;
  747. this.elementContainer.AddSignatureConfirmation(sigConfElement);
  748. }
  749. }
  750. bool generateTargettablePrimarySignature = ((this.endorsingTokenParameters != null) || (this.signedEndorsingTokenParameters != null));
  751. this.StartPrimarySignatureCore(signingToken, signingKeyIdentifier, this.signatureParts, generateTargettablePrimarySignature);
  752. }
  753. void CompleteSignature()
  754. {
  755. ISignatureValueSecurityElement signedXml = this.CompletePrimarySignatureCore(
  756. elementContainer.GetSignatureConfirmations(), elementContainer.GetSignedEndorsingSupportingTokens(),
  757. elementContainer.GetSignedSupportingTokens(), elementContainer.GetBasicSupportingTokens(), true);
  758. if (signedXml == null)
  759. {
  760. return;
  761. }
  762. this.elementContainer.PrimarySignature = new SendSecurityHeaderElement(signedXml.Id, signedXml);
  763. this.elementContainer.PrimarySignature.MarkedForEncryption = this.encryptSignature;
  764. AddGeneratedSignatureValue(signedXml.GetSignatureValue(), this.EncryptPrimarySignature);
  765. this.primarySignatureDone = true;
  766. this.primarySignatureValue = signedXml.GetSignatureValue();
  767. }
  768. protected abstract void StartPrimarySignatureCore(SecurityToken token, SecurityKeyIdentifier identifier, MessagePartSpecification signatureParts, bool generateTargettablePrimarySignature);
  769. protected abstract ISignatureValueSecurityElement CompletePrimarySignatureCore(SendSecurityHeaderElement[] signatureConfirmations,
  770. SecurityToken[] signedEndorsingTokens, SecurityToken[] signedTokens, SendSecurityHeaderElement[] basicTokens, bool isPrimarySignature);
  771. protected abstract ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIdentifier identifier);
  772. protected abstract ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIdentifier identifier, ISecurityElement primarySignature);
  773. protected abstract void StartEncryptionCore(SecurityToken token, SecurityKeyIdentifier keyIdentifier);
  774. protected abstract ISecurityElement CompleteEncryptionCore(SendSecurityHeaderElement primarySignature,
  775. SendSecurityHeaderElement[] basicTokens, SendSecurityHeaderElement[] signatureConfirmations, SendSecurityHeaderElement[] endorsingSignatures);
  776. void SignWithSupportingToken(SecurityToken token, SecurityKeyIdentifierClause identifierClause)
  777. {
  778. if (token == null)
  779. {
  780. throw TraceUtility.ThrowHelperArgumentNull("token", this.Message);
  781. }
  782. if (identifierClause == null)
  783. {
  784. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  785. }
  786. if (!this.RequireMessageProtection)
  787. {
  788. if (this.elementContainer.Timestamp == null)
  789. {
  790. throw TraceUtility.ThrowHelperError(new InvalidOperationException(
  791. SR.GetString(SR.SigningWithoutPrimarySignatureRequiresTimestamp)), this.Message);
  792. }
  793. }
  794. else
  795. {
  796. if (!this.primarySignatureDone)
  797. {
  798. throw TraceUtility.ThrowHelperError(new InvalidOperationException(
  799. SR.GetString(SR.PrimarySignatureMustBeComputedBeforeSupportingTokenSignatures)), this.Message);
  800. }
  801. if (this.elementContainer.PrimarySignature.Item == null)
  802. {
  803. throw TraceUtility.ThrowHelperError(new InvalidOperationException(
  804. SR.GetString(SR.SupportingTokenSignaturesNotExpected)), this.Message);
  805. }
  806. }
  807. SecurityKeyIdentifier identifier = new SecurityKeyIdentifier(identifierClause);
  808. ISignatureValueSecurityElement supportingSignature;
  809. if (!this.RequireMessageProtection)
  810. {
  811. supportingSignature = CreateSupportingSignature(token, identifier);
  812. }
  813. else
  814. {
  815. supportingSignature = CreateSupportingSignature(token, identifier, elementContainer.PrimarySignature.Item);
  816. }
  817. AddGeneratedSignatureValue(supportingSignature.GetSignatureValue(), encryptSignature);
  818. SendSecurityHeaderElement supportingSignatureElement = new SendSecurityHeaderElement(supportingSignature.Id, supportingSignature);
  819. supportingSignatureElement.MarkedForEncryption = encryptSignature;
  820. this.elementContainer.AddEndorsingSignature(supportingSignatureElement);
  821. }
  822. void SignWithSupportingTokens()
  823. {
  824. SecurityToken[] endorsingTokens = this.elementContainer.GetEndorsingSupportingTokens();
  825. if (endorsingTokens != null)
  826. {
  827. for (int i = 0; i < endorsingTokens.Length; ++i)
  828. {
  829. SecurityToken source = endorsingTokens[i];
  830. SecurityKeyIdentifierClause sourceKeyClause = endorsingTokenParameters[i].CreateKeyIdentifierClause(source, GetTokenReferenceStyle(endorsingTokenParameters[i]));
  831. if (sourceKeyClause == null)
  832. {
  833. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  834. }
  835. SecurityToken signingToken;
  836. SecurityKeyIdentifierClause signingKeyClause;
  837. if (endorsingTokenParameters[i].RequireDerivedKeys && !endorsingTokenParameters[i].HasAsymmetricKey)
  838. {
  839. string derivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  840. DerivedKeySecurityToken dkt = new DerivedKeySecurityToken(-1, 0,
  841. this.AlgorithmSuite.GetSignatureKeyDerivationLength(source, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion), null,
  842. DerivedKeySecurityToken.DefaultNonceLength, source, sourceKeyClause, derivationAlgorithm, GenerateId());
  843. signingToken = dkt;
  844. signingKeyClause = new LocalIdKeyIdentifierClause(dkt.Id, dkt.GetType());
  845. this.elementContainer.AddEndorsingDerivedSupportingToken(dkt);
  846. }
  847. else
  848. {
  849. signingToken = source;
  850. signingKeyClause = sourceKeyClause;
  851. }
  852. SignWithSupportingToken(signingToken, signingKeyClause);
  853. }
  854. }
  855. SecurityToken[] signedEndorsingSupportingTokens = this.elementContainer.GetSignedEndorsingSupportingTokens();
  856. if (signedEndorsingSupportingTokens != null)
  857. {
  858. for (int i = 0; i < signedEndorsingSupportingTokens.Length; ++i)
  859. {
  860. SecurityToken source = signedEndorsingSupportingTokens[i];
  861. SecurityKeyIdentifierClause sourceKeyClause = signedEndorsingTokenParameters[i].CreateKeyIdentifierClause(source, GetTokenReferenceStyle(signedEndorsingTokenParameters[i]));
  862. if (sourceKeyClause == null)
  863. {
  864. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  865. }
  866. SecurityToken signingToken;
  867. SecurityKeyIdentifierClause signingKeyClause;
  868. if (signedEndorsingTokenParameters[i].RequireDerivedKeys && !signedEndorsingTokenParameters[i].HasAsymmetricKey)
  869. {
  870. string derivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(this.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  871. DerivedKeySecurityToken dkt = new DerivedKeySecurityToken(-1, 0,
  872. this.AlgorithmSuite.GetSignatureKeyDerivationLength(source, this.StandardsManager.MessageSecurityVersion.SecureConversationVersion), null,
  873. DerivedKeySecurityToken.DefaultNonceLength, source, sourceKeyClause, derivationAlgorithm, GenerateId());
  874. signingToken = dkt;
  875. signingKeyClause = new LocalIdKeyIdentifierClause(dkt.Id, dkt.GetType());
  876. this.elementContainer.AddSignedEndorsingDerivedSupportingToken(dkt);
  877. }
  878. else
  879. {
  880. signingToken = source;
  881. signingKeyClause = sourceKeyClause;
  882. }
  883. SignWithSupportingToken(signingToken, signingKeyClause);
  884. }
  885. }
  886. }
  887. protected bool ShouldUseStrTransformForToken(SecurityToken securityToken, int position, SecurityTokenAttachmentMode mode, out SecurityKeyIdentifierClause keyIdentifierClause)
  888. {
  889. IssuedSecurityTokenParameters tokenParams = null;
  890. keyIdentifierClause = null;
  891. switch (mode)
  892. {
  893. case SecurityTokenAttachmentMode.SignedEndorsing:
  894. tokenParams = this.signedEndorsingTokenParameters[position] as IssuedSecurityTokenParameters;
  895. break;
  896. case SecurityTokenAttachmentMode.Signed:
  897. tokenParams = this.signedTokenParameters[position] as IssuedSecurityTokenParameters;
  898. break;
  899. case SecurityTokenAttachmentMode.SignedEncrypted:
  900. tokenParams = this.basicSupportingTokenParameters[position] as IssuedSecurityTokenParameters;
  901. break;
  902. default:
  903. return false;
  904. }
  905. if (tokenParams != null && tokenParams.UseStrTransform)
  906. {
  907. keyIdentifierClause = tokenParams.CreateKeyIdentifierClause(securityToken, GetTokenReferenceStyle(tokenParams));
  908. if (keyIdentifierClause == null)
  909. {
  910. throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TokenManagerCannotCreateTokenReference)), this.Message);
  911. }
  912. return true;
  913. }
  914. return false;
  915. }
  916. XmlDictionaryString IMessageHeaderWithSharedNamespace.SharedNamespace
  917. {
  918. get { return XD.UtilityDictionary.Namespace; }
  919. }
  920. XmlDictionaryString IMessageHeaderWithSharedNamespace.SharedPrefix
  921. {
  922. get { return XD.UtilityDictionary.Prefix; }
  923. }
  924. void AddGeneratedSignatureValue(byte[] signatureValue, bool wasEncrypted)
  925. {
  926. // cache outgoing signatures only on the client side
  927. if (this.MaintainSignatureConfirmationState && (this.signatureConfirmationsToSend == null))
  928. {
  929. if (this.signatureValuesGenerated == null)
  930. {
  931. this.signatureValuesGenerated = new SignatureConfirmations();
  932. }
  933. this.signatureValuesGenerated.AddConfirmation(signatureValue, wasEncrypted);
  934. }
  935. }
  936. }
  937. class TokenElement : ISecurityElement
  938. {
  939. SecurityStandardsManager standardsManager;
  940. SecurityToken token;
  941. public TokenElement(SecurityToken token, SecurityStandardsManager standardsManager)
  942. {
  943. this.token = token;
  944. this.standardsManager = standardsManager;
  945. }
  946. public override bool Equals(object item)
  947. {
  948. TokenElement element = item as TokenElement;
  949. return (element != null && this.token == element.token && this.standardsManager == element.standardsManager);
  950. }
  951. public override int GetHashCode()
  952. {
  953. return token.GetHashCode() ^ standardsManager.GetHashCode();
  954. }
  955. public bool HasId
  956. {
  957. get { return true; }
  958. }
  959. public string Id
  960. {
  961. get { return token.Id; }
  962. }
  963. public SecurityToken Token
  964. {
  965. get { return token; }
  966. }
  967. public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
  968. {
  969. standardsManager.SecurityTokenSerializer.WriteToken(writer, token);
  970. }
  971. }
  972. }