Modify expected output of a certificate to match the changed printout
[openssl.git] / test / ssl_test.c
1 /*
2  * Copyright 2016-2017 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 #include <string.h>
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"
20
21 static CONF *conf = NULL;
22
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
25
26 static const char *print_alert(int alert)
27 {
28     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
29 }
30
31 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
32 {
33     if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
34         TEST_info("ExpectedResult mismatch: expected %s, got %s.",
35                   ssl_test_result_name(test_ctx->expected_result),
36                   ssl_test_result_name(result->result));
37         return 0;
38     }
39     return 1;
40 }
41
42 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
43 {
44     if (!TEST_int_eq(result->client_alert_sent,
45                      result->client_alert_received)) {
46         TEST_info("Client sent alert %s but server received %s.",
47                   print_alert(result->client_alert_sent),
48                   print_alert(result->client_alert_received));
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
63     if (!TEST_int_eq(result->server_alert_sent,
64                      result->server_alert_received)) {
65         TEST_info("Server sent alert %s but client received %s.",
66                   print_alert(result->server_alert_sent),
67                   print_alert(result->server_alert_received));
68         /* return 0; */
69     }
70
71     /* Tolerate an alert if one wasn't explicitly specified in the test. */
72     if (test_ctx->expected_client_alert
73         /*
74          * The info callback alert value is computed as
75          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
76          * where the low byte is the alert code and the high byte is other stuff.
77          */
78         && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
79         TEST_error("ClientAlert mismatch: expected %s, got %s.",
80                    print_alert(test_ctx->expected_client_alert),
81                    print_alert(result->client_alert_sent));
82         return 0;
83     }
84
85     if (test_ctx->expected_server_alert
86         && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
87         TEST_error("ServerAlert mismatch: expected %s, got %s.",
88                    print_alert(test_ctx->expected_server_alert),
89                    print_alert(result->server_alert_sent));
90         return 0;
91     }
92
93     if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
94         return 0;
95     if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
96         return 0;
97     return 1;
98 }
99
100 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
101 {
102     if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
103         TEST_info("Client has protocol %s but server has %s.",
104                   ssl_protocol_name(result->client_protocol),
105                   ssl_protocol_name(result->server_protocol));
106         return 0;
107     }
108
109     if (test_ctx->expected_protocol) {
110         if (!TEST_int_eq(result->client_protocol,
111                          test_ctx->expected_protocol)) {
112             TEST_info("Protocol mismatch: expected %s, got %s.\n",
113                       ssl_protocol_name(test_ctx->expected_protocol),
114                       ssl_protocol_name(result->client_protocol));
115             return 0;
116         }
117     }
118     return 1;
119 }
120
121 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
122 {
123     if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
124       TEST_info("Client ServerName mismatch, expected %s, got %s.",
125                 ssl_servername_name(test_ctx->expected_servername),
126                 ssl_servername_name(result->servername));
127       return 0;
128     }
129   return 1;
130 }
131
132 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
133 {
134     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
135         return 1;
136     if (!TEST_int_eq(result->session_ticket,
137                      test_ctx->session_ticket_expected)) {
138         TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
139                   ssl_session_ticket_name(test_ctx->session_ticket_expected),
140                   ssl_session_ticket_name(result->session_ticket));
141         return 0;
142     }
143     return 1;
144 }
145
146 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
147 {
148     if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
149         return 1;
150     if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
151         TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
152                 ssl_session_id_name(test_ctx->session_id_expected),
153                 ssl_session_id_name(result->session_id));
154         return 0;
155     }
156     return 1;
157 }
158
159 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
160 {
161     if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
162         return 0;
163     return 1;
164 }
165 #ifndef OPENSSL_NO_NEXTPROTONEG
166 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
167 {
168     int ret = 1;
169     if (!TEST_str_eq(result->client_npn_negotiated,
170                      result->server_npn_negotiated))
171         ret = 0;
172     if (!TEST_str_eq(test_ctx->expected_npn_protocol,
173                      result->client_npn_negotiated))
174         ret = 0;
175     return ret;
176 }
177 #endif
178
179 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
180 {
181     int ret = 1;
182     if (!TEST_str_eq(result->client_alpn_negotiated,
183                      result->server_alpn_negotiated))
184         ret = 0;
185     if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
186                      result->client_alpn_negotiated))
187         ret = 0;
188     return ret;
189 }
190
191 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
192 {
193     if (!TEST_int_eq(result->client_resumed, result->server_resumed))
194         return 0;
195     if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
196         return 0;
197     return 1;
198 }
199
200 static int check_nid(const char *name, int expected_nid, int nid)
201 {
202     if (expected_nid == 0 || expected_nid == nid)
203         return 1;
204     TEST_error("%s type mismatch, %s vs %s\n",
205                name, OBJ_nid2ln(expected_nid),
206                nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
207     return 0;
208 }
209
210 static void print_ca_names(STACK_OF(X509_NAME) *names)
211 {
212     int i;
213
214     if (names == NULL || sk_X509_NAME_num(names) == 0) {
215         TEST_note("    <empty>");
216         return;
217     }
218     for (i = 0; i < sk_X509_NAME_num(names); i++) {
219         X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
220                            XN_FLAG_ONELINE);
221         BIO_puts(bio_err, "\n");
222     }
223 }
224
225 static int check_ca_names(const char *name,
226                           STACK_OF(X509_NAME) *expected_names,
227                           STACK_OF(X509_NAME) *names)
228 {
229     int i;
230
231     if (expected_names == NULL)
232         return 1;
233     if (names == NULL || sk_X509_NAME_num(names) == 0) {
234         if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
235             return 1;
236         goto err;
237     }
238     if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
239         goto err;
240     for (i = 0; i < sk_X509_NAME_num(names); i++) {
241         if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
242                                        sk_X509_NAME_value(expected_names, i)),
243                          0)) {
244             goto err;
245         }
246     }
247     return 1;
248 err:
249     TEST_info("%s: list mismatch", name);
250     TEST_note("Expected Names:");
251     print_ca_names(expected_names);
252     TEST_note("Received Names:");
253     print_ca_names(names);
254     return 0;
255 }
256
257 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
258 {
259     return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
260                      result->tmp_key_type);
261 }
262
263 static int check_server_cert_type(HANDSHAKE_RESULT *result,
264                                   SSL_TEST_CTX *test_ctx)
265 {
266     return check_nid("Server certificate", test_ctx->expected_server_cert_type,
267                      result->server_cert_type);
268 }
269
270 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
271                                   SSL_TEST_CTX *test_ctx)
272 {
273     return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
274                      result->server_sign_hash);
275 }
276
277 static int check_server_sign_type(HANDSHAKE_RESULT *result,
278                                   SSL_TEST_CTX *test_ctx)
279 {
280     return check_nid("Server signing", test_ctx->expected_server_sign_type,
281                      result->server_sign_type);
282 }
283
284 static int check_server_ca_names(HANDSHAKE_RESULT *result,
285                                  SSL_TEST_CTX *test_ctx)
286 {
287     return check_ca_names("Server CA names",
288                           test_ctx->expected_server_ca_names,
289                           result->server_ca_names);
290 }
291
292 static int check_client_cert_type(HANDSHAKE_RESULT *result,
293                                   SSL_TEST_CTX *test_ctx)
294 {
295     return check_nid("Client certificate", test_ctx->expected_client_cert_type,
296                      result->client_cert_type);
297 }
298
299 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
300                                   SSL_TEST_CTX *test_ctx)
301 {
302     return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
303                      result->client_sign_hash);
304 }
305
306 static int check_client_sign_type(HANDSHAKE_RESULT *result,
307                                   SSL_TEST_CTX *test_ctx)
308 {
309     return check_nid("Client signing", test_ctx->expected_client_sign_type,
310                      result->client_sign_type);
311 }
312
313 static int check_client_ca_names(HANDSHAKE_RESULT *result,
314                                  SSL_TEST_CTX *test_ctx)
315 {
316     return check_ca_names("Client CA names",
317                           test_ctx->expected_client_ca_names,
318                           result->client_ca_names);
319 }
320
321 /*
322  * This could be further simplified by constructing an expected
323  * HANDSHAKE_RESULT, and implementing comparison methods for
324  * its fields.
325  */
326 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
327 {
328     int ret = 1;
329     ret &= check_result(result, test_ctx);
330     ret &= check_alerts(result, test_ctx);
331     if (result->result == SSL_TEST_SUCCESS) {
332         ret &= check_protocol(result, test_ctx);
333         ret &= check_servername(result, test_ctx);
334         ret &= check_session_ticket(result, test_ctx);
335         ret &= check_compression(result, test_ctx);
336         ret &= check_session_id(result, test_ctx);
337         ret &= (result->session_ticket_do_not_call == 0);
338 #ifndef OPENSSL_NO_NEXTPROTONEG
339         ret &= check_npn(result, test_ctx);
340 #endif
341         ret &= check_alpn(result, test_ctx);
342         ret &= check_resumption(result, test_ctx);
343         ret &= check_tmp_key(result, test_ctx);
344         ret &= check_server_cert_type(result, test_ctx);
345         ret &= check_server_sign_hash(result, test_ctx);
346         ret &= check_server_sign_type(result, test_ctx);
347         ret &= check_server_ca_names(result, test_ctx);
348         ret &= check_client_cert_type(result, test_ctx);
349         ret &= check_client_sign_hash(result, test_ctx);
350         ret &= check_client_sign_type(result, test_ctx);
351         ret &= check_client_ca_names(result, test_ctx);
352     }
353     return ret;
354 }
355
356 static int test_handshake(int idx)
357 {
358     int ret = 0;
359     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
360         *resume_server_ctx = NULL, *resume_client_ctx = NULL;
361     SSL_TEST_CTX *test_ctx = NULL;
362     HANDSHAKE_RESULT *result = NULL;
363     char test_app[MAX_TESTCASE_NAME_LENGTH];
364
365     BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
366
367     test_ctx = SSL_TEST_CTX_create(conf, test_app);
368     if (!TEST_ptr(test_ctx))
369         goto err;
370
371 #ifndef OPENSSL_NO_DTLS
372     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
373         server_ctx = SSL_CTX_new(DTLS_server_method());
374         if (test_ctx->extra.server.servername_callback !=
375             SSL_TEST_SERVERNAME_CB_NONE) {
376             if (!TEST_ptr(server2_ctx = SSL_CTX_new(DTLS_server_method())))
377                 goto err;
378         }
379         client_ctx = SSL_CTX_new(DTLS_client_method());
380         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
381             resume_server_ctx = SSL_CTX_new(DTLS_server_method());
382             resume_client_ctx = SSL_CTX_new(DTLS_client_method());
383             if (!TEST_ptr(resume_server_ctx)
384                     || !TEST_ptr(resume_client_ctx))
385                 goto err;
386         }
387     }
388 #endif
389     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
390         server_ctx = SSL_CTX_new(TLS_server_method());
391         /* SNI on resumption isn't supported/tested yet. */
392         if (test_ctx->extra.server.servername_callback !=
393             SSL_TEST_SERVERNAME_CB_NONE) {
394             if (!TEST_ptr(server2_ctx = SSL_CTX_new(TLS_server_method())))
395                 goto err;
396         }
397         client_ctx = SSL_CTX_new(TLS_client_method());
398
399         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
400             resume_server_ctx = SSL_CTX_new(TLS_server_method());
401             resume_client_ctx = SSL_CTX_new(TLS_client_method());
402             if (!TEST_ptr(resume_server_ctx)
403                     || !TEST_ptr(resume_client_ctx))
404                 goto err;
405         }
406     }
407
408     if (!TEST_ptr(server_ctx)
409             || !TEST_ptr(client_ctx)
410             || !TEST_int_gt(CONF_modules_load(conf, test_app, 0),  0))
411         goto err;
412
413     if (!SSL_CTX_config(server_ctx, "server")
414         || !SSL_CTX_config(client_ctx, "client")) {
415         goto err;
416     }
417
418     if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
419         goto err;
420     if (resume_server_ctx != NULL
421         && !SSL_CTX_config(resume_server_ctx, "resume-server"))
422         goto err;
423     if (resume_client_ctx != NULL
424         && !SSL_CTX_config(resume_client_ctx, "resume-client"))
425         goto err;
426
427     result = do_handshake(server_ctx, server2_ctx, client_ctx,
428                           resume_server_ctx, resume_client_ctx, test_ctx);
429
430     if (result != NULL)
431         ret = check_test(result, test_ctx);
432
433 err:
434     CONF_modules_unload(0);
435     SSL_CTX_free(server_ctx);
436     SSL_CTX_free(server2_ctx);
437     SSL_CTX_free(client_ctx);
438     SSL_CTX_free(resume_server_ctx);
439     SSL_CTX_free(resume_client_ctx);
440     SSL_TEST_CTX_free(test_ctx);
441     HANDSHAKE_RESULT_free(result);
442     return ret;
443 }
444
445 int setup_tests(void)
446 {
447     long num_tests;
448
449     if (!TEST_ptr(conf = NCONF_new(NULL))
450             /* argv[1] should point to the test conf file */
451             || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
452             || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
453                                                &num_tests), 0))
454         return 0;
455
456     ADD_ALL_TESTS(test_handshake, (int)num_tests);
457     return 1;
458 }
459
460 void cleanup_tests(void)
461 {
462     NCONF_free(conf);
463 }