2003-02-13 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / tools / cert2spc.cs
1 //
2 // Cert2Spc.cs: cert2spc clone tool
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Reflection;
13 using Mono.Security.ASN1;
14
15 [assembly: AssemblyTitle("Mono Cert2Spc")]
16 [assembly: AssemblyDescription("Transform a chain of certificate into an Authenticode(TM) \"Software Publisher Certificate\"")]
17 [assembly: AssemblyCompany("Sébastien Pouliot, Motus Technologies")]
18 [assembly: AssemblyProduct("Open Source Tools for .NET")]
19 [assembly: AssemblyCopyright("Copyright 2002 Motus Technologies. Released under BSD license.")]
20 [assembly: AssemblyVersion("0.17.99.0")]
21
22 namespace Mono.Tools {
23
24 class Cert2Spc {
25
26         static private string error;
27
28         static private void Header () 
29         {
30                 Assembly a = Assembly.GetExecutingAssembly ();
31                 AssemblyName an = a.GetName ();
32         
33                 object [] att = a.GetCustomAttributes (typeof (AssemblyTitleAttribute), false);
34                 string title = ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : "Mono SecUtil");
35
36                 att = a.GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);
37                 string copyright = ((att.Length > 0) ? ((AssemblyCopyrightAttribute) att [0]).Copyright : "");
38
39                 Console.WriteLine ("{0} {1}", title, an.Version.ToString ());
40                 Console.WriteLine ("{0}{1}", copyright, Environment.NewLine);
41         }
42
43         static private void Help () 
44         {
45                 Console.WriteLine ("Usage: cert2spc certificate|crl [certificate|crl] [...] outputfile.spc{0}", Environment.NewLine);
46         }
47
48         static void Process (string[] args) 
49         {
50                 if (args.Length < 2) {
51                         error = "At least one input and output files must be specified";
52                         return;
53                 }
54
55                 string outFile = args [args.Length - 1];
56                 // build certificate/crl list
57                 ASN1 listOfCerts = new ASN1 (0xA0, null);
58                 for (int i=0; i < args.Length - 1; i++) {
59                         FileStream fs = new FileStream (args[i], FileMode.Open, FileAccess.Read);
60                         byte[] cert = new byte [fs.Length];
61                         fs.Read (cert, 0, cert.Length);
62                         listOfCerts.Add (new ASN1(cert));
63                 }
64
65                 // compose header
66                 ASN1 integer = new ASN1 (0x02, null);
67                 integer.Value = new byte[1];
68                 integer.Value[0] = 1;
69
70                 ASN1 seqOID = new ASN1 (0x30, null);
71                 seqOID.Add (new OID ("1.2.840.113549.1.7.1"));
72
73                 ASN1 sequence = new ASN1 (0x30, null);
74                 sequence.Add (integer);
75                 sequence.Add (new ASN1 (0x31, null)); // empty set
76                 sequence.Add (seqOID);
77                 sequence.Add (listOfCerts);
78                 sequence.Add (new ASN1 (0x31, null)); // empty set
79
80                 ASN1 a0 = new ASN1 (0xA0, null);
81                 a0.Add (sequence);
82
83                 ASN1 spc = new ASN1 (0x30, null);
84                 spc.Add (new OID ("1.2.840.113549.1.7.2"));
85                 spc.Add (a0);
86
87                 // write output file
88                 FileStream spcFile = new FileStream (outFile, FileMode.Create, FileAccess.Write);
89                 byte[] rawSpc = spc.GetBytes ();
90                 spcFile.Write (rawSpc, 0, rawSpc.Length);
91                 spcFile.Close ();
92         }
93
94         [STAThread]
95         static void Main (string[] args) 
96         {
97                 try {
98                         Header();
99                         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         }
113 }
114
115 }