mscorlib(4.0.0.0) API with additions
StrongNameKeyPair.cs
1 using Microsoft.Runtime.Hosting;
2 using System.IO;
6 using System.Security;
8 
9 namespace System.Reflection
10 {
12  [Serializable]
13  [ComVisible(true)]
15  {
16  private bool _keyPairExported;
17 
18  private byte[] _keyPairArray;
19 
20  private string _keyPairContainer;
21 
22  private byte[] _publicKey;
23 
26  public byte[] PublicKey
27  {
28  [SecuritySafeCritical]
29  get
30  {
31  if (_publicKey == null)
32  {
33  _publicKey = ComputePublicKey();
34  }
35  byte[] array = new byte[_publicKey.Length];
36  Array.Copy(_publicKey, array, _publicKey.Length);
37  return array;
38  }
39  }
40 
46  [SecuritySafeCritical]
47  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
48  public StrongNameKeyPair(FileStream keyPairFile)
49  {
50  if (keyPairFile == null)
51  {
52  throw new ArgumentNullException("keyPairFile");
53  }
54  int num = (int)keyPairFile.Length;
55  _keyPairArray = new byte[num];
56  keyPairFile.Read(_keyPairArray, 0, num);
57  _keyPairExported = true;
58  }
59 
65  [SecuritySafeCritical]
66  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
67  public StrongNameKeyPair(byte[] keyPairArray)
68  {
69  if (keyPairArray == null)
70  {
71  throw new ArgumentNullException("keyPairArray");
72  }
73  _keyPairArray = new byte[keyPairArray.Length];
74  Array.Copy(keyPairArray, _keyPairArray, keyPairArray.Length);
75  _keyPairExported = true;
76  }
77 
83  [SecuritySafeCritical]
84  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
85  public StrongNameKeyPair(string keyPairContainer)
86  {
87  if (keyPairContainer == null)
88  {
89  throw new ArgumentNullException("keyPairContainer");
90  }
91  _keyPairContainer = keyPairContainer;
92  _keyPairExported = false;
93  }
94 
98  [SecuritySafeCritical]
99  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
101  {
102  _keyPairExported = (bool)info.GetValue("_keyPairExported", typeof(bool));
103  _keyPairArray = (byte[])info.GetValue("_keyPairArray", typeof(byte[]));
104  _keyPairContainer = (string)info.GetValue("_keyPairContainer", typeof(string));
105  _publicKey = (byte[])info.GetValue("_publicKey", typeof(byte[]));
106  }
107 
108  [SecurityCritical]
109  private unsafe byte[] ComputePublicKey()
110  {
111  byte[] result = null;
113  try
114  {
115  return result;
116  }
117  finally
118  {
119  IntPtr ppbPublicKeyBlob = IntPtr.Zero;
120  int pcbPublicKeyBlob = 0;
121  try
122  {
123  if (!((!_keyPairExported) ? StrongNameHelpers.StrongNameGetPublicKey(_keyPairContainer, null, 0, out ppbPublicKeyBlob, out pcbPublicKeyBlob) : StrongNameHelpers.StrongNameGetPublicKey(null, _keyPairArray, _keyPairArray.Length, out ppbPublicKeyBlob, out pcbPublicKeyBlob)))
124  {
125  throw new ArgumentException(Environment.GetResourceString("Argument_StrongNameGetPublicKey"));
126  }
127  result = new byte[pcbPublicKeyBlob];
128  Buffer.Memcpy(result, 0, (byte*)ppbPublicKeyBlob.ToPointer(), 0, pcbPublicKeyBlob);
129  }
130  finally
131  {
132  if (ppbPublicKeyBlob != IntPtr.Zero)
133  {
134  StrongNameHelpers.StrongNameFreeBuffer(ppbPublicKeyBlob);
135  }
136  }
137  }
138  }
139 
145  [SecurityCritical]
147  {
148  info.AddValue("_keyPairExported", _keyPairExported);
149  info.AddValue("_keyPairArray", _keyPairArray);
150  info.AddValue("_keyPairContainer", _keyPairContainer);
151  info.AddValue("_publicKey", _publicKey);
152  }
153 
156  void IDeserializationCallback.OnDeserialization(object sender)
157  {
158  }
159 
160  private bool GetKeyPair(out object arrayOrContainer)
161  {
162  arrayOrContainer = (_keyPairExported ? ((object)_keyPairArray) : ((object)_keyPairContainer));
163  return _keyPairExported;
164  }
165  }
166 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
Indicates that a class is to be notified when deserialization of the entire object graph has been com...
Encapsulates access to a public or private key pair used to sign strong name assemblies.
Definition: __Canon.cs:3
override int Read([In] [Out] byte[] array, int offset, int count)
Reads a block of bytes from the stream and writes the data in a given buffer.
Definition: FileStream.cs:1222
Describes the source and destination of a given serialized stream, and provides an additional caller-...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
void OnDeserialization(object sender)
Runs when the entire object graph has been deserialized.
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
StrongNameKeyPair(FileStream keyPairFile)
Initializes a new instance of the T:System.Reflection.StrongNameKeyPair class, building the key pair ...
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
StrongNameKeyPair(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Reflection.StrongNameKeyPair class, building the key pair ...
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
override long Length
Gets the length in bytes of the stream.
Definition: FileStream.cs:126
StrongNameKeyPair(byte[] keyPairArray)
Initializes a new instance of the T:System.Reflection.StrongNameKeyPair class, building the key pair ...
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Specifies that the class can be serialized.
SecurityPermissionFlag
Specifies access flags for the security permission object.
byte [] PublicKey
Gets the public part of the public key or public key token of the key pair.
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Provides a set of static methods and properties that provide support for compilers....
StrongNameKeyPair(string keyPairContainer)
Initializes a new instance of the T:System.Reflection.StrongNameKeyPair class, building the key pair ...