New tests.
[mono.git] / mcs / class / corlib / Mono.Security / StrongName.cs
old mode 100755 (executable)
new mode 100644 (file)
index f1ae174..2905a03
@@ -8,6 +8,27 @@
 // (C) 2004 Novell (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
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Configuration.Assemblies;
 using System.Globalization;
@@ -93,6 +114,11 @@ namespace Mono.Security {
                {
                }
 
+               public StrongName (int keySize)
+               {
+                       rsa = new RSAManaged (keySize);
+               }
+
                public StrongName (byte[] data)
                {
                        if (data == null)
@@ -134,7 +160,7 @@ namespace Mono.Security {
                        get {
                                if (rsa == null)
                                        return false;
-#if INSIDE_CORLIB
+#if INSIDE_CORLIB && !MOONLIGHT
                                // the easy way
                                if (RSA is RSACryptoServiceProvider) {
                                        // available as internal for corlib
@@ -174,8 +200,9 @@ namespace Mono.Security {
                public byte[] PublicKey {
                        get { 
                                if (publicKey == null) {
-                                       byte[] keyPair = CryptoConvert.ToCapiKeyBlob (rsa, false); 
-                                       publicKey = new byte [32 + 128]; // always 1024 bits
+                                       byte[] keyPair = CryptoConvert.ToCapiKeyBlob (rsa, false);
+                                       // since 2.0 public keys can vary from 384 to 16384 bits
+                                       publicKey = new byte [32 + (rsa.KeySize >> 3)];
 
                                        // The first 12 bytes are documented at:
                                        // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
@@ -212,7 +239,7 @@ namespace Mono.Security {
                                        byte[] publicKey = PublicKey;
                                        if (publicKey == null)
                                                return null;
-                                       HashAlgorithm ha = SHA1.Create (TokenAlgorithm);
+                                       HashAlgorithm ha = HashAlgorithm.Create (TokenAlgorithm);
                                        byte[] hash = ha.ComputeHash (publicKey);
                                        // we need the last 8 bytes in reverse order
                                        keyToken = new byte [8];
@@ -403,11 +430,17 @@ namespace Mono.Security {
 
                public bool Verify (string fileName) 
                {
-                       StrongNameSignature sn;
+                       bool result = false;
                        using (FileStream fs = File.OpenRead (fileName)) {
-                               sn = StrongHash (fs, StrongNameOptions.Signature);
+                               result = Verify (fs);
                                fs.Close ();
                        }
+                       return result;
+               }
+
+               public bool Verify (Stream stream)
+               {
+                       StrongNameSignature sn = StrongHash (stream, StrongNameOptions.Signature);
                        if (sn.Hash == null) {
                                return false;
                        }
@@ -425,9 +458,26 @@ namespace Mono.Security {
                }
 
 #if INSIDE_CORLIB
+               static object lockObject = new object ();
+               static bool initialized = false;
+
                // We don't want a dependency on StrongNameManager in Mono.Security.dll
                static public bool IsAssemblyStrongnamed (string assemblyName) 
                {
+                       if (!initialized) {
+                               lock (lockObject) {
+                                       if (!initialized) {
+#if NET_2_1
+                                               // Moonlight cannot depend on machine.config
+#else
+                                               string config = Environment.GetMachineConfigPath ();
+                                               StrongNameManager.LoadConfig (config);
+#endif
+                                               initialized = true;
+                                       }
+                               }
+                       }
+
                        try {
                                // this doesn't load the assembly (well it unloads it ;)
                                // http://weblogs.asp.net/nunitaddin/posts/9991.aspx