**** Merged from MCS ****
[mono-project.git] / mcs / tools / security / chktrust.cs
blobb58f346816b228279731f8f21917e21de933ce46
1 //
2 // ChkTrust.cs: chktrust clone tool
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using System;
11 using System.IO;
12 using System.Reflection;
13 using System.Security.Cryptography;
15 using Mono.Security.Authenticode;
17 [assembly: AssemblyTitle ("Mono CheckTrust")]
18 [assembly: AssemblyDescription ("Verify if an PE executable has a valid Authenticode(tm) signature")]
20 namespace Mono.Tools {
22 class CheckTrust {
24 static private void Header ()
26 Assembly a = Assembly.GetExecutingAssembly ();
27 AssemblyName an = a.GetName ();
29 object [] att = a.GetCustomAttributes (typeof (AssemblyTitleAttribute), false);
30 string title = ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : "Mono ChkTrust");
32 att = a.GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);
33 string copyright = ((att.Length > 0) ? ((AssemblyCopyrightAttribute) att [0]).Copyright : "");
35 Console.WriteLine ("{0} {1}", title, an.Version.ToString ());
36 Console.WriteLine ("{0}{1}", copyright, Environment.NewLine);
39 static private void Help ()
41 Console.WriteLine ("Usage: chktrust [options] filename{0}", Environment.NewLine);
42 Console.WriteLine ("\t-q\tquiet mode (no gui)");
43 Console.WriteLine ("\t-v\tverbose mode (display status for every steps)");
44 Console.WriteLine ("\t-?\thelp (display this help message)");
47 // static methods
48 static public int Check (string fileName, bool quiet, bool verbose)
50 AuthenticodeDeformatter a = new AuthenticodeDeformatter (fileName);
52 // debug
53 /* FileStream fs = File.Open (fileName + ".sig", FileMode.Create, FileAccess.Write);
54 fs.Write (a.Signature, 0, a.Signature.Length);
55 fs.Close ();*/
57 // get something shorter to display
58 fileName = Path.GetFileName (fileName);
60 if (verbose) {
61 Console.WriteLine ("Verifying file {0} for Authenticode(tm) signatures...{1}", fileName, Environment.NewLine);
64 if (a.Timestamp == DateTime.MinValue) {
65 // signature only valid if the certificate is valid
66 Console.WriteLine ("WARNING! {0} is not timestamped!", fileName);
68 else if (verbose) {
69 Console.WriteLine ("INFO! {0} was timestamped on {1}", fileName, a.Timestamp);
72 if (!a.IsTrusted ()) {
73 string msg = null;
74 // FAILURES
75 switch (a.Reason) {
76 case 1:
77 msg = "doesn't contain a digital signature";
78 break;
79 case 2:
80 msg = "digital signature is invalid";
81 break;
82 case 3:
83 msg = "countersignature (timestamp) is invalid";
84 break;
85 case 4:
86 msg = "timestamp is outside certificate validity";
87 break;
88 case 5:
89 msg = "use an unsupported hash algorithm. Verification is impossible";
90 break;
91 case 6:
92 msg = "signature can't be traced back to a trusted root";
93 break;
94 case 7:
95 msg = "couldn't find the certificate that signed the file";
96 break;
97 case 8:
98 msg = "certificate is expired and no timestamp is present";
99 break;
100 default:
101 msg = "unknown error";
102 break;
105 Console.WriteLine ("ERROR! {0} {1}!{2}", fileName, msg, Environment.NewLine);
106 return 1;
109 Console.WriteLine ("SUCCESS: {0} signature is valid{1}and can be traced back to a trusted root!{2}", fileName, Environment.NewLine, Environment.NewLine);
110 return 0;
113 [STAThread]
114 static int Main (string[] args)
116 bool verbose = false;
117 bool quiet = true; // always true as we don't show UI
118 bool help = false;
119 string fileName = null;
121 Header();
122 try {
123 for (int i=0; i < args.Length; i++) {
124 switch (args[i]) {
125 case "-q":
126 case "-quiet":
127 quiet = true;
128 break;
129 case "-v":
130 case "-verbose":
131 verbose = true;
132 break;
133 case "-h":
134 case "-help":
135 case "-?":
136 case "/?":
137 help = true;
138 break;
139 default:
140 fileName = args [i];
141 break;
145 if ((help) || (fileName == null))
146 Help ();
147 else
148 return Check (fileName, quiet, verbose);
151 catch (CryptographicException ce) {
152 Console.WriteLine ("WARNING: " + ce.Message);
153 Console.WriteLine ("ERROR: Trust evaluation is incomplete!");
155 catch (Exception e) {
156 Console.WriteLine ("ERROR: " + e.ToString ());
157 Help ();
159 Console.WriteLine ();
160 return 1;