test_sshkey.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* $OpenBSD: test_sshkey.c,v 1.21 2020/08/27 03:55:22 djm Exp $ */
  2. /*
  3. * Regress test for sshkey.h key management API
  4. *
  5. * Placed in the public domain
  6. */
  7. #include "includes.h"
  8. #include <sys/types.h>
  9. #include <sys/param.h>
  10. #include <stdio.h>
  11. #ifdef HAVE_STDINT_H
  12. #include <stdint.h>
  13. #endif
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/dsa.h>
  19. #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
  20. # include <openssl/ec.h>
  21. #endif
  22. #include "../test_helper/test_helper.h"
  23. #include "ssherr.h"
  24. #include "sshbuf.h"
  25. #define SSHBUF_INTERNAL 1 /* access internals for testing */
  26. #include "sshkey.h"
  27. #include "authfile.h"
  28. #include "common.h"
  29. #include "ssh2.h"
  30. void sshkey_tests(void);
  31. static void
  32. put_opt(struct sshbuf *b, const char *name, const char *value)
  33. {
  34. struct sshbuf *sect;
  35. sect = sshbuf_new();
  36. ASSERT_PTR_NE(sect, NULL);
  37. ASSERT_INT_EQ(sshbuf_put_cstring(b, name), 0);
  38. if (value != NULL)
  39. ASSERT_INT_EQ(sshbuf_put_cstring(sect, value), 0);
  40. ASSERT_INT_EQ(sshbuf_put_stringb(b, sect), 0);
  41. sshbuf_free(sect);
  42. }
  43. #ifdef WITH_OPENSSL
  44. static void
  45. build_cert(struct sshbuf *b, struct sshkey *k, const char *type,
  46. struct sshkey *sign_key, struct sshkey *ca_key,
  47. const char *sig_alg)
  48. {
  49. struct sshbuf *ca_buf, *pk, *principals, *critopts, *exts;
  50. u_char *sigblob;
  51. size_t siglen;
  52. ca_buf = sshbuf_new();
  53. ASSERT_PTR_NE(ca_buf, NULL);
  54. ASSERT_INT_EQ(sshkey_putb(ca_key, ca_buf), 0);
  55. /*
  56. * Get the public key serialisation by rendering the key and skipping
  57. * the type string. This is a bit of a hack :/
  58. */
  59. pk = sshbuf_new();
  60. ASSERT_PTR_NE(pk, NULL);
  61. ASSERT_INT_EQ(sshkey_putb_plain(k, pk), 0);
  62. ASSERT_INT_EQ(sshbuf_skip_string(pk), 0);
  63. principals = sshbuf_new();
  64. ASSERT_PTR_NE(principals, NULL);
  65. ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gsamsa"), 0);
  66. ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gregor"), 0);
  67. critopts = sshbuf_new();
  68. ASSERT_PTR_NE(critopts, NULL);
  69. put_opt(critopts, "force-command", "/usr/local/bin/nethack");
  70. put_opt(critopts, "source-address", "192.168.0.0/24,127.0.0.1,::1");
  71. exts = sshbuf_new();
  72. ASSERT_PTR_NE(exts, NULL);
  73. put_opt(critopts, "permit-X11-forwarding", NULL);
  74. ASSERT_INT_EQ(sshbuf_put_cstring(b, type), 0);
  75. ASSERT_INT_EQ(sshbuf_put_cstring(b, "noncenoncenonce!"), 0); /* nonce */
  76. ASSERT_INT_EQ(sshbuf_putb(b, pk), 0); /* public key serialisation */
  77. ASSERT_INT_EQ(sshbuf_put_u64(b, 1234), 0); /* serial */
  78. ASSERT_INT_EQ(sshbuf_put_u32(b, SSH2_CERT_TYPE_USER), 0); /* type */
  79. ASSERT_INT_EQ(sshbuf_put_cstring(b, "gregor"), 0); /* key ID */
  80. ASSERT_INT_EQ(sshbuf_put_stringb(b, principals), 0); /* principals */
  81. ASSERT_INT_EQ(sshbuf_put_u64(b, 0), 0); /* start */
  82. ASSERT_INT_EQ(sshbuf_put_u64(b, 0xffffffffffffffffULL), 0); /* end */
  83. ASSERT_INT_EQ(sshbuf_put_stringb(b, critopts), 0); /* options */
  84. ASSERT_INT_EQ(sshbuf_put_stringb(b, exts), 0); /* extensions */
  85. ASSERT_INT_EQ(sshbuf_put_string(b, NULL, 0), 0); /* reserved */
  86. ASSERT_INT_EQ(sshbuf_put_stringb(b, ca_buf), 0); /* signature key */
  87. ASSERT_INT_EQ(sshkey_sign(sign_key, &sigblob, &siglen,
  88. sshbuf_ptr(b), sshbuf_len(b), sig_alg, NULL, NULL, 0), 0);
  89. ASSERT_INT_EQ(sshbuf_put_string(b, sigblob, siglen), 0); /* signature */
  90. free(sigblob);
  91. sshbuf_free(ca_buf);
  92. sshbuf_free(exts);
  93. sshbuf_free(critopts);
  94. sshbuf_free(principals);
  95. sshbuf_free(pk);
  96. }
  97. #endif /* WITH_OPENSSL */
  98. static void
  99. signature_test(struct sshkey *k, struct sshkey *bad, const char *sig_alg,
  100. const u_char *d, size_t l)
  101. {
  102. size_t len;
  103. u_char *sig;
  104. ASSERT_INT_EQ(sshkey_sign(k, &sig, &len, d, l, sig_alg,
  105. NULL, NULL, 0), 0);
  106. ASSERT_SIZE_T_GT(len, 8);
  107. ASSERT_PTR_NE(sig, NULL);
  108. ASSERT_INT_EQ(sshkey_verify(k, sig, len, d, l, NULL, 0, NULL), 0);
  109. ASSERT_INT_NE(sshkey_verify(bad, sig, len, d, l, NULL, 0, NULL), 0);
  110. /* Fuzz test is more comprehensive, this is just a smoke test */
  111. sig[len - 5] ^= 0x10;
  112. ASSERT_INT_NE(sshkey_verify(k, sig, len, d, l, NULL, 0, NULL), 0);
  113. free(sig);
  114. }
  115. static void
  116. banana(u_char *s, size_t l)
  117. {
  118. size_t o;
  119. const u_char the_banana[] = { 'b', 'a', 'n', 'a', 'n', 'a' };
  120. for (o = 0; o < l; o += sizeof(the_banana)) {
  121. if (l - o < sizeof(the_banana)) {
  122. memcpy(s + o, "nanananana", l - o);
  123. break;
  124. }
  125. memcpy(s + o, banana, sizeof(the_banana));
  126. }
  127. }
  128. static void
  129. signature_tests(struct sshkey *k, struct sshkey *bad, const char *sig_alg)
  130. {
  131. u_char i, buf[2049];
  132. size_t lens[] = {
  133. 1, 2, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129,
  134. 255, 256, 257, 1023, 1024, 1025, 2047, 2048, 2049
  135. };
  136. for (i = 0; i < (sizeof(lens)/sizeof(lens[0])); i++) {
  137. test_subtest_info("%s key, banana length %zu",
  138. sshkey_type(k), lens[i]);
  139. banana(buf, lens[i]);
  140. signature_test(k, bad, sig_alg, buf, lens[i]);
  141. }
  142. }
  143. static struct sshkey *
  144. get_private(const char *n)
  145. {
  146. struct sshbuf *b;
  147. struct sshkey *ret;
  148. b = load_file(n);
  149. ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", &ret, NULL), 0);
  150. sshbuf_free(b);
  151. return ret;
  152. }
  153. void
  154. sshkey_tests(void)
  155. {
  156. struct sshkey *k1, *k2, *k3, *kf;
  157. #ifdef WITH_OPENSSL
  158. struct sshkey *k4, *kr, *kd;
  159. #ifdef OPENSSL_HAS_ECC
  160. struct sshkey *ke;
  161. #endif /* OPENSSL_HAS_ECC */
  162. #endif /* WITH_OPENSSL */
  163. struct sshbuf *b;
  164. TEST_START("new invalid");
  165. k1 = sshkey_new(-42);
  166. ASSERT_PTR_EQ(k1, NULL);
  167. TEST_DONE();
  168. TEST_START("new/free KEY_UNSPEC");
  169. k1 = sshkey_new(KEY_UNSPEC);
  170. ASSERT_PTR_NE(k1, NULL);
  171. sshkey_free(k1);
  172. TEST_DONE();
  173. #ifdef WITH_OPENSSL
  174. TEST_START("new/free KEY_RSA");
  175. k1 = sshkey_new(KEY_RSA);
  176. ASSERT_PTR_NE(k1, NULL);
  177. ASSERT_PTR_NE(k1->rsa, NULL);
  178. sshkey_free(k1);
  179. TEST_DONE();
  180. TEST_START("new/free KEY_DSA");
  181. k1 = sshkey_new(KEY_DSA);
  182. ASSERT_PTR_NE(k1, NULL);
  183. ASSERT_PTR_NE(k1->dsa, NULL);
  184. sshkey_free(k1);
  185. TEST_DONE();
  186. #ifdef OPENSSL_HAS_ECC
  187. TEST_START("new/free KEY_ECDSA");
  188. k1 = sshkey_new(KEY_ECDSA);
  189. ASSERT_PTR_NE(k1, NULL);
  190. ASSERT_PTR_EQ(k1->ecdsa, NULL); /* Can't allocate without NID */
  191. sshkey_free(k1);
  192. TEST_DONE();
  193. #endif
  194. TEST_START("new/free KEY_ED25519");
  195. k1 = sshkey_new(KEY_ED25519);
  196. ASSERT_PTR_NE(k1, NULL);
  197. /* These should be blank until key loaded or generated */
  198. ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
  199. ASSERT_PTR_EQ(k1->ed25519_pk, NULL);
  200. sshkey_free(k1);
  201. TEST_DONE();
  202. TEST_START("generate KEY_RSA too small modulus");
  203. ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 128, &k1),
  204. SSH_ERR_KEY_LENGTH);
  205. ASSERT_PTR_EQ(k1, NULL);
  206. TEST_DONE();
  207. TEST_START("generate KEY_RSA too large modulus");
  208. ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1 << 20, &k1),
  209. SSH_ERR_KEY_LENGTH);
  210. ASSERT_PTR_EQ(k1, NULL);
  211. TEST_DONE();
  212. TEST_START("generate KEY_DSA wrong bits");
  213. ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 2048, &k1),
  214. SSH_ERR_KEY_LENGTH);
  215. ASSERT_PTR_EQ(k1, NULL);
  216. sshkey_free(k1);
  217. TEST_DONE();
  218. #ifdef OPENSSL_HAS_ECC
  219. TEST_START("generate KEY_ECDSA wrong bits");
  220. ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 42, &k1),
  221. SSH_ERR_KEY_LENGTH);
  222. ASSERT_PTR_EQ(k1, NULL);
  223. sshkey_free(k1);
  224. TEST_DONE();
  225. #endif
  226. TEST_START("generate KEY_RSA");
  227. ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 767, &kr),
  228. SSH_ERR_KEY_LENGTH);
  229. ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
  230. ASSERT_PTR_NE(kr, NULL);
  231. ASSERT_PTR_NE(kr->rsa, NULL);
  232. ASSERT_PTR_NE(rsa_n(kr), NULL);
  233. ASSERT_PTR_NE(rsa_e(kr), NULL);
  234. ASSERT_PTR_NE(rsa_p(kr), NULL);
  235. ASSERT_INT_EQ(BN_num_bits(rsa_n(kr)), 1024);
  236. TEST_DONE();
  237. TEST_START("generate KEY_DSA");
  238. ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
  239. ASSERT_PTR_NE(kd, NULL);
  240. ASSERT_PTR_NE(kd->dsa, NULL);
  241. ASSERT_PTR_NE(dsa_g(kd), NULL);
  242. ASSERT_PTR_NE(dsa_priv_key(kd), NULL);
  243. TEST_DONE();
  244. #ifdef OPENSSL_HAS_ECC
  245. TEST_START("generate KEY_ECDSA");
  246. ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &ke), 0);
  247. ASSERT_PTR_NE(ke, NULL);
  248. ASSERT_PTR_NE(ke->ecdsa, NULL);
  249. ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
  250. ASSERT_PTR_NE(EC_KEY_get0_private_key(ke->ecdsa), NULL);
  251. TEST_DONE();
  252. #endif /* OPENSSL_HAS_ECC */
  253. #endif /* WITH_OPENSSL */
  254. TEST_START("generate KEY_ED25519");
  255. ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &kf), 0);
  256. ASSERT_PTR_NE(kf, NULL);
  257. ASSERT_INT_EQ(kf->type, KEY_ED25519);
  258. ASSERT_PTR_NE(kf->ed25519_pk, NULL);
  259. ASSERT_PTR_NE(kf->ed25519_sk, NULL);
  260. TEST_DONE();
  261. #ifdef WITH_OPENSSL
  262. TEST_START("demote KEY_RSA");
  263. ASSERT_INT_EQ(sshkey_from_private(kr, &k1), 0);
  264. ASSERT_PTR_NE(k1, NULL);
  265. ASSERT_PTR_NE(kr, k1);
  266. ASSERT_INT_EQ(k1->type, KEY_RSA);
  267. ASSERT_PTR_NE(k1->rsa, NULL);
  268. ASSERT_PTR_NE(rsa_n(k1), NULL);
  269. ASSERT_PTR_NE(rsa_e(k1), NULL);
  270. ASSERT_PTR_EQ(rsa_p(k1), NULL);
  271. TEST_DONE();
  272. TEST_START("equal KEY_RSA/demoted KEY_RSA");
  273. ASSERT_INT_EQ(sshkey_equal(kr, k1), 1);
  274. sshkey_free(k1);
  275. TEST_DONE();
  276. TEST_START("demote KEY_DSA");
  277. ASSERT_INT_EQ(sshkey_from_private(kd, &k1), 0);
  278. ASSERT_PTR_NE(k1, NULL);
  279. ASSERT_PTR_NE(kd, k1);
  280. ASSERT_INT_EQ(k1->type, KEY_DSA);
  281. ASSERT_PTR_NE(k1->dsa, NULL);
  282. ASSERT_PTR_NE(dsa_g(k1), NULL);
  283. ASSERT_PTR_EQ(dsa_priv_key(k1), NULL);
  284. TEST_DONE();
  285. TEST_START("equal KEY_DSA/demoted KEY_DSA");
  286. ASSERT_INT_EQ(sshkey_equal(kd, k1), 1);
  287. sshkey_free(k1);
  288. TEST_DONE();
  289. #ifdef OPENSSL_HAS_ECC
  290. TEST_START("demote KEY_ECDSA");
  291. ASSERT_INT_EQ(sshkey_from_private(ke, &k1), 0);
  292. ASSERT_PTR_NE(k1, NULL);
  293. ASSERT_PTR_NE(ke, k1);
  294. ASSERT_INT_EQ(k1->type, KEY_ECDSA);
  295. ASSERT_PTR_NE(k1->ecdsa, NULL);
  296. ASSERT_INT_EQ(k1->ecdsa_nid, ke->ecdsa_nid);
  297. ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
  298. ASSERT_PTR_EQ(EC_KEY_get0_private_key(k1->ecdsa), NULL);
  299. TEST_DONE();
  300. TEST_START("equal KEY_ECDSA/demoted KEY_ECDSA");
  301. ASSERT_INT_EQ(sshkey_equal(ke, k1), 1);
  302. sshkey_free(k1);
  303. TEST_DONE();
  304. #endif /* OPENSSL_HAS_ECC */
  305. #endif /* WITH_OPENSSL */
  306. TEST_START("demote KEY_ED25519");
  307. ASSERT_INT_EQ(sshkey_from_private(kf, &k1), 0);
  308. ASSERT_PTR_NE(k1, NULL);
  309. ASSERT_PTR_NE(kf, k1);
  310. ASSERT_INT_EQ(k1->type, KEY_ED25519);
  311. ASSERT_PTR_NE(k1->ed25519_pk, NULL);
  312. ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
  313. TEST_DONE();
  314. TEST_START("equal KEY_ED25519/demoted KEY_ED25519");
  315. ASSERT_INT_EQ(sshkey_equal(kf, k1), 1);
  316. sshkey_free(k1);
  317. TEST_DONE();
  318. #ifdef WITH_OPENSSL
  319. TEST_START("equal mismatched key types");
  320. ASSERT_INT_EQ(sshkey_equal(kd, kr), 0);
  321. #ifdef OPENSSL_HAS_ECC
  322. ASSERT_INT_EQ(sshkey_equal(kd, ke), 0);
  323. ASSERT_INT_EQ(sshkey_equal(kr, ke), 0);
  324. ASSERT_INT_EQ(sshkey_equal(ke, kf), 0);
  325. #endif /* OPENSSL_HAS_ECC */
  326. ASSERT_INT_EQ(sshkey_equal(kd, kf), 0);
  327. TEST_DONE();
  328. #endif /* WITH_OPENSSL */
  329. TEST_START("equal different keys");
  330. #ifdef WITH_OPENSSL
  331. ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &k1), 0);
  332. ASSERT_INT_EQ(sshkey_equal(kr, k1), 0);
  333. sshkey_free(k1);
  334. ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &k1), 0);
  335. ASSERT_INT_EQ(sshkey_equal(kd, k1), 0);
  336. sshkey_free(k1);
  337. #ifdef OPENSSL_HAS_ECC
  338. ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &k1), 0);
  339. ASSERT_INT_EQ(sshkey_equal(ke, k1), 0);
  340. sshkey_free(k1);
  341. #endif /* OPENSSL_HAS_ECC */
  342. #endif /* WITH_OPENSSL */
  343. ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &k1), 0);
  344. ASSERT_INT_EQ(sshkey_equal(kf, k1), 0);
  345. sshkey_free(k1);
  346. TEST_DONE();
  347. #ifdef WITH_OPENSSL
  348. sshkey_free(kr);
  349. sshkey_free(kd);
  350. #ifdef OPENSSL_HAS_ECC
  351. sshkey_free(ke);
  352. #endif /* OPENSSL_HAS_ECC */
  353. #endif /* WITH_OPENSSL */
  354. sshkey_free(kf);
  355. TEST_START("certify key");
  356. ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_1.pub"),
  357. &k1, NULL), 0);
  358. k2 = get_private("ed25519_2");
  359. ASSERT_INT_EQ(sshkey_to_certified(k1), 0);
  360. ASSERT_PTR_NE(k1->cert, NULL);
  361. k1->cert->type = SSH2_CERT_TYPE_USER;
  362. k1->cert->serial = 1234;
  363. k1->cert->key_id = strdup("estragon");
  364. ASSERT_PTR_NE(k1->cert->key_id, NULL);
  365. k1->cert->principals = calloc(4, sizeof(*k1->cert->principals));
  366. ASSERT_PTR_NE(k1->cert->principals, NULL);
  367. k1->cert->principals[0] = strdup("estragon");
  368. k1->cert->principals[1] = strdup("vladimir");
  369. k1->cert->principals[2] = strdup("pozzo");
  370. k1->cert->principals[3] = strdup("lucky");
  371. ASSERT_PTR_NE(k1->cert->principals[0], NULL);
  372. ASSERT_PTR_NE(k1->cert->principals[1], NULL);
  373. ASSERT_PTR_NE(k1->cert->principals[2], NULL);
  374. ASSERT_PTR_NE(k1->cert->principals[3], NULL);
  375. k1->cert->nprincipals = 4;
  376. k1->cert->valid_after = 0;
  377. k1->cert->valid_before = (u_int64_t)-1;
  378. sshbuf_free(k1->cert->critical);
  379. k1->cert->critical = sshbuf_new();
  380. ASSERT_PTR_NE(k1->cert->critical, NULL);
  381. sshbuf_free(k1->cert->extensions);
  382. k1->cert->extensions = sshbuf_new();
  383. ASSERT_PTR_NE(k1->cert->extensions, NULL);
  384. put_opt(k1->cert->critical, "force-command", "/usr/bin/true");
  385. put_opt(k1->cert->critical, "source-address", "127.0.0.1");
  386. put_opt(k1->cert->extensions, "permit-X11-forwarding", NULL);
  387. put_opt(k1->cert->extensions, "permit-agent-forwarding", NULL);
  388. ASSERT_INT_EQ(sshkey_from_private(k2, &k1->cert->signature_key), 0);
  389. ASSERT_INT_EQ(sshkey_certify(k1, k2, NULL, NULL, NULL), 0);
  390. b = sshbuf_new();
  391. ASSERT_PTR_NE(b, NULL);
  392. ASSERT_INT_EQ(sshkey_putb(k1, b), 0);
  393. ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k3), 0);
  394. sshkey_free(k1);
  395. sshkey_free(k2);
  396. sshkey_free(k3);
  397. sshbuf_reset(b);
  398. TEST_DONE();
  399. #ifdef WITH_OPENSSL
  400. TEST_START("sign and verify RSA");
  401. k1 = get_private("rsa_1");
  402. ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
  403. NULL), 0);
  404. signature_tests(k1, k2, "ssh-rsa");
  405. sshkey_free(k1);
  406. sshkey_free(k2);
  407. TEST_DONE();
  408. TEST_START("sign and verify RSA-SHA256");
  409. k1 = get_private("rsa_1");
  410. ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
  411. NULL), 0);
  412. signature_tests(k1, k2, "rsa-sha2-256");
  413. sshkey_free(k1);
  414. sshkey_free(k2);
  415. TEST_DONE();
  416. TEST_START("sign and verify RSA-SHA512");
  417. k1 = get_private("rsa_1");
  418. ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
  419. NULL), 0);
  420. signature_tests(k1, k2, "rsa-sha2-512");
  421. sshkey_free(k1);
  422. sshkey_free(k2);
  423. TEST_DONE();
  424. TEST_START("sign and verify DSA");
  425. k1 = get_private("dsa_1");
  426. ASSERT_INT_EQ(sshkey_load_public(test_data_file("dsa_2.pub"), &k2,
  427. NULL), 0);
  428. signature_tests(k1, k2, NULL);
  429. sshkey_free(k1);
  430. sshkey_free(k2);
  431. TEST_DONE();
  432. #ifdef OPENSSL_HAS_ECC
  433. TEST_START("sign and verify ECDSA");
  434. k1 = get_private("ecdsa_1");
  435. ASSERT_INT_EQ(sshkey_load_public(test_data_file("ecdsa_2.pub"), &k2,
  436. NULL), 0);
  437. signature_tests(k1, k2, NULL);
  438. sshkey_free(k1);
  439. sshkey_free(k2);
  440. TEST_DONE();
  441. #endif /* OPENSSL_HAS_ECC */
  442. #endif /* WITH_OPENSSL */
  443. TEST_START("sign and verify ED25519");
  444. k1 = get_private("ed25519_1");
  445. ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_2.pub"), &k2,
  446. NULL), 0);
  447. signature_tests(k1, k2, NULL);
  448. sshkey_free(k1);
  449. sshkey_free(k2);
  450. TEST_DONE();
  451. #ifdef WITH_OPENSSL
  452. TEST_START("nested certificate");
  453. ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k1), 0);
  454. ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2,
  455. NULL), 0);
  456. k3 = get_private("rsa_1");
  457. build_cert(b, k2, "ssh-rsa-cert-v01@openssh.com", k3, k1, NULL);
  458. ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k4),
  459. SSH_ERR_KEY_CERT_INVALID_SIGN_KEY);
  460. ASSERT_PTR_EQ(k4, NULL);
  461. sshkey_free(k1);
  462. sshkey_free(k2);
  463. sshkey_free(k3);
  464. sshbuf_free(b);
  465. TEST_DONE();
  466. #endif /* WITH_OPENSSL */
  467. }