ssl_test_ctx.c 29 KB

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