4 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // StrongNameIdentityPermission.cs
9 // <OWNER>[....]</OWNER>
12 namespace System.Security.Permissions
15 #if FEATURE_CAS_POLICY
16 using SecurityElement = System.Security.SecurityElement;
17 #endif // FEATURE_CAS_POLICY
18 using System.Security.Util;
20 using String = System.String;
21 using Version = System.Version;
22 using System.Security.Policy;
23 using System.Collections;
24 using System.Collections.Generic;
25 using System.Globalization;
26 using System.Diagnostics.Contracts;
28 // The only difference between this class and System.Security.Policy.StrongName is that this one
29 // allows m_name to be null. We should merge this class with System.Security.Policy.StrongName
31 sealed internal class StrongName2
33 public StrongNamePublicKeyBlob m_publicKeyBlob;
35 public Version m_version;
37 public StrongName2(StrongNamePublicKeyBlob publicKeyBlob, String name, Version version)
39 m_publicKeyBlob = publicKeyBlob;
44 public StrongName2 Copy()
46 return new StrongName2(m_publicKeyBlob, m_name, m_version);
49 public bool IsSubsetOf(StrongName2 target)
51 // This StrongName2 is a subset of the target if it's public key blob is null no matter what
52 if (this.m_publicKeyBlob == null)
55 // Subsets are always false if the public key blobs do not match
56 if (!this.m_publicKeyBlob.Equals( target.m_publicKeyBlob ))
59 // We use null in strings to represent the "Anything" state.
60 // Therefore, the logic to detect an individual subset is:
62 // 1. If the this string is null ("Anything" is a subset of any other).
63 // 2. If the this string and target string are the same (equality is sufficient for a subset).
65 // The logic is reversed here to discover things that are not subsets.
66 if (this.m_name != null)
68 if (target.m_name == null || !System.Security.Policy.StrongName.CompareNames( target.m_name, this.m_name ))
72 if ((Object) this.m_version != null)
74 if ((Object) target.m_version == null ||
75 target.m_version.CompareTo( this.m_version ) != 0)
84 public StrongName2 Intersect(StrongName2 target)
86 if (target.IsSubsetOf( this ))
88 else if (this.IsSubsetOf( target ))
94 public bool Equals(StrongName2 target)
96 if (!target.IsSubsetOf(this))
98 if (!this.IsSubsetOf(target))
106 [System.Runtime.InteropServices.ComVisible(true)]
108 sealed public class StrongNameIdentityPermission : CodeAccessPermission, IBuiltInPermission
110 //------------------------------------------------------
112 // PRIVATE STATE DATA
114 //------------------------------------------------------
116 private bool m_unrestricted;
117 private StrongName2[] m_strongNames;
119 //------------------------------------------------------
121 // PUBLIC CONSTRUCTORS
123 //------------------------------------------------------
126 public StrongNameIdentityPermission(PermissionState state)
128 if (state == PermissionState.Unrestricted)
130 m_unrestricted = true;
132 else if (state == PermissionState.None)
134 m_unrestricted = false;
138 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
142 public StrongNameIdentityPermission( StrongNamePublicKeyBlob blob, String name, Version version )
145 throw new ArgumentNullException( "blob" );
146 if (name != null && name.Equals( "" ))
147 throw new ArgumentException( Environment.GetResourceString( "Argument_EmptyStrongName" ) );
148 Contract.EndContractBlock();
149 m_unrestricted = false;
150 m_strongNames = new StrongName2[1];
151 m_strongNames[0] = new StrongName2(blob, name, version);
155 //------------------------------------------------------
157 // PUBLIC ACCESSOR METHODS
159 //------------------------------------------------------
161 public StrongNamePublicKeyBlob PublicKey
166 throw new ArgumentNullException( "PublicKey" );
167 Contract.EndContractBlock();
168 m_unrestricted = false;
169 if(m_strongNames != null && m_strongNames.Length == 1)
170 m_strongNames[0].m_publicKeyBlob = value;
173 m_strongNames = new StrongName2[1];
174 m_strongNames[0] = new StrongName2(value, "", new Version());
180 if(m_strongNames == null || m_strongNames.Length == 0)
182 if(m_strongNames.Length > 1)
183 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
184 return m_strongNames[0].m_publicKeyBlob;
192 if (value != null && value.Length == 0)
193 throw new ArgumentException( Environment.GetResourceString("Argument_EmptyName" ));
194 Contract.EndContractBlock();
195 m_unrestricted = false;
196 if(m_strongNames != null && m_strongNames.Length == 1)
197 m_strongNames[0].m_name = value;
200 m_strongNames = new StrongName2[1];
201 m_strongNames[0] = new StrongName2(null, value, new Version());
207 if(m_strongNames == null || m_strongNames.Length == 0)
209 if(m_strongNames.Length > 1)
210 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
211 return m_strongNames[0].m_name;
215 public Version Version
219 m_unrestricted = false;
220 if(m_strongNames != null && m_strongNames.Length == 1)
221 m_strongNames[0].m_version = value;
224 m_strongNames = new StrongName2[1];
225 m_strongNames[0] = new StrongName2(null, "", value);
231 if(m_strongNames == null || m_strongNames.Length == 0)
232 return new Version();
233 if(m_strongNames.Length > 1)
234 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
235 return m_strongNames[0].m_version;
239 //------------------------------------------------------
241 // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
243 //------------------------------------------------------
245 //------------------------------------------------------
247 // CODEACCESSPERMISSION IMPLEMENTATION
249 //------------------------------------------------------
251 //------------------------------------------------------
253 // IPERMISSION IMPLEMENTATION
255 //------------------------------------------------------
258 public override IPermission Copy()
260 StrongNameIdentityPermission perm = new StrongNameIdentityPermission(PermissionState.None);
261 perm.m_unrestricted = this.m_unrestricted;
262 if(this.m_strongNames != null)
264 perm.m_strongNames = new StrongName2[this.m_strongNames.Length];
266 for(n = 0; n < this.m_strongNames.Length; n++)
267 perm.m_strongNames[n] = this.m_strongNames[n].Copy();
272 public override bool IsSubsetOf(IPermission target)
278 if(m_strongNames == null)
280 if(m_strongNames.Length == 0)
284 StrongNameIdentityPermission that = target as StrongNameIdentityPermission;
286 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
287 if(that.m_unrestricted)
291 if(this.m_strongNames != null)
293 foreach(StrongName2 snThis in m_strongNames)
296 if(that.m_strongNames != null)
298 foreach(StrongName2 snThat in that.m_strongNames)
300 if(snThis.IsSubsetOf(snThat))
316 public override IPermission Intersect(IPermission target)
320 StrongNameIdentityPermission that = target as StrongNameIdentityPermission;
322 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
323 if(this.m_unrestricted && that.m_unrestricted)
325 StrongNameIdentityPermission res = new StrongNameIdentityPermission(PermissionState.None);
326 res.m_unrestricted = true;
329 if(this.m_unrestricted)
331 if(that.m_unrestricted)
333 if(this.m_strongNames == null || that.m_strongNames == null || this.m_strongNames.Length == 0 || that.m_strongNames.Length == 0)
335 List<StrongName2> alStrongNames = new List<StrongName2>();
336 foreach(StrongName2 snThis in this.m_strongNames)
338 foreach(StrongName2 snThat in that.m_strongNames)
340 StrongName2 snInt = (StrongName2)snThis.Intersect(snThat);
342 alStrongNames.Add(snInt);
345 if(alStrongNames.Count == 0)
347 StrongNameIdentityPermission result = new StrongNameIdentityPermission(PermissionState.None);
348 result.m_strongNames = alStrongNames.ToArray();
352 public override IPermission Union(IPermission target)
356 if((this.m_strongNames == null || this.m_strongNames.Length == 0) && !this.m_unrestricted)
360 StrongNameIdentityPermission that = target as StrongNameIdentityPermission;
362 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
363 if(this.m_unrestricted || that.m_unrestricted)
365 StrongNameIdentityPermission res = new StrongNameIdentityPermission(PermissionState.None);
366 res.m_unrestricted = true;
369 if (this.m_strongNames == null || this.m_strongNames.Length == 0)
371 if(that.m_strongNames == null || that.m_strongNames.Length == 0)
375 if(that.m_strongNames == null || that.m_strongNames.Length == 0)
377 List<StrongName2> alStrongNames = new List<StrongName2>();
378 foreach(StrongName2 snThis in this.m_strongNames)
379 alStrongNames.Add(snThis);
380 foreach(StrongName2 snThat in that.m_strongNames)
383 foreach(StrongName2 sn in alStrongNames)
385 if(snThat.Equals(sn))
392 alStrongNames.Add(snThat);
394 StrongNameIdentityPermission result = new StrongNameIdentityPermission(PermissionState.None);
395 result.m_strongNames = alStrongNames.ToArray();
399 #if FEATURE_CAS_POLICY
400 public override void FromXml(SecurityElement e)
402 m_unrestricted = false;
403 m_strongNames = null;
404 CodeAccessPermission.ValidateElement( e, this );
405 String unr = e.Attribute( "Unrestricted" );
406 if(unr != null && String.Compare(unr, "true", StringComparison.OrdinalIgnoreCase) == 0)
408 m_unrestricted = true;
411 String elBlob = e.Attribute("PublicKeyBlob");
412 String elName = e.Attribute("Name");
413 String elVersion = e.Attribute("AssemblyVersion");
415 List<StrongName2> al = new List<StrongName2>();
416 if(elBlob != null || elName != null || elVersion != null)
418 sn = new StrongName2(
419 (elBlob == null ? null : new StrongNamePublicKeyBlob(elBlob)),
421 (elVersion == null ? null : new Version(elVersion)));
424 ArrayList alChildren = e.Children;
425 if(alChildren != null)
427 foreach(SecurityElement child in alChildren)
429 elBlob = child.Attribute("PublicKeyBlob");
430 elName = child.Attribute("Name");
431 elVersion = child.Attribute("AssemblyVersion");
432 if(elBlob != null || elName != null || elVersion != null)
434 sn = new StrongName2(
435 (elBlob == null ? null : new StrongNamePublicKeyBlob(elBlob)),
437 (elVersion == null ? null : new Version(elVersion)));
443 m_strongNames = al.ToArray();
446 public override SecurityElement ToXml()
448 SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.StrongNameIdentityPermission" );
450 esd.AddAttribute( "Unrestricted", "true" );
451 else if (m_strongNames != null)
453 if (m_strongNames.Length == 1)
455 if (m_strongNames[0].m_publicKeyBlob != null)
456 esd.AddAttribute("PublicKeyBlob", Hex.EncodeHexString(m_strongNames[0].m_publicKeyBlob.PublicKey));
457 if (m_strongNames[0].m_name != null)
458 esd.AddAttribute("Name", m_strongNames[0].m_name);
459 if ((Object)m_strongNames[0].m_version != null)
460 esd.AddAttribute("AssemblyVersion", m_strongNames[0].m_version.ToString());
465 for(n = 0; n < m_strongNames.Length; n++)
467 SecurityElement child = new SecurityElement("StrongName");
468 if (m_strongNames[n].m_publicKeyBlob != null)
469 child.AddAttribute("PublicKeyBlob", Hex.EncodeHexString(m_strongNames[n].m_publicKeyBlob.PublicKey));
470 if (m_strongNames[n].m_name != null)
471 child.AddAttribute("Name", m_strongNames[n].m_name);
472 if ((Object)m_strongNames[n].m_version != null)
473 child.AddAttribute("AssemblyVersion", m_strongNames[n].m_version.ToString());
480 #endif // FEATURE_CAS_POLICY
483 int IBuiltInPermission.GetTokenIndex()
485 return StrongNameIdentityPermission.GetTokenIndex();
488 internal static int GetTokenIndex()
490 return BuiltInPermissionIndex.StrongNameIdentityPermissionIndex;