RequestSecurityTokenResponse.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.Xml;
  10. using System.Security.Cryptography;
  11. using System.Security.Cryptography.Xml;
  12. using System.Runtime.Serialization;
  13. using System.Xml.Serialization;
  14. using System.Xml.Schema;
  15. using System.Security.Principal;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.IdentityModel.Claims;
  19. using System.IdentityModel.Policy;
  20. using System.IdentityModel.Tokens;
  21. using System.IdentityModel.Selectors;
  22. using System.ServiceModel.Security.Tokens;
  23. using System.IO;
  24. using System.ServiceModel.Security;
  25. using Psha1DerivedKeyGenerator = System.IdentityModel.Psha1DerivedKeyGenerator;
  26. using System.ServiceModel.Dispatcher;
  27. class RequestSecurityTokenResponse : BodyWriter
  28. {
  29. static int minSaneKeySizeInBits = 8 * 8; // 8 Bytes.
  30. static int maxSaneKeySizeInBits = (16 * 1024) * 8; // 16 K
  31. SecurityStandardsManager standardsManager;
  32. string context;
  33. int keySize;
  34. bool computeKey;
  35. string tokenType;
  36. SecurityKeyIdentifierClause requestedAttachedReference;
  37. SecurityKeyIdentifierClause requestedUnattachedReference;
  38. SecurityToken issuedToken;
  39. SecurityToken proofToken;
  40. SecurityToken entropyToken;
  41. BinaryNegotiation negotiationData;
  42. XmlElement rstrXml;
  43. DateTime effectiveTime;
  44. DateTime expirationTime;
  45. bool isLifetimeSet;
  46. byte[] authenticator;
  47. bool isReceiver;
  48. bool isReadOnly;
  49. byte[] cachedWriteBuffer;
  50. int cachedWriteBufferLength;
  51. bool isRequestedTokenClosed;
  52. object appliesTo;
  53. XmlObjectSerializer appliesToSerializer;
  54. Type appliesToType;
  55. Object thisLock = new Object();
  56. System.IdentityModel.XmlBuffer issuedTokenBuffer;
  57. public RequestSecurityTokenResponse()
  58. : this(SecurityStandardsManager.DefaultInstance)
  59. {
  60. }
  61. public RequestSecurityTokenResponse(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer securityTokenSerializer)
  62. : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer))
  63. {
  64. }
  65. public RequestSecurityTokenResponse(XmlElement requestSecurityTokenResponseXml,
  66. string context,
  67. string tokenType,
  68. int keySize,
  69. SecurityKeyIdentifierClause requestedAttachedReference,
  70. SecurityKeyIdentifierClause requestedUnattachedReference,
  71. bool computeKey,
  72. DateTime validFrom,
  73. DateTime validTo,
  74. bool isRequestedTokenClosed)
  75. : this(SecurityStandardsManager.DefaultInstance,
  76. requestSecurityTokenResponseXml,
  77. context,
  78. tokenType,
  79. keySize,
  80. requestedAttachedReference,
  81. requestedUnattachedReference,
  82. computeKey,
  83. validFrom,
  84. validTo,
  85. isRequestedTokenClosed,
  86. null)
  87. {
  88. }
  89. public RequestSecurityTokenResponse(MessageSecurityVersion messageSecurityVersion,
  90. SecurityTokenSerializer securityTokenSerializer,
  91. XmlElement requestSecurityTokenResponseXml,
  92. string context,
  93. string tokenType,
  94. int keySize,
  95. SecurityKeyIdentifierClause requestedAttachedReference,
  96. SecurityKeyIdentifierClause requestedUnattachedReference,
  97. bool computeKey,
  98. DateTime validFrom,
  99. DateTime validTo,
  100. bool isRequestedTokenClosed)
  101. : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer),
  102. requestSecurityTokenResponseXml,
  103. context,
  104. tokenType,
  105. keySize,
  106. requestedAttachedReference,
  107. requestedUnattachedReference,
  108. computeKey,
  109. validFrom,
  110. validTo,
  111. isRequestedTokenClosed,
  112. null)
  113. {
  114. }
  115. internal RequestSecurityTokenResponse(SecurityStandardsManager standardsManager)
  116. : base(true)
  117. {
  118. if (standardsManager == null)
  119. {
  120. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
  121. }
  122. this.standardsManager = standardsManager;
  123. effectiveTime = SecurityUtils.MinUtcDateTime;
  124. expirationTime = SecurityUtils.MaxUtcDateTime;
  125. isRequestedTokenClosed = false;
  126. this.isLifetimeSet = false;
  127. this.isReceiver = false;
  128. this.isReadOnly = false;
  129. }
  130. internal RequestSecurityTokenResponse(SecurityStandardsManager standardsManager,
  131. XmlElement rstrXml,
  132. string context,
  133. string tokenType,
  134. int keySize,
  135. SecurityKeyIdentifierClause requestedAttachedReference,
  136. SecurityKeyIdentifierClause requestedUnattachedReference,
  137. bool computeKey,
  138. DateTime validFrom,
  139. DateTime validTo,
  140. bool isRequestedTokenClosed,
  141. System.IdentityModel.XmlBuffer issuedTokenBuffer)
  142. : base(true)
  143. {
  144. if (standardsManager == null)
  145. {
  146. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
  147. }
  148. this.standardsManager = standardsManager;
  149. if (rstrXml == null)
  150. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rstrXml");
  151. this.rstrXml = rstrXml;
  152. this.context = context;
  153. this.tokenType = tokenType;
  154. this.keySize = keySize;
  155. this.requestedAttachedReference = requestedAttachedReference;
  156. this.requestedUnattachedReference = requestedUnattachedReference;
  157. this.computeKey = computeKey;
  158. this.effectiveTime = validFrom.ToUniversalTime();
  159. this.expirationTime = validTo.ToUniversalTime();
  160. this.isLifetimeSet = true;
  161. this.isRequestedTokenClosed = isRequestedTokenClosed;
  162. this.issuedTokenBuffer = issuedTokenBuffer;
  163. this.isReceiver = true;
  164. this.isReadOnly = true;
  165. }
  166. public string Context
  167. {
  168. get
  169. {
  170. return this.context;
  171. }
  172. set
  173. {
  174. if (this.IsReadOnly)
  175. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  176. this.context = value;
  177. }
  178. }
  179. public string TokenType
  180. {
  181. get
  182. {
  183. return this.tokenType;
  184. }
  185. set
  186. {
  187. if (this.IsReadOnly)
  188. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  189. this.tokenType = value;
  190. }
  191. }
  192. public SecurityKeyIdentifierClause RequestedAttachedReference
  193. {
  194. get
  195. {
  196. return this.requestedAttachedReference;
  197. }
  198. set
  199. {
  200. if (this.IsReadOnly)
  201. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  202. this.requestedAttachedReference = value;
  203. }
  204. }
  205. public SecurityKeyIdentifierClause RequestedUnattachedReference
  206. {
  207. get
  208. {
  209. return this.requestedUnattachedReference;
  210. }
  211. set
  212. {
  213. if (this.IsReadOnly)
  214. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  215. this.requestedUnattachedReference = value;
  216. }
  217. }
  218. public DateTime ValidFrom
  219. {
  220. get
  221. {
  222. return this.effectiveTime;
  223. }
  224. }
  225. public DateTime ValidTo
  226. {
  227. get
  228. {
  229. return this.expirationTime;
  230. }
  231. }
  232. public bool ComputeKey
  233. {
  234. get
  235. {
  236. return this.computeKey;
  237. }
  238. set
  239. {
  240. if (this.IsReadOnly)
  241. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  242. this.computeKey = value;
  243. }
  244. }
  245. public int KeySize
  246. {
  247. get
  248. {
  249. return this.keySize;
  250. }
  251. set
  252. {
  253. if (this.IsReadOnly)
  254. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  255. if (value < 0)
  256. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", SR.GetString(SR.ValueMustBeNonNegative)));
  257. this.keySize = value;
  258. }
  259. }
  260. public bool IsRequestedTokenClosed
  261. {
  262. get
  263. {
  264. return this.isRequestedTokenClosed;
  265. }
  266. set
  267. {
  268. if (this.IsReadOnly)
  269. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  270. this.isRequestedTokenClosed = value;
  271. }
  272. }
  273. public bool IsReadOnly
  274. {
  275. get
  276. {
  277. return this.isReadOnly;
  278. }
  279. }
  280. protected Object ThisLock
  281. {
  282. get
  283. {
  284. return this.thisLock;
  285. }
  286. }
  287. internal bool IsReceiver
  288. {
  289. get
  290. {
  291. return this.isReceiver;
  292. }
  293. }
  294. internal SecurityStandardsManager StandardsManager
  295. {
  296. get
  297. {
  298. return this.standardsManager;
  299. }
  300. set
  301. {
  302. if (this.IsReadOnly)
  303. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  304. this.standardsManager = (value != null ? value : SecurityStandardsManager.DefaultInstance);
  305. }
  306. }
  307. public SecurityToken EntropyToken
  308. {
  309. get
  310. {
  311. if (this.isReceiver)
  312. {
  313. // PreSharp Bug: Property get methods should not throw exceptions.
  314. #pragma warning suppress 56503
  315. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRSTR, "EntropyToken")));
  316. }
  317. return this.entropyToken;
  318. }
  319. }
  320. public SecurityToken RequestedSecurityToken
  321. {
  322. get
  323. {
  324. if (this.isReceiver)
  325. {
  326. // PreSharp Bug: Property get methods should not throw exceptions.
  327. #pragma warning suppress 56503
  328. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRSTR, "IssuedToken")));
  329. }
  330. return this.issuedToken;
  331. }
  332. set
  333. {
  334. if (this.isReadOnly)
  335. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  336. this.issuedToken = value;
  337. }
  338. }
  339. public SecurityToken RequestedProofToken
  340. {
  341. get
  342. {
  343. if (this.isReceiver)
  344. {
  345. // PreSharp Bug: Property get methods should not throw exceptions.
  346. #pragma warning suppress 56503
  347. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRSTR, "ProofToken")));
  348. }
  349. return this.proofToken;
  350. }
  351. set
  352. {
  353. if (this.isReadOnly)
  354. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  355. this.proofToken = value;
  356. }
  357. }
  358. public XmlElement RequestSecurityTokenResponseXml
  359. {
  360. get
  361. {
  362. if (!this.isReceiver)
  363. {
  364. // PreSharp Bug: Property get methods should not throw exceptions.
  365. #pragma warning suppress 56503
  366. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTROnly, "RequestSecurityTokenXml")));
  367. }
  368. return this.rstrXml;
  369. }
  370. }
  371. internal object AppliesTo
  372. {
  373. get
  374. {
  375. if (this.isReceiver)
  376. {
  377. // PreSharp Bug: Property get methods should not throw exceptions.
  378. #pragma warning suppress 56503
  379. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesTo")));
  380. }
  381. return this.appliesTo;
  382. }
  383. }
  384. internal XmlObjectSerializer AppliesToSerializer
  385. {
  386. get
  387. {
  388. if (this.isReceiver)
  389. {
  390. // PreSharp Bug: Property get methods should not throw exceptions.
  391. #pragma warning suppress 56503
  392. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesToSerializer")));
  393. }
  394. return this.appliesToSerializer;
  395. }
  396. }
  397. internal Type AppliesToType
  398. {
  399. get
  400. {
  401. if (this.isReceiver)
  402. {
  403. // PreSharp Bug: Property get methods should not throw exceptions.
  404. #pragma warning suppress 56503
  405. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesToType")));
  406. }
  407. return this.appliesToType;
  408. }
  409. }
  410. internal bool IsLifetimeSet
  411. {
  412. get
  413. {
  414. if (this.isReceiver)
  415. {
  416. // PreSharp Bug: Property get methods should not throw exceptions.
  417. #pragma warning suppress 56503
  418. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRSTR, "IsLifetimeSet")));
  419. }
  420. return this.isLifetimeSet;
  421. }
  422. }
  423. internal System.IdentityModel.XmlBuffer IssuedTokenBuffer
  424. {
  425. get
  426. {
  427. return this.issuedTokenBuffer;
  428. }
  429. }
  430. public void SetIssuerEntropy(byte[] issuerEntropy)
  431. {
  432. if (this.IsReadOnly)
  433. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  434. this.entropyToken = (issuerEntropy != null) ? new NonceToken(issuerEntropy) : null;
  435. }
  436. internal void SetIssuerEntropy(WrappedKeySecurityToken issuerEntropy)
  437. {
  438. if (this.IsReadOnly)
  439. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  440. this.entropyToken = issuerEntropy;
  441. }
  442. public SecurityToken GetIssuerEntropy()
  443. {
  444. return this.GetIssuerEntropy(null);
  445. }
  446. internal SecurityToken GetIssuerEntropy(SecurityTokenResolver resolver)
  447. {
  448. if (this.isReceiver)
  449. {
  450. return this.standardsManager.TrustDriver.GetEntropy(this, resolver);
  451. }
  452. else
  453. return this.entropyToken;
  454. }
  455. public void SetLifetime(DateTime validFrom, DateTime validTo)
  456. {
  457. if (this.IsReadOnly)
  458. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  459. if (validFrom.ToUniversalTime() > validTo.ToUniversalTime())
  460. {
  461. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.EffectiveGreaterThanExpiration));
  462. }
  463. this.effectiveTime = validFrom.ToUniversalTime();
  464. this.expirationTime = validTo.ToUniversalTime();
  465. this.isLifetimeSet = true;
  466. }
  467. public void SetAppliesTo<T>(T appliesTo, XmlObjectSerializer serializer)
  468. {
  469. if (this.IsReadOnly)
  470. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  471. if (appliesTo != null && serializer == null)
  472. {
  473. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer");
  474. }
  475. this.appliesTo = appliesTo;
  476. this.appliesToSerializer = serializer;
  477. this.appliesToType = typeof(T);
  478. }
  479. public void GetAppliesToQName(out string localName, out string namespaceUri)
  480. {
  481. if (!this.isReceiver)
  482. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTOnly, "MatchesAppliesTo")));
  483. this.standardsManager.TrustDriver.GetAppliesToQName(this, out localName, out namespaceUri);
  484. }
  485. public T GetAppliesTo<T>()
  486. {
  487. return this.GetAppliesTo<T>(DataContractSerializerDefaults.CreateSerializer(typeof(T), DataContractSerializerDefaults.MaxItemsInObjectGraph));
  488. }
  489. public T GetAppliesTo<T>(XmlObjectSerializer serializer)
  490. {
  491. if (this.isReceiver)
  492. {
  493. if (serializer == null)
  494. {
  495. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer");
  496. }
  497. return this.standardsManager.TrustDriver.GetAppliesTo<T>(this, serializer);
  498. }
  499. else
  500. {
  501. return (T)this.appliesTo;
  502. }
  503. }
  504. internal void SetBinaryNegotiation(BinaryNegotiation negotiation)
  505. {
  506. if (negotiation == null)
  507. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("negotiation");
  508. if (this.IsReadOnly)
  509. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  510. this.negotiationData = negotiation;
  511. }
  512. internal BinaryNegotiation GetBinaryNegotiation()
  513. {
  514. if (this.isReceiver)
  515. return this.standardsManager.TrustDriver.GetBinaryNegotiation(this);
  516. else
  517. return this.negotiationData;
  518. }
  519. internal void SetAuthenticator(byte[] authenticator)
  520. {
  521. if (authenticator == null)
  522. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("authenticator");
  523. if (this.IsReadOnly)
  524. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  525. this.authenticator = DiagnosticUtility.Utility.AllocateByteArray(authenticator.Length);
  526. Buffer.BlockCopy(authenticator, 0, this.authenticator, 0, authenticator.Length);
  527. }
  528. internal byte[] GetAuthenticator()
  529. {
  530. if (this.isReceiver)
  531. return this.standardsManager.TrustDriver.GetAuthenticator(this);
  532. else
  533. {
  534. if (this.authenticator == null)
  535. return null;
  536. else
  537. {
  538. byte[] result = DiagnosticUtility.Utility.AllocateByteArray(this.authenticator.Length);
  539. Buffer.BlockCopy(this.authenticator, 0, result, 0, this.authenticator.Length);
  540. return result;
  541. }
  542. }
  543. }
  544. void OnWriteTo(XmlWriter w)
  545. {
  546. if (this.isReceiver)
  547. {
  548. this.rstrXml.WriteTo(w);
  549. }
  550. else
  551. {
  552. this.standardsManager.TrustDriver.WriteRequestSecurityTokenResponse(this, w);
  553. }
  554. }
  555. public void WriteTo(XmlWriter writer)
  556. {
  557. if (writer == null)
  558. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  559. if (this.IsReadOnly)
  560. {
  561. // cache the serialized bytes to ensure repeatability
  562. if (this.cachedWriteBuffer == null)
  563. {
  564. MemoryStream stream = new MemoryStream();
  565. using (XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, XD.Dictionary))
  566. {
  567. this.OnWriteTo(binaryWriter);
  568. binaryWriter.Flush();
  569. stream.Flush();
  570. stream.Seek(0, SeekOrigin.Begin);
  571. this.cachedWriteBuffer = stream.GetBuffer();
  572. this.cachedWriteBufferLength = (int)stream.Length;
  573. }
  574. }
  575. writer.WriteNode(XmlDictionaryReader.CreateBinaryReader(this.cachedWriteBuffer, 0, this.cachedWriteBufferLength, XD.Dictionary, XmlDictionaryReaderQuotas.Max), false);
  576. }
  577. else
  578. this.OnWriteTo(writer);
  579. }
  580. public static RequestSecurityTokenResponse CreateFrom(XmlReader reader)
  581. {
  582. return CreateFrom(SecurityStandardsManager.DefaultInstance, reader);
  583. }
  584. public static RequestSecurityTokenResponse CreateFrom(XmlReader reader, MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer securityTokenSerializer)
  585. {
  586. return CreateFrom(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer), reader);
  587. }
  588. internal static RequestSecurityTokenResponse CreateFrom(SecurityStandardsManager standardsManager, XmlReader reader)
  589. {
  590. return standardsManager.TrustDriver.CreateRequestSecurityTokenResponse(reader);
  591. }
  592. protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
  593. {
  594. WriteTo(writer);
  595. }
  596. public void MakeReadOnly()
  597. {
  598. if (!this.isReadOnly)
  599. {
  600. this.isReadOnly = true;
  601. this.OnMakeReadOnly();
  602. }
  603. }
  604. public GenericXmlSecurityToken GetIssuedToken(SecurityTokenResolver resolver, IList<SecurityTokenAuthenticator> allowedAuthenticators, SecurityKeyEntropyMode keyEntropyMode, byte[] requestorEntropy, string expectedTokenType,
  605. ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies)
  606. {
  607. return this.GetIssuedToken(resolver, allowedAuthenticators, keyEntropyMode, requestorEntropy, expectedTokenType, authorizationPolicies, 0, false);
  608. }
  609. public virtual GenericXmlSecurityToken GetIssuedToken(SecurityTokenResolver resolver, IList<SecurityTokenAuthenticator> allowedAuthenticators, SecurityKeyEntropyMode keyEntropyMode, byte[] requestorEntropy, string expectedTokenType,
  610. ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, int defaultKeySize, bool isBearerKeyType)
  611. {
  612. if (!this.isReceiver)
  613. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTROnly, "GetIssuedToken")));
  614. return this.standardsManager.TrustDriver.GetIssuedToken(this, resolver, allowedAuthenticators, keyEntropyMode, requestorEntropy, expectedTokenType, authorizationPolicies, defaultKeySize, isBearerKeyType);
  615. }
  616. public virtual GenericXmlSecurityToken GetIssuedToken(string expectedTokenType, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, RSA clientKey)
  617. {
  618. if (!this.isReceiver)
  619. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTROnly, "GetIssuedToken")));
  620. return this.standardsManager.TrustDriver.GetIssuedToken(this, expectedTokenType, authorizationPolicies, clientKey);
  621. }
  622. protected internal virtual void OnWriteCustomAttributes(XmlWriter writer)
  623. { }
  624. protected internal virtual void OnWriteCustomElements(XmlWriter writer)
  625. { }
  626. protected virtual void OnMakeReadOnly() { }
  627. public static byte[] ComputeCombinedKey(byte[] requestorEntropy, byte[] issuerEntropy, int keySizeInBits)
  628. {
  629. if (requestorEntropy == null)
  630. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestorEntropy");
  631. if (issuerEntropy == null)
  632. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("issuerEntropy");
  633. // Do a sanity check here. We don't want to allow invalid keys or keys that are too
  634. // large.
  635. if ((keySizeInBits < minSaneKeySizeInBits) || (keySizeInBits > maxSaneKeySizeInBits))
  636. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.GetString(SR.InvalidKeySizeSpecifiedInNegotiation, keySizeInBits, minSaneKeySizeInBits, maxSaneKeySizeInBits)));
  637. Psha1DerivedKeyGenerator generator = new Psha1DerivedKeyGenerator(requestorEntropy);
  638. return generator.GenerateDerivedKey(new byte[] { }, issuerEntropy, keySizeInBits, 0);
  639. }
  640. }
  641. }