AssemblyName.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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;
  42. using Mono.Security;
  43. using Mono.Security.Cryptography;
  44. namespace System.Reflection {
  45. // References:
  46. // a. Uniform Resource Identifiers (URI): Generic Syntax
  47. // http://www.ietf.org/rfc/rfc2396.txt
  48. [ComVisible (true)]
  49. [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
  50. [Serializable]
  51. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  52. [StructLayout (LayoutKind.Sequential)]
  53. #if MOBILE
  54. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback {
  55. #else
  56. public sealed class AssemblyName : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
  57. #endif
  58. #pragma warning disable 169
  59. #region Synch with object-internals.h
  60. string name;
  61. string codebase;
  62. int major, minor, build, revision;
  63. CultureInfo cultureinfo;
  64. AssemblyNameFlags flags;
  65. AssemblyHashAlgorithm hashalg;
  66. StrongNameKeyPair keypair;
  67. byte[] publicKey;
  68. byte[] keyToken;
  69. AssemblyVersionCompatibility versioncompat;
  70. Version version;
  71. ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
  72. #endregion
  73. #pragma warning restore 169
  74. AssemblyContentType contentType;
  75. public AssemblyName ()
  76. {
  77. // defaults
  78. versioncompat = AssemblyVersionCompatibility.SameMachine;
  79. }
  80. [MethodImpl (MethodImplOptions.InternalCall)]
  81. static extern bool ParseName (AssemblyName aname, string assemblyName);
  82. public AssemblyName (string assemblyName)
  83. {
  84. if (assemblyName == null)
  85. throw new ArgumentNullException ("assemblyName");
  86. if (assemblyName.Length < 1)
  87. throw new ArgumentException ("assemblyName cannot have zero length.");
  88. if (!ParseName (this, assemblyName))
  89. throw new FileLoadException ("The assembly name is invalid.");
  90. }
  91. [MonoLimitation ("Not used, as the values are too limited; Mono supports more")]
  92. public ProcessorArchitecture ProcessorArchitecture {
  93. get {
  94. return processor_architecture;
  95. }
  96. set {
  97. processor_architecture = value;
  98. }
  99. }
  100. internal AssemblyName (SerializationInfo si, StreamingContext sc)
  101. {
  102. name = si.GetString ("_Name");
  103. codebase = si.GetString ("_CodeBase");
  104. version = (Version)si.GetValue ("_Version", typeof (Version));
  105. publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
  106. keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
  107. hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
  108. keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
  109. versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
  110. flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
  111. int lcid = si.GetInt32 ("_CultureInfo");
  112. if (lcid != -1) cultureinfo = new CultureInfo (lcid);
  113. }
  114. public string Name {
  115. get { return name; }
  116. set { name = value; }
  117. }
  118. public string CodeBase {
  119. get { return codebase; }
  120. set { codebase = value; }
  121. }
  122. public string EscapedCodeBase {
  123. get {
  124. if (codebase == null)
  125. return null;
  126. return Uri.EscapeString (codebase, false, true, true);
  127. }
  128. }
  129. public CultureInfo CultureInfo {
  130. get { return cultureinfo; }
  131. set { cultureinfo = value; }
  132. }
  133. public AssemblyNameFlags Flags {
  134. get { return flags; }
  135. set { flags = value; }
  136. }
  137. public string FullName {
  138. get {
  139. if (name == null)
  140. return string.Empty;
  141. StringBuilder fname = new StringBuilder ();
  142. if (Char.IsWhiteSpace (name [0]))
  143. fname.Append ("\"" + name + "\"");
  144. else
  145. fname.Append (name);
  146. if (Version != null) {
  147. fname.Append (", Version=");
  148. fname.Append (Version.ToString ());
  149. }
  150. if (cultureinfo != null) {
  151. fname.Append (", Culture=");
  152. if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
  153. fname.Append ("neutral");
  154. else
  155. fname.Append (cultureinfo.Name);
  156. }
  157. byte [] pub_tok = InternalGetPublicKeyToken ();
  158. if (pub_tok != null) {
  159. if (pub_tok.Length == 0)
  160. fname.Append (", PublicKeyToken=null");
  161. else {
  162. fname.Append (", PublicKeyToken=");
  163. for (int i = 0; i < pub_tok.Length; i++)
  164. fname.Append (pub_tok[i].ToString ("x2"));
  165. }
  166. }
  167. if ((Flags & AssemblyNameFlags.Retargetable) != 0)
  168. fname.Append (", Retargetable=Yes");
  169. return fname.ToString ();
  170. }
  171. }
  172. public AssemblyHashAlgorithm HashAlgorithm {
  173. get { return hashalg; }
  174. set { hashalg = value; }
  175. }
  176. public StrongNameKeyPair KeyPair {
  177. get { return keypair; }
  178. set { keypair = value; }
  179. }
  180. public Version Version {
  181. get {
  182. return version;
  183. }
  184. set {
  185. version = value;
  186. if (value == null)
  187. major = minor = build = revision = 0;
  188. else {
  189. major = value.Major;
  190. minor = value.Minor;
  191. build = value.Build;
  192. revision = value.Revision;
  193. }
  194. }
  195. }
  196. public AssemblyVersionCompatibility VersionCompatibility {
  197. get { return versioncompat; }
  198. set { versioncompat = value; }
  199. }
  200. public override string ToString ()
  201. {
  202. string name = FullName;
  203. return (name != null) ? name : base.ToString ();
  204. }
  205. public byte[] GetPublicKey()
  206. {
  207. return publicKey;
  208. }
  209. public byte[] GetPublicKeyToken ()
  210. {
  211. if (keyToken != null)
  212. return keyToken;
  213. if (publicKey == null)
  214. return null;
  215. if (publicKey.Length == 0)
  216. return EmptyArray<byte>.Value;
  217. if (!IsPublicKeyValid)
  218. throw new SecurityException ("The public key is not valid.");
  219. keyToken = ComputePublicKeyToken ();
  220. return keyToken;
  221. }
  222. private bool IsPublicKeyValid {
  223. get {
  224. // check for ECMA key
  225. if (publicKey.Length == 16) {
  226. int i = 0;
  227. int sum = 0;
  228. while (i < publicKey.Length)
  229. sum += publicKey [i++];
  230. if (sum == 4)
  231. return true;
  232. }
  233. switch (publicKey [0]) {
  234. case 0x00: // public key inside a header
  235. if (publicKey.Length > 12 && publicKey [12] == 0x06) {
  236. #if MOBILE
  237. return true;
  238. #else
  239. try {
  240. CryptoConvert.FromCapiPublicKeyBlob (
  241. publicKey, 12);
  242. return true;
  243. } catch (CryptographicException) {
  244. }
  245. #endif
  246. }
  247. break;
  248. case 0x06: // public key
  249. #if MOBILE
  250. return true;
  251. #else
  252. try {
  253. CryptoConvert.FromCapiPublicKeyBlob (publicKey);
  254. return true;
  255. } catch (CryptographicException) {
  256. }
  257. break;
  258. #endif
  259. case 0x07: // private key
  260. break;
  261. }
  262. return false;
  263. }
  264. }
  265. private byte [] InternalGetPublicKeyToken ()
  266. {
  267. if (keyToken != null)
  268. return keyToken;
  269. if (publicKey == null)
  270. return null;
  271. if (publicKey.Length == 0)
  272. return EmptyArray<byte>.Value;
  273. if (!IsPublicKeyValid)
  274. throw new SecurityException ("The public key is not valid.");
  275. return ComputePublicKeyToken ();
  276. }
  277. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  278. extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
  279. private unsafe byte [] ComputePublicKeyToken ()
  280. {
  281. byte [] token = new byte [8];
  282. fixed (byte* pkt = token)
  283. fixed (byte *pk = publicKey)
  284. get_public_token (pkt, pk, publicKey.Length);
  285. return token;
  286. }
  287. public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
  288. {
  289. if (reference == null)
  290. throw new ArgumentNullException ("reference");
  291. if (definition == null)
  292. throw new ArgumentNullException ("definition");
  293. // we only compare the simple assembly name to be consistent with MS .NET,
  294. // which is the result of a bug in their implementation (see https://connect.microsoft.com/VisualStudio/feedback/details/752902)
  295. return string.Equals (reference.Name, definition.Name, StringComparison.OrdinalIgnoreCase);
  296. }
  297. public void SetPublicKey (byte[] publicKey)
  298. {
  299. if (publicKey == null)
  300. flags ^= AssemblyNameFlags.PublicKey;
  301. else
  302. flags |= AssemblyNameFlags.PublicKey;
  303. this.publicKey = publicKey;
  304. }
  305. public void SetPublicKeyToken (byte[] publicKeyToken)
  306. {
  307. keyToken = publicKeyToken;
  308. }
  309. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  310. public void GetObjectData (SerializationInfo info, StreamingContext context)
  311. {
  312. if (info == null)
  313. throw new ArgumentNullException ("info");
  314. info.AddValue ("_Name", name);
  315. info.AddValue ("_PublicKey", publicKey);
  316. info.AddValue ("_PublicKeyToken", keyToken);
  317. info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
  318. info.AddValue ("_CodeBase", codebase);
  319. info.AddValue ("_Version", Version);
  320. info.AddValue ("_HashAlgorithm", hashalg);
  321. info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
  322. info.AddValue ("_StrongNameKeyPair", keypair);
  323. info.AddValue ("_VersionCompatibility", versioncompat);
  324. info.AddValue ("_Flags", flags);
  325. info.AddValue ("_HashForControl", null);
  326. }
  327. public object Clone()
  328. {
  329. AssemblyName an = new AssemblyName ();
  330. an.name = name;
  331. an.codebase = codebase;
  332. an.major = major;
  333. an.minor = minor;
  334. an.build = build;
  335. an.revision = revision;
  336. an.version = version;
  337. an.cultureinfo = cultureinfo;
  338. an.flags = flags;
  339. an.hashalg = hashalg;
  340. an.keypair = keypair;
  341. an.publicKey = publicKey;
  342. an.keyToken = keyToken;
  343. an.versioncompat = versioncompat;
  344. an.processor_architecture = processor_architecture;
  345. return an;
  346. }
  347. public void OnDeserialization (object sender)
  348. {
  349. Version = version;
  350. }
  351. public static AssemblyName GetAssemblyName (string assemblyFile)
  352. {
  353. if (assemblyFile == null)
  354. throw new ArgumentNullException ("assemblyFile");
  355. AssemblyName aname = new AssemblyName ();
  356. Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
  357. return aname;
  358. }
  359. #if !MOBILE
  360. void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  361. {
  362. throw new NotImplementedException ();
  363. }
  364. void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  365. {
  366. throw new NotImplementedException ();
  367. }
  368. void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
  369. {
  370. throw new NotImplementedException ();
  371. }
  372. void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  373. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  374. {
  375. throw new NotImplementedException ();
  376. }
  377. #endif
  378. public string CultureName {
  379. get {
  380. return (cultureinfo == null)? null : cultureinfo.Name;
  381. }
  382. #if NETSTANDARD
  383. set {
  384. throw new NotImplementedException ();
  385. }
  386. #endif
  387. }
  388. [ComVisibleAttribute(false)]
  389. public AssemblyContentType ContentType {
  390. get {
  391. return contentType;
  392. }
  393. set {
  394. contentType = value;
  395. }
  396. }
  397. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  398. static extern unsafe MonoAssemblyName* GetNativeName (IntPtr assembly_ptr);
  399. internal static AssemblyName Create (Assembly assembly, bool fillCodebase)
  400. {
  401. AssemblyName aname = new AssemblyName ();
  402. unsafe {
  403. MonoAssemblyName *native = GetNativeName (assembly._mono_assembly);
  404. aname.name = RuntimeMarshal.PtrToUtf8String (native->name);
  405. aname.major = native->major;
  406. aname.minor = native->minor;
  407. aname.build = native->build;
  408. aname.revision = native->revision;
  409. aname.flags = (AssemblyNameFlags)native->flags;
  410. aname.hashalg = (AssemblyHashAlgorithm)native->hash_alg;
  411. aname.versioncompat = AssemblyVersionCompatibility.SameMachine;
  412. aname.processor_architecture = (ProcessorArchitecture)native->arch;
  413. aname.version = new Version (aname.major, aname.minor, aname.build, aname.revision);
  414. if (fillCodebase)
  415. aname.codebase = assembly.CodeBase;
  416. if (native->culture != IntPtr.Zero)
  417. aname.cultureinfo = CultureInfo.CreateCulture ( RuntimeMarshal.PtrToUtf8String (native->culture), false);
  418. if (native->public_key != IntPtr.Zero) {
  419. aname.publicKey = RuntimeMarshal.DecodeBlobArray (native->public_key);
  420. aname.flags |= AssemblyNameFlags.PublicKey;
  421. } else {
  422. aname.publicKey = EmptyArray<byte>.Value;
  423. aname.flags |= AssemblyNameFlags.PublicKey;
  424. }
  425. // MonoAssemblyName keeps the public key token as an hexadecimal string
  426. if (native->public_key_token [0] != 0) {
  427. byte[] keyToken = new byte [8];
  428. for (int i = 0, j = 0; i < 8; ++i) {
  429. keyToken [i] = (byte)(RuntimeMarshal.AsciHexDigitValue (native->public_key_token [j++]) << 4);
  430. keyToken [i] |= (byte)RuntimeMarshal.AsciHexDigitValue (native->public_key_token [j++]);
  431. }
  432. aname.keyToken = keyToken;
  433. } else {
  434. aname.keyToken = EmptyArray<byte>.Value;
  435. }
  436. }
  437. return aname;
  438. }
  439. }
  440. }