rpktest.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/ssl.h>
  10. #include "helpers/ssltestlib.h"
  11. #include "internal/dane.h"
  12. #include "testutil.h"
  13. #undef OSSL_NO_USABLE_TLS1_3
  14. #if defined(OPENSSL_NO_TLS1_3) \
  15. || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
  16. /*
  17. * If we don't have ec or dh then there are no built-in groups that are usable
  18. * with TLSv1.3
  19. */
  20. # define OSSL_NO_USABLE_TLS1_3
  21. #endif
  22. static char *certsdir = NULL;
  23. static char *rootcert = NULL;
  24. static char *cert = NULL;
  25. static char *privkey = NULL;
  26. static char *cert2 = NULL;
  27. static char *privkey2 = NULL;
  28. static char *cert448 = NULL;
  29. static char *privkey448 = NULL;
  30. static char *cert25519 = NULL;
  31. static char *privkey25519 = NULL;
  32. static OSSL_LIB_CTX *libctx = NULL;
  33. static OSSL_PROVIDER *defctxnull = NULL;
  34. static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 };
  35. static const unsigned char SID_CTX[] = { 'r', 'p', 'k' };
  36. static int rpk_verify_client_cb(int ok, X509_STORE_CTX *ctx)
  37. {
  38. int err = X509_STORE_CTX_get_error(ctx);
  39. if (X509_STORE_CTX_get0_rpk(ctx) != NULL) {
  40. if (err != X509_V_OK) {
  41. TEST_info("rpk_verify_client_cb: ok=%d err=%d", ok, err);
  42. return 0;
  43. }
  44. }
  45. return 1;
  46. }
  47. static int rpk_verify_server_cb(int ok, X509_STORE_CTX *ctx)
  48. {
  49. int err = X509_STORE_CTX_get_error(ctx);
  50. if (X509_STORE_CTX_get0_rpk(ctx) != NULL) {
  51. if (err != X509_V_OK) {
  52. TEST_info("rpk_verify_server_cb: ok=%d err=%d", ok, err);
  53. return 0;
  54. }
  55. }
  56. return 1;
  57. }
  58. /*
  59. * Test dimensions:
  60. * (2) server_cert_type RPK off/on for server
  61. * (2) client_cert_type RPK off/on for server
  62. * (2) server_cert_type RPK off/on for client
  63. * (2) client_cert_type RPK off/on for client
  64. * (4) RSA vs ECDSA vs Ed25519 vs Ed448 certificates
  65. * (2) TLSv1.2 vs TLSv1.3
  66. *
  67. * Tests:
  68. * idx = 0 - is the normal success case, certificate, single peer key
  69. * idx = 1 - only a private key
  70. * idx = 2 - add client authentication
  71. * idx = 3 - add second peer key (rootcert.pem)
  72. * idx = 4 - add second peer key (different, RSA or ECDSA)
  73. * idx = 5 - reverse peer keys (rootcert.pem, different order)
  74. * idx = 6 - reverse peer keys (RSA or ECDSA, different order)
  75. * idx = 7 - expects failure due to mismatched key (RSA or ECDSA)
  76. * idx = 8 - expects failure due to no configured key on client
  77. * idx = 9 - add client authentication (PHA)
  78. * idx = 10 - add client authentication (privake key only)
  79. * idx = 11 - simple resumption
  80. * idx = 12 - simple resumption, no ticket
  81. * idx = 13 - resumption with client authentication
  82. * idx = 14 - resumption with client authentication, no ticket
  83. * idx = 15 - like 0, but use non-default libctx
  84. *
  85. * 16 * 2 * 4 * 2 * 2 * 2 * 2 = 2048 tests
  86. */
  87. static int test_rpk(int idx)
  88. {
  89. # define RPK_TESTS 16
  90. # define RPK_DIMS (2 * 4 * 2 * 2 * 2 * 2)
  91. SSL_CTX *cctx = NULL, *sctx = NULL;
  92. SSL *clientssl = NULL, *serverssl = NULL;
  93. EVP_PKEY *pkey = NULL, *other_pkey = NULL, *root_pkey = NULL;
  94. X509 *x509 = NULL, *other_x509 = NULL, *root_x509 = NULL;
  95. int testresult = 0, ret, expected = 1;
  96. int client_expected = X509_V_OK;
  97. int verify;
  98. int tls_version;
  99. char *cert_file = NULL;
  100. char *privkey_file = NULL;
  101. char *other_cert_file = NULL;
  102. SSL_SESSION *client_sess = NULL;
  103. SSL_SESSION *server_sess = NULL;
  104. int idx_server_server_rpk, idx_server_client_rpk;
  105. int idx_client_server_rpk, idx_client_client_rpk;
  106. int idx_cert, idx_prot;
  107. int client_auth = 0;
  108. int resumption = 0;
  109. long server_verify_result = 0;
  110. long client_verify_result = 0;
  111. OSSL_LIB_CTX *test_libctx = NULL;
  112. if (!TEST_int_le(idx, RPK_TESTS * RPK_DIMS))
  113. return 0;
  114. idx_server_server_rpk = idx / (RPK_TESTS * 2 * 4 * 2 * 2 * 2);
  115. idx %= RPK_TESTS * 2 * 4 * 2 * 2 * 2;
  116. idx_server_client_rpk = idx / (RPK_TESTS * 2 * 4 * 2 * 2);
  117. idx %= RPK_TESTS * 2 * 4 * 2 * 2;
  118. idx_client_server_rpk = idx / (RPK_TESTS * 2 * 4 * 2);
  119. idx %= RPK_TESTS * 2 * 4 * 2;
  120. idx_client_client_rpk = idx / (RPK_TESTS * 2 * 4);
  121. idx %= RPK_TESTS * 2 * 4;
  122. idx_cert = idx / (RPK_TESTS * 2);
  123. idx %= RPK_TESTS * 2;
  124. idx_prot = idx / RPK_TESTS;
  125. idx %= RPK_TESTS;
  126. /* Load "root" cert/pubkey */
  127. root_x509 = load_cert_pem(rootcert, NULL);
  128. if (!TEST_ptr(root_x509))
  129. goto end;
  130. root_pkey = X509_get0_pubkey(root_x509);
  131. if (!TEST_ptr(root_pkey))
  132. goto end;
  133. switch (idx_cert) {
  134. case 0:
  135. /* use RSA */
  136. cert_file = cert;
  137. privkey_file = privkey;
  138. other_cert_file = cert2;
  139. break;
  140. #ifndef OPENSSL_NO_ECDSA
  141. case 1:
  142. /* use ECDSA */
  143. cert_file = cert2;
  144. privkey_file = privkey2;
  145. other_cert_file = cert;
  146. break;
  147. # ifndef OPENSSL_NO_ECX
  148. case 2:
  149. /* use Ed448 */
  150. cert_file = cert448;
  151. privkey_file = privkey448;
  152. other_cert_file = cert;
  153. break;
  154. case 3:
  155. /* use Ed25519 */
  156. cert_file = cert25519;
  157. privkey_file = privkey25519;
  158. other_cert_file = cert;
  159. break;
  160. # endif
  161. #endif
  162. default:
  163. testresult = TEST_skip("EDCSA disabled");
  164. goto end;
  165. }
  166. /* Load primary cert */
  167. x509 = load_cert_pem(cert_file, NULL);
  168. if (!TEST_ptr(x509))
  169. goto end;
  170. pkey = X509_get0_pubkey(x509);
  171. /* load other cert */
  172. other_x509 = load_cert_pem(other_cert_file, NULL);
  173. if (!TEST_ptr(other_x509))
  174. goto end;
  175. other_pkey = X509_get0_pubkey(other_x509);
  176. #ifdef OPENSSL_NO_ECDSA
  177. /* Can't get other_key if it's ECDSA */
  178. if (other_pkey == NULL && idx_cert == 0
  179. && (idx == 4 || idx == 6 || idx == 7)) {
  180. testresult = TEST_skip("EDCSA disabled");
  181. goto end;
  182. }
  183. #endif
  184. switch (idx_prot) {
  185. case 0:
  186. #ifdef OSSL_NO_USABLE_TLS1_3
  187. testresult = TEST_skip("TLSv1.3 disabled");
  188. goto end;
  189. #else
  190. tls_version = TLS1_3_VERSION;
  191. break;
  192. #endif
  193. case 1:
  194. #ifdef OPENSSL_NO_TLS1_2
  195. testresult = TEST_skip("TLSv1.2 disabled");
  196. goto end;
  197. #else
  198. tls_version = TLS1_2_VERSION;
  199. break;
  200. #endif
  201. default:
  202. goto end;
  203. }
  204. if (idx == 15) {
  205. test_libctx = libctx;
  206. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  207. if (!TEST_ptr(defctxnull))
  208. goto end;
  209. }
  210. if (!TEST_true(create_ssl_ctx_pair(test_libctx,
  211. TLS_server_method(), TLS_client_method(),
  212. tls_version, tls_version,
  213. &sctx, &cctx, NULL, NULL)))
  214. goto end;
  215. if (idx_server_server_rpk)
  216. if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_rpk, sizeof(cert_type_rpk))))
  217. goto end;
  218. if (idx_server_client_rpk)
  219. if (!TEST_true(SSL_CTX_set1_client_cert_type(sctx, cert_type_rpk, sizeof(cert_type_rpk))))
  220. goto end;
  221. if (idx_client_server_rpk)
  222. if (!TEST_true(SSL_CTX_set1_server_cert_type(cctx, cert_type_rpk, sizeof(cert_type_rpk))))
  223. goto end;
  224. if (idx_client_client_rpk)
  225. if (!TEST_true(SSL_CTX_set1_client_cert_type(cctx, cert_type_rpk, sizeof(cert_type_rpk))))
  226. goto end;
  227. if (!TEST_true(SSL_CTX_set_session_id_context(sctx, SID_CTX, sizeof(SID_CTX))))
  228. goto end;
  229. if (!TEST_true(SSL_CTX_set_session_id_context(cctx, SID_CTX, sizeof(SID_CTX))))
  230. goto end;
  231. if (!TEST_int_gt(SSL_CTX_dane_enable(sctx), 0))
  232. goto end;
  233. if (!TEST_int_gt(SSL_CTX_dane_enable(cctx), 0))
  234. goto end;
  235. /* NEW */
  236. SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, rpk_verify_client_cb);
  237. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  238. NULL, NULL)))
  239. goto end;
  240. if (!TEST_int_gt(SSL_dane_enable(serverssl, NULL), 0))
  241. goto end;
  242. if (!TEST_int_gt(SSL_dane_enable(clientssl, "example.com"), 0))
  243. goto end;
  244. /* Set private key and certificate */
  245. if (!TEST_int_eq(SSL_use_PrivateKey_file(serverssl, privkey_file, SSL_FILETYPE_PEM), 1))
  246. goto end;
  247. /* Only a private key */
  248. if (idx == 1) {
  249. if (idx_server_server_rpk == 0 || idx_client_server_rpk == 0)
  250. expected = 0;
  251. } else {
  252. /* Add certificate */
  253. if (!TEST_int_eq(SSL_use_certificate_file(serverssl, cert_file, SSL_FILETYPE_PEM), 1))
  254. goto end;
  255. if (!TEST_int_eq(SSL_check_private_key(serverssl), 1))
  256. goto end;
  257. }
  258. switch (idx) {
  259. default:
  260. if (!TEST_true(idx < RPK_TESTS))
  261. goto end;
  262. break;
  263. case 0:
  264. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  265. goto end;
  266. break;
  267. case 1:
  268. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  269. goto end;
  270. break;
  271. case 2:
  272. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  273. goto end;
  274. if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey)))
  275. goto end;
  276. /* Use the same key for client auth */
  277. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  278. goto end;
  279. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  280. goto end;
  281. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  282. goto end;
  283. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  284. client_auth = 1;
  285. break;
  286. case 3:
  287. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  288. goto end;
  289. if (!TEST_true(SSL_add_expected_rpk(clientssl, root_pkey)))
  290. goto end;
  291. break;
  292. case 4:
  293. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  294. goto end;
  295. if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey)))
  296. goto end;
  297. break;
  298. case 5:
  299. if (!TEST_true(SSL_add_expected_rpk(clientssl, root_pkey)))
  300. goto end;
  301. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  302. goto end;
  303. break;
  304. case 6:
  305. if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey)))
  306. goto end;
  307. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  308. goto end;
  309. break;
  310. case 7:
  311. if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1)
  312. client_expected = -1;
  313. if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey)))
  314. goto end;
  315. client_verify_result = X509_V_ERR_DANE_NO_MATCH;
  316. break;
  317. case 8:
  318. if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1)
  319. client_expected = -1;
  320. /* no peer keys */
  321. client_verify_result = X509_V_ERR_RPK_UNTRUSTED;
  322. break;
  323. case 9:
  324. if (tls_version != TLS1_3_VERSION) {
  325. testresult = TEST_skip("PHA requires TLSv1.3");
  326. goto end;
  327. }
  328. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  329. goto end;
  330. if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey)))
  331. goto end;
  332. /* Use the same key for client auth */
  333. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  334. goto end;
  335. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  336. goto end;
  337. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  338. goto end;
  339. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_POST_HANDSHAKE, rpk_verify_server_cb);
  340. SSL_set_post_handshake_auth(clientssl, 1);
  341. client_auth = 1;
  342. break;
  343. case 10:
  344. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  345. goto end;
  346. if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey)))
  347. goto end;
  348. /* Use the same key for client auth */
  349. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  350. goto end;
  351. /* Since there's no cert, this is expected to fail without RPK support */
  352. if (!idx_server_client_rpk || !idx_client_client_rpk)
  353. expected = 0;
  354. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  355. client_auth = 1;
  356. break;
  357. case 11:
  358. if (!idx_server_server_rpk || !idx_client_server_rpk) {
  359. testresult = TEST_skip("Only testing resumption with server RPK");
  360. goto end;
  361. }
  362. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  363. goto end;
  364. resumption = 1;
  365. break;
  366. case 12:
  367. if (!idx_server_server_rpk || !idx_client_server_rpk) {
  368. testresult = TEST_skip("Only testing resumption with server RPK");
  369. goto end;
  370. }
  371. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  372. goto end;
  373. SSL_set_options(serverssl, SSL_OP_NO_TICKET);
  374. SSL_set_options(clientssl, SSL_OP_NO_TICKET);
  375. resumption = 1;
  376. break;
  377. case 13:
  378. if (!idx_server_server_rpk || !idx_client_server_rpk) {
  379. testresult = TEST_skip("Only testing resumption with server RPK");
  380. goto end;
  381. }
  382. if (!idx_server_client_rpk || !idx_client_client_rpk) {
  383. testresult = TEST_skip("Only testing client authentication resumption with client RPK");
  384. goto end;
  385. }
  386. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  387. goto end;
  388. if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey)))
  389. goto end;
  390. /* Use the same key for client auth */
  391. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  392. goto end;
  393. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  394. goto end;
  395. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  396. goto end;
  397. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  398. client_auth = 1;
  399. resumption = 1;
  400. break;
  401. case 14:
  402. if (!idx_server_server_rpk || !idx_client_server_rpk) {
  403. testresult = TEST_skip("Only testing resumption with server RPK");
  404. goto end;
  405. }
  406. if (!idx_server_client_rpk || !idx_client_client_rpk) {
  407. testresult = TEST_skip("Only testing client authentication resumption with client RPK");
  408. goto end;
  409. }
  410. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  411. goto end;
  412. if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey)))
  413. goto end;
  414. /* Use the same key for client auth */
  415. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  416. goto end;
  417. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  418. goto end;
  419. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  420. goto end;
  421. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  422. SSL_set_options(serverssl, SSL_OP_NO_TICKET);
  423. SSL_set_options(clientssl, SSL_OP_NO_TICKET);
  424. client_auth = 1;
  425. resumption = 1;
  426. break;
  427. case 15:
  428. if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey)))
  429. goto end;
  430. break;
  431. }
  432. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  433. if (!TEST_int_eq(expected, ret))
  434. goto end;
  435. /* Make sure client gets RPK or certificate as configured */
  436. if (expected == 1) {
  437. if (idx_server_server_rpk && idx_client_server_rpk) {
  438. if (!TEST_long_eq(SSL_get_verify_result(clientssl), client_verify_result))
  439. goto end;
  440. if (!TEST_ptr(SSL_get0_peer_rpk(clientssl)))
  441. goto end;
  442. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_rpk))
  443. goto end;
  444. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_rpk))
  445. goto end;
  446. } else {
  447. if (!TEST_ptr(SSL_get0_peer_certificate(clientssl)))
  448. goto end;
  449. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_x509))
  450. goto end;
  451. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_x509))
  452. goto end;
  453. }
  454. }
  455. if (idx == 9) {
  456. /* Make PHA happen... */
  457. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  458. goto end;
  459. if (!TEST_true(SSL_do_handshake(serverssl)))
  460. goto end;
  461. if (!TEST_int_le(SSL_read(clientssl, NULL, 0), 0))
  462. goto end;
  463. if (!TEST_int_le(SSL_read(serverssl, NULL, 0), 0))
  464. goto end;
  465. }
  466. /* Make sure server gets an RPK or certificate as configured */
  467. if (client_auth) {
  468. if (idx_server_client_rpk && idx_client_client_rpk) {
  469. if (!TEST_long_eq(SSL_get_verify_result(serverssl), server_verify_result))
  470. goto end;
  471. if (!TEST_ptr(SSL_get0_peer_rpk(serverssl)))
  472. goto end;
  473. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_rpk))
  474. goto end;
  475. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_rpk))
  476. goto end;
  477. } else {
  478. /* only if connection is expected to succeed */
  479. if (expected == 1 && !TEST_ptr(SSL_get0_peer_certificate(serverssl)))
  480. goto end;
  481. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_x509))
  482. goto end;
  483. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_x509))
  484. goto end;
  485. }
  486. }
  487. if (resumption) {
  488. EVP_PKEY *client_pkey = NULL;
  489. EVP_PKEY *server_pkey = NULL;
  490. if (!TEST_ptr((client_sess = SSL_get1_session(clientssl)))
  491. || !TEST_ptr((client_pkey = SSL_SESSION_get0_peer_rpk(client_sess))))
  492. goto end;
  493. if (client_auth) {
  494. if (!TEST_ptr((server_sess = SSL_get1_session(serverssl)))
  495. || !TEST_ptr((server_pkey = SSL_SESSION_get0_peer_rpk(server_sess))))
  496. goto end;
  497. }
  498. SSL_shutdown(clientssl);
  499. SSL_shutdown(serverssl);
  500. SSL_free(clientssl);
  501. SSL_free(serverssl);
  502. serverssl = clientssl = NULL;
  503. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  504. NULL, NULL))
  505. || !TEST_true(SSL_set_session(clientssl, client_sess)))
  506. goto end;
  507. /* Set private key (and maybe certificate) */
  508. if (!TEST_int_eq(SSL_use_PrivateKey_file(serverssl, privkey_file, SSL_FILETYPE_PEM), 1))
  509. goto end;
  510. if (!TEST_int_eq(SSL_use_certificate_file(serverssl, cert_file, SSL_FILETYPE_PEM), 1))
  511. goto end;
  512. if (!TEST_int_eq(SSL_check_private_key(serverssl), 1))
  513. goto end;
  514. if (!TEST_int_gt(SSL_dane_enable(serverssl, "example.com"), 0))
  515. goto end;
  516. if (!TEST_int_gt(SSL_dane_enable(clientssl, "example.com"), 0))
  517. goto end;
  518. switch (idx) {
  519. default:
  520. break;
  521. case 11:
  522. if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey)))
  523. goto end;
  524. break;
  525. case 12:
  526. if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey)))
  527. goto end;
  528. SSL_set_options(clientssl, SSL_OP_NO_TICKET);
  529. SSL_set_options(serverssl, SSL_OP_NO_TICKET);
  530. break;
  531. case 13:
  532. if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey)))
  533. goto end;
  534. if (!TEST_true(SSL_add_expected_rpk(serverssl, server_pkey)))
  535. goto end;
  536. /* Use the same key for client auth */
  537. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  538. goto end;
  539. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  540. goto end;
  541. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  542. goto end;
  543. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  544. break;
  545. case 14:
  546. if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey)))
  547. goto end;
  548. if (!TEST_true(SSL_add_expected_rpk(serverssl, server_pkey)))
  549. goto end;
  550. /* Use the same key for client auth */
  551. if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1))
  552. goto end;
  553. if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1))
  554. goto end;
  555. if (!TEST_int_eq(SSL_check_private_key(clientssl), 1))
  556. goto end;
  557. SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb);
  558. SSL_set_options(serverssl, SSL_OP_NO_TICKET);
  559. SSL_set_options(clientssl, SSL_OP_NO_TICKET);
  560. break;
  561. }
  562. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  563. if (!TEST_int_eq(expected, ret))
  564. goto end;
  565. verify = SSL_get_verify_result(clientssl);
  566. if (!TEST_int_eq(client_expected, verify))
  567. goto end;
  568. if (!TEST_true(SSL_session_reused(clientssl)))
  569. goto end;
  570. if (!TEST_ptr(SSL_get0_peer_rpk(clientssl)))
  571. goto end;
  572. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_rpk))
  573. goto end;
  574. if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_rpk))
  575. goto end;
  576. if (client_auth) {
  577. if (!TEST_ptr(SSL_get0_peer_rpk(serverssl)))
  578. goto end;
  579. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_rpk))
  580. goto end;
  581. if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_rpk))
  582. goto end;
  583. }
  584. }
  585. testresult = 1;
  586. end:
  587. OSSL_PROVIDER_unload(defctxnull);
  588. defctxnull = NULL;
  589. SSL_SESSION_free(client_sess);
  590. SSL_SESSION_free(server_sess);
  591. SSL_free(serverssl);
  592. SSL_free(clientssl);
  593. SSL_CTX_free(sctx);
  594. SSL_CTX_free(cctx);
  595. X509_free(x509);
  596. X509_free(other_x509);
  597. X509_free(root_x509);
  598. if (testresult == 0) {
  599. TEST_info("idx_ss_rpk=%d, idx_sc_rpk=%d, idx_cs_rpk=%d, idx_cc_rpk=%d, idx_cert=%d, idx_prot=%d, idx=%d",
  600. idx_server_server_rpk, idx_server_client_rpk,
  601. idx_client_server_rpk, idx_client_client_rpk,
  602. idx_cert, idx_prot, idx);
  603. }
  604. return testresult;
  605. }
  606. static int test_rpk_api(void)
  607. {
  608. int ret = 0;
  609. SSL_CTX *cctx = NULL, *sctx = NULL;
  610. unsigned char cert_type_dups[] = { TLSEXT_cert_type_rpk,
  611. TLSEXT_cert_type_x509,
  612. TLSEXT_cert_type_x509 };
  613. unsigned char cert_type_bad[] = { 0xFF };
  614. unsigned char cert_type_extra[] = { TLSEXT_cert_type_rpk,
  615. TLSEXT_cert_type_x509,
  616. 0xFF };
  617. unsigned char cert_type_unsup[] = { TLSEXT_cert_type_pgp,
  618. TLSEXT_cert_type_1609dot2 };
  619. unsigned char cert_type_just_x509[] = { TLSEXT_cert_type_x509 };
  620. unsigned char cert_type_just_rpk[] = { TLSEXT_cert_type_rpk };
  621. if (!TEST_true(create_ssl_ctx_pair(NULL,
  622. TLS_server_method(), TLS_client_method(),
  623. TLS1_2_VERSION, TLS1_2_VERSION,
  624. &sctx, &cctx, NULL, NULL)))
  625. goto end;
  626. if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_dups, sizeof(cert_type_dups))))
  627. goto end;
  628. if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_bad, sizeof(cert_type_bad))))
  629. goto end;
  630. if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_extra, sizeof(cert_type_extra))))
  631. goto end;
  632. if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_unsup, sizeof(cert_type_unsup))))
  633. goto end;
  634. if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_just_x509, sizeof(cert_type_just_x509))))
  635. goto end;
  636. if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_just_rpk, sizeof(cert_type_just_rpk))))
  637. goto end;
  638. ret = 1;
  639. end:
  640. SSL_CTX_free(sctx);
  641. SSL_CTX_free(cctx);
  642. return ret;
  643. }
  644. OPT_TEST_DECLARE_USAGE("certdir\n")
  645. int setup_tests(void)
  646. {
  647. if (!test_skip_common_options()) {
  648. TEST_error("Error parsing test options\n");
  649. return 0;
  650. }
  651. if (!TEST_ptr(certsdir = test_get_argument(0)))
  652. return 0;
  653. rootcert = test_mk_file_path(certsdir, "rootcert.pem");
  654. if (rootcert == NULL)
  655. goto err;
  656. cert = test_mk_file_path(certsdir, "servercert.pem");
  657. if (cert == NULL)
  658. goto err;
  659. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  660. if (privkey == NULL)
  661. goto err;
  662. cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  663. if (cert2 == NULL)
  664. goto err;
  665. privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  666. if (privkey2 == NULL)
  667. goto err;
  668. cert448 = test_mk_file_path(certsdir, "server-ed448-cert.pem");
  669. if (cert2 == NULL)
  670. goto err;
  671. privkey448 = test_mk_file_path(certsdir, "server-ed448-key.pem");
  672. if (privkey2 == NULL)
  673. goto err;
  674. cert25519 = test_mk_file_path(certsdir, "server-ed25519-cert.pem");
  675. if (cert2 == NULL)
  676. goto err;
  677. privkey25519 = test_mk_file_path(certsdir, "server-ed25519-key.pem");
  678. if (privkey2 == NULL)
  679. goto err;
  680. libctx = OSSL_LIB_CTX_new();
  681. if (libctx == NULL)
  682. goto err;
  683. ADD_TEST(test_rpk_api);
  684. ADD_ALL_TESTS(test_rpk, RPK_TESTS * RPK_DIMS);
  685. return 1;
  686. err:
  687. return 0;
  688. }
  689. void cleanup_tests(void)
  690. {
  691. OPENSSL_free(rootcert);
  692. OPENSSL_free(cert);
  693. OPENSSL_free(privkey);
  694. OPENSSL_free(cert2);
  695. OPENSSL_free(privkey2);
  696. OPENSSL_free(cert448);
  697. OPENSSL_free(privkey448);
  698. OPENSSL_free(cert25519);
  699. OPENSSL_free(privkey25519);
  700. OSSL_LIB_CTX_free(libctx);
  701. }