(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / tools / security / cert2spc.cs
blob554964e9f1640493a227f23a64ef7518b4a65b37
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 Assembly a = Assembly.GetExecutingAssembly ();
30 AssemblyName an = a.GetName ();
32 object [] att = a.GetCustomAttributes (typeof (AssemblyTitleAttribute), false);
33 string title = ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : "Mono SecUtil");
35 att = a.GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);
36 string copyright = ((att.Length > 0) ? ((AssemblyCopyrightAttribute) att [0]).Copyright : "");
38 Console.WriteLine ("{0} {1}", title, an.Version.ToString ());
39 Console.WriteLine ("{0}{1}", copyright, Environment.NewLine);
42 static private void Help ()
44 Console.WriteLine ("Usage: cert2spc certificate|crl [certificate|crl] [...] outputfile.spc{0}", Environment.NewLine);
47 // until we have real CRL support
48 static byte[] GetFile (string filename)
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 ();
56 return data;
59 static int Process (string[] args)
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;
67 string output = args [nargs];
68 SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate ();
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;
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 ();
90 return 0;
93 [STAThread]
94 static int Main (string[] args)
96 int result = 1;
97 try {
98 Header ();
99 result = Process (args);
101 if (error == null)
102 Console.WriteLine ("Success");
103 else {
104 Console.WriteLine ("Error: {0}{1}", error, Environment.NewLine);
105 Help ();
108 catch (Exception e) {
109 Console.WriteLine ("Error: " + e.ToString ());
110 Help ();
112 return result;