2 // ChkTrust.cs: chktrust clone tool
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
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
{
24 static private void Header ()
26 Console
.WriteLine (new AssemblyInfo ().ToString ());
29 static private void Help ()
31 Console
.WriteLine ("Usage: chktrust [options] filename{0}", Environment
.NewLine
);
32 Console
.WriteLine ("\t-q\tquiet mode (no gui)");
33 Console
.WriteLine ("\t-v\tverbose mode (display status for every steps)");
34 Console
.WriteLine ("\t-?\thelp (display this help message)");
38 static public int Check (string fileName
, bool quiet
, bool verbose
)
40 AuthenticodeDeformatter a
= new AuthenticodeDeformatter (fileName
);
43 /* FileStream fs = File.Open (fileName + ".sig", FileMode.Create, FileAccess.Write);
44 fs.Write (a.Signature, 0, a.Signature.Length);
47 // get something shorter to display
48 fileName
= Path
.GetFileName (fileName
);
51 Console
.WriteLine ("Verifying file {0} for Authenticode(tm) signatures...{1}", fileName
, Environment
.NewLine
);
54 if (a
.Timestamp
== DateTime
.MinValue
) {
55 // signature only valid if the certificate is valid
56 Console
.WriteLine ("WARNING! {0} is not timestamped!", fileName
);
59 Console
.WriteLine ("INFO! {0} was timestamped on {1}", fileName
, a
.Timestamp
);
67 msg
= "doesn't contain a digital signature";
70 msg
= "digital signature is invalid";
73 msg
= "countersignature (timestamp) is invalid";
76 msg
= "timestamp is outside certificate validity";
79 msg
= "use an unsupported hash algorithm. Verification is impossible";
82 msg
= "signature can't be traced back to a trusted root";
85 msg
= "couldn't find the certificate that signed the file";
88 msg
= "certificate is expired and no timestamp is present";
91 msg
= "unknown error";
95 Console
.WriteLine ("ERROR! {0} {1}!{2}", fileName
, msg
, Environment
.NewLine
);
99 Console
.WriteLine ("SUCCESS: {0} signature is valid{1}and can be traced back to a trusted root!{2}", fileName
, Environment
.NewLine
, Environment
.NewLine
);
104 static int Main (string[] args
)
106 bool verbose
= false;
107 bool quiet
= true; // always true as we don't show UI
109 string fileName
= null;
113 for (int i
=0; i
< args
.Length
; i
++) {
135 if ((help
) || (fileName
== null))
138 return Check (fileName
, quiet
, verbose
);
141 catch (CryptographicException ce
) {
142 Console
.WriteLine ("WARNING: " + ce
.Message
);
143 Console
.WriteLine ("ERROR: Trust evaluation is incomplete!");
145 catch (Exception e
) {
146 Console
.WriteLine ("ERROR: " + e
.ToString ());
149 Console
.WriteLine ();