ssl_test_ctx.c 20.2 KB
Newer Older
E
Emilia Kasper 已提交
1 2 3
/*
 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
 *
R
Rich Salz 已提交
4 5 6
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
E
Emilia Kasper 已提交
7 8 9 10 11 12 13 14 15 16
 * https://www.openssl.org/source/license.html
 */

#include <string.h>

#include <openssl/e_os2.h>
#include <openssl/crypto.h>

#include "e_os.h"
#include "ssl_test_ctx.h"
E
Emilia Kasper 已提交
17
#include "testutil.h"
E
Emilia Kasper 已提交
18

19
static const int default_app_data_size = 256;
E
Emilia Kasper 已提交
20 21
/* Default set to be as small as possible to exercise fragmentation. */
static const int default_max_fragment_size = 512;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

static int parse_boolean(const char *value, int *result)
{
    if (strcasecmp(value, "Yes") == 0) {
        *result = 1;
        return 1;
    }
    else if (strcasecmp(value, "No") == 0) {
        *result = 0;
        return 1;
    }
    return 0;
}

#define IMPLEMENT_SSL_TEST_BOOL_OPTION(struct_type, name, field)        \
    static int parse_##name##_##field(struct_type *ctx, const char *value) \
    {                                                                   \
        return parse_boolean(value, &ctx->field);                       \
    }

#define IMPLEMENT_SSL_TEST_STRING_OPTION(struct_type, name, field)      \
    static int parse_##name##_##field(struct_type *ctx, const char *value) \
    {                                                                   \
        OPENSSL_free(ctx->field);                                       \
        ctx->field = OPENSSL_strdup(value);                             \
        TEST_check(ctx->field != NULL);                                 \
        return 1;                                                       \
    }

#define IMPLEMENT_SSL_TEST_INT_OPTION(struct_type, name, field)        \
    static int parse_##name##_##field(struct_type *ctx, const char *value) \
    {                                                                   \
        ctx->field = atoi(value);                                       \
        return 1;                                                       \
    }

E
Emilia Kasper 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* True enums and other test configuration values that map to an int. */
typedef struct {
    const char *name;
    int value;
} test_enum;


__owur static int parse_enum(const test_enum *enums, size_t num_enums,
                             int *value, const char *name)
{
    size_t i;
    for (i = 0; i < num_enums; i++) {
        if (strcmp(enums[i].name, name) == 0) {
            *value = enums[i].value;
            return 1;
        }
    }
    return 0;
}

static const char *enum_name(const test_enum *enums, size_t num_enums,
                             int value)
{
    size_t i;
    for (i = 0; i < num_enums; i++) {
        if (enums[i].value == value) {
            return enums[i].name;
        }
    }
    return "InvalidValue";
}


/*******************/
/* ExpectedResult. */
/*******************/

static const test_enum ssl_test_results[] = {
    {"Success", SSL_TEST_SUCCESS},
    {"ServerFail", SSL_TEST_SERVER_FAIL},
    {"ClientFail", SSL_TEST_CLIENT_FAIL},
    {"InternalError", SSL_TEST_INTERNAL_ERROR},
};

__owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
                    &ret_value, value)) {
        return 0;
    }
    test_ctx->expected_result = ret_value;
    return 1;
}

113
const char *ssl_test_result_name(ssl_test_result_t result)
E
Emilia Kasper 已提交
114 115 116 117
{
    return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
}

E
Emilia Kasper 已提交
118 119 120
/**********************************************/
/* ExpectedClientAlert / ExpectedServerAlert. */
/**********************************************/
E
Emilia Kasper 已提交
121 122 123

