2005-12-02 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / tools / security / secutil.cs
1 //
2 // SecUtil.cs: secutil clone tool
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Text;
13 using System.Reflection;
14 using System.Security.Cryptography.X509Certificates;
15
16 [assembly: AssemblyTitle("Mono SecUtil")]
17 [assembly: AssemblyDescription("Extract strongname and X509 certificates from assemblies.")]
18
19 namespace Mono.Tools {
20
21 class SecUtil {
22
23         static private bool hexDisplay;
24         static private bool vbMode;
25         static private string error;
26
27         static private void WriteArray (byte[] array) 
28         {
29                 StringBuilder sb = new StringBuilder ();
30                 if (hexDisplay) {
31                         sb.Append ("0x");
32                         for (int i=0; i < array.Length; i++) 
33                                 sb.Append (array [i].ToString ("X2"));
34                 }
35                 else {
36                         sb.Append ((vbMode ? "( " : "{ "));
37                         for (int i=0; i < array.Length; i++) {
38                                 sb.Append (array [i]);
39                                 if (i != array.Length-1)
40                                         sb.Append (", ");
41                         }
42                         sb.Append ((vbMode ? " )" : " }"));
43                 }
44                 Console.WriteLine (sb.ToString ());
45         }
46
47         static private void StrongName (string fileName) 
48         {
49                 AssemblyName an = AssemblyName.GetAssemblyName (fileName);
50                 Console.WriteLine ("PublicKey =");
51                 WriteArray (an.GetPublicKey ());
52                 Console.WriteLine ("Name =");
53                 Console.WriteLine (an.Name);
54                 Console.WriteLine ("Version =");
55                 Console.WriteLine (an.Version.ToString ());
56         }
57
58         static private void Certificate (string fileName) 
59         {
60                 X509Certificate x509 = X509Certificate.CreateFromSignedFile (fileName);
61                 if (x509 == null)
62                         error = "Error: Specified file isn't signed";
63                 else {
64                         Console.WriteLine ("X.509 Certificate =");
65                         WriteArray (x509.GetRawCertData ());
66                 }
67         }
68
69         static private void Header () 
70         {
71                 Console.WriteLine (new AssemblyInfo ().ToString ());
72         }
73
74         static private void Help () 
75         {
76                 Console.WriteLine ("Usage: secutil [options] [filename]{0}", Environment.NewLine);
77                 Console.WriteLine ("secutil -s assembly");
78                 Console.WriteLine ("secutil -strongname assembly");
79                 Console.WriteLine ("\tShow strongname informations about the assembly{0}", Environment.NewLine);
80                 Console.WriteLine ("secutil -x assembly");
81                 Console.WriteLine ("secutil -x509certificate assembly");
82                 Console.WriteLine ("\tShow the X509 Authenticode certificate for the assembly{0}", Environment.NewLine);
83                 Console.WriteLine ("secutil -hex");
84                 Console.WriteLine ("\tShow data in hexadecimal{0}", Environment.NewLine);
85                 Console.WriteLine ("secutil -a");
86                 Console.WriteLine ("secutil -array");
87                 Console.WriteLine ("\tShow data in a decimal array (default){0}", Environment.NewLine);
88                 Console.WriteLine ("secutil -v");
89                 Console.WriteLine ("secutil -vbmode");
90                 Console.WriteLine ("\tShow data in a VisualBasic friendly format{0}", Environment.NewLine);
91                 Console.WriteLine ("secutil -c");
92                 Console.WriteLine ("secutil -cmode");
93                 Console.WriteLine ("\tShow data in a C/C++/C# friendly format (default){0}", Environment.NewLine);
94                 Console.WriteLine ("secutil -h");
95                 Console.WriteLine ("secutil -help");
96                 Console.WriteLine ("secutil -?");
97                 Console.WriteLine ("secutil /?");
98                 Console.WriteLine ("\tShow help about this tool{0}", Environment.NewLine);
99         }
100
101         [STAThread]
102         static void Main (string[] args)
103         {
104                 bool sn = false;
105                 bool cert = false;
106                 bool help = false;
107                 string fileName = null;
108
109                 Header();
110
111                 try {
112                         for (int i=0; i < args.Length; i++) {
113                                 switch (args[i]) {
114                                 case "-s":
115                                 case "-strongname":
116                                         sn = true;
117                                         fileName = args[i+1];
118                                         break;
119                                 case "-x":
120                                 case "-x509certificate":
121                                         cert = true;
122                                         fileName = args[i+1];
123                                         break;
124                                 case "-hex":
125                                         hexDisplay = true;
126                                         break;
127                                 case "-a":
128                                 case "-array":
129                                         hexDisplay = false;
130                                         break;
131                                 case "-v":
132                                 case "-vbmode":
133                                         vbMode = true;
134                                         break;
135                                 case "-c":
136                                 case "-cmode":
137                                         vbMode = false;
138                                         break;
139                                 case "-h":
140                                 case "-help":
141                                 case "-?":
142                                 case "/?":
143                                         help = true;
144                                         break;
145                                 }
146                         }
147
148                         if (help)
149                                 Help ();
150                         if (sn)
151                                 StrongName (fileName);
152                         else if (cert)
153                                 Certificate (fileName);
154                         else
155                                 Help ();
156
157                         Console.WriteLine ((error == null) ? "Success" : error);
158                 }
159                 catch (Exception e) {
160                         Console.WriteLine ("Error: " + e.ToString ());
161                 }
162         }
163 }
164
165 }