RsaKey.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Security.Cryptography;
  4. namespace Renci.SshNet.Security
  5. {
  6. /// <summary>
  7. /// Contains RSA private and public key
  8. /// </summary>
  9. public class RsaKey : Key, IDisposable
  10. {
  11. /// <summary>
  12. /// Gets the modulus.
  13. /// </summary>
  14. public BigInteger Modulus
  15. {
  16. get
  17. {
  18. return _privateKey[0];
  19. }
  20. }
  21. /// <summary>
  22. /// Gets the exponent.
  23. /// </summary>
  24. public BigInteger Exponent
  25. {
  26. get
  27. {
  28. return _privateKey[1];
  29. }
  30. }
  31. /// <summary>
  32. /// Gets the D.
  33. /// </summary>
  34. public BigInteger D
  35. {
  36. get
  37. {
  38. if (_privateKey.Length > 2)
  39. return _privateKey[2];
  40. return BigInteger.Zero;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets the P.
  45. /// </summary>
  46. public BigInteger P
  47. {
  48. get
  49. {
  50. if (_privateKey.Length > 3)
  51. return _privateKey[3];
  52. return BigInteger.Zero;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets the Q.
  57. /// </summary>
  58. public BigInteger Q
  59. {
  60. get
  61. {
  62. if (_privateKey.Length > 4)
  63. return _privateKey[4];
  64. return BigInteger.Zero;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets the DP.
  69. /// </summary>
  70. public BigInteger DP
  71. {
  72. get
  73. {
  74. if (_privateKey.Length > 5)
  75. return _privateKey[5];
  76. return BigInteger.Zero;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the DQ.
  81. /// </summary>
  82. public BigInteger DQ
  83. {
  84. get
  85. {
  86. if (_privateKey.Length > 6)
  87. return _privateKey[6];
  88. return BigInteger.Zero;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets the inverse Q.
  93. /// </summary>
  94. public BigInteger InverseQ
  95. {
  96. get
  97. {
  98. if (_privateKey.Length > 7)
  99. return _privateKey[7];
  100. return BigInteger.Zero;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets the length of the key.
  105. /// </summary>
  106. /// <value>
  107. /// The length of the key.
  108. /// </value>
  109. public override int KeyLength
  110. {
  111. get
  112. {
  113. return Modulus.BitLength;
  114. }
  115. }
  116. private RsaDigitalSignature _digitalSignature;
  117. /// <summary>
  118. /// Gets the digital signature.
  119. /// </summary>
  120. protected override DigitalSignature DigitalSignature
  121. {
  122. get
  123. {
  124. if (_digitalSignature == null)
  125. {
  126. _digitalSignature = new RsaDigitalSignature(this);
  127. }
  128. return _digitalSignature;
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the public.
  133. /// </summary>
  134. /// <value>
  135. /// The public.
  136. /// </value>
  137. public override BigInteger[] Public
  138. {
  139. get
  140. {
  141. return new[] { Exponent, Modulus };
  142. }
  143. set
  144. {
  145. if (value.Length != 2)
  146. throw new InvalidOperationException("Invalid private key.");
  147. _privateKey = new[] { value[1], value[0] };
  148. }
  149. }
  150. /// <summary>
  151. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  152. /// </summary>
  153. public RsaKey()
  154. {
  155. }
  156. /// <summary>
  157. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  158. /// </summary>
  159. /// <param name="data">DER encoded private key data.</param>
  160. public RsaKey(byte[] data)
  161. : base(data)
  162. {
  163. if (_privateKey.Length != 8)
  164. throw new InvalidOperationException("Invalid private key.");
  165. }
  166. /// <summary>
  167. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  168. /// </summary>
  169. /// <param name="modulus">The modulus.</param>
  170. /// <param name="exponent">The exponent.</param>
  171. /// <param name="d">The d.</param>
  172. /// <param name="p">The p.</param>
  173. /// <param name="q">The q.</param>
  174. /// <param name="inverseQ">The inverse Q.</param>
  175. public RsaKey(BigInteger modulus, BigInteger exponent, BigInteger d, BigInteger p, BigInteger q, BigInteger inverseQ)
  176. {
  177. _privateKey = new BigInteger[8];
  178. _privateKey[0] = modulus;
  179. _privateKey[1] = exponent;
  180. _privateKey[2] = d;
  181. _privateKey[3] = p;
  182. _privateKey[4] = q;
  183. _privateKey[5] = PrimeExponent(d, p);
  184. _privateKey[6] = PrimeExponent(d, q);
  185. _privateKey[7] = inverseQ;
  186. }
  187. private static BigInteger PrimeExponent(BigInteger privateExponent, BigInteger prime)
  188. {
  189. BigInteger pe = prime - new BigInteger(1);
  190. return privateExponent % pe;
  191. }
  192. #region IDisposable Members
  193. private bool _isDisposed;
  194. /// <summary>
  195. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  196. /// </summary>
  197. public void Dispose()
  198. {
  199. Dispose(true);
  200. GC.SuppressFinalize(this);
  201. }
  202. /// <summary>
  203. /// Releases unmanaged and - optionally - managed resources
  204. /// </summary>
  205. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  206. protected virtual void Dispose(bool disposing)
  207. {
  208. // Check to see if Dispose has already been called.
  209. if (!_isDisposed)
  210. {
  211. // If disposing equals true, dispose all managed
  212. // and unmanaged ResourceMessages.
  213. if (disposing)
  214. {
  215. // Dispose managed ResourceMessages.
  216. if (_digitalSignature != null)
  217. {
  218. _digitalSignature.Dispose();
  219. _digitalSignature = null;
  220. }
  221. }
  222. // Note disposing has been done.
  223. _isDisposed = true;
  224. }
  225. }
  226. /// <summary>
  227. /// Releases unmanaged resources and performs other cleanup operations before the
  228. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  229. /// </summary>
  230. ~RsaKey()
  231. {
  232. // Do not re-create Dispose clean-up code here.
  233. // Calling Dispose(false) is optimal in terms of
  234. // readability and maintainability.
  235. Dispose(false);
  236. }
  237. #endregion
  238. }
  239. }