2
0

X509CertificateValidator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // X509CertificateValidator.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;
  29. using System.Collections.ObjectModel;
  30. using System.Security.Cryptography.X509Certificates;
  31. namespace System.IdentityModel.Selectors
  32. {
  33. public abstract class X509CertificateValidator
  34. {
  35. static X509CertificateValidator none, chain, peer_or_chain, peer;
  36. static X509CertificateValidator ()
  37. {
  38. none = new X509NoValidator ();
  39. chain = new X509CertificateValidatorImpl (
  40. false, true, false, new X509ChainPolicy ());
  41. peer = new X509CertificateValidatorImpl (
  42. true, false, false, null);
  43. peer_or_chain = new X509CertificateValidatorImpl (
  44. true, true, false, new X509ChainPolicy ());
  45. }
  46. protected X509CertificateValidator ()
  47. {
  48. }
  49. public static X509CertificateValidator None {
  50. get { return none; }
  51. }
  52. public static X509CertificateValidator ChainTrust {
  53. get { return chain; }
  54. }
  55. public static X509CertificateValidator PeerOrChainTrust {
  56. get { return peer_or_chain; }
  57. }
  58. public static X509CertificateValidator PeerTrust {
  59. get { return peer; }
  60. }
  61. public static X509CertificateValidator CreateChainTrustValidator (
  62. bool useMachineContext, X509ChainPolicy chainPolicy)
  63. {
  64. return new X509CertificateValidatorImpl (
  65. false, true, useMachineContext, chainPolicy);
  66. }
  67. public static X509CertificateValidator CreatePeerOrChainTrustValidator (
  68. bool useMachineContext, X509ChainPolicy chainPolicy)
  69. {
  70. return new X509CertificateValidatorImpl (
  71. true, true, useMachineContext, chainPolicy);
  72. }
  73. public abstract void Validate (X509Certificate2 certificate);
  74. class X509NoValidator : X509CertificateValidator
  75. {
  76. public override void Validate (X509Certificate2 cert)
  77. {
  78. }
  79. }
  80. class X509CertificateValidatorImpl : X509CertificateValidator
  81. {
  82. bool check_peer;
  83. bool check_chain;
  84. bool use_machine_ctx;
  85. X509ChainPolicy policy;
  86. X509Chain chain;
  87. public X509CertificateValidatorImpl (bool peer, bool chain, bool useMachineContext, X509ChainPolicy chainPolicy)
  88. {
  89. this.check_peer = peer;
  90. this.check_chain = chain;
  91. use_machine_ctx = useMachineContext;
  92. policy = chainPolicy;
  93. }
  94. public override void Validate (X509Certificate2 cert)
  95. {
  96. if (check_peer) {
  97. X509Store store = new X509Store ();
  98. store.Open (OpenFlags.ReadOnly);
  99. foreach (X509Certificate2 c in store.Certificates)
  100. if (c.Thumbprint == cert.Thumbprint)
  101. return;
  102. }
  103. if (check_chain) {
  104. if (chain == null) {
  105. if (use_machine_ctx)
  106. chain = X509Chain.Create ();
  107. else
  108. chain = new X509Chain ();
  109. chain.ChainPolicy = policy;
  110. }
  111. else
  112. chain.Reset ();
  113. if (chain.Build (cert))
  114. return;
  115. }
  116. throw new ArgumentException ("The argument certificate is invalid.");
  117. }
  118. }
  119. }
  120. }