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