StrongNameKeyPair.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // (C) 2004 Novell (http://www.novell.com)
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.IO;
  35. using System.Security.Cryptography;
  36. using Mono.Security;
  37. using Mono.Security.Cryptography;
  38. namespace System.Reflection {
  39. [Serializable]
  40. public class StrongNameKeyPair
  41. {
  42. private byte[] _publicKey;
  43. private string _keyPairContainer;
  44. private bool _keyPairExported;
  45. private byte[] _keyPairArray;
  46. [NonSerialized]
  47. private RSA _rsa;
  48. public StrongNameKeyPair (byte[] keyPairArray)
  49. {
  50. if (keyPairArray == null)
  51. throw new ArgumentNullException ("keyPairArray");
  52. LoadKey (keyPairArray);
  53. GetRSA ();
  54. }
  55. public StrongNameKeyPair (FileStream keyPairFile)
  56. {
  57. if (keyPairFile == null)
  58. throw new ArgumentNullException ("keyPairFile");
  59. byte[] input = new byte [keyPairFile.Length];
  60. keyPairFile.Read (input, 0, input.Length);
  61. LoadKey (input);
  62. GetRSA ();
  63. }
  64. public StrongNameKeyPair (string keyPairContainer)
  65. {
  66. // named key container
  67. if (keyPairContainer == null)
  68. throw new ArgumentNullException ("keyPairContainer");
  69. _keyPairContainer = keyPairContainer;
  70. GetRSA ();
  71. }
  72. private RSA GetRSA ()
  73. {
  74. if (_rsa != null) return _rsa;
  75. if (_keyPairArray != null) {
  76. try {
  77. _rsa = CryptoConvert.FromCapiKeyBlob (_keyPairArray);
  78. }
  79. catch {
  80. // exception is thrown when getting PublicKey
  81. // to match MS implementation
  82. _keyPairArray = null;
  83. }
  84. }
  85. else if (_keyPairContainer != null) {
  86. CspParameters csp = new CspParameters ();
  87. csp.KeyContainerName = _keyPairContainer;
  88. _rsa = new RSACryptoServiceProvider (csp);
  89. }
  90. return _rsa;
  91. }
  92. private void LoadKey (byte[] key)
  93. {
  94. try {
  95. // check for ECMA key
  96. if (key.Length == 16) {
  97. int i = 0;
  98. int sum = 0;
  99. while (i < key.Length)
  100. sum += key [i++];
  101. if (sum == 4) {
  102. // it is the ECMA key
  103. _publicKey = (byte[]) key.Clone ();
  104. }
  105. }
  106. else
  107. _keyPairArray = key;
  108. }
  109. catch
  110. {
  111. // exception is thrown when getting PublicKey
  112. // to match MS implementation
  113. }
  114. }
  115. public byte[] PublicKey {
  116. get {
  117. if (_publicKey == null) {
  118. RSA rsa = GetRSA ();
  119. // ECMA "key" is valid but doesn't produce a RSA instance
  120. if (rsa == null)
  121. throw new ArgumentException ("invalid keypair");
  122. byte[] blob = CryptoConvert.ToCapiKeyBlob (rsa, false);
  123. _publicKey = new byte [blob.Length + 12];
  124. // The first 12 bytes are documented at:
  125. // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
  126. // ALG_ID - Signature
  127. _publicKey[0] = 0x00;
  128. _publicKey[1] = 0x24;
  129. _publicKey[2] = 0x00;
  130. _publicKey[3] = 0x00;
  131. // ALG_ID - Hash
  132. _publicKey[4] = 0x04;
  133. _publicKey[5] = 0x80;
  134. _publicKey[6] = 0x00;
  135. _publicKey[7] = 0x00;
  136. // Length of Public Key (in bytes)
  137. int lastPart = blob.Length;
  138. _publicKey[8] = (byte)(lastPart % 256);
  139. _publicKey[9] = (byte)(lastPart / 256); // just in case
  140. _publicKey[10] = 0x00;
  141. _publicKey[11] = 0x00;
  142. Buffer.BlockCopy (blob, 0, _publicKey, 12, blob.Length);
  143. }
  144. return _publicKey;
  145. }
  146. }
  147. internal StrongName StrongName ()
  148. {
  149. RSA rsa = GetRSA ();
  150. if (rsa != null)
  151. return new StrongName (rsa);
  152. if (_publicKey != null)
  153. return new StrongName (_publicKey);
  154. return null;
  155. }
  156. }
  157. }