static const test_enum ssl_alerts[] = {
    {"UnknownCA", SSL_AD_UNKNOWN_CA},
124
    {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
125
    {"UnrecognizedName", SSL_AD_UNRECOGNIZED_NAME},
126 127
    {"BadCertificate", SSL_AD_BAD_CERTIFICATE},
    {"NoApplicationProtocol", SSL_AD_NO_APPLICATION_PROTOCOL},
E
Emilia Kasper 已提交
128 129 130 131 132 133 134 135 136
};

__owur static int parse_alert(int *alert, const char *value)
{
    return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
}

__owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
{
E
Emilia Kasper 已提交
137
    return parse_alert(&test_ctx->expected_client_alert, value);
E
Emilia Kasper 已提交
138 139 140 141
}

__owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
{
E
Emilia Kasper 已提交
142
    return parse_alert(&test_ctx->expected_server_alert, value);
E
Emilia Kasper 已提交
143 144 145 146 147 148 149
}

const char *ssl_alert_name(int alert)
{
    return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
}

E
Emilia Kasper 已提交
150 151 152
/********************/
/* ExpectedProtocol */
/********************/
E
Emilia Kasper 已提交
153 154 155 156 157 158

static const test_enum ssl_protocols[] = {
     {"TLSv1.2", TLS1_2_VERSION},
     {"TLSv1.1", TLS1_1_VERSION},
     {"TLSv1", TLS1_VERSION},
     {"SSLv3", SSL3_VERSION},
159 160
     {"DTLSv1", DTLS1_VERSION},
     {"DTLSv1.2", DTLS1_2_VERSION},
E
Emilia Kasper 已提交
161 162 163 164 165
};

__owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
{
    return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
E
Emilia Kasper 已提交
166
                      &test_ctx->expected_protocol, value);
E
Emilia Kasper 已提交
167 168 169 170 171 172 173
}

const char *ssl_protocol_name(int protocol)
{
    return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
}

174
/***********************/
175
/* VerifyCallback.     */
176 177 178 179 180 181 182 183
/***********************/

static const test_enum ssl_verify_callbacks[] = {
    {"None", SSL_TEST_VERIFY_NONE},
    {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL},
    {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL},
};

E
Emilia Kasper 已提交
184
__owur static int parse_client_verify_callback(SSL_TEST_CLIENT_CONF *client_conf,
185
                                               const char *value)
186 187 188 189 190 191
{
    int ret_value;
    if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
                    &ret_value, value)) {
        return 0;
    }
E
Emilia Kasper 已提交
192
    client_conf->verify_callback = ret_value;
193 194 195 196 197 198 199 200 201
    return 1;
}

const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
{
    return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
                     callback);
}

T
Todd Short 已提交
202 203 204 205 206
/**************/
/* ServerName */
/**************/

static const test_enum ssl_servername[] = {
E
Emilia Kasper 已提交
207
    {"None", SSL_TEST_SERVERNAME_NONE},
T
Todd Short 已提交
208 209
    {"server1", SSL_TEST_SERVERNAME_SERVER1},
    {"server2", SSL_TEST_SERVERNAME_SERVER2},
210
    {"invalid", SSL_TEST_SERVERNAME_INVALID},
T
Todd Short 已提交
211 212
};

E
Emilia Kasper 已提交
213
__owur static int parse_servername(SSL_TEST_CLIENT_CONF *client_conf,
T
Todd Short 已提交
214 215 216 217 218 219 220
                                   const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername),
                    &ret_value, value)) {
        return 0;
    }
E
Emilia Kasper 已提交
221
    client_conf->servername = ret_value;
T
Todd Short 已提交
222 223 224
    return 1;
}

225 226 227 228 229 230 231 232 233 234 235 236
__owur static int parse_expected_servername(SSL_TEST_CTX *test_ctx,
                                            const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername),
                    &ret_value, value)) {
        return 0;
    }
    test_ctx->expected_servername = ret_value;
    return 1;
}

T
Todd Short 已提交
237 238 239 240 241 242
const char *ssl_servername_name(ssl_servername_t server)
{
    return enum_name(ssl_servername, OSSL_NELEM(ssl_servername),
                     server);
}

E
Emilia Kasper 已提交
243 244 245
/**********************/
/* ServerNameCallback */
/**********************/
246 247 248 249 250 251 252

