X509CertificateValidator.cs 4.0 KB

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