System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationTrust.cs
1 //
2 // System.Security.Policy.ApplicationTrust.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System.IO;
31 using System.Runtime.InteropServices;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Security.Permissions;
34
35 using Mono.Security.Cryptography;
36 using System.Collections.Generic;
37
38 namespace System.Security.Policy
39 {
40         [Serializable]
41         [ComVisible (true)]
42         public sealed class ApplicationTrust : ISecurityEncodable {
43
44                 private ApplicationIdentity _appid;
45                 private PolicyStatement _defaultPolicy;
46                 private object _xtranfo;
47                 private bool _trustrun;
48                 private bool _persist;
49                 IList<StrongName> fullTrustAssemblies;
50
51                 public ApplicationTrust ()
52                 {
53                         fullTrustAssemblies = new List<StrongName> (0);
54                 }
55
56                 public ApplicationTrust (ApplicationIdentity applicationIdentity)
57                         : this ()
58                 {
59                         if (applicationIdentity == null)
60                                 throw new ArgumentNullException ("applicationIdentity");
61                         _appid = applicationIdentity;
62                 }
63                 
64 #if NET_4_0
65                 public
66 #else
67                 internal
68 #endif
69                 ApplicationTrust (PermissionSet defaultGrantSet, IEnumerable<StrongName> fullTrustAssemblies)
70                 {
71                         if (defaultGrantSet == null)
72                                 throw new ArgumentNullException ("defaultGrantSet");
73
74                         _defaultPolicy = new PolicyStatement (defaultGrantSet);
75
76                         if (fullTrustAssemblies == null)
77                                 throw new ArgumentNullException ("fullTrustAssemblies");
78
79                         this.fullTrustAssemblies = new List<StrongName> ();
80                         foreach (var a in fullTrustAssemblies) {
81                                 if (a == null)
82                                         throw new ArgumentException ("fullTrustAssemblies contains an assembly that does not have a StrongName");
83
84                                 this.fullTrustAssemblies.Add ((StrongName) a.Copy ());
85                         }
86                 }
87
88                 public ApplicationIdentity ApplicationIdentity {
89                         get { return _appid; }
90                         set {
91                                 if (value == null)
92                                         throw new ArgumentNullException ("ApplicationIdentity");
93                                 _appid = value;
94                         }
95                 }
96
97                 public PolicyStatement DefaultGrantSet {
98                         get {
99                                 if (_defaultPolicy == null)
100                                         _defaultPolicy = GetDefaultGrantSet ();
101
102                                 return _defaultPolicy;
103                         }
104                         set { _defaultPolicy = value; }
105                 }
106
107                 public object ExtraInfo {
108                         get { return _xtranfo; }
109                         set { _xtranfo = value; }
110                 }
111
112                 public bool IsApplicationTrustedToRun {
113                         get { return _trustrun; }
114                         set { _trustrun = value; }
115                 }
116
117                 public bool Persist {
118                         get { return _persist; }
119                         set { _persist = value; }
120                 }
121
122                 public void FromXml (SecurityElement element) 
123                 {
124                         if (element == null)
125                                 throw new ArgumentNullException ("element");
126
127                         if (element.Tag != "ApplicationTrust")
128                                 throw new ArgumentException ("element");
129
130                         string s = element.Attribute ("FullName");
131                         if (s != null)
132                                 _appid = new ApplicationIdentity (s);
133                         else
134                                 _appid = null;
135
136                         _defaultPolicy = null;
137                         SecurityElement defaultGrant = element.SearchForChildByTag ("DefaultGrant");
138                         if (defaultGrant != null) {
139                                 for (int i=0; i < defaultGrant.Children.Count; i++) {
140                                         SecurityElement se = (defaultGrant.Children [i] as SecurityElement);
141                                         if (se.Tag == "PolicyStatement") {
142                                                 DefaultGrantSet.FromXml (se, null);
143                                                 break;
144                                         }
145                                 }
146                         }
147
148                         if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
149                                 _trustrun = false;
150
151                         if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
152                                 _persist = false;
153
154                         _xtranfo = null;
155                         SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
156                         if (xtra != null) {
157                                 s = xtra.Attribute ("Data");
158                                 if (s != null) {
159                                         byte[] data = CryptoConvert.FromHex (s);
160                                         using (MemoryStream ms = new MemoryStream (data)) {
161                                                 BinaryFormatter bf = new BinaryFormatter ();
162                                                 _xtranfo = bf.Deserialize (ms);
163                                         }
164                                 }
165                         }
166                 }
167
168                 public SecurityElement ToXml () 
169                 {
170                         SecurityElement se = new SecurityElement ("ApplicationTrust");
171                         se.AddAttribute ("version", "1");
172
173                         if (_appid != null) {
174                                 se.AddAttribute ("FullName", _appid.FullName);
175                         }
176
177                         if (_trustrun) {
178                                 se.AddAttribute ("TrustedToRun", "true");
179                         }
180
181                         if (_persist) {
182                                 se.AddAttribute ("Persist", "true");
183                         }
184
185                         SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
186                         defaultGrant.AddChild (DefaultGrantSet.ToXml ());
187                         se.AddChild (defaultGrant);
188
189                         if (_xtranfo != null) {
190                                 byte[] data = null;
191                                 using (MemoryStream ms = new MemoryStream ()) {
192                                         BinaryFormatter bf = new BinaryFormatter ();
193                                         bf.Serialize (ms, _xtranfo);
194                                         data = ms.ToArray ();
195                                 }
196                                 SecurityElement xtra = new SecurityElement ("ExtraInfo");
197                                 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
198                                 se.AddChild (xtra);
199                         }
200
201                         return se;
202                 }
203                 
204 #if NET_4_0             
205                 public IList<StrongName> FullTrustAssemblies {
206                         get {
207                                 return fullTrustAssemblies;
208                         }
209                 }
210 #endif          
211
212                 // internal stuff
213
214                 private PolicyStatement GetDefaultGrantSet ()
215                 {
216                         PermissionSet ps = new PermissionSet (PermissionState.None);
217                         return new PolicyStatement (ps);
218                 }
219         }
220 }
221