This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 Assembly a = Assembly.GetExecutingAssembly ();
30                 AssemblyName an = a.GetName ();
31         
32                 object [] att = a.GetCustomAttributes (typeof (AssemblyTitleAttribute), false);
33                 string title = ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : "Mono SecUtil");
34
35                 att = a.GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);
36                 string copyright = ((att.Length > 0) ? ((AssemblyCopyrightAttribute) att [0]).Copyright : "");
37
38                 Console.WriteLine ("{0} {1}", title, an.Version.ToString ());
39                 Console.WriteLine ("{0}{1}", copyright, Environment.NewLine);
40         }
41
42         static private void Help () 
43         {
44                 Console.WriteLine ("Usage: cert2spc certificate|crl [certificate|crl] [...] outputfile.spc{0}", Environment.NewLine);
45         }
46
47         // until we have real CRL support
48         static byte[] GetFile (string filename) 
49         {
50                 byte[] data = null;
51                 using (FileStream fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
52                         data = new byte [fs.Length];
53                         fs.Read (data, 0, data.Length);
54                         fs.Close ();
55                 }
56                 return data;
57         }
58
59         static int Process (string[] args) 
60         {
61                 int nargs = args.Length - 1;
62                 if (nargs < 1) {
63                         error = "At least one input and output files must be specified";
64                         return 1;
65                 }
66
67                 string output = args [nargs];
68                 SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate ();
69
70                 for (int i=0; i < args.Length - 1; i++) {
71                         switch (Path.GetExtension (args[i]).ToLower ()) {
72                                 case ".cer":
73                                 case ".crt":
74                                         spc.Certificates.Add (new X509Certificate (GetFile (args[i])));
75                                         break;
76                                 case ".crl":
77                                         spc.Crls.Add (GetFile (args[i]));
78                                         break;
79                                 default:
80                                         error = "Unknown file extension : " + args[i];
81                                         return 1;
82                         }
83                 }
84
85                 using (FileStream fs = File.Open (output, FileMode.Create, FileAccess.Write)) {
86                         byte[] data = spc.GetBytes ();
87                         fs.Write (data, 0, data.Length);
88                         fs.Close ();
89                 }
90                 return 0;
91         }
92
93         [STAThread]
94         static int Main (string[] args) 
95         {
96                 int result = 1;
97                 try {
98                         Header ();
99                         result = Process (args);
100
101                         if (error == null)
102                                 Console.WriteLine ("Success");
103                         else {
104                                 Console.WriteLine ("Error: {0}{1}", error, Environment.NewLine);
105                                 Help ();
106                         }
107                 }
108                 catch (Exception e) {
109                         Console.WriteLine ("Error: " + e.ToString ());
110                         Help ();
111                 }
112                 return result;
113         }
114 }
115
116 }