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)")]
44 public partial class X509Certificate : IDeserializationCallback, ISerializable {
45 private string issuer_name;
46 private string subject_name;
49 public X509Certificate ()
51 // this allows an empty certificate to exists
54 public X509Certificate (byte[] rawData, string password)
56 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
59 [MonoTODO ("SecureString support is incomplete")]
60 public X509Certificate (byte[] rawData, SecureString password)
62 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
65 public X509Certificate (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
67 Import (rawData, password, keyStorageFlags);
70 [MonoTODO ("SecureString support is incomplete")]
71 public X509Certificate (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
73 Import (rawData, password, keyStorageFlags);
76 public X509Certificate (string fileName)
78 Import (fileName, (string)null, X509KeyStorageFlags.DefaultKeySet);
81 public X509Certificate (string fileName, string password)
83 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
86 [MonoTODO ("SecureString support is incomplete")]
87 public X509Certificate (string fileName, SecureString password)
89 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
92 public X509Certificate (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
94 Import (fileName, password, keyStorageFlags);
97 [MonoTODO ("SecureString support is incomplete")]
98 public X509Certificate (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
100 Import (fileName, password, keyStorageFlags);
103 public X509Certificate (SerializationInfo info, StreamingContext context)
105 byte[] raw = (byte[]) info.GetValue ("RawData", typeof (byte[]));
106 Import (raw, (string)null, X509KeyStorageFlags.DefaultKeySet);
110 public string Issuer {
113 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
115 if (issuer_name == null)
116 issuer_name = X501.ToString (x509.GetIssuerName (), true, ", ", true);
121 public string Subject {
124 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
126 if (subject_name == null)
127 subject_name = X501.ToString (x509.GetSubjectName (), true, ", ", true);
133 public IntPtr Handle {
134 get { return IntPtr.Zero; }
139 public override bool Equals (object obj)
141 X509Certificate x = (obj as X509Certificate);
143 return this.Equals (x);
147 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
149 public virtual byte[] Export (X509ContentType contentType)
151 return Export (contentType, (byte[])null);
154 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
156 public virtual byte[] Export (X509ContentType contentType, string password)
158 byte[] pwd = (password == null) ? null : Encoding.UTF8.GetBytes (password);
159 return Export (contentType, pwd);
162 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported. SecureString support is incomplete.")]
163 public virtual byte[] Export (X509ContentType contentType, SecureString password)
165 byte[] pwd = (password == null) ? null : password.GetBuffer ();
166 return Export (contentType, pwd);
169 internal byte[] Export (X509ContentType contentType, byte[] password)
172 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
175 switch (contentType) {
176 case X509ContentType.Cert:
178 case X509ContentType.Pfx: // this includes Pkcs12
180 throw new NotSupportedException ();
181 case X509ContentType.SerializedCert:
183 throw new NotSupportedException ();
185 string msg = Locale.GetText ("This certificate format '{0}' cannot be exported.", contentType);
186 throw new CryptographicException (msg);
191 if (password != null)
192 Array.Clear (password, 0, password.Length);
197 public virtual void Import (byte[] rawData)
199 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
202 [MonoTODO ("missing KeyStorageFlags support")]
204 public virtual void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
207 if (password == null) {
209 x509 = new Mono.Security.X509.X509Certificate (rawData);
211 catch (Exception e) {
213 PKCS12 pfx = new PKCS12 (rawData);
214 if (pfx.Certificates.Count > 0)
215 x509 = pfx.Certificates [0];
220 string msg = Locale.GetText ("Unable to decode certificate.");
221 // inner exception is the original (not second) exception
222 throw new CryptographicException (msg, e);
228 PKCS12 pfx = new PKCS12 (rawData, password);
229 if (pfx.Certificates.Count > 0) {
230 x509 = pfx.Certificates [0];
236 // it's possible to supply a (unrequired/unusued) password
238 x509 = new Mono.Security.X509.X509Certificate (rawData);
243 [MonoTODO ("SecureString support is incomplete")]
244 public virtual void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
246 Import (rawData, (string)null, keyStorageFlags);
250 public virtual void Import (string fileName)
252 byte[] rawData = Load (fileName);
253 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
256 [MonoTODO ("missing KeyStorageFlags support")]
258 public virtual void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
260 byte[] rawData = Load (fileName);
261 Import (rawData, password, keyStorageFlags);
264 [MonoTODO ("SecureString support is incomplete, missing KeyStorageFlags support")]
265 public virtual void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
267 byte[] rawData = Load (fileName);
268 Import (rawData, (string)null, keyStorageFlags);
271 void IDeserializationCallback.OnDeserialization (object sender)
275 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
277 // will throw a NRE if info is null (just like MS implementation)
278 info.AddValue ("RawData", x509.RawData);
282 public virtual void Reset ()
288 cachedCertificateHash = null;