2 // SoftwarePublisherCertificate.cs
3 // - Software Publisher Certificates Implementation
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Globalization;
40 using Mono.Security.X509;
42 namespace Mono.Security.Authenticode {
44 public class SoftwarePublisherCertificate {
46 private PKCS7.SignedData pkcs7;
48 public SoftwarePublisherCertificate ()
50 pkcs7 = new PKCS7.SignedData ();
51 pkcs7.ContentInfo.ContentType = PKCS7.Oid.data;
54 public SoftwarePublisherCertificate (byte[] data) : this ()
57 throw new ArgumentNullException ("data");
59 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (data);
60 if (ci.ContentType != PKCS7.Oid.signedData) {
61 throw new ArgumentException (
62 Locale.GetText ("Unsupported ContentType"));
64 pkcs7 = new PKCS7.SignedData (ci.Content);
67 public X509CertificateCollection Certificates {
68 get { return pkcs7.Certificates; }
71 public ArrayList Crls {
72 get { return pkcs7.Crls; }
75 public byte[] GetBytes ()
77 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (PKCS7.Oid.signedData);
78 ci.Content.Add (pkcs7.ASN1);
79 return ci.GetBytes ();
82 static public SoftwarePublisherCertificate CreateFromFile (string filename)
85 throw new ArgumentNullException ("filename");
88 using (FileStream fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
89 data = new byte [fs.Length];
90 fs.Read (data, 0, data.Length);
94 // It seems that VeriSign send the SPC file in Unicode
95 // (base64 encoded) and Windows accept them.
99 if (data [0] != 0x30) {
100 // this isn't an ASN.1 SEQUENCE (so not legal)
101 if (data [1] == 0x00) {
102 // this could be base64/unicode (e.g. VeriSign)
103 data = Convert.FromBase64String (Encoding.Unicode.GetString (data));
106 // default to base64/ascii
107 data = Convert.FromBase64String (Encoding.ASCII.GetString (data));
111 using (FileStream fs = File.OpenWrite (filename + ".der")) {
112 fs.Write (data, 0, data.Length);
116 return new SoftwarePublisherCertificate (data);