The Labs \ Source Viewer \ SSCLI \ System.Reflection \ StrongNameKeyPair

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** File:    StrongNameKeyPair.cs
  18. **
  19. **
  20. ** Purpose: Encapsulate access to a public/private key pair
  21. **          used to sign strong name assemblies.
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System.Reflection
  26. {
  27.    
  28.     using System;
  29.     using System.IO;
  30.     using System.Runtime.CompilerServices;
  31.     using System.Runtime.Serialization;
  32.     using System.Security.Permissions;
  33.    
  34.     [Serializable()]
  35.     [System.Runtime.InteropServices.ComVisible(true)]
  36.     public class StrongNameKeyPair : IDeserializationCallback, ISerializable
  37.     {
  38.         private bool _keyPairExported;
  39.         private byte[] _keyPairArray;
  40.         private string _keyPairContainer;
  41.         private byte[] _publicKey;
  42.        
  43.         // Build key pair from file.
  44.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  45.         public StrongNameKeyPair(FileStream keyPairFile)
  46.         {
  47.             if (keyPairFile == null)
  48.                 throw new ArgumentNullException("keyPairFile");
  49.            
  50.             int length = (int)keyPairFile.Length;
  51.             _keyPairArray = new byte[length];
  52.             keyPairFile.Read(_keyPairArray, 0, length);
  53.            
  54.             _keyPairExported = true;
  55.         }
  56.        
  57.         // Build key pair from byte array in memory.
  58.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  59.         public StrongNameKeyPair(byte[] keyPairArray)
  60.         {
  61.             if (keyPairArray == null)
  62.                 throw new ArgumentNullException("keyPairArray");
  63.            
  64.             _keyPairArray = new byte[keyPairArray.Length];
  65.             Array.Copy(keyPairArray, _keyPairArray, keyPairArray.Length);
  66.            
  67.             _keyPairExported = true;
  68.         }
  69.        
  70.         // Reference key pair in named key container.
  71.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  72.         public StrongNameKeyPair(string keyPairContainer)
  73.         {
  74.             if (keyPairContainer == null)
  75.                 throw new ArgumentNullException("keyPairContainer");
  76.            
  77.             _keyPairContainer = keyPairContainer;
  78.            
  79.             _keyPairExported = false;
  80.         }
  81.        
  82.         [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  83.         protected StrongNameKeyPair(SerializationInfo info, StreamingContext context)
  84.         {
  85.             _keyPairExported = (bool)info.GetValue("_keyPairExported", typeof(bool));
  86.             _keyPairArray = (byte[])info.GetValue("_keyPairArray", typeof(byte[]));
  87.             _keyPairContainer = (string)info.GetValue("_keyPairContainer", typeof(string));
  88.             _publicKey = (byte[])info.GetValue("_publicKey", typeof(byte[]));
  89.         }
  90.        
  91.         // Get the public portion of the key pair.
  92.         public byte[] PublicKey {
  93.             get {
  94.                 if (_publicKey == null) {
  95.                     _publicKey = nGetPublicKey(_keyPairExported, _keyPairArray, _keyPairContainer);
  96.                 }
  97.                
  98.                 byte[] publicKey = new byte[_publicKey.Length];
  99.                 Array.Copy(_publicKey, publicKey, _publicKey.Length);
  100.                
  101.                 return publicKey;
  102.             }
  103.         }
  104.        
  105.         /// <internalonly/>
  106.         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  107.         {
  108.             info.AddValue("_keyPairExported", _keyPairExported);
  109.             info.AddValue("_keyPairArray", _keyPairArray);
  110.             info.AddValue("_keyPairContainer", _keyPairContainer);
  111.             info.AddValue("_publicKey", _publicKey);
  112.         }
  113.        
  114.         /// <internalonly/>
  115.         void IDeserializationCallback.OnDeserialization(object sender)
  116.         {
  117.         }
  118.        
  119.         // Internal routine used to retrieve key pair info from unmanaged code.
  120.         private bool GetKeyPair(out object arrayOrContainer)
  121.         {
  122.             arrayOrContainer = _keyPairExported ? (object)_keyPairArray : (object)_keyPairContainer;
  123.             return _keyPairExported;
  124.         }
  125.        
  126.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  127.         private extern byte[] nGetPublicKey(bool exported, byte[] array, string container);
  128.     }
  129. }

Developer Fusion