In .:
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationTrust.cs
1 //
2 // System.Security.Policy.ApplicationTrust class
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 #if NET_2_0
30
31 using System.IO;
32 using System.Runtime.InteropServices;
33 using System.Runtime.Serialization.Formatters.Binary;
34 using System.Security.Permissions;
35
36 using Mono.Security.Cryptography;
37
38 namespace System.Security.Policy {
39
40         [ComVisible (true)]
41         public sealed class ApplicationTrust : ISecurityEncodable {
42
43                 private ApplicationIdentity _appid;
44                 private PolicyStatement _defaultPolicy;
45                 private object _xtranfo;
46                 private bool _trustrun;
47                 private bool _persist;
48
49                 public ApplicationTrust ()
50                 {
51                 }
52
53                 public ApplicationTrust (ApplicationIdentity applicationIdentity)
54                 {
55                         if (applicationIdentity == null)
56                                 throw new ArgumentNullException ("applicationIdentity");
57                         _appid = applicationIdentity;
58                 }
59
60                 public ApplicationIdentity ApplicationIdentity {
61                         get { return _appid; }
62                         set {
63                                 if (value == null)
64                                         throw new ArgumentNullException ("ApplicationIdentity");
65                                 _appid = value;
66                         }
67                 }
68
69                 public PolicyStatement DefaultGrantSet {
70                         get {
71                                 if (_defaultPolicy == null)
72                                         _defaultPolicy = GetDefaultGrantSet ();
73
74                                 return _defaultPolicy;
75                         }
76                         set { _defaultPolicy = value; }
77                 }
78
79                 public object ExtraInfo {
80                         get { return _xtranfo; }
81                         set { _xtranfo = value; }
82                 }
83
84                 public bool IsApplicationTrustedToRun {
85                         get { return _trustrun; }
86                         set { _trustrun = value; }
87                 }
88
89                 public bool Persist {
90                         get { return _persist; }
91                         set { _persist = value; }
92                 }
93
94                 public void FromXml (SecurityElement element) 
95                 {
96                         if (element == null)
97                                 throw new ArgumentNullException ("element");
98
99                         if (element.Tag != "ApplicationTrust")
100                                 throw new ArgumentException ("element");
101
102                         string s = element.Attribute ("FullName");
103                         if (s != null)
104                                 _appid = new ApplicationIdentity (s);
105                         else
106                                 _appid = null;
107
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);
115                                                 break;
116                                         }
117                                 }
118                         }
119
120                         if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
121                                 _trustrun = false;
122
123                         if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
124                                 _persist = false;
125
126                         _xtranfo = null;
127                         SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
128                         if (xtra != null) {
129                                 s = xtra.Attribute ("Data");
130                                 if (s != null) {
131                                         byte[] data = CryptoConvert.FromHex (s);
132                                         using (MemoryStream ms = new MemoryStream (data)) {
133                                                 BinaryFormatter bf = new BinaryFormatter ();
134                                                 _xtranfo = bf.Deserialize (ms);
135                                         }
136                                 }
137                         }
138                 }
139
140                 public SecurityElement ToXml () 
141                 {
142                         SecurityElement se = new SecurityElement ("ApplicationTrust");
143                         se.AddAttribute ("version", "1");
144
145                         if (_appid != null) {
146                                 se.AddAttribute ("FullName", _appid.FullName);
147                         }
148
149                         if (_trustrun) {
150                                 se.AddAttribute ("TrustedToRun", "true");
151                         }
152
153                         if (_persist) {
154                                 se.AddAttribute ("Persist", "true");
155                         }
156
157                         SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
158                         defaultGrant.AddChild (DefaultGrantSet.ToXml ());
159                         se.AddChild (defaultGrant);
160
161                         if (_xtranfo != null) {
162                                 byte[] data = null;
163                                 using (MemoryStream ms = new MemoryStream ()) {
164                                         BinaryFormatter bf = new BinaryFormatter ();
165                                         bf.Serialize (ms, _xtranfo);
166                                         data = ms.ToArray ();
167                                 }
168                                 SecurityElement xtra = new SecurityElement ("ExtraInfo");
169                                 xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
170                                 se.AddChild (xtra);
171                         }
172
173                         return se;
174                 }
175
176                 // internal stuff
177
178                 private PolicyStatement GetDefaultGrantSet ()
179                 {
180                         PermissionSet ps = new PermissionSet (PermissionState.None);
181                         return new PolicyStatement (ps);
182                 }
183         }
184 }
185
186 #endif