This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
index c737d8d635eceae962795d2fc2e92029c307f4ea..344395be1a3cce5f3da9ddbe27adaaaa162e062c 100755 (executable)
@@ -7,6 +7,29 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Reflection;
 using System.Resources;
@@ -85,6 +108,7 @@ namespace System.Reflection.Emit {
                Win32VersionResource version_res;
                bool created;
                bool is_module_only;
+               private Mono.Security.StrongName sn;
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern void basic_init (AssemblyBuilder ab);
@@ -106,6 +130,18 @@ namespace System.Reflection.Emit {
                                version = v.ToString ();
                        }
 
+                       if (n.KeyPair != null) {
+                               // full keypair is available (for signing)
+                               sn = n.KeyPair.StrongName ();
+                       }
+                       else {
+                               // public key is available (for delay-signing)
+                               byte[] pk = n.GetPublicKey ();
+                               if ((pk != null) && (pk.Length > 0)) {
+                                       sn = new Mono.Security.StrongName (pk);
+                               }
+                       }
+
                        basic_init (this);
                }
 
@@ -168,12 +204,12 @@ namespace System.Reflection.Emit {
                        resources [p].attrs = attribute;
                }
 
-               public void EmbedResourceFile (string name, string fileName)
+               internal void EmbedResourceFile (string name, string fileName)
                {
                        EmbedResourceFile (name, fileName, ResourceAttributes.Public);
                }
 
-               public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
+               internal void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
                {
                        if (resources != null) {
                                MonoResource[] new_r = new MonoResource [resources.Length + 1];
@@ -561,7 +597,12 @@ namespace System.Reflection.Emit {
 
                        if (version_res != null)
                                DefineVersionInfoResourceImpl (assemblyFileName);
-                       
+
+                       if (sn != null) {
+                               // runtime needs to value to embed it into the assembly
+                               public_key = sn.PublicKey;
+                       }
+
                        foreach (ModuleBuilder module in modules)
                                if (module != mainModule)
                                        module.Save ();
@@ -570,6 +611,10 @@ namespace System.Reflection.Emit {
                        // contain the hash of the other modules
                        mainModule.Save ();
 
+                       if ((sn != null) && (sn.CanSign)) {
+                               sn.Sign (System.IO.Path.Combine (this.AssemblyDir, assemblyFileName));
+                       }
+
                        created = true;
                }
 
@@ -596,36 +641,11 @@ namespace System.Reflection.Emit {
 
                        string attrname = customBuilder.Ctor.ReflectedType.FullName;
                        byte[] data;
-                       int len, pos;
-                       Mono.Security.StrongName sn;
+                       int pos;
+
                        if (attrname == "System.Reflection.AssemblyVersionAttribute") {
                                version = create_assembly_version (customBuilder.string_arg ());
                                return;
-                       } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
-                               string keyfile_name = customBuilder.string_arg ();
-                               if (keyfile_name == String.Empty)
-                                       return;
-                               using (FileStream fs = new FileStream (keyfile_name, FileMode.Open)) {
-                                       byte[] snkeypair = new byte [fs.Length];
-                                       fs.Read (snkeypair, 0, snkeypair.Length);
-
-                                       // this will import public or private/public keys
-                                       RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
-                                       // and export only the public part
-                                       sn = new Mono.Security.StrongName (rsa);
-                                       public_key = sn.PublicKey;
-                               }
-                               return;
-                       } else if (attrname == "System.Reflection.AssemblyKeyNameAttribute") {
-                               string key_name = customBuilder.string_arg ();
-                               if (key_name == String.Empty)
-                                       return;
-                               CspParameters csparam = new CspParameters ();
-                               csparam.KeyContainerName = key_name;
-                               RSA rsacsp = new RSACryptoServiceProvider (csparam);
-                               sn = new Mono.Security.StrongName (rsacsp);
-                               public_key = sn.PublicKey;
-                               return;
                        } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
                                culture = customBuilder.string_arg ();
                        } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
@@ -643,11 +663,8 @@ namespace System.Reflection.Emit {
                                flags |= ((uint)data [pos + 2]) << 16;
                                flags |= ((uint)data [pos + 3]) << 24;
                                return;
-                       } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
-                               data = customBuilder.Data;
-                               pos = 2;
-                               delay_sign = data [2] != 0;
                        }
+
                        if (cattrs != null) {
                                CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
                                cattrs.CopyTo (new_array, 0);
@@ -658,6 +675,7 @@ namespace System.Reflection.Emit {
                                cattrs [0] = customBuilder;
                        }
                }
+
                public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
                        if (con == null)
                                throw new ArgumentNullException ("con");
@@ -667,13 +685,13 @@ namespace System.Reflection.Emit {
                        SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
                }
 
-               public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
+               internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
                        this.corlib_object_type = corlib_object_type;
                        this.corlib_value_type = corlib_value_type;
                        this.corlib_enum_type = corlib_enum_type;
                }
 
-               public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
+               internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
                {
                        SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
                        this.corlib_void_type = corlib_void_type;