[w32file] Move MonoIO.Find{First,Next,Close} to managed
[mono.git] / mcs / class / Mono.Security / Mono.Security.Authenticode / SoftwarePublisherCertificate.cs
1 //
2 // SoftwarePublisherCertificate.cs 
3 //      - Software Publisher Certificates Implementation
4 //
5 // Author:
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004,2008 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.IO;
35 using System.Security.Cryptography;
36 using System.Text;
37
38 using Mono.Security;
39 using Mono.Security.X509;
40
41 namespace Mono.Security.Authenticode {
42
43         public class SoftwarePublisherCertificate {
44
45                 private PKCS7.SignedData pkcs7;
46
47                 public SoftwarePublisherCertificate () 
48                 {
49                         pkcs7 = new PKCS7.SignedData ();
50                         pkcs7.ContentInfo.ContentType = PKCS7.Oid.data;
51                 }
52
53                 public SoftwarePublisherCertificate (byte[] data) : this ()
54                 {
55                         if (data == null)
56                                 throw new ArgumentNullException ("data");
57
58                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (data);
59                         if (ci.ContentType != PKCS7.Oid.signedData) {
60                                 throw new ArgumentException (
61                                         Locale.GetText ("Unsupported ContentType"));
62                         }
63                         pkcs7 = new PKCS7.SignedData (ci.Content);
64                 }
65
66                 public X509CertificateCollection Certificates {
67                         get { return pkcs7.Certificates; }
68                 }
69
70                 public ArrayList Crls {
71                         get { return pkcs7.Crls; }
72                 }
73
74                 public byte[] GetBytes () 
75                 {
76                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (PKCS7.Oid.signedData);
77                         ci.Content.Add (pkcs7.ASN1);
78                         return ci.GetBytes ();
79                 }
80
81                 static public SoftwarePublisherCertificate CreateFromFile (string filename) 
82                 {
83                         if (filename == null)
84                                 throw new ArgumentNullException ("filename");
85
86                         byte[] data = null;
87                         using (FileStream fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
88                                 data = new byte [fs.Length];
89                                 fs.Read (data, 0, data.Length);
90                                 fs.Close ();
91                         }
92
93                         // It seems that VeriSign send the SPC file in Unicode
94                         // (base64 encoded) and Windows accept them.
95                         if (data.Length < 2)
96                                 return null;
97
98                         if (data [0] != 0x30) {
99                                 // this isn't an ASN.1 SEQUENCE (so not legal), check for PEM/base64 encoding
100                                 try {
101                                         data = PEM (data);
102                                 }
103                                 catch (Exception ex) {
104                                         throw new CryptographicException ("Invalid encoding", ex);
105                                 }
106                         }
107 #if DEBUG
108                         using (FileStream fs = File.OpenWrite (filename + ".der")) {
109                                 fs.Write (data, 0, data.Length);
110                                 fs.Close ();
111                         }
112 #endif
113                         return new SoftwarePublisherCertificate (data);
114                 }
115
116                 const string header = "-----BEGIN PKCS7-----";
117                 const string footer = "-----END PKCS7-----";
118
119                 static byte[] PEM (byte[] data) 
120                 {
121                         // this could be base64/unicode (e.g. VeriSign) otherwise default to ASCII
122                         string pem = (data [1] == 0x00) ? Encoding.Unicode.GetString (data) : Encoding.ASCII.GetString (data);
123                         int start = pem.IndexOf (header) + header.Length;
124                         int end = pem.IndexOf (footer, start);
125                         string base64 = ((start == -1) || (end == -1)) ? pem : pem.Substring (start, (end - start));
126                         return Convert.FromBase64String (base64);
127                 }
128         }
129 }