Author: spouliot
Date: 2006-11-08 09:39:52 -0500 (Wed, 08 Nov 2006)
New Revision: 67529

Modified:
   
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
   
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X500DistinguishedName.cs
   
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
   
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
Log:
2006-11-08  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * X500DistinguishedName.cs: Fix most common cases. Some flags are 
        still not supported.
        * X509Certificate2.cs: Fix importing PKCS#12 certificates (with keys)
        in some cases. Implement GetCertContentType for Cert, Pfx and Pkcs12.
        Implement ToString methods.
        * X509ExtensionCollection.cs: Add an internal ctor that can populate
        the collection from extensions coming from an Mono.Security.X509.
        X509Certificate. CryptoConfig is used to allow extensibility to the
        X509Extension class.



Modified: 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
===================================================================
--- 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog  
    2006-11-08 14:34:54 UTC (rev 67528)
+++ 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog  
    2006-11-08 14:39:52 UTC (rev 67529)
@@ -1,3 +1,15 @@
+2006-11-08  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * X500DistinguishedName.cs: Fix most common cases. Some flags are 
+       still not supported.
+       * X509Certificate2.cs: Fix importing PKCS#12 certificates (with keys)
+       in some cases. Implement GetCertContentType for Cert, Pfx and Pkcs12.
+       Implement ToString methods.
+       * X509ExtensionCollection.cs: Add an internal ctor that can populate
+       the collection from extensions coming from an Mono.Security.X509.
+       X509Certificate. CryptoConfig is used to allow extensibility to the
+       X509Extension class.
+
 2006-10-08  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * X509Certificate2.cs: Ensure we can load certificates from read-only

Modified: 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X500DistinguishedName.cs
===================================================================
--- 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X500DistinguishedName.cs
       2006-11-08 14:34:54 UTC (rev 67528)
+++ 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X500DistinguishedName.cs
       2006-11-08 14:39:52 UTC (rev 67529)
@@ -4,7 +4,7 @@
 // Author:
 //     Sebastien Pouliot  <[EMAIL PROTECTED]>
 //
