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