[Mono-bugs] [Bug 61796][Nor] Changed - exception getting x509 certificate info

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Tue, 10 Aug 2004 09:36:31 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by sebastien@ximian.com.

http://bugzilla.ximian.com/show_bug.cgi?id=61796

--- shadow/61796	2004-07-23 15:22:16.000000000 -0400
+++ shadow/61796.tmp.20992	2004-08-10 09:36:31.000000000 -0400
@@ -62,6 +62,38 @@
 c. get the binary using Convert.FromBase64String
 d. create a new X509Certificate with the buffer
 
 I'll keep this bug open because I'll fix the (most probably wrong)
 exception, NullReferenceException, in CreateFromSignedFile after
 vacations.
+
+------- Additional Comments From sebastien@ximian.com  2004-08-10 09:36 -------
+Here's a sample, copied from /mcs/tools/certmgr.cs, to create a X.509
+certificate from a PEM (base64 encoded) file.
+
+static byte[] PEM (string type, byte[] data) 
+{
+	string pem = Encoding.ASCII.GetString (data);
+	string header = String.Format ("-----BEGIN {0}-----", type);
+	string footer = String.Format ("-----END {0}-----", type);
+	int start = pem.IndexOf (header) + header.Length;
+	int end = pem.IndexOf (footer, start);
+	string base64 = pem.Substring (start, (end - start));
+	return Convert.FromBase64String (base64);
+}
+
+static X509Certificate LoadFile (string filename)
+{
+	X509Certificate x509 = null;
+	using (FileStream fs = File.OpenRead (filename)) {
+		byte[] data = new byte [fs.Length];
+		fs.Read (data, 0, data.Length);
+		if (data [0] != 0x30) {
+			// maybe it's ASCII PEM base64 encoded ?
+			data = PEM ("CERTIFICATE", data);
+		}
+		if (data != null)
+			x509 = new X509Certificate (data);
+	}
+	return x509;
+}
+