2 // Cert2Spc.cs: cert2spc clone tool
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
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
{
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
)
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
);
59 static int Process (string[] args
)
61 int nargs
= args
.Length
- 1;
63 error
= "At least one input and output files must be specified";
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 ()) {
74 spc
.Certificates
.Add (new X509Certificate (GetFile (args
[i
])));
77 spc
.Crls
.Add (GetFile (args
[i
]));
80 error
= "Unknown file extension : " + args
[i
];
85 using (FileStream fs
= File
.Open (output
, FileMode
.Create
, FileAccess
.Write
)) {
86 byte[] data
= spc
.GetBytes ();
87 fs
.Write (data
, 0, data
.Length
);
94 static int Main (string[] args
)
99 result
= Process (args
);
102 Console
.WriteLine ("Success");
104 Console
.WriteLine ("Error: {0}{1}", error
, Environment
.NewLine
);
108 catch (Exception e
) {
109 Console
.WriteLine ("Error: " + e
.ToString ());