WrappedKeySecurityToken.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // WrappedKeySecurityToken.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.ObjectModel;
  30. using System.Security.Cryptography;
  31. using System.Security.Cryptography.Xml;
  32. using System.Xml;
  33. using System.IdentityModel.Policy;
  34. using System.IdentityModel.Tokens;
  35. namespace System.ServiceModel.Security.Tokens
  36. {
  37. public class WrappedKeySecurityToken : SecurityToken
  38. {
  39. string id;
  40. byte [] raw_key;
  41. byte [] wrapped_key;
  42. string wrap_alg;
  43. SecurityToken wrap_token;
  44. SecurityKeyIdentifier wrap_token_ref;
  45. DateTime valid_from = DateTime.Now.ToUniversalTime ();
  46. ReadOnlyCollection<SecurityKey> keys;
  47. ReferenceList reference_list;
  48. byte [] keyhash;
  49. public WrappedKeySecurityToken (
  50. string id,
  51. byte [] keyToWrap,
  52. string wrappingAlgorithm,
  53. SecurityToken wrappingToken,
  54. SecurityKeyIdentifier wrappingTokenReference)
  55. {
  56. if (id == null)
  57. throw new ArgumentNullException ("id");
  58. if (keyToWrap == null)
  59. throw new ArgumentNullException ("keyToWrap");
  60. if (wrappingAlgorithm == null)
  61. throw new ArgumentNullException ("wrappingAlgorithm");
  62. if (wrappingToken == null)
  63. throw new ArgumentNullException ("wrappingToken");
  64. raw_key = keyToWrap;
  65. this.id = id;
  66. wrap_alg = wrappingAlgorithm;
  67. wrap_token = wrappingToken;
  68. wrap_token_ref = wrappingTokenReference;
  69. Collection<SecurityKey> l = new Collection<SecurityKey> ();
  70. foreach (SecurityKey sk in wrappingToken.SecurityKeys) {
  71. if (sk.IsSupportedAlgorithm (wrappingAlgorithm)) {
  72. wrapped_key = sk.EncryptKey (wrappingAlgorithm, keyToWrap);
  73. l.Add (new InMemorySymmetricSecurityKey (keyToWrap));
  74. break;
  75. }
  76. }
  77. keys = new ReadOnlyCollection<SecurityKey> (l);
  78. if (wrapped_key == null)
  79. throw new ArgumentException (String.Format ("None of the security keys in the argument token supports specified wrapping algorithm '{0}'", wrappingAlgorithm));
  80. }
  81. internal byte [] RawKey {
  82. get { return raw_key; }
  83. }
  84. // It is kind of compromised solution to output
  85. // ReferenceList inside e:EncryptedKey and might disappear
  86. // when non-wrapped key is represented by another token type.
  87. internal ReferenceList ReferenceList {
  88. get { return reference_list; }
  89. set { reference_list = value; }
  90. }
  91. public override DateTime ValidFrom {
  92. get { return valid_from; }
  93. }
  94. public override DateTime ValidTo {
  95. get { return DateTime.MaxValue.AddDays (-1); }
  96. }
  97. public override string Id {
  98. get { return id; }
  99. }
  100. public override ReadOnlyCollection<SecurityKey> SecurityKeys {
  101. get { return keys; }
  102. }
  103. public string WrappingAlgorithm {
  104. get { return wrap_alg; }
  105. }
  106. public SecurityToken WrappingToken {
  107. get { return wrap_token; }
  108. }
  109. public SecurityKeyIdentifier WrappingTokenReference {
  110. get { return wrap_token_ref; }
  111. }
  112. public byte [] GetWrappedKey ()
  113. {
  114. return (byte []) wrapped_key.Clone ();
  115. }
  116. internal void SetWrappedKey (byte [] value)
  117. {
  118. wrapped_key = (byte []) value.Clone ();
  119. }
  120. [MonoTODO]
  121. public override bool CanCreateKeyIdentifierClause<T> ()
  122. {
  123. /*
  124. foreach (SecurityKeyIdentifierClause k in WrappingTokenReference) {
  125. Type t = k.GetType ();
  126. if (t == typeof (T) || t.IsSubclassOf (typeof (T)))
  127. return true;
  128. }
  129. */
  130. return false;
  131. }
  132. [MonoTODO]
  133. public override T CreateKeyIdentifierClause<T> ()
  134. {
  135. /*
  136. foreach (SecurityKeyIdentifierClause k in WrappingTokenReference) {
  137. Type t = k.GetType ();
  138. if (t == typeof (T) || t.IsSubclassOf (typeof (T)))
  139. return (T) k;
  140. }
  141. */
  142. throw new NotSupportedException (String.Format ("WrappedKeySecurityToken cannot create '{0}'", typeof (T)));
  143. }
  144. public override bool MatchesKeyIdentifierClause (SecurityKeyIdentifierClause keyIdentifierClause)
  145. {
  146. LocalIdKeyIdentifierClause lkic = keyIdentifierClause as LocalIdKeyIdentifierClause;
  147. if (lkic != null && lkic.LocalId == Id)
  148. return true;
  149. InternalEncryptedKeyIdentifierClause khic = keyIdentifierClause as InternalEncryptedKeyIdentifierClause;
  150. if (keyhash == null)
  151. keyhash = SHA1.Create ().ComputeHash (wrapped_key);
  152. if (khic != null && khic.Matches (keyhash))
  153. return true;
  154. return false;
  155. }
  156. }
  157. }