2 // System.Security.Cryptography.X509Certificate2 class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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.
36 using MX = Mono.Security.X509;
38 namespace System.Security.Cryptography.X509Certificates {
40 public class X509Certificate2 : X509Certificate {
42 private bool _archived;
43 private X509ExtensionCollection _extensions;
45 private string _serial;
46 private PublicKey _publicKey;
48 private MX.X509Certificate _cert;
52 public X509Certificate2 () : base ()
57 public X509Certificate2 (byte[] rawData) : base (rawData)
59 _cert = new MX.X509Certificate (base.GetRawCertData ());
62 public X509Certificate2 (byte[] rawData, string password) : base (rawData, password)
64 _cert = new MX.X509Certificate (base.GetRawCertData ());
67 public X509Certificate2 (byte[] rawData, SecureString password) : base (rawData, password)
69 _cert = new MX.X509Certificate (base.GetRawCertData ());
72 public X509Certificate2 (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
73 : base (rawData, password, keyStorageFlags)
75 _cert = new MX.X509Certificate (base.GetRawCertData ());
78 public X509Certificate2 (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
79 : base (rawData, password, keyStorageFlags)
81 _cert = new MX.X509Certificate (base.GetRawCertData ());
84 public X509Certificate2 (string fileName) : base (fileName)
86 _cert = new MX.X509Certificate (base.GetRawCertData ());
89 public X509Certificate2 (string fileName, string password)
91 _cert = new MX.X509Certificate (base.GetRawCertData ());
94 public X509Certificate2 (string fileName, SecureString password)
96 _cert = new MX.X509Certificate (base.GetRawCertData ());
99 public X509Certificate2 (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
100 : base (fileName, password, keyStorageFlags)
102 _cert = new MX.X509Certificate (base.GetRawCertData ());
105 public X509Certificate2 (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
106 : base (fileName, password, keyStorageFlags)
108 _cert = new MX.X509Certificate (base.GetRawCertData ());
111 public X509Certificate2 (IntPtr handle) : base (handle)
113 _cert = new MX.X509Certificate (base.GetRawCertData ());
116 public X509Certificate2 (X509Certificate certificate)
118 _cert = new MX.X509Certificate (base.GetRawCertData ());
123 public bool Archived {
124 get { return _archived; }
125 set { _archived = value; }
128 public X509ExtensionCollection Extensions {
129 get { return _extensions; }
132 public string FriendlyName {
133 get { return _name; }
134 set { _name = value; }
138 public bool HasPrivateKey {
139 get { return false; }
143 public X500DistinguishedName IssuerName {
147 public DateTime NotAfter {
148 get { return _cert.ValidUntil; }
151 public DateTime NotBefore {
152 get { return _cert.ValidFrom; }
155 public AsymmetricAlgorithm PrivateKey {
157 if (_cert.RSA != null)
159 else if (_cert.DSA != null)
165 _cert.RSA = (RSA) value;
166 else if (value is DSA)
167 _cert.DSA = (DSA) value;
169 throw new NotSupportedException ();
173 public PublicKey PublicKey {
175 if (_publicKey == null) {
176 _publicKey = new PublicKey (_cert);
182 public byte[] RawData {
185 throw new CryptographicException (Locale.GetText ("No certificate data."));
187 return base.GetRawCertData ();
191 public string SerialNumber {
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 ();
204 public Oid SignatureAlgorithm {
209 public X500DistinguishedName SubjectName {
213 public string Thumbprint {
214 get { return base.GetCertHashString (); }
218 get { return _cert.Version; }
224 public void Display ()
229 public void Display (IntPtr hwndParent)
234 public string GetNameInfo (X509NameType nameType, bool forIssuer)
239 public override void Import (byte[] rawData)
241 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
244 [MonoTODO ("missing KeyStorageFlags support")]
245 public override void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
247 base.Import (rawData, password, keyStorageFlags);
248 if (password == null) {
249 _cert = new Mono.Security.X509.X509Certificate (rawData);
250 // TODO - PKCS12 without password
253 MX.PKCS12 pfx = new MX.PKCS12 (rawData, password);
254 if (pfx.Certificates.Count > 0) {
255 _cert = pfx.Certificates [0];
259 if (pfx.Keys.Count > 0) {
260 _cert.RSA = (pfx.Keys [0] as RSA);
261 _cert.DSA = (pfx.Keys [0] as DSA);
266 [MonoTODO ("SecureString is incomplete")]
267 public override void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
269 Import (rawData, (string) null, keyStorageFlags);
272 public override void Import (string fileName)
274 byte[] rawData = Load (fileName);
275 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
278 [MonoTODO ("missing KeyStorageFlags support")]
279 public override void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
281 byte[] rawData = Load (fileName);
282 Import (rawData, password, keyStorageFlags);
285 [MonoTODO ("SecureString is incomplete")]
286 public override void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
288 byte[] rawData = Load (fileName);
289 Import (rawData, (string)null, keyStorageFlags);
292 private byte[] Load (string fileName)
295 using (FileStream fs = new FileStream (fileName, FileMode.Open)) {
296 data = new byte [fs.Length];
297 fs.Read (data, 0, data.Length);
303 public override void Reset ()
311 public override string ToString ()
317 public override string ToString (bool verbose)
323 public bool Verify ()
325 X509Chain chain = new X509Chain ();
326 if (!chain.Build (this))
328 // TODO - check chain and other stuff ???
335 public static X509ContentType GetCertContentType (byte[] rawData)
337 return X509ContentType.Unknown;
341 public static X509ContentType GetCertContentType (string fileName)
343 return X509ContentType.Unknown;