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.
34 using System.Collections;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using System.Security.Cryptography.X509Certificates;
39 namespace System.Security.Policy {
42 [MonoTODO ("Fix serialization compatibility with MS.NET")]
43 public sealed class Evidence : ICollection, IEnumerable {
46 private ArrayList hostEvidenceList;
47 private ArrayList assemblyEvidenceList;
48 private int _hashCode;
52 hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
53 assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
56 public Evidence (Evidence evidence) : this ()
62 public Evidence (object[] hostEvidence, object[] assemblyEvidence) : this ()
64 if (null != hostEvidence)
65 hostEvidenceList.AddRange (hostEvidence);
66 if (null != assemblyEvidence)
67 assemblyEvidenceList.AddRange (assemblyEvidence);
76 return (hostEvidenceList.Count + assemblyEvidenceList.Count);
80 public bool IsReadOnly {
84 public bool IsSynchronized {
88 // LAMESPEC: Always TRUE (not FALSE)
94 get { return _locked; }
96 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
101 public object SyncRoot {
109 public void AddAssembly (object id)
111 assemblyEvidenceList.Add (id);
115 public void AddHost (object id)
118 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
120 hostEvidenceList.Add (id);
127 hostEvidenceList.Clear ();
128 assemblyEvidenceList.Clear ();
133 public void CopyTo (Array array, int index)
135 if (hostEvidenceList.Count > 0)
136 hostEvidenceList.CopyTo (array, index);
137 if (assemblyEvidenceList.Count > 0)
138 assemblyEvidenceList.CopyTo (array, index + hostEvidenceList.Count);
142 public override bool Equals (object obj)
146 Evidence e = (obj as Evidence);
150 if (hostEvidenceList.Count != e.hostEvidenceList.Count)
152 if (assemblyEvidenceList.Count != e.assemblyEvidenceList.Count)
155 for (int i = 0; i < hostEvidenceList.Count; i++) {
157 for (int j = 0; j < e.hostEvidenceList.Count; i++) {
158 if (hostEvidenceList [i].Equals (e.hostEvidenceList [j])) {
166 for (int i = 0; i < assemblyEvidenceList.Count; i++) {
168 for (int j = 0; j < e.assemblyEvidenceList.Count; i++) {
169 if (assemblyEvidenceList [i].Equals (e.assemblyEvidenceList [j])) {
182 public IEnumerator GetEnumerator ()
184 return new EvidenceEnumerator (hostEvidenceList.GetEnumerator (),
185 assemblyEvidenceList.GetEnumerator ());
188 public IEnumerator GetAssemblyEnumerator ()
190 return assemblyEvidenceList.GetEnumerator ();
194 public override int GetHashCode ()
196 // kind of long so we cache it
197 if (_hashCode == 0) {
198 for (int i = 0; i < hostEvidenceList.Count; i++)
199 _hashCode ^= hostEvidenceList [i].GetHashCode ();
200 for (int i = 0; i < assemblyEvidenceList.Count; i++)
201 _hashCode ^= assemblyEvidenceList [i].GetHashCode ();
207 public IEnumerator GetHostEnumerator ()
209 return hostEvidenceList.GetEnumerator ();
212 public void Merge (Evidence evidence)
214 if ((evidence != null) && (evidence.Count > 0)) {
215 IEnumerator hostenum = evidence.GetHostEnumerator ();
216 while (hostenum.MoveNext ()) {
217 AddHost (hostenum.Current);
220 IEnumerator assemblyenum = evidence.GetAssemblyEnumerator ();
221 while (assemblyenum.MoveNext ()) {
222 AddAssembly (assemblyenum.Current);
229 public void RemoveType (Type t)
231 for (int i = hostEvidenceList.Count; i >= 0; i--) {
232 if (hostEvidenceList.GetType () == t) {
233 hostEvidenceList.RemoveAt (i);
237 for (int i = assemblyEvidenceList.Count; i >= 0; i--) {
238 if (assemblyEvidenceList.GetType () == t) {
239 assemblyEvidenceList.RemoveAt (i);
246 // this avoid us to build all evidences from the runtime
247 // (i.e. multiple unmanaged->managed calls) and also allows
248 // to delay their creation until (if) needed
249 static internal Evidence GetDefaultHostEvidence (Assembly a)
251 Evidence e = new Evidence ();
252 string aname = a.CodeBase;
254 // by default all assembly have the Zone, Url and Hash evidences
255 e.AddHost (Zone.CreateFromUrl (aname));
256 e.AddHost (new Url (aname));
257 e.AddHost (new Hash (a));
259 // non local files (e.g. http://) also get a Site evidence
260 if (!aname.ToUpper ().StartsWith ("FILE://")) {
261 e.AddHost (Site.CreateFromUrl (aname));
264 // strongnamed assemblies gets a StrongName evidence
265 AssemblyName an = a.GetName ();
266 byte[] pk = an.GetPublicKey ();
268 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (pk);
269 e.AddHost (new StrongName (blob, an.Name, an.Version));
272 // Authenticode(r) signed assemblies get a Publisher evidence
274 X509Certificate x509 = X509Certificate.CreateFromSignedFile (a.Location);
275 if (x509.GetHashCode () != 0) {
276 e.AddHost (new Publisher (x509));
279 catch (ArgumentException) {
280 // URI are not supported
283 // assemblies loaded from the GAC also get a Gac evidence (new in Fx 2.0)
284 if (a.GlobalAssemblyCache) {
285 e.AddHost (new Gac ());
288 // the current HostSecurityManager may add/remove some evidence
289 AppDomainManager dommgr = AppDomain.CurrentDomain.DomainManager;
290 if (dommgr != null) {
291 if ((dommgr.HostSecurityManager.Flags & HostSecurityManagerFlags.HostAssemblyEvidence) ==
292 HostSecurityManagerFlags.HostAssemblyEvidence) {
293 e = dommgr.HostSecurityManager.ProvideAssemblyEvidence (a, e);
300 private class EvidenceEnumerator : IEnumerator {
302 private IEnumerator currentEnum, hostEnum, assemblyEnum;
304 public EvidenceEnumerator (IEnumerator hostenum, IEnumerator assemblyenum)
306 this.hostEnum = hostenum;
307 this.assemblyEnum = assemblyenum;
308 currentEnum = hostEnum;
311 public bool MoveNext ()
313 bool ret = currentEnum.MoveNext ();
315 if ( !ret && hostEnum == currentEnum ) {
316 currentEnum = assemblyEnum;
317 ret = assemblyEnum.MoveNext ();
326 assemblyEnum.Reset ();
327 currentEnum = hostEnum;
330 public object Current {
332 return currentEnum.Current;