StrongNameKeyPair.cs 4.9 KB

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