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