PeerCredential.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Security;
  9. public class PeerCredential
  10. {
  11. internal const StoreLocation DefaultStoreLocation = StoreLocation.CurrentUser;
  12. internal const StoreName DefaultStoreName = StoreName.My;
  13. internal const X509FindType DefaultFindType = X509FindType.FindBySubjectDistinguishedName;
  14. X509Certificate2 certificate;
  15. string meshPassword;
  16. X509PeerCertificateAuthentication peerAuthentication;
  17. X509PeerCertificateAuthentication messageSenderAuthentication;
  18. bool isReadOnly;
  19. internal PeerCredential()
  20. {
  21. peerAuthentication = new X509PeerCertificateAuthentication();
  22. messageSenderAuthentication = new X509PeerCertificateAuthentication();
  23. }
  24. internal PeerCredential(PeerCredential other)
  25. {
  26. this.certificate = other.certificate;
  27. this.meshPassword = other.meshPassword;
  28. this.peerAuthentication = new X509PeerCertificateAuthentication(other.peerAuthentication);
  29. this.messageSenderAuthentication = new X509PeerCertificateAuthentication(other.messageSenderAuthentication);
  30. this.isReadOnly = other.isReadOnly;
  31. }
  32. public X509Certificate2 Certificate
  33. {
  34. get
  35. {
  36. return this.certificate;
  37. }
  38. set
  39. {
  40. ThrowIfImmutable();
  41. this.certificate = value;
  42. }
  43. }
  44. public string MeshPassword
  45. {
  46. get
  47. {
  48. return this.meshPassword;
  49. }
  50. set
  51. {
  52. ThrowIfImmutable();
  53. this.meshPassword = value;
  54. }
  55. }
  56. public X509PeerCertificateAuthentication PeerAuthentication
  57. {
  58. get
  59. {
  60. return this.peerAuthentication;
  61. }
  62. set
  63. {
  64. ThrowIfImmutable();
  65. this.peerAuthentication = value;
  66. }
  67. }
  68. public X509PeerCertificateAuthentication MessageSenderAuthentication
  69. {
  70. get
  71. {
  72. return this.messageSenderAuthentication;
  73. }
  74. set
  75. {
  76. ThrowIfImmutable();
  77. this.messageSenderAuthentication = value;
  78. }
  79. }
  80. public void SetCertificate(string subjectName, StoreLocation storeLocation, StoreName storeName)
  81. {
  82. if (subjectName == null)
  83. {
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("subjectName");
  85. }
  86. this.SetCertificate(storeLocation, storeName, DefaultFindType, subjectName);
  87. }
  88. public void SetCertificate(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
  89. {
  90. if (findValue == null)
  91. {
  92. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue");
  93. }
  94. ThrowIfImmutable();
  95. this.certificate = SecurityUtils.GetCertificateFromStore(storeName, storeLocation, findType, findValue, null);
  96. }
  97. internal void MakeReadOnly()
  98. {
  99. this.isReadOnly = true;
  100. this.peerAuthentication.MakeReadOnly();
  101. this.messageSenderAuthentication.MakeReadOnly();
  102. }
  103. void ThrowIfImmutable()
  104. {
  105. if (this.isReadOnly)
  106. {
  107. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  108. }
  109. }
  110. bool SameAuthenticators(X509PeerCertificateAuthentication one, X509PeerCertificateAuthentication two)
  111. {
  112. if (one.CertificateValidationMode != two.CertificateValidationMode)
  113. return false;
  114. if (one.CertificateValidationMode != X509CertificateValidationMode.Custom)
  115. {
  116. return (one.GetType().Equals(two.GetType()));
  117. }
  118. else
  119. {
  120. System.IdentityModel.Selectors.X509CertificateValidator first = null, second = null;
  121. one.TryGetCertificateValidator(out first);
  122. two.TryGetCertificateValidator(out second);
  123. return (first != null && second != null && first.Equals(second));
  124. }
  125. }
  126. internal bool Equals(PeerCredential that, PeerAuthenticationMode mode, bool messageAuthentication)
  127. {
  128. if (messageAuthentication)
  129. {
  130. if (!SameAuthenticators(this.MessageSenderAuthentication, that.messageSenderAuthentication))
  131. return false;
  132. if (this.Certificate != null && that.Certificate != null && !this.Certificate.Equals(that.Certificate))
  133. return false;
  134. }
  135. switch (mode)
  136. {
  137. case PeerAuthenticationMode.None:
  138. return true;
  139. case PeerAuthenticationMode.Password:
  140. if (!this.MeshPassword.Equals(that.MeshPassword))
  141. return false;
  142. if (this.Certificate == null && that.Certificate == null)
  143. return true;
  144. if ((this.Certificate == null) || !this.Certificate.Equals(that.Certificate))
  145. return false;
  146. break;
  147. case PeerAuthenticationMode.MutualCertificate:
  148. if (!this.Certificate.Equals(that.Certificate))
  149. return false;
  150. if (!SameAuthenticators(this.PeerAuthentication, that.PeerAuthentication))
  151. return false;
  152. break;
  153. }
  154. return true;
  155. }
  156. }
  157. }