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.
30 #if NET_2_0 && SECURITY_DEP
35 using MX = Mono.Security.X509;
37 namespace System.Security.Cryptography.X509Certificates {
39 public class X509Certificate2 : X509Certificate {
41 private bool _archived;
42 private X509ExtensionCollection _extensions;
44 private string _serial;
45 private PublicKey _publicKey;
47 private MX.X509Certificate _cert;
51 public X509Certificate2 () : base ()
56 public X509Certificate2 (byte[] rawData) : base (rawData)
58 _cert = new MX.X509Certificate (base.GetRawCertData ());
61 public X509Certificate2 (byte[] rawData, string password) : base (rawData, password)
63 _cert = new MX.X509Certificate (base.GetRawCertData ());
66 public X509Certificate2 (byte[] rawData, SecureString password) : base (rawData, password)
68 _cert = new MX.X509Certificate (base.GetRawCertData ());
71 public X509Certificate2 (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
72 : base (rawData, password, keyStorageFlags)
74 _cert = new MX.X509Certificate (base.GetRawCertData ());
77 public X509Certificate2 (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
78 : base (rawData, password, keyStorageFlags)
80 _cert = new MX.X509Certificate (base.GetRawCertData ());
83 public X509Certificate2 (string fileName) : base (fileName)
85 _cert = new MX.X509Certificate (base.GetRawCertData ());
88 public X509Certificate2 (string fileName, string password)
90 _cert = new MX.X509Certificate (base.GetRawCertData ());
93 public X509Certificate2 (string fileName, SecureString password)
95 _cert = new MX.X509Certificate (base.GetRawCertData ());
98 public X509Certificate2 (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
99 : base (fileName, password, keyStorageFlags)
101 _cert = new MX.X509Certificate (base.GetRawCertData ());
104 public X509Certificate2 (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
105 : base (fileName, password, keyStorageFlags)
107 _cert = new MX.X509Certificate (base.GetRawCertData ());
110 public X509Certificate2 (IntPtr handle) : base (handle)
112 _cert = new MX.X509Certificate (base.GetRawCertData ());
115 public X509Certificate2 (X509Certificate certificate)
117 _cert = new MX.X509Certificate (base.GetRawCertData ());
122 public bool Archived {
123 get { return _archived; }
124 set { _archived = value; }
127 public X509ExtensionCollection Extensions {
128 get { return _extensions; }
131 public string FriendlyName {
132 get { return _name; }
133 set { _name = value; }
137 public bool HasPrivateKey {
138 get { return false; }
142 public X500DistinguishedName IssuerName {
146 public DateTime NotAfter {
147 get { return _cert.ValidUntil; }
150 public DateTime NotBefore {
151 get { return _cert.ValidFrom; }
154 public AsymmetricAlgorithm PrivateKey {
156 if (_cert.RSA != null)
158 else if (_cert.DSA != null)
164 _cert.RSA = (RSA) value;
165 else if (value is DSA)
166 _cert.DSA = (DSA) value;
168 throw new NotSupportedException ();
172 public PublicKey PublicKey {
174 if (_publicKey == null) {
175 _publicKey = new PublicKey (_cert);
181 public byte[] RawData {
184 throw new CryptographicException (Locale.GetText ("No certificate data."));
186 return base.GetRawCertData ();
190 public string SerialNumber {
192 if (_serial == null) {
193 StringBuilder sb = new StringBuilder ();
194 byte[] serial = _cert.SerialNumber;
195 for (int i=serial.Length - 1; i >= 0; i--)
196 sb.Append (serial [i].ToString ("X2"));
197 _serial = sb.ToString ();
203 public Oid SignatureAlgorithm {
208 public X500DistinguishedName SubjectName {
212 public string Thumbprint {
213 get { return base.GetCertHashString (); }
217 get { return _cert.Version; }
223 public string GetNameInfo (X509NameType nameType, bool forIssuer)
228 public override void Import (byte[] rawData)
230 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
233 [MonoTODO ("missing KeyStorageFlags support")]
234 public override void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
236 base.Import (rawData, password, keyStorageFlags);
237 if (password == null) {
238 _cert = new Mono.Security.X509.X509Certificate (rawData);
239 // TODO - PKCS12 without password
242 MX.PKCS12 pfx = new MX.PKCS12 (rawData, password);
243 if (pfx.Certificates.Count > 0) {
244 _cert = pfx.Certificates [0];
248 if (pfx.Keys.Count > 0) {
249 _cert.RSA = (pfx.Keys [0] as RSA);
250 _cert.DSA = (pfx.Keys [0] as DSA);
255 [MonoTODO ("SecureString is incomplete")]
256 public override void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
258 Import (rawData, (string) null, keyStorageFlags);
261 public override void Import (string fileName)
263 byte[] rawData = Load (fileName);
264 Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
267 [MonoTODO ("missing KeyStorageFlags support")]
268 public override void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
270 byte[] rawData = Load (fileName);
271 Import (rawData, password, keyStorageFlags);
274 [MonoTODO ("SecureString is incomplete")]
275 public override void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
277 byte[] rawData = Load (fileName);
278 Import (rawData, (string)null, keyStorageFlags);
281 private byte[] Load (string fileName)
284 using (FileStream fs = new FileStream (fileName, FileMode.Open)) {
285 data = new byte [fs.Length];
286 fs.Read (data, 0, data.Length);
292 public override void Reset ()
300 public override string ToString ()
306 public override string ToString (bool verbose)
312 public bool Verify ()
314 X509Chain chain = new X509Chain ();
315 if (!chain.Build (this))
317 // TODO - check chain and other stuff ???
324 public static X509ContentType GetCertContentType (byte[] rawData)
326 return X509ContentType.Unknown;
330 public static X509ContentType GetCertContentType (string fileName)
332 return X509ContentType.Unknown;