]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/ssl_test.c
Fix a compression bug
[thirdparty/openssl.git] / test / ssl_test.c
CommitLineData
453dfd8d
EK
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
440e5d80
RS
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
453dfd8d 7 * https://www.openssl.org/source/license.html
453dfd8d
EK
8 */
9
10#include <stdio.h>
5c753de6 11#include <string.h>
453dfd8d
EK
12
13#include <openssl/conf.h>
14#include <openssl/err.h>
15#include <openssl/ssl.h>
16
17#include "handshake_helper.h"
18#include "ssl_test_ctx.h"
19#include "testutil.h"
e364c3b2 20#include "test_main_custom.h"
453dfd8d
EK
21
22static CONF *conf = NULL;
23
24/* Currently the section names are of the form test-<number>, e.g. test-15. */
25#define MAX_TESTCASE_NAME_LENGTH 100
26
453dfd8d
EK
27static const char *print_alert(int alert)
28{
29 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
30}
31
ce2cdac2 32static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
453dfd8d 33{
ce2cdac2 34 if (result->result != test_ctx->expected_result) {
453dfd8d 35 fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
a263f320 36 ssl_test_result_name(test_ctx->expected_result),
ce2cdac2 37 ssl_test_result_name(result->result));
453dfd8d
EK
38 return 0;
39 }
40 return 1;
41}
42
ce2cdac2 43static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
453dfd8d 44{
ce2cdac2 45 if (result->client_alert_sent != result->client_alert_received) {
453dfd8d 46 fprintf(stderr, "Client sent alert %s but server received %s\n.",
ce2cdac2
EK
47 print_alert(result->client_alert_sent),
48 print_alert(result->client_alert_received));
453dfd8d
EK
49 /*
50 * We can't bail here because the peer doesn't always get far enough
51 * to process a received alert. Specifically, in protocol version
52 * negotiation tests, we have the following scenario.
53 * Client supports TLS v1.2 only; Server supports TLS v1.1.
54 * Client proposes TLS v1.2; server responds with 1.1;
55 * Client now sends a protocol alert, using TLS v1.2 in the header.
56 * The server, however, rejects the alert because of version mismatch
57 * in the record layer; therefore, the server appears to never
58 * receive the alert.
59 */
60 /* return 0; */
61 }
62
ce2cdac2 63 if (result->server_alert_sent != result->server_alert_received) {
453dfd8d 64 fprintf(stderr, "Server sent alert %s but client received %s\n.",
ce2cdac2
EK
65 print_alert(result->server_alert_sent),
66 print_alert(result->server_alert_received));
453dfd8d
EK
67 /* return 0; */
68 }
69
70 /* Tolerate an alert if one wasn't explicitly specified in the test. */
9f48bbac 71 if (test_ctx->expected_client_alert
453dfd8d
EK
72 /*
73 * The info callback alert value is computed as
74 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
75 * where the low byte is the alert code and the high byte is other stuff.
76 */
9f48bbac 77 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
453dfd8d 78 fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
9f48bbac 79 print_alert(test_ctx->expected_client_alert),
ce2cdac2 80 print_alert(result->client_alert_sent));
453dfd8d
EK
81 return 0;
82 }
83
9f48bbac
EK
84 if (test_ctx->expected_server_alert
85 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
453dfd8d 86 fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
9f48bbac 87 print_alert(test_ctx->expected_server_alert),
ce2cdac2 88 print_alert(result->server_alert_sent));
453dfd8d
EK
89 return 0;
90 }
91
dd8e5a57
EK
92 if (result->client_num_fatal_alerts_sent > 1) {
93 fprintf(stderr, "Client sent %d fatal alerts.\n",
94 result->client_num_fatal_alerts_sent);
95 return 0;
96 }
97 if (result->server_num_fatal_alerts_sent > 1) {
98 fprintf(stderr, "Server sent %d alerts.\n",
99 result->server_num_fatal_alerts_sent);
100 return 0;
101 }
453dfd8d
EK
102 return 1;
103}
104
ce2cdac2 105static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
453dfd8d 106{
ce2cdac2 107 if (result->client_protocol != result->server_protocol) {
453dfd8d 108 fprintf(stderr, "Client has protocol %s but server has %s\n.",
ce2cdac2
EK
109 ssl_protocol_name(result->client_protocol),
110 ssl_protocol_name(result->server_protocol));
453dfd8d
EK
111 return 0;
112 }
113
9f48bbac
EK
114 if (test_ctx->expected_protocol) {
115 if (result->client_protocol != test_ctx->expected_protocol) {
453dfd8d 116 fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
9f48bbac 117 ssl_protocol_name(test_ctx->expected_protocol),
ce2cdac2 118 ssl_protocol_name(result->client_protocol));
453dfd8d
EK
119 return 0;
120 }
121 }
122 return 1;
123}
124
ce2cdac2 125static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
5c753de6 126{
ce2cdac2 127 if (result->servername != test_ctx->expected_servername) {
d2b23cd2
EK
128 fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
129 ssl_servername_name(test_ctx->expected_servername),
ce2cdac2 130 ssl_servername_name(result->servername));
d2b23cd2 131 return 0;
5c753de6 132 }
d2b23cd2 133 return 1;
5c753de6
TS
134}
135
ce2cdac2 136static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
5c753de6
TS
137{
138 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
139 return 1;
ce2cdac2 140 if (result->session_ticket != test_ctx->session_ticket_expected) {
5c753de6 141 fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
81fc33c9 142 ssl_session_ticket_name(test_ctx->session_ticket_expected),
ce2cdac2 143 ssl_session_ticket_name(result->session_ticket));
5c753de6
TS
144 return 0;
145 }
146 return 1;
147}
148
620c6ad3 149#ifndef OPENSSL_NO_NEXTPROTONEG
ce2cdac2
EK
150static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
151{
152 int ret = 1;
153 ret &= strings_equal("NPN Negotiated (client vs server)",
154 result->client_npn_negotiated,
155 result->server_npn_negotiated);
156 ret &= strings_equal("ExpectedNPNProtocol",
157 test_ctx->expected_npn_protocol,
158 result->client_npn_negotiated);
159 return ret;
160}
7b7cea6d 161#endif
ce2cdac2
EK
162
163static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
164{
165 int ret = 1;
166 ret &= strings_equal("ALPN Negotiated (client vs server)",
167 result->client_alpn_negotiated,
168 result->server_alpn_negotiated);
169 ret &= strings_equal("ExpectedALPNProtocol",
170 test_ctx->expected_alpn_protocol,
171 result->client_alpn_negotiated);
172 return ret;
173}
174
590ed3d7
EK
175static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
176{
177 if (result->client_resumed != result->server_resumed) {
178 fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
179 result->client_resumed, result->server_resumed);
180 return 0;
181 }
182 if (result->client_resumed != test_ctx->resumption_expected) {
183 fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
184 test_ctx->resumption_expected, result->client_resumed);
185 return 0;
186 }
187 return 1;
188}
189
ee5b6a42 190static int check_nid(const char *name, int expected_nid, int nid)
b93ad05d 191{
ee5b6a42 192 if (expected_nid == 0 || expected_nid == nid)
b93ad05d 193 return 1;
7f5f35af 194 fprintf(stderr, "%s type mismatch, %s vs %s\n",
ee5b6a42
DSH
195 name, OBJ_nid2ln(expected_nid),
196 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
b93ad05d
DSH
197 return 0;
198}
199
7f5f35af
DSH
200static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
201{
ee5b6a42
DSH
202 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
203 result->tmp_key_type);
7f5f35af
DSH
204}
205
206static int check_server_cert_type(HANDSHAKE_RESULT *result,
207 SSL_TEST_CTX *test_ctx)
208{
ee5b6a42
DSH
209 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
210 result->server_cert_type);
211}
212
213static int check_server_sign_hash(HANDSHAKE_RESULT *result,
214 SSL_TEST_CTX *test_ctx)
215{
216 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
217 result->server_sign_hash);
7f5f35af
DSH
218}
219
54b7f2a5
DSH
220static int check_server_sign_type(HANDSHAKE_RESULT *result,
221 SSL_TEST_CTX *test_ctx)
222{
223 return check_nid("Server signing", test_ctx->expected_server_sign_type,
224 result->server_sign_type);
225}
226
7f5f35af
DSH
227static int check_client_cert_type(HANDSHAKE_RESULT *result,
228 SSL_TEST_CTX *test_ctx)
229{
ee5b6a42
DSH
230 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
231 result->client_cert_type);
232}
233
234static int check_client_sign_hash(HANDSHAKE_RESULT *result,
235 SSL_TEST_CTX *test_ctx)
236{
237 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
238 result->client_sign_hash);
7f5f35af
DSH
239}
240
54b7f2a5
DSH
241static int check_client_sign_type(HANDSHAKE_RESULT *result,
242 SSL_TEST_CTX *test_ctx)
243{
244 return check_nid("Client signing", test_ctx->expected_client_sign_type,
245 result->client_sign_type);
246}
247
453dfd8d
EK
248/*
249 * This could be further simplified by constructing an expected
250 * HANDSHAKE_RESULT, and implementing comparison methods for
251 * its fields.
252 */
ce2cdac2 253static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
453dfd8d
EK
254{
255 int ret = 1;
256 ret &= check_result(result, test_ctx);
257 ret &= check_alerts(result, test_ctx);
ce2cdac2 258 if (result->result == SSL_TEST_SUCCESS) {
453dfd8d 259 ret &= check_protocol(result, test_ctx);
5c753de6 260 ret &= check_servername(result, test_ctx);
81fc33c9 261 ret &= check_session_ticket(result, test_ctx);
ce2cdac2 262 ret &= (result->session_ticket_do_not_call == 0);
620c6ad3 263#ifndef OPENSSL_NO_NEXTPROTONEG
ce2cdac2 264 ret &= check_npn(result, test_ctx);
620c6ad3 265#endif
7b7cea6d 266 ret &= check_alpn(result, test_ctx);
590ed3d7 267 ret &= check_resumption(result, test_ctx);
b93ad05d 268 ret &= check_tmp_key(result, test_ctx);
7f5f35af 269 ret &= check_server_cert_type(result, test_ctx);
ee5b6a42 270 ret &= check_server_sign_hash(result, test_ctx);
54b7f2a5 271 ret &= check_server_sign_type(result, test_ctx);
7f5f35af 272 ret &= check_client_cert_type(result, test_ctx);
ee5b6a42 273 ret &= check_client_sign_hash(result, test_ctx);
54b7f2a5 274 ret &= check_client_sign_type(result, test_ctx);
5c753de6 275 }
453dfd8d
EK
276 return ret;
277}
278
d836d71b 279static int test_handshake(int idx)
453dfd8d 280{
ababe86b 281 int ret = 0;
590ed3d7 282 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
11279b13 283 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
453dfd8d 284 SSL_TEST_CTX *test_ctx = NULL;
ce2cdac2 285 HANDSHAKE_RESULT *result = NULL;
d836d71b
EK
286 char test_app[MAX_TESTCASE_NAME_LENGTH];
287
288 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
453dfd8d 289
d836d71b 290 test_ctx = SSL_TEST_CTX_create(conf, test_app);
74726750
EK
291 if (test_ctx == NULL)
292 goto err;
293
294#ifndef OPENSSL_NO_DTLS
295 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
296 server_ctx = SSL_CTX_new(DTLS_server_method());
9f48bbac
EK
297 if (test_ctx->extra.server.servername_callback !=
298 SSL_TEST_SERVERNAME_CB_NONE) {
d2b23cd2 299 server2_ctx = SSL_CTX_new(DTLS_server_method());
d61f0078 300 TEST_check(server2_ctx != NULL);
d2b23cd2 301 }
74726750 302 client_ctx = SSL_CTX_new(DTLS_client_method());
590ed3d7
EK
303 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
304 resume_server_ctx = SSL_CTX_new(DTLS_server_method());
11279b13 305 resume_client_ctx = SSL_CTX_new(DTLS_client_method());
d61f0078
EK
306 TEST_check(resume_server_ctx != NULL);
307 TEST_check(resume_client_ctx != NULL);
590ed3d7 308 }
74726750
EK
309 }
310#endif
311 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
312 server_ctx = SSL_CTX_new(TLS_server_method());
9f48bbac
EK
313 /* SNI on resumption isn't supported/tested yet. */
314 if (test_ctx->extra.server.servername_callback !=
315 SSL_TEST_SERVERNAME_CB_NONE) {
d2b23cd2 316 server2_ctx = SSL_CTX_new(TLS_server_method());
d61f0078 317 TEST_check(server2_ctx != NULL);
d2b23cd2 318 }
74726750 319 client_ctx = SSL_CTX_new(TLS_client_method());
590ed3d7
EK
320
321 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
322 resume_server_ctx = SSL_CTX_new(TLS_server_method());
11279b13 323 resume_client_ctx = SSL_CTX_new(TLS_client_method());
d61f0078
EK
324 TEST_check(resume_server_ctx != NULL);
325 TEST_check(resume_client_ctx != NULL);
590ed3d7 326 }
74726750
EK
327 }
328
d61f0078
EK
329 TEST_check(server_ctx != NULL);
330 TEST_check(client_ctx != NULL);
453dfd8d 331
d836d71b 332 TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
453dfd8d
EK
333
334 if (!SSL_CTX_config(server_ctx, "server")
5c753de6 335 || !SSL_CTX_config(client_ctx, "client")) {
453dfd8d
EK
336 goto err;
337 }
338
d2b23cd2
EK
339 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
340 goto err;
590ed3d7
EK
341 if (resume_server_ctx != NULL
342 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
343 goto err;
11279b13
EK
344 if (resume_client_ctx != NULL
345 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
346 goto err;
d2b23cd2 347
590ed3d7 348 result = do_handshake(server_ctx, server2_ctx, client_ctx,
11279b13 349 resume_server_ctx, resume_client_ctx, test_ctx);
453dfd8d 350
ababe86b 351 ret = check_test(result, test_ctx);
453dfd8d
EK
352
353err:
354 CONF_modules_unload(0);
355 SSL_CTX_free(server_ctx);
5c753de6 356 SSL_CTX_free(server2_ctx);
453dfd8d 357 SSL_CTX_free(client_ctx);
590ed3d7 358 SSL_CTX_free(resume_server_ctx);
11279b13 359 SSL_CTX_free(resume_client_ctx);
453dfd8d 360 SSL_TEST_CTX_free(test_ctx);
ce2cdac2 361 HANDSHAKE_RESULT_free(result);
453dfd8d
EK
362 return ret;
363}
364
e364c3b2 365int test_main(int argc, char **argv)
453dfd8d
EK
366{
367 int result = 0;
368 long num_tests;
369
370 if (argc != 2)
371 return 1;
372
373 conf = NCONF_new(NULL);
d61f0078 374 TEST_check(conf != NULL);
453dfd8d
EK
375
376 /* argv[1] should point to the test conf file */
d61f0078 377 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
453dfd8d 378
d61f0078 379 TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
453dfd8d
EK
380
381 ADD_ALL_TESTS(test_handshake, (int)(num_tests));
382 result = run_tests(argv[0]);
383
e364c3b2 384 NCONF_free(conf);
453dfd8d
EK
385 return result;
386}