xref: /freebsd-src/crypto/openssl/test/ssl_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert 
13*e0c4386eSCy Schubert #include <openssl/conf.h>
14*e0c4386eSCy Schubert #include <openssl/err.h>
15*e0c4386eSCy Schubert #include <openssl/ssl.h>
16*e0c4386eSCy Schubert #include <openssl/provider.h>
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #include "helpers/handshake.h"
19*e0c4386eSCy Schubert #include "helpers/ssl_test_ctx.h"
20*e0c4386eSCy Schubert #include "testutil.h"
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert static CONF *conf = NULL;
23*e0c4386eSCy Schubert static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL;
24*e0c4386eSCy Schubert static OSSL_LIB_CTX *libctx = NULL;
25*e0c4386eSCy Schubert 
26*e0c4386eSCy Schubert /* Currently the section names are of the form test-<number>, e.g. test-15. */
27*e0c4386eSCy Schubert #define MAX_TESTCASE_NAME_LENGTH 100
28*e0c4386eSCy Schubert 
print_alert(int alert)29*e0c4386eSCy Schubert static const char *print_alert(int alert)
30*e0c4386eSCy Schubert {
31*e0c4386eSCy Schubert     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
32*e0c4386eSCy Schubert }
33*e0c4386eSCy Schubert 
check_result(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)34*e0c4386eSCy Schubert static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
35*e0c4386eSCy Schubert {
36*e0c4386eSCy Schubert     if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
37*e0c4386eSCy Schubert         TEST_info("ExpectedResult mismatch: expected %s, got %s.",
38*e0c4386eSCy Schubert                   ssl_test_result_name(test_ctx->expected_result),
39*e0c4386eSCy Schubert                   ssl_test_result_name(result->result));
40*e0c4386eSCy Schubert         return 0;
41*e0c4386eSCy Schubert     }
42*e0c4386eSCy Schubert     return 1;
43*e0c4386eSCy Schubert }
44*e0c4386eSCy Schubert 
check_alerts(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)45*e0c4386eSCy Schubert static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
46*e0c4386eSCy Schubert {
47*e0c4386eSCy Schubert     if (!TEST_int_eq(result->client_alert_sent,
48*e0c4386eSCy Schubert                      result->client_alert_received)) {
49*e0c4386eSCy Schubert         TEST_info("Client sent alert %s but server received %s.",
50*e0c4386eSCy Schubert                   print_alert(result->client_alert_sent),
51*e0c4386eSCy Schubert                   print_alert(result->client_alert_received));
52*e0c4386eSCy Schubert         /*
53*e0c4386eSCy Schubert          * We can't bail here because the peer doesn't always get far enough
54*e0c4386eSCy Schubert          * to process a received alert. Specifically, in protocol version
55*e0c4386eSCy Schubert          * negotiation tests, we have the following scenario.
56*e0c4386eSCy Schubert          * Client supports TLS v1.2 only; Server supports TLS v1.1.
57*e0c4386eSCy Schubert          * Client proposes TLS v1.2; server responds with 1.1;
58*e0c4386eSCy Schubert          * Client now sends a protocol alert, using TLS v1.2 in the header.
59*e0c4386eSCy Schubert          * The server, however, rejects the alert because of version mismatch
60*e0c4386eSCy Schubert          * in the record layer; therefore, the server appears to never
61*e0c4386eSCy Schubert          * receive the alert.
62*e0c4386eSCy Schubert          */
63*e0c4386eSCy Schubert         /* return 0; */
64*e0c4386eSCy Schubert     }
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert     if (!TEST_int_eq(result->server_alert_sent,
67*e0c4386eSCy Schubert                      result->server_alert_received)) {
68*e0c4386eSCy Schubert         TEST_info("Server sent alert %s but client received %s.",
69*e0c4386eSCy Schubert                   print_alert(result->server_alert_sent),
70*e0c4386eSCy Schubert                   print_alert(result->server_alert_received));
71*e0c4386eSCy Schubert         /* return 0; */
72*e0c4386eSCy Schubert     }
73*e0c4386eSCy Schubert 
74*e0c4386eSCy Schubert     /* Tolerate an alert if one wasn't explicitly specified in the test. */
75*e0c4386eSCy Schubert     if (test_ctx->expected_client_alert
76*e0c4386eSCy Schubert         /*
77*e0c4386eSCy Schubert          * The info callback alert value is computed as
78*e0c4386eSCy Schubert          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
79*e0c4386eSCy Schubert          * where the low byte is the alert code and the high byte is other stuff.
80*e0c4386eSCy Schubert          */
81*e0c4386eSCy Schubert         && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
82*e0c4386eSCy Schubert         TEST_error("ClientAlert mismatch: expected %s, got %s.",
83*e0c4386eSCy Schubert                    print_alert(test_ctx->expected_client_alert),
84*e0c4386eSCy Schubert                    print_alert(result->client_alert_sent));
85*e0c4386eSCy Schubert         return 0;
86*e0c4386eSCy Schubert     }
87*e0c4386eSCy Schubert 
88*e0c4386eSCy Schubert     if (test_ctx->expected_server_alert
89*e0c4386eSCy Schubert         && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
90*e0c4386eSCy Schubert         TEST_error("ServerAlert mismatch: expected %s, got %s.",
91*e0c4386eSCy Schubert                    print_alert(test_ctx->expected_server_alert),
92*e0c4386eSCy Schubert                    print_alert(result->server_alert_sent));
93*e0c4386eSCy Schubert         return 0;
94*e0c4386eSCy Schubert     }
95*e0c4386eSCy Schubert 
96*e0c4386eSCy Schubert     if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
97*e0c4386eSCy Schubert         return 0;
98*e0c4386eSCy Schubert     if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
99*e0c4386eSCy Schubert         return 0;
100*e0c4386eSCy Schubert     return 1;
101*e0c4386eSCy Schubert }
102*e0c4386eSCy Schubert 
check_protocol(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)103*e0c4386eSCy Schubert static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
104*e0c4386eSCy Schubert {
105*e0c4386eSCy Schubert     if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
106*e0c4386eSCy Schubert         TEST_info("Client has protocol %s but server has %s.",
107*e0c4386eSCy Schubert                   ssl_protocol_name(result->client_protocol),
108*e0c4386eSCy Schubert                   ssl_protocol_name(result->server_protocol));
109*e0c4386eSCy Schubert         return 0;
110*e0c4386eSCy Schubert     }
111*e0c4386eSCy Schubert 
112*e0c4386eSCy Schubert     if (test_ctx->expected_protocol) {
113*e0c4386eSCy Schubert         if (!TEST_int_eq(result->client_protocol,
114*e0c4386eSCy Schubert                          test_ctx->expected_protocol)) {
115*e0c4386eSCy Schubert             TEST_info("Protocol mismatch: expected %s, got %s.\n",
116*e0c4386eSCy Schubert                       ssl_protocol_name(test_ctx->expected_protocol),
117*e0c4386eSCy Schubert                       ssl_protocol_name(result->client_protocol));
118*e0c4386eSCy Schubert             return 0;
119*e0c4386eSCy Schubert         }
120*e0c4386eSCy Schubert     }
121*e0c4386eSCy Schubert     return 1;
122*e0c4386eSCy Schubert }
123*e0c4386eSCy Schubert 
check_servername(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)124*e0c4386eSCy Schubert static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
125*e0c4386eSCy Schubert {
126*e0c4386eSCy Schubert     if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
127*e0c4386eSCy Schubert       TEST_info("Client ServerName mismatch, expected %s, got %s.",
128*e0c4386eSCy Schubert                 ssl_servername_name(test_ctx->expected_servername),
129*e0c4386eSCy Schubert                 ssl_servername_name(result->servername));
130*e0c4386eSCy Schubert       return 0;
131*e0c4386eSCy Schubert     }
132*e0c4386eSCy Schubert   return 1;
133*e0c4386eSCy Schubert }
134*e0c4386eSCy Schubert 
check_session_ticket(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)135*e0c4386eSCy Schubert static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
136*e0c4386eSCy Schubert {
137*e0c4386eSCy Schubert     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
138*e0c4386eSCy Schubert         return 1;
139*e0c4386eSCy Schubert     if (!TEST_int_eq(result->session_ticket,
140*e0c4386eSCy Schubert                      test_ctx->session_ticket_expected)) {
141*e0c4386eSCy Schubert         TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
142*e0c4386eSCy Schubert                   ssl_session_ticket_name(test_ctx->session_ticket_expected),
143*e0c4386eSCy Schubert                   ssl_session_ticket_name(result->session_ticket));
144*e0c4386eSCy Schubert         return 0;
145*e0c4386eSCy Schubert     }
146*e0c4386eSCy Schubert     return 1;
147*e0c4386eSCy Schubert }
148*e0c4386eSCy Schubert 
check_session_id(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)149*e0c4386eSCy Schubert static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
150*e0c4386eSCy Schubert {
151*e0c4386eSCy Schubert     if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
152*e0c4386eSCy Schubert         return 1;
153*e0c4386eSCy Schubert     if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
154*e0c4386eSCy Schubert         TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
155*e0c4386eSCy Schubert                 ssl_session_id_name(test_ctx->session_id_expected),
156*e0c4386eSCy Schubert                 ssl_session_id_name(result->session_id));
157*e0c4386eSCy Schubert         return 0;
158*e0c4386eSCy Schubert     }
159*e0c4386eSCy Schubert     return 1;
160*e0c4386eSCy Schubert }
161*e0c4386eSCy Schubert 
check_compression(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)162*e0c4386eSCy Schubert static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
163*e0c4386eSCy Schubert {
164*e0c4386eSCy Schubert     if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
165*e0c4386eSCy Schubert         return 0;
166*e0c4386eSCy Schubert     return 1;
167*e0c4386eSCy Schubert }
168*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
check_npn(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)169*e0c4386eSCy Schubert static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
170*e0c4386eSCy Schubert {
171*e0c4386eSCy Schubert     int ret = 1;
172*e0c4386eSCy Schubert     if (!TEST_str_eq(result->client_npn_negotiated,
173*e0c4386eSCy Schubert                      result->server_npn_negotiated))
174*e0c4386eSCy Schubert         ret = 0;
175*e0c4386eSCy Schubert     if (!TEST_str_eq(test_ctx->expected_npn_protocol,
176*e0c4386eSCy Schubert                      result->client_npn_negotiated))
177*e0c4386eSCy Schubert         ret = 0;
178*e0c4386eSCy Schubert     return ret;
179*e0c4386eSCy Schubert }
180*e0c4386eSCy Schubert #endif
181*e0c4386eSCy Schubert 
check_alpn(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)182*e0c4386eSCy Schubert static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
183*e0c4386eSCy Schubert {
184*e0c4386eSCy Schubert     int ret = 1;
185*e0c4386eSCy Schubert     if (!TEST_str_eq(result->client_alpn_negotiated,
186*e0c4386eSCy Schubert                      result->server_alpn_negotiated))
187*e0c4386eSCy Schubert         ret = 0;
188*e0c4386eSCy Schubert     if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
189*e0c4386eSCy Schubert                      result->client_alpn_negotiated))
190*e0c4386eSCy Schubert         ret = 0;
191*e0c4386eSCy Schubert     return ret;
192*e0c4386eSCy Schubert }
193*e0c4386eSCy Schubert 
check_session_ticket_app_data(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)194*e0c4386eSCy Schubert static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
195*e0c4386eSCy Schubert                                          SSL_TEST_CTX *test_ctx)
196*e0c4386eSCy Schubert {
197*e0c4386eSCy Schubert     size_t result_len = 0;
198*e0c4386eSCy Schubert     size_t expected_len = 0;
199*e0c4386eSCy Schubert 
200*e0c4386eSCy Schubert     /* consider empty and NULL strings to be the same */
201*e0c4386eSCy Schubert     if (result->result_session_ticket_app_data != NULL)
202*e0c4386eSCy Schubert         result_len = strlen(result->result_session_ticket_app_data);
203*e0c4386eSCy Schubert     if (test_ctx->expected_session_ticket_app_data != NULL)
204*e0c4386eSCy Schubert         expected_len = strlen(test_ctx->expected_session_ticket_app_data);
205*e0c4386eSCy Schubert     if (result_len == 0 && expected_len == 0)
206*e0c4386eSCy Schubert         return 1;
207*e0c4386eSCy Schubert 
208*e0c4386eSCy Schubert     if (!TEST_str_eq(result->result_session_ticket_app_data,
209*e0c4386eSCy Schubert                      test_ctx->expected_session_ticket_app_data))
210*e0c4386eSCy Schubert         return 0;
211*e0c4386eSCy Schubert 
212*e0c4386eSCy Schubert     return 1;
213*e0c4386eSCy Schubert }
214*e0c4386eSCy Schubert 
check_resumption(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)215*e0c4386eSCy Schubert static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
216*e0c4386eSCy Schubert {
217*e0c4386eSCy Schubert     if (!TEST_int_eq(result->client_resumed, result->server_resumed))
218*e0c4386eSCy Schubert         return 0;
219*e0c4386eSCy Schubert     if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
220*e0c4386eSCy Schubert         return 0;
221*e0c4386eSCy Schubert     return 1;
222*e0c4386eSCy Schubert }
223*e0c4386eSCy Schubert 
check_nid(const char * name,int expected_nid,int nid)224*e0c4386eSCy Schubert static int check_nid(const char *name, int expected_nid, int nid)
225*e0c4386eSCy Schubert {
226*e0c4386eSCy Schubert     if (expected_nid == 0 || expected_nid == nid)
227*e0c4386eSCy Schubert         return 1;
228*e0c4386eSCy Schubert     TEST_error("%s type mismatch, %s vs %s\n",
229*e0c4386eSCy Schubert                name, OBJ_nid2ln(expected_nid),
230*e0c4386eSCy Schubert                nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
231*e0c4386eSCy Schubert     return 0;
232*e0c4386eSCy Schubert }
233*e0c4386eSCy Schubert 
print_ca_names(STACK_OF (X509_NAME)* names)234*e0c4386eSCy Schubert static void print_ca_names(STACK_OF(X509_NAME) *names)
235*e0c4386eSCy Schubert {
236*e0c4386eSCy Schubert     int i;
237*e0c4386eSCy Schubert 
238*e0c4386eSCy Schubert     if (names == NULL || sk_X509_NAME_num(names) == 0) {
239*e0c4386eSCy Schubert         TEST_note("    <empty>");
240*e0c4386eSCy Schubert         return;
241*e0c4386eSCy Schubert     }
242*e0c4386eSCy Schubert     for (i = 0; i < sk_X509_NAME_num(names); i++) {
243*e0c4386eSCy Schubert         X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
244*e0c4386eSCy Schubert                            XN_FLAG_ONELINE);
245*e0c4386eSCy Schubert         BIO_puts(bio_err, "\n");
246*e0c4386eSCy Schubert     }
247*e0c4386eSCy Schubert }
248*e0c4386eSCy Schubert 
check_ca_names(const char * name,STACK_OF (X509_NAME)* expected_names,STACK_OF (X509_NAME)* names)249*e0c4386eSCy Schubert static int check_ca_names(const char *name,
250*e0c4386eSCy Schubert                           STACK_OF(X509_NAME) *expected_names,
251*e0c4386eSCy Schubert                           STACK_OF(X509_NAME) *names)
252*e0c4386eSCy Schubert {
253*e0c4386eSCy Schubert     int i;
254*e0c4386eSCy Schubert 
255*e0c4386eSCy Schubert     if (expected_names == NULL)
256*e0c4386eSCy Schubert         return 1;
257*e0c4386eSCy Schubert     if (names == NULL || sk_X509_NAME_num(names) == 0) {
258*e0c4386eSCy Schubert         if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
259*e0c4386eSCy Schubert             return 1;
260*e0c4386eSCy Schubert         goto err;
261*e0c4386eSCy Schubert     }
262*e0c4386eSCy Schubert     if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
263*e0c4386eSCy Schubert         goto err;
264*e0c4386eSCy Schubert     for (i = 0; i < sk_X509_NAME_num(names); i++) {
265*e0c4386eSCy Schubert         if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
266*e0c4386eSCy Schubert                                        sk_X509_NAME_value(expected_names, i)),
267*e0c4386eSCy Schubert                          0)) {
268*e0c4386eSCy Schubert             goto err;
269*e0c4386eSCy Schubert         }
270*e0c4386eSCy Schubert     }
271*e0c4386eSCy Schubert     return 1;
272*e0c4386eSCy Schubert err:
273*e0c4386eSCy Schubert     TEST_info("%s: list mismatch", name);
274*e0c4386eSCy Schubert     TEST_note("Expected Names:");
275*e0c4386eSCy Schubert     print_ca_names(expected_names);
276*e0c4386eSCy Schubert     TEST_note("Received Names:");
277*e0c4386eSCy Schubert     print_ca_names(names);
278*e0c4386eSCy Schubert     return 0;
279*e0c4386eSCy Schubert }
280*e0c4386eSCy Schubert 
check_tmp_key(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)281*e0c4386eSCy Schubert static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
282*e0c4386eSCy Schubert {
283*e0c4386eSCy Schubert     return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
284*e0c4386eSCy Schubert                      result->tmp_key_type);
285*e0c4386eSCy Schubert }
286*e0c4386eSCy Schubert 
check_server_cert_type(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)287*e0c4386eSCy Schubert static int check_server_cert_type(HANDSHAKE_RESULT *result,
288*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
289*e0c4386eSCy Schubert {
290*e0c4386eSCy Schubert     return check_nid("Server certificate", test_ctx->expected_server_cert_type,
291*e0c4386eSCy Schubert                      result->server_cert_type);
292*e0c4386eSCy Schubert }
293*e0c4386eSCy Schubert 
check_server_sign_hash(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)294*e0c4386eSCy Schubert static int check_server_sign_hash(HANDSHAKE_RESULT *result,
295*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
296*e0c4386eSCy Schubert {
297*e0c4386eSCy Schubert     return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
298*e0c4386eSCy Schubert                      result->server_sign_hash);
299*e0c4386eSCy Schubert }
300*e0c4386eSCy Schubert 
check_server_sign_type(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)301*e0c4386eSCy Schubert static int check_server_sign_type(HANDSHAKE_RESULT *result,
302*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
303*e0c4386eSCy Schubert {
304*e0c4386eSCy Schubert     return check_nid("Server signing", test_ctx->expected_server_sign_type,
305*e0c4386eSCy Schubert                      result->server_sign_type);
306*e0c4386eSCy Schubert }
307*e0c4386eSCy Schubert 
check_server_ca_names(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)308*e0c4386eSCy Schubert static int check_server_ca_names(HANDSHAKE_RESULT *result,
309*e0c4386eSCy Schubert                                  SSL_TEST_CTX *test_ctx)
310*e0c4386eSCy Schubert {
311*e0c4386eSCy Schubert     return check_ca_names("Server CA names",
312*e0c4386eSCy Schubert                           test_ctx->expected_server_ca_names,
313*e0c4386eSCy Schubert                           result->server_ca_names);
314*e0c4386eSCy Schubert }
315*e0c4386eSCy Schubert 
check_client_cert_type(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)316*e0c4386eSCy Schubert static int check_client_cert_type(HANDSHAKE_RESULT *result,
317*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
318*e0c4386eSCy Schubert {
319*e0c4386eSCy Schubert     return check_nid("Client certificate", test_ctx->expected_client_cert_type,
320*e0c4386eSCy Schubert                      result->client_cert_type);
321*e0c4386eSCy Schubert }
322*e0c4386eSCy Schubert 
check_client_sign_hash(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)323*e0c4386eSCy Schubert static int check_client_sign_hash(HANDSHAKE_RESULT *result,
324*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
325*e0c4386eSCy Schubert {
326*e0c4386eSCy Schubert     return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
327*e0c4386eSCy Schubert                      result->client_sign_hash);
328*e0c4386eSCy Schubert }
329*e0c4386eSCy Schubert 
check_client_sign_type(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)330*e0c4386eSCy Schubert static int check_client_sign_type(HANDSHAKE_RESULT *result,
331*e0c4386eSCy Schubert                                   SSL_TEST_CTX *test_ctx)
332*e0c4386eSCy Schubert {
333*e0c4386eSCy Schubert     return check_nid("Client signing", test_ctx->expected_client_sign_type,
334*e0c4386eSCy Schubert                      result->client_sign_type);
335*e0c4386eSCy Schubert }
336*e0c4386eSCy Schubert 
check_client_ca_names(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)337*e0c4386eSCy Schubert static int check_client_ca_names(HANDSHAKE_RESULT *result,
338*e0c4386eSCy Schubert                                  SSL_TEST_CTX *test_ctx)
339*e0c4386eSCy Schubert {
340*e0c4386eSCy Schubert     return check_ca_names("Client CA names",
341*e0c4386eSCy Schubert                           test_ctx->expected_client_ca_names,
342*e0c4386eSCy Schubert                           result->client_ca_names);
343*e0c4386eSCy Schubert }
344*e0c4386eSCy Schubert 
check_cipher(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)345*e0c4386eSCy Schubert static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
346*e0c4386eSCy Schubert {
347*e0c4386eSCy Schubert     if (test_ctx->expected_cipher == NULL)
348*e0c4386eSCy Schubert         return 1;
349*e0c4386eSCy Schubert     if (!TEST_ptr(result->cipher))
350*e0c4386eSCy Schubert         return 0;
351*e0c4386eSCy Schubert     if (!TEST_str_eq(test_ctx->expected_cipher,
352*e0c4386eSCy Schubert                      result->cipher))
353*e0c4386eSCy Schubert         return 0;
354*e0c4386eSCy Schubert     return 1;
355*e0c4386eSCy Schubert }
356*e0c4386eSCy Schubert 
357*e0c4386eSCy Schubert /*
358*e0c4386eSCy Schubert  * This could be further simplified by constructing an expected
359*e0c4386eSCy Schubert  * HANDSHAKE_RESULT, and implementing comparison methods for
360*e0c4386eSCy Schubert  * its fields.
361*e0c4386eSCy Schubert  */
check_test(HANDSHAKE_RESULT * result,SSL_TEST_CTX * test_ctx)362*e0c4386eSCy Schubert static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
363*e0c4386eSCy Schubert {
364*e0c4386eSCy Schubert     int ret = 1;
365*e0c4386eSCy Schubert     ret &= check_result(result, test_ctx);
366*e0c4386eSCy Schubert     ret &= check_alerts(result, test_ctx);
367*e0c4386eSCy Schubert     if (result->result == SSL_TEST_SUCCESS) {
368*e0c4386eSCy Schubert         ret &= check_protocol(result, test_ctx);
369*e0c4386eSCy Schubert         ret &= check_servername(result, test_ctx);
370*e0c4386eSCy Schubert         ret &= check_session_ticket(result, test_ctx);
371*e0c4386eSCy Schubert         ret &= check_compression(result, test_ctx);
372*e0c4386eSCy Schubert         ret &= check_session_id(result, test_ctx);
373*e0c4386eSCy Schubert         ret &= (result->session_ticket_do_not_call == 0);
374*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
375*e0c4386eSCy Schubert         ret &= check_npn(result, test_ctx);
376*e0c4386eSCy Schubert #endif
377*e0c4386eSCy Schubert         ret &= check_cipher(result, test_ctx);
378*e0c4386eSCy Schubert         ret &= check_alpn(result, test_ctx);
379*e0c4386eSCy Schubert         ret &= check_session_ticket_app_data(result, test_ctx);
380*e0c4386eSCy Schubert         ret &= check_resumption(result, test_ctx);
381*e0c4386eSCy Schubert         ret &= check_tmp_key(result, test_ctx);
382*e0c4386eSCy Schubert         ret &= check_server_cert_type(result, test_ctx);
383*e0c4386eSCy Schubert         ret &= check_server_sign_hash(result, test_ctx);
384*e0c4386eSCy Schubert         ret &= check_server_sign_type(result, test_ctx);
385*e0c4386eSCy Schubert         ret &= check_server_ca_names(result, test_ctx);
386*e0c4386eSCy Schubert         ret &= check_client_cert_type(result, test_ctx);
387*e0c4386eSCy Schubert         ret &= check_client_sign_hash(result, test_ctx);
388*e0c4386eSCy Schubert         ret &= check_client_sign_type(result, test_ctx);
389*e0c4386eSCy Schubert         ret &= check_client_ca_names(result, test_ctx);
390*e0c4386eSCy Schubert     }
391*e0c4386eSCy Schubert     return ret;
392*e0c4386eSCy Schubert }
393*e0c4386eSCy Schubert 
test_handshake(int idx)394*e0c4386eSCy Schubert static int test_handshake(int idx)
395*e0c4386eSCy Schubert {
396*e0c4386eSCy Schubert     int ret = 0;
397*e0c4386eSCy Schubert     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
398*e0c4386eSCy Schubert         *resume_server_ctx = NULL, *resume_client_ctx = NULL;
399*e0c4386eSCy Schubert     SSL_TEST_CTX *test_ctx = NULL;
400*e0c4386eSCy Schubert     HANDSHAKE_RESULT *result = NULL;
401*e0c4386eSCy Schubert     char test_app[MAX_TESTCASE_NAME_LENGTH];
402*e0c4386eSCy Schubert 
403*e0c4386eSCy Schubert     BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
404*e0c4386eSCy Schubert 
405*e0c4386eSCy Schubert     test_ctx = SSL_TEST_CTX_create(conf, test_app, libctx);
406*e0c4386eSCy Schubert     if (!TEST_ptr(test_ctx))
407*e0c4386eSCy Schubert         goto err;
408*e0c4386eSCy Schubert 
409*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
410*e0c4386eSCy Schubert     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
411*e0c4386eSCy Schubert         server_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_server_method());
412*e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_options(server_ctx,
413*e0c4386eSCy Schubert                         SSL_OP_ALLOW_CLIENT_RENEGOTIATION))
414*e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
415*e0c4386eSCy Schubert             goto err;
416*e0c4386eSCy Schubert         if (test_ctx->extra.server.servername_callback !=
417*e0c4386eSCy Schubert             SSL_TEST_SERVERNAME_CB_NONE) {
418*e0c4386eSCy Schubert             if (!TEST_ptr(server2_ctx =
419*e0c4386eSCy Schubert                             SSL_CTX_new_ex(libctx, NULL, DTLS_server_method()))
420*e0c4386eSCy Schubert                     || !TEST_true(SSL_CTX_set_options(server2_ctx,
421*e0c4386eSCy Schubert                             SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
422*e0c4386eSCy Schubert                 goto err;
423*e0c4386eSCy Schubert         }
424*e0c4386eSCy Schubert         client_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_client_method());
425*e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
426*e0c4386eSCy Schubert             goto err;
427*e0c4386eSCy Schubert         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
428*e0c4386eSCy Schubert             resume_server_ctx = SSL_CTX_new_ex(libctx, NULL,
429*e0c4386eSCy Schubert                                                DTLS_server_method());
430*e0c4386eSCy Schubert             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0))
431*e0c4386eSCy Schubert                     || !TEST_true(SSL_CTX_set_options(resume_server_ctx,
432*e0c4386eSCy Schubert                             SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
433*e0c4386eSCy Schubert                 goto err;
434*e0c4386eSCy Schubert             resume_client_ctx = SSL_CTX_new_ex(libctx, NULL,
435*e0c4386eSCy Schubert                                                DTLS_client_method());
436*e0c4386eSCy Schubert             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
437*e0c4386eSCy Schubert                 goto err;
438*e0c4386eSCy Schubert             if (!TEST_ptr(resume_server_ctx)
439*e0c4386eSCy Schubert                     || !TEST_ptr(resume_client_ctx))
440*e0c4386eSCy Schubert                 goto err;
441*e0c4386eSCy Schubert         }
442*e0c4386eSCy Schubert     }
443*e0c4386eSCy Schubert #endif
444*e0c4386eSCy Schubert     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
445*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_3) \
446*e0c4386eSCy Schubert     && defined(OPENSSL_NO_EC) \
447*e0c4386eSCy Schubert     && defined(OPENSSL_NO_DH)
448*e0c4386eSCy Schubert         /* Without ec or dh there are no built-in groups for TLSv1.3 */
449*e0c4386eSCy Schubert         int maxversion = TLS1_2_VERSION;
450*e0c4386eSCy Schubert #else
451*e0c4386eSCy Schubert         int maxversion = 0;
452*e0c4386eSCy Schubert #endif
453*e0c4386eSCy Schubert 
454*e0c4386eSCy Schubert         server_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
455*e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, maxversion))
456*e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_options(server_ctx,
457*e0c4386eSCy Schubert                             SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
458*e0c4386eSCy Schubert             goto err;
459*e0c4386eSCy Schubert         /* SNI on resumption isn't supported/tested yet. */
460*e0c4386eSCy Schubert         if (test_ctx->extra.server.servername_callback !=
461*e0c4386eSCy Schubert             SSL_TEST_SERVERNAME_CB_NONE) {
462*e0c4386eSCy Schubert             if (!TEST_ptr(server2_ctx =
463*e0c4386eSCy Schubert                             SSL_CTX_new_ex(libctx, NULL, TLS_server_method()))
464*e0c4386eSCy Schubert                     || !TEST_true(SSL_CTX_set_options(server2_ctx,
465*e0c4386eSCy Schubert                             SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
466*e0c4386eSCy Schubert                 goto err;
467*e0c4386eSCy Schubert             if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx,
468*e0c4386eSCy Schubert                                                          maxversion)))
469*e0c4386eSCy Schubert                 goto err;
470*e0c4386eSCy Schubert         }
471*e0c4386eSCy Schubert         client_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
472*e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, maxversion)))
473*e0c4386eSCy Schubert             goto err;
474*e0c4386eSCy Schubert 
475*e0c4386eSCy Schubert         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
476*e0c4386eSCy Schubert             resume_server_ctx = SSL_CTX_new_ex(libctx, NULL,
477*e0c4386eSCy Schubert                                                TLS_server_method());
478*e0c4386eSCy Schubert             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx,
479*e0c4386eSCy Schubert                                                          maxversion))
480*e0c4386eSCy Schubert                     || !TEST_true(SSL_CTX_set_options(resume_server_ctx,
481*e0c4386eSCy Schubert                             SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
482*e0c4386eSCy Schubert                 goto err;
483*e0c4386eSCy Schubert             resume_client_ctx = SSL_CTX_new_ex(libctx, NULL,
484*e0c4386eSCy Schubert                                                TLS_client_method());
485*e0c4386eSCy Schubert             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx,
486*e0c4386eSCy Schubert                                                          maxversion)))
487*e0c4386eSCy Schubert                 goto err;
488*e0c4386eSCy Schubert             if (!TEST_ptr(resume_server_ctx)
489*e0c4386eSCy Schubert                     || !TEST_ptr(resume_client_ctx))
490*e0c4386eSCy Schubert                 goto err;
491*e0c4386eSCy Schubert         }
492*e0c4386eSCy Schubert     }
493*e0c4386eSCy Schubert 
494*e0c4386eSCy Schubert #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
495*e0c4386eSCy Schubert     if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
496*e0c4386eSCy Schubert         goto err;
497*e0c4386eSCy Schubert #endif
498*e0c4386eSCy Schubert 
499*e0c4386eSCy Schubert     if (!TEST_ptr(server_ctx)
500*e0c4386eSCy Schubert             || !TEST_ptr(client_ctx)
501*e0c4386eSCy Schubert             || !TEST_int_gt(CONF_modules_load(conf, test_app, 0),  0))
502*e0c4386eSCy Schubert         goto err;
503*e0c4386eSCy Schubert 
504*e0c4386eSCy Schubert     if (!SSL_CTX_config(server_ctx, "server")
505*e0c4386eSCy Schubert         || !SSL_CTX_config(client_ctx, "client")) {
506*e0c4386eSCy Schubert         goto err;
507*e0c4386eSCy Schubert     }
508*e0c4386eSCy Schubert 
509*e0c4386eSCy Schubert     if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
510*e0c4386eSCy Schubert         goto err;
511*e0c4386eSCy Schubert     if (resume_server_ctx != NULL
512*e0c4386eSCy Schubert         && !SSL_CTX_config(resume_server_ctx, "resume-server"))
513*e0c4386eSCy Schubert         goto err;
514*e0c4386eSCy Schubert     if (resume_client_ctx != NULL
515*e0c4386eSCy Schubert         && !SSL_CTX_config(resume_client_ctx, "resume-client"))
516*e0c4386eSCy Schubert         goto err;
517*e0c4386eSCy Schubert 
518*e0c4386eSCy Schubert     result = do_handshake(server_ctx, server2_ctx, client_ctx,
519*e0c4386eSCy Schubert                           resume_server_ctx, resume_client_ctx, test_ctx);
520*e0c4386eSCy Schubert 
521*e0c4386eSCy Schubert     if (result != NULL)
522*e0c4386eSCy Schubert         ret = check_test(result, test_ctx);
523*e0c4386eSCy Schubert 
524*e0c4386eSCy Schubert err:
525*e0c4386eSCy Schubert     CONF_modules_unload(0);
526*e0c4386eSCy Schubert     SSL_CTX_free(server_ctx);
527*e0c4386eSCy Schubert     SSL_CTX_free(server2_ctx);
528*e0c4386eSCy Schubert     SSL_CTX_free(client_ctx);
529*e0c4386eSCy Schubert     SSL_CTX_free(resume_server_ctx);
530*e0c4386eSCy Schubert     SSL_CTX_free(resume_client_ctx);
531*e0c4386eSCy Schubert     SSL_TEST_CTX_free(test_ctx);
532*e0c4386eSCy Schubert     HANDSHAKE_RESULT_free(result);
533*e0c4386eSCy Schubert     return ret;
534*e0c4386eSCy Schubert }
535*e0c4386eSCy Schubert 
536*e0c4386eSCy Schubert #define USAGE "conf_file module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)537*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE(USAGE)
538*e0c4386eSCy Schubert 
539*e0c4386eSCy Schubert int setup_tests(void)
540*e0c4386eSCy Schubert {
541*e0c4386eSCy Schubert     long num_tests;
542*e0c4386eSCy Schubert 
543*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
544*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
545*e0c4386eSCy Schubert         return 0;
546*e0c4386eSCy Schubert     }
547*e0c4386eSCy Schubert 
548*e0c4386eSCy Schubert     if (!TEST_ptr(conf = NCONF_new(NULL))
549*e0c4386eSCy Schubert             /* argv[1] should point to the test conf file */
550*e0c4386eSCy Schubert             || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
551*e0c4386eSCy Schubert             || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
552*e0c4386eSCy Schubert                                                &num_tests), 0)) {
553*e0c4386eSCy Schubert         TEST_error("usage: ssl_test %s", USAGE);
554*e0c4386eSCy Schubert         return 0;
555*e0c4386eSCy Schubert     }
556*e0c4386eSCy Schubert 
557*e0c4386eSCy Schubert     if (!test_arg_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
558*e0c4386eSCy Schubert         return 0;
559*e0c4386eSCy Schubert 
560*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_handshake, (int)num_tests);
561*e0c4386eSCy Schubert     return 1;
562*e0c4386eSCy Schubert }
563*e0c4386eSCy Schubert 
cleanup_tests(void)564*e0c4386eSCy Schubert void cleanup_tests(void)
565*e0c4386eSCy Schubert {
566*e0c4386eSCy Schubert     NCONF_free(conf);
567*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(defctxnull);
568*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(thisprov);
569*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
570*e0c4386eSCy Schubert }
571