Finalize the implementation of SignedXml.ComputeSignature(). (#4452)
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / SignedXml.cs
index 05c2b091d98b229262f43bc1c25577656f0009a2..2ebec9809470e239cd1a6902fa06ead25ef57bf5 100644 (file)
@@ -624,34 +624,36 @@ namespace System.Security.Cryptography.Xml {
 
                public void ComputeSignature () 
                {
-                       if (key != null) {
-                               if (m_signature.SignedInfo.SignatureMethod == null)
-                                       // required before hashing
-                                       m_signature.SignedInfo.SignatureMethod = key.SignatureAlgorithm;
-                               else if (m_signature.SignedInfo.SignatureMethod != key.SignatureAlgorithm)
-                                       throw new CryptographicException ("Specified SignatureAlgorithm is not supported by the signing key.");
-                               DigestReferences ();
-
-                               AsymmetricSignatureFormatter signer = null;
-                               // in need for a CryptoConfig factory
-                               if (key is DSA)
-                                       signer = new DSASignatureFormatter (key);
-                               else if (key is RSA) 
-                                       signer = new RSAPKCS1SignatureFormatter (key);
-
-                               if (signer != null) {
-                                       SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName (m_signature.SignedInfo.SignatureMethod);
-
-                                       HashAlgorithm hash = GetHash (sd.DigestAlgorithm, false);
-                                       // get the hash of the C14N SignedInfo element
-                                       byte[] digest = hash.ComputeHash (SignedInfoTransformed ());
-
-                                       signer.SetHashAlgorithm ("SHA1");
-                                       m_signature.SignatureValue = signer.CreateSignature (digest);
+                       DigestReferences ();
+
+                       if (key == null)
+                               throw new CryptographicException (SR.Cryptography_Xml_LoadKeyFailed);
+
+                       // Check the signature algorithm associated with the key so that we can accordingly set the signature method
+                       if (SignedInfo.SignatureMethod == null) {
+                               if (key is DSA) {
+                                       SignedInfo.SignatureMethod = XmlDsigDSAUrl;
+                               } else if (key is RSA) {
+                                       // Default to RSA-SHA1
+                                       SignedInfo.SignatureMethod = XmlDsigRSASHA1Url;
+                               } else {
+                                       throw new CryptographicException (SR.Cryptography_Xml_CreatedKeyFailed);
                                }
                        }
-                       else
-                               throw new CryptographicException ("signing key is not specified");
+
+                       // See if there is a signature description class defined in the Config file
+                       SignatureDescription signatureDescription = CryptoConfig.CreateFromName (SignedInfo.SignatureMethod) as SignatureDescription;
+                       if (signatureDescription == null)
+                               throw new CryptographicException (SR.Cryptography_Xml_SignatureDescriptionNotCreated);
+
+                       HashAlgorithm hashAlg = signatureDescription.CreateDigest ();
+                       if (hashAlg == null)
+                               throw new CryptographicException (SR.Cryptography_Xml_CreateHashAlgorithmFailed);
+
+                       byte[] hashvalue = hashAlg.ComputeHash (SignedInfoTransformed ());
+                       AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter (key);
+
+                       m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature (hashAlg);
                }
 
                public void ComputeSignature (KeyedHashAlgorithm macAlg)