Adjust sleeping values
[mono.git] / mcs / class / corlib / System.Reflection / StrongNameKeyPair.cs
index dd13075a47b2b1ba805ba70049051943089a4338..aff2ae187e6e69546a111e67f70b70a279f0ced9 100644 (file)
@@ -7,11 +7,7 @@
 //
 // (C) 2002 Kevin Winchester
 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 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
 
 using System.IO;
 using System.Security.Cryptography;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.Serialization;
 
 using Mono.Security;
 using Mono.Security.Cryptography;
 
 namespace System.Reflection {
 
+       [ComVisible (true)]
 [Serializable]
-public class StrongNameKeyPair 
+public class StrongNameKeyPair : ISerializable, IDeserializationCallback
 {              
        private byte[] _publicKey;
        private string _keyPairContainer;
@@ -52,6 +52,10 @@ public class StrongNameKeyPair
        [NonSerialized]
        private RSA _rsa;
 
+       // note: we ask for UnmanagedCode because we do not want everyone
+       // to be able to generate strongnamed assemblies
+
+       [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
        public StrongNameKeyPair (byte[] keyPairArray) 
        {
                if (keyPairArray == null)
@@ -61,6 +65,7 @@ public class StrongNameKeyPair
                GetRSA ();
        }
        
+       [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
        public StrongNameKeyPair (FileStream keyPairFile) 
        {
                if (keyPairFile == null)
@@ -72,6 +77,7 @@ public class StrongNameKeyPair
                GetRSA ();
        }
        
+       [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
        public StrongNameKeyPair (string keyPairContainer) 
        {
                // named key container
@@ -81,7 +87,26 @@ public class StrongNameKeyPair
                _keyPairContainer = keyPairContainer;
                GetRSA ();
        }
-       
+       protected StrongNameKeyPair (SerializationInfo info, StreamingContext context)
+       {
+               _publicKey = (byte []) info.GetValue ("_publicKey", typeof (byte []));
+               _keyPairContainer = info.GetString ("_keyPairContainer");
+               _keyPairExported = info.GetBoolean ("_keyPairExported");
+               _keyPairArray = (byte []) info.GetValue ("_keyPairArray", typeof (byte []));
+       }
+
+       void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
+       {
+               info.AddValue ("_publicKey", _publicKey, typeof (byte []));
+               info.AddValue ("_keyPairContainer", _keyPairContainer);
+               info.AddValue ("_keyPairExported", _keyPairExported);
+               info.AddValue ("_keyPairArray", _keyPairArray, typeof (byte []));
+       }
+
+       void IDeserializationCallback.OnDeserialization (object sender)
+       {
+       }
+
        private RSA GetRSA ()
        {
                if (_rsa != null) return _rsa;
@@ -96,11 +121,13 @@ public class StrongNameKeyPair
                                _keyPairArray = null;
                        }
                }
+#if !NET_2_1
                else if (_keyPairContainer != null) {
                        CspParameters csp = new CspParameters ();
                        csp.KeyContainerName = _keyPairContainer;
                        _rsa = new RSACryptoServiceProvider (csp);
                }
+#endif
                return _rsa;
        }
 
@@ -138,24 +165,24 @@ public class StrongNameKeyPair
 
                                byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
                                _publicKey = new byte [blob.Length + 12];
-                               // The first 12 bytes are documented at:\r
-                               // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp\r
-                               // ALG_ID - Signature\r
-                               _publicKey[0] = 0x00;\r
-                               _publicKey[1] = 0x24;   \r
-                               _publicKey[2] = 0x00;   \r
-                               _publicKey[3] = 0x00;   \r
-                               // ALG_ID - Hash\r
-                               _publicKey[4] = 0x04;\r
-                               _publicKey[5] = 0x80;\r
-                               _publicKey[6] = 0x00;\r
-                               _publicKey[7] = 0x00;\r
-                               // Length of Public Key (in bytes)\r
-                               int lastPart = blob.Length;\r
-                               _publicKey[8] = (byte)(lastPart % 256);\r
-                               _publicKey[9] = (byte)(lastPart / 256); // just in case\r
-                               _publicKey[10] = 0x00;\r
-                               _publicKey[11] = 0x00;\r
+                               // The first 12 bytes are documented at:
+                               // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
+                               // ALG_ID - Signature
+                               _publicKey[0] = 0x00;
+                               _publicKey[1] = 0x24;   
+                               _publicKey[2] = 0x00;   
+                               _publicKey[3] = 0x00;   
+                               // ALG_ID - Hash
+                               _publicKey[4] = 0x04;
+                               _publicKey[5] = 0x80;
+                               _publicKey[6] = 0x00;
+                               _publicKey[7] = 0x00;
+                               // Length of Public Key (in bytes)
+                               int lastPart = blob.Length;
+                               _publicKey[8] = (byte)(lastPart % 256);
+                               _publicKey[9] = (byte)(lastPart / 256); // just in case
+                               _publicKey[10] = 0x00;
+                               _publicKey[11] = 0x00;
 
                                Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
                        }