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