2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / security / cert2spc.cs
blob0a1c199e13e71dd88eb490b1fa93201f7ea27d03
1 //
2 // Cert2Spc.cs: cert2spc clone tool
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
11 using System;
12 using System.IO;
13 using System.Reflection;
15 using Mono.Security.Authenticode;
16 using Mono.Security.X509;
18 [assembly: AssemblyTitle("Mono Cert2Spc")]
19 [assembly: AssemblyDescription("Transform a set of X.509 certificates and CRLs into an Authenticode(TM) \"Software Publisher Certificate\"")]
21 namespace Mono.Tools {
23 class Cert2Spc {
25 static private string error;
27 static private void Header ()
29 Console.WriteLine (new AssemblyInfo ().ToString ());
32 static private void Help ()
34 Console.WriteLine ("Usage: cert2spc certificate|crl [certificate|crl] [...] outputfile.spc{0}", Environment.NewLine);
37 // until we have real CRL support
38 static byte[] GetFile (string filename)
40 byte[] data = null;
41 using (FileStream fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
42 data = new byte [fs.Length];
43 fs.Read (data, 0, data.Length);
44 fs.Close ();
46 return data;
49 static int Process (string[] args)
51 int nargs = args.Length - 1;
52 if (nargs < 1) {
53 error = "At least one input and output files must be specified";
54 return 1;
57 string output = args [nargs];
58 SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate ();
60 for (int i=0; i < args.Length - 1; i++) {
61 switch (Path.GetExtension (args[i]).ToLower ()) {
62 case ".cer":
63 case ".crt":
64 spc.Certificates.Add (new X509Certificate (GetFile (args[i])));
65 break;
66 case ".crl":
67 spc.Crls.Add (GetFile (args[i]));
68 break;
69 default:
70 error = "Unknown file extension : " + args[i];
71 return 1;
75 using (FileStream fs = File.Open (output, FileMode.Create, FileAccess.Write)) {
76 byte[] data = spc.GetBytes ();
77 fs.Write (data, 0, data.Length);
78 fs.Close ();
80 return 0;
83 [STAThread]
84 static int Main (string[] args)
86 int result = 1;
87 try {
88 Header ();
89 result = Process (args);
91 if (error == null)
92 Console.WriteLine ("Success");
93 else {
94 Console.WriteLine ("Error: {0}{1}", error, Environment.NewLine);
95 Help ();
98 catch (Exception e) {
99 Console.WriteLine ("Error: " + e.ToString ());
100 Help ();
102 return result;