2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
13 #include <openssl/e_os2.h>
14 #include <openssl/crypto.h>
17 #include "ssl_test_ctx.h"
19 /* True enums and other test configuration values that map to an int. */
26 __owur static int parse_enum(const test_enum *enums, size_t num_enums,
27 int *value, const char *name)
30 for (i = 0; i < num_enums; i++) {
31 if (strcmp(enums[i].name, name) == 0) {
32 *value = enums[i].value;
39 static const char *enum_name(const test_enum *enums, size_t num_enums,
43 for (i = 0; i < num_enums; i++) {
44 if (enums[i].value == value) {
48 return "InvalidValue";
56 static const test_enum ssl_test_results[] = {
57 {"Success", SSL_TEST_SUCCESS},
58 {"ServerFail", SSL_TEST_SERVER_FAIL},
59 {"ClientFail", SSL_TEST_CLIENT_FAIL},
60 {"InternalError", SSL_TEST_INTERNAL_ERROR},
63 __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
66 if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
70 test_ctx->expected_result = ret_value;
74 const char *ssl_test_result_name(ssl_test_result_t result)
76 return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
79 /******************************/
80 /* ClientAlert / ServerAlert. */
81 /******************************/
83 static const test_enum ssl_alerts[] = {
84 {"UnknownCA", SSL_AD_UNKNOWN_CA},
85 {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
88 __owur static int parse_alert(int *alert, const char *value)
90 return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
93 __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
95 return parse_alert(&test_ctx->client_alert, value);
98 __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
100 return parse_alert(&test_ctx->server_alert, value);
103 const char *ssl_alert_name(int alert)
105 return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
112 static const test_enum ssl_protocols[] = {
113 {"TLSv1.2", TLS1_2_VERSION},
114 {"TLSv1.1", TLS1_1_VERSION},
115 {"TLSv1", TLS1_VERSION},
116 {"SSLv3", SSL3_VERSION},
119 __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
121 return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
122 &test_ctx->protocol, value);
125 const char *ssl_protocol_name(int protocol)
127 return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
130 /***********************/
131 /* CertVerifyCallback. */
132 /***********************/
134 static const test_enum ssl_verify_callbacks[] = {
135 {"None", SSL_TEST_VERIFY_NONE},
136 {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL},
137 {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL},
140 __owur static int parse_client_verify_callback(SSL_TEST_CTX *test_ctx,
144 if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
145 &ret_value, value)) {
148 test_ctx->client_verify_callback = ret_value;
152 const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
154 return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
159 /*************************************************************/
160 /* Known test options and their corresponding parse methods. */
161 /*************************************************************/
165 int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
166 } ssl_test_ctx_option;
168 static const ssl_test_ctx_option ssl_test_ctx_options[] = {
169 { "ExpectedResult", &parse_expected_result },
170 { "ClientAlert", &parse_client_alert },
171 { "ServerAlert", &parse_server_alert },
172 { "Protocol", &parse_protocol },
173 { "ClientVerifyCallback", &parse_client_verify_callback },
178 * Since these methods are used to create tests, we use OPENSSL_assert liberally
179 * for malloc failures and other internal errors.
181 SSL_TEST_CTX *SSL_TEST_CTX_new()
184 ret = OPENSSL_zalloc(sizeof(*ret));
185 OPENSSL_assert(ret != NULL);
189 void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
194 SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
196 STACK_OF(CONF_VALUE) *sk_conf;
201 sk_conf = NCONF_get_section(conf, test_section);
202 OPENSSL_assert(sk_conf != NULL);
204 ctx = SSL_TEST_CTX_new();
205 OPENSSL_assert(ctx != NULL);
207 for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
209 const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
210 for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
211 if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
212 if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
213 fprintf(stderr, "Bad value %s for option %s\n",
214 option->value, option->name);
222 fprintf(stderr, "Unknown test option: %s\n", option->name);
230 SSL_TEST_CTX_free(ctx);