Fix null sessions in HttpContextWrapper.Session
[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                         else if (publicKey == null)
236                                 return null;
237                         else {
238                                 if (publicKey.Length == 0)
239                                         return new byte [0];
240
241                                 if (!IsPublicKeyValid)
242                                         throw new  SecurityException ("The public key is not valid.");
243
244                                 keyToken = ComputePublicKeyToken ();
245                                 return keyToken;
246                         }
247                 }
248
249                 private bool IsPublicKeyValid {
250                         get {
251                                 // check for ECMA key
252                                 if (publicKey.Length == 16) {
253                                         int i = 0;
254                                         int sum = 0;
255                                         while (i < publicKey.Length)
256                                                 sum += publicKey [i++];
257                                         if (sum == 4)
258                                                 return true;
259                                 }
260
261                                 switch (publicKey [0]) {
262                                 case 0x00: // public key inside a header
263                                         if (publicKey.Length > 12 && publicKey [12] == 0x06) {
264                                                 try {
265                                                         CryptoConvert.FromCapiPublicKeyBlob (
266                                                                 publicKey, 12);
267                                                         return true;
268                                                 } catch (CryptographicException) {
269                                                 }
270                                         }
271                                         break;
272                                 case 0x06: // public key
273                                         try {
274                                                 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
275                                                 return true;
276                                         } catch (CryptographicException) {
277                                         }
278                                         break;
279                                 case 0x07: // private key
280                                         break;
281                                 }
282
283                                 return false;
284                         }
285                 }
286
287                 private byte [] InternalGetPublicKeyToken ()
288                 {
289                         if (keyToken != null)
290                                 return keyToken;
291
292                         if (publicKey == null)
293                                 return null;
294
295                         if (publicKey.Length == 0)
296                                 return new byte [0];
297
298                         if (!IsPublicKeyValid)
299                                 throw new  SecurityException ("The public key is not valid.");
300
301                         return ComputePublicKeyToken ();
302                 }
303
304                 private byte [] ComputePublicKeyToken ()
305                 {
306                         HashAlgorithm ha = SHA1.Create ();
307                         byte [] hash = ha.ComputeHash (publicKey);
308                         // we need the last 8 bytes in reverse order
309                         byte [] token = new byte [8];
310                         Array.Copy (hash, (hash.Length - 8), token, 0, 8);
311                         Array.Reverse (token, 0, 8);
312                         return token;
313                 }
314
315                 [MonoTODO]
316                 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
317                 {
318                         if (reference == null)
319                                 throw new ArgumentNullException ("reference");
320                         if (definition == null)
321                                 throw new ArgumentNullException ("definition");
322                         if (reference.Name != definition.Name)
323                                 return false;
324                         throw new NotImplementedException ();
325                 }
326
327                 public void SetPublicKey (byte[] publicKey) 
328                 {
329                         if (publicKey == null)
330                                 flags ^= AssemblyNameFlags.PublicKey;
331                         else
332                                 flags |= AssemblyNameFlags.PublicKey;
333                         this.publicKey = publicKey;
334                 }
335
336                 public void SetPublicKeyToken (byte[] publicKeyToken) 
337                 {
338                         keyToken = publicKeyToken;
339                 }
340
341                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
342                 public void GetObjectData (SerializationInfo info, StreamingContext context)
343                 {
344                         if (info == null)
345                                 throw new ArgumentNullException ("info");
346
347                         info.AddValue ("_Name", name);
348                         info.AddValue ("_PublicKey", publicKey);
349                         info.AddValue ("_PublicKeyToken", keyToken);
350                         info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
351                         info.AddValue ("_CodeBase", codebase);
352                         info.AddValue ("_Version", Version);
353                         info.AddValue ("_HashAlgorithm", hashalg);
354                         info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
355                         info.AddValue ("_StrongNameKeyPair", keypair);
356                         info.AddValue ("_VersionCompatibility", versioncompat);
357                         info.AddValue ("_Flags", flags);
358                         info.AddValue ("_HashForControl", null);
359                 }
360
361                 public object Clone() 
362                 {
363                         AssemblyName an = new AssemblyName ();
364                         an.name = name;
365                         an.codebase = codebase;
366                         an.major = major;
367                         an.minor = minor;
368                         an.build = build;
369                         an.revision = revision;
370                         an.version = version;
371                         an.cultureinfo = cultureinfo;
372                         an.flags = flags;
373                         an.hashalg = hashalg;
374                         an.keypair = keypair;
375                         an.publicKey = publicKey;
376                         an.keyToken = keyToken;
377                         an.versioncompat = versioncompat;
378                         return an;
379                 }
380
381                 public void OnDeserialization (object sender) 
382                 {
383                         Version = version;
384                 }
385
386                 public static AssemblyName GetAssemblyName (string assemblyFile) 
387                 {
388                         if (assemblyFile == null)
389                                 throw new ArgumentNullException ("assemblyFile");
390
391                         AssemblyName aname = new AssemblyName ();
392                         Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
393                         return aname;
394                 }
395
396                 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
397                 {
398                         throw new NotImplementedException ();
399                 }
400
401                 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
402                 {
403                         throw new NotImplementedException ();
404                 }
405
406                 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
407                 {
408                         throw new NotImplementedException ();
409                 }
410
411                 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
412                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
413                 {
414                         throw new NotImplementedException ();
415                 }
416         }
417 }