* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / tools / security / cert2spc.cs
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 //
10
11 using System;
12 using System.IO;
13 using System.Reflection;
14
15 using Mono.Security.Authenticode;
16 using Mono.Security.X509;
17
18 [assembly: AssemblyTitle("Mono Cert2Spc")]
19 [assembly: AssemblyDescription("Transform a set of X.509 certificates and CRLs into an Authenticode(TM) \"Software Publisher Certificate\"")]
20
21 namespace Mono.Tools {
22
23 class Cert2Spc {
24
25         static private string error;
26
27         static private void Header () 
28         {
29                 Console.WriteLine (new AssemblyInfo ().ToString ());
30         }
31
32         static private void Help () 
33         {
34                 Console.WriteLine ("Usage: cert2spc certificate|crl [certificate|crl] [...] outputfile.spc{0}", Environment.NewLine);
35         }
36
37         // until we have real CRL support
38         static byte[] GetFile (string filename) 
39         {
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 ();
45                 }
46                 return data;
47         }
48
49         static int Process (string[] args) 
50         {
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;
55                 }
56
57                 string output = args [nargs];
58                 SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate ();
59
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;
72                         }
73                 }
74
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 ();
79                 }
80                 return 0;
81         }
82
83         [STAThread]
84         static int Main (string[] args) 
85         {
86                 int result = 1;
87                 try {
88                         Header ();
89                         result = Process (args);
90
91                         if (error == null)
92                                 Console.WriteLine ("Success");
93                         else {
94                                 Console.WriteLine ("Error: {0}{1}", error, Environment.NewLine);
95                                 Help ();
96                         }
97                 }
98                 catch (Exception e) {
99                         Console.WriteLine ("Error: " + e.ToString ());
100                         Help ();
101                 }
102                 return result;
103         }
104 }
105
106 }