[System] Fixes failing tests on mobile
[mono.git] / mcs / tools / security / sn.cs
index 9031d1a1c557a41dede341e7a6a66555e5865e2a..8dac86944b2232d8a52fc137077813b46b8d7d38 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2006,2008 Novell, Inc (http://www.novell.com)
 //
 
 using System;
@@ -126,7 +126,7 @@ namespace Mono.Tools {
                                return new StrongName (data).RSA;
                        }
                        catch {
-                               if (data [0] != 0x30)
+                               if (data.Length == 0 || data [0] != 0x30)
                                        throw;
                                // this could be a PFX file
                                Console.Write ("Enter password for private key (will be visible when typed): ");
@@ -157,7 +157,7 @@ namespace Mono.Tools {
                        return false;
                }
 #endif
-               static bool ReSign (string assemblyName, RSA key) 
+               static bool ReSign (string assemblyName, RSA key, bool quiet
                {
                        // this doesn't load the assembly (well it unloads it ;)
                        // http://weblogs.asp.net/nunitaddin/posts/9991.aspx
@@ -188,16 +188,18 @@ namespace Mono.Tools {
 
                        if (same) {
                                bool signed = sign.Sign (assemblyName);
-                               Console.WriteLine (signed ? "Assembly {0} signed." : "Couldn't sign the assembly {0}.", 
-                                                  assemblyName);
+                               if (!quiet || !signed) {
+                                       Console.WriteLine (signed ? "Assembly {0} signed." : "Couldn't sign the assembly {0}.", 
+                                                          assemblyName);
+                               }
                                return signed;
                        }
                        
-                       Console.WriteLine ("Couldn't sign the assembly {0} with this key pair.", assemblyName);
+                       Console.WriteLine ("Couldn't sign the assembly {0} with this key pair. Public key of assembly did not match signing public key.", assemblyName);
                        return false;
                }
 
-               static int Verify (string assemblyName, bool forceVerification) 
+               static int Verify (string assemblyName, bool forceVerification, bool quiet
                {
                        // this doesn't load the assembly (well it unloads it ;)
                        // http://weblogs.asp.net/nunitaddin/posts/9991.aspx
@@ -228,11 +230,12 @@ namespace Mono.Tools {
                                RSA rsa = CryptoConvert.FromCapiPublicKeyBlob (publicKey, 12);
                                StrongName sn = new StrongName (rsa);
                                if (sn.Verify (assemblyName)) {
-                                       Console.WriteLine ("Assembly {0} is strongnamed.", assemblyName);
+                                       if (!quiet)
+                                               Console.WriteLine ("Assembly {0} is strongnamed.", assemblyName);
                                        return 0;
                                }
                                else {
-                                       Console.WriteLine ("Assembly {0} isn't strongnamed", assemblyName);
+                                       Console.WriteLine ("Assembly {0} is delay-signed but not strongnamed", assemblyName);
                                        return 1;
                                }
                        }
@@ -290,9 +293,9 @@ namespace Mono.Tools {
                                        Console.WriteLine (" -k keypair.snk{0}\tCreate a new keypair in the specified file", Environment.NewLine);
                                        Console.WriteLine (" -R assembly keypair.snk{0}\tResign the assembly with the specified StrongName key file", Environment.NewLine);
                                        Console.WriteLine (" -Rc assembly container{0}\tResign the assembly with the specified CSP container", Environment.NewLine);
-                                       Console.WriteLine (" -t file{0}\tShow the public key from the specified file", Environment.NewLine);
+                                       Console.WriteLine (" -t file{0}\tShow the public key token from the specified file", Environment.NewLine);
                                        Console.WriteLine (" -tp file{0}\tShow the public key and pk token from the specified file", Environment.NewLine);
-                                       Console.WriteLine (" -T assembly{0}\tShow the public key from the specified assembly", Environment.NewLine);
+                                       Console.WriteLine (" -T assembly{0}\tShow the public key token from the specified assembly", Environment.NewLine);
                                        Console.WriteLine (" -Tp assembly{0}\tShow the public key and pk token from the specified assembly", Environment.NewLine);
                                        Console.WriteLine (" -v assembly{0}\tVerify the specified assembly signature", Environment.NewLine);
                                        Console.WriteLine (" -vf assembly{0}\tVerify the specified assembly signature (even if disabled).", Environment.NewLine);
@@ -416,14 +419,14 @@ namespace Mono.Tools {
                                        break;
                                case "-R":
                                        string filename = args [i++];
-                                       if (! ReSign (filename, GetKeyFromFile (args [i])))
+                                       if (! ReSign (filename, GetKeyFromFile (args [i]), quiet))
                                                return 1;
                                        break;
                                case "-Rc":
                                        filename = args [i++];
                                        csp.KeyContainerName = args [i];
                                        rsa = new RSACryptoServiceProvider (csp);
-                                       if (! ReSign (filename, rsa))
+                                       if (! ReSign (filename, rsa, quiet))
                                                return 1;
                                        break;
                                case "-t":
@@ -463,10 +466,10 @@ namespace Mono.Tools {
                                        break;
                                case "-v":
                                        filename = args [i++];
-                                       return Verify (filename, false);
+                                       return Verify (filename, false, quiet);
                                case "-vf":
                                        filename = args [i++];
-                                       return Verify (filename, true); // force verification
+                                       return Verify (filename, true, quiet);  // force verification
                                case "-Vl":
                                        Console.WriteLine (new StrongNameManager ().ToString ());
                                        break;
@@ -507,6 +510,9 @@ namespace Mono.Tools {
                                Console.WriteLine ("ERROR: Invalid number of parameters.{0}", Environment.NewLine);
                                Help (null);
                        }
+                       catch (CryptographicException ce) {
+                               Console.WriteLine ("ERROR: {0}", ce.Message);
+                       }
                        catch (Exception e) {
                                Console.WriteLine ("ERROR: Unknown error during processing: {0}", e);
                        }