Explorar o código

*** empty log message ***

svn path=/trunk/mcs/; revision=9023
Sebastien Pouliot %!s(int64=23) %!d(string=hai) anos
pai
achega
db91d39fd8

+ 59 - 1
mcs/class/corlib/System.Security.Cryptography/CryptoConfig.cs

@@ -18,6 +18,7 @@ public class CryptoConfig
 {
 	static private Hashtable algorithms;
 	static private Hashtable oid;
+	static Assembly xmldsig;
 
 	private const string defaultNamespace = "System.Security.Cryptography.";
 	private const string defaultSHA1 = defaultNamespace + "SHA1CryptoServiceProvider";
@@ -35,6 +36,16 @@ public class CryptoConfig
 	private const string defaultRNG = defaultNamespace + "RNGCryptoServiceProvider";
 	private const string defaultHMAC = defaultNamespace + "HMACSHA1";
 	private const string defaultMAC3DES = defaultNamespace + "MACTripleDES";
+	// LAMESPEC: undocumented classes (also undocumented in CryptoConfig ;-)
+	private const string defaultDSASigDesc = defaultNamespace + "DSASignatureDescription";
+	private const string defaultRSASigDesc = defaultNamespace + "RSAPKCS1SHA1SignatureDescription";
+	// LAMESPEC: undocumented names in CryptoConfig
+	private const string defaultC14N = defaultNamespace + "Xml.XmlDsigC14NTransform";
+	private const string defaultC14NWithComments = defaultNamespace + "Xml.XmlDsigC14NWithCommentsTransform";
+	private const string defaultBase64 = defaultNamespace + "Xml.XmlDsigBase64Transform";
+	private const string defaultXPath = defaultNamespace + "Xml.XmlDsigXPathTransform";
+	private const string defaultXslt = defaultNamespace + "Xml.XmlDsigXsltTransform";
+	private const string defaultEnveloped = defaultNamespace + "Xml.XmlDsigEnvelopedSignatureTransform";
 
 	// Oddly OID seems only available for hash algorithms
 	private const string oidSHA1 = "1.3.14.3.2.26";
@@ -82,6 +93,16 @@ public class CryptoConfig
 	private const string nameHMACb = "System.Security.Cryptography.HMACSHA1";
 	private const string nameMAC3DESa = "MACTripleDES";
 	private const string nameMAC3DESb = "System.Security.Cryptography.MACTripleDES";
+	// LAMESPEC: undocumented URLs in CryptoConfig
+	private const string urlDSASHA1 = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
+	private const string urlRSASHA1 = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
+	private const string urlSHA1 = "http://www.w3.org/2000/09/xmldsig#sha1";
+	private const string urlC14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; 
+	private const string urlC14NWithComments = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
+	private const string urlBase64 = "http://www.w3.org/2000/09/xmldsig#base64";
+	private const string urlXPath = "http://www.w3.org/TR/1999/REC-xpath-19991116";
+	private const string urlXslt = "http://www.w3.org/TR/1999/REC-xslt-19991116";
+	private const string urlEnveloped = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
 
 	// ??? must we read from the machine.config each time or just at startup ???
 	[MonoTODO ("support machine.config")]
@@ -143,6 +164,19 @@ public class CryptoConfig
 		algorithms.Add (nameMAC3DESa, defaultMAC3DES);
 		algorithms.Add (nameMAC3DESb, defaultMAC3DES);
 
+		// LAMESPEC These URLs aren't documented but (hint) installing the WSDK
+		// add some of the XMLDSIG urls into machine.config (and they make a LOT
+		// of sense for implementing XMLDSIG in System.Security.Cryptography.Xml)
+		algorithms.Add (urlDSASHA1, defaultDSASigDesc); 
+		algorithms.Add (urlRSASHA1, defaultRSASigDesc);
+		algorithms.Add (urlSHA1, defaultSHA1);
+		algorithms.Add (urlC14N, defaultC14N);
+		algorithms.Add (urlC14NWithComments, defaultC14NWithComments);
+		algorithms.Add (urlBase64, defaultBase64);
+		algorithms.Add (urlXPath, defaultXPath);
+		algorithms.Add (urlXslt, defaultXslt);
+		algorithms.Add (urlEnveloped, defaultEnveloped);
+
 		oid = new Hashtable ();
 		// comments here are to match with MS implementation (but not with doc)
 		// LAMESPEC: only HashAlgorithm seems to have their OID included
@@ -174,8 +208,22 @@ public class CryptoConfig
 			throw new ArgumentNullException ();
 	
 		try {
+			Type algoClass = null;
 			string algo = (string)algorithms [name];
-			Type algoClass = Type.GetType (algo);
+			// do we have an entry
+			if (algo != null) {
+				algoClass = Type.GetType (algo);
+				// some classes are in assembly System.Security.Cryptography.Xml
+				if ((algoClass == null) && (algo.StartsWith ("System.Security.Cryptography.Xml."))) {
+					// second chance !
+					if (xmldsig == null)
+						xmldsig = Assembly.LoadWithPartialName ("System.Security");
+					if (xmldsig != null)
+						algoClass = xmldsig.GetType (algo);
+				}
+			}
+			else
+				algoClass = Type.GetType (name);
 			// call the constructor for the type
 			return Activator.CreateInstance (algoClass, args);
 		}
@@ -184,6 +232,16 @@ public class CryptoConfig
 		}
 	}
 
