PrivateKeyFile.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Renci.SshNet.Security;
  8. using System.Security.Cryptography;
  9. using System.Security;
  10. using Renci.SshNet.Common;
  11. using System.Globalization;
  12. using Renci.SshNet.Security.Cryptography;
  13. using Renci.SshNet.Security.Cryptography.Ciphers;
  14. using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
  15. using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
  16. using System.Diagnostics.CodeAnalysis;
  17. namespace Renci.SshNet
  18. {
  19. /// <summary>
  20. /// old private key information/
  21. /// </summary>
  22. public class PrivateKeyFile : IDisposable
  23. {
  24. #if SILVERLIGHT
  25. private static Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,72}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+", RegexOptions.Multiline);
  26. #else
  27. private static Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,72}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+", RegexOptions.Compiled | RegexOptions.Multiline);
  28. #endif
  29. private Key _key;
  30. /// <summary>
  31. /// Gets the host key.
  32. /// </summary>
  33. public HostAlgorithm HostKey { get; private set; }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="PrivateKeyFile"/> class.
  36. /// </summary>
  37. /// <param name="privateKey">The private key.</param>
  38. public PrivateKeyFile(Stream privateKey)
  39. {
  40. this.Open(privateKey, null);
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="PrivateKeyFile"/> class.
  44. /// </summary>
  45. /// <param name="fileName">Name of the file.</param>
  46. /// <exception cref="ArgumentNullException"><paramref name="fileName"/> is null or empty.</exception>
  47. /// <remarks>This method calls <see cref="System.IO.File.Open(string, System.IO.FileMode)"/> internally, this method does not catch exceptions from <see cref="System.IO.File.Open(string, System.IO.FileMode)"/>.</remarks>
  48. public PrivateKeyFile(string fileName)
  49. {
  50. if (string.IsNullOrEmpty(fileName))
  51. throw new ArgumentNullException("fileName");
  52. using (var keyFile = File.Open(fileName, FileMode.Open))
  53. {
  54. this.Open(keyFile, null);
  55. }
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="PrivateKeyFile"/> class.
  59. /// </summary>
  60. /// <param name="fileName">Name of the file.</param>
  61. /// <param name="passPhrase">The pass phrase.</param>
  62. /// <exception cref="ArgumentNullException"><paramref name="fileName"/> is null or empty, or <paramref name="passPhrase"/> is null.</exception>
  63. /// <remarks>This method calls <see cref="System.IO.File.Open(string, System.IO.FileMode)"/> internally, this method does not catch exceptions from <see cref="System.IO.File.Open(string, System.IO.FileMode)"/>.</remarks>
  64. public PrivateKeyFile(string fileName, string passPhrase)
  65. {
  66. if (string.IsNullOrEmpty(fileName))
  67. throw new ArgumentNullException("fileName");
  68. using (var keyFile = File.Open(fileName, FileMode.Open))
  69. {
  70. this.Open(keyFile, passPhrase);
  71. }
  72. }
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref="PrivateKeyFile"/> class.
  75. /// </summary>
  76. /// <param name="privateKey">The private key.</param>
  77. /// <param name="passPhrase">The pass phrase.</param>
  78. /// <exception cref="ArgumentNullException"><paramref name="privateKey"/> or <paramref name="passPhrase"/> is null.</exception>
  79. public PrivateKeyFile(Stream privateKey, string passPhrase)
  80. {
  81. this.Open(privateKey, passPhrase);
  82. }
  83. /// <summary>
  84. /// Opens the specified private key.
  85. /// </summary>
  86. /// <param name="privateKey">The private key.</param>
  87. /// <param name="passPhrase">The pass phrase.</param>
  88. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "this._key disposed in Dispose(bool) method.")]
  89. private void Open(Stream privateKey, string passPhrase)
  90. {
  91. if (privateKey == null)
  92. throw new ArgumentNullException("privateKey");
  93. Match privateKeyMatch = null;
  94. using (StreamReader sr = new StreamReader(privateKey))
  95. {
  96. var text = sr.ReadToEnd();
  97. privateKeyMatch = _privateKeyRegex.Match(text);
  98. }
  99. if (!privateKeyMatch.Success)
  100. {
  101. throw new SshException("Invalid private key file.");
  102. }
  103. var keyName = privateKeyMatch.Result("${keyName}");
  104. var cipherName = privateKeyMatch.Result("${cipherName}");
  105. var salt = privateKeyMatch.Result("${salt}");
  106. var data = privateKeyMatch.Result("${data}");
  107. var binaryData = System.Convert.FromBase64String(data);
  108. byte[] decryptedData;
  109. if (!string.IsNullOrEmpty(cipherName) && !string.IsNullOrEmpty(salt))
  110. {
  111. if (string.IsNullOrEmpty(passPhrase))
  112. throw new SshPassPhraseNullOrEmptyException("Private key is encrypted but passphrase is empty.");
  113. byte[] binarySalt = new byte[salt.Length / 2];
  114. for (int i = 0; i < binarySalt.Length; i++)
  115. binarySalt[i] = Convert.ToByte(salt.Substring(i * 2, 2), 16);
  116. CipherInfo cipher = null;
  117. switch (cipherName)
  118. {
  119. case "DES-EDE3-CBC":
  120. cipher = new CipherInfo(192, (key, iv) => { return new TripleDesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
  121. break;
  122. case "DES-EDE3-CFB":
  123. cipher = new CipherInfo(192, (key, iv) => { return new TripleDesCipher(key, new CfbCipherMode(iv), new PKCS7Padding()); });
  124. break;
  125. case "DES-CBC":
  126. cipher = new CipherInfo(64, (key, iv) => { return new DesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
  127. break;
  128. // TODO: Implement more private key ciphers
  129. //case "AES-128-CBC":
  130. // cipher = new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS5Padding()); });
  131. // break;
  132. //case "AES-192-CBC":
  133. // cipher = new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS5Padding()); });
  134. // break;
  135. //case "AES-256-CBC":
  136. // cipher = new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS5Padding()); });
  137. // break;
  138. default:
  139. throw new SshException(string.Format(CultureInfo.CurrentCulture, "Private key cipher \"{0}\" is not supported.", cipherName));
  140. }
  141. decryptedData = DecryptKey(cipher, binaryData, passPhrase, binarySalt);
  142. }
  143. else
  144. {
  145. decryptedData = binaryData;
  146. }
  147. switch (keyName)
  148. {
  149. case "RSA":
  150. this._key = new RsaKey(decryptedData.ToArray());
  151. this.HostKey = new KeyHostAlgorithm("ssh-rsa", this._key);
  152. break;
  153. case "DSA":
  154. this._key = new DsaKey(decryptedData.ToArray());
  155. this.HostKey = new KeyHostAlgorithm("ssh-dss", this._key);
  156. break;
  157. case "SSH2 ENCRYPTED":
  158. var reader = new SshDataReader(decryptedData);
  159. var magicNumber = reader.ReadUInt32();
  160. if (magicNumber != 0x3f6ff9eb)
  161. {
  162. throw new SshException("Invalid SSH2 private key.");
  163. }
  164. var totalLength = reader.ReadUInt32(); // Read total bytes length including magic number
  165. var keyType = reader.ReadString();
  166. var ssh2CipherName = reader.ReadString();
  167. var blobSize = (int)reader.ReadUInt32();
  168. byte[] keyData = null;
  169. if (ssh2CipherName == "none")
  170. {
  171. keyData = reader.ReadBytes(blobSize);
  172. }
  173. //else if (ssh2CipherName == "3des-cbc")
  174. //{
  175. // var key = GetCipherKey(passPhrase, 192 / 8);
  176. // var ssh2Сipher = new TripleDesCipher(key, null, null);
  177. // keyData = ssh2Сipher.Decrypt(reader.ReadBytes(blobSize));
  178. //}
  179. else
  180. {
  181. throw new SshException(string.Format("Cipher method '{0}' is not supported.", cipherName));
  182. }
  183. reader = new SshDataReader(keyData);
  184. var decryptedLength = reader.ReadUInt32();
  185. if (decryptedLength + 4 != blobSize)
  186. throw new SshException("Invalid passphrase.");
  187. if (keyType == "if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}")
  188. {
  189. var exponent = reader.ReadBigIntWithBits();//e
  190. var d = reader.ReadBigIntWithBits();//d
  191. var modulus = reader.ReadBigIntWithBits();//n
  192. var inverseQ = reader.ReadBigIntWithBits();//u
  193. var q = reader.ReadBigIntWithBits();//p
  194. var p = reader.ReadBigIntWithBits();//q
  195. this._key = new RsaKey(modulus, exponent, d, p, q, inverseQ);
  196. this.HostKey = new KeyHostAlgorithm("ssh-rsa", this._key);
  197. }
  198. else if (keyType == "dl-modp{sign{dsa-nist-sha1},dh{plain}}")
  199. {
  200. var zero = reader.ReadUInt32();
  201. if (zero != 0)
  202. {
  203. throw new SshException("Invalid private key");
  204. }
  205. var p = reader.ReadBigIntWithBits();
  206. var g = reader.ReadBigIntWithBits();
  207. var q = reader.ReadBigIntWithBits();
  208. var y = reader.ReadBigIntWithBits();
  209. var x = reader.ReadBigIntWithBits();
  210. this._key = new DsaKey(p, q, g, y, x);
  211. this.HostKey = new KeyHostAlgorithm("ssh-dss", this._key);
  212. }
  213. else
  214. {
  215. throw new NotSupportedException(string.Format("Key type '{0}' is not supported.", keyType));
  216. }
  217. break;
  218. default:
  219. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Key '{0}' is not supported.", keyName));
  220. }
  221. }
  222. private static byte[] GetCipherKey(string passphrase, int length)
  223. {
  224. List<byte> cipherKey = new List<byte>();
  225. using (var md5 = new MD5Hash())
  226. {
  227. byte[] passwordBytes = Encoding.UTF8.GetBytes(passphrase);
  228. var hash = md5.ComputeHash(passwordBytes.ToArray()).AsEnumerable();
  229. cipherKey.AddRange(hash);
  230. while (cipherKey.Count < length)
  231. {
  232. hash = passwordBytes.Concat(hash);
  233. hash = md5.ComputeHash(hash.ToArray());
  234. cipherKey.AddRange(hash);
  235. }
  236. }
  237. return cipherKey.Take(length).ToArray();
  238. }
  239. /// <summary>
  240. /// Decrypts encrypted private key file data.
  241. /// </summary>
  242. /// <param name="cipherInfo">The cipher info.</param>
  243. /// <param name="cipherData">Encrypted data.</param>
  244. /// <param name="passPhrase">Decryption pass phrase.</param>
  245. /// <param name="binarySalt">Decryption binary salt.</param>
  246. /// <returns></returns>
  247. /// <exception cref="ArgumentNullException"><paramref name="cipherInfo"/>, <paramref name="cipherData"/>, <paramref name="passPhrase"/> or <paramref name="binarySalt"/> is null.</exception>
  248. private static byte[] DecryptKey(CipherInfo cipherInfo, byte[] cipherData, string passPhrase, byte[] binarySalt)
  249. {
  250. if (cipherInfo == null)
  251. throw new ArgumentNullException("cipherInfo");
  252. if (cipherData == null)
  253. throw new ArgumentNullException("cipherData");
  254. if (binarySalt == null)
  255. throw new ArgumentNullException("binarySalt");
  256. List<byte> cipherKey = new List<byte>();
  257. using (var md5 = new MD5Hash())
  258. {
  259. var passwordBytes = Encoding.UTF8.GetBytes(passPhrase);
  260. var initVector = passwordBytes.Concat(binarySalt);
  261. var hash = md5.ComputeHash(initVector.ToArray()).AsEnumerable();
  262. cipherKey.AddRange(hash);
  263. while (cipherKey.Count < cipherInfo.KeySize / 8)
  264. {
  265. hash = hash.Concat(initVector);
  266. hash = md5.ComputeHash(hash.ToArray());
  267. cipherKey.AddRange(hash);
  268. }
  269. }
  270. var cipher = cipherInfo.Cipher(cipherKey.ToArray(), binarySalt);
  271. return cipher.Decrypt(cipherData);
  272. }
  273. #region IDisposable Members
  274. private bool _isDisposed = false;
  275. /// <summary>
  276. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  277. /// </summary>
  278. public void Dispose()
  279. {
  280. Dispose(true);
  281. GC.SuppressFinalize(this);
  282. }
  283. /// <summary>
  284. /// Releases unmanaged and - optionally - managed resources
  285. /// </summary>
  286. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  287. protected virtual void Dispose(bool disposing)
  288. {
  289. // Check to see if Dispose has already been called.
  290. if (!this._isDisposed)
  291. {
  292. // If disposing equals true, dispose all managed
  293. // and unmanaged ResourceMessages.
  294. if (disposing)
  295. {
  296. // Dispose managed ResourceMessages.
  297. if (this._key != null)
  298. {
  299. ((IDisposable)this._key).Dispose();
  300. this._key = null;
  301. }
  302. }
  303. // Note disposing has been done.
  304. _isDisposed = true;
  305. }
  306. }
  307. /// <summary>
  308. /// Releases unmanaged resources and performs other cleanup operations before the
  309. /// <see cref="BaseClient"/> is reclaimed by garbage collection.
  310. /// </summary>
  311. ~PrivateKeyFile()
  312. {
  313. // Do not re-create Dispose clean-up code here.
  314. // Calling Dispose(false) is optimal in terms of
  315. // readability and maintainability.
  316. Dispose(false);
  317. }
  318. #endregion
  319. private class SshDataReader : SshData
  320. {
  321. public SshDataReader(byte[] data)
  322. {
  323. this.LoadBytes(data);
  324. }
  325. public UInt32 ReadUInt32()
  326. {
  327. return base.ReadUInt32();
  328. }
  329. public string ReadString()
  330. {
  331. return base.ReadString();
  332. }
  333. public byte[] ReadBytes(int length)
  334. {
  335. return base.ReadBytes(length);
  336. }
  337. /// <summary>
  338. /// Reads next mpint data type from internal buffer where length specified in bits.
  339. /// </summary>
  340. /// <returns>mpint read.</returns>
  341. public BigInteger ReadBigIntWithBits()
  342. {
  343. var length = (int)base.ReadUInt32();
  344. length = (int)(length + 7) / 8;
  345. var data = base.ReadBytes(length);
  346. var bytesArray = new byte[data.Length + 1];
  347. Buffer.BlockCopy(data, 0, bytesArray, 1, data.Length);
  348. return new BigInteger(bytesArray.Reverse().ToArray());
  349. }
  350. protected override void LoadData()
  351. {
  352. }
  353. protected override void SaveData()
  354. {
  355. }
  356. }
  357. }
  358. }