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 : ISecurityEncodable {
44 private ApplicationIdentity _appid;
45 private PolicyStatement _defaultPolicy;
46 private object _xtranfo;
47 private bool _trustrun;
48 private bool _persist;
49 IList<StrongName> fullTrustAssemblies;
51 public ApplicationTrust ()
53 fullTrustAssemblies = new List<StrongName> (0);
56 public ApplicationTrust (ApplicationIdentity applicationIdentity)
59 if (applicationIdentity == null)
60 throw new ArgumentNullException ("applicationIdentity");
61 _appid = applicationIdentity;
69 ApplicationTrust (PermissionSet defaultGrantSet, IEnumerable<StrongName> fullTrustAssemblies)
71 if (defaultGrantSet == null)
72 throw new ArgumentNullException ("defaultGrantSet");
74 _defaultPolicy = new PolicyStatement (defaultGrantSet);
76 if (fullTrustAssemblies == null)
77 throw new ArgumentNullException ("fullTrustAssemblies");
79 this.fullTrustAssemblies = new List<StrongName> ();
80 foreach (var a in fullTrustAssemblies) {
82 throw new ArgumentException ("fullTrustAssemblies contains an assembly that does not have a StrongName");
84 this.fullTrustAssemblies.Add ((StrongName) a.Copy ());
88 public ApplicationIdentity ApplicationIdentity {
89 get { return _appid; }
92 throw new ArgumentNullException ("ApplicationIdentity");
97 public PolicyStatement DefaultGrantSet {
99 if (_defaultPolicy == null)
100 _defaultPolicy = GetDefaultGrantSet ();
102 return _defaultPolicy;
104 set { _defaultPolicy = value; }
107 public object ExtraInfo {
108 get { return _xtranfo; }
109 set { _xtranfo = value; }
112 public bool IsApplicationTrustedToRun {
113 get { return _trustrun; }
114 set { _trustrun = value; }
117 public bool Persist {
118 get { return _persist; }
119 set { _persist = value; }
122 public void FromXml (SecurityElement element)
125 throw new ArgumentNullException ("element");
127 if (element.Tag != "ApplicationTrust")
128 throw new ArgumentException ("element");
130 string s = element.Attribute ("FullName");
132 _appid = new ApplicationIdentity (s);
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);
148 if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
151 if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
155 SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
157 s = xtra.Attribute ("Data");
159 byte[] data = CryptoConvert.FromHex (s);
160 using (MemoryStream ms = new MemoryStream (data)) {
161 BinaryFormatter bf = new BinaryFormatter ();
162 _xtranfo = bf.Deserialize (ms);
168 public SecurityElement ToXml ()
170 SecurityElement se = new SecurityElement ("ApplicationTrust");
171 se.AddAttribute ("version", "1");
173 if (_appid != null) {
174 se.AddAttribute ("FullName", _appid.FullName);
178 se.AddAttribute ("TrustedToRun", "true");
182 se.AddAttribute ("Persist", "true");
185 SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
186 defaultGrant.AddChild (DefaultGrantSet.ToXml ());
187 se.AddChild (defaultGrant);
189 if (_xtranfo != null) {
191 using (MemoryStream ms = new MemoryStream ()) {
192 BinaryFormatter bf = new BinaryFormatter ();
193 bf.Serialize (ms, _xtranfo);
194 data = ms.ToArray ();
196 SecurityElement xtra = new SecurityElement ("ExtraInfo");
197 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
205 public IList<StrongName> FullTrustAssemblies {
207 return fullTrustAssemblies;
214 private PolicyStatement GetDefaultGrantSet ()
216 PermissionSet ps = new PermissionSet (PermissionState.None);
217 return new PolicyStatement (ps);