Merge pull request #1506 from akoeplinger/fix-paste-test
[mono.git] / mcs / class / corlib / System.Security.Policy / Evidence.cs
1 //
2 // System.Security.Policy.Evidence
3 //
4 // Authors:
5 //      Sean MacIsaac (macisaac@ximian.com)
6 //      Nick Drochak (ndrochak@gol.com)
7 //      Jackson Harper (Jackson@LatitudeGeo.com)
8 //      Sebastien Pouliot  <sebastien@ximian.com>
9 //
10 // (C) 2001 Ximian, Inc.
11 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
12 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Security.Permissions;
40 using System.Security.Cryptography.X509Certificates;
41 using Mono.Security.Authenticode;
42
43 namespace System.Security.Policy {
44
45         [Serializable]
46         [MonoTODO ("Serialization format not compatible with .NET")]
47         [ComVisible (true)]
48         public sealed class Evidence : ICollection, IEnumerable {
49         
50                 private bool _locked;
51                 private ArrayList hostEvidenceList;     
52                 private ArrayList assemblyEvidenceList;
53
54                 public Evidence () 
55                 {
56                 }
57
58                 public Evidence (Evidence evidence)
59                 {
60                         if (evidence != null)
61                                 Merge (evidence);       
62                 }
63
64                 [Obsolete]
65                 public Evidence (object[] hostEvidence, object[] assemblyEvidence)
66                 {
67                         if (null != hostEvidence)
68                                 HostEvidenceList.AddRange (hostEvidence);
69                         if (null != assemblyEvidence)
70                                 AssemblyEvidenceList.AddRange (assemblyEvidence);
71                 }
72                 
73                 //
74                 // Public Properties
75                 //
76         
77                 [Obsolete]
78                 public int Count {
79                         get {
80                                 int count = 0;
81                                 if (hostEvidenceList != null)
82                                         count += hostEvidenceList.Count;
83                                 if (assemblyEvidenceList!= null)
84                                         count += assemblyEvidenceList.Count;
85                                 return count;
86                         }
87                 }
88
89                 public bool IsReadOnly {
90                         get{ return false; }
91                 }
92                 
93                 public bool IsSynchronized {
94                         get { return false; }
95                 }
96
97                 public bool Locked {
98                         get { return _locked; }
99                         [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
100                         set { 
101                                 _locked = value; 
102                         }
103                 }       
104
105                 public object SyncRoot {
106                         get { return this; }
107                 }
108
109                 internal ArrayList HostEvidenceList {
110                         get {
111                                 if (hostEvidenceList == null)
112                                         hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
113                                 return hostEvidenceList;
114                         }
115                 }
116
117                 internal ArrayList AssemblyEvidenceList {
118                         get {
119                                 if (assemblyEvidenceList == null)
120                                         assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
121                                 return assemblyEvidenceList;
122                         }
123                 }
124
125                 //
126                 // Public Methods
127                 //
128
129                 [Obsolete]
130                 public void AddAssembly (object id) 
131                 {
132                         AssemblyEvidenceList.Add (id);
133                 }
134
135                 [Obsolete]
136                 public void AddHost (object id) 
137                 {
138                         if (_locked && SecurityManager.SecurityEnabled) {
139                                 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
140                         }
141                         HostEvidenceList.Add (id);
142                 }
143
144                 [ComVisible (false)]
145                 public void Clear ()
146                 {
147                         if (hostEvidenceList != null)
148                                 hostEvidenceList.Clear ();
149                         if (assemblyEvidenceList != null)
150                                 assemblyEvidenceList.Clear ();
151                 }
152
153                 [Obsolete]
154                 public void CopyTo (Array array, int index) 
155                 {
156                         int hc = 0;
157                         if (hostEvidenceList != null) {
158                                 hc = hostEvidenceList.Count;
159                                 if (hc > 0)
160                                         hostEvidenceList.CopyTo (array, index);
161                         }
162                         if ((assemblyEvidenceList != null) && (assemblyEvidenceList.Count > 0))
163                                 assemblyEvidenceList.CopyTo (array, index + hc);
164                 }
165
166
167                 [Obsolete]
168                 public IEnumerator GetEnumerator () 
169                 {
170                         IEnumerator he = null;
171                         if (hostEvidenceList != null)
172                                 he = hostEvidenceList.GetEnumerator ();
173                         IEnumerator ae = null;
174                         if (assemblyEvidenceList != null)
175                                 ae = assemblyEvidenceList.GetEnumerator ();
176                         return new EvidenceEnumerator (he, ae);
177                 }
178
179                 public IEnumerator GetAssemblyEnumerator () 
180                 {
181                         return AssemblyEvidenceList.GetEnumerator ();
182                 }
183
184                 public IEnumerator GetHostEnumerator () 
185                 {
186                         return HostEvidenceList.GetEnumerator ();
187                 }
188
189                 public void Merge (Evidence evidence) 
190                 {
191                         if ((evidence != null) && (evidence.Count > 0)) {
192                                 if (evidence.hostEvidenceList != null) {
193                                         foreach (object o in evidence.hostEvidenceList)
194                                                 AddHost (o);
195                                 }
196                                 if (evidence.assemblyEvidenceList != null) {
197                                         foreach (object o in evidence.assemblyEvidenceList)
198                                                 AddAssembly (o);
199                                 }
200                         }
201                 }
202
203                 [ComVisible (false)]
204                 public void RemoveType (Type t)
205                 {
206                         for (int i = hostEvidenceList.Count; i >= 0; i--) {
207                                 if (hostEvidenceList.GetType () == t) {
208                                         hostEvidenceList.RemoveAt (i);
209                                 }
210                         }
211                         for (int i = assemblyEvidenceList.Count; i >= 0; i--) {
212                                 if (assemblyEvidenceList.GetType () == t) {
213                                         assemblyEvidenceList.RemoveAt (i);
214                                 }
215                         }
216                 }
217
218                 // Use an icall to avoid multiple file i/o to detect the 
219                 // "possible" presence of an Authenticode signature
220                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
221                 static extern bool IsAuthenticodePresent (Assembly a);
222 #if NET_2_1
223                 static internal Evidence GetDefaultHostEvidence (Assembly a)
224                 {
225                         return new Evidence ();
226                 }
227 #else
228                 // this avoid us to build all evidences from the runtime
229                 // (i.e. multiple unmanaged->managed calls) and also allows
230                 // to delay their creation until (if) needed
231                 [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
232                 static internal Evidence GetDefaultHostEvidence (Assembly a) 
233                 {
234                         Evidence e = new Evidence ();
235                         string aname = a.EscapedCodeBase;
236
237                         // by default all assembly have the Zone, Url and Hash evidences
238                         e.AddHost (Zone.CreateFromUrl (aname));
239                         e.AddHost (new Url (aname));
240                         e.AddHost (new Hash (a));
241
242                         // non local files (e.g. http://) also get a Site evidence
243                         if (String.Compare ("FILE://", 0, aname, 0, 7, true, CultureInfo.InvariantCulture) != 0) {
244                                 e.AddHost (Site.CreateFromUrl (aname));
245                         }
246
247                         // strongnamed assemblies gets a StrongName evidence
248                         AssemblyName an = a.UnprotectedGetName ();
249                         byte[] pk = an.GetPublicKey ();
250                         if ((pk != null) && (pk.Length > 0)) {
251                                 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (pk);
252                                 e.AddHost (new StrongName (blob, an.Name, an.Version));
253                         }
254
255                         // Authenticode(r) signed assemblies get a Publisher evidence
256                         if (IsAuthenticodePresent (a)) {
257                                 // Note: The certificate is part of the evidences even if it is not trusted!
258                                 // so we can't call X509Certificate.CreateFromSignedFile
259                                 AuthenticodeDeformatter ad = new AuthenticodeDeformatter (a.Location);
260                                 if (ad.SigningCertificate != null) {
261                                         X509Certificate x509 = new X509Certificate (ad.SigningCertificate.RawData);
262                                         if (x509.GetHashCode () != 0) {
263                                                 e.AddHost (new Publisher (x509));
264                                         }
265                                 }
266                         }
267                         // assemblies loaded from the GAC also get a Gac evidence (new in Fx 2.0)
268                         if (a.GlobalAssemblyCache) {
269                                 e.AddHost (new GacInstalled ());
270                         }
271
272                         // the current HostSecurityManager may add/remove some evidence
273                         AppDomainManager dommgr = AppDomain.CurrentDomain.DomainManager;
274                         if (dommgr != null) {
275                                 if ((dommgr.HostSecurityManager.Flags & HostSecurityManagerOptions.HostAssemblyEvidence) ==
276                                         HostSecurityManagerOptions.HostAssemblyEvidence) {
277                                         e = dommgr.HostSecurityManager.ProvideAssemblyEvidence (a, e);
278                                 }
279                         }
280
281                         return e;
282                 }
283
284 #endif // NET_2_1
285
286                 private class EvidenceEnumerator : IEnumerator {
287                         
288                         private IEnumerator currentEnum, hostEnum, assemblyEnum;                
289         
290                         public EvidenceEnumerator (IEnumerator hostenum, IEnumerator assemblyenum) 
291                         {
292                                 this.hostEnum = hostenum;
293                                 this.assemblyEnum = assemblyenum;
294                                 currentEnum = hostEnum;         
295                         }
296
297                         public bool MoveNext () 
298                         {
299                                 if (currentEnum == null)
300                                         return false;
301
302                                 bool ret = currentEnum.MoveNext ();
303                                 
304                                 if (!ret && (hostEnum == currentEnum) && (assemblyEnum != null)) {
305                                         currentEnum = assemblyEnum;
306                                         ret = assemblyEnum.MoveNext ();
307                                 }
308
309                                 return ret;
310                         }
311
312                         public void Reset () 
313                         {
314                                 if (hostEnum != null) {
315                                         hostEnum.Reset ();
316                                         currentEnum = hostEnum;
317                                 } else {
318                                         currentEnum = assemblyEnum;
319                                 }
320                                 if (assemblyEnum != null)
321                                         assemblyEnum.Reset ();
322                         }
323
324                         public object Current {
325                                 get {
326                                         return currentEnum.Current;
327                                 }
328                         }
329                 }
330         }
331 }
332