+	// Note: Couldn't access private in DefaultConfig so I copied the
+	// two required functions.
+/*	[MethodImplAttribute(MethodImplOptions.InternalCall)]
+	extern private static string get_machine_config_path ();
+
+	private static string GetMachineConfigPath () 
+	{
+		return get_machine_config_path ();
+	}*/
+
 	// encode (7bits array) number greater than 127
 	private static byte[] EncodeLongNumber (long x)
 	{

+ 36 - 50
mcs/class/corlib/System.Security.Cryptography/SignatureDescription.cs

@@ -8,14 +8,10 @@
 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
 //
 
-// TODO: Implement SecurityElement parsing
-// TODO: Complete AsymmetricSignatureFormatter & AsymmetricSignatureDeformatter methods
-
 // Notes:
 // There seems to be some (internal?) class inheriting from SignatureDescription
 // http://www.csharpfriends.com/Members/Main/Classes/get_class.aspx?assembly=mscorlib,%20Version=1.0.3300.0,%20Culture=neutral,%20PublicKeyToken=b77a5c561934e089&namespace=System.Security.Cryptography&class=SignatureDescription
-// However I've no idea where the class is being used in the framework 
-// (doesn't look like it's for every users ;-)
+// Those 2 classes are returned by CryptoConfig.CreateFromName and used in XMLDSIG
 
 using System;
 using System.Security;
@@ -30,10 +26,8 @@ public class SignatureDescription {
 
 	public SignatureDescription () {}
 	
-	/// <summary>
 	/// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
-	/// </summary>
-	[MonoTODO]
+	[MonoTODO("Parse SecurityElement")]
 	public SignatureDescription (SecurityElement el) 
 	{
 		if (el == null)
@@ -68,24 +62,6 @@ public class SignatureDescription {
 		set { _KeyAlgorithm = value; }
 	}
 
-	private object CreateFromName (string objectName) 
-	{
-		try {
-			// first try
-			Type algoClass = Type.GetType (objectName);
-			if (algoClass == null) {
-				// second (and last) try
-				algoClass = Type.GetType ("System.Security.Cryptography." + objectName);
-			}
-			// call the constructor for the type
-			return Activator.CreateInstance (algoClass);
-		}
-		catch {
-			return null;
-		}
-	}
-
-	[MonoTODO]
 	public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
 	{
 		if (_DeformatterAlgorithm == null)
@@ -93,18 +69,13 @@ public class SignatureDescription {
 
 		// this should throw the InvalidCastException if we have an invalid class
 		// (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
-		AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CreateFromName (_DeformatterAlgorithm);
-		if (def == null)
-			throw new InvalidCastException ("DeformatterAlgorithm");
-		def.SetKey (key);
+		AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName (_DeformatterAlgorithm);
 
-		throw new NullReferenceException ("why?");
-		
-		// We must make a choice of the Deformatter based on
-		// the DeformatterAlgorithm property (factory like CryptoConfig ?)
-		// There are only 2 SignatureDeformatter based on the
-		// key algorithm (DSA or RSA) - but how does the 
-		// KeyAlgorithm property string really looks like ?
+		if (_KeyAlgorithm == null)
+			throw new NullReferenceException ("KeyAlgorithm");
+
+		def.SetKey (key);
+		return def;
 	}
 	
 	/// <summary>
@@ -112,10 +83,11 @@ public class SignatureDescription {
 	/// </summary>
 	public virtual HashAlgorithm CreateDigest ()
 	{
-		return HashAlgorithm.Create (_DigestAlgorithm);
+		if (_DigestAlgorithm == null)
+			throw new ArgumentNullException ("DigestAlgorithm");
+		return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
 	}
 
-	[MonoTODO]
 	public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
 	{
 		if (_FormatterAlgorithm == null)
@@ -123,21 +95,35 @@ public class SignatureDescription {
 
 		// this should throw the InvalidCastException if we have an invalid class
 		// (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
-		AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CreateFromName (_FormatterAlgorithm);
-		if (fmt == null)
-			throw new InvalidCastException ("FormatterAlgorithm");
-		fmt.SetKey (key);
+		AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName (_FormatterAlgorithm);
 
-		throw new NullReferenceException ("why?");
+		if (_KeyAlgorithm == null)
+			throw new NullReferenceException ("KeyAlgorithm");
 
-		// We must make a choice of the Formatter based on
-		// the FormatterAlgorithm property (factory like CryptoConfig ?)
-		// There are only 2 SignatureFormatter based on the
-		// key algorithm (DSA or RSA) - but how does the 
-		// KeyAlgorithm property string really looks like ?
+		fmt.SetKey (key);
+		return fmt;
 	}
 	
 } // SignatureDescription
+
+internal class DSASignatureDescription : SignatureDescription {
+	public DSASignatureDescription () 
+	{
+		DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
+		DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
+		FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";		
+		KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";		
+	}
+}
+
+internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
+	public RSAPKCS1SHA1SignatureDescription () 
+	{
+		DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
+		DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
+		FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";		
+		KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";		
+	}
+}
 	
 } // System.Security.Cryptography
-