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