CryptoConvert.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // CryptoConvert.cs - Crypto Convertion Routines
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004-2006 Novell Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if !READ_ONLY
  30. #if !NET_CORE
  31. using System;
  32. using System.Security.Cryptography;
  33. namespace Mono.Security.Cryptography {
  34. static class CryptoConvert {
  35. static private int ToInt32LE (byte [] bytes, int offset)
  36. {
  37. return (bytes [offset+3] << 24) | (bytes [offset+2] << 16) | (bytes [offset+1] << 8) | bytes [offset];
  38. }
  39. static private uint ToUInt32LE (byte [] bytes, int offset)
  40. {
  41. return (uint)((bytes [offset+3] << 24) | (bytes [offset+2] << 16) | (bytes [offset+1] << 8) | bytes [offset]);
  42. }
  43. static private byte[] Trim (byte[] array)
  44. {
  45. for (int i=0; i < array.Length; i++) {
  46. if (array [i] != 0x00) {
  47. byte[] result = new byte [array.Length - i];
  48. Buffer.BlockCopy (array, i, result, 0, result.Length);
  49. return result;
  50. }
  51. }
  52. return null;
  53. }
  54. static RSA FromCapiPrivateKeyBlob (byte[] blob, int offset)
  55. {
  56. RSAParameters rsap = new RSAParameters ();
  57. try {
  58. if ((blob [offset] != 0x07) || // PRIVATEKEYBLOB (0x07)
  59. (blob [offset+1] != 0x02) || // Version (0x02)
  60. (blob [offset+2] != 0x00) || // Reserved (word)
  61. (blob [offset+3] != 0x00) ||
  62. (ToUInt32LE (blob, offset+8) != 0x32415352)) // DWORD magic = RSA2
  63. throw new CryptographicException ("Invalid blob header");
  64. // ALGID (CALG_RSA_SIGN, CALG_RSA_KEYX, ...)
  65. // int algId = ToInt32LE (blob, offset+4);
  66. // DWORD bitlen
  67. int bitLen = ToInt32LE (blob, offset+12);
  68. // DWORD public exponent
  69. byte[] exp = new byte [4];
  70. Buffer.BlockCopy (blob, offset+16, exp, 0, 4);
  71. Array.Reverse (exp);
  72. rsap.Exponent = Trim (exp);
  73. int pos = offset+20;
  74. // BYTE modulus[rsapubkey.bitlen/8];
  75. int byteLen = (bitLen >> 3);
  76. rsap.Modulus = new byte [byteLen];
  77. Buffer.BlockCopy (blob, pos, rsap.Modulus, 0, byteLen);
  78. Array.Reverse (rsap.Modulus);
  79. pos += byteLen;
  80. // BYTE prime1[rsapubkey.bitlen/16];
  81. int byteHalfLen = (byteLen >> 1);
  82. rsap.P = new byte [byteHalfLen];
  83. Buffer.BlockCopy (blob, pos, rsap.P, 0, byteHalfLen);
  84. Array.Reverse (rsap.P);
  85. pos += byteHalfLen;
  86. // BYTE prime2[rsapubkey.bitlen/16];
  87. rsap.Q = new byte [byteHalfLen];
  88. Buffer.BlockCopy (blob, pos, rsap.Q, 0, byteHalfLen);
  89. Array.Reverse (rsap.Q);
  90. pos += byteHalfLen;
  91. // BYTE exponent1[rsapubkey.bitlen/16];
  92. rsap.DP = new byte [byteHalfLen];
  93. Buffer.BlockCopy (blob, pos, rsap.DP, 0, byteHalfLen);
  94. Array.Reverse (rsap.DP);
  95. pos += byteHalfLen;
  96. // BYTE exponent2[rsapubkey.bitlen/16];
  97. rsap.DQ = new byte [byteHalfLen];
  98. Buffer.BlockCopy (blob, pos, rsap.DQ, 0, byteHalfLen);
  99. Array.Reverse (rsap.DQ);
  100. pos += byteHalfLen;
  101. // BYTE coefficient[rsapubkey.bitlen/16];
  102. rsap.InverseQ = new byte [byteHalfLen];
  103. Buffer.BlockCopy (blob, pos, rsap.InverseQ, 0, byteHalfLen);
  104. Array.Reverse (rsap.InverseQ);
  105. pos += byteHalfLen;
  106. // ok, this is hackish but CryptoAPI support it so...
  107. // note: only works because CRT is used by default
  108. // http://bugzilla.ximian.com/show_bug.cgi?id=57941
  109. rsap.D = new byte [byteLen]; // must be allocated
  110. if (pos + byteLen + offset <= blob.Length) {
  111. // BYTE privateExponent[rsapubkey.bitlen/8];
  112. Buffer.BlockCopy (blob, pos, rsap.D, 0, byteLen);
  113. Array.Reverse (rsap.D);
  114. }
  115. }
  116. catch (Exception e) {
  117. throw new CryptographicException ("Invalid blob.", e);
  118. }
  119. RSA rsa = null;
  120. try {
  121. rsa = RSA.Create ();
  122. rsa.ImportParameters (rsap);
  123. }
  124. catch (CryptographicException) {
  125. // this may cause problem when this code is run under
  126. // the SYSTEM identity on Windows (e.g. ASP.NET). See
  127. // http://bugzilla.ximian.com/show_bug.cgi?id=77559
  128. bool throws = false;
  129. try {
  130. CspParameters csp = new CspParameters ();
  131. csp.Flags = CspProviderFlags.UseMachineKeyStore;
  132. rsa = new RSACryptoServiceProvider (csp);
  133. rsa.ImportParameters (rsap);
  134. }
  135. catch {
  136. throws = true;
  137. }
  138. if (throws) {
  139. // rethrow original, not the latter, exception if this fails
  140. throw;
  141. }
  142. }
  143. return rsa;
  144. }
  145. static RSA FromCapiPublicKeyBlob (byte[] blob, int offset)
  146. {
  147. try {
  148. if ((blob [offset] != 0x06) || // PUBLICKEYBLOB (0x06)
  149. (blob [offset+1] != 0x02) || // Version (0x02)
  150. (blob [offset+2] != 0x00) || // Reserved (word)
  151. (blob [offset+3] != 0x00) ||
  152. (ToUInt32LE (blob, offset+8) != 0x31415352)) // DWORD magic = RSA1
  153. throw new CryptographicException ("Invalid blob header");
  154. // ALGID (CALG_RSA_SIGN, CALG_RSA_KEYX, ...)
  155. // int algId = ToInt32LE (blob, offset+4);
  156. // DWORD bitlen
  157. int bitLen = ToInt32LE (blob, offset+12);
  158. // DWORD public exponent
  159. RSAParameters rsap = new RSAParameters ();
  160. rsap.Exponent = new byte [3];
  161. rsap.Exponent [0] = blob [offset+18];
  162. rsap.Exponent [1] = blob [offset+17];
  163. rsap.Exponent [2] = blob [offset+16];
  164. int pos = offset+20;
  165. // BYTE modulus[rsapubkey.bitlen/8];
  166. int byteLen = (bitLen >> 3);
  167. rsap.Modulus = new byte [byteLen];
  168. Buffer.BlockCopy (blob, pos, rsap.Modulus, 0, byteLen);
  169. Array.Reverse (rsap.Modulus);
  170. RSA rsa = null;
  171. try {
  172. rsa = RSA.Create ();
  173. rsa.ImportParameters (rsap);
  174. }
  175. catch (CryptographicException) {
  176. // this may cause problem when this code is run under
  177. // the SYSTEM identity on Windows (e.g. ASP.NET). See
  178. // http://bugzilla.ximian.com/show_bug.cgi?id=77559
  179. CspParameters csp = new CspParameters ();
  180. csp.Flags = CspProviderFlags.UseMachineKeyStore;
  181. rsa = new RSACryptoServiceProvider (csp);
  182. rsa.ImportParameters (rsap);
  183. }
  184. return rsa;
  185. }
  186. catch (Exception e) {
  187. throw new CryptographicException ("Invalid blob.", e);
  188. }
  189. }
  190. // PRIVATEKEYBLOB
  191. // PUBLICKEYBLOB
  192. static public RSA FromCapiKeyBlob (byte[] blob)
  193. {
  194. return FromCapiKeyBlob (blob, 0);
  195. }
  196. static public RSA FromCapiKeyBlob (byte[] blob, int offset)
  197. {
  198. if (blob == null)
  199. throw new ArgumentNullException ("blob");
  200. if (offset >= blob.Length)
  201. throw new ArgumentException ("blob is too small.");
  202. switch (blob [offset]) {
  203. case 0x00:
  204. // this could be a public key inside an header
  205. // like "sn -e" would produce
  206. if (blob [offset + 12] == 0x06) {
  207. return FromCapiPublicKeyBlob (blob, offset + 12);
  208. }
  209. break;
  210. case 0x06:
  211. return FromCapiPublicKeyBlob (blob, offset);
  212. case 0x07:
  213. return FromCapiPrivateKeyBlob (blob, offset);
  214. }
  215. throw new CryptographicException ("Unknown blob format.");
  216. }
  217. }
  218. }
  219. #endif
  220. #endif