SecurityHeaderTokenResolver.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.IdentityModel.Tokens;
  7. using System.ServiceModel;
  8. using System.IdentityModel.Selectors;
  9. using System.ServiceModel.Security.Tokens;
  10. using System.IO;
  11. using System.Globalization;
  12. using System.Security.Cryptography.X509Certificates;
  13. internal sealed class SecurityHeaderTokenResolver : SecurityTokenResolver, System.IdentityModel.IWrappedTokenKeyResolver
  14. {
  15. const int InitialTokenArraySize = 10;
  16. int tokenCount;
  17. SecurityTokenEntry[] tokens;
  18. SecurityToken expectedWrapper;
  19. SecurityTokenParameters expectedWrapperTokenParameters;
  20. ReceiveSecurityHeader securityHeader;
  21. public SecurityHeaderTokenResolver()
  22. : this(null)
  23. {
  24. }
  25. public SecurityHeaderTokenResolver(ReceiveSecurityHeader securityHeader)
  26. {
  27. this.tokens = new SecurityTokenEntry[InitialTokenArraySize];
  28. this.securityHeader = securityHeader;
  29. }
  30. public SecurityToken ExpectedWrapper
  31. {
  32. get { return this.expectedWrapper; }
  33. set { this.expectedWrapper = value; }
  34. }
  35. public SecurityTokenParameters ExpectedWrapperTokenParameters
  36. {
  37. get { return this.expectedWrapperTokenParameters; }
  38. set { this.expectedWrapperTokenParameters = value; }
  39. }
  40. public void Add(SecurityToken token)
  41. {
  42. Add(token, SecurityTokenReferenceStyle.Internal, null);
  43. }
  44. public void Add(SecurityToken token, SecurityTokenReferenceStyle allowedReferenceStyle, SecurityTokenParameters tokenParameters)
  45. {
  46. if (token == null)
  47. {
  48. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
  49. }
  50. if ((allowedReferenceStyle == SecurityTokenReferenceStyle.External) && (tokenParameters == null))
  51. {
  52. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ResolvingExternalTokensRequireSecurityTokenParameters));
  53. }
  54. EnsureCapacityToAddToken();
  55. this.tokens[this.tokenCount++] = new SecurityTokenEntry(token, tokenParameters, allowedReferenceStyle);
  56. }
  57. void EnsureCapacityToAddToken()
  58. {
  59. if (this.tokenCount == this.tokens.Length)
  60. {
  61. SecurityTokenEntry[] newTokens = new SecurityTokenEntry[this.tokens.Length * 2];
  62. Array.Copy(this.tokens, 0, newTokens, 0, this.tokenCount);
  63. this.tokens = newTokens;
  64. }
  65. }
  66. public bool CheckExternalWrapperMatch(SecurityKeyIdentifier keyIdentifier)
  67. {
  68. if (this.expectedWrapper == null || this.expectedWrapperTokenParameters == null)
  69. {
  70. return false;
  71. }
  72. for (int i = 0; i < keyIdentifier.Count; i++)
  73. {
  74. if (this.expectedWrapperTokenParameters.MatchesKeyIdentifierClause(this.expectedWrapper, keyIdentifier[i], SecurityTokenReferenceStyle.External))
  75. {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. internal SecurityToken ResolveToken(SecurityKeyIdentifier keyIdentifier, bool matchOnlyExternalTokens, bool resolveIntrinsicKeyClause)
  82. {
  83. if (keyIdentifier == null)
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("keyIdentifier");
  86. }
  87. for (int i = 0; i < keyIdentifier.Count; i++)
  88. {
  89. SecurityToken token = ResolveToken(keyIdentifier[i], matchOnlyExternalTokens, resolveIntrinsicKeyClause);
  90. if (token != null)
  91. {
  92. return token;
  93. }
  94. }
  95. return null;
  96. }
  97. SecurityKey ResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, bool createIntrinsicKeys)
  98. {
  99. if (keyIdentifierClause == null)
  100. {
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("keyIdentifierClause"));
  102. }
  103. SecurityKey securityKey;
  104. for (int i = 0; i < this.tokenCount; i++)
  105. {
  106. securityKey = this.tokens[i].Token.ResolveKeyIdentifierClause(keyIdentifierClause);
  107. if (securityKey != null)
  108. {
  109. return securityKey;
  110. }
  111. }
  112. if (createIntrinsicKeys)
  113. {
  114. if (SecurityUtils.TryCreateKeyFromIntrinsicKeyClause(keyIdentifierClause, this, out securityKey))
  115. {
  116. return securityKey;
  117. }
  118. }
  119. return null;
  120. }
  121. bool MatchDirectReference(SecurityToken token, SecurityKeyIdentifierClause keyClause)
  122. {
  123. LocalIdKeyIdentifierClause localClause = keyClause as LocalIdKeyIdentifierClause;
  124. if (localClause == null) return false;
  125. return token.MatchesKeyIdentifierClause(localClause);
  126. }
  127. internal SecurityToken ResolveToken(SecurityKeyIdentifierClause keyIdentifierClause, bool matchOnlyExternal, bool resolveIntrinsicKeyClause)
  128. {
  129. if (keyIdentifierClause == null)
  130. {
  131. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("keyIdentifierClause");
  132. }
  133. SecurityToken resolvedToken = null;
  134. for (int i = 0; i < this.tokenCount; i++)
  135. {
  136. if (matchOnlyExternal && tokens[i].AllowedReferenceStyle != SecurityTokenReferenceStyle.External)
  137. {
  138. continue;
  139. }
  140. SecurityToken token = tokens[i].Token;
  141. if (tokens[i].TokenParameters != null && tokens[i].TokenParameters.MatchesKeyIdentifierClause(token, keyIdentifierClause, tokens[i].AllowedReferenceStyle))
  142. {
  143. resolvedToken = token;
  144. break;
  145. }
  146. else if (tokens[i].TokenParameters == null)
  147. {
  148. // match it according to the allowed reference style
  149. if (tokens[i].AllowedReferenceStyle == SecurityTokenReferenceStyle.Internal && MatchDirectReference(token, keyIdentifierClause))
  150. {
  151. resolvedToken = token;
  152. break;
  153. }
  154. }
  155. }
  156. if ((resolvedToken == null) && (keyIdentifierClause is EncryptedKeyIdentifierClause))
  157. {
  158. EncryptedKeyIdentifierClause keyClause = (EncryptedKeyIdentifierClause)keyIdentifierClause;
  159. SecurityKeyIdentifier wrappingTokenReference = keyClause.EncryptingKeyIdentifier;
  160. SecurityToken unwrappingToken;
  161. if (this.expectedWrapper != null
  162. && CheckExternalWrapperMatch(wrappingTokenReference))
  163. unwrappingToken = this.expectedWrapper;
  164. else
  165. unwrappingToken = ResolveToken(wrappingTokenReference, true, resolveIntrinsicKeyClause);
  166. if (unwrappingToken != null)
  167. {
  168. resolvedToken = SecurityUtils.CreateTokenFromEncryptedKeyClause(keyClause, unwrappingToken);
  169. }
  170. }
  171. if ((resolvedToken == null) && (keyIdentifierClause is X509RawDataKeyIdentifierClause) && (!matchOnlyExternal) && (resolveIntrinsicKeyClause))
  172. {
  173. resolvedToken = new X509SecurityToken(new X509Certificate2(((X509RawDataKeyIdentifierClause)keyIdentifierClause).GetX509RawData()));
  174. }
  175. byte[] derivationNonce = keyIdentifierClause.GetDerivationNonce();
  176. if ((resolvedToken != null) && (derivationNonce != null))
  177. {
  178. // A Implicit Derived Key is specified. Create a derived key off of the resolve token.
  179. if (SecurityUtils.GetSecurityKey<SymmetricSecurityKey>(resolvedToken) == null)
  180. {
  181. // The resolved token contains no Symmetric Security key and thus we cannot create
  182. // a derived key off of it.
  183. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.UnableToDeriveKeyFromKeyInfoClause, keyIdentifierClause, resolvedToken)));
  184. }
  185. int derivationLength = (keyIdentifierClause.DerivationLength == 0) ? DerivedKeySecurityToken.DefaultDerivedKeyLength : keyIdentifierClause.DerivationLength;
  186. if (derivationLength > this.securityHeader.MaxDerivedKeyLength)
  187. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.DerivedKeyLengthSpecifiedInImplicitDerivedKeyClauseTooLong, keyIdentifierClause.ToString(), derivationLength, this.securityHeader.MaxDerivedKeyLength)));
  188. bool alreadyDerived = false;
  189. for (int i = 0; i < this.tokenCount; ++i)
  190. {
  191. DerivedKeySecurityToken derivedKeyToken = this.tokens[i].Token as DerivedKeySecurityToken;
  192. if (derivedKeyToken != null)
  193. {
  194. if ((derivedKeyToken.Length == derivationLength) &&
  195. (CryptoHelper.IsEqual(derivedKeyToken.Nonce, derivationNonce)) &&
  196. (derivedKeyToken.TokenToDerive.MatchesKeyIdentifierClause(keyIdentifierClause)))
  197. {
  198. // This is a implcit derived key for which we have already derived the
  199. // token.
  200. resolvedToken = this.tokens[i].Token;
  201. alreadyDerived = true;
  202. break;
  203. }
  204. }
  205. }
  206. if (!alreadyDerived)
  207. {
  208. string psha1Algorithm = SecurityUtils.GetKeyDerivationAlgorithm(this.securityHeader.StandardsManager.MessageSecurityVersion.SecureConversationVersion);
  209. resolvedToken = new DerivedKeySecurityToken(-1, 0, derivationLength, null, derivationNonce, resolvedToken, keyIdentifierClause, psha1Algorithm, SecurityUtils.GenerateId());
  210. ((DerivedKeySecurityToken)resolvedToken).InitializeDerivedKey(derivationLength);
  211. Add(resolvedToken, SecurityTokenReferenceStyle.Internal, null);
  212. this.securityHeader.EnsureDerivedKeyLimitNotReached();
  213. }
  214. }
  215. return resolvedToken;
  216. }
  217. public override string ToString()
  218. {
  219. using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
  220. {
  221. writer.WriteLine("SecurityTokenResolver");
  222. writer.WriteLine(" (");
  223. writer.WriteLine(" TokenCount = {0},", this.tokenCount);
  224. for (int i = 0; i < this.tokenCount; i++)
  225. {
  226. writer.WriteLine(" TokenEntry[{0}] = (AllowedReferenceStyle={1}, Token={2}, Parameters={3})",
  227. i, this.tokens[i].AllowedReferenceStyle, this.tokens[i].Token.GetType(), tokens[i].TokenParameters);
  228. }
  229. writer.WriteLine(" )");
  230. return writer.ToString();
  231. }
  232. }
  233. protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
  234. {
  235. token = ResolveToken(keyIdentifier, false, true);
  236. return token != null;
  237. }
  238. internal bool TryResolveToken(SecurityKeyIdentifier keyIdentifier, bool matchOnlyExternalTokens, bool resolveIntrinsicKeyClause, out SecurityToken token)
  239. {
  240. token = ResolveToken(keyIdentifier, matchOnlyExternalTokens, resolveIntrinsicKeyClause);
  241. return token != null;
  242. }
  243. protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token)
  244. {
  245. token = ResolveToken(keyIdentifierClause, false, true);
  246. return token != null;
  247. }
  248. internal bool TryResolveToken(SecurityKeyIdentifierClause keyIdentifierClause, bool matchOnlyExternalTokens, bool resolveIntrinsicKeyClause, out SecurityToken token)
  249. {
  250. token = ResolveToken(keyIdentifierClause, matchOnlyExternalTokens, resolveIntrinsicKeyClause);
  251. return token != null;
  252. }
  253. internal bool TryResolveSecurityKey(SecurityKeyIdentifierClause keyIdentifierClause, bool createIntrinsicKeys, out SecurityKey key)
  254. {
  255. if (keyIdentifierClause == null)
  256. {
  257. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("keyIdentifierClause");
  258. }
  259. key = this.ResolveSecurityKeyCore(keyIdentifierClause, createIntrinsicKeys);
  260. return key != null;
  261. }
  262. protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
  263. {
  264. key = ResolveSecurityKeyCore(keyIdentifierClause, true);
  265. return key != null;
  266. }
  267. struct SecurityTokenEntry
  268. {
  269. SecurityTokenParameters tokenParameters;
  270. SecurityToken token;
  271. SecurityTokenReferenceStyle allowedReferenceStyle;
  272. public SecurityTokenEntry(SecurityToken token, SecurityTokenParameters tokenParameters, SecurityTokenReferenceStyle allowedReferenceStyle)
  273. {
  274. this.token = token;
  275. this.tokenParameters = tokenParameters;
  276. this.allowedReferenceStyle = allowedReferenceStyle;
  277. }
  278. public SecurityToken Token
  279. {
  280. get { return this.token; }
  281. }
  282. public SecurityTokenParameters TokenParameters
  283. {
  284. get { return this.tokenParameters; }
  285. }
  286. public SecurityTokenReferenceStyle AllowedReferenceStyle
  287. {
  288. get { return this.allowedReferenceStyle; }
  289. }
  290. }
  291. }
  292. }