X509ClientCertificateAuthentication.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.IdentityModel.Selectors;
  7. using System.ServiceModel;
  8. using System.Security.Cryptography.X509Certificates;
  9. public class X509ClientCertificateAuthentication
  10. {
  11. internal const X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidationMode.ChainTrust;
  12. internal const X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online;
  13. internal const StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine;
  14. internal const bool DefaultMapCertificateToWindowsAccount = false;
  15. static X509CertificateValidator defaultCertificateValidator;
  16. X509CertificateValidationMode certificateValidationMode = DefaultCertificateValidationMode;
  17. X509RevocationMode revocationMode = DefaultRevocationMode;
  18. StoreLocation trustedStoreLocation = DefaultTrustedStoreLocation;
  19. X509CertificateValidator customCertificateValidator = null;
  20. bool mapClientCertificateToWindowsAccount = DefaultMapCertificateToWindowsAccount;
  21. bool includeWindowsGroups = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims;
  22. bool isReadOnly;
  23. internal X509ClientCertificateAuthentication()
  24. {
  25. }
  26. internal X509ClientCertificateAuthentication(X509ClientCertificateAuthentication other)
  27. {
  28. this.certificateValidationMode = other.certificateValidationMode;
  29. this.customCertificateValidator = other.customCertificateValidator;
  30. this.includeWindowsGroups = other.includeWindowsGroups;
  31. this.mapClientCertificateToWindowsAccount = other.mapClientCertificateToWindowsAccount;
  32. this.trustedStoreLocation = other.trustedStoreLocation;
  33. this.revocationMode = other.revocationMode;
  34. this.isReadOnly = other.isReadOnly;
  35. }
  36. internal static X509CertificateValidator DefaultCertificateValidator
  37. {
  38. get
  39. {
  40. if (defaultCertificateValidator == null)
  41. {
  42. bool useMachineContext = DefaultTrustedStoreLocation == StoreLocation.LocalMachine;
  43. X509ChainPolicy chainPolicy = new X509ChainPolicy();
  44. chainPolicy.RevocationMode = DefaultRevocationMode;
  45. defaultCertificateValidator = X509CertificateValidator.CreateChainTrustValidator(useMachineContext, chainPolicy);
  46. }
  47. return defaultCertificateValidator;
  48. }
  49. }
  50. public X509CertificateValidationMode CertificateValidationMode
  51. {
  52. get
  53. {
  54. return this.certificateValidationMode;
  55. }
  56. set
  57. {
  58. X509CertificateValidationModeHelper.Validate(value);
  59. ThrowIfImmutable();
  60. this.certificateValidationMode = value;
  61. }
  62. }
  63. public X509RevocationMode RevocationMode
  64. {
  65. get
  66. {
  67. return this.revocationMode;
  68. }
  69. set
  70. {
  71. ThrowIfImmutable();
  72. this.revocationMode = value;
  73. }
  74. }
  75. public StoreLocation TrustedStoreLocation
  76. {
  77. get
  78. {
  79. return this.trustedStoreLocation;
  80. }
  81. set
  82. {
  83. ThrowIfImmutable();
  84. this.trustedStoreLocation = value;
  85. }
  86. }
  87. public X509CertificateValidator CustomCertificateValidator
  88. {
  89. get
  90. {
  91. return this.customCertificateValidator;
  92. }
  93. set
  94. {
  95. ThrowIfImmutable();
  96. this.customCertificateValidator = value;
  97. }
  98. }
  99. public bool MapClientCertificateToWindowsAccount
  100. {
  101. get
  102. {
  103. return this.mapClientCertificateToWindowsAccount;
  104. }
  105. set
  106. {
  107. ThrowIfImmutable();
  108. this.mapClientCertificateToWindowsAccount = value;
  109. }
  110. }
  111. public bool IncludeWindowsGroups
  112. {
  113. get
  114. {
  115. return this.includeWindowsGroups;
  116. }
  117. set
  118. {
  119. ThrowIfImmutable();
  120. this.includeWindowsGroups = value;
  121. }
  122. }
  123. internal X509CertificateValidator GetCertificateValidator()
  124. {
  125. if (this.certificateValidationMode == X509CertificateValidationMode.None)
  126. {
  127. return X509CertificateValidator.None;
  128. }
  129. else if (this.certificateValidationMode == X509CertificateValidationMode.PeerTrust)
  130. {
  131. return X509CertificateValidator.PeerTrust;
  132. }
  133. else if (this.certificateValidationMode == X509CertificateValidationMode.Custom)
  134. {
  135. if (this.customCertificateValidator == null)
  136. {
  137. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MissingCustomCertificateValidator)));
  138. }
  139. return this.customCertificateValidator;
  140. }
  141. else
  142. {
  143. bool useMachineContext = this.trustedStoreLocation == StoreLocation.LocalMachine;
  144. X509ChainPolicy chainPolicy = new X509ChainPolicy();
  145. chainPolicy.RevocationMode = this.revocationMode;
  146. if (this.certificateValidationMode == X509CertificateValidationMode.ChainTrust)
  147. {
  148. return X509CertificateValidator.CreateChainTrustValidator(useMachineContext, chainPolicy);
  149. }
  150. else
  151. {
  152. return X509CertificateValidator.CreatePeerOrChainTrustValidator(useMachineContext, chainPolicy);
  153. }
  154. }
  155. }
  156. internal void MakeReadOnly()
  157. {
  158. this.isReadOnly = true;
  159. }
  160. void ThrowIfImmutable()
  161. {
  162. if (this.isReadOnly)
  163. {
  164. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  165. }
  166. }
  167. }
  168. }