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
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
17 #include "handshake_helper.h"
18 #include "ssl_test_ctx.h"
21 static CONF
*conf
= NULL
;
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
26 typedef struct ssl_test_ctx_test_fixture
{
27 const char *test_case_name
;
28 char test_app
[MAX_TESTCASE_NAME_LENGTH
];
31 static SSL_TEST_FIXTURE
set_up(const char *const test_case_name
)
33 SSL_TEST_FIXTURE fixture
;
34 fixture
.test_case_name
= test_case_name
;
38 static const char *print_alert(int alert
)
40 return alert
? SSL_alert_desc_string_long(alert
) : "no alert";
43 static int check_result(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
45 if (result
->result
!= test_ctx
->expected_result
) {
46 fprintf(stderr
, "ExpectedResult mismatch: expected %s, got %s.\n",
47 ssl_test_result_name(test_ctx
->expected_result
),
48 ssl_test_result_name(result
->result
));
54 static int check_alerts(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
56 if (result
->client_alert_sent
!= result
->client_alert_received
) {
57 fprintf(stderr
, "Client sent alert %s but server received %s\n.",
58 print_alert(result
->client_alert_sent
),
59 print_alert(result
->client_alert_received
));
61 * We can't bail here because the peer doesn't always get far enough
62 * to process a received alert. Specifically, in protocol version
63 * negotiation tests, we have the following scenario.
64 * Client supports TLS v1.2 only; Server supports TLS v1.1.
65 * Client proposes TLS v1.2; server responds with 1.1;
66 * Client now sends a protocol alert, using TLS v1.2 in the header.
67 * The server, however, rejects the alert because of version mismatch
68 * in the record layer; therefore, the server appears to never
74 if (result
->server_alert_sent
!= result
->server_alert_received
) {
75 fprintf(stderr
, "Server sent alert %s but client received %s\n.",
76 print_alert(result
->server_alert_sent
),
77 print_alert(result
->server_alert_received
));
81 /* Tolerate an alert if one wasn't explicitly specified in the test. */
82 if (test_ctx
->expected_client_alert
84 * The info callback alert value is computed as
85 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
86 * where the low byte is the alert code and the high byte is other stuff.
88 && (result
->client_alert_sent
& 0xff) != test_ctx
->expected_client_alert
) {
89 fprintf(stderr
, "ClientAlert mismatch: expected %s, got %s.\n",
90 print_alert(test_ctx
->expected_client_alert
),
91 print_alert(result
->client_alert_sent
));
95 if (test_ctx
->expected_server_alert
96 && (result
->server_alert_sent
& 0xff) != test_ctx
->expected_server_alert
) {
97 fprintf(stderr
, "ServerAlert mismatch: expected %s, got %s.\n",
98 print_alert(test_ctx
->expected_server_alert
),
99 print_alert(result
->server_alert_sent
));
106 static int check_protocol(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
108 if (result
->client_protocol
!= result
->server_protocol
) {
109 fprintf(stderr
, "Client has protocol %s but server has %s\n.",
110 ssl_protocol_name(result
->client_protocol
),
111 ssl_protocol_name(result
->server_protocol
));
115 if (test_ctx
->expected_protocol
) {
116 if (result
->client_protocol
!= test_ctx
->expected_protocol
) {
117 fprintf(stderr
, "Protocol mismatch: expected %s, got %s.\n",
118 ssl_protocol_name(test_ctx
->expected_protocol
),
119 ssl_protocol_name(result
->client_protocol
));
126 static int check_servername(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
128 if (result
->servername
!= test_ctx
->expected_servername
) {
129 fprintf(stderr
, "Client ServerName mismatch, expected %s, got %s\n.",
130 ssl_servername_name(test_ctx
->expected_servername
),
131 ssl_servername_name(result
->servername
));
137 static int check_session_ticket(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
139 if (test_ctx
->session_ticket_expected
== SSL_TEST_SESSION_TICKET_IGNORE
)
141 if (result
->session_ticket
!= test_ctx
->session_ticket_expected
) {
142 fprintf(stderr
, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
143 ssl_session_ticket_name(test_ctx
->session_ticket_expected
),
144 ssl_session_ticket_name(result
->session_ticket
));
150 #ifndef OPENSSL_NO_NEXTPROTONEG
151 static int check_npn(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
154 ret
&= strings_equal("NPN Negotiated (client vs server)",
155 result
->client_npn_negotiated
,
156 result
->server_npn_negotiated
);
157 ret
&= strings_equal("ExpectedNPNProtocol",
158 test_ctx
->expected_npn_protocol
,
159 result
->client_npn_negotiated
);
164 static int check_alpn(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
167 ret
&= strings_equal("ALPN Negotiated (client vs server)",
168 result
->client_alpn_negotiated
,
169 result
->server_alpn_negotiated
);
170 ret
&= strings_equal("ExpectedALPNProtocol",
171 test_ctx
->expected_alpn_protocol
,
172 result
->client_alpn_negotiated
);
176 static int check_resumption(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
178 if (result
->client_resumed
!= result
->server_resumed
) {
179 fprintf(stderr
, "Resumption mismatch (client vs server): %d vs %d\n",
180 result
->client_resumed
, result
->server_resumed
);
183 if (result
->client_resumed
!= test_ctx
->resumption_expected
) {
184 fprintf(stderr
, "ResumptionExpected mismatch: %d vs %d\n",
185 test_ctx
->resumption_expected
, result
->client_resumed
);
192 * This could be further simplified by constructing an expected
193 * HANDSHAKE_RESULT, and implementing comparison methods for
196 static int check_test(HANDSHAKE_RESULT
*result
, SSL_TEST_CTX
*test_ctx
)
199 ret
&= check_result(result
, test_ctx
);
200 ret
&= check_alerts(result
, test_ctx
);
201 if (result
->result
== SSL_TEST_SUCCESS
) {
202 ret
&= check_protocol(result
, test_ctx
);
203 ret
&= check_servername(result
, test_ctx
);
204 ret
&= check_session_ticket(result
, test_ctx
);
205 ret
&= (result
->session_ticket_do_not_call
== 0);
206 #ifndef OPENSSL_NO_NEXTPROTONEG
207 ret
&= check_npn(result
, test_ctx
);
209 ret
&= check_alpn(result
, test_ctx
);
210 ret
&= check_resumption(result
, test_ctx
);
215 static int execute_test(SSL_TEST_FIXTURE fixture
)
218 SSL_CTX
*server_ctx
= NULL
, *server2_ctx
= NULL
, *client_ctx
= NULL
,
219 *resume_server_ctx
= NULL
, *resume_client_ctx
= NULL
;
220 SSL_TEST_CTX
*test_ctx
= NULL
;
221 HANDSHAKE_RESULT
*result
= NULL
;
223 test_ctx
= SSL_TEST_CTX_create(conf
, fixture
.test_app
);
224 if (test_ctx
== NULL
)
227 #ifndef OPENSSL_NO_DTLS
228 if (test_ctx
->method
== SSL_TEST_METHOD_DTLS
) {
229 server_ctx
= SSL_CTX_new(DTLS_server_method());
230 if (test_ctx
->extra
.server
.servername_callback
!=
231 SSL_TEST_SERVERNAME_CB_NONE
) {
232 server2_ctx
= SSL_CTX_new(DTLS_server_method());
233 OPENSSL_assert(server2_ctx
!= NULL
);
235 client_ctx
= SSL_CTX_new(DTLS_client_method());
236 if (test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RESUME
) {
237 resume_server_ctx
= SSL_CTX_new(DTLS_server_method());
238 resume_client_ctx
= SSL_CTX_new(DTLS_client_method());
239 OPENSSL_assert(resume_server_ctx
!= NULL
);
240 OPENSSL_assert(resume_client_ctx
!= NULL
);
244 if (test_ctx
->method
== SSL_TEST_METHOD_TLS
) {
245 server_ctx
= SSL_CTX_new(TLS_server_method());
246 /* SNI on resumption isn't supported/tested yet. */
247 if (test_ctx
->extra
.server
.servername_callback
!=
248 SSL_TEST_SERVERNAME_CB_NONE
) {
249 server2_ctx
= SSL_CTX_new(TLS_server_method());
250 OPENSSL_assert(server2_ctx
!= NULL
);
252 client_ctx
= SSL_CTX_new(TLS_client_method());
254 if (test_ctx
->handshake_mode
== SSL_TEST_HANDSHAKE_RESUME
) {
255 resume_server_ctx
= SSL_CTX_new(TLS_server_method());
256 resume_client_ctx
= SSL_CTX_new(TLS_client_method());
257 OPENSSL_assert(resume_server_ctx
!= NULL
);
258 OPENSSL_assert(resume_client_ctx
!= NULL
);
262 OPENSSL_assert(server_ctx
!= NULL
);
263 OPENSSL_assert(client_ctx
!= NULL
);
265 OPENSSL_assert(CONF_modules_load(conf
, fixture
.test_app
, 0) > 0);
267 if (!SSL_CTX_config(server_ctx
, "server")
268 || !SSL_CTX_config(client_ctx
, "client")) {
272 if (server2_ctx
!= NULL
&& !SSL_CTX_config(server2_ctx
, "server2"))
274 if (resume_server_ctx
!= NULL
275 && !SSL_CTX_config(resume_server_ctx
, "resume-server"))
277 if (resume_client_ctx
!= NULL
278 && !SSL_CTX_config(resume_client_ctx
, "resume-client"))
281 result
= do_handshake(server_ctx
, server2_ctx
, client_ctx
,
282 resume_server_ctx
, resume_client_ctx
, test_ctx
);
284 ret
= check_test(result
, test_ctx
);
287 CONF_modules_unload(0);
288 SSL_CTX_free(server_ctx
);
289 SSL_CTX_free(server2_ctx
);
290 SSL_CTX_free(client_ctx
);
291 SSL_CTX_free(resume_server_ctx
);
292 SSL_CTX_free(resume_client_ctx
);
293 SSL_TEST_CTX_free(test_ctx
);
295 ERR_print_errors_fp(stderr
);
296 HANDSHAKE_RESULT_free(result
);
300 static void tear_down(SSL_TEST_FIXTURE fixture
)
304 #define SETUP_SSL_TEST_FIXTURE() \
305 SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
306 #define EXECUTE_SSL_TEST() \
307 EXECUTE_TEST(execute_test, tear_down)
309 static int test_handshake(int idx
)
311 SETUP_SSL_TEST_FIXTURE();
312 BIO_snprintf(fixture
.test_app
, sizeof(fixture
.test_app
),
317 int main(int argc
, char **argv
)
325 conf
= NCONF_new(NULL
);
326 OPENSSL_assert(conf
!= NULL
);
328 /* argv[1] should point to the test conf file */
329 OPENSSL_assert(NCONF_load(conf
, argv
[1], NULL
) > 0);
331 OPENSSL_assert(NCONF_get_number_e(conf
, NULL
, "num_tests", &num_tests
));
333 ADD_ALL_TESTS(test_handshake
, (int)(num_tests
));
334 result
= run_tests(argv
[0]);