2 // System.Security.Policy.ApplicationTrust class
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.
32 using System.Runtime.InteropServices;
33 using System.Runtime.Serialization.Formatters.Binary;
34 using System.Security.Permissions;
36 using Mono.Security.Cryptography;
38 namespace System.Security.Policy {
41 public sealed class ApplicationTrust : ISecurityEncodable {
43 private ApplicationIdentity _appid;
44 private PolicyStatement _defaultPolicy;
45 private object _xtranfo;
46 private bool _trustrun;
47 private bool _persist;
49 public ApplicationTrust ()
53 public ApplicationTrust (ApplicationIdentity applicationIdentity)
55 if (applicationIdentity == null)
56 throw new ArgumentNullException ("applicationIdentity");
57 _appid = applicationIdentity;
60 public ApplicationIdentity ApplicationIdentity {
61 get { return _appid; }
64 throw new ArgumentNullException ("ApplicationIdentity");
69 public PolicyStatement DefaultGrantSet {
71 if (_defaultPolicy == null)
72 _defaultPolicy = GetDefaultGrantSet ();
74 return _defaultPolicy;
76 set { _defaultPolicy = value; }
79 public object ExtraInfo {
80 get { return _xtranfo; }
81 set { _xtranfo = value; }
84 public bool IsApplicationTrustedToRun {
85 get { return _trustrun; }
86 set { _trustrun = value; }
90 get { return _persist; }
91 set { _persist = value; }
94 public void FromXml (SecurityElement element)
97 throw new ArgumentNullException ("element");
99 if (element.Tag != "ApplicationTrust")
100 throw new ArgumentException ("element");
102 string s = element.Attribute ("FullName");
104 _appid = new ApplicationIdentity (s);
108 _defaultPolicy = null;
109 SecurityElement defaultGrant = element.SearchForChildByTag ("DefaultGrant");
110 if (defaultGrant != null) {
111 for (int i=0; i < defaultGrant.Children.Count; i++) {
112 SecurityElement se = (defaultGrant.Children [i] as SecurityElement);
113 if (se.Tag == "PolicyStatement") {
114 DefaultGrantSet.FromXml (se, null);
120 if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
123 if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
127 SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
129 s = xtra.Attribute ("Data");
131 byte[] data = CryptoConvert.FromHex (s);
132 using (MemoryStream ms = new MemoryStream (data)) {
133 BinaryFormatter bf = new BinaryFormatter ();
134 _xtranfo = bf.Deserialize (ms);
140 public SecurityElement ToXml ()
142 SecurityElement se = new SecurityElement ("ApplicationTrust");
143 se.AddAttribute ("version", "1");
145 if (_appid != null) {
146 se.AddAttribute ("FullName", _appid.FullName);
150 se.AddAttribute ("TrustedToRun", "true");
154 se.AddAttribute ("Persist", "true");
157 SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
158 defaultGrant.AddChild (DefaultGrantSet.ToXml ());
159 se.AddChild (defaultGrant);
161 if (_xtranfo != null) {
163 using (MemoryStream ms = new MemoryStream ()) {
164 BinaryFormatter bf = new BinaryFormatter ();
165 bf.Serialize (ms, _xtranfo);
166 data = ms.ToArray ();
168 SecurityElement xtra = new SecurityElement ("ExtraInfo");
169 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
178 private PolicyStatement GetDefaultGrantSet ()
180 PermissionSet ps = new PermissionSet (PermissionState.None);
181 return new PolicyStatement (ps);