2 // X509Certificate.cs: Handles X.509 certificates.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006 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;
39 using Mono.Security.Authenticode;
41 namespace System.Security.Cryptography.X509Certificates {
44 // a. Internet X.509 Public Key Infrastructure Certificate and CRL Profile
45 // http://www.ietf.org/rfc/rfc3280.txt
47 // LAMESPEC: the MSDN docs always talks about X509v3 certificates
48 // and/or Authenticode certs. However this class works with older
49 // X509v1 certificates and non-authenticode (code signing) certs.
52 public partial class X509Certificate {
54 public partial class X509Certificate : IDeserializationCallback, ISerializable {
56 X509CertificateImpl impl;
58 private bool hideDates;
62 public static X509Certificate CreateFromCertFile (string filename)
64 byte[] data = File.ReadAllBytes (filename);
65 return new X509Certificate (data);
68 [MonoTODO ("Incomplete - minimal validation in this version")]
69 public static X509Certificate CreateFromSignedFile (string filename)
72 AuthenticodeDeformatter a = new AuthenticodeDeformatter (filename);
73 if (a.SigningCertificate != null) {
74 return new X509Certificate (a.SigningCertificate.RawData);
77 catch (SecurityException) {
78 // don't wrap SecurityException into a COMException
82 string msg = Locale.GetText ("Couldn't extract digital signature from {0}.", filename);
83 throw new COMException (msg, e);
85 throw new CryptographicException (Locale.GetText ("{0} isn't signed.", filename));
90 // special constructor for Publisher (and related classes).
91 // Dates strings are null
92 internal X509Certificate (byte[] data, bool dates)
95 Import (data, (string)null, X509KeyStorageFlags.DefaultKeySet);
100 public X509Certificate (byte[] data) : this (data, true)
104 public X509Certificate (IntPtr handle)
106 if (handle == IntPtr.Zero)
107 throw new ArgumentException ("Invalid handle.");
109 impl = X509Helper.InitFromHandle (handle);
112 internal X509Certificate (X509CertificateImpl impl)
115 throw new ArgumentNullException ("impl");
117 this.impl = X509Helper.InitFromCertificate (impl);
120 public X509Certificate (System.Security.Cryptography.X509Certificates.X509Certificate cert)
123 throw new ArgumentNullException ("cert");
125 impl = X509Helper.InitFromCertificate (cert);
129 internal void ImportHandle (X509CertificateImpl impl)
135 internal X509CertificateImpl Impl {
137 X509Helper.ThrowIfContextInvalid (impl);
142 internal bool IsValid {
143 get { return X509Helper.IsValid (impl); }
146 internal void ThrowIfContextInvalid ()
148 X509Helper.ThrowIfContextInvalid (impl);
153 public virtual bool Equals (System.Security.Cryptography.X509Certificates.X509Certificate other)
158 if (!X509Helper.IsValid (other.impl)) {
159 if (!X509Helper.IsValid (impl))
161 throw new CryptographicException (Locale.GetText ("Certificate instance is empty."));
164 return X509CertificateImpl.Equals (impl, other.impl);
168 // LAMESPEC: This is the equivalent of the "thumbprint" that can be seen
169 // in the certificate viewer of Windows. This is ALWAYS the SHA1 hash of
170 // the certificate (i.e. it has nothing to do with the actual hash
171 // algorithm used to sign the certificate).
172 public virtual byte[] GetCertHash ()
174 X509Helper.ThrowIfContextInvalid (impl);
175 return impl.GetCertHash ();
178 public virtual string GetCertHashString ()
180 // must call GetCertHash (not variable) or optimization wont work
181 return X509Helper.ToHexString (GetCertHash ());
184 // strangly there are no DateTime returning function
185 public virtual string GetEffectiveDateString ()
189 X509Helper.ThrowIfContextInvalid (impl);
191 return impl.GetValidFrom ().ToLocalTime ().ToString ();
194 // strangly there are no DateTime returning function
195 public virtual string GetExpirationDateString ()
199 X509Helper.ThrowIfContextInvalid (impl);
201 return impl.GetValidUntil ().ToLocalTime ().ToString ();
204 // well maybe someday there'll be support for PGP or SPKI ?
205 public virtual string GetFormat ()
207 return "X509"; // DO NOT TRANSLATE
210 public override int GetHashCode ()
212 if (!X509Helper.IsValid (impl))
214 return impl.GetHashCode ();
217 [Obsolete ("Use the Issuer property.")]
218 public virtual string GetIssuerName ()
220 X509Helper.ThrowIfContextInvalid (impl);
221 return impl.GetIssuerName (true);
224 public virtual string GetKeyAlgorithm ()
226 X509Helper.ThrowIfContextInvalid (impl);
227 return impl.GetKeyAlgorithm ();
230 public virtual byte[] GetKeyAlgorithmParameters ()
232 X509Helper.ThrowIfContextInvalid (impl);
234 byte[] kap = impl.GetKeyAlgorithmParameters ();
236 throw new CryptographicException (Locale.GetText ("Parameters not part of the certificate"));
241 public virtual string GetKeyAlgorithmParametersString ()
243 return X509Helper.ToHexString (GetKeyAlgorithmParameters ());
246 [Obsolete ("Use the Subject property.")]
247 public virtual string GetName ()
249 X509Helper.ThrowIfContextInvalid (impl);
250 return impl.GetSubjectName (true);
253 public virtual byte[] GetPublicKey ()
255 X509Helper.ThrowIfContextInvalid (impl);
256 return impl.GetPublicKey ();
259 public virtual string GetPublicKeyString ()
261 return X509Helper.ToHexString (GetPublicKey ());
264 public virtual byte[] GetRawCertData ()
266 X509Helper.ThrowIfContextInvalid (impl);
267 return impl.GetRawCertData ();
270 public virtual string GetRawCertDataString ()
272 X509Helper.ThrowIfContextInvalid (impl);
273 return X509Helper.ToHexString (impl.GetRawCertData ());
276 public virtual byte[] GetSerialNumber ()
278 X509Helper.ThrowIfContextInvalid (impl);
279 return impl.GetSerialNumber ();
282 public virtual string GetSerialNumberString ()
284 byte[] sn = GetSerialNumber ();
286 return X509Helper.ToHexString (sn);
289 // to please corcompare ;-)
290 public override string ToString ()
292 return base.ToString ();
295 public virtual string ToString (bool fVerbose)
297 if (!fVerbose || !X509Helper.IsValid (impl))
298 return base.ToString ();
300 return impl.ToString (true);
303 protected static string FormatDate (DateTime date)
305 throw new NotImplementedException ();