Add some accessor API's
[openssl.git] / test / ssl_test_ctx.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <string.h>
11
12 #include <openssl/e_os2.h>
13 #include <openssl/crypto.h>
14
15 #include "e_os.h"
16 #include "ssl_test_ctx.h"
17
18 /* True enums and other test configuration values that map to an int. */
19 typedef struct {
20     const char *name;
21     int value;
22 } test_enum;
23
24
25 __owur static int parse_enum(const test_enum *enums, size_t num_enums,
26                              int *value, const char *name)
27 {
28     size_t i;
29     for (i = 0; i < num_enums; i++) {
30         if (strcmp(enums[i].name, name) == 0) {
31             *value = enums[i].value;
32             return 1;
33         }
34     }
35     return 0;
36 }
37
38 static const char *enum_name(const test_enum *enums, size_t num_enums,
39                              int value)
40 {
41     size_t i;
42     for (i = 0; i < num_enums; i++) {
43         if (enums[i].value == value) {
44             return enums[i].name;
45         }
46     }
47     return "InvalidValue";
48 }
49
50
51 /*******************/
52 /* ExpectedResult. */
53 /*******************/
54
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},
60 };
61
62 __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
63 {
64     int ret_value;
65     if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
66                     &ret_value, value)) {
67         return 0;
68     }
69     test_ctx->expected_result = ret_value;
70     return 1;
71 }
72
73 const char *ssl_test_result_name(ssl_test_result_t result)
74 {
75     return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
76 }
77
78 /******************************/
79 /* ClientAlert / ServerAlert. */
80 /******************************/
81
82 static const test_enum ssl_alerts[] = {
83     {"UnknownCA", SSL_AD_UNKNOWN_CA},
84     {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
85 };
86
87 __owur static int parse_alert(int *alert, const char *value)
88 {
89     return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
90 }
91
92 __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
93 {
94     return parse_alert(&test_ctx->client_alert, value);
95 }
96
97 __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
98 {
99     return parse_alert(&test_ctx->server_alert, value);
100 }
101
102 const char *ssl_alert_name(int alert)
103 {
104     return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
105 }
106
107 /************/
108 /* Protocol */
109 /************/
110
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},
116 };
117
118 __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
119 {
120     return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
121                       &test_ctx->protocol, value);
122 }
123
124 const char *ssl_protocol_name(int protocol)
125 {
126     return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
127 }
128
129 /***********************/
130 /* CertVerifyCallback. */
131 /***********************/
132
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},
137 };
138
139 __owur static int parse_client_verify_callback(SSL_TEST_CTX *test_ctx,
140                                               const char *value)
141 {
142     int ret_value;
143     if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
144                     &ret_value, value)) {
145         return 0;
146     }
147     test_ctx->client_verify_callback = ret_value;
148     return 1;
149 }
150
151 const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
152 {
153     return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
154                      callback);
155 }
156
157
158 /*************************************************************/
159 /* Known test options and their corresponding parse methods. */
160 /*************************************************************/
161
162 typedef struct {
163     const char *name;
164     int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
165 } ssl_test_ctx_option;
166
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 },
173 };
174
175
176 /*
177  * Since these methods are used to create tests, we use OPENSSL_assert liberally
178  * for malloc failures and other internal errors.
179  */
180 SSL_TEST_CTX *SSL_TEST_CTX_new()
181 {
182     SSL_TEST_CTX *ret;
183     ret = OPENSSL_zalloc(sizeof(*ret));
184     OPENSSL_assert(ret != NULL);
185     return ret;
186 }
187
188 void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
189 {
190     OPENSSL_free(ctx);
191 }
192
193 SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
194 {
195     STACK_OF(CONF_VALUE) *sk_conf;
196     SSL_TEST_CTX *ctx;
197     int i;
198     size_t j;
199
200     sk_conf = NCONF_get_section(conf, test_section);
201     OPENSSL_assert(sk_conf != NULL);
202
203     ctx = SSL_TEST_CTX_new();
204     OPENSSL_assert(ctx != NULL);
205
206     for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
207         int found = 0;
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);
214                     goto err;
215                 }
216                 found = 1;
217                 break;
218             }
219         }
220         if (!found) {
221             fprintf(stderr, "Unknown test option: %s\n", option->name);
222             goto err;
223         }
224     }
225
226     goto done;
227
228  err:
229     SSL_TEST_CTX_free(ctx);
230     ctx = NULL;
231  done:
232     return ctx;
233 }