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