2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / security / secutil.cs
blobb68dffebf295b11be9481a691db2bf329c77e9a5
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 //
11 using System;
12 using System.Text;
13 using System.Reflection;
14 using System.Security.Cryptography.X509Certificates;
16 [assembly: AssemblyTitle("Mono SecUtil")]
17 [assembly: AssemblyDescription("Extract strongname and X509 certificates from assemblies.")]
19 namespace Mono.Tools {
21 class SecUtil {
23 static private bool hexDisplay;
24 static private bool vbMode;
25 static private string error;
27 static private void WriteArray (byte[] array)
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"));
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 (", ");
42 sb.Append ((vbMode ? " )" : " }"));
44 Console.WriteLine (sb.ToString ());
47 static private void StrongName (string fileName)
49 AssemblyName an = AssemblyName.GetAssemblyName (fileName);
50 byte[] key = an.GetPublicKey ();
52 if (key == null) {
53 error = "Error: Assembly has no strong name";
54 } else {
55 Console.WriteLine ("PublicKey =");
56 WriteArray (key);
57 Console.WriteLine ("Name =");
58 Console.WriteLine (an.Name);
59 Console.WriteLine ("Version =");
60 Console.WriteLine (an.Version.ToString ());
64 static private void Certificate (string fileName)
66 X509Certificate x509 = X509Certificate.CreateFromSignedFile (fileName);
67 if (x509 == null)
68 error = "Error: Specified file isn't signed";
69 else {
70 Console.WriteLine ("X.509 Certificate =");
71 WriteArray (x509.GetRawCertData ());
75 static private void Header ()
77 Console.WriteLine (new AssemblyInfo ().ToString ());
80 static private void Help ()
82 Console.WriteLine ("Usage: secutil [options] [filename]{0}", Environment.NewLine);
83 Console.WriteLine ("secutil -s assembly");
84 Console.WriteLine ("secutil -strongname assembly");
85 Console.WriteLine ("\tShow strongname informations about the assembly{0}", Environment.NewLine);
86 Console.WriteLine ("secutil -x assembly");
87 Console.WriteLine ("secutil -x509certificate assembly");
88 Console.WriteLine ("\tShow the X509 Authenticode certificate for the assembly{0}", Environment.NewLine);
89 Console.WriteLine ("secutil -hex");
90 Console.WriteLine ("\tShow data in hexadecimal{0}", Environment.NewLine);
91 Console.WriteLine ("secutil -a");
92 Console.WriteLine ("secutil -array");
93 Console.WriteLine ("\tShow data in a decimal array (default){0}", Environment.NewLine);
94 Console.WriteLine ("secutil -v");
95 Console.WriteLine ("secutil -vbmode");
96 Console.WriteLine ("\tShow data in a VisualBasic friendly format{0}", Environment.NewLine);
97 Console.WriteLine ("secutil -c");
98 Console.WriteLine ("secutil -cmode");
99 Console.WriteLine ("\tShow data in a C/C++/C# friendly format (default){0}", Environment.NewLine);
100 Console.WriteLine ("secutil -h");
101 Console.WriteLine ("secutil -help");
102 Console.WriteLine ("secutil -?");
103 Console.WriteLine ("secutil /?");
104 Console.WriteLine ("\tShow help about this tool{0}", Environment.NewLine);
107 [STAThread]
108 static void Main (string[] args)
110 bool sn = false;
111 bool cert = false;
112 bool help = false;
113 string fileName = null;
115 Header();
117 try {
118 for (int i=0; i < args.Length - 1; i++) {
119 switch (args[i]) {
120 case "-s":
121 case "-strongname":
122 sn = true;
123 fileName = args[i+1];
124 break;
125 case "-x":
126 case "-x509certificate":
127 cert = true;
128 fileName = args[i+1];
129 break;
130 case "-hex":
131 hexDisplay = true;
132 break;
133 case "-a":
134 case "-array":
135 hexDisplay = false;
136 break;
137 case "-v":
138 case "-vbmode":
139 vbMode = true;
140 break;
141 case "-c":
142 case "-cmode":
143 vbMode = false;
144 break;
145 case "-h":
146 case "-help":
147 case "-?":
148 case "/?":
149 help = true;
150 break;
154 if (help)
155 Help ();
156 if (sn)
157 StrongName (fileName);
158 else if (cert)
159 Certificate (fileName);
160 else
161 Help ();
163 Console.WriteLine ((error == null) ? "Success" : error);
165 catch (Exception e) {
166 Console.WriteLine ("Error: " + e.ToString ());