2 // X509Certificate20.cs: Partial class to handle new 2.0-only stuff
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006,2008 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Runtime.InteropServices;
32 using System.Security.Permissions;
36 using Mono.Security.X509;
38 using System.Runtime.Serialization;
40 namespace System.Security.Cryptography.X509Certificates {
43 [MonoTODO ("X509ContentType.SerializedCert isn't supported (anywhere in the class)")]
45 public partial class X509Certificate {
47 public partial class X509Certificate : IDeserializationCallback, ISerializable {
49 private string issuer_name;
50 private string subject_name;
53 public X509Certificate ()
55 // this allows an empty certificate to exists
58 public X509Certificate (byte[] rawData, string password)
60 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
64 [MonoTODO ("SecureString support is incomplete")]
65 public X509Certificate (byte[] rawData, SecureString password)
67 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
71 public X509Certificate (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
73 Import (rawData, password, keyStorageFlags);
77 [MonoTODO ("SecureString support is incomplete")]
78 public X509Certificate (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
80 Import (rawData, password, keyStorageFlags);
84 public X509Certificate (string fileName)
86 Import (fileName, (string)null, X509KeyStorageFlags.DefaultKeySet);
89 public X509Certificate (string fileName, string password)
91 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
95 [MonoTODO ("SecureString support is incomplete")]
96 public X509Certificate (string fileName, SecureString password)
98 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
102 public X509Certificate (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
104 Import (fileName, password, keyStorageFlags);
108 [MonoTODO ("SecureString support is incomplete")]
109 public X509Certificate (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
111 Import (fileName, password, keyStorageFlags);
115 public X509Certificate (SerializationInfo info, StreamingContext context)
117 byte[] raw = (byte[]) info.GetValue ("RawData", typeof (byte[]));
118 Import (raw, (string)null, X509KeyStorageFlags.DefaultKeySet);
122 public string Issuer {
125 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
127 if (issuer_name == null)
128 issuer_name = X501.ToString (x509.GetIssuerName (), true, ", ", true);
133 public string Subject {
136 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
138 if (subject_name == null)
139 subject_name = X501.ToString (x509.GetSubjectName (), true, ", ", true);
145 public IntPtr Handle {
146 get { return IntPtr.Zero; }
151 public override bool Equals (object obj)
153 X509Certificate x = (obj as X509Certificate);
155 return this.Equals (x);
159 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
161 public virtual byte[] Export (X509ContentType contentType)
163 return Export (contentType, (byte[])null);
166 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
168 public virtual byte[] Export (X509ContentType contentType, string password)
170 byte[] pwd = (password == null) ? null : Encoding.UTF8.GetBytes (password);
171 return Export (contentType, pwd);
175 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported. SecureString support is incomplete.")]
176 public virtual byte[] Export (X509ContentType contentType, SecureString password)
178 byte[] pwd = (password == null) ? null : password.GetBuffer ();
179 return Export (contentType, pwd);
183 internal byte[] Export (X509ContentType contentType, byte[] password)
186 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
189 switch (contentType) {
190 case X509ContentType.Cert:
193 case X509ContentType.Pfx: // this includes Pkcs12
195 throw new NotSupportedException ();
196 case X509ContentType.SerializedCert:
198 throw new NotSupportedException ();
201 string msg = Locale.GetText ("This certificate format '{0}' cannot be exported.", contentType);
202 throw new CryptographicException (msg);
207 if (password != null)
208 Array.Clear (password, 0, password.Length);
213 public virtual void Import (byte[] rawData)
215 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
218 [MonoTODO ("missing KeyStorageFlags support")]
220 public virtual void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
223 if (password == null) {
225 x509 = new Mono.Security.X509.X509Certificate (rawData);
227 catch (Exception e) {
229 PKCS12 pfx = new PKCS12 (rawData);
230 if (pfx.Certificates.Count > 0)
231 x509 = pfx.Certificates [0];
236 string msg = Locale.GetText ("Unable to decode certificate.");
237 // inner exception is the original (not second) exception
238 throw new CryptographicException (msg, e);
244 PKCS12 pfx = new PKCS12 (rawData, password);
245 if (pfx.Certificates.Count > 0) {
246 x509 = pfx.Certificates [0];
252 // it's possible to supply a (unrequired/unusued) password
254 x509 = new Mono.Security.X509.X509Certificate (rawData);
260 [MonoTODO ("SecureString support is incomplete")]
261 public virtual void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
263 Import (rawData, (string)null, keyStorageFlags);
268 public virtual void Import (string fileName)
270 byte[] rawData = Load (fileName);
271 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
274 [MonoTODO ("missing KeyStorageFlags support")]
276 public virtual void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
278 byte[] rawData = Load (fileName);
279 Import (rawData, password, keyStorageFlags);
283 [MonoTODO ("SecureString support is incomplete, missing KeyStorageFlags support")]
284 public virtual void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
286 byte[] rawData = Load (fileName);
287 Import (rawData, (string)null, keyStorageFlags);
292 void IDeserializationCallback.OnDeserialization (object sender)
296 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
298 // will throw a NRE if info is null (just like MS implementation)
299 info.AddValue ("RawData", x509.RawData);
303 public virtual void Reset ()
309 cachedCertificateHash = null;