2 // System.Security.Policy.ApplicationTrust.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Runtime.InteropServices;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Security.Permissions;
35 using Mono.Security.Cryptography;
36 using System.Collections.Generic;
38 namespace System.Security.Policy
42 public sealed class ApplicationTrust :
48 private ApplicationIdentity _appid;
49 private PolicyStatement _defaultPolicy;
50 private object _xtranfo;
51 private bool _trustrun;
52 private bool _persist;
53 IList<StrongName> fullTrustAssemblies;
55 public ApplicationTrust ()
57 fullTrustAssemblies = new List<StrongName> (0);
60 public ApplicationTrust (ApplicationIdentity applicationIdentity)
63 if (applicationIdentity == null)
64 throw new ArgumentNullException ("applicationIdentity");
65 _appid = applicationIdentity;
73 ApplicationTrust (PermissionSet defaultGrantSet, IEnumerable<StrongName> fullTrustAssemblies)
75 if (defaultGrantSet == null)
76 throw new ArgumentNullException ("defaultGrantSet");
78 _defaultPolicy = new PolicyStatement (defaultGrantSet);
80 if (fullTrustAssemblies == null)
81 throw new ArgumentNullException ("fullTrustAssemblies");
83 this.fullTrustAssemblies = new List<StrongName> ();
84 foreach (var a in fullTrustAssemblies) {
86 throw new ArgumentException ("fullTrustAssemblies contains an assembly that does not have a StrongName");
88 this.fullTrustAssemblies.Add ((StrongName) a.Copy ());
92 public ApplicationIdentity ApplicationIdentity {
93 get { return _appid; }
96 throw new ArgumentNullException ("ApplicationIdentity");
101 public PolicyStatement DefaultGrantSet {
103 if (_defaultPolicy == null)
104 _defaultPolicy = GetDefaultGrantSet ();
106 return _defaultPolicy;
108 set { _defaultPolicy = value; }
111 public object ExtraInfo {
112 get { return _xtranfo; }
113 set { _xtranfo = value; }
116 public bool IsApplicationTrustedToRun {
117 get { return _trustrun; }
118 set { _trustrun = value; }
121 public bool Persist {
122 get { return _persist; }
123 set { _persist = value; }
126 public void FromXml (SecurityElement element)
129 throw new ArgumentNullException ("element");
131 if (element.Tag != "ApplicationTrust")
132 throw new ArgumentException ("element");
134 string s = element.Attribute ("FullName");
136 _appid = new ApplicationIdentity (s);
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);
152 if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
155 if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
159 SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
161 s = xtra.Attribute ("Data");
163 byte[] data = CryptoConvert.FromHex (s);
164 using (MemoryStream ms = new MemoryStream (data)) {
165 BinaryFormatter bf = new BinaryFormatter ();
166 _xtranfo = bf.Deserialize (ms);
172 public SecurityElement ToXml ()
174 SecurityElement se = new SecurityElement ("ApplicationTrust");
175 se.AddAttribute ("version", "1");
177 if (_appid != null) {
178 se.AddAttribute ("FullName", _appid.FullName);
182 se.AddAttribute ("TrustedToRun", "true");
186 se.AddAttribute ("Persist", "true");
189 SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
190 defaultGrant.AddChild (DefaultGrantSet.ToXml ());
191 se.AddChild (defaultGrant);
193 if (_xtranfo != null) {
195 using (MemoryStream ms = new MemoryStream ()) {
196 BinaryFormatter bf = new BinaryFormatter ();
197 bf.Serialize (ms, _xtranfo);
198 data = ms.ToArray ();
200 SecurityElement xtra = new SecurityElement ("ExtraInfo");
201 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
209 public IList<StrongName> FullTrustAssemblies {
211 return fullTrustAssemblies;
218 private PolicyStatement GetDefaultGrantSet ()
220 PermissionSet ps = new PermissionSet (PermissionState.None);
221 return new PolicyStatement (ps);