2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/e_os2.h>
13 #include <openssl/crypto.h>
16 #include "ssl_test_ctx.h"
18 /* True enums and other test configuration values that map to an int. */
25 __owur static int parse_enum(const test_enum *enums, size_t num_enums,
26 int *value, const char *name)
29 for (i = 0; i < num_enums; i++) {
30 if (strcmp(enums[i].name, name) == 0) {
31 *value = enums[i].value;
38 static const char *enum_name(const test_enum *enums, size_t num_enums,
42 for (i = 0; i < num_enums; i++) {
43 if (enums[i].value == value) {
47 return "InvalidValue";
55 static const test_enum ssl_test_results[] = {
56 {"Success", SSL_TEST_SUCCESS},
57 {"ServerFail", SSL_TEST_SERVER_FAIL},
58 {"ClientFail", SSL_TEST_CLIENT_FAIL},
59 {"InternalError", SSL_TEST_INTERNAL_ERROR},
62 __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
65 if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
69 test_ctx->expected_result = ret_value;
73 const char *ssl_test_result_name(ssl_test_result_t result)
75 return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
78 /******************************/
79 /* ClientAlert / ServerAlert. */
80 /******************************/
82 static const test_enum ssl_alerts[] = {
83 {"UnknownCA", SSL_AD_UNKNOWN_CA},
84 {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
87 __owur static int parse_alert(int *alert, const char *value)
89 return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
92 __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
94 return parse_alert(&test_ctx->client_alert, value);
97 __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
99 return parse_alert(&test_ctx->server_alert, value);
102 const char *ssl_alert_name(int alert)
104 return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
111 static const test_enum ssl_protocols[] = {
112 {"TLSv1.2", TLS1_2_VERSION},
113 {"TLSv1.1", TLS1_1_VERSION},
114 {"TLSv1", TLS1_VERSION},
115 {"SSLv3", SSL3_VERSION},
118 __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
120 return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
121 &test_ctx->protocol, value);
124 const char *ssl_protocol_name(int protocol)
126 return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
129 /***********************/
130 /* CertVerifyCallback. */
131 /***********************/
133 static const test_enum ssl_verify_callbacks[] = {
134 {"None", SSL_TEST_VERIFY_NONE},
135 {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL},
136 {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL},
139 __owur static int parse_client_verify_callback(SSL_TEST_CTX *test_ctx,
143 if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
144 &ret_value, value)) {
147 test_ctx->client_verify_callback = ret_value;
151 const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
153 return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
158 /*************************************************************/
159 /* Known test options and their corresponding parse methods. */
160 /*************************************************************/
164 int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
165 } ssl_test_ctx_option;
167 static const ssl_test_ctx_option ssl_test_ctx_options[] = {
168 { "ExpectedResult", &parse_expected_result },
169 { "ClientAlert", &parse_client_alert },
170 { "ServerAlert", &parse_server_alert },
171 { "Protocol", &parse_protocol },
172 { "ClientVerifyCallback", &parse_client_verify_callback },
177 * Since these methods are used to create tests, we use OPENSSL_assert liberally
178 * for malloc failures and other internal errors.
180 SSL_TEST_CTX *SSL_TEST_CTX_new()
183 ret = OPENSSL_zalloc(sizeof(*ret));
184 OPENSSL_assert(ret != NULL);
188 void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
193 SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
195 STACK_OF(CONF_VALUE) *sk_conf;
200 sk_conf = NCONF_get_section(conf, test_section);
201 OPENSSL_assert(sk_conf != NULL);
203 ctx = SSL_TEST_CTX_new();
204 OPENSSL_assert(ctx != NULL);
206 for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
208 const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
209 for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
210 if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
211 if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
212 fprintf(stderr, "Bad value %s for option %s\n",
213 option->value, option->name);
221 fprintf(stderr, "Unknown test option: %s\n", option->name);
229 SSL_TEST_CTX_free(ctx);