2007-04-26 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / tools / security / signcode.cs
index 4b10875b28ae670786860264bad8161997a3973c..f6df11103bc8b14f4e96963e904b49e2ca4058b6 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
+// Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
 //
 
 using System;
@@ -62,15 +62,21 @@ namespace Mono.Tools {
                                        return null;
                                }
 
-                               PrivateKey pvk = PrivateKey.CreateFromFile (keyfile);
-                               if (pvk.Encrypted) {
+                               try {
+                                       PrivateKey pvk = PrivateKey.CreateFromFile (keyfile);
+                                       rsa = pvk.RSA;
+                               }
+                               catch (CryptographicException) {
                                        Console.WriteLine ("Enter password for {0}: ", keyfile);
                                        string password = Console.ReadLine ();
-                                       pvk = PrivateKey.CreateFromFile (keyfile, password);
-                                       if (pvk.RSA == null)
+                                       try {
+                                               PrivateKey pvk = PrivateKey.CreateFromFile (keyfile, password);
+                                               rsa = pvk.RSA;
+                                       }
+                                       catch (CryptographicException) {
                                                Console.WriteLine ("Invalid password!");
+                                       }
                                }
-                               rsa = pvk.RSA;
                        }
                        else {
                                rsa = new RSACryptoServiceProvider (csp);
@@ -106,7 +112,7 @@ namespace Mono.Tools {
                        CspParameters csp = new CspParameters ();
                        string pvkFilename = null;
                        string spcFilename = null;
-                       int timestampRetry = 0;
+                       int timestampRetry = 1;
                        int timestampDelay = 0;
                        bool sign = true;
 
@@ -116,7 +122,7 @@ namespace Mono.Tools {
                        AuthenticodeFormatter af = new AuthenticodeFormatter ();
 
                        int i = 0;
-                       while (i < args.Length) {
+                       while (i < args.Length - 1) {
                                switch (args[i++]) {
                                        case "-spc":
                                                spcFilename = args [i++];
@@ -131,10 +137,10 @@ namespace Mono.Tools {
                                                string auth = args [i++].ToLower ();
                                                switch (auth) {
                                                        case "individual":
-                                                               af.Authority = Authority.Commercial;
+                                                               af.Authority = Authority.Individual;
                                                                break;
                                                        case "commercial":
-                                                               af.Authority = Authority.Individual;
+                                                               af.Authority = Authority.Commercial;
                                                                break;
                                                        default:
                                                                Console.WriteLine ("Unknown authority {0}", auth);
@@ -204,6 +210,10 @@ namespace Mono.Tools {
                                        case "-jp":
                                                Console.WriteLine ("Unsupported option {0}", args[i-1]);
                                                return 1;
+                                       // other options
+                                       case "-?":
+                                               Help ();
+                                               return 0;
                                }
                        }
 
@@ -227,24 +237,33 @@ namespace Mono.Tools {
                                        Console.WriteLine ("No certificates available to sign the assembly.");
                                        return 1;
                                }
-                               af.Certificates.AddRange (certs);
-
-                               af.Sign (tbsFilename);
-                       }
-/* TODO
-                       if (af.TimestampURL != null) {
-                               for (int j=0; j < timestampRetry + 1; j++) {
-                                       if (!af.Timestamp (tbsFilename)) {
-                                               Thread.Sleep (timestampDelay);
-                                               continue;
-                                       }
-                                       break;
-                               }
-                       }*/
-                       // temp
-                       if ((timestampRetry > 0) || (timestampDelay > 0)) {
-                               Console.WriteLine ("Timestamp retries and delays aren't implemented.");
-                       }
+                               af.Certificates.AddRange (certs);\r
+\r
+                               if (!af.Sign (tbsFilename)) {\r
+                                       Console.WriteLine ("Couldn't sign file '{0}'.", tbsFilename);\r
+                                       return 1;\r
+                               }\r
+                       } else if (af.TimestampUrl != null) {\r
+                               bool ts = false;\r
+                               // only timestamp an already signed file\r
+                               for (int j = 0; j < timestampRetry && !ts; j++) {\r
+                                       ts = af.Timestamp (tbsFilename);\r
+                                       // wait (unless it's the last try) and retry\r
+                                       if (!ts && (j < timestampRetry - 1)) {\r
+                                               Console.WriteLine ("Couldn't timestamp file '{0}', will retry in {1} ms", tbsFilename, timestampDelay);\r
+                                               Thread.Sleep (timestampDelay);\r
+                                       }\r
+                               }\r
+                               if (!ts) {\r
+                                       Console.WriteLine ("Couldn't timestamp file '{0}' after {1} retries.", tbsFilename, timestampRetry);\r
+                                       return 1;\r
+                               }\r
+                       } else {\r
+                               Help ();\r
+                               return 1;\r
+                       }\r
+\r
+                       Console.WriteLine ("Success");
                        return 0;
                }
        }