ssl_test_ctx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright 2016-2022 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 <string.h>
  10. #include <openssl/e_os2.h>
  11. #include <openssl/crypto.h>
  12. #include "internal/nelem.h"
  13. #include "ssl_test_ctx.h"
  14. #include "../testutil.h"
  15. static const int default_app_data_size = 256;
  16. /* Default set to be as small as possible to exercise fragmentation. */
  17. static const int default_max_fragment_size = 512;
  18. static int parse_boolean(const char *value, int *result)
  19. {
  20. if (OPENSSL_strcasecmp(value, "Yes") == 0) {
  21. *result = 1;
  22. return 1;
  23. }
  24. else if (OPENSSL_strcasecmp(value, "No") == 0) {
  25. *result = 0;
  26. return 1;
  27. }
  28. TEST_error("parse_boolean given: '%s'", value);
  29. return 0;
  30. }
  31. #define IMPLEMENT_SSL_TEST_BOOL_OPTION(struct_type, name, field) \
  32. static int parse_##name##_##field(struct_type *ctx, const char *value) \
  33. { \
  34. return parse_boolean(value, &ctx->field); \
  35. }
  36. #define IMPLEMENT_SSL_TEST_STRING_OPTION(struct_type, name, field) \
  37. static int parse_##name##_##field(struct_type *ctx, const char *value) \
  38. { \
  39. OPENSSL_free(ctx->field); \
  40. ctx->field = OPENSSL_strdup(value); \
  41. return TEST_ptr(ctx->field); \
  42. }
  43. #define IMPLEMENT_SSL_TEST_INT_OPTION(struct_type, name, field) \
  44. static int parse_##name##_##field(struct_type *ctx, const char *value) \
  45. { \
  46. ctx->field = atoi(value); \
  47. return 1; \
  48. }
  49. /* True enums and other test configuration values that map to an int. */
  50. typedef struct {
  51. const char *name;
  52. int value;
  53. } test_enum;
  54. __owur static int parse_enum(const test_enum *enums, size_t num_enums,
  55. int *value, const char *name)
  56. {
  57. size_t i;
  58. for (i = 0; i < num_enums; i++) {
  59. if (strcmp(enums[i].name, name) == 0) {
  60. *value = enums[i].value;
  61. return 1;
  62. }
  63. }
  64. return 0;
  65. }
  66. static const char *enum_name(const test_enum *enums, size_t num_enums,
  67. int value)
  68. {
  69. size_t i;
  70. for (i = 0; i < num_enums; i++) {
  71. if (enums[i].value == value) {
  72. return enums[i].name;
  73. }
  74. }
  75. return "InvalidValue";
  76. }
  77. /* ExpectedResult */
  78. static const test_enum ssl_test_results[] = {
  79. {"Success", SSL_TEST_SUCCESS},
  80. {"ServerFail", SSL_TEST_SERVER_FAIL},
  81. {"ClientFail", SSL_TEST_CLIENT_FAIL},
  82. {"InternalError", SSL_TEST_INTERNAL_ERROR},
  83. {"FirstHandshakeFailed", SSL_TEST_FIRST_HANDSHAKE_FAILED},
  84. };
  85. __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
  86. {
  87. int ret_value;
  88. if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
  89. &ret_value, value)) {
  90. return 0;
  91. }
  92. test_ctx->expected_result = ret_value;
  93. return 1;
  94. }
  95. const char *ssl_test_result_name(ssl_test_result_t result)
  96. {
  97. return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
  98. }
  99. /* ExpectedClientAlert / ExpectedServerAlert */
  100. static const test_enum ssl_alerts[] = {
  101. {"UnknownCA", SSL_AD_UNKNOWN_CA},
  102. {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
  103. {"UnrecognizedName", SSL_AD_UNRECOGNIZED_NAME},
  104. {"NoRenegotiation", SSL_AD_NO_RENEGOTIATION},
  105. {"BadCertificate", SSL_AD_BAD_CERTIFICATE},
  106. {"NoApplicationProtocol", SSL_AD_NO_APPLICATION_PROTOCOL},
  107. {"CertificateRequired", SSL_AD_CERTIFICATE_REQUIRED},
  108. };
  109. __owur static int parse_alert(int *alert, const char *value)
  110. {
  111. return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
  112. }
  113. __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
  114. {
  115. return parse_alert(&test_ctx->expected_client_alert, value);
  116. }
  117. __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
  118. {
  119. return parse_alert(&test_ctx->expected_server_alert, value);
  120. }
  121. const char *ssl_alert_name(int alert)
  122. {
  123. return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
  124. }
  125. /* ExpectedProtocol */
  126. static const test_enum ssl_protocols[] = {
  127. {"TLSv1.3", TLS1_3_VERSION},
  128. {"TLSv1.2", TLS1_2_VERSION},
  129. {"TLSv1.1", TLS1_1_VERSION},
  130. {"TLSv1", TLS1_VERSION},
  131. {"SSLv3", SSL3_VERSION},
  132. {"DTLSv1", DTLS1_VERSION},
  133. {"DTLSv1.2", DTLS1_2_VERSION},
  134. };
  135. __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
  136. {
  137. return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
  138. &test_ctx->expected_protocol, value);
  139. }
  140. const char *ssl_protocol_name(int protocol)
  141. {
  142. return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
  143. }
  144. /* VerifyCallback */
  145. static const test_enum ssl_verify_callbacks[] = {
  146. {"None", SSL_TEST_VERIFY_NONE},
  147. {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL},
  148. {"RetryOnce", SSL_TEST_VERIFY_RETRY_ONCE},
  149. {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL},
  150. };
  151. __owur static int parse_client_verify_callback(SSL_TEST_CLIENT_CONF *client_conf,
  152. const char *value)
  153. {
  154. int ret_value;
  155. if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
  156. &ret_value, value)) {
  157. return 0;
  158. }
  159. client_conf->verify_callback = ret_value;
  160. return 1;
  161. }
  162. const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
  163. {
  164. return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
  165. callback);
  166. }
  167. /* ServerName */
  168. static const test_enum ssl_servername[] = {
  169. {"None", SSL_TEST_SERVERNAME_NONE},
  170. {"server1", SSL_TEST_SERVERNAME_SERVER1},
  171. {"server2", SSL_TEST_SERVERNAME_SERVER2},
  172. {"invalid", SSL_TEST_SERVERNAME_INVALID},
  173. };
  174. __owur static int parse_servername(SSL_TEST_CLIENT_CONF *client_conf,
  175. const char *value)
  176. {
  177. int ret_value;
  178. if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername),
  179. &ret_value, value)) {
  180. return 0;
  181. }
  182. client_conf->servername = ret_value;
  183. return 1;
  184. }
  185. __owur static int parse_expected_servername(SSL_TEST_CTX *test_ctx,
  186. const char *value)
  187. {
  188. int ret_value;
  189. if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername),
  190. &ret_value, value)) {
  191. return 0;
  192. }
  193. test_ctx->expected_servername = ret_value;
  194. return 1;
  195. }
  196. const char *ssl_servername_name(ssl_servername_t server)
  197. {
  198. return enum_name(ssl_servername, OSSL_NELEM(ssl_servername),
  199. server);
  200. }
  201. /* ServerNameCallback */
  202. static const test_enum ssl_servername_callbacks[] = {
  203. {"None", SSL_TEST_SERVERNAME_CB_NONE},
  204. {"IgnoreMismatch", SSL_TEST_SERVERNAME_IGNORE_MISMATCH},
  205. {"RejectMismatch", SSL_TEST_SERVERNAME_REJECT_MISMATCH},
  206. {"ClientHelloIgnoreMismatch",
  207. SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH},
  208. {"ClientHelloRejectMismatch",
  209. SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH},
  210. {"ClientHelloNoV12", SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12},
  211. };
  212. __owur static int parse_servername_callback(SSL_TEST_SERVER_CONF *server_conf,
  213. const char *value)
  214. {
  215. int ret_value;
  216. if (!parse_enum(ssl_servername_callbacks,
  217. OSSL_NELEM(ssl_servername_callbacks), &ret_value, value)) {
  218. return 0;
  219. }
  220. server_conf->servername_callback = ret_value;
  221. return 1;
  222. }
  223. const char *ssl_servername_callback_name(ssl_servername_callback_t callback)
  224. {
  225. return enum_name(ssl_servername_callbacks,
  226. OSSL_NELEM(ssl_servername_callbacks), callback);
  227. }
  228. /* SessionTicketExpected */
  229. static const test_enum ssl_session_ticket[] = {
  230. {"Ignore", SSL_TEST_SESSION_TICKET_IGNORE},
  231. {"Yes", SSL_TEST_SESSION_TICKET_YES},
  232. {"No", SSL_TEST_SESSION_TICKET_NO},
  233. };
  234. __owur static int parse_session_ticket(SSL_TEST_CTX *test_ctx, const char *value)
  235. {
  236. int ret_value;
  237. if (!parse_enum(ssl_session_ticket, OSSL_NELEM(ssl_session_ticket),
  238. &ret_value, value)) {
  239. return 0;
  240. }
  241. test_ctx->session_ticket_expected = ret_value;
  242. return 1;
  243. }
  244. const char *ssl_session_ticket_name(ssl_session_ticket_t server)
  245. {
  246. return enum_name(ssl_session_ticket,
  247. OSSL_NELEM(ssl_session_ticket),
  248. server);
  249. }
  250. /* CompressionExpected */
  251. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, compression_expected)
  252. /* SessionIdExpected */
  253. static const test_enum ssl_session_id[] = {
  254. {"Ignore", SSL_TEST_SESSION_ID_IGNORE},
  255. {"Yes", SSL_TEST_SESSION_ID_YES},
  256. {"No", SSL_TEST_SESSION_ID_NO},
  257. };
  258. __owur static int parse_session_id(SSL_TEST_CTX *test_ctx, const char *value)
  259. {
  260. int ret_value;
  261. if (!parse_enum(ssl_session_id, OSSL_NELEM(ssl_session_id),
  262. &ret_value, value)) {
  263. return 0;
  264. }
  265. test_ctx->session_id_expected = ret_value;
  266. return 1;
  267. }
  268. const char *ssl_session_id_name(ssl_session_id_t server)
  269. {
  270. return enum_name(ssl_session_id,
  271. OSSL_NELEM(ssl_session_id),
  272. server);
  273. }
  274. /* Method */
  275. static const test_enum ssl_test_methods[] = {
  276. {"TLS", SSL_TEST_METHOD_TLS},
  277. {"DTLS", SSL_TEST_METHOD_DTLS},
  278. {"QUIC", SSL_TEST_METHOD_QUIC}
  279. };
  280. __owur static int parse_test_method(SSL_TEST_CTX *test_ctx, const char *value)
  281. {
  282. int ret_value;
  283. if (!parse_enum(ssl_test_methods, OSSL_NELEM(ssl_test_methods),
  284. &ret_value, value)) {
  285. return 0;
  286. }
  287. test_ctx->method = ret_value;
  288. return 1;
  289. }
  290. const char *ssl_test_method_name(ssl_test_method_t method)
  291. {
  292. return enum_name(ssl_test_methods, OSSL_NELEM(ssl_test_methods), method);
  293. }
  294. /* NPN and ALPN options */
  295. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, npn_protocols)
  296. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, npn_protocols)
  297. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_npn_protocol)
  298. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, alpn_protocols)
  299. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, alpn_protocols)
  300. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_alpn_protocol)
  301. /* SRP options */
  302. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, srp_user)
  303. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, srp_user)
  304. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, srp_password)
  305. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, srp_password)
  306. /* Session Ticket App Data options */
  307. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_session_ticket_app_data)
  308. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, session_ticket_app_data)
  309. /* Handshake mode */
  310. static const test_enum ssl_handshake_modes[] = {
  311. {"Simple", SSL_TEST_HANDSHAKE_SIMPLE},
  312. {"Resume", SSL_TEST_HANDSHAKE_RESUME},
  313. {"RenegotiateServer", SSL_TEST_HANDSHAKE_RENEG_SERVER},
  314. {"RenegotiateClient", SSL_TEST_HANDSHAKE_RENEG_CLIENT},
  315. {"KeyUpdateServer", SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER},
  316. {"KeyUpdateClient", SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT},
  317. {"PostHandshakeAuth", SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH},
  318. };
  319. __owur static int parse_handshake_mode(SSL_TEST_CTX *test_ctx, const char *value)
  320. {
  321. int ret_value;
  322. if (!parse_enum(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes),
  323. &ret_value, value)) {
  324. return 0;
  325. }
  326. test_ctx->handshake_mode = ret_value;
  327. return 1;
  328. }
  329. const char *ssl_handshake_mode_name(ssl_handshake_mode_t mode)
  330. {
  331. return enum_name(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes),
  332. mode);
  333. }
  334. /* Renegotiation Ciphersuites */
  335. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, reneg_ciphers)
  336. /* KeyUpdateType */
  337. static const test_enum ssl_key_update_types[] = {
  338. {"KeyUpdateRequested", SSL_KEY_UPDATE_REQUESTED},
  339. {"KeyUpdateNotRequested", SSL_KEY_UPDATE_NOT_REQUESTED},
  340. };
  341. __owur static int parse_key_update_type(SSL_TEST_CTX *test_ctx, const char *value)
  342. {
  343. int ret_value;
  344. if (!parse_enum(ssl_key_update_types, OSSL_NELEM(ssl_key_update_types),
  345. &ret_value, value)) {
  346. return 0;
  347. }
  348. test_ctx->key_update_type = ret_value;
  349. return 1;
  350. }
  351. /* CT Validation */
  352. static const test_enum ssl_ct_validation_modes[] = {
  353. {"None", SSL_TEST_CT_VALIDATION_NONE},
  354. {"Permissive", SSL_TEST_CT_VALIDATION_PERMISSIVE},
  355. {"Strict", SSL_TEST_CT_VALIDATION_STRICT},
  356. };
  357. __owur static int parse_ct_validation(SSL_TEST_CLIENT_CONF *client_conf,
  358. const char *value)
  359. {
  360. int ret_value;
  361. if (!parse_enum(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes),
  362. &ret_value, value)) {
  363. return 0;
  364. }
  365. client_conf->ct_validation = ret_value;
  366. return 1;
  367. }
  368. const char *ssl_ct_validation_name(ssl_ct_validation_t mode)
  369. {
  370. return enum_name(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes),
  371. mode);
  372. }
  373. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, resumption_expected)
  374. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_SERVER_CONF, server, broken_session_ticket)
  375. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, use_sctp)
  376. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, compress_certificates)
  377. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, enable_client_sctp_label_bug)
  378. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, enable_server_sctp_label_bug)
  379. /* CertStatus */
  380. static const test_enum ssl_certstatus[] = {
  381. {"None", SSL_TEST_CERT_STATUS_NONE},
  382. {"GoodResponse", SSL_TEST_CERT_STATUS_GOOD_RESPONSE},
  383. {"BadResponse", SSL_TEST_CERT_STATUS_BAD_RESPONSE}
  384. };
  385. __owur static int parse_certstatus(SSL_TEST_SERVER_CONF *server_conf,
  386. const char *value)
  387. {
  388. int ret_value;
  389. if (!parse_enum(ssl_certstatus, OSSL_NELEM(ssl_certstatus), &ret_value,
  390. value)) {
  391. return 0;
  392. }
  393. server_conf->cert_status = ret_value;
  394. return 1;
  395. }
  396. const char *ssl_certstatus_name(ssl_cert_status_t cert_status)
  397. {
  398. return enum_name(ssl_certstatus,
  399. OSSL_NELEM(ssl_certstatus), cert_status);
  400. }
  401. /* ApplicationData */
  402. IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, app_data_size)
  403. /* MaxFragmentSize */
  404. IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, max_fragment_size)
  405. /* Maximum-Fragment-Length TLS extension mode */
  406. static const test_enum ssl_max_fragment_len_mode[] = {
  407. {"None", TLSEXT_max_fragment_length_DISABLED},
  408. { "512", TLSEXT_max_fragment_length_512},
  409. {"1024", TLSEXT_max_fragment_length_1024},
  410. {"2048", TLSEXT_max_fragment_length_2048},
  411. {"4096", TLSEXT_max_fragment_length_4096}
  412. };
  413. __owur static int parse_max_fragment_len_mode(SSL_TEST_CLIENT_CONF *client_conf,
  414. const char *value)
  415. {
  416. int ret_value;
  417. if (!parse_enum(ssl_max_fragment_len_mode,
  418. OSSL_NELEM(ssl_max_fragment_len_mode), &ret_value, value)) {
  419. return 0;
  420. }
  421. client_conf->max_fragment_len_mode = ret_value;
  422. return 1;
  423. }
  424. const char *ssl_max_fragment_len_name(int MFL_mode)
  425. {
  426. return enum_name(ssl_max_fragment_len_mode,
  427. OSSL_NELEM(ssl_max_fragment_len_mode), MFL_mode);
  428. }
  429. /* Expected key and signature types */
  430. __owur static int parse_expected_key_type(int *ptype, const char *value)
  431. {
  432. int nid;
  433. const EVP_PKEY_ASN1_METHOD *ameth;
  434. if (value == NULL)
  435. return 0;
  436. ameth = EVP_PKEY_asn1_find_str(NULL, value, -1);
  437. if (ameth != NULL)
  438. EVP_PKEY_asn1_get0_info(&nid, NULL, NULL, NULL, NULL, ameth);
  439. else
  440. nid = OBJ_sn2nid(value);
  441. if (nid == NID_undef)
  442. nid = OBJ_ln2nid(value);
  443. #ifndef OPENSSL_NO_EC
  444. if (nid == NID_undef)
  445. nid = EC_curve_nist2nid(value);
  446. #endif
  447. switch (nid) {
  448. case NID_brainpoolP256r1tls13:
  449. nid = NID_brainpoolP256r1;
  450. break;
  451. case NID_brainpoolP384r1tls13:
  452. nid = NID_brainpoolP384r1;
  453. break;
  454. case NID_brainpoolP512r1tls13:
  455. nid = NID_brainpoolP512r1;
  456. break;
  457. }
  458. if (nid == NID_undef)
  459. return 0;
  460. *ptype = nid;
  461. return 1;
  462. }
  463. __owur static int parse_expected_tmp_key_type(SSL_TEST_CTX *test_ctx,
  464. const char *value)
  465. {
  466. return parse_expected_key_type(&test_ctx->expected_tmp_key_type, value);
  467. }
  468. __owur static int parse_expected_server_cert_type(SSL_TEST_CTX *test_ctx,
  469. const char *value)
  470. {
  471. return parse_expected_key_type(&test_ctx->expected_server_cert_type,
  472. value);
  473. }
  474. __owur static int parse_expected_server_sign_type(SSL_TEST_CTX *test_ctx,
  475. const char *value)
  476. {
  477. return parse_expected_key_type(&test_ctx->expected_server_sign_type,
  478. value);
  479. }
  480. __owur static int parse_expected_client_cert_type(SSL_TEST_CTX *test_ctx,
  481. const char *value)
  482. {
  483. return parse_expected_key_type(&test_ctx->expected_client_cert_type,
  484. value);
  485. }
  486. __owur static int parse_expected_client_sign_type(SSL_TEST_CTX *test_ctx,
  487. const char *value)
  488. {
  489. return parse_expected_key_type(&test_ctx->expected_client_sign_type,
  490. value);
  491. }
  492. /* Expected signing hash */
  493. __owur static int parse_expected_sign_hash(int *ptype, const char *value)
  494. {
  495. int nid;
  496. if (value == NULL)
  497. return 0;
  498. nid = OBJ_sn2nid(value);
  499. if (nid == NID_undef)
  500. nid = OBJ_ln2nid(value);
  501. if (nid == NID_undef)
  502. return 0;
  503. *ptype = nid;
  504. return 1;
  505. }
  506. __owur static int parse_expected_server_sign_hash(SSL_TEST_CTX *test_ctx,
  507. const char *value)
  508. {
  509. return parse_expected_sign_hash(&test_ctx->expected_server_sign_hash,
  510. value);
  511. }
  512. __owur static int parse_expected_client_sign_hash(SSL_TEST_CTX *test_ctx,
  513. const char *value)
  514. {
  515. return parse_expected_sign_hash(&test_ctx->expected_client_sign_hash,
  516. value);
  517. }
  518. __owur static int parse_expected_ca_names(STACK_OF(X509_NAME) **pnames,
  519. const char *value,
  520. OSSL_LIB_CTX *libctx)
  521. {
  522. if (value == NULL)
  523. return 0;
  524. if (!strcmp(value, "empty"))
  525. *pnames = sk_X509_NAME_new_null();
  526. else
  527. *pnames = SSL_load_client_CA_file_ex(value, libctx, NULL);
  528. return *pnames != NULL;
  529. }
  530. __owur static int parse_expected_server_ca_names(SSL_TEST_CTX *test_ctx,
  531. const char *value)
  532. {
  533. return parse_expected_ca_names(&test_ctx->expected_server_ca_names, value,
  534. test_ctx->libctx);
  535. }
  536. __owur static int parse_expected_client_ca_names(SSL_TEST_CTX *test_ctx,
  537. const char *value)
  538. {
  539. return parse_expected_ca_names(&test_ctx->expected_client_ca_names, value,
  540. test_ctx->libctx);
  541. }
  542. /* ExpectedCipher */
  543. IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_cipher)
  544. /* Client and Server PHA */
  545. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CLIENT_CONF, client, enable_pha)
  546. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_SERVER_CONF, server, force_pha)
  547. IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CLIENT_CONF, client, no_extms_on_reneg)
  548. /* Known test options and their corresponding parse methods. */
  549. /* Top-level options. */
  550. typedef struct {
  551. const char *name;
  552. int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
  553. } ssl_test_ctx_option;
  554. static const ssl_test_ctx_option ssl_test_ctx_options[] = {
  555. { "ExpectedResult", &parse_expected_result },
  556. { "ExpectedClientAlert", &parse_client_alert },
  557. { "ExpectedServerAlert", &parse_server_alert },
  558. { "ExpectedProtocol", &parse_protocol },
  559. { "ExpectedServerName", &parse_expected_servername },
  560. { "SessionTicketExpected", &parse_session_ticket },
  561. { "CompressionExpected", &parse_test_compression_expected },
  562. { "SessionIdExpected", &parse_session_id },
  563. { "Method", &parse_test_method },
  564. { "ExpectedNPNProtocol", &parse_test_expected_npn_protocol },
  565. { "ExpectedALPNProtocol", &parse_test_expected_alpn_protocol },
  566. { "HandshakeMode", &parse_handshake_mode },
  567. { "KeyUpdateType", &parse_key_update_type },
  568. { "ResumptionExpected", &parse_test_resumption_expected },
  569. { "ApplicationData", &parse_test_app_data_size },
  570. { "MaxFragmentSize", &parse_test_max_fragment_size },
  571. { "ExpectedTmpKeyType", &parse_expected_tmp_key_type },
  572. { "ExpectedServerCertType", &parse_expected_server_cert_type },
  573. { "ExpectedServerSignHash", &parse_expected_server_sign_hash },
  574. { "ExpectedServerSignType", &parse_expected_server_sign_type },
  575. { "ExpectedServerCANames", &parse_expected_server_ca_names },
  576. { "ExpectedClientCertType", &parse_expected_client_cert_type },
  577. { "ExpectedClientSignHash", &parse_expected_client_sign_hash },
  578. { "ExpectedClientSignType", &parse_expected_client_sign_type },
  579. { "ExpectedClientCANames", &parse_expected_client_ca_names },
  580. { "UseSCTP", &parse_test_use_sctp },
  581. { "CompressCertificates", &parse_test_compress_certificates },
  582. { "EnableClientSCTPLabelBug", &parse_test_enable_client_sctp_label_bug },
  583. { "EnableServerSCTPLabelBug", &parse_test_enable_server_sctp_label_bug },
  584. { "ExpectedCipher", &parse_test_expected_cipher },
  585. { "ExpectedSessionTicketAppData", &parse_test_expected_session_ticket_app_data },
  586. };
  587. /* Nested client options. */
  588. typedef struct {
  589. const char *name;
  590. int (*parse)(SSL_TEST_CLIENT_CONF *conf, const char *value);
  591. } ssl_test_client_option;
  592. static const ssl_test_client_option ssl_test_client_options[] = {
  593. { "VerifyCallback", &parse_client_verify_callback },
  594. { "ServerName", &parse_servername },
  595. { "NPNProtocols", &parse_client_npn_protocols },
  596. { "ALPNProtocols", &parse_client_alpn_protocols },
  597. { "CTValidation", &parse_ct_validation },
  598. { "RenegotiateCiphers", &parse_client_reneg_ciphers},
  599. { "SRPUser", &parse_client_srp_user },
  600. { "SRPPassword", &parse_client_srp_password },
  601. { "MaxFragmentLenExt", &parse_max_fragment_len_mode },
  602. { "EnablePHA", &parse_client_enable_pha },
  603. { "RenegotiateNoExtms", &parse_client_no_extms_on_reneg },
  604. };
  605. /* Nested server options. */
  606. typedef struct {
  607. const char *name;
  608. int (*parse)(SSL_TEST_SERVER_CONF *conf, const char *value);
  609. } ssl_test_server_option;
  610. static const ssl_test_server_option ssl_test_server_options[] = {
  611. { "ServerNameCallback", &parse_servername_callback },
  612. { "NPNProtocols", &parse_server_npn_protocols },
  613. { "ALPNProtocols", &parse_server_alpn_protocols },
  614. { "BrokenSessionTicket", &parse_server_broken_session_ticket },
  615. { "CertStatus", &parse_certstatus },
  616. { "SRPUser", &parse_server_srp_user },
  617. { "SRPPassword", &parse_server_srp_password },
  618. { "ForcePHA", &parse_server_force_pha },
  619. { "SessionTicketAppData", &parse_server_session_ticket_app_data },
  620. };
  621. SSL_TEST_CTX *SSL_TEST_CTX_new(OSSL_LIB_CTX *libctx)
  622. {
  623. SSL_TEST_CTX *ret;
  624. /* The return code is checked by caller */
  625. if ((ret = OPENSSL_zalloc(sizeof(*ret))) != NULL) {
  626. ret->libctx = libctx;
  627. ret->app_data_size = default_app_data_size;
  628. ret->max_fragment_size = default_max_fragment_size;
  629. }
  630. return ret;
  631. }
  632. static void ssl_test_extra_conf_free_data(SSL_TEST_EXTRA_CONF *conf)
  633. {
  634. OPENSSL_free(conf->client.npn_protocols);
  635. OPENSSL_free(conf->server.npn_protocols);
  636. OPENSSL_free(conf->server2.npn_protocols);
  637. OPENSSL_free(conf->client.alpn_protocols);
  638. OPENSSL_free(conf->server.alpn_protocols);
  639. OPENSSL_free(conf->server2.alpn_protocols);
  640. OPENSSL_free(conf->client.reneg_ciphers);
  641. OPENSSL_free(conf->server.srp_user);
  642. OPENSSL_free(conf->server.srp_password);
  643. OPENSSL_free(conf->server2.srp_user);
  644. OPENSSL_free(conf->server2.srp_password);
  645. OPENSSL_free(conf->client.srp_user);
  646. OPENSSL_free(conf->client.srp_password);
  647. OPENSSL_free(conf->server.session_ticket_app_data);
  648. OPENSSL_free(conf->server2.session_ticket_app_data);
  649. }
  650. static void ssl_test_ctx_free_extra_data(SSL_TEST_CTX *ctx)
  651. {
  652. ssl_test_extra_conf_free_data(&ctx->extra);
  653. ssl_test_extra_conf_free_data(&ctx->resume_extra);
  654. }
  655. void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
  656. {
  657. if (ctx == NULL)
  658. return;
  659. ssl_test_ctx_free_extra_data(ctx);
  660. OPENSSL_free(ctx->expected_npn_protocol);
  661. OPENSSL_free(ctx->expected_alpn_protocol);
  662. OPENSSL_free(ctx->expected_session_ticket_app_data);
  663. sk_X509_NAME_pop_free(ctx->expected_server_ca_names, X509_NAME_free);
  664. sk_X509_NAME_pop_free(ctx->expected_client_ca_names, X509_NAME_free);
  665. OPENSSL_free(ctx->expected_cipher);
  666. OPENSSL_free(ctx);
  667. }
  668. static int parse_client_options(SSL_TEST_CLIENT_CONF *client, const CONF *conf,
  669. const char *client_section)
  670. {
  671. STACK_OF(CONF_VALUE) *sk_conf;
  672. int i;
  673. size_t j;
  674. if (!TEST_ptr(sk_conf = NCONF_get_section(conf, client_section)))
  675. return 0;
  676. for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
  677. int found = 0;
  678. const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
  679. for (j = 0; j < OSSL_NELEM(ssl_test_client_options); j++) {
  680. if (strcmp(option->name, ssl_test_client_options[j].name) == 0) {
  681. if (!ssl_test_client_options[j].parse(client, option->value)) {
  682. TEST_info("Bad value %s for option %s",
  683. option->value, option->name);
  684. return 0;
  685. }
  686. found = 1;
  687. break;
  688. }
  689. }
  690. if (!found) {
  691. TEST_info("Unknown test option: %s", option->name);
  692. return 0;
  693. }
  694. }
  695. return 1;
  696. }
  697. static int parse_server_options(SSL_TEST_SERVER_CONF *server, const CONF *conf,
  698. const char *server_section)
  699. {
  700. STACK_OF(CONF_VALUE) *sk_conf;
  701. int i;
  702. size_t j;
  703. if (!TEST_ptr(sk_conf = NCONF_get_section(conf, server_section)))
  704. return 0;
  705. for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
  706. int found = 0;
  707. const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
  708. for (j = 0; j < OSSL_NELEM(ssl_test_server_options); j++) {
  709. if (strcmp(option->name, ssl_test_server_options[j].name) == 0) {
  710. if (!ssl_test_server_options[j].parse(server, option->value)) {
  711. TEST_info("Bad value %s for option %s",
  712. option->value, option->name);
  713. return 0;
  714. }
  715. found = 1;
  716. break;
  717. }
  718. }
  719. if (!found) {
  720. TEST_info("Unknown test option: %s", option->name);
  721. return 0;
  722. }
  723. }
  724. return 1;
  725. }
  726. SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section,
  727. OSSL_LIB_CTX *libctx)
  728. {
  729. STACK_OF(CONF_VALUE) *sk_conf = NULL;
  730. SSL_TEST_CTX *ctx = NULL;
  731. int i;
  732. size_t j;
  733. if (!TEST_ptr(sk_conf = NCONF_get_section(conf, test_section))
  734. || !TEST_ptr(ctx = SSL_TEST_CTX_new(libctx)))
  735. goto err;
  736. for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
  737. int found = 0;
  738. const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
  739. /* Subsections */
  740. if (strcmp(option->name, "client") == 0) {
  741. if (!parse_client_options(&ctx->extra.client, conf, option->value))
  742. goto err;
  743. } else if (strcmp(option->name, "server") == 0) {
  744. if (!parse_server_options(&ctx->extra.server, conf, option->value))
  745. goto err;
  746. } else if (strcmp(option->name, "server2") == 0) {
  747. if (!parse_server_options(&ctx->extra.server2, conf, option->value))
  748. goto err;
  749. } else if (strcmp(option->name, "resume-client") == 0) {
  750. if (!parse_client_options(&ctx->resume_extra.client, conf,
  751. option->value))
  752. goto err;
  753. } else if (strcmp(option->name, "resume-server") == 0) {
  754. if (!parse_server_options(&ctx->resume_extra.server, conf,
  755. option->value))
  756. goto err;
  757. } else if (strcmp(option->name, "resume-server2") == 0) {
  758. if (!parse_server_options(&ctx->resume_extra.server2, conf,
  759. option->value))
  760. goto err;
  761. } else {
  762. for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
  763. if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
  764. if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
  765. TEST_info("Bad value %s for option %s",
  766. option->value, option->name);
  767. goto err;
  768. }
  769. found = 1;
  770. break;
  771. }
  772. }
  773. if (!found) {
  774. TEST_info("Unknown test option: %s", option->name);
  775. goto err;
  776. }
  777. }
  778. }
  779. goto done;
  780. err:
  781. SSL_TEST_CTX_free(ctx);
  782. ctx = NULL;
  783. done:
  784. return ctx;
  785. }