SslSecurityTokenParameters.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // SslSecurityTokenParameters.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.IdentityModel.Selectors;
  29. using System.IdentityModel.Tokens;
  30. using System.ServiceModel.Security;
  31. using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  32. namespace System.ServiceModel.Security.Tokens
  33. {
  34. public class SslSecurityTokenParameters : SecurityTokenParameters
  35. {
  36. public SslSecurityTokenParameters ()
  37. : this (false, false)
  38. {
  39. }
  40. public SslSecurityTokenParameters (bool requireClientCertificate)
  41. : this (requireClientCertificate, false)
  42. {
  43. }
  44. public SslSecurityTokenParameters (bool requireClientCertificate,
  45. bool requireCancellation)
  46. {
  47. this.cert = requireClientCertificate;
  48. this.cancel = requireCancellation;
  49. }
  50. protected SslSecurityTokenParameters (SslSecurityTokenParameters source)
  51. : base (source)
  52. {
  53. cert = source.cert;
  54. cancel = source.cancel;
  55. }
  56. bool cert, cancel;
  57. public bool RequireClientCertificate {
  58. get { return cert; }
  59. set { cert = value; }
  60. }
  61. public bool RequireCancellation {
  62. get { return cancel; }
  63. set { cancel = value; }
  64. }
  65. public override string ToString ()
  66. {
  67. return base.ToString ();
  68. }
  69. protected override bool HasAsymmetricKey {
  70. get { return false; }
  71. }
  72. protected override bool SupportsClientAuthentication {
  73. get { return false; }
  74. }
  75. protected override bool SupportsClientWindowsIdentity {
  76. get { return false; }
  77. }
  78. protected override bool SupportsServerAuthentication {
  79. get { return true; }
  80. }
  81. protected override SecurityTokenParameters CloneCore ()
  82. {
  83. return new SslSecurityTokenParameters (this);
  84. }
  85. protected override SecurityKeyIdentifierClause CreateKeyIdentifierClause (
  86. SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
  87. {
  88. if (token == null)
  89. throw new ArgumentNullException ("token");
  90. SecurityContextSecurityToken sct = token as SecurityContextSecurityToken;
  91. if (sct == null)
  92. throw new ArgumentException (String.Format ("Not supported SecurityToken: '{0}'", token));
  93. return referenceStyle == SecurityTokenReferenceStyle.Internal ?
  94. (SecurityKeyIdentifierClause)
  95. new LocalIdKeyIdentifierClause (sct.Id) :
  96. new SecurityContextKeyIdentifierClause (sct.ContextId, sct.KeyGeneration);
  97. /*
  98. GenericXmlSecurityToken x = token as GenericXmlSecurityToken;
  99. if (x == null)
  100. throw new ArgumentException (String.Format ("Not supported SecurityToken: '{0}'", token));
  101. return referenceStyle == SecurityTokenReferenceStyle.Internal ? x.InternalTokenReference : x.ExternalTokenReference;
  102. */
  103. }
  104. protected override void InitializeSecurityTokenRequirement (SecurityTokenRequirement requirement)
  105. {
  106. requirement.TokenType =
  107. RequireClientCertificate ?
  108. ServiceModelSecurityTokenTypes.MutualSslnego :
  109. ServiceModelSecurityTokenTypes.AnonymousSslnego;
  110. requirement.RequireCryptographicToken = true;
  111. requirement.Properties [ReqType.SupportSecurityContextCancellationProperty] = RequireCancellation;
  112. requirement.Properties [ReqType.IssuedSecurityTokenParametersProperty] = this.Clone ();
  113. requirement.KeyType = SecurityKeyType.SymmetricKey;
  114. }
  115. }
  116. }