* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509Certificate2.cs
1 //
2 // System.Security.Cryptography.X509Certificate2 class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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
30 #if NET_2_0 && SECURITY_DEP
31
32 using System.IO;
33 using System.Text;
34
35 using MX = Mono.Security.X509;
36
37 namespace System.Security.Cryptography.X509Certificates {
38
39         public class X509Certificate2 : X509Certificate {
40
41                 private bool _archived;
42                 private X509ExtensionCollection _extensions;
43                 private string _name;
44                 private string _serial;
45                 private PublicKey _publicKey;
46
47                 private MX.X509Certificate _cert;
48
49                 // constructors
50
51                 public X509Certificate2 () : base () 
52                 {
53                         _cert = null;
54                 }
55
56                 public X509Certificate2 (byte[] rawData) : base (rawData) 
57                 {
58                         _cert = new MX.X509Certificate (base.GetRawCertData ());
59                 }
60
61                 public X509Certificate2 (byte[] rawData, string password) : base (rawData, password) 
62                 {
63                         _cert = new MX.X509Certificate (base.GetRawCertData ());
64                 }
65
66                 public X509Certificate2 (byte[] rawData, SecureString password) : base (rawData, password) 
67                 {
68                         _cert = new MX.X509Certificate (base.GetRawCertData ());
69                 }
70
71                 public X509Certificate2 (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
72                         : base (rawData, password, keyStorageFlags) 
73                 {
74                         _cert = new MX.X509Certificate (base.GetRawCertData ());
75                 }
76
77                 public X509Certificate2 (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
78                         : base (rawData, password, keyStorageFlags) 
79                 {
80                         _cert = new MX.X509Certificate (base.GetRawCertData ());
81                 }
82
83                 public X509Certificate2 (string fileName) : base (fileName) 
84                 {
85                         _cert = new MX.X509Certificate (base.GetRawCertData ());
86                 }
87
88                 public X509Certificate2 (string fileName, string password) 
89                 {
90                         _cert = new MX.X509Certificate (base.GetRawCertData ());
91                 }
92
93                 public X509Certificate2 (string fileName, SecureString password) 
94                 {
95                         _cert = new MX.X509Certificate (base.GetRawCertData ());
96                 }
97
98                 public X509Certificate2 (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
99                         : base (fileName, password, keyStorageFlags) 
100                 {
101                         _cert = new MX.X509Certificate (base.GetRawCertData ());
102                 }
103
104                 public X509Certificate2 (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
105                         : base (fileName, password, keyStorageFlags) 
106                 {
107                         _cert = new MX.X509Certificate (base.GetRawCertData ());
108                 }
109
110                 public X509Certificate2 (IntPtr handle) : base (handle) 
111                 {
112                         _cert = new MX.X509Certificate (base.GetRawCertData ());
113                 }
114
115                 public X509Certificate2 (X509Certificate certificate) 
116                 {
117                         _cert = new MX.X509Certificate (base.GetRawCertData ());
118                 }
119
120                 // properties
121
122                 public bool Archived {
123                         get { return _archived; }
124                         set { _archived = value; }
125                 }
126
127                 public X509ExtensionCollection Extensions {
128                         get { return _extensions; }
129                 }
130
131                 public string FriendlyName {
132                         get { return _name; }
133                         set { _name = value; }
134                 }
135
136                 [MonoTODO]
137                 public bool HasPrivateKey {
138                         get { return false; }
139                 }
140
141                 [MonoTODO]
142                 public X500DistinguishedName IssuerName {
143                         get { return null; }
144                 } 
145
146                 public DateTime NotAfter {
147                         get { return _cert.ValidUntil; }
148                 }
149
150                 public DateTime NotBefore {
151                         get { return _cert.ValidFrom; }
152                 }
153
154                 public AsymmetricAlgorithm PrivateKey {
155                         get {
156                                 if (_cert.RSA != null)
157                                         return _cert.RSA; 
158                                 else if (_cert.DSA != null)
159                                         return _cert.DSA;
160                                 return null;
161                         }
162                         set {
163                                 if (value is RSA)
164                                         _cert.RSA = (RSA) value;
165                                 else if (value is DSA)
166                                         _cert.DSA = (DSA) value;
167                                 else
168                                         throw new NotSupportedException ();
169                         }
170                 } 
171
172                 public PublicKey PublicKey {
173                         get { 
174                                 if (_publicKey == null) {
175                                         _publicKey = new PublicKey (_cert);
176                                 }
177                                 return _publicKey;
178                         }
179                 } 
180
181                 public byte[] RawData {
182                         get {
183                                 if (_cert == null) {
184                                         throw new CryptographicException (Locale.GetText ("No certificate data."));
185                                 }
186                                 return base.GetRawCertData ();
187                         }
188                 } 
189
190                 public string SerialNumber {
191                         get { 
192                                 if (_serial == null) {
193                                         StringBuilder sb = new StringBuilder ();
194                                         byte[] serial = _cert.SerialNumber;
195                                         for (int i=serial.Length - 1; i >= 0; i--)
196                                                 sb.Append (serial [i].ToString ("X2"));
197                                         _serial = sb.ToString ();
198                                 }
199                                 return _serial; 
200                         }
201                 } 
202
203                 public Oid SignatureAlgorithm {
204                         get { return null; }
205                 } 
206
207                 [MonoTODO]
208                 public X500DistinguishedName SubjectName {
209                         get { return null; }
210                 } 
211
212                 public string Thumbprint {
213                         get { return base.GetCertHashString (); }
214                 } 
215
216                 public int Version {
217                         get { return _cert.Version; }
218                 }
219
220                 // methods
221
222                 [MonoTODO]
223                 public string GetNameInfo (X509NameType nameType, bool forIssuer) 
224                 {
225                         return null;
226                 }
227
228                 public override void Import (byte[] rawData) 
229                 {
230                         Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
231                 }
232
233                 [MonoTODO ("missing KeyStorageFlags support")]
234                 public override void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
235                 {
236                         base.Import (rawData, password, keyStorageFlags);
237                         if (password == null) {
238                                 _cert = new Mono.Security.X509.X509Certificate (rawData);
239                                 // TODO - PKCS12 without password
240                         } else {
241                                 // try PKCS#12
242                                 MX.PKCS12 pfx = new MX.PKCS12 (rawData, password);
243                                 if (pfx.Certificates.Count > 0) {
244                                         _cert = pfx.Certificates [0];
245                                 } else {
246                                         _cert = null;
247                                 }
248                                 if (pfx.Keys.Count > 0) {
249                                         _cert.RSA = (pfx.Keys [0] as RSA);
250                                         _cert.DSA = (pfx.Keys [0] as DSA);
251                                 }
252                         }
253                 }
254
255                 [MonoTODO ("SecureString is incomplete")]
256                 public override void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
257                 {
258                         Import (rawData, (string) null, keyStorageFlags);
259                 }
260
261                 public override void Import (string fileName) 
262                 {
263                         byte[] rawData = Load (fileName);
264                         Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
265                 }
266
267                 [MonoTODO ("missing KeyStorageFlags support")]
268                 public override void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags) 
269                 {
270                         byte[] rawData = Load (fileName);
271                         Import (rawData, password, keyStorageFlags);
272                 }
273
274                 [MonoTODO ("SecureString is incomplete")]
275                 public override void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags) 
276                 {
277                         byte[] rawData = Load (fileName);
278                         Import (rawData, (string)null, keyStorageFlags);
279                 }
280
281                 private byte[] Load (string fileName)
282                 {
283                         byte[] data = null;
284                         using (FileStream fs = new FileStream (fileName, FileMode.Open)) {
285                                 data = new byte [fs.Length];
286                                 fs.Read (data, 0, data.Length);
287                                 fs.Close ();
288                         }
289                         return data;
290                 }
291
292                 public override void Reset () 
293                 {
294                         _serial = null;
295                         _publicKey = null;
296                         base.Reset ();
297                 }
298
299                 [MonoTODO]
300                 public override string ToString ()
301                 {
302                         return null;
303                 }
304
305                 [MonoTODO]
306                 public override string ToString (bool verbose)
307                 {
308                         return null;
309                 }
310
311                 [MonoTODO]
312                 public bool Verify ()
313                 {
314                         X509Chain chain = new X509Chain ();
315                         if (!chain.Build (this))
316                                 return false;
317                         // TODO - check chain and other stuff ???
318                         return true;
319                 }
320
321                 // static methods
322
323                 [MonoTODO]
324                 public static X509ContentType GetCertContentType (byte[] rawData)
325                 {
326                         return X509ContentType.Unknown;
327                 }
328
329                 [MonoTODO]
330                 public static X509ContentType GetCertContentType (string fileName)
331                 {
332                         return X509ContentType.Unknown;
333                 }
334         }
335 }
336
337 #endif