1
0

rpktest.c 28 KB

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