2
0

ssl_test_ctx.c 29 KB

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