X509ServiceCertificateAuthenticationElement.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.ServiceModel;
  8. using System.Configuration;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Security;
  11. using System.Xml;
  12. using System.Security.Cryptography.X509Certificates;
  13. using System.IdentityModel.Selectors;
  14. public sealed partial class X509ServiceCertificateAuthenticationElement : ConfigurationElement
  15. {
  16. public X509ServiceCertificateAuthenticationElement()
  17. {
  18. }
  19. [ConfigurationProperty(ConfigurationStrings.CustomCertificateValidatorType, DefaultValue = "")]
  20. [StringValidator(MinLength = 0)]
  21. public string CustomCertificateValidatorType
  22. {
  23. get { return (string)base[ConfigurationStrings.CustomCertificateValidatorType]; }
  24. set
  25. {
  26. if (String.IsNullOrEmpty(value))
  27. {
  28. value = String.Empty;
  29. }
  30. base[ConfigurationStrings.CustomCertificateValidatorType] = value;
  31. }
  32. }
  33. [ConfigurationProperty(ConfigurationStrings.CertificateValidationMode, DefaultValue = X509ServiceCertificateAuthentication.DefaultCertificateValidationMode)]
  34. [ServiceModelEnumValidator(typeof(X509CertificateValidationModeHelper))]
  35. public X509CertificateValidationMode CertificateValidationMode
  36. {
  37. get { return (X509CertificateValidationMode)base[ConfigurationStrings.CertificateValidationMode]; }
  38. set { base[ConfigurationStrings.CertificateValidationMode] = value; }
  39. }
  40. [ConfigurationProperty(ConfigurationStrings.RevocationMode, DefaultValue = X509ServiceCertificateAuthentication.DefaultRevocationMode)]
  41. [StandardRuntimeEnumValidator(typeof(X509RevocationMode))]
  42. public X509RevocationMode RevocationMode
  43. {
  44. get { return (X509RevocationMode)base[ConfigurationStrings.RevocationMode]; }
  45. set { base[ConfigurationStrings.RevocationMode] = value; }
  46. }
  47. [ConfigurationProperty(ConfigurationStrings.TrustedStoreLocation, DefaultValue = X509ServiceCertificateAuthentication.DefaultTrustedStoreLocation)]
  48. [StandardRuntimeEnumValidator(typeof(StoreLocation))]
  49. public StoreLocation TrustedStoreLocation
  50. {
  51. get { return (StoreLocation)base[ConfigurationStrings.TrustedStoreLocation]; }
  52. set { base[ConfigurationStrings.TrustedStoreLocation] = value; }
  53. }
  54. public void Copy(X509ServiceCertificateAuthenticationElement from)
  55. {
  56. if (this.IsReadOnly())
  57. {
  58. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  59. }
  60. if (null == from)
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
  63. }
  64. this.CertificateValidationMode = from.CertificateValidationMode;
  65. this.RevocationMode = from.RevocationMode;
  66. this.TrustedStoreLocation = from.TrustedStoreLocation;
  67. this.CustomCertificateValidatorType = from.CustomCertificateValidatorType;
  68. }
  69. internal void ApplyConfiguration(X509ServiceCertificateAuthentication cert)
  70. {
  71. if (cert == null)
  72. {
  73. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("cert");
  74. }
  75. cert.CertificateValidationMode = this.CertificateValidationMode;
  76. cert.RevocationMode = this.RevocationMode;
  77. cert.TrustedStoreLocation = this.TrustedStoreLocation;
  78. if (!string.IsNullOrEmpty(this.CustomCertificateValidatorType))
  79. {
  80. Type validatorType = System.Type.GetType(this.CustomCertificateValidatorType, true);
  81. if (!typeof(X509CertificateValidator).IsAssignableFrom(validatorType))
  82. {
  83. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  84. SR.GetString(SR.ConfigInvalidCertificateValidatorType, this.CustomCertificateValidatorType, typeof(X509CertificateValidator).ToString())));
  85. }
  86. cert.CustomCertificateValidator = (X509CertificateValidator)Activator.CreateInstance(validatorType);
  87. }
  88. }
  89. }
  90. }