Cleanup openssl.ec
[openssl.git] / test / ssl_test.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 <stdio.h>
11
12 #include <openssl/conf.h>
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
15
16 #include "handshake_helper.h"
17 #include "ssl_test_ctx.h"
18 #include "testutil.h"
19
20 static CONF *conf = NULL;
21
22 /* Currently the section names are of the form test-<number>, e.g. test-15. */
23 #define MAX_TESTCASE_NAME_LENGTH 100
24
25 typedef struct ssl_test_ctx_test_fixture {
26     const char *test_case_name;
27     char test_app[MAX_TESTCASE_NAME_LENGTH];
28 } SSL_TEST_FIXTURE;
29
30 static SSL_TEST_FIXTURE set_up(const char *const test_case_name)
31 {
32     SSL_TEST_FIXTURE fixture;
33     fixture.test_case_name = test_case_name;
34     return fixture;
35 }
36
37 static const char *print_alert(int alert)
38 {
39     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
40 }
41
42 static int check_result(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
43 {
44     if (result.result != test_ctx->expected_result) {
45         fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
46                 ssl_test_result_name(test_ctx->expected_result),
47                 ssl_test_result_name(result.result));
48         return 0;
49     }
50     return 1;
51 }
52
53 static int check_alerts(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
54 {
55     if (result.client_alert_sent != result.client_alert_received) {
56         fprintf(stderr, "Client sent alert %s but server received %s\n.",
57                 print_alert(result.client_alert_sent),
58                 print_alert(result.client_alert_received));
59         /*
60          * We can't bail here because the peer doesn't always get far enough
61          * to process a received alert. Specifically, in protocol version
62          * negotiation tests, we have the following scenario.
63          * Client supports TLS v1.2 only; Server supports TLS v1.1.
64          * Client proposes TLS v1.2; server responds with 1.1;
65          * Client now sends a protocol alert, using TLS v1.2 in the header.
66          * The server, however, rejects the alert because of version mismatch
67          * in the record layer; therefore, the server appears to never
68          * receive the alert.
69          */
70         /* return 0; */
71     }
72
73     if (result.server_alert_sent != result.server_alert_received) {
74         fprintf(stderr, "Server sent alert %s but client received %s\n.",
75                 print_alert(result.server_alert_sent),
76                 print_alert(result.server_alert_received));
77         /* return 0; */
78     }
79
80     /* Tolerate an alert if one wasn't explicitly specified in the test. */
81     if (test_ctx->client_alert
82         /*
83          * The info callback alert value is computed as
84          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
85          * where the low byte is the alert code and the high byte is other stuff.
86          */
87         && (result.client_alert_sent & 0xff) != test_ctx->client_alert) {
88         fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
89                 print_alert(test_ctx->client_alert),
90                 print_alert(result.client_alert_sent));
91         return 0;
92     }
93
94     if (test_ctx->server_alert
95         && (result.server_alert_sent & 0xff) != test_ctx->server_alert) {
96         fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
97                 print_alert(test_ctx->server_alert),
98                 print_alert(result.server_alert_sent));
99         return 0;
100     }
101
102     return 1;
103 }
104
105 static int check_protocol(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
106 {
107     if (result.client_protocol != result.server_protocol) {
108         fprintf(stderr, "Client has protocol %s but server has %s\n.",
109                 ssl_protocol_name(result.client_protocol),
110                 ssl_protocol_name(result.server_protocol));
111         return 0;
112     }
113
114     if (test_ctx->protocol) {
115         if (result.client_protocol != test_ctx->protocol) {
116             fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
117                     ssl_protocol_name(test_ctx->protocol),
118                     ssl_protocol_name(result.client_protocol));
119             return 0;
120         }
121     }
122     return 1;
123 }
124
125 /*
126  * This could be further simplified by constructing an expected
127  * HANDSHAKE_RESULT, and implementing comparison methods for
128  * its fields.
129  */
130 static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
131 {
132     int ret = 1;
133     ret &= check_result(result, test_ctx);
134     ret &= check_alerts(result, test_ctx);
135     if (result.result == SSL_TEST_SUCCESS)
136         ret &= check_protocol(result, test_ctx);
137     return ret;
138 }
139
140 static int execute_test(SSL_TEST_FIXTURE fixture)
141 {
142     int ret = 0;
143     SSL_CTX *server_ctx = NULL, *client_ctx = NULL;
144     SSL_TEST_CTX *test_ctx = NULL;
145     HANDSHAKE_RESULT result;
146
147     server_ctx = SSL_CTX_new(TLS_server_method());
148     client_ctx = SSL_CTX_new(TLS_client_method());
149     OPENSSL_assert(server_ctx != NULL && client_ctx != NULL);
150
151     OPENSSL_assert(CONF_modules_load(conf, fixture.test_app, 0) > 0);
152
153     if (!SSL_CTX_config(server_ctx, "server")
154        || !SSL_CTX_config(client_ctx, "client")) {
155         goto err;
156     }
157
158     test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
159     if (test_ctx == NULL)
160         goto err;
161
162     result = do_handshake(server_ctx, client_ctx, test_ctx);
163
164     ret = check_test(result, test_ctx);
165
166 err:
167     CONF_modules_unload(0);
168     SSL_CTX_free(server_ctx);
169     SSL_CTX_free(client_ctx);
170     SSL_TEST_CTX_free(test_ctx);
171     if (ret != 1)
172         ERR_print_errors_fp(stderr);
173     return ret;
174 }
175
176 static void tear_down(SSL_TEST_FIXTURE fixture)
177 {
178 }
179
180 #define SETUP_SSL_TEST_FIXTURE()                        \
181     SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
182 #define EXECUTE_SSL_TEST()             \
183     EXECUTE_TEST(execute_test, tear_down)
184
185 static int test_handshake(int idx)
186 {
187     SETUP_SSL_TEST_FIXTURE();
188     BIO_snprintf(fixture.test_app, sizeof(fixture.test_app),
189                  "test-%d", idx);
190     EXECUTE_SSL_TEST();
191 }
192
193 int main(int argc, char **argv)
194 {
195     int result = 0;
196     long num_tests;
197
198     if (argc != 2)
199         return 1;
200
201     conf = NCONF_new(NULL);
202     OPENSSL_assert(conf != NULL);
203
204     /* argv[1] should point to the test conf file */
205     OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
206
207     OPENSSL_assert(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
208
209     ADD_ALL_TESTS(test_handshake, (int)(num_tests));
210     result = run_tests(argv[0]);
211
212     return result;
213 }