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)
9 // Copyright 2013 Xamarin Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
37 using Mono.Security.X509;
39 using System.Runtime.Serialization;
41 namespace System.Security.Cryptography.X509Certificates {
44 [MonoTODO ("X509ContentType.SerializedCert isn't supported (anywhere in the class)")]
45 public partial class X509Certificate : IDeserializationCallback, ISerializable, IDisposable {
46 private string issuer_name;
47 private string subject_name;
50 public X509Certificate ()
52 // this allows an empty certificate to exists
55 public X509Certificate (byte[] rawData, string password)
57 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
60 [MonoTODO ("SecureString support is incomplete")]
61 public X509Certificate (byte[] rawData, SecureString password)
63 Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
66 public X509Certificate (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
68 Import (rawData, password, keyStorageFlags);
71 [MonoTODO ("SecureString support is incomplete")]
72 public X509Certificate (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
74 Import (rawData, password, keyStorageFlags);
77 public X509Certificate (string fileName)
79 Import (fileName, (string)null, X509KeyStorageFlags.DefaultKeySet);
82 public X509Certificate (string fileName, string password)
84 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
87 [MonoTODO ("SecureString support is incomplete")]
88 public X509Certificate (string fileName, SecureString password)
90 Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
93 public X509Certificate (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
95 Import (fileName, password, keyStorageFlags);
98 [MonoTODO ("SecureString support is incomplete")]
99 public X509Certificate (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
101 Import (fileName, password, keyStorageFlags);
104 public X509Certificate (SerializationInfo info, StreamingContext context)
106 byte[] raw = (byte[]) info.GetValue ("RawData", typeof (byte[]));
107 Import (raw, (string)null, X509KeyStorageFlags.DefaultKeySet);
111 public string Issuer {
113 X509Helper.ThrowIfContextInvalid (impl);
115 if (issuer_name == null)
116 issuer_name = impl.GetIssuerName (false);
121 public string Subject {
123 X509Helper.ThrowIfContextInvalid (impl);
125 if (subject_name == null)
126 subject_name = impl.GetSubjectName (false);
132 public IntPtr Handle {
134 if (X509Helper.IsValid (impl))
142 public override bool Equals (object obj)
144 X509Certificate x = (obj as X509Certificate);
146 return this.Equals (x);
150 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
152 public virtual byte[] Export (X509ContentType contentType)
154 return Export (contentType, (byte[])null);
157 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
159 public virtual byte[] Export (X509ContentType contentType, string password)
161 byte[] pwd = (password == null) ? null : Encoding.UTF8.GetBytes (password);
162 return Export (contentType, pwd);
165 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported. SecureString support is incomplete.")]
166 public virtual byte[] Export (X509ContentType contentType, SecureString password)
168 byte[] pwd = (password == null) ? null : password.GetBuffer ();
169 return Export (contentType, pwd);
172 internal byte[] Export (X509ContentType contentType, byte[] password)
175 X509Helper.ThrowIfContextInvalid (impl);
176 return impl.Export (contentType, password);
179 if (password != null)
180 Array.Clear (password, 0, password.Length);
185 public virtual void Import (byte[] rawData)
187 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
190 [MonoTODO ("missing KeyStorageFlags support")]
192 public virtual void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
195 impl = X509Helper.Import (rawData, password, keyStorageFlags);
198 [MonoTODO ("SecureString support is incomplete")]
199 public virtual void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
201 Import (rawData, (string)null, keyStorageFlags);
205 public virtual void Import (string fileName)
207 byte[] rawData = File.ReadAllBytes (fileName);
208 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
211 [MonoTODO ("missing KeyStorageFlags support")]
213 public virtual void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
215 byte[] rawData = File.ReadAllBytes (fileName);
216 Import (rawData, password, keyStorageFlags);
219 [MonoTODO ("SecureString support is incomplete, missing KeyStorageFlags support")]
220 public virtual void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
222 byte[] rawData = File.ReadAllBytes (fileName);
223 Import (rawData, (string)null, keyStorageFlags);
226 void IDeserializationCallback.OnDeserialization (object sender)
230 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
232 if (!X509Helper.IsValid (impl))
233 throw new NullReferenceException ();
234 // will throw a NRE if info is null (just like MS implementation)
235 info.AddValue ("RawData", impl.GetRawCertData ());
238 public void Dispose ()
243 protected virtual void Dispose (bool disposing)
250 public virtual void Reset ()