-// 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
@@ -36,6 +36,7 @@
 
 namespace System.Security.Cryptography.X509Certificates {
 
+       [MonoTODO ("Some X500DistinguishedNameFlags options aren't supported, 
like DoNotUsePlusSign, DoNotUseQuotes and ForceUTF8Encoding")]
        public sealed class X500DistinguishedName : AsnEncodedData {
 
                private const X500DistinguishedNameFlags AllFlags = 
X500DistinguishedNameFlags.Reversed |
@@ -45,46 +46,38 @@
                        X500DistinguishedNameFlags.UseT61Encoding | 
X500DistinguishedNameFlags.ForceUTF8Encoding;
 
                private string name;
-               private ArrayList list;
 
-               [MonoTODO]
+
                public X500DistinguishedName (AsnEncodedData 
encodedDistinguishedName)
                {
                        if (encodedDistinguishedName == null)
                                throw new ArgumentNullException 
("encodedDistinguishedName");
+
                        RawData = encodedDistinguishedName.RawData;
-                       DecodeRawData ();
-                       name = Decode (X500DistinguishedNameFlags.None);
+                       if (RawData.Length > 0)
+                               DecodeRawData ();
+                       else
+                               name = String.Empty;
                }
 
-               [MonoTODO]
                public X500DistinguishedName (byte[] encodedDistinguishedName)
                {
                        if (encodedDistinguishedName == null)
                                throw new ArgumentNullException 
("encodedDistinguishedName");
+
                        Oid = new Oid ();
                        RawData = encodedDistinguishedName;
-                       DecodeRawData ();
-                       name = Decode (X500DistinguishedNameFlags.None);
+                       if (encodedDistinguishedName.Length > 0)
+                               DecodeRawData ();
+                       else
+                               name = String.Empty;
                }
 
-               [MonoTODO]
                public X500DistinguishedName (string distinguishedName)
+                       : this (distinguishedName, 
X500DistinguishedNameFlags.Reversed)
                {
-                       if (distinguishedName == null)
-                               throw new ArgumentNullException 
("distinguishedName");
-
-                       if (distinguishedName.Length == 0) {
-                               // empty (0x00) ASN.1 sequence (0x30)
-                               RawData = new byte [2] { 0x30, 0x00 };
-                               DecodeRawData ();
-                       } else {
-                               DecodeName ();
-                               name = distinguishedName;
-                       }
                }
 
-               [MonoTODO]
                public X500DistinguishedName (string distinguishedName, 
X500DistinguishedNameFlags flag)
                {
                        if (distinguishedName == null)
@@ -92,107 +85,97 @@
                        if ((flag != 0) && ((flag & AllFlags) == 0))
                                throw new ArgumentException ("flag");
 
+                       Oid = new Oid ();
                        if (distinguishedName.Length == 0) {
                                // empty (0x00) ASN.1 sequence (0x30)
                                RawData = new byte [2] { 0x30, 0x00 };
                                DecodeRawData ();
                        } else {
-                               DecodeName ();
-                               name = distinguishedName;
+                               ASN1 dn = MX.X501.FromString 
(distinguishedName);
+                               if ((flag & 
X500DistinguishedNameFlags.Reversed) != 0) {
+                                       ASN1 rdn = new ASN1 (0x30);
+                                       for (int i = dn.Count - 1; i >= 0; i--) 
+                                               rdn.Add (dn [i]);
+                                       dn = rdn;
+                               }
+                               RawData = dn.GetBytes ();
+                               if (flag == X500DistinguishedNameFlags.None)
+                                       name = distinguishedName;
+                               else
+                                       name = Decode (flag);
                        }
                }
 
-               [MonoTODO]
                public X500DistinguishedName (X500DistinguishedName 
distinguishedName)
                {
                        if (distinguishedName == null)
                                throw new ArgumentNullException 
("distinguishedName");
+
+                       Oid = new Oid ();
+                       RawData = distinguishedName.RawData;
                        name = distinguishedName.name;
-                       list = (ArrayList) distinguishedName.list.Clone ();
                }
 
-               [MonoTODO]
+
                public string Name {
                        get { return name; }
                }
 
-               [MonoTODO]
+
                public string Decode (X500DistinguishedNameFlags flag)
                {
-                       return String.Empty;
+                       if ((flag != 0) && ((flag & AllFlags) == 0))
+                               throw new ArgumentException ("flag");
+
+                       if (RawData.Length == 0)
+                               return String.Empty;
+
+                       // Mono.Security reversed isn't the same as fx 2.0 
(which is the reverse of 1.x)
+                       bool reversed = ((flag & 
X500DistinguishedNameFlags.Reversed) != 0);
+                       bool quotes = ((flag & 
X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
+                       string separator = GetSeparator (flag);
+
+                       ASN1 rdn = new ASN1 (RawData);
+                       return MX.X501.ToString (rdn, reversed, separator, 
quotes);
                }
 
-               [MonoTODO]
                public override string Format (bool multiLine)
                {
-                       if (list.Count == 0)
-                               return String.Empty;
-
-                       StringBuilder sb = new StringBuilder ();
-                       foreach (DictionaryEntry de in list) {
-                               FormatEntry (sb, de, 
X500DistinguishedNameFlags.None);
-                               if (multiLine)
-                                       sb.Append (Environment.NewLine);
+                       if (multiLine) {
+                               string s = Decode 
(X500DistinguishedNameFlags.UseNewLines);
+                               if (s.Length > 0)
+                                       return s + Environment.NewLine;
+                               else
+                                       return s;
+                       } else {
+                               return Decode 
(X500DistinguishedNameFlags.UseCommas);
                        }
-                       if (multiLine)
-                               sb.Append (Environment.NewLine);
-                       return sb.ToString ();
                }
 
                // private stuff
 
-               private void FormatEntry (StringBuilder sb, DictionaryEntry de, 
X500DistinguishedNameFlags flag)
+               private static string GetSeparator (X500DistinguishedNameFlags 
flag)
                {
-                       sb.Append (de.Key);
-                       sb.Append ("=");
-                       // needs quotes ?
-               }
-
-#if false
-               private string GetSeparator (X500DistinguishedNameFlags flag)
-               {
                        if ((flag & X500DistinguishedNameFlags.UseSemicolons) 
!= 0)
-                               return ";";
+                               return "; ";
                        if ((flag & X500DistinguishedNameFlags.UseCommas) != 0)
-                               return ",";
+                               return ", ";
                        if ((flag & X500DistinguishedNameFlags.UseNewLines) != 
0)
                                return Environment.NewLine;
-                       return ","; //default
+                       return ", "; //default
                }
-#endif
 
                // decode the DN using the (byte[]) RawData
                private void DecodeRawData ()
                {
-                       list = new ArrayList ();
                        if ((RawData == null) || (RawData.Length < 3)) {
                                name = String.Empty;
                                return;
                        }
 
                        ASN1 sequence = new ASN1 (RawData);
-                       for (int i=0; i < sequence.Count; i++) {
-                       }
+                       name = MX.X501.ToString (sequence, true, ", ", true);
                }
-
-               // decode the DN using the (string) name
-               private void DecodeName ()
-               {
-                       if ((name == null) || (name.Length == 0))
-                               return;
-
-                       ASN1 dn = MX.X501.FromString (name);
-
-/*                     int pos = 0;
-                       ASN1 asn1 = new ASN1 (0x30);
-                       while (pos < name.Length) {
-                               MX.X520.AttributeTypeAndValue atv = 
ReadAttribute (name, ref pos);
-                               atv.Value = ReadValue (name, ref pos);
-                       }*/
-
-                       RawData = dn.GetBytes ();
-                       DecodeRawData ();
-               }
        }
 }
 

Modified: 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
===================================================================
--- 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
    2006-11-08 14:34:54 UTC (rev 67528)
+++ 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
    2006-11-08 14:39:52 UTC (rev 67529)
@@ -31,6 +31,7 @@
 
 using System.IO;
 using System.Text;
+using Mono.Security;
 using Mono.Security.Cryptography;
 using MX = Mono.Security.X509;
 
@@ -40,72 +41,68 @@
 
                private bool _archived;
                private X509ExtensionCollection _extensions;
-               private string _name;
+               private string _name = String.Empty;
                private string _serial;
                private PublicKey _publicKey;
-               X500DistinguishedName _issuer_name;
+               private X500DistinguishedName issuer_name;
+               private X500DistinguishedName subject_name;
+               private Oid signature_algorithm;
 
                private MX.X509Certificate _cert;
 
                // constructors
 
-               public X509Certificate2 () : base () 
+               public X509Certificate2 ()
                {
                        _cert = null;
                }
 
-               public X509Certificate2 (byte[] rawData) : base (rawData) 
+               public X509Certificate2 (byte[] rawData)
                {
-                       _cert = new MX.X509Certificate (base.GetRawCertData ());
+                       Import (rawData, (string)null, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
-               public X509Certificate2 (byte[] rawData, string password) : 
base (rawData, password) 
+               public X509Certificate2 (byte[] rawData, string password)
                {
                        Import (rawData, password, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
-               public X509Certificate2 (byte[] rawData, SecureString password) 
: base (rawData, password) 
+               public X509Certificate2 (byte[] rawData, SecureString password)
                {
                        Import (rawData, password, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
                public X509Certificate2 (byte[] rawData, string password, 
X509KeyStorageFlags keyStorageFlags)
-                       : base (rawData, password, keyStorageFlags) 
                {
                        Import (rawData, password, keyStorageFlags);
                }
 
                public X509Certificate2 (byte[] rawData, SecureString password, 
X509KeyStorageFlags keyStorageFlags)
-                       : base (rawData, password, keyStorageFlags) 
                {
                        Import (rawData, password, keyStorageFlags);
                }
 
                public X509Certificate2 (string fileName) : base (fileName) 
                {
-                       _cert = new MX.X509Certificate (base.GetRawCertData ());
+                       Import (fileName, (string)null, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
                public X509Certificate2 (string fileName, string password)
-                       : base (fileName, password)
                {
                        Import (fileName, password, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
                public X509Certificate2 (string fileName, SecureString password)
-                       : base (fileName, password)
                {
                        Import (fileName, password, 
X509KeyStorageFlags.DefaultKeySet);
                }
 
                public X509Certificate2 (string fileName, string password, 
X509KeyStorageFlags keyStorageFlags)
-                       : base (fileName, password, keyStorageFlags) 
                {
                        Import (fileName, password, keyStorageFlags);
                }
 
                public X509Certificate2 (string fileName, SecureString 
password, X509KeyStorageFlags keyStorageFlags)
-                       : base (fileName, password, keyStorageFlags) 
                {
                        Import (fileName, password, keyStorageFlags);
                }
@@ -129,7 +126,11 @@
                }
 
                public X509ExtensionCollection Extensions {
-                       get { return _extensions; }
+                       get {
+                               if (_extensions == null)
+                                       _extensions = new 
X509ExtensionCollection (_cert);
+                               return _extensions;
+                       }
                }
 
                public string FriendlyName {
@@ -137,17 +138,16 @@
                        set { _name = value; }
                }
 
-               [MonoTODO ("Probably it could be more efficient")]
+               // FIXME - Could be more efficient
                public bool HasPrivateKey {
                        get { return PrivateKey != null; }
                }
 
-               [MonoTODO]
                public X500DistinguishedName IssuerName {
                        get {
-                               if (_issuer_name == null)
-                                       _issuer_name = new 
X500DistinguishedName (Issuer);
-                               return _issuer_name;
+                               if (issuer_name == null)
+                                       issuer_name = new X500DistinguishedName 
(_cert.GetIssuerName ().GetBytes ());
+                               return issuer_name;
                        }
                } 
 
@@ -161,31 +161,28 @@
 
                public AsymmetricAlgorithm PrivateKey {
                        get {
-                               if (_cert.RSA != null) {
-                                       RSACryptoServiceProvider rcsp = 
_cert.RSA as RSACryptoServiceProvider;
-                                       if (rcsp != null)
-                                               return rcsp.PublicOnly ? null : 
rcsp;
-                                       RSAManaged rsam = _cert.RSA as 
RSAManaged;
-                                       if (rsam != null)
-                                               return rsam.PublicOnly ? null : 
rsam;
-                                       try {
+                               try {
+                                       if (_cert.RSA != null) {
+                                               RSACryptoServiceProvider rcsp = 
_cert.RSA as RSACryptoServiceProvider;
+                                               if (rcsp != null)
+                                                       return rcsp.PublicOnly 
? null : rcsp;
+                                               RSAManaged rsam = _cert.RSA as 
RSAManaged;
+                                               if (rsam != null)
+                                                       return rsam.PublicOnly 
? null : rsam;
+
                                                _cert.RSA.ExportParameters 
(true);
                                                return _cert.RSA;
-                                       } catch (CryptographicException) {
-                                               return null;
-                                       }
-                               }
-                               else if (_cert.DSA != null) {
-                                       DSACryptoServiceProvider dcsp = 
_cert.DSA as DSACryptoServiceProvider;
-                                       if (dcsp != null)
-                                               return dcsp.PublicOnly ? null : 
dcsp;
-                                       try {
+                                       } else if (_cert.DSA != null) {
+                                               DSACryptoServiceProvider dcsp = 
_cert.DSA as DSACryptoServiceProvider;
+                                               if (dcsp != null)
+                                                       return dcsp.PublicOnly 
? null : dcsp;
+       
                                                _cert.DSA.ExportParameters 
(true);
                                                return _cert.DSA;
-                                       } catch (CryptographicException) {
-                                               return null;
                                        }
                                }
+                               catch {
+                               }
                                return null;
                        }
                        set {
@@ -201,7 +198,13 @@
                public PublicKey PublicKey {
                        get { 
                                if (_publicKey == null) {
-                                       _publicKey = new PublicKey (_cert);
+                                       try {
+                                               _publicKey = new PublicKey 
(_cert);
+                                       }
+                                       catch (Exception e) {
+                                               string msg = Locale.GetText 
("Unable to decode public key.");
+                                               throw new 
CryptographicException (msg, e);
+                                       }
                                }
                                return _publicKey;
                        }
@@ -230,12 +233,19 @@
                } 
 
                public Oid SignatureAlgorithm {
-                       get { return null; }
+                       get {
+                               if (signature_algorithm == null)
+                                       signature_algorithm = new Oid 
(_cert.SignatureAlgorithm);
+                               return signature_algorithm;
+                       }
                } 
 
-               [MonoTODO]
                public X500DistinguishedName SubjectName {
-                       get { return null; }
+                       get {
+                               if (subject_name == null)
+                                       subject_name = new 
X500DistinguishedName (_cert.GetSubjectName ().GetBytes ());
+                               return subject_name;
+                       }
                } 
 
                public string Thumbprint {
@@ -248,12 +258,36 @@
 
                // methods
 
-               [MonoTODO]
+               [MonoTODO ("always returns String.Empty")]
                public string GetNameInfo (X509NameType nameType, bool 
forIssuer) 
                {
-                       return null;
+                       switch (nameType) {
+                       case X509NameType.SimpleName:
+                       case X509NameType.EmailName:
+                       case X509NameType.UpnName:
+                       case X509NameType.DnsName:
+                       case X509NameType.DnsFromAlternativeName:
+                       case X509NameType.UrlName:
+                               return String.Empty;
+                       default:
+                               throw new ArgumentException ("nameType");
+                       }
                }
 
+               private void ImportPkcs12 (byte[] rawData, string password)
+               {
+                       MX.PKCS12 pfx = (password == null) ? new MX.PKCS12 
(rawData) : new MX.PKCS12 (rawData, password);
+                       if (pfx.Certificates.Count > 0) {
+                               _cert = pfx.Certificates [0];
+                       } else {
+                               _cert = null;
+                       }
+                       if (pfx.Keys.Count > 0) {
+                               _cert.RSA = (pfx.Keys [0] as RSA);
+                               _cert.DSA = (pfx.Keys [0] as DSA);
+                       }
+               }
+
                public override void Import (byte[] rawData) 
                {
                        Import (rawData, (string)null, 
X509KeyStorageFlags.DefaultKeySet);
@@ -264,22 +298,24 @@
                {
                        base.Import (rawData, password, keyStorageFlags);
                        if (password == null) {
-                               _cert = new Mono.Security.X509.X509Certificate 
(rawData);
-                               // TODO - PKCS12 without password
-                       } else {
-                               // try PKCS#12
                                try {
-                                       MX.PKCS12 pfx = new MX.PKCS12 (rawData, 
password);
-                                       if (pfx.Certificates.Count > 0) {
-                                               _cert = pfx.Certificates [0];
-                                       } else {
-                                               _cert = null;
+                                       _cert = new 
Mono.Security.X509.X509Certificate (rawData);
+                               }
+                               catch (Exception e) {
+                                       try {
+                                               ImportPkcs12 (rawData, null);
                                        }
-                                       if (pfx.Keys.Count > 0) {
-                                               _cert.RSA = (pfx.Keys [0] as 
RSA);
-                                               _cert.DSA = (pfx.Keys [0] as 
DSA);
+                                       catch {
+                                               string msg = Locale.GetText 
("Unable to decode certificate.");
+                                               // inner exception is the 
original (not second) exception
+                                               throw new 
CryptographicException (msg, e);
                                        }
                                }
+                       } else {
+                               // try PKCS#12
+                               try {
+                                       ImportPkcs12 (rawData, password);
+                               }
                                catch {
                                        // it's possible to supply a 
(unrequired/unusued) password
                                        // fix bug #79028
@@ -314,7 +350,7 @@
                        Import (rawData, (string)null, keyStorageFlags);
                }
 
-               private byte[] Load (string fileName)
+               private static byte[] Load (string fileName)
                {
                        byte[] data = null;
                        using (FileStream fs = File.OpenRead (fileName)) {
@@ -332,18 +368,57 @@
                        base.Reset ();
                }
 
-               [MonoTODO]
                public override string ToString ()
                {
-                       return null;
+                       return base.ToString (true);
                }
 
-               [MonoTODO]
                public override string ToString (bool verbose)
                {
-                       return null;
+                       // the non-verbose X509Certificate2 == verbose 
X509Certificate
+                       if (!verbose)
+                               return base.ToString (true);
+
+                       string nl = Environment.NewLine;
+                       StringBuilder sb = new StringBuilder ();
+                       sb.AppendFormat ("[Version]{0}  V{1}{0}{0}", nl, 
Version);
+                       sb.AppendFormat ("[Subject]{0}  {1}{0}{0}", nl, 
Subject);
+                       sb.AppendFormat ("[Issuer]{0}  {1}{0}{0}", nl, Issuer);
+                       sb.AppendFormat ("[Serial Number]{0}  {1}{0}{0}", nl, 
SerialNumber);
+                       sb.AppendFormat ("[Not Before]{0}  {1}{0}{0}", nl, 
NotBefore);
+                       sb.AppendFormat ("[Not After]{0}  {1}{0}{0}", nl, 
NotAfter);
+                       sb.AppendFormat ("[Thumbprint]{0}  {1}{0}{0}", nl, 
Thumbprint);
+                       sb.AppendFormat ("[Signature Algorithm]{0}  
{1}({2}){0}{0}", nl, SignatureAlgorithm.FriendlyName, 
+                               SignatureAlgorithm.Value);
+
+                       AsymmetricAlgorithm key = PublicKey.Key;
+                       sb.AppendFormat ("[Public Key]{0}  Algorithm: ", nl);
+                       if (key is RSA)
+                               sb.Append ("RSA");
+                       else if (key is DSA)
+                               sb.Append ("DSA");
+                       else
+                               sb.Append (key.ToString ());
+                       sb.AppendFormat ("{0}  Length: {1}{0}  Key Blob: ", nl, 
key.KeySize);
+                       AppendBuffer (sb, PublicKey.EncodedKeyValue.RawData);
+                       sb.AppendFormat ("{0}  Parameters: ", nl);
+                       AppendBuffer (sb, PublicKey.EncodedParameters.RawData);
+                       sb.Append (nl);
+
+                       return sb.ToString ();
                }
 
+               private static void AppendBuffer (StringBuilder sb, byte[] 
buffer)
+               {
+                       if (buffer == null)
+                               return;
+                       for (int i=0; i < buffer.Length; i++) {
+                               sb.Append (buffer [i].ToString ("x2"));
+                               if (i < buffer.Length - 1)
+                                       sb.Append (" ");
+                       }
+               }
+
                [MonoTODO]
                public bool Verify ()
                {
@@ -356,16 +431,54 @@
 
                // static methods
 
-               [MonoTODO]
+               [MonoTODO ("Detection limited to Cert, Pfx, Pkcs12 and 
Unknown")]
                public static X509ContentType GetCertContentType (byte[] 
rawData)
                {
-                       return X509ContentType.Unknown;
+                       if ((rawData == null) || (rawData.Length == 0))
+                               throw new ArgumentException ("rawData");
+
+                       X509ContentType type = X509ContentType.Unknown;
+                       try {
+                               ASN1 data = new ASN1 (rawData);
+                               if (data.Tag != 0x30) {
+                                       string msg = Locale.GetText ("Unable to 
decode certificate.");
+                                       throw new CryptographicException (msg);
+                               }
+
+                               if (data.Count == 3) {
+                                       switch (data [0].Tag) {
+                                       case 0x30:
+                                               // SEQUENCE / SEQUENCE / 
BITSTRING
+                                               if ((data [1].Tag == 0x30) && 
(data [2].Tag == 0x03))
+                                                       type = 
X509ContentType.Cert;
+                                               break;
+                                       case 0x02:
+                                               // INTEGER / SEQUENCE / SEQUENCE
+                                               if ((data [1].Tag == 0x30) && 
(data [2].Tag == 0x30))
+                                                       type = 
X509ContentType.Pkcs12;
+                                               // note: Pfx == Pkcs12
+                                               break;
+                                       }
+                               }
+                       }
+                       catch (Exception e) {
+                               string msg = Locale.GetText ("Unable to decode 
certificate.");
+                               throw new CryptographicException (msg, e);
+                       }
+
+                       return type;
                }
 
-               [MonoTODO]
+               [MonoTODO ("Detection limited to Cert, Pfx, Pkcs12 and 
Unknown")]
                public static X509ContentType GetCertContentType (string 
fileName)
                {
-                       return X509ContentType.Unknown;
+                       if (fileName == null)
+                               throw new ArgumentNullException ("fileName");
+                       if (fileName.Length == 0)
+                               throw new ArgumentException ("fileName");
+
+                       byte[] data = Load (fileName);
+                       return GetCertContentType (data);
                }
        }
 }

Modified: 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
===================================================================
--- 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
     2006-11-08 14:34:54 UTC (rev 67528)
+++ 
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
     2006-11-08 14:39:52 UTC (rev 67529)
@@ -32,6 +32,8 @@
 #if NET_2_0 && SECURITY_DEP
 
 using System.Collections;
+using Mono.Security;
+using MX = Mono.Security.X509;
 
 namespace System.Security.Cryptography.X509Certificates {
 
@@ -46,6 +48,33 @@
                        _list = new ArrayList ();
                }
 
+               internal X509ExtensionCollection (MX.X509Certificate cert)
+               {
+                       _list = new ArrayList (cert.Extensions.Count);
+                       if (cert.Extensions.Count == 0)
+                               return;
+
+                       object[] parameters = new object [2];
+                       foreach (MX.X509Extension ext in cert.Extensions) {
+                               bool critical = ext.Critical;
+                               string oid = ext.Oid;
+                               byte[] raw_data = null;
+                               // extension data is embedded in an octet 
stream (4)
+                               ASN1 value = ext.Value;
+                               if ((value.Tag == 0x04) && (value.Count > 0))
+                                       raw_data = value [0].GetBytes ();
+
+                               parameters [0] = new AsnEncodedData (oid, 
raw_data);
+                               parameters [1] = critical;
+                               X509Extension newt = (X509Extension) 
CryptoConfig.CreateFromName (oid, parameters);
+                               if (newt == null) {
+                                       // not registred in CryptoConfig, using 
default
+                                       newt = new X509Extension (oid, 
raw_data, critical);
+                               }
+                               _list.Add (newt);
+                       }
+               }
+
                // properties
 
                public int Count {

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to