2004-04-16 Carlos Guzman Alvarez <carlosga@telefonica.net>
[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 using System;
11 using System.IO;
12
13 using Mono.Security.Authenticode;
14 using Mono.Security.X509;
15 using Mono.Security.X509.Stores;
16
17 namespace Mono.Security.X509.Stores {
18
19         public class FileCertificateStore : ICertificateStore {
20
21                 private string _name;
22                 private string _location;
23                 private bool _readOnly;
24                 private bool _createIfRequired;
25                 private bool _includeArchives;
26                 private bool _saveOnClose;
27                 private SoftwarePublisherCertificate _spc;
28
29                 public FileCertificateStore ()
30                 {
31                         _readOnly = true;
32                         _includeArchives = false;
33                         _createIfRequired = true;
34                 }
35
36                 // properties
37
38                 public X509CertificateCollection Certificates {
39                         get { 
40                                 if (_spc != null)
41                                         return _spc.Certificates; 
42                                 return null;
43                         }
44                 }
45                 
46                 public IntPtr Handle {
47                         get { return (IntPtr) 0; }
48                 }
49
50                 // methods
51
52                 public void Open (string name, string location, bool readOnly, bool createIfNonExisting, bool includeArchives) 
53                 {
54                         _name = name;
55                         _location = _location;
56                         _readOnly = readOnly;
57                         _createIfRequired = createIfNonExisting;
58                         _includeArchives = includeArchives;
59                         _saveOnClose = false;
60
61                         if (File.Exists (_name)) {
62                                 _spc = SoftwarePublisherCertificate.CreateFromFile (_name);
63                         }
64                         else if (_createIfRequired) {
65                                 _spc = new SoftwarePublisherCertificate ();
66                                 _saveOnClose = true;
67                         }
68                 }
69
70                 public void Close () 
71                 {
72                         if (_saveOnClose) {
73                                 byte[] store = _spc.GetBytes ();
74                                 using (FileStream fs = File.OpenWrite (_name)) {
75                                         fs.Write (store, 0, store.Length);
76                                         fs.Close ();
77                                 }
78                         }
79                 }
80                 
81                 public void Add (X509Certificate certificate) 
82                 {
83                         if ((!_readOnly) && (_spc != null)) {
84                                 _spc.Certificates.Add (certificate);
85                                 _saveOnClose = true;
86                         }
87                 }
88                 
89                 public void Remove (X509Certificate certificate) 
90                 {
91                         if ((!_readOnly) && (_spc != null)) {
92                                 _spc.Certificates.Remove (certificate);
93                                 _saveOnClose = true;
94                         }
95                 }
96         }
97 }