New test.
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Stores / FileCertificateStore.cs
1 //
2 // FileCertificateStore.cs: Handles a file-based certificate store.
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.IO;
33
34 using Mono.Security.Authenticode;
35 using Mono.Security.X509;
36 using Mono.Security.X509.Stores;
37
38 namespace Mono.Security.X509.Stores {
39
40         public class FileCertificateStore : ICertificateStore {
41
42                 private string _name;
43                 private string _location;
44                 private bool _readOnly;
45                 private bool _createIfRequired;
46                 private bool _includeArchives;
47                 private bool _saveOnClose;
48                 private SoftwarePublisherCertificate _spc;
49
50                 public FileCertificateStore ()
51                 {
52                         _readOnly = true;
53                         _includeArchives = false;
54                         _createIfRequired = true;
55                 }
56
57                 // properties
58
59                 public X509CertificateCollection Certificates {
60                         get { 
61                                 if (_spc != null)
62                                         return _spc.Certificates; 
63                                 return null;
64                         }
65                 }
66                 
67                 public IntPtr Handle {
68                         get { return (IntPtr) 0; }
69                 }
70
71                 // methods
72
73                 public void Open (string name, string location, bool readOnly, bool createIfNonExisting, bool includeArchives) 
74                 {
75                         _name = name;
76                         _location = _location;
77                         _readOnly = readOnly;
78                         _createIfRequired = createIfNonExisting;
79                         _includeArchives = includeArchives;
80                         _saveOnClose = false;
81
82                         if (File.Exists (_name)) {
83                                 _spc = SoftwarePublisherCertificate.CreateFromFile (_name);
84                         }
85                         else if (_createIfRequired) {
86                                 _spc = new SoftwarePublisherCertificate ();
87                                 _saveOnClose = true;
88                         }
89                 }
90
91                 public void Close () 
92                 {
93                         if (_saveOnClose) {
94                                 byte[] store = _spc.GetBytes ();
95                                 using (FileStream fs = File.OpenWrite (_name)) {
96                                         fs.Write (store, 0, store.Length);
97                                         fs.Close ();
98                                 }
99                         }
100                 }
101                 
102                 public void Add (X509Certificate certificate) 
103                 {
104                         if ((!_readOnly) && (_spc != null)) {
105                                 _spc.Certificates.Add (certificate);
106                                 _saveOnClose = true;
107                         }
108                 }
109                 
110                 public void Remove (X509Certificate certificate) 
111                 {
112                         if ((!_readOnly) && (_spc != null)) {
113                                 _spc.Certificates.Remove (certificate);
114                                 _saveOnClose = true;
115                         }
116                 }
117         }
118 }