Merge pull request #895 from ranma42/fix-tuple-hash-codegen
[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 :
43 #if NET_4_0
44                 EvidenceBase,
45 #endif
46                 ISecurityEncodable {
47
48                 private ApplicationIdentity _appid;
49                 private PolicyStatement _defaultPolicy;
50                 private object _xtranfo;
51                 private bool _trustrun;
52                 private bool _persist;
53                 IList<StrongName> fullTrustAssemblies;
54
55                 public ApplicationTrust ()
56                 {
57                         fullTrustAssemblies = new List<StrongName> (0);
58                 }
59
60                 public ApplicationTrust (ApplicationIdentity applicationIdentity)
61                         : this ()
62                 {
63                         if (applicationIdentity == null)
64                                 throw new ArgumentNullException ("applicationIdentity");
65                         _appid = applicationIdentity;
66                 }
67                 
68 #if NET_4_0
69                 public
70 #else
71                 internal
72 #endif
73                 ApplicationTrust (PermissionSet defaultGrantSet, IEnumerable<StrongName> fullTrustAssemblies)
74                 {
75                         if (defaultGrantSet == null)
76                                 throw new ArgumentNullException ("defaultGrantSet");
77
78                         _defaultPolicy = new PolicyStatement (defaultGrantSet);
79
80                         if (fullTrustAssemblies == null)
81                                 throw new ArgumentNullException ("fullTrustAssemblies");
82
83                         this.fullTrustAssemblies = new List<StrongName> ();
84                         foreach (var a in fullTrustAssemblies) {
85                                 if (a == null)
86                                         throw new ArgumentException ("fullTrustAssemblies contains an assembly that does not have a StrongName");
87
88                                 this.fullTrustAssemblies.Add ((StrongName) a.Copy ());
89                         }
90                 }
91
92                 public ApplicationIdentity ApplicationIdentity {
93                         get { return _appid; }
94                         set {
95                                 if (value == null)
96                                         throw new ArgumentNullException ("ApplicationIdentity");
97                                 _appid = value;
98                         }
99                 }
100
101                 public PolicyStatement DefaultGrantSet {
102                         get {
103                                 if (_defaultPolicy == null)
104                                         _defaultPolicy = GetDefaultGrantSet ();
105
106                                 return _defaultPolicy;
107                         }
108                         set { _defaultPolicy = value; }
109                 }
110
111                 public object ExtraInfo {
112                         get { return _xtranfo; }
113                         set { _xtranfo = value; }
114                 }
115
116                 public bool IsApplicationTrustedToRun {
117                         get { return _trustrun; }
118                         set { _trustrun = value; }
119                 }
120
121                 public bool Persist {
122                         get { return _persist; }
123                         set { _persist = value; }
124                 }
125
126                 public void FromXml (SecurityElement element) 
127                 {
128                         if (element == null)
129                                 throw new ArgumentNullException ("element");
130
131                         if (element.Tag != "ApplicationTrust")
132                                 throw new ArgumentException ("element");
133
134                         string s = element.Attribute ("FullName");
135                         if (s != null)
136                                 _appid = new ApplicationIdentity (s);
137                         else
138                                 _appid = null;
139
140                         _defaultPolicy = null;
141                         SecurityElement defaultGrant = element.SearchForChildByTag ("DefaultGrant");
142                         if (defaultGrant != null) {
143                                 for (int i=0; i < defaultGrant.Children.Count; i++) {
144                                         SecurityElement se = (defaultGrant.Children [i] as SecurityElement);
145                                         if (se.Tag == "PolicyStatement") {
146                                                 DefaultGrantSet.FromXml (se, null);
147                                                 break;
148                                         }
149                                 }
150                         }
151
152                         if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
153                                 _trustrun = false;
154
155                         if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
156                                 _persist = false;
157
158                         _xtranfo = null;
159                         SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
160                         if (xtra != null) {
161                                 s = xtra.Attribute ("Data");
162                                 if (s != null) {
163                                         byte[] data = CryptoConvert.FromHex (s);
164                                         using (MemoryStream ms = new MemoryStream (data)) {
165                                                 BinaryFormatter bf = new BinaryFormatter ();
166                                                 _xtranfo = bf.Deserialize (ms);
167                                         }
168                                 }
169                         }
170                 }
171
172                 public SecurityElement ToXml () 
173                 {
174                         SecurityElement se = new SecurityElement ("ApplicationTrust");
175                         se.AddAttribute ("version", "1");
176
177                         if (_appid != null) {
178                                 se.AddAttribute ("FullName", _appid.FullName);
179                         }
180
181                         if (_trustrun) {
182                                 se.AddAttribute ("TrustedToRun", "true");
183                         }
184
185                         if (_persist) {
186                                 se.AddAttribute ("Persist", "true");
187                         }
188
189                         SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
190                         defaultGrant.AddChild (DefaultGrantSet.ToXml ());
191                         se.AddChild (defaultGrant);
192
193                         if (_xtranfo != null) {
194                                 byte[] data = null;
195                                 using (MemoryStream ms = new MemoryStream ()) {
196                                         BinaryFormatter bf = new BinaryFormatter ();
197                                         bf.Serialize (ms, _xtranfo);
198                                         data = ms.ToArray ();
199                                 }
200                                 SecurityElement xtra = new SecurityElement ("ExtraInfo");
201                                 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
202                                 se.AddChild (xtra);
203                         }
204
205                         return se;
206                 }
207                 
208 #if NET_4_0             
209                 public IList<StrongName> FullTrustAssemblies {
210                         get {
211                                 return fullTrustAssemblies;
212                         }
213                 }
214 #endif          
215
216                 // internal stuff
217
218                 private PolicyStatement GetDefaultGrantSet ()
219                 {
220                         PermissionSet ps = new PermissionSet (PermissionState.None);
221                         return new PolicyStatement (ps);
222                 }
223         }
224 }
225