PrivateKeyFileTest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. /// <summary>
  10. /// old private key information/
  11. /// </summary>
  12. [TestClass]
  13. public class PrivateKeyFileTest : TestBase
  14. {
  15. private string _temporaryFile;
  16. [TestInitialize]
  17. public void SetUp()
  18. {
  19. _temporaryFile = GetTempFileName();
  20. }
  21. [TestCleanup]
  22. public void TearDown()
  23. {
  24. if (_temporaryFile != null)
  25. {
  26. File.Delete(_temporaryFile);
  27. }
  28. }
  29. /// <summary>
  30. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  31. ///</summary>
  32. [WorkItem(703), TestMethod]
  33. public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
  34. {
  35. var fileName = string.Empty;
  36. try
  37. {
  38. _ = new PrivateKeyFile(fileName);
  39. Assert.Fail();
  40. }
  41. catch (ArgumentNullException ex)
  42. {
  43. Assert.IsNull(ex.InnerException);
  44. Assert.AreEqual("fileName", ex.ParamName);
  45. }
  46. }
  47. /// <summary>
  48. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  49. ///</summary>
  50. [WorkItem(703), TestMethod]
  51. public void ConstructorWithFileNameShouldThrowArgumentNullExceptionWhenFileNameIsNull()
  52. {
  53. var fileName = string.Empty;
  54. try
  55. {
  56. _ = new PrivateKeyFile(fileName);
  57. Assert.Fail();
  58. }
  59. catch (ArgumentNullException ex)
  60. {
  61. Assert.IsNull(ex.InnerException);
  62. Assert.AreEqual("fileName", ex.ParamName);
  63. }
  64. }
  65. /// <summary>
  66. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  67. ///</summary>
  68. [WorkItem(703), TestMethod]
  69. public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsEmpty()
  70. {
  71. var fileName = string.Empty;
  72. try
  73. {
  74. _ = new PrivateKeyFile(fileName, "12345");
  75. Assert.Fail();
  76. }
  77. catch (ArgumentNullException ex)
  78. {
  79. Assert.IsNull(ex.InnerException);
  80. Assert.AreEqual("fileName", ex.ParamName);
  81. }
  82. }
  83. /// <summary>
  84. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  85. ///</summary>
  86. [WorkItem(703), TestMethod]
  87. public void ConstructorWithFileNameAndPassphraseShouldThrowArgumentNullExceptionWhenFileNameIsNull()
  88. {
  89. var fileName = string.Empty;
  90. try
  91. {
  92. _ = new PrivateKeyFile(fileName, "12345");
  93. Assert.Fail();
  94. }
  95. catch (ArgumentNullException ex)
  96. {
  97. Assert.IsNull(ex.InnerException);
  98. Assert.AreEqual("fileName", ex.ParamName);
  99. }
  100. }
  101. [WorkItem(703), TestMethod]
  102. public void ConstructorWithPrivateKeyShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
  103. {
  104. Stream privateKey = null;
  105. try
  106. {
  107. _ = new PrivateKeyFile(privateKey);
  108. Assert.Fail();
  109. }
  110. catch (ArgumentNullException ex)
  111. {
  112. Assert.IsNull(ex.InnerException);
  113. Assert.AreEqual("privateKey", ex.ParamName);
  114. }
  115. }
  116. [WorkItem(703), TestMethod]
  117. public void ConstructorWithPrivateKeyAndPassphraseShouldThrowArgumentNullExceptionWhenPrivateKeyIsNull()
  118. {
  119. Stream privateKey = null;
  120. try
  121. {
  122. _ = new PrivateKeyFile(privateKey, "12345");
  123. Assert.Fail();
  124. }
  125. catch (ArgumentNullException ex)
  126. {
  127. Assert.IsNull(ex.InnerException);
  128. Assert.AreEqual("privateKey", ex.ParamName);
  129. }
  130. }
  131. [TestMethod]
  132. [Owner("olegkap")]
  133. [TestCategory("PrivateKey")]
  134. public void Test_PrivateKey_RSA()
  135. {
  136. using (var stream = GetData("Key.RSA.txt"))
  137. {
  138. TestRsaKeyFile(new PrivateKeyFile(stream));
  139. }
  140. }
  141. [TestMethod]
  142. [Owner("drieseng")]
  143. [TestCategory("PrivateKey")]
  144. public void Test_PrivateKey_SSH2_DSA()
  145. {
  146. using (var stream = GetData("Key.SSH2.DSA.txt"))
  147. {
  148. _ = new PrivateKeyFile(stream);
  149. }
  150. }
  151. [TestMethod]
  152. [Owner("drieseng")]
  153. [TestCategory("PrivateKey")]
  154. public void Test_PrivateKey_SSH2_RSA()
  155. {
  156. using (var stream = GetData("Key.SSH2.RSA.txt"))
  157. {
  158. TestRsaKeyFile(new PrivateKeyFile(stream));
  159. }
  160. }
  161. [TestMethod]
  162. [Owner("drieseng")]
  163. [TestCategory("PrivateKey")]
  164. public void Test_PrivateKey_SSH2_Encrypted_DSA_DES_CBC()
  165. {
  166. using (var stream = GetData("Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt"))
  167. {
  168. _ = new PrivateKeyFile(stream, "12345");
  169. }
  170. }
  171. [TestMethod]
  172. [Owner("drieseng")]
  173. [TestCategory("PrivateKey")]
  174. public void Test_PrivateKey_SSH2_Encrypted_RSA_DES_CBC()
  175. {
  176. using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  177. {
  178. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  179. }
  180. }
  181. [TestMethod]
  182. [Owner("drieseng")]
  183. [TestCategory("PrivateKey")]
  184. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshExceptionWhenPassphraseIsWrong()
  185. {
  186. using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  187. {
  188. try
  189. {
  190. _ = new PrivateKeyFile(stream, "34567");
  191. Assert.Fail();
  192. }
  193. catch (SshException ex)
  194. {
  195. Assert.IsInstanceOfType<SshException>(ex);
  196. Assert.IsNull(ex.InnerException);
  197. Assert.AreEqual("Invalid passphrase.", ex.Message);
  198. }
  199. }
  200. }
  201. [TestMethod]
  202. [Owner("drieseng")]
  203. [TestCategory("PrivateKey")]
  204. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull()
  205. {
  206. using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  207. {
  208. try
  209. {
  210. _ = new PrivateKeyFile(stream, null);
  211. Assert.Fail();
  212. }
  213. catch (SshPassPhraseNullOrEmptyException ex)
  214. {
  215. Assert.IsInstanceOfType<SshPassPhraseNullOrEmptyException>(ex);
  216. Assert.IsNull(ex.InnerException);
  217. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  218. }
  219. }
  220. }
  221. [TestMethod]
  222. [Owner("drieseng")]
  223. [TestCategory("PrivateKey")]
  224. public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsEmpty()
  225. {
  226. using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
  227. {
  228. try
  229. {
  230. _ = new PrivateKeyFile(stream, string.Empty);
  231. Assert.Fail();
  232. }
  233. catch (SshPassPhraseNullOrEmptyException ex)
  234. {
  235. Assert.IsInstanceOfType<SshPassPhraseNullOrEmptyException>(ex);
  236. Assert.IsNull(ex.InnerException);
  237. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  238. }
  239. }
  240. }
  241. [TestMethod]
  242. [Owner("olegkap")]
  243. [TestCategory("PrivateKey")]
  244. public void Test_PrivateKey_RSA_DES_CBC()
  245. {
  246. using (var stream = GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
  247. {
  248. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  249. }
  250. }
  251. [TestMethod]
  252. [Owner("olegkap")]
  253. [TestCategory("PrivateKey")]
  254. public void Test_PrivateKey_RSA_DES_EDE3_CBC()
  255. {
  256. using (var stream = GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
  257. {
  258. _ = new PrivateKeyFile(stream, "12345");
  259. }
  260. }
  261. [TestMethod]
  262. [Owner("olegkap")]
  263. [TestCategory("PrivateKey")]
  264. public void Test_PrivateKey_RSA_AES_128_CBC()
  265. {
  266. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  267. {
  268. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  269. }
  270. }
  271. [TestMethod]
  272. [Owner("olegkap")]
  273. [TestCategory("PrivateKey")]
  274. public void Test_PrivateKey_RSA_AES_192_CBC()
  275. {
  276. using (var stream = GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
  277. {
  278. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  279. }
  280. }
  281. [TestMethod]
  282. [Owner("olegkap")]
  283. [TestCategory("PrivateKey")]
  284. public void Test_PrivateKey_RSA_AES_256_CBC()
  285. {
  286. using (var stream = GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
  287. {
  288. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  289. }
  290. }
  291. [TestMethod]
  292. [Owner("olegkap")]
  293. [TestCategory("PrivateKey")]
  294. public void Test_PrivateKey_RSA_DES_EDE3_CFB()
  295. {
  296. using (var stream = GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
  297. {
  298. TestRsaKeyFile(new PrivateKeyFile(stream, "1234567890"));
  299. }
  300. }
  301. [TestMethod]
  302. [Owner("darinkes")]
  303. [TestCategory("PrivateKey")]
  304. public void Test_PrivateKey_ECDSA()
  305. {
  306. using (var stream = GetData("Key.ECDSA.txt"))
  307. {
  308. _ = new PrivateKeyFile(stream);
  309. }
  310. }
  311. [TestMethod]
  312. [Owner("darinkes")]
  313. [TestCategory("PrivateKey")]
  314. public void Test_PrivateKey_ECDSA384()
  315. {
  316. using (var stream = GetData("Key.ECDSA384.txt"))
  317. {
  318. _ = new PrivateKeyFile(stream);
  319. }
  320. }
  321. [TestMethod]
  322. [Owner("darinkes")]
  323. [TestCategory("PrivateKey")]
  324. public void Test_PrivateKey_ECDSA521()
  325. {
  326. using (var stream = GetData("Key.ECDSA521.txt"))
  327. {
  328. _ = new PrivateKeyFile(stream);
  329. }
  330. }
  331. [TestMethod]
  332. [Owner("darinkes")]
  333. [TestCategory("PrivateKey")]
  334. public void Test_PrivateKey_ECDSA_Encrypted()
  335. {
  336. using (var stream = GetData("Key.ECDSA.Encrypted.txt"))
  337. {
  338. _ = new PrivateKeyFile(stream, "12345");
  339. }
  340. }
  341. [TestMethod]
  342. [Owner("darinkes")]
  343. [TestCategory("PrivateKey")]
  344. public void Test_PrivateKey_ECDSA384_Encrypted()
  345. {
  346. using (var stream = GetData("Key.ECDSA384.Encrypted.txt"))
  347. {
  348. _ = new PrivateKeyFile(stream, "12345");
  349. }
  350. }
  351. [TestMethod]
  352. [Owner("darinkes")]
  353. [TestCategory("PrivateKey")]
  354. public void Test_PrivateKey_ECDSA521_Encrypted()
  355. {
  356. using (var stream = GetData("Key.ECDSA521.Encrypted.txt"))
  357. {
  358. _ = new PrivateKeyFile(stream, "12345");
  359. }
  360. }
  361. /// <summary>
  362. ///A test for Dispose
  363. ///</summary>
  364. [TestMethod()]
  365. public void DisposeTest()
  366. {
  367. using (var privateKeyStream = GetData("Key.RSA.txt"))
  368. {
  369. var target = new PrivateKeyFile(privateKeyStream);
  370. target.Dispose();
  371. }
  372. }
  373. /// <summary>
  374. /// A test for <see cref="PrivateKeyFile(Stream, string)"/> ctor.
  375. ///</summary>
  376. [TestMethod()]
  377. public void ConstructorWithStreamAndPassphrase()
  378. {
  379. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  380. {
  381. var privateKeyFile = new PrivateKeyFile(stream, "12345");
  382. TestRsaKeyFile(privateKeyFile);
  383. }
  384. }
  385. /// <summary>
  386. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  387. ///</summary>
  388. [TestMethod()]
  389. public void ConstructorWithFileNameAndPassphrase()
  390. {
  391. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  392. {
  393. SaveStreamToFile(stream, _temporaryFile);
  394. }
  395. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  396. {
  397. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  398. TestRsaKeyFile(privateKeyFile);
  399. fs.Close();
  400. }
  401. }
  402. /// <summary>
  403. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  404. ///</summary>
  405. [TestMethod()]
  406. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenNeededPassphraseIsEmpty()
  407. {
  408. var passphrase = string.Empty;
  409. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  410. {
  411. SaveStreamToFile(stream, _temporaryFile);
  412. }
  413. try
  414. {
  415. _ = new PrivateKeyFile(_temporaryFile, passphrase);
  416. Assert.Fail();
  417. }
  418. catch (SshPassPhraseNullOrEmptyException ex)
  419. {
  420. Assert.IsNull(ex.InnerException);
  421. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  422. }
  423. }
  424. /// <summary>
  425. /// A test for <see cref="PrivateKeyFile(string, string)"/> ctor.
  426. ///</summary>
  427. [TestMethod()]
  428. public void ConstructorWithFileNameAndPassphraseShouldThrowSshPassPhraseNullOrEmptyExceptionWhenNeededPassphraseIsNull()
  429. {
  430. string passphrase = null;
  431. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  432. {
  433. SaveStreamToFile(stream, _temporaryFile);
  434. }
  435. try
  436. {
  437. _ = new PrivateKeyFile(_temporaryFile, passphrase);
  438. Assert.Fail();
  439. }
  440. catch (SshPassPhraseNullOrEmptyException ex)
  441. {
  442. Assert.IsNull(ex.InnerException);
  443. Assert.AreEqual("Private key is encrypted but passphrase is empty.", ex.Message);
  444. }
  445. }
  446. /// <summary>
  447. /// A test for <see cref="PrivateKeyFile(string)"/> ctor.
  448. ///</summary>
  449. [TestMethod()]
  450. public void ConstructorWithFileName()
  451. {
  452. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  453. {
  454. SaveStreamToFile(stream, _temporaryFile);
  455. }
  456. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  457. TestRsaKeyFile(privateKeyFile);
  458. }
  459. /// <summary>
  460. /// A test for <see cref="PrivateKeyFile(Stream)"/> ctor.
  461. ///</summary>
  462. [TestMethod()]
  463. public void ConstructorWithStream()
  464. {
  465. using (var stream = GetData("Key.RSA.txt"))
  466. {
  467. var privateKeyFile = new PrivateKeyFile(stream);
  468. TestRsaKeyFile(privateKeyFile);
  469. }
  470. }
  471. [TestMethod]
  472. [TestCategory("PrivateKey")]
  473. public void ConstructorWithFileNameShouldBeAbleToReadFileThatIsSharedForReadAccess()
  474. {
  475. using (var stream = GetData("Key.RSA.txt"))
  476. {
  477. SaveStreamToFile(stream, _temporaryFile);
  478. }
  479. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  480. {
  481. var privateKeyFile = new PrivateKeyFile(_temporaryFile);
  482. TestRsaKeyFile(privateKeyFile);
  483. fs.Close();
  484. }
  485. }
  486. [TestMethod]
  487. [TestCategory("PrivateKey")]
  488. public void ConstructorWithFileNameAndPassPhraseShouldBeAbleToReadFileThatIsSharedForReadAccess()
  489. {
  490. using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
  491. {
  492. SaveStreamToFile(stream, _temporaryFile);
  493. }
  494. using (var fs = File.Open(_temporaryFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  495. {
  496. var privateKeyFile = new PrivateKeyFile(_temporaryFile, "12345");
  497. TestRsaKeyFile(privateKeyFile);
  498. fs.Close();
  499. }
  500. }
  501. [TestMethod()]
  502. [Owner("bhalbright")]
  503. [TestCategory("PrivateKey")]
  504. public void Test_PrivateKey_OPENSSH_ED25519()
  505. {
  506. using (var stream = GetData("Key.OPENSSH.ED25519.txt"))
  507. {
  508. _ = new PrivateKeyFile(stream);
  509. }
  510. }
  511. [TestMethod()]
  512. [Owner("scott-xu")]
  513. [TestCategory("PrivateKey")]
  514. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_3DES_CBC()
  515. {
  516. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.3Des.CBC.txt"))
  517. {
  518. _ = new PrivateKeyFile(stream, "12345");
  519. }
  520. }
  521. [TestMethod()]
  522. [Owner("scott-xu")]
  523. [TestCategory("PrivateKey")]
  524. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_128_CBC()
  525. {
  526. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.128.CBC.txt"))
  527. {
  528. _ = new PrivateKeyFile(stream, "12345");
  529. }
  530. }
  531. [TestMethod()]
  532. [Owner("scott-xu")]
  533. [TestCategory("PrivateKey")]
  534. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_192_CBC()
  535. {
  536. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.192.CBC.txt"))
  537. {
  538. _ = new PrivateKeyFile(stream, "12345");
  539. }
  540. }
  541. [TestMethod()]
  542. [Owner("scott-xu")]
  543. [TestCategory("PrivateKey")]
  544. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_256_CBC()
  545. {
  546. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.256.CBC.txt"))
  547. {
  548. _ = new PrivateKeyFile(stream, "12345");
  549. }
  550. }
  551. [TestMethod()]
  552. [Owner("scott-xu")]
  553. [TestCategory("PrivateKey")]
  554. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_128_CTR()
  555. {
  556. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.128.CTR.txt"))
  557. {
  558. _ = new PrivateKeyFile(stream, "12345");
  559. }
  560. }
  561. [TestMethod()]
  562. [Owner("scott-xu")]
  563. [TestCategory("PrivateKey")]
  564. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_192_CTR()
  565. {
  566. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.192.CTR.txt"))
  567. {
  568. _ = new PrivateKeyFile(stream, "12345");
  569. }
  570. }
  571. [TestMethod()]
  572. [Owner("scott-xu")]
  573. [TestCategory("PrivateKey")]
  574. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_256_CTR()
  575. {
  576. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.256.CTR.txt"))
  577. {
  578. _ = new PrivateKeyFile(stream, "12345");
  579. }
  580. }
  581. [TestMethod()]
  582. [Owner("scott-xu")]
  583. [TestCategory("PrivateKey")]
  584. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_128_GCM()
  585. {
  586. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.128.GCM.txt"))
  587. {
  588. _ = new PrivateKeyFile(stream, "12345");
  589. }
  590. }
  591. [TestMethod()]
  592. [Owner("scott-xu")]
  593. [TestCategory("PrivateKey")]
  594. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_AES_256_GCM()
  595. {
  596. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.Aes.256.GCM.txt"))
  597. {
  598. _ = new PrivateKeyFile(stream, "12345");
  599. }
  600. }
  601. [TestMethod()]
  602. [Owner("scott-xu")]
  603. [TestCategory("PrivateKey")]
  604. public void Test_PrivateKey_OPENSSH_ED25519_ENCRYPTED_ChaCha20_Poly1305()
  605. {
  606. using (var stream = GetData("Key.OPENSSH.ED25519.Encrypted.ChaCha20.Poly1305.txt"))
  607. {
  608. _ = new PrivateKeyFile(stream, "12345");
  609. }
  610. }
  611. [TestMethod()]
  612. [Owner("darinkes")]
  613. [TestCategory("PrivateKey")]
  614. public void Test_PrivateKey_OPENSSH_RSA()
  615. {
  616. using (var stream = GetData("Key.OPENSSH.RSA.txt"))
  617. {
  618. TestRsaKeyFile(new PrivateKeyFile(stream));
  619. }
  620. }
  621. [TestMethod()]
  622. [Owner("darinkes")]
  623. [TestCategory("PrivateKey")]
  624. public void Test_PrivateKey_OPENSSH_RSA_ENCRYPTED()
  625. {
  626. using (var stream = GetData("Key.OPENSSH.RSA.Encrypted.txt"))
  627. {
  628. TestRsaKeyFile(new PrivateKeyFile(stream, "12345"));
  629. }
  630. }
  631. [TestMethod()]
  632. [Owner("darinkes")]
  633. [TestCategory("PrivateKey")]
  634. public void Test_PrivateKey_OPENSSH_ECDSA()
  635. {
  636. using (var stream = GetData("Key.OPENSSH.ECDSA.txt"))
  637. {
  638. _ = new PrivateKeyFile(stream);
  639. }
  640. }
  641. [TestMethod()]
  642. [Owner("darinkes")]
  643. [TestCategory("PrivateKey")]
  644. public void Test_PrivateKey_OPENSSH_ECDSA_ENCRYPTED()
  645. {
  646. using (var stream = GetData("Key.OPENSSH.ECDSA.Encrypted.txt"))
  647. {
  648. _ = new PrivateKeyFile(stream, "12345");
  649. }
  650. }
  651. [TestMethod()]
  652. [Owner("darinkes")]
  653. [TestCategory("PrivateKey")]
  654. public void Test_PrivateKey_OPENSSH_ECDSA384()
  655. {
  656. using (var stream = GetData("Key.OPENSSH.ECDSA384.txt"))
  657. {
  658. _ = new PrivateKeyFile(stream);
  659. }
  660. }
  661. [TestMethod()]
  662. [Owner("darinkes")]
  663. [TestCategory("PrivateKey")]
  664. public void Test_PrivateKey_OPENSSH_ECDSA384_ENCRYPTED()
  665. {
  666. using (var stream = GetData("Key.OPENSSH.ECDSA384.Encrypted.txt"))
  667. {
  668. _ = new PrivateKeyFile(stream, "12345");
  669. }
  670. }
  671. [TestMethod()]
  672. [Owner("darinkes")]
  673. [TestCategory("PrivateKey")]
  674. public void Test_PrivateKey_OPENSSH_ECDSA521()
  675. {
  676. using (var stream = GetData("Key.OPENSSH.ECDSA521.txt"))
  677. {
  678. _ = new PrivateKeyFile(stream);
  679. }
  680. }
  681. [TestMethod()]
  682. [Owner("darinkes")]
  683. [TestCategory("PrivateKey")]
  684. public void Test_PrivateKey_OPENSSH_ECDSA521_ENCRYPTED()
  685. {
  686. using (var stream = GetData("Key.OPENSSH.ECDSA521.Encrypted.txt"))
  687. {
  688. _ = new PrivateKeyFile(stream, "12345");
  689. }
  690. }
  691. private void SaveStreamToFile(Stream stream, string fileName)
  692. {
  693. var buffer = new byte[4000];
  694. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  695. {
  696. var bytesRead = stream.Read(buffer, 0, buffer.Length);
  697. while (bytesRead > 0)
  698. {
  699. fs.Write(buffer, 0, bytesRead);
  700. bytesRead = stream.Read(buffer, 0, buffer.Length);
  701. }
  702. }
  703. }
  704. private string GetTempFileName()
  705. {
  706. var tempFile = Path.GetTempFileName();
  707. File.Delete(tempFile);
  708. return tempFile;
  709. }
  710. private static void TestRsaKeyFile(PrivateKeyFile rsaPrivateKeyFile)
  711. {
  712. Assert.IsNotNull(rsaPrivateKeyFile.HostKeyAlgorithms);
  713. Assert.AreEqual(3, rsaPrivateKeyFile.HostKeyAlgorithms.Count);
  714. var algorithms = rsaPrivateKeyFile.HostKeyAlgorithms.ToList();
  715. // ssh-rsa should be attempted first during authentication by default.
  716. // See https://github.com/sshnet/SSH.NET/issues/1233#issuecomment-1871196405
  717. Assert.AreEqual("ssh-rsa", algorithms[0].Name);
  718. Assert.AreEqual("rsa-sha2-512", algorithms[1].Name);
  719. Assert.AreEqual("rsa-sha2-256", algorithms[2].Name);
  720. }
  721. }
  722. }