2 // SecUtil.cs: secutil clone tool
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
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
{
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 ();
32 for (int i
=0; i
< array
.Length
; i
++)
33 sb
.Append (array
[i
].ToString ("X2"));
36 sb
.Append ((vbMode
? "( " : "{ "));
37 for (int i
=0; i
< array
.Length
; i
++) {
38 sb
.Append (array
[i
]);
39 if (i
!= array
.Length
-1)
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 ();
53 error
= "Error: Assembly has no strong name";
55 Console
.WriteLine ("PublicKey =");
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
);
68 error
= "Error: Specified file isn't signed";
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
);
108 static void Main (string[] args
)
113 string fileName
= null;
118 for (int i
=0; i
< args
.Length
- 1; i
++) {
123 fileName
= args
[i
+1];
126 case "-x509certificate":
128 fileName
= args
[i
+1];
157 StrongName (fileName
);
159 Certificate (fileName
);
163 Console
.WriteLine ((error
== null) ? "Success" : error
);
165 catch (Exception e
) {
166 Console
.WriteLine ("Error: " + e
.ToString ());