AssemblyName.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // System.Reflection/AssemblyName.cs
  3. //
  4. // Authors:
  5. // Paolo Molaro ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Configuration.Assemblies;
  32. using System.Globalization;
  33. using System.Runtime.Serialization;
  34. using System.Security;
  35. using System.Security.Cryptography;
  36. using System.Security.Permissions;
  37. using System.Text;
  38. using System.Runtime.InteropServices;
  39. using System.Runtime.CompilerServices;
  40. using System.IO;
  41. using Mono.Security;
  42. using Mono.Security.Cryptography;
  43. namespace System.Reflection {
  44. // References:
  45. // a. Uniform Resource Identifiers (URI): Generic Syntax
  46. // http://www.ietf.org/rfc/rfc2396.txt
  47. [ComVisible (true)]
  48. [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
  49. [Serializable]
  50. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  51. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
  52. #pragma warning disable 169
  53. #region Synch with object-internals.h
  54. string name;
  55. string codebase;
  56. int major, minor, build, revision;
  57. CultureInfo cultureinfo;
  58. AssemblyNameFlags flags;
  59. AssemblyHashAlgorithm hashalg;
  60. StrongNameKeyPair keypair;
  61. byte[] publicKey;
  62. byte[] keyToken;
  63. AssemblyVersionCompatibility versioncompat;
  64. Version version;
  65. ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
  66. #endregion
  67. #pragma warning restore 169
  68. public AssemblyName ()
  69. {
  70. // defaults
  71. versioncompat = AssemblyVersionCompatibility.SameMachine;
  72. }
  73. [MethodImpl (MethodImplOptions.InternalCall)]
  74. static extern bool ParseName (AssemblyName aname, string assemblyName);
  75. public AssemblyName (string assemblyName)
  76. {
  77. if (assemblyName == null)
  78. throw new ArgumentNullException ("assemblyName");
  79. if (assemblyName.Length < 1)
  80. throw new ArgumentException ("assemblyName cannot have zero length.");
  81. if (!ParseName (this, assemblyName))
  82. throw new FileLoadException ("The assembly name is invalid.");
  83. }
  84. [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
  85. public ProcessorArchitecture ProcessorArchitecture {
  86. get {
  87. return processor_architecture;
  88. }
  89. set {
  90. processor_architecture = value;
  91. }
  92. }
  93. internal AssemblyName (SerializationInfo si, StreamingContext sc)
  94. {
  95. name = si.GetString ("_Name");
  96. codebase = si.GetString ("_CodeBase");
  97. version = (Version)si.GetValue ("_Version", typeof (Version));
  98. publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
  99. keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
  100. hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
  101. keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
  102. versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
  103. flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
  104. int lcid = si.GetInt32 ("_CultureInfo");
  105. if (lcid != -1) cultureinfo = new CultureInfo (lcid);
  106. }
  107. public string Name {
  108. get { return name; }
  109. set { name = value; }
  110. }
  111. public string CodeBase {
  112. get { return codebase; }
  113. set { codebase = value; }
  114. }
  115. public string EscapedCodeBase {
  116. get {
  117. if (codebase == null)
  118. return null;
  119. return Uri.EscapeString (codebase, false, true, true);
  120. }
  121. }
  122. public CultureInfo CultureInfo {
  123. get { return cultureinfo; }
  124. set { cultureinfo = value; }
  125. }
  126. public AssemblyNameFlags Flags {
  127. get { return flags; }
  128. set { flags = value; }
  129. }
  130. public string FullName {
  131. get {
  132. if (name == null)
  133. return string.Empty;
  134. StringBuilder fname = new StringBuilder ();
  135. if (Char.IsWhiteSpace (name [0]))
  136. fname.Append ("\"" + name + "\"");
  137. else
  138. fname.Append (name);
  139. if (Version != null) {
  140. fname.Append (", Version=");
  141. fname.Append (Version.ToString ());
  142. }
  143. if (cultureinfo != null) {
  144. fname.Append (", Culture=");
  145. if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
  146. fname.Append ("neutral");
  147. else
  148. fname.Append (cultureinfo.Name);
  149. }
  150. byte [] pub_tok = InternalGetPublicKeyToken ();
  151. if (pub_tok != null) {
  152. if (pub_tok.Length == 0)
  153. fname.Append (", PublicKeyToken=null");
  154. else {
  155. fname.Append (", PublicKeyToken=");
  156. for (int i = 0; i < pub_tok.Length; i++)
  157. fname.Append (pub_tok[i].ToString ("x2"));
  158. }
  159. }
  160. if ((Flags & AssemblyNameFlags.Retargetable) != 0)
  161. fname.Append (", Retargetable=Yes");
  162. return fname.ToString ();
  163. }
  164. }
  165. public AssemblyHashAlgorithm HashAlgorithm {
  166. get { return hashalg; }
  167. set { hashalg = value; }
  168. }
  169. public StrongNameKeyPair KeyPair {
  170. get { return keypair; }
  171. set { keypair = value; }
  172. }
  173. public Version Version {
  174. get {
  175. return version;
  176. }
  177. set {
  178. version = value;
  179. if (value == null)
  180. major = minor = build = revision = 0;
  181. else {
  182. major = value.Major;
  183. minor = value.Minor;
  184. build = value.Build;
  185. revision = value.Revision;
  186. }
  187. }
  188. }
  189. public AssemblyVersionCompatibility VersionCompatibility {
  190. get { return versioncompat; }
  191. set { versioncompat = value; }
  192. }
  193. public override string ToString ()
  194. {
  195. string name = FullName;
  196. return (name != null) ? name : base.ToString ();
  197. }
  198. public byte[] GetPublicKey()
  199. {
  200. return publicKey;
  201. }
  202. public byte[] GetPublicKeyToken ()
  203. {
  204. if (keyToken != null)
  205. return keyToken;
  206. else if (publicKey == null)
  207. return null;
  208. else {
  209. if (publicKey.Length == 0)
  210. return new byte [0];
  211. if (!IsPublicKeyValid)
  212. throw new SecurityException ("The public key is not valid.");
  213. keyToken = ComputePublicKeyToken ();
  214. return keyToken;
  215. }
  216. }
  217. private bool IsPublicKeyValid {
  218. get {
  219. // check for ECMA key
  220. if (publicKey.Length == 16) {
  221. int i = 0;
  222. int sum = 0;
  223. while (i < publicKey.Length)
  224. sum += publicKey [i++];
  225. if (sum == 4)
  226. return true;
  227. }
  228. switch (publicKey [0]) {
  229. case 0x00: // public key inside a header
  230. if (publicKey.Length > 12 && publicKey [12] == 0x06) {
  231. try {
  232. CryptoConvert.FromCapiPublicKeyBlob (
  233. publicKey, 12);
  234. return true;
  235. } catch (CryptographicException) {
  236. }
  237. }
  238. break;
  239. case 0x06: // public key
  240. try {
  241. CryptoConvert.FromCapiPublicKeyBlob (publicKey);
  242. return true;
  243. } catch (CryptographicException) {
  244. }
  245. break;
  246. case 0x07: // private key
  247. break;
  248. }
  249. return false;
  250. }
  251. }
  252. private byte [] InternalGetPublicKeyToken ()
  253. {
  254. if (keyToken != null)
  255. return keyToken;
  256. if (publicKey == null)
  257. return null;
  258. if (publicKey.Length == 0)
  259. return new byte [0];
  260. if (!IsPublicKeyValid)
  261. throw new SecurityException ("The public key is not valid.");
  262. return ComputePublicKeyToken ();
  263. }
  264. private byte [] ComputePublicKeyToken ()
  265. {
  266. HashAlgorithm ha = SHA1.Create ();
  267. byte [] hash = ha.ComputeHash (publicKey);
  268. // we need the last 8 bytes in reverse order
  269. byte [] token = new byte [8];
  270. Array.Copy (hash, (hash.Length - 8), token, 0, 8);
  271. Array.Reverse (token, 0, 8);
  272. return token;
  273. }
  274. [MonoTODO]
  275. public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
  276. {
  277. if (reference == null)
  278. throw new ArgumentNullException ("reference");
  279. if (definition == null)
  280. throw new ArgumentNullException ("definition");
  281. if (reference.Name != definition.Name)
  282. return false;
  283. throw new NotImplementedException ();
  284. }
  285. public void SetPublicKey (byte[] publicKey)
  286. {
  287. if (publicKey == null)
  288. flags ^= AssemblyNameFlags.PublicKey;
  289. else
  290. flags |= AssemblyNameFlags.PublicKey;
  291. this.publicKey = publicKey;
  292. }
  293. public void SetPublicKeyToken (byte[] publicKeyToken)
  294. {
  295. keyToken = publicKeyToken;
  296. }
  297. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  298. public void GetObjectData (SerializationInfo info, StreamingContext context)
  299. {
  300. if (info == null)
  301. throw new ArgumentNullException ("info");
  302. info.AddValue ("_Name", name);
  303. info.AddValue ("_PublicKey", publicKey);
  304. info.AddValue ("_PublicKeyToken", keyToken);
  305. info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
  306. info.AddValue ("_CodeBase", codebase);
  307. info.AddValue ("_Version", Version);
  308. info.AddValue ("_HashAlgorithm", hashalg);
  309. info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
  310. info.AddValue ("_StrongNameKeyPair", keypair);
  311. info.AddValue ("_VersionCompatibility", versioncompat);
  312. info.AddValue ("_Flags", flags);
  313. info.AddValue ("_HashForControl", null);
  314. }
  315. public object Clone()
  316. {
  317. AssemblyName an = new AssemblyName ();
  318. an.name = name;
  319. an.codebase = codebase;
  320. an.major = major;
  321. an.minor = minor;
  322. an.build = build;
  323. an.revision = revision;
  324. an.version = version;
  325. an.cultureinfo = cultureinfo;
  326. an.flags = flags;
  327. an.hashalg = hashalg;
  328. an.keypair = keypair;
  329. an.publicKey = publicKey;
  330. an.keyToken = keyToken;
  331. an.versioncompat = versioncompat;
  332. return an;
  333. }
  334. public void OnDeserialization (object sender)
  335. {
  336. Version = version;
  337. }
  338. public static AssemblyName GetAssemblyName (string assemblyFile)
  339. {
  340. if (assemblyFile == null)
  341. throw new ArgumentNullException ("assemblyFile");
  342. AssemblyName aname = new AssemblyName ();
  343. Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
  344. return aname;
  345. }
  346. void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  347. {
  348. throw new NotImplementedException ();
  349. }
  350. void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  351. {
  352. throw new NotImplementedException ();
  353. }
  354. void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
  355. {
  356. throw new NotImplementedException ();
  357. }
  358. void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  359. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  360. {
  361. throw new NotImplementedException ();
  362. }
  363. }
  364. }