1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
//
// SignCode.cs: secutil clone tool
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
//
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Mono.Security.Authenticode;
using Mono.Security.X509;
[assembly: AssemblyTitle("Mono SignCode")]
[assembly: AssemblyDescription("Sign assemblies and PE files using Authenticode(tm).")]
namespace Mono.Tools {
class SignCode {
static private void Header ()
{
Console.WriteLine (new AssemblyInfo ().ToString ());
}
static private void Help ()
{
Console.WriteLine ("Usage: signcode [options] filename{0}", Environment.NewLine);
Console.WriteLine ("\t-spc spc\tSoftware Publisher Certificate file");
Console.WriteLine ("\t-v pvk\t\tPrivate Key file");
Console.WriteLine ("\t-a md5 | sha1\tHash Algorithm (default: MD5)");
Console.WriteLine ("\t-$ indivisual | commercial\tSignature type");
Console.WriteLine ("\t-n description\tDescription for the signed file");
Console.WriteLine ("\t-i url\tURL for the signed file");
Console.WriteLine ("Timestamp options");
Console.WriteLine ("\t-t url\tTimestamp service http URL");
Console.WriteLine ("\t-tr #\tNumber of retries for timestamp");
Console.WriteLine ("\t-tw #\tDelay between retries");
Console.WriteLine ("\t-x\tOnly timestamp (no signature)");
Console.WriteLine ("CSP options");
Console.WriteLine ("\t-k name\tKey Container Name");
Console.WriteLine ("\t-p name\tProvider Name");
Console.WriteLine ("\t-y #\tProvider Type");
Console.WriteLine ("\t-ky [signature|exchange|#]\tKey Type");
Console.WriteLine ("\t-r [localMachine|currentUser]\tKey Location");
}
static private RSA GetPrivateKey (string keyfile, CspParameters csp)
{
RSA rsa = null;
if (keyfile != null) {
if (!File.Exists (keyfile)) {
Console.WriteLine ("Couldn't find '{0}' file.", keyfile);
return null;
}
PrivateKey pvk = PrivateKey.CreateFromFile (keyfile);
if (pvk.Encrypted) {
Console.WriteLine ("Enter password for {0}: ", keyfile);
string password = Console.ReadLine ();
pvk = PrivateKey.CreateFromFile (keyfile, password);
if (pvk.RSA == null)
Console.WriteLine ("Invalid password!");
}
rsa = pvk.RSA;
}
else {
rsa = new RSACryptoServiceProvider (csp);
}
return rsa;
}
static private X509CertificateCollection GetCertificates (string spcfile)
{
if (spcfile == null) {
Console.WriteLine ("Missing SPC (certificate) file.");
return null;
}
if (!File.Exists (spcfile)) {
Console.WriteLine ("Couldn't find '{0}' file.", spcfile);
return null;
}
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (spcfile);
return spc.Certificates;
}
[STAThread]
static int Main(string[] args)
{
Header ();
if (args.Length < 1) {
Help ();
return 1;
}
CspParameters csp = new CspParameters ();
string pvkFilename = null;
string spcFilename = null;
int timestampRetry = 1;
int timestampDelay = 0;
bool sign = true;
// to be signed
string tbsFilename = args [args.Length - 1];
AuthenticodeFormatter af = new AuthenticodeFormatter ();
int i = 0;
while (i < args.Length - 1) {
switch (args[i++]) {
case "-spc":
spcFilename = args [i++];
break;
case "-v":
pvkFilename = args [i++];
break;
case "-a":
af.Hash = args [i++];
break;
case "-$":
string auth = args [i++].ToLower ();
switch (auth) {
case "individual":
af.Authority = Authority.Commercial;
break;
case "commercial":
af.Authority = Authority.Individual;
break;
default:
Console.WriteLine ("Unknown authority {0}", auth);
return 1;
}
break;
case "-n":
af.Description = args [i++];
break;
case "-i":
af.Url = new Uri (args [i++]);
break;
// timestamp options
case "-t":
af.TimestampUrl = new Uri (args [i++]);
break;
case "-tr":
timestampRetry = Convert.ToInt32 (args [i++]);
break;
case "-tw":
timestampDelay = Convert.ToInt32 (args [i++]) * 1000;
break;
case "-x":
// only timestamp
sign = false;
break;
// CSP provider options
case "-k":
csp.KeyContainerName = args [i++];
break;
case "-p":
csp.ProviderName = args [i++];
break;
case "-y":
csp.ProviderType = Convert.ToInt32 (args [i++]);
break;
case "-ky":
string key = args [i++];
switch (key) {
case "signature":
csp.KeyNumber = 0;
break;
case "exchange":
csp.KeyNumber = 0;
break;
default:
csp.KeyNumber = Convert.ToInt32 (key);
break;
}
break;
case "-r":
string location = args [i++];
switch (location) {
case "localMachine":
csp.Flags = CspProviderFlags.UseMachineKeyStore;
break;
case "currentUser":
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
break;
default:
Console.WriteLine ("Unknown location {0}", location);
return 1;
}
break;
// unsupported options
case "-j":
case "-jp":
Console.WriteLine ("Unsupported option {0}", args[i-1]);
return 1;
// other options
case "-?":
Help ();
return 0;
}
}
// no need to continue if we can't find the assembly
// to be signed (and/or timestamped)
if (!File.Exists (tbsFilename)) {
Console.WriteLine ("Couldn't find {0}.", tbsFilename);
return 1;
}
if (sign) {
RSA rsa = GetPrivateKey (pvkFilename, csp);
if (rsa == null) {
Console.WriteLine ("No private key available to sign the assembly.");
return 1;
}
af.RSA = rsa;
X509CertificateCollection certs = GetCertificates (spcFilename);
if ((certs == null) || (certs.Count == 0)) {
Console.WriteLine ("No certificates available to sign the assembly.");
return 1;
}
af.Certificates.AddRange (certs);
if (!af.Sign (tbsFilename)) {
Console.WriteLine ("Couldn't sign file '{0}'.", tbsFilename);
return 1;
}
} else if (af.TimestampUrl != null) {
bool ts = false;
// only timestamp an already signed file
for (int j = 0; j < timestampRetry && !ts; j++) {
ts = af.Timestamp (tbsFilename);
// wait (unless it's the last try) and retry
if (!ts && (j < timestampRetry - 1)) {
Console.WriteLine ("Couldn't timestamp file '{0}', will retry in {1} ms", tbsFilename, timestampDelay);
Thread.Sleep (timestampDelay);
}
}
if (!ts) {
Console.WriteLine ("Couldn't timestamp file '{0}' after {1} retries.", tbsFilename, timestampRetry);
return 1;
}
} else {
Help ();
return 1;
}
Console.WriteLine ("Success");
return 0;
}
}
}
|