* HttpsClientStream.cs: Added a TrustFailure property so a WebException can now repor...
[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 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Globalization;
36 using System.IO;
37 using System.Text;
38
39 using Mono.Security;
40 using Mono.Security.X509;
41
42 namespace Mono.Security.Authenticode {
43
44         public class SoftwarePublisherCertificate {
45
46                 private PKCS7.SignedData pkcs7;
47
48                 public SoftwarePublisherCertificate () 
49                 {
50                         pkcs7 = new PKCS7.SignedData ();
51                         pkcs7.ContentInfo.ContentType = PKCS7.Oid.data;
52                 }
53
54                 public SoftwarePublisherCertificate (byte[] data) : this ()
55                 {
56                         if (data == null)
57                                 throw new ArgumentNullException ("data");
58
59                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (data);
60                         if (ci.ContentType != PKCS7.Oid.signedData) {
61                                 throw new ArgumentException (
62                                         Locale.GetText ("Unsupported ContentType"));
63                         }
64                         pkcs7 = new PKCS7.SignedData (ci.Content);
65                 }
66
67                 public X509CertificateCollection Certificates {
68                         get { return pkcs7.Certificates; }
69                 }
70
71                 public ArrayList Crls {
72                         get { return pkcs7.Crls; }
73                 }
74
75                 public byte[] GetBytes () 
76                 {
77                         PKCS7.ContentInfo ci = new PKCS7.ContentInfo (PKCS7.Oid.signedData);
78                         ci.Content.Add (pkcs7.ASN1);
79                         return ci.GetBytes ();
80                 }
81
82                 static public SoftwarePublisherCertificate CreateFromFile (string filename) 
83                 {
84                         if (filename == null)
85                                 throw new ArgumentNullException ("filename");
86
87                         byte[] data = null;
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);
91                                 fs.Close ();
92                         }
93
94                         // It seems that VeriSign send the SPC file in Unicode
95                         // (base64 encoded) and Windows accept them.
96                         if (data.Length < 2)
97                                 return null;
98
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));
104                                 }
105                                 else {
106                                         // default to base64/ascii
107                                         data = Convert.FromBase64String (Encoding.ASCII.GetString (data));
108                                 }
109                         }
110 #if DEBUG
111                         using (FileStream fs = File.OpenWrite (filename + ".der")) {
112                                 fs.Write (data, 0, data.Length);
113                                 fs.Close ();
114                         }
115 #endif
116                         return new SoftwarePublisherCertificate (data);
117                 }
118         }
119 }