X509Store.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // X509Store.cs - System.Security.Cryptography.X509Certificates.X509Store
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. #if NET_1_2
  10. using System;
  11. using Mono.Security.X509;
  12. using Mono.Security.X509.Stores;
  13. namespace System.Security.Cryptography.X509Certificates {
  14. // Note: Match the definition of framework version 1.2.3400.0 on http://longhorn.msdn.microsoft.com
  15. public sealed class X509Store {
  16. private string _name;
  17. private StoreLocation _location;
  18. private X509CertificateExCollection _certs;
  19. private OpenFlags _flags;
  20. private ICertificateStore _store;
  21. // constructors
  22. // BUG: MY when using this constructor - My when using StoreName.My
  23. public X509Store ()
  24. : this ("MY", StoreLocation.CurrentUser) {}
  25. public X509Store (string storeName)
  26. : this (storeName, StoreLocation.CurrentUser) {}
  27. public X509Store (StoreName storeName)
  28. : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
  29. public X509Store (StoreLocation storeLocation)
  30. : this ("MY", storeLocation) {}
  31. public X509Store (StoreName storeName, StoreLocation storeLocation)
  32. : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
  33. public X509Store (string storeName, StoreLocation storeLocation)
  34. {
  35. if (storeName == null)
  36. throw new ArgumentNullException ("storeName");
  37. _name = storeName;
  38. _location = storeLocation;
  39. _store = new Mono.Security.X509.Stores.FileCertificateStore ();
  40. }
  41. // properties
  42. public X509CertificateExCollection Certificates {
  43. get {
  44. if (_certs == null)
  45. _certs = new X509CertificateExCollection ();
  46. return _certs;
  47. }
  48. }
  49. public StoreLocation Location {
  50. get { return _location; }
  51. }
  52. public string Name {
  53. get { return _name; }
  54. }
  55. private bool ReadOnly {
  56. get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
  57. }
  58. // methods
  59. private static string StoreNameToString (StoreName sn)
  60. {
  61. switch (sn) {
  62. case StoreName.CertificateAuthority:
  63. return "CA";
  64. default:
  65. return sn.ToString ();
  66. }
  67. }
  68. public void Add (X509CertificateEx certificate)
  69. {
  70. if (certificate == null)
  71. throw new ArgumentNullException ("certificate");
  72. if ((!ReadOnly) && (_store != null)) {
  73. try {
  74. Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
  75. _store.Add (x);
  76. }
  77. catch {
  78. throw new CryptographicException ("couldn't add certificate");
  79. }
  80. }
  81. }
  82. public void AddRange (X509CertificateExCollection certificates)
  83. {
  84. if (certificates == null)
  85. throw new ArgumentNullException ("certificates");
  86. if (!ReadOnly) {
  87. foreach (X509CertificateEx certificate in certificates) {
  88. Add (certificate);
  89. }
  90. }
  91. }
  92. public void Close ()
  93. {
  94. if (_store != null)
  95. _store.Close ();
  96. }
  97. public void Open (OpenFlags flags)
  98. {
  99. _flags = flags;
  100. bool readOnly = ((flags & OpenFlags.ReadOnly) == OpenFlags.ReadOnly);
  101. bool create = !((flags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly);
  102. bool archive = ((flags & OpenFlags.IncludeArchived) == OpenFlags.IncludeArchived);
  103. _store.Open (_name, _location.ToString (), readOnly, create, archive);
  104. }
  105. public void Remove (X509CertificateEx certificate)
  106. {
  107. if (certificate == null)
  108. throw new ArgumentNullException ("certificate");
  109. if ((!ReadOnly) && (_store != null)) {
  110. try {
  111. Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
  112. _store.Remove (x);
  113. }
  114. catch {
  115. throw new CryptographicException ("couldn't remove certificate");
  116. }
  117. }
  118. }
  119. public void RemoveRange (X509CertificateExCollection certificates)
  120. {
  121. if (certificates == null)
  122. throw new ArgumentNullException ("certificates");
  123. if (!this.ReadOnly) {
  124. foreach (X509CertificateEx certificate in certificates) {
  125. Remove (certificate);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. #endif