static const test_enum ssl_servername_callbacks[] = {
    {"None", SSL_TEST_SERVERNAME_CB_NONE},
    {"IgnoreMismatch", SSL_TEST_SERVERNAME_IGNORE_MISMATCH},
    {"RejectMismatch", SSL_TEST_SERVERNAME_REJECT_MISMATCH},
};

E
Emilia Kasper 已提交
253 254
__owur static int parse_servername_callback(SSL_TEST_SERVER_CONF *server_conf,
                                            const char *value)
255 256 257 258 259 260
{
    int ret_value;
    if (!parse_enum(ssl_servername_callbacks,
                    OSSL_NELEM(ssl_servername_callbacks), &ret_value, value)) {
        return 0;
    }
E
Emilia Kasper 已提交
261
    server_conf->servername_callback = ret_value;
262 263 264 265 266 267 268 269 270
    return 1;
}

const char *ssl_servername_callback_name(ssl_servername_callback_t callback)
{
    return enum_name(ssl_servername_callbacks,
                     OSSL_NELEM(ssl_servername_callbacks), callback);
}

T
Todd Short 已提交
271 272 273 274
/*************************/
/* SessionTicketExpected */
/*************************/

E
Emilia Kasper 已提交
275
static const test_enum ssl_session_ticket[] = {
T
Todd Short 已提交
276 277 278 279 280
    {"Ignore", SSL_TEST_SESSION_TICKET_IGNORE},
    {"Yes", SSL_TEST_SESSION_TICKET_YES},
    {"No", SSL_TEST_SESSION_TICKET_NO},
};

E
Emilia Kasper 已提交
281
__owur static int parse_session_ticket(SSL_TEST_CTX *test_ctx, const char *value)
T
Todd Short 已提交
282 283
{
    int ret_value;
E
Emilia Kasper 已提交
284
    if (!parse_enum(ssl_session_ticket, OSSL_NELEM(ssl_session_ticket),
T
Todd Short 已提交
285 286 287 288 289 290 291
                    &ret_value, value)) {
        return 0;
    }
    test_ctx->session_ticket_expected = ret_value;
    return 1;
}

E
Emilia Kasper 已提交
292
const char *ssl_session_ticket_name(ssl_session_ticket_t server)
T
Todd Short 已提交
293
{
E
Emilia Kasper 已提交
294 295
    return enum_name(ssl_session_ticket,
                     OSSL_NELEM(ssl_session_ticket),
T
Todd Short 已提交
296 297
                     server);
}
E
Emilia Kasper 已提交
298

299
/***********************/
E
Emilia Kasper 已提交
300
/* Method              */
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/***********************/

static const test_enum ssl_test_methods[] = {
    {"TLS", SSL_TEST_METHOD_TLS},
    {"DTLS", SSL_TEST_METHOD_DTLS},
};

__owur static int parse_test_method(SSL_TEST_CTX *test_ctx, const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_test_methods, OSSL_NELEM(ssl_test_methods),
                    &ret_value, value)) {
        return 0;
    }
    test_ctx->method = ret_value;
    return 1;
}

const char *ssl_test_method_name(ssl_test_method_t method)
{
    return enum_name(ssl_test_methods, OSSL_NELEM(ssl_test_methods), method);
}

324 325 326 327
/************************************/
/* NPN and ALPN options             */
/************************************/

E
Emilia Kasper 已提交
328 329 330 331 332 333
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, npn_protocols)
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, npn_protocols)
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_npn_protocol)
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, alpn_protocols)
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, alpn_protocols)
IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_alpn_protocol)
334

335 336 337 338 339 340 341
/***********************/
/* Handshake mode      */
/***********************/

static const test_enum ssl_handshake_modes[] = {
    {"Simple", SSL_TEST_HANDSHAKE_SIMPLE},
    {"Resume", SSL_TEST_HANDSHAKE_RESUME},
M
Matt Caswell 已提交
342 343
    {"RenegotiateServer", SSL_TEST_HANDSHAKE_RENEG_SERVER},
    {"RenegotiateClient", SSL_TEST_HANDSHAKE_RENEG_CLIENT},
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
};

