RsaKey.cs 7.1 KB

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