RsaKey.cs 7.8 KB

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