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