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