StrongNameKeyPair.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Reflection.StrongNameKeyPair.cs
  3. //
  4. // Authors:
  5. // Kevin Winchester ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // (C) 2002 Kevin Winchester
  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. using System.IO;
  32. using System.Security.Cryptography;
  33. using System.Security.Permissions;
  34. using System.Runtime.InteropServices;
  35. using System.Runtime.Serialization;
  36. using Mono.Security;
  37. using Mono.Security.Cryptography;
  38. namespace System.Reflection {
  39. [ComVisible (true)]
  40. [Serializable]
  41. public class StrongNameKeyPair : ISerializable, IDeserializationCallback
  42. {
  43. private byte[] _publicKey;
  44. private string _keyPairContainer;
  45. private bool _keyPairExported;
  46. private byte[] _keyPairArray;
  47. [NonSerialized]
  48. private RSA _rsa;
  49. // note: we ask for UnmanagedCode because we do not want everyone
  50. // to be able to generate strongnamed assemblies
  51. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  52. public StrongNameKeyPair (byte[] keyPairArray)
  53. {
  54. if (keyPairArray == null)
  55. throw new ArgumentNullException ("keyPairArray");
  56. LoadKey (keyPairArray);
  57. GetRSA ();
  58. }
  59. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  60. public StrongNameKeyPair (FileStream keyPairFile)
  61. {
  62. if (keyPairFile == null)
  63. throw new ArgumentNullException ("keyPairFile");
  64. byte[] input = new byte [keyPairFile.Length];
  65. keyPairFile.Read (input, 0, input.Length);
  66. LoadKey (input);
  67. GetRSA ();
  68. }
  69. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  70. public StrongNameKeyPair (string keyPairContainer)
  71. {
  72. // named key container
  73. if (keyPairContainer == null)
  74. throw new ArgumentNullException ("keyPairContainer");
  75. _keyPairContainer = keyPairContainer;
  76. GetRSA ();
  77. }
  78. protected StrongNameKeyPair (SerializationInfo info, StreamingContext context)
  79. {
  80. _publicKey = (byte []) info.GetValue ("_publicKey", typeof (byte []));
  81. _keyPairContainer = info.GetString ("_keyPairContainer");
  82. _keyPairExported = info.GetBoolean ("_keyPairExported");
  83. _keyPairArray = (byte []) info.GetValue ("_keyPairArray", typeof (byte []));
  84. }
  85. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  86. {
  87. info.AddValue ("_publicKey", _publicKey, typeof (byte []));
  88. info.AddValue ("_keyPairContainer", _keyPairContainer);
  89. info.AddValue ("_keyPairExported", _keyPairExported);
  90. info.AddValue ("_keyPairArray", _keyPairArray, typeof (byte []));
  91. }
  92. void IDeserializationCallback.OnDeserialization (object sender)
  93. {
  94. }
  95. private RSA GetRSA ()
  96. {
  97. if (_rsa != null) return _rsa;
  98. if (_keyPairArray != null) {
  99. try {
  100. _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
  101. }
  102. catch {
  103. // exception is thrown when getting PublicKey
  104. // to match MS implementation
  105. _keyPairArray = null;
  106. }
  107. }
  108. #if !MOBILE
  109. else if (_keyPairContainer != null) {
  110. CspParameters csp = new CspParameters ();
  111. csp.KeyContainerName = _keyPairContainer;
  112. _rsa = new RSACryptoServiceProvider (csp);
  113. }
  114. #endif
  115. return _rsa;
  116. }
  117. private void LoadKey (byte[] key)
  118. {
  119. try {
  120. // check for ECMA key
  121. if (key.Length == 16) {
  122. int i = 0;
  123. int sum = 0;
  124. while (i < key.Length)
  125. sum += key [i++];
  126. if (sum == 4) {
  127. // it is the ECMA key
  128. _publicKey = (byte[]) key.Clone ();
  129. }
  130. }
  131. else
  132. _keyPairArray = key;
  133. }
  134. catch
  135. {
  136. // exception is thrown when getting PublicKey
  137. // to match MS implementation
  138. }
  139. }
  140. public byte[] PublicKey {
  141. get {
  142. if (_publicKey == null) {
  143. RSA rsa = GetRSA ();
  144. // ECMA "key" is valid but doesn't produce a RSA instance
  145. if (rsa == null)
  146. throw new ArgumentException ("invalid keypair");
  147. byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
  148. _publicKey = new byte [blob.Length + 12];
  149. // The first 12 bytes are documented at:
  150. // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
  151. // ALG_ID - Signature
  152. _publicKey[0] = 0x00;
  153. _publicKey[1] = 0x24;
  154. _publicKey[2] = 0x00;
  155. _publicKey[3] = 0x00;
  156. // ALG_ID - Hash
  157. _publicKey[4] = 0x04;
  158. _publicKey[5] = 0x80;
  159. _publicKey[6] = 0x00;
  160. _publicKey[7] = 0x00;
  161. // Length of Public Key (in bytes)
  162. int lastPart = blob.Length;
  163. _publicKey[8] = (byte)(lastPart % 256);
  164. _publicKey[9] = (byte)(lastPart / 256); // just in case
  165. _publicKey[10] = 0x00;
  166. _publicKey[11] = 0x00;
  167. Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
  168. }
  169. return _publicKey;
  170. }
  171. }
  172. internal StrongName StrongName ()
  173. {
  174. RSA rsa = GetRSA ();
  175. if (rsa != null)
  176. return new StrongName (rsa);
  177. if (_publicKey != null)
  178. return new StrongName (_publicKey);
  179. return null;
  180. }
  181. }
  182. }