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