__owur static int parse_handshake_mode(SSL_TEST_CTX *test_ctx, const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes),
                    &ret_value, value)) {
        return 0;
    }
    test_ctx->handshake_mode = ret_value;
    return 1;
}

const char *ssl_handshake_mode_name(ssl_handshake_mode_t mode)
{
    return enum_name(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes),
                     mode);
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/***********************/
/* CT Validation       */
/***********************/

static const test_enum ssl_ct_validation_modes[] = {
    {"None", SSL_TEST_CT_VALIDATION_NONE},
    {"Permissive", SSL_TEST_CT_VALIDATION_PERMISSIVE},
    {"Strict", SSL_TEST_CT_VALIDATION_STRICT},
};

__owur static int parse_ct_validation(SSL_TEST_CLIENT_CONF *client_conf,
                                      const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes),
                    &ret_value, value)) {
        return 0;
    }
    client_conf->ct_validation = ret_value;
    return 1;
}

const char *ssl_ct_validation_name(ssl_ct_validation_t mode)
{
    return enum_name(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes),
                     mode);
}

E
Emilia Kasper 已提交
391 392
IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, resumption_expected)
IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_SERVER_CONF, server, broken_session_ticket)
393

M
Matt Caswell 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/**************/
/* CertStatus */
/**************/

static const test_enum ssl_certstatus[] = {
    {"None", SSL_TEST_CERT_STATUS_NONE},
    {"GoodResponse", SSL_TEST_CERT_STATUS_GOOD_RESPONSE},
    {"BadResponse", SSL_TEST_CERT_STATUS_BAD_RESPONSE}
};

__owur static int parse_certstatus(SSL_TEST_SERVER_CONF *server_conf,
                                            const char *value)
{
    int ret_value;
    if (!parse_enum(ssl_certstatus, OSSL_NELEM(ssl_certstatus), &ret_value,
                    value)) {
        return 0;
    }
    server_conf->cert_status = ret_value;
    return 1;
}

const char *ssl_certstatus_name(ssl_cert_status_t cert_status)
{
    return enum_name(ssl_certstatus,
                     OSSL_NELEM(ssl_certstatus), cert_status);
}

422
/***********************/
E
Emilia Kasper 已提交
423
/* ApplicationData     */
424 425 426 427
/***********************/

IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, app_data_size)

E
Emilia Kasper 已提交
428 429 430 431 432 433
/***********************/
/* MaxFragmentSize     */
/***********************/

IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, max_fragment_size)

E
Emilia Kasper 已提交
434 435 436 437
/*************************************************************/
/* Known test options and their corresponding parse methods. */
/*************************************************************/

E
Emilia Kasper 已提交
438
/* Top-level options. */
E
Emilia Kasper 已提交
439 440 441 442 443 444 445
typedef struct {
    const char *name;
    int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
} ssl_test_ctx_option;

static const ssl_test_ctx_option ssl_test_ctx_options[] = {
    { "ExpectedResult", &parse_expected_result },
E
Emilia Kasper 已提交
446 447 448
    { "ExpectedClientAlert", &parse_client_alert },
    { "ExpectedServerAlert", &parse_server_alert },
    { "ExpectedProtocol", &parse_protocol },
449
    { "ExpectedServerName", &parse_expected_servername },
E
Emilia Kasper 已提交
450
    { "SessionTicketExpected", &parse_session_ticket },
451
    { "Method", &parse_test_method },
E
Emilia Kasper 已提交
452 453
    { "ExpectedNPNProtocol", &parse_test_expected_npn_protocol },
    { "ExpectedALPNProtocol", &parse_test_expected_alpn_protocol },
454
    { "HandshakeMode", &parse_handshake_mode },
E
Emilia Kasper 已提交
455
    { "ResumptionExpected", &parse_test_resumption_expected },
456
    { "ApplicationData", &parse_test_app_data_size },
E
Emilia Kasper 已提交
457
    { "MaxFragmentSize", &parse_test_max_fragment_size },
E
Emilia Kasper 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470
};

/* Nested client options. */
typedef struct {
    const char *name;
    int (*parse)(SSL_TEST_CLIENT_CONF *conf, const char *value);
} ssl_test_client_option;

static const ssl_test_client_option ssl_test_client_options[] = {
    { "VerifyCallback", &parse_client_verify_callback },
    { "ServerName", &parse_servername },
    { "NPNProtocols", &parse_client_npn_protocols },
    { "ALPNProtocols", &parse_client_alpn_protocols },
471
    { "CTValidation", &parse_ct_validation },
E
Emilia Kasper 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484
};

/* Nested server options. */
typedef struct {
    const char *name;
    int (*parse)(SSL_TEST_SERVER_CONF *conf, const char *value);
} ssl_test_server_option;

static const ssl_test_server_option ssl_test_server_options[] = {
    { "ServerNameCallback", &parse_servername_callback },
    { "NPNProtocols", &parse_server_npn_protocols },
    { "ALPNProtocols", &parse_server_alpn_protocols },
    { "BrokenSessionTicket", &parse_server_broken_session_ticket },
M
Matt Caswell 已提交
485
    { "CertStatus", &parse_certstatus },
E
Emilia Kasper 已提交
486 487 488
};

/*
E
Emilia Kasper 已提交
489
 * Since these methods are used to create tests, we use TEST_check liberally
E
Emilia Kasper 已提交
490 491 492 493 494 495
 * for malloc failures and other internal errors.
 */
SSL_TEST_CTX *SSL_TEST_CTX_new()
{
    SSL_TEST_CTX *ret;
    ret = OPENSSL_zalloc(sizeof(*ret));
E
Emilia Kasper 已提交
496
    TEST_check(ret != NULL);
497
    ret->app_data_size = default_app_data_size;
E
Emilia Kasper 已提交
498
    ret->max_fragment_size = default_max_fragment_size;
E
Emilia Kasper 已提交
499 500 501
    return ret;
}

E
Emilia Kasper 已提交
502 503 504 505 506 507 508 509 510 511 512
static void ssl_test_extra_conf_free_data(SSL_TEST_EXTRA_CONF *conf)
{
    OPENSSL_free(conf->client.npn_protocols);
    OPENSSL_free(conf->server.npn_protocols);
    OPENSSL_free(conf->server2.npn_protocols);
    OPENSSL_free(conf->client.alpn_protocols);
    OPENSSL_free(conf->server.alpn_protocols);
    OPENSSL_free(conf->server2.alpn_protocols);
}

static void ssl_test_ctx_free_extra_data(SSL_TEST_CTX *ctx)
E
Emilia Kasper 已提交
513
{
E
Emilia Kasper 已提交
514 515 516
    ssl_test_extra_conf_free_data(&ctx->extra);
    ssl_test_extra_conf_free_data(&ctx->resume_extra);
}
517

E
Emilia Kasper 已提交
518 519 520
void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
{
    ssl_test_ctx_free_extra_data(ctx);
521 522
    OPENSSL_free(ctx->expected_npn_protocol);
    OPENSSL_free(ctx->expected_alpn_protocol);
E
Emilia Kasper 已提交
523 524 525
    OPENSSL_free(ctx);
}

E
Emilia Kasper 已提交
526 527
static int parse_client_options(SSL_TEST_CLIENT_CONF *client, const CONF *conf,
                                const char *client_section)
E
Emilia Kasper 已提交
528 529 530 531 532
{
    STACK_OF(CONF_VALUE) *sk_conf;
    int i;
    size_t j;

E
Emilia Kasper 已提交
533
    sk_conf = NCONF_get_section(conf, client_section);
E
Emilia Kasper 已提交
534
    TEST_check(sk_conf != NULL);
E
Emilia Kasper 已提交
535

E
Emilia Kasper 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
        int found = 0;
        const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
        for (j = 0; j < OSSL_NELEM(ssl_test_client_options); j++) {
            if (strcmp(option->name, ssl_test_client_options[j].name) == 0) {
                if (!ssl_test_client_options[j].parse(client, option->value)) {
                    fprintf(stderr, "Bad value %s for option %s\n",
                            option->value, option->name);
                    return 0;
                }
                found = 1;
                break;
            }
        }
        if (!found) {
            fprintf(stderr, "Unknown test option: %s\n", option->name);
            return 0;
        }
    }

    return 1;
}

static int parse_server_options(SSL_TEST_SERVER_CONF *server, const CONF *conf,
                                const char *server_section)
{
    STACK_OF(CONF_VALUE) *sk_conf;
    int i;
    size_t j;

    sk_conf = NCONF_get_section(conf, server_section);
E
Emilia Kasper 已提交
567
    TEST_check(sk_conf != NULL);
E
Emilia Kasper 已提交
568 569 570 571

    for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
        int found = 0;
        const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
E
Emilia Kasper 已提交
572 573 574
        for (j = 0; j < OSSL_NELEM(ssl_test_server_options); j++) {
            if (strcmp(option->name, ssl_test_server_options[j].name) == 0) {
                if (!ssl_test_server_options[j].parse(server, option->value)) {
E
Emilia Kasper 已提交
575 576
                    fprintf(stderr, "Bad value %s for option %s\n",
                            option->value, option->name);
E
Emilia Kasper 已提交
577
                    return 0;
E
Emilia Kasper 已提交
578 579 580 581 582 583 584
                }
                found = 1;
                break;
            }
        }
        if (!found) {
            fprintf(stderr, "Unknown test option: %s\n", option->name);
E
Emilia Kasper 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            return 0;
        }
    }

    return 1;
}

SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
{
    STACK_OF(CONF_VALUE) *sk_conf;
    SSL_TEST_CTX *ctx;
    int i;
    size_t j;

    sk_conf = NCONF_get_section(conf, test_section);
E
Emilia Kasper 已提交
600
    TEST_check(sk_conf != NULL);
E
Emilia Kasper 已提交
601 602

    ctx = SSL_TEST_CTX_new();
E
Emilia Kasper 已提交
603
    TEST_check(ctx != NULL);
E
Emilia Kasper 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

    for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
        int found = 0;
        const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);

        /* Subsections */
        if (strcmp(option->name, "client") == 0) {
            if (!parse_client_options(&ctx->extra.client, conf,
                                      option->value))
                goto err;
        } else if (strcmp(option->name, "server") == 0) {
            if (!parse_server_options(&ctx->extra.server, conf,
                                      option->value))
                goto err;
        } else if (strcmp(option->name, "server2") == 0) {
            if (!parse_server_options(&ctx->extra.server2, conf,
                                      option->value))
                goto err;
        } else if (strcmp(option->name, "resume-client") == 0) {
            if (!parse_client_options(&ctx->resume_extra.client, conf,
                                      option->value))
                goto err;
        } else if (strcmp(option->name, "resume-server") == 0) {
            if (!parse_server_options(&ctx->resume_extra.server, conf,
                                      option->value))
                goto err;
        } else if (strcmp(option->name, "resume-server2") == 0) {
            if (!parse_server_options(&ctx->resume_extra.server2, conf,
                                      option->value))
                goto err;

        } else {
            for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
                if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
                    if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
                        fprintf(stderr, "Bad value %s for option %s\n",
                                option->value, option->name);
                        goto err;
                    }
                    found = 1;
                    break;
                }
            }
            if (!found) {
                fprintf(stderr, "Unknown test option: %s\n", option->name);
                goto err;
            }
E
Emilia Kasper 已提交
651 652 653 654 655 656 657 658 659 660 661
        }
    }

    goto done;

 err:
    SSL_TEST_CTX_free(ctx);
    ctx = NULL;
 done:
    return ctx;
}