X509Store.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using Mono.Security.X509;
  32. namespace System.Security.Cryptography.X509Certificates {
  33. public sealed class X509Store {
  34. private string _name;
  35. private StoreLocation _location;
  36. private X509CertificateExCollection _certs;
  37. private OpenFlags _flags;
  38. // constructors
  39. // BUG: MY when using this constructor - My when using StoreName.My
  40. public X509Store ()
  41. : this ("MY", StoreLocation.CurrentUser)
  42. {
  43. }
  44. public X509Store (string storeName)
  45. : this (storeName, StoreLocation.CurrentUser)
  46. {
  47. }
  48. public X509Store (StoreName storeName)
  49. : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
  50. {
  51. }
  52. public X509Store (StoreLocation storeLocation)
  53. : this ("MY", storeLocation)
  54. {
  55. }
  56. public X509Store (StoreName storeName, StoreLocation storeLocation)
  57. : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
  58. {
  59. }
  60. [MonoTODO ("call Mono.Security.X509.X509Store*")]
  61. public X509Store (string storeName, StoreLocation storeLocation)
  62. {
  63. if (storeName == null)
  64. throw new ArgumentNullException ("storeName");
  65. _name = storeName;
  66. _location = storeLocation;
  67. }
  68. // properties
  69. public X509CertificateExCollection Certificates {
  70. get {
  71. if (_certs == null)
  72. _certs = new X509CertificateExCollection ();
  73. return _certs;
  74. }
  75. }
  76. public StoreLocation Location {
  77. get { return _location; }
  78. }
  79. public string Name {
  80. get { return _name; }
  81. }
  82. private bool ReadOnly {
  83. get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
  84. }
  85. // methods
  86. private static string StoreNameToString (StoreName sn)
  87. {
  88. switch (sn) {
  89. case StoreName.CertificateAuthority:
  90. return "CA";
  91. default:
  92. return sn.ToString ();
  93. }
  94. }
  95. [MonoTODO ("call Mono.Security.X509.X509Store*")]
  96. public void Add (X509CertificateEx certificate)
  97. {
  98. if (certificate == null)
  99. throw new ArgumentNullException ("certificate");
  100. if (!ReadOnly) {
  101. try {
  102. Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
  103. // TODO
  104. }
  105. catch {
  106. throw new CryptographicException ("couldn't add certificate");
  107. }
  108. }
  109. }
  110. public void AddRange (X509CertificateExCollection certificates)
  111. {
  112. if (certificates == null)
  113. throw new ArgumentNullException ("certificates");
  114. if (!ReadOnly) {
  115. foreach (X509CertificateEx certificate in certificates) {
  116. Add (certificate);
  117. }
  118. }
  119. }
  120. [MonoTODO ("call Mono.Security.X509.X509Store*")]
  121. public void Close ()
  122. {
  123. }
  124. [MonoTODO ("call Mono.Security.X509.X509Store*")]
  125. public void Open (OpenFlags flags)
  126. {
  127. _flags = flags;
  128. bool readOnly = ((flags & OpenFlags.ReadOnly) == OpenFlags.ReadOnly);
  129. bool create = !((flags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly);
  130. bool archive = ((flags & OpenFlags.IncludeArchived) == OpenFlags.IncludeArchived);
  131. // TODO
  132. }
  133. [MonoTODO ("call Mono.Security.X509.X509Store*")]
  134. public void Remove (X509CertificateEx certificate)
  135. {
  136. if (certificate == null)
  137. throw new ArgumentNullException ("certificate");
  138. if (!ReadOnly) {
  139. try {
  140. Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
  141. // TODO
  142. }
  143. catch {
  144. throw new CryptographicException ("couldn't remove certificate");
  145. }
  146. }
  147. }
  148. public void RemoveRange (X509CertificateExCollection certificates)
  149. {
  150. if (certificates == null)
  151. throw new ArgumentNullException ("certificates");
  152. if (!this.ReadOnly) {
  153. foreach (X509CertificateEx certificate in certificates) {
  154. Remove (certificate);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. #endif