2 // System.Security.Policy.Evidence
5 // Sean MacIsaac (macisaac@ximian.com)
6 // Nick Drochak (ndrochak@gol.com)
7 // Jackson Harper (Jackson@LatitudeGeo.com)
8 // Sebastien Pouliot <sebastien@ximian.com>
10 // (C) 2001 Ximian, Inc.
11 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Reflection;
37 using System.Security.Permissions;
38 using System.Security.Cryptography.X509Certificates;
40 namespace System.Security.Policy {
43 [MonoTODO ("Fix serialization compatibility with MS.NET")]
44 public sealed class Evidence : ICollection, IEnumerable {
47 private ArrayList hostEvidenceList;
48 private ArrayList assemblyEvidenceList;
49 private int _hashCode;
53 hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
54 assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
57 public Evidence (Evidence evidence) : this ()
63 public Evidence (object[] hostEvidence, object[] assemblyEvidence) : this ()
65 if (null != hostEvidence)
66 hostEvidenceList.AddRange (hostEvidence);
67 if (null != assemblyEvidence)
68 assemblyEvidenceList.AddRange (assemblyEvidence);
77 return (hostEvidenceList.Count + assemblyEvidenceList.Count);
81 public bool IsReadOnly {
85 // LAMESPEC: Always TRUE (not FALSE)
86 public bool IsSynchronized {
91 get { return _locked; }
93 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
98 public object SyncRoot {
106 public void AddAssembly (object id)
108 assemblyEvidenceList.Add (id);
112 public void AddHost (object id)
115 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
117 hostEvidenceList.Add (id);
124 hostEvidenceList.Clear ();
125 assemblyEvidenceList.Clear ();
130 public void CopyTo (Array array, int index)
132 if (hostEvidenceList.Count > 0)
133 hostEvidenceList.CopyTo (array, index);
134 if (assemblyEvidenceList.Count > 0)
135 assemblyEvidenceList.CopyTo (array, index + hostEvidenceList.Count);
139 public override bool Equals (object obj)
143 Evidence e = (obj as Evidence);
147 if (hostEvidenceList.Count != e.hostEvidenceList.Count)
149 if (assemblyEvidenceList.Count != e.assemblyEvidenceList.Count)
152 for (int i = 0; i < hostEvidenceList.Count; i++) {
154 for (int j = 0; j < e.hostEvidenceList.Count; i++) {
155 if (hostEvidenceList [i].Equals (e.hostEvidenceList [j])) {
163 for (int i = 0; i < assemblyEvidenceList.Count; i++) {
165 for (int j = 0; j < e.assemblyEvidenceList.Count; i++) {
166 if (assemblyEvidenceList [i].Equals (e.assemblyEvidenceList [j])) {
179 public IEnumerator GetEnumerator ()
181 return new EvidenceEnumerator (hostEvidenceList.GetEnumerator (),
182 assemblyEvidenceList.GetEnumerator ());
185 public IEnumerator GetAssemblyEnumerator ()
187 return assemblyEvidenceList.GetEnumerator ();
191 public override int GetHashCode ()
193 // kind of long so we cache it
194 if (_hashCode == 0) {
195 for (int i = 0; i < hostEvidenceList.Count; i++)
196 _hashCode ^= hostEvidenceList [i].GetHashCode ();
197 for (int i = 0; i < assemblyEvidenceList.Count; i++)
198 _hashCode ^= assemblyEvidenceList [i].GetHashCode ();
204 public IEnumerator GetHostEnumerator ()
206 return hostEvidenceList.GetEnumerator ();
209 public void Merge (Evidence evidence)
211 IEnumerator hostenum, assemblyenum;
213 hostenum = evidence.GetHostEnumerator ();
214 while( hostenum.MoveNext () ) {
215 AddHost (hostenum.Current);
218 assemblyenum = evidence.GetAssemblyEnumerator ();
219 while( assemblyenum.MoveNext () ) {
220 AddAssembly (assemblyenum.Current);
225 public void RemoveType (Type t)
227 for (int i = hostEvidenceList.Count; i >= 0; i--) {
228 if (hostEvidenceList.GetType () == t) {
229 hostEvidenceList.RemoveAt (i);
233 for (int i = assemblyEvidenceList.Count; i >= 0; i--) {
234 if (assemblyEvidenceList.GetType () == t) {
235 assemblyEvidenceList.RemoveAt (i);
242 // this avoid us to build all evidences from the runtime
243 // (i.e. multiple unmanaged->managed calls) and also allows
244 // to delay their creation until (if) needed
245 static internal Evidence GetDefaultHostEvidence (Assembly a)
247 Evidence e = new Evidence ();
248 string aname = a.CodeBase;
250 // by default all assembly have the Zone, Url and Hash evidences
251 e.AddHost (Zone.CreateFromUrl (aname));
252 e.AddHost (new Url (aname));
253 e.AddHost (new Hash (a));
255 // non local files (e.g. http://) also get a Site evidence
256 if (!aname.ToUpper ().StartsWith ("FILE://")) {
257 e.AddHost (Site.CreateFromUrl (aname));
260 // strongnamed assemblies gets a StrongName evidence
261 AssemblyName an = a.GetName ();
262 byte[] pk = an.GetPublicKey ();
264 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (pk);
265 e.AddHost (new StrongName (blob, an.Name, an.Version));
268 // Authenticode(r) signed assemblies get a Publisher evidence
270 X509Certificate x509 = X509Certificate.CreateFromSignedFile (a.Location);
271 if (x509.GetHashCode () != 0) {
272 e.AddHost (new Publisher (x509));
275 catch (ArgumentException) {
276 // URI are not supported
279 // assemblies loaded from the GAC also get a Gac evidence (new in Fx 2.0)
280 if (a.GlobalAssemblyCache) {
281 e.AddHost (new Gac ());
284 // the current HostSecurityManager may add/remove some evidence
285 AppDomainManager dommgr = AppDomain.CurrentDomain.DomainManager;
286 if (dommgr != null) {
287 if ((dommgr.HostSecurityManager.Flags & HostSecurityManagerFlags.HostAssemblyEvidence) ==
288 HostSecurityManagerFlags.HostAssemblyEvidence) {
289 e = dommgr.HostSecurityManager.ProvideAssemblyEvidence (a, e);
296 private class EvidenceEnumerator : IEnumerator {
298 private IEnumerator currentEnum, hostEnum, assemblyEnum;
300 public EvidenceEnumerator (IEnumerator hostenum, IEnumerator assemblyenum)
302 this.hostEnum = hostenum;
303 this.assemblyEnum = assemblyenum;
304 currentEnum = hostEnum;
307 public bool MoveNext ()
309 bool ret = currentEnum.MoveNext ();
311 if ( !ret && hostEnum == currentEnum ) {
312 currentEnum = assemblyEnum;
313 ret = assemblyEnum.MoveNext ();
322 assemblyEnum.Reset ();
323 currentEnum = hostEnum;
326 public object Current {
328 return currentEnum.Current;