New test.
[mono.git] / mcs / class / corlib / System.Security.Cryptography.X509Certificates / X509Certificate.cs
index b61fd6e0da365263ca60a05f143955cc57b35a2a..c226033ad20e0f0e33f393a09dc27565e9eea7ad 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -98,7 +98,7 @@ namespace System.Security.Cryptography.X509Certificates {
                public static X509Certificate CreateFromCertFile (string filename) 
                {
                        byte[] data = null;
-                       using (FileStream fs = new FileStream (filename, FileMode.Open)) {
+                       using (FileStream fs = File.OpenRead (filename)) {
                                data = new byte [fs.Length];
                                fs.Read (data, 0, data.Length);
                                fs.Close ();
@@ -160,11 +160,20 @@ namespace System.Security.Cryptography.X509Certificates {
                                Marshal.Copy (cc.pbCertEncoded, data, 0, (int)cc.cbCertEncoded);
                                x509 = new Mono.Security.X509.X509Certificate (data);
                        }
+#if NET_2_0
+                       else
+                               throw new ArgumentException ("Invalid handle.");
+#endif
                        // IntPtr.Zero results in an "empty" certificate instance
                }
        
                public X509Certificate (System.Security.Cryptography.X509Certificates.X509Certificate cert) 
                {
+#if NET_2_0
+                       if (cert == null)
+                               throw new ArgumentNullException ();
+#endif
+
                        if (cert != null) {
                                byte[] data = cert.GetRawCertData ();
                                if (data != null)
@@ -262,7 +271,9 @@ namespace System.Security.Cryptography.X509Certificates {
                                                return false;
                                }
                        }
-                       return (x509.RawData == null);
+                       else
+                               return false;
+                       return x509 == null || (x509.RawData == null);
                }
        
                // LAMESPEC: This is the equivalent of the "thumbprint" that can be seen
@@ -286,25 +297,31 @@ namespace System.Security.Cryptography.X509Certificates {
                }
        
                // strangly there are no DateTime returning function
-               // LAMESPEC: Microsoft returns the local time from Pacific Time (GMT-8)
-               // BUG: This will not be corrected in Framework 1.1 and also affect WSE 1.0
                public virtual string GetEffectiveDateString ()
                {
                        if (hideDates)
                                return null;
-                       DateTime dt = x509.ValidFrom.ToUniversalTime().AddHours (-8);
-                       return dt.ToString (); //"yyyy-MM-dd HH:mm:ss");
+#if NET_2_0
+                       return x509.ValidFrom.ToString ();
+#else
+                       // LAMESPEC: Microsoft returns the local time from Pacific Time (GMT-8)
+                       // BUG: This will not be corrected in Framework 1.1 and also affect WSE 1.0
+                       return x509.ValidFrom.ToUniversalTime ().AddHours (-8).ToString ();
+#endif
                }
        
                // strangly there are no DateTime returning function
-               // LAMESPEC: Microsoft returns the local time from Pacific Time (GMT-8)
-               // BUG: This will not be corrected in Framework 1.1 and also affect WSE 1.0
                public virtual string GetExpirationDateString () 
                {
                        if (hideDates)
                                return null;
-                       DateTime dt = x509.ValidUntil.ToUniversalTime().AddHours (-8);
-                       return dt.ToString (); //"yyyy-MM-dd HH:mm:ss");
+#if NET_2_0
+                       return x509.ValidUntil.ToString ();
+#else
+                       // LAMESPEC: Microsoft returns the local time from Pacific Time (GMT-8)
+                       // BUG: This will not be corrected in Framework 1.1 and also affect WSE 1.0
+                       return x509.ValidUntil.ToUniversalTime ().AddHours (-8).ToString ();
+#endif
                }
        
                // well maybe someday there'll be support for PGP or SPKI ?
@@ -520,11 +537,18 @@ namespace System.Security.Cryptography.X509Certificates {
                                // TODO - PKCS12 without password
                        } else {
                                // try PKCS#12
-                               PKCS12 pfx = new PKCS12 (rawData, password);
-                               if (pfx.Certificates.Count > 0) {
-                                       x509 = pfx.Certificates [0];
-                               } else {
-                                       x509 = null;
+                               try {
+                                       PKCS12 pfx = new PKCS12 (rawData, password);
+                                       if (pfx.Certificates.Count > 0) {
+                                               x509 = pfx.Certificates [0];
+                                       } else {
+                                               x509 = null;
+                                       }
+                               }
+                               catch {
+                                       // it's possible to supply a (unrequired/unusued) password
+                                       // fix bug #79028
+                                       x509 = new Mono.Security.X509.X509Certificate (rawData);
                                }
                        }
                }