3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>[....]</OWNER>
12 // StrongName is an IIdentity representing strong names.
15 namespace System.Security.Policy {
17 using System.Reflection;
18 using System.Security.Util;
19 using System.Security.Permissions;
20 using System.Diagnostics.Contracts;
21 using CultureInfo = System.Globalization.CultureInfo;
24 [System.Runtime.InteropServices.ComVisible(true)]
25 public sealed class StrongName : EvidenceBase, IIdentityPermissionFactory, IDelayEvaluatedEvidence
27 private StrongNamePublicKeyBlob m_publicKeyBlob;
28 private String m_name;
29 private Version m_version;
31 // Delay evaluated evidence is for policy resolution only, so it doesn't make sense to save that
32 // state away and then try to evaluate the strong name later.
34 private RuntimeAssembly m_assembly = null;
37 private bool m_wasUsed = false;
39 internal StrongName() {}
41 public StrongName( StrongNamePublicKeyBlob blob, String name, Version version ) : this(blob, name, version, null)
45 internal StrongName(StrongNamePublicKeyBlob blob, String name, Version version, Assembly assembly)
48 throw new ArgumentNullException("name");
49 if (String.IsNullOrEmpty(name))
50 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyStrongName"));
53 throw new ArgumentNullException("blob");
56 throw new ArgumentNullException("version");
57 Contract.EndContractBlock();
59 RuntimeAssembly rtAssembly = assembly as RuntimeAssembly;
60 if (assembly != null && rtAssembly == null)
61 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"), "assembly");
63 m_publicKeyBlob = blob;
66 m_assembly = rtAssembly;
69 public StrongNamePublicKeyBlob PublicKey
73 return m_publicKeyBlob;
85 public Version Version
93 bool IDelayEvaluatedEvidence.IsVerified
95 [System.Security.SecurityCritical] // auto-generated
98 #if FEATURE_CAS_POLICY
99 return m_assembly != null ? m_assembly.IsStrongNameVerified : true;
100 #else // !FEATURE_CAS_POLICY
102 #endif // FEATURE_CAS_POLICY
106 bool IDelayEvaluatedEvidence.WasUsed
108 get { return m_wasUsed; }
111 void IDelayEvaluatedEvidence.MarkUsed()
116 internal static bool CompareNames( String asmName, String mcName )
118 if (mcName.Length > 0 && mcName[mcName.Length-1] == '*' && mcName.Length - 1 <= asmName.Length)
119 return String.Compare( mcName, 0, asmName, 0, mcName.Length - 1, StringComparison.OrdinalIgnoreCase) == 0;
121 return String.Compare( mcName, asmName, StringComparison.OrdinalIgnoreCase) == 0;
124 public IPermission CreateIdentityPermission( Evidence evidence )
126 return new StrongNameIdentityPermission( m_publicKeyBlob, m_name, m_version );
129 public override EvidenceBase Clone()
131 return new StrongName(m_publicKeyBlob, m_name, m_version);
139 #if FEATURE_CAS_POLICY
140 internal SecurityElement ToXml()
142 SecurityElement root = new SecurityElement( "StrongName" );
143 root.AddAttribute( "version", "1" );
145 if (m_publicKeyBlob != null)
146 root.AddAttribute( "Key", System.Security.Util.Hex.EncodeHexString( m_publicKeyBlob.PublicKey ) );
149 root.AddAttribute( "Name", m_name );
151 if (m_version != null)
152 root.AddAttribute( "Version", m_version.ToString() );
157 internal void FromXml (SecurityElement element)
160 throw new ArgumentNullException("element");
161 if (String.Compare(element.Tag, "StrongName", StringComparison.Ordinal) != 0)
162 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
163 Contract.EndContractBlock();
165 m_publicKeyBlob = null;
168 string key = element.Attribute("Key");
170 m_publicKeyBlob = new StrongNamePublicKeyBlob(System.Security.Util.Hex.DecodeHexString(key));
172 m_name = element.Attribute("Name");
174 string version = element.Attribute("Version");
176 m_version = new Version(version);
179 public override String ToString()
181 return ToXml().ToString();
183 #endif // FEATURE_CAS_POLICY
185 public override bool Equals( Object o )
187 StrongName that = (o as StrongName);
188 return (that != null) &&
189 Equals( this.m_publicKeyBlob, that.m_publicKeyBlob ) &&
190 Equals( this.m_name, that.m_name ) &&
191 Equals( this.m_version, that.m_version );
194 public override int GetHashCode()
196 if (m_publicKeyBlob != null)
198 return m_publicKeyBlob.GetHashCode();
200 else if (m_name != null || m_version != null)
202 return (m_name == null ? 0 : m_name.GetHashCode()) + (m_version == null ? 0 : m_version.GetHashCode());
206 return typeof( StrongName ).GetHashCode();
210 // INormalizeForIsolatedStorage is not implemented for startup perf
211 // equivalent to INormalizeForIsolatedStorage.Normalize()
212 internal Object Normalize()
214 MemoryStream ms = new MemoryStream();
215 BinaryWriter bw = new BinaryWriter(ms);
217 bw.Write(m_publicKeyBlob.PublicKey);
218 bw.Write(m_version.Major);