2 // SN.cs: sn 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;
17 using Mono.Security.Cryptography;
19 [assembly: AssemblyTitle("Mono StrongName")]
20 [assembly: AssemblyDescription("StrongName utility for signing assemblies")]
22 namespace Mono.Tools {
26 static private void Header ()
28 Assembly a = Assembly.GetExecutingAssembly ();
29 AssemblyName an = a.GetName ();
31 object [] att = a.GetCustomAttributes (typeof (AssemblyTitleAttribute), false);
32 string title = ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : "Mono StrongName");
34 att = a.GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);
35 string copyright = ((att.Length > 0) ? ((AssemblyCopyrightAttribute) att [0]).Copyright : "");
37 Console.WriteLine ("{0} {1}", title, an.Version.ToString ());
38 Console.WriteLine ("{0}{1}", copyright, Environment.NewLine);
41 static string defaultCSP = null;
43 static bool LoadConfig ()
49 static int SaveConfig ()
55 static byte[] ReadFromFile (string fileName)
58 FileStream fs = File.Open (fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
60 data = new byte [fs.Length];
61 fs.Read (data, 0, data.Length);
69 static void WriteToFile (string fileName, byte[] data)
71 FileStream fs = File.Open (fileName, FileMode.Create, FileAccess.Write);
73 fs.Write (data, 0, data.Length);
80 static void WriteCSVToFile (string fileName, byte[] data, string mask)
82 StreamWriter sw = File.CreateText (fileName);
84 for (int i=0; i < data.Length; i++) {
87 sw.Write (data [i].ToString (mask));
96 static string ToString (byte[] data)
98 StringBuilder sb = new StringBuilder ();
99 for (int i=0; i < data.Length; i++) {
100 if ((i % 39 == 0) && (data.Length > 39))
101 sb.Append (Environment.NewLine);
102 sb.Append (data [i].ToString ("x2"));
104 sb.Append (" !!! TOO LONG !!!");
108 return sb.ToString ();
111 static void Help (string details)
113 Console.WriteLine ("Usage: sn [-q | -quiet] options [parameters]{0}", Environment.NewLine);
114 Console.WriteLine (" -q | -quiet \tQuiet mode (minimal display){0}", Environment.NewLine);
117 Console.WriteLine ("Configuration options <1>");
118 Console.WriteLine (" -c provider{0}\tChange the default CSP provider", Environment.NewLine);
119 Console.WriteLine (" -m [y|n]{0}\tUse a machine [y] key container or user key container [n]", Environment.NewLine);
120 Console.WriteLine (" -Vl{0}\tList the verification options", Environment.NewLine);
121 Console.WriteLine (" -Vr assembly [userlist]{0}\tExempt the specified assembly from verification for the user list", Environment.NewLine);
122 Console.WriteLine (" -Vu assembly{0}\tRemove exemption entry for the specified assembly", Environment.NewLine);
123 Console.WriteLine (" -Vx{0}\tRemove all exemptions entries", Environment.NewLine);
126 Console.WriteLine ("CSP related options <2>");
127 Console.WriteLine (" -d container{0}\tDelete the specified key container", Environment.NewLine);
128 Console.WriteLine (" -i keypair.snk container{0}\tImport the keypair from a SNK file into a CSP container", Environment.NewLine);
129 Console.WriteLine (" -pc container public.key{0}\tExport the public key from a CSP container to the specified file", Environment.NewLine);
132 Console.WriteLine ("Convertion options");
133 Console.WriteLine (" -e assembly output.pub{0}\tExport the assembly public key to the specified file", Environment.NewLine);
134 Console.WriteLine (" -p keypair.snk output.pub{0}\tExport the public key from a SNK file to the specified file", Environment.NewLine);
135 Console.WriteLine (" -o input output.txt{0}\tConvert the input file to a CVS file (using decimal).", Environment.NewLine);
136 Console.WriteLine (" -oh input output.txt{0}\tConvert the input file to a CVS file (using hexadecimal).", Environment.NewLine);
139 Console.WriteLine ("StrongName signing options");
140 Console.WriteLine (" -D assembly1 assembly2{0}\tCompare assembly1 and assembly2 (without signatures) <1>", Environment.NewLine);
141 Console.WriteLine (" -k keypair.snk{0}\tCreate a new keypair in the specified file", Environment.NewLine);
142 Console.WriteLine (" -R assembly keypair.snk{0}\tResign the assembly with the specified StrongName key file", Environment.NewLine);
143 Console.WriteLine (" -Rc assembly container{0}\tResign the assembly with the specified CSP container", Environment.NewLine);
144 Console.WriteLine (" -t file{0}\tShow the public key from the specified file [1]", Environment.NewLine);
145 Console.WriteLine (" -tp file{0}\tShow the public key and pk token from the specified file <1>", Environment.NewLine);
146 Console.WriteLine (" -T assembly{0}\tShow the public key from the specified assembly", Environment.NewLine);
147 Console.WriteLine (" -Tp assembly{0}\tShow the public key and pk token from the specified assembly", Environment.NewLine);
148 Console.WriteLine (" -V assembly{0}\tVerify the specified assembly signature <1>", Environment.NewLine);
149 Console.WriteLine (" -Vf assembly{0}\tVerify the specified assembly signature (even if disabled) <1>.", Environment.NewLine);
152 Console.WriteLine ("Help options");
153 Console.WriteLine (" -? | -h \tShow this help screen about the tool");
154 Console.WriteLine (" -? | -h config \tConfiguration options (see strongname.xml)");
155 Console.WriteLine (" -? | -h csp \tCrypto Service Provider (CSP) related options");
156 Console.WriteLine (" -? | -h convert\tFormat convertion options");
157 Console.WriteLine (" -? | -h sn \tStrongName signing options");
160 Console.WriteLine ("{0}<1> Currently not implemented in the tool", Environment.NewLine);
161 Console.WriteLine ("<2> Implemented in the tool but not in Mono{0}", Environment.NewLine);
165 static int Main (string[] args)
167 if (args.Length < 1) {
174 string param = args [i];
175 bool quiet = ((param == "-quiet") || (param == "-q"));
181 bool config = LoadConfig ();
183 StrongName sn = null;
184 AssemblyName an = null;
185 RSACryptoServiceProvider rsa = null;
186 CspParameters csp = new CspParameters ();
187 csp.ProviderName = defaultCSP;
189 switch (args [i++]) {
191 // Change global CSP provider options
192 defaultCSP = args [i];
193 return SaveConfig ();
195 // Delete specified key container
196 csp.KeyContainerName = args [i];
197 rsa = new RSACryptoServiceProvider (csp);
198 rsa.PersistKeyInCsp = false;
200 Console.WriteLine ("Keypair in container {0} has been deleted", args [i]);
203 Console.WriteLine ("Unimplemented option");
206 // Export public key from assembly
207 an = AssemblyName.GetAssemblyName (args [i++]);
208 WriteToFile (args[i], an.GetPublicKey ());
210 Console.WriteLine ("Public Key extracted to file {0}", args [i]);
213 // import keypair from SNK to container
214 sn = new StrongName (ReadFromFile (args [i++]));
215 csp.KeyContainerName = args [i];
216 rsa = new RSACryptoServiceProvider (csp);
217 rsa.ImportParameters (sn.RSA.ExportParameters (true));
220 // Create a new strong name key pair
221 // (a new RSA keypair automagically if none is present)
222 sn = new StrongName ();
223 WriteToFile (args[i], CryptoConvert.ToCapiKeyBlob (sn.RSA, true));
225 Console.WriteLine ("A new strong name keypair has been generated in {0}", args [i]);
228 byte[] infileD = ReadFromFile (args [i++]);
229 WriteCSVToFile (args [i], infileD, "D");
231 Console.WriteLine ("Output CVS file is {0} (decimal format)", args [i]);
234 byte[] infileX2 = ReadFromFile (args [i++]);
235 WriteCSVToFile (args [i], infileX2, "X2");
237 Console.WriteLine ("Output CVS file is {0} (hexadecimal format)", args [i]);
240 // Extract public key from SNK file
241 sn = new StrongName (ReadFromFile (args [i++]));
242 WriteToFile (args[i], CryptoConvert.ToCapiKeyBlob (sn.RSA, false));
244 Console.WriteLine ("Public Key extracted to file {0}", args [i]);
247 // Extract public key from container
248 csp.KeyContainerName = args [i++];
249 rsa = new RSACryptoServiceProvider (csp);
250 WriteToFile (args[i], CryptoConvert.ToCapiKeyBlob (rsa, false));
252 Console.WriteLine ("Public Key extracted to file {0}", args [i]);
255 Console.WriteLine ("Unimplemented option");
258 Console.WriteLine ("Unimplemented option");
261 // Show public key token from file
262 sn = new StrongName (ReadFromFile (args [i]));
263 // note: ignore quiet
264 Console.WriteLine ("Public Key Token: " + ToString (sn.PublicKeyToken), Environment.NewLine);
267 // Show public key and public key token from assembly
268 sn = new StrongName (ReadFromFile (args [i]));
269 // note: ignore quiet
270 Console.WriteLine ("Public Key:" + ToString (sn.PublicKey));
271 Console.WriteLine ("{0}Public Key Token: " + ToString (sn.PublicKeyToken), Environment.NewLine);
274 // Show public key token from assembly
275 an = AssemblyName.GetAssemblyName (args [i++]);
276 // note: ignore quiet
277 Console.WriteLine ("Public Key Token: " + ToString (an.GetPublicKeyToken ()));
280 // Show public key and public key token from assembly
281 an = AssemblyName.GetAssemblyName (args [i++]);
282 // note: ignore quiet
283 Console.WriteLine ("Public Key:" + ToString (an.GetPublicKey ()));
284 Console.WriteLine ("{0}Public Key Token: " + ToString (an.GetPublicKeyToken ()), Environment.NewLine);
287 Console.WriteLine ("Unimplemented option");
290 Console.WriteLine ("Unimplemented option");
293 Console.WriteLine ("Unimplemented option");
296 Console.WriteLine ("Unimplemented option");
299 Console.WriteLine ("Unimplemented option");
302 Console.WriteLine ("Unimplemented option");
306 Help ((i < args.Length) ? args [i] : null);
310 Console.WriteLine ("Unknown option {0}", args [i-1]);