xref: /freebsd/crypto/openssl/test/helpers/ssl_test_ctx.h (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 #ifndef OSSL_TEST_SSL_TEST_CTX_H
11*e0c4386eSCy Schubert #define OSSL_TEST_SSL_TEST_CTX_H
12*e0c4386eSCy Schubert 
13*e0c4386eSCy Schubert #include <openssl/conf.h>
14*e0c4386eSCy Schubert #include <openssl/ssl.h>
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert typedef enum {
17*e0c4386eSCy Schubert     SSL_TEST_SUCCESS = 0,  /* Default */
18*e0c4386eSCy Schubert     SSL_TEST_SERVER_FAIL,
19*e0c4386eSCy Schubert     SSL_TEST_CLIENT_FAIL,
20*e0c4386eSCy Schubert     SSL_TEST_INTERNAL_ERROR,
21*e0c4386eSCy Schubert     /* Couldn't test resumption/renegotiation: original handshake failed. */
22*e0c4386eSCy Schubert     SSL_TEST_FIRST_HANDSHAKE_FAILED
23*e0c4386eSCy Schubert } ssl_test_result_t;
24*e0c4386eSCy Schubert 
25*e0c4386eSCy Schubert typedef enum {
26*e0c4386eSCy Schubert     SSL_TEST_VERIFY_NONE = 0, /* Default */
27*e0c4386eSCy Schubert     SSL_TEST_VERIFY_ACCEPT_ALL,
28*e0c4386eSCy Schubert     SSL_TEST_VERIFY_RETRY_ONCE,
29*e0c4386eSCy Schubert     SSL_TEST_VERIFY_REJECT_ALL
30*e0c4386eSCy Schubert } ssl_verify_callback_t;
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert typedef enum {
33*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_NONE = 0, /* Default */
34*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_SERVER1,
35*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_SERVER2,
36*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_INVALID
37*e0c4386eSCy Schubert } ssl_servername_t;
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert typedef enum {
40*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_CB_NONE = 0,  /* Default */
41*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_IGNORE_MISMATCH,
42*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_REJECT_MISMATCH,
43*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH,
44*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH,
45*e0c4386eSCy Schubert     SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12
46*e0c4386eSCy Schubert } ssl_servername_callback_t;
47*e0c4386eSCy Schubert 
48*e0c4386eSCy Schubert typedef enum {
49*e0c4386eSCy Schubert     SSL_TEST_SESSION_TICKET_IGNORE = 0, /* Default */
50*e0c4386eSCy Schubert     SSL_TEST_SESSION_TICKET_YES,
51*e0c4386eSCy Schubert     SSL_TEST_SESSION_TICKET_NO,
52*e0c4386eSCy Schubert     SSL_TEST_SESSION_TICKET_BROKEN /* Special test */
53*e0c4386eSCy Schubert } ssl_session_ticket_t;
54*e0c4386eSCy Schubert 
55*e0c4386eSCy Schubert typedef enum {
56*e0c4386eSCy Schubert     SSL_TEST_COMPRESSION_NO = 0, /* Default */
57*e0c4386eSCy Schubert     SSL_TEST_COMPRESSION_YES
58*e0c4386eSCy Schubert } ssl_compression_t;
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert typedef enum {
61*e0c4386eSCy Schubert     SSL_TEST_SESSION_ID_IGNORE = 0, /* Default */
62*e0c4386eSCy Schubert     SSL_TEST_SESSION_ID_YES,
63*e0c4386eSCy Schubert     SSL_TEST_SESSION_ID_NO
64*e0c4386eSCy Schubert } ssl_session_id_t;
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert typedef enum {
67*e0c4386eSCy Schubert     SSL_TEST_METHOD_TLS = 0, /* Default */
68*e0c4386eSCy Schubert     SSL_TEST_METHOD_DTLS
69*e0c4386eSCy Schubert } ssl_test_method_t;
70*e0c4386eSCy Schubert 
71*e0c4386eSCy Schubert typedef enum {
72*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_SIMPLE = 0, /* Default */
73*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_RESUME,
74*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_RENEG_SERVER,
75*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_RENEG_CLIENT,
76*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER,
77*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT,
78*e0c4386eSCy Schubert     SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH
79*e0c4386eSCy Schubert } ssl_handshake_mode_t;
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert typedef enum {
82*e0c4386eSCy Schubert     SSL_TEST_CT_VALIDATION_NONE = 0, /* Default */
83*e0c4386eSCy Schubert     SSL_TEST_CT_VALIDATION_PERMISSIVE,
84*e0c4386eSCy Schubert     SSL_TEST_CT_VALIDATION_STRICT
85*e0c4386eSCy Schubert } ssl_ct_validation_t;
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert typedef enum {
88*e0c4386eSCy Schubert     SSL_TEST_CERT_STATUS_NONE = 0, /* Default */
89*e0c4386eSCy Schubert     SSL_TEST_CERT_STATUS_GOOD_RESPONSE,
90*e0c4386eSCy Schubert     SSL_TEST_CERT_STATUS_BAD_RESPONSE
91*e0c4386eSCy Schubert } ssl_cert_status_t;
92*e0c4386eSCy Schubert 
93*e0c4386eSCy Schubert /*
94*e0c4386eSCy Schubert  * Server/client settings that aren't supported by the SSL CONF library,
95*e0c4386eSCy Schubert  * such as callbacks.
96*e0c4386eSCy Schubert  */
97*e0c4386eSCy Schubert typedef struct {
98*e0c4386eSCy Schubert     /* One of a number of predefined custom callbacks. */
99*e0c4386eSCy Schubert     ssl_verify_callback_t verify_callback;
100*e0c4386eSCy Schubert     /* One of a number of predefined server names use by the client */
101*e0c4386eSCy Schubert     ssl_servername_t servername;
102*e0c4386eSCy Schubert     /* Maximum Fragment Length extension mode */
103*e0c4386eSCy Schubert     int max_fragment_len_mode;
104*e0c4386eSCy Schubert     /* Supported NPN and ALPN protocols. A comma-separated list. */
105*e0c4386eSCy Schubert     char *npn_protocols;
106*e0c4386eSCy Schubert     char *alpn_protocols;
107*e0c4386eSCy Schubert     ssl_ct_validation_t ct_validation;
108*e0c4386eSCy Schubert     /* Ciphersuites to set on a renegotiation */
109*e0c4386eSCy Schubert     char *reneg_ciphers;
110*e0c4386eSCy Schubert     char *srp_user;
111*e0c4386eSCy Schubert     char *srp_password;
112*e0c4386eSCy Schubert     /* PHA enabled */
113*e0c4386eSCy Schubert     int enable_pha;
114*e0c4386eSCy Schubert     /* Do not send extms on renegotiation */
115*e0c4386eSCy Schubert     int no_extms_on_reneg;
116*e0c4386eSCy Schubert } SSL_TEST_CLIENT_CONF;
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert typedef struct {
119*e0c4386eSCy Schubert     /* SNI callback (server-side). */
120*e0c4386eSCy Schubert     ssl_servername_callback_t servername_callback;
121*e0c4386eSCy Schubert     /* Supported NPN and ALPN protocols. A comma-separated list. */
122*e0c4386eSCy Schubert     char *npn_protocols;
123*e0c4386eSCy Schubert     char *alpn_protocols;
124*e0c4386eSCy Schubert     /* Whether to set a broken session ticket callback. */
125*e0c4386eSCy Schubert     int broken_session_ticket;
126*e0c4386eSCy Schubert     /* Should we send a CertStatus message? */
127*e0c4386eSCy Schubert     ssl_cert_status_t cert_status;
128*e0c4386eSCy Schubert     /* An SRP user known to the server. */
129*e0c4386eSCy Schubert     char *srp_user;
130*e0c4386eSCy Schubert     char *srp_password;
131*e0c4386eSCy Schubert     /* Forced PHA */
132*e0c4386eSCy Schubert     int force_pha;
133*e0c4386eSCy Schubert     char *session_ticket_app_data;
134*e0c4386eSCy Schubert } SSL_TEST_SERVER_CONF;
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert typedef struct {
137*e0c4386eSCy Schubert     SSL_TEST_CLIENT_CONF client;
138*e0c4386eSCy Schubert     SSL_TEST_SERVER_CONF server;
139*e0c4386eSCy Schubert     SSL_TEST_SERVER_CONF server2;
140*e0c4386eSCy Schubert } SSL_TEST_EXTRA_CONF;
141*e0c4386eSCy Schubert 
142*e0c4386eSCy Schubert typedef struct {
143*e0c4386eSCy Schubert     /*
144*e0c4386eSCy Schubert      * Global test configuration. Does not change between handshakes.
145*e0c4386eSCy Schubert      */
146*e0c4386eSCy Schubert     /* Whether the server/client CTX should use DTLS or TLS. */
147*e0c4386eSCy Schubert     ssl_test_method_t method;
148*e0c4386eSCy Schubert     /* Whether to test a resumed/renegotiated handshake. */
149*e0c4386eSCy Schubert     ssl_handshake_mode_t handshake_mode;
150*e0c4386eSCy Schubert     /*
151*e0c4386eSCy Schubert      * How much application data to exchange (default is 256 bytes).
152*e0c4386eSCy Schubert      * Both peers will send |app_data_size| bytes interleaved.
153*e0c4386eSCy Schubert      */
154*e0c4386eSCy Schubert     int app_data_size;
155*e0c4386eSCy Schubert     /* Maximum send fragment size. */
156*e0c4386eSCy Schubert     int max_fragment_size;
157*e0c4386eSCy Schubert     /* KeyUpdate type */
158*e0c4386eSCy Schubert     int key_update_type;
159*e0c4386eSCy Schubert 
160*e0c4386eSCy Schubert     /*
161*e0c4386eSCy Schubert      * Extra server/client configurations. Per-handshake.
162*e0c4386eSCy Schubert      */
163*e0c4386eSCy Schubert     /* First handshake. */
164*e0c4386eSCy Schubert     SSL_TEST_EXTRA_CONF extra;
165*e0c4386eSCy Schubert     /* Resumed handshake. */
166*e0c4386eSCy Schubert     SSL_TEST_EXTRA_CONF resume_extra;
167*e0c4386eSCy Schubert 
168*e0c4386eSCy Schubert     /*
169*e0c4386eSCy Schubert      * Test expectations. These apply to the LAST handshake.
170*e0c4386eSCy Schubert      */
171*e0c4386eSCy Schubert     /* Defaults to SUCCESS. */
172*e0c4386eSCy Schubert     ssl_test_result_t expected_result;
173*e0c4386eSCy Schubert     /* Alerts. 0 if no expectation. */
174*e0c4386eSCy Schubert     /* See ssl.h for alert codes. */
175*e0c4386eSCy Schubert     /* Alert sent by the client / received by the server. */
176*e0c4386eSCy Schubert     int expected_client_alert;
177*e0c4386eSCy Schubert     /* Alert sent by the server / received by the client. */
178*e0c4386eSCy Schubert     int expected_server_alert;
179*e0c4386eSCy Schubert     /* Negotiated protocol version. 0 if no expectation. */
180*e0c4386eSCy Schubert     /* See ssl.h for protocol versions. */
181*e0c4386eSCy Schubert     int expected_protocol;
182*e0c4386eSCy Schubert     /*
183*e0c4386eSCy Schubert      * The expected SNI context to use.
184*e0c4386eSCy Schubert      * We test server-side that the server switched to the expected context.
185*e0c4386eSCy Schubert      * Set by the callback upon success, so if the callback wasn't called or
186*e0c4386eSCy Schubert      * terminated with an alert, the servername will match with
187*e0c4386eSCy Schubert      * SSL_TEST_SERVERNAME_NONE.
188*e0c4386eSCy Schubert      * Note: in the event that the servername was accepted, the client should
189*e0c4386eSCy Schubert      * also receive an empty SNI extension back but we have no way of probing
190*e0c4386eSCy Schubert      * client-side via the API that this was the case.
191*e0c4386eSCy Schubert      */
192*e0c4386eSCy Schubert     ssl_servername_t expected_servername;
193*e0c4386eSCy Schubert     ssl_session_ticket_t session_ticket_expected;
194*e0c4386eSCy Schubert     int compression_expected;
195*e0c4386eSCy Schubert     /* The expected NPN/ALPN protocol to negotiate. */
196*e0c4386eSCy Schubert     char *expected_npn_protocol;
197*e0c4386eSCy Schubert     char *expected_alpn_protocol;
198*e0c4386eSCy Schubert     /* Whether the second handshake is resumed or a full handshake (boolean). */
199*e0c4386eSCy Schubert     int resumption_expected;
200*e0c4386eSCy Schubert     /* Expected temporary key type */
201*e0c4386eSCy Schubert     int expected_tmp_key_type;
202*e0c4386eSCy Schubert     /* Expected server certificate key type */
203*e0c4386eSCy Schubert     int expected_server_cert_type;
204*e0c4386eSCy Schubert     /* Expected server signing hash */
205*e0c4386eSCy Schubert     int expected_server_sign_hash;
206*e0c4386eSCy Schubert     /* Expected server signature type */
207*e0c4386eSCy Schubert     int expected_server_sign_type;
208*e0c4386eSCy Schubert     /* Expected server CA names */
209*e0c4386eSCy Schubert     STACK_OF(X509_NAME) *expected_server_ca_names;
210*e0c4386eSCy Schubert     /* Expected client certificate key type */
211*e0c4386eSCy Schubert     int expected_client_cert_type;
212*e0c4386eSCy Schubert     /* Expected client signing hash */
213*e0c4386eSCy Schubert     int expected_client_sign_hash;
214*e0c4386eSCy Schubert     /* Expected client signature type */
215*e0c4386eSCy Schubert     int expected_client_sign_type;
216*e0c4386eSCy Schubert     /* Expected CA names for client auth */
217*e0c4386eSCy Schubert     STACK_OF(X509_NAME) *expected_client_ca_names;
218*e0c4386eSCy Schubert     /* Whether to use SCTP for the transport */
219*e0c4386eSCy Schubert     int use_sctp;
220*e0c4386eSCy Schubert     /* Enable SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG on client side */
221*e0c4386eSCy Schubert     int enable_client_sctp_label_bug;
222*e0c4386eSCy Schubert     /* Enable SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG on server side */
223*e0c4386eSCy Schubert     int enable_server_sctp_label_bug;
224*e0c4386eSCy Schubert     /* Whether to expect a session id from the server */
225*e0c4386eSCy Schubert     ssl_session_id_t session_id_expected;
226*e0c4386eSCy Schubert     char *expected_cipher;
227*e0c4386eSCy Schubert     /* Expected Session Ticket Application Data */
228*e0c4386eSCy Schubert     char *expected_session_ticket_app_data;
229*e0c4386eSCy Schubert 
230*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx;
231*e0c4386eSCy Schubert } SSL_TEST_CTX;
232*e0c4386eSCy Schubert 
233*e0c4386eSCy Schubert const char *ssl_test_result_name(ssl_test_result_t result);
234*e0c4386eSCy Schubert const char *ssl_alert_name(int alert);
235*e0c4386eSCy Schubert const char *ssl_protocol_name(int protocol);
236*e0c4386eSCy Schubert const char *ssl_verify_callback_name(ssl_verify_callback_t verify_callback);
237*e0c4386eSCy Schubert const char *ssl_servername_name(ssl_servername_t server);
238*e0c4386eSCy Schubert const char *ssl_servername_callback_name(ssl_servername_callback_t
239*e0c4386eSCy Schubert                                          servername_callback);
240*e0c4386eSCy Schubert const char *ssl_session_ticket_name(ssl_session_ticket_t server);
241*e0c4386eSCy Schubert const char *ssl_session_id_name(ssl_session_id_t server);
242*e0c4386eSCy Schubert const char *ssl_test_method_name(ssl_test_method_t method);
243*e0c4386eSCy Schubert const char *ssl_handshake_mode_name(ssl_handshake_mode_t mode);
244*e0c4386eSCy Schubert const char *ssl_ct_validation_name(ssl_ct_validation_t mode);
245*e0c4386eSCy Schubert const char *ssl_certstatus_name(ssl_cert_status_t cert_status);
246*e0c4386eSCy Schubert const char *ssl_max_fragment_len_name(int MFL_mode);
247*e0c4386eSCy Schubert 
248*e0c4386eSCy Schubert /*
249*e0c4386eSCy Schubert  * Load the test case context from |conf|.
250*e0c4386eSCy Schubert  * See test/README.ssltest.md for details on the conf file format.
251*e0c4386eSCy Schubert  */
252*e0c4386eSCy Schubert SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section,
253*e0c4386eSCy Schubert                                   OSSL_LIB_CTX *libctx);
254*e0c4386eSCy Schubert 
255*e0c4386eSCy Schubert SSL_TEST_CTX *SSL_TEST_CTX_new(OSSL_LIB_CTX *libctx);
256*e0c4386eSCy Schubert 
257*e0c4386eSCy Schubert void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx);
258*e0c4386eSCy Schubert 
259*e0c4386eSCy Schubert #endif  /* OSSL_TEST_SSL_TEST_CTX_H */
260