Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1 | /* |
Matt Caswell | 6738bf1 | 2018-02-13 12:51:29 +0000 | [diff] [blame] | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 3 | * |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 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 |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 7 | * https://www.openssl.org/source/license.html |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <openssl/bio.h> |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 13 | #include <openssl/x509_vfy.h> |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 14 | #include <openssl/ssl.h> |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 15 | #ifndef OPENSSL_NO_SRP |
| 16 | #include <openssl/srp.h> |
| 17 | #endif |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 18 | |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 19 | #include "../ssl/ssl_locl.h" |
Matt Caswell | f7d1d2a | 2017-08-24 12:25:09 +0100 | [diff] [blame] | 20 | #include "internal/sockets.h" |
Rich Salz | 0e97f1e | 2017-08-21 17:22:19 -0400 | [diff] [blame] | 21 | #include "internal/nelem.h" |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 22 | #include "handshake_helper.h" |
Emilia Kasper | d61f007 | 2016-08-09 17:03:23 +0200 | [diff] [blame] | 23 | #include "testutil.h" |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 24 | |
Kurt Roeckx | 3cb7c5c | 2018-05-09 17:09:50 +0200 | [diff] [blame] | 25 | HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void) |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 26 | { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 27 | HANDSHAKE_RESULT *ret; |
| 28 | |
| 29 | TEST_ptr(ret = OPENSSL_zalloc(sizeof(*ret))); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result) |
| 34 | { |
Emilia Kasper | 2f35e6a | 2016-08-09 17:08:59 +0200 | [diff] [blame] | 35 | if (result == NULL) |
| 36 | return; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 37 | OPENSSL_free(result->client_npn_negotiated); |
| 38 | OPENSSL_free(result->server_npn_negotiated); |
| 39 | OPENSSL_free(result->client_alpn_negotiated); |
| 40 | OPENSSL_free(result->server_alpn_negotiated); |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 41 | OPENSSL_free(result->result_session_ticket_app_data); |
Dr. Stephen Henson | f15b50c | 2017-03-31 22:35:28 +0100 | [diff] [blame] | 42 | sk_X509_NAME_pop_free(result->server_ca_names, X509_NAME_free); |
Dr. Stephen Henson | 2e21539 | 2017-03-15 16:07:07 +0000 | [diff] [blame] | 43 | sk_X509_NAME_pop_free(result->client_ca_names, X509_NAME_free); |
Todd Short | e1c7871 | 2015-12-21 15:19:29 -0500 | [diff] [blame] | 44 | OPENSSL_free(result->cipher); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 45 | OPENSSL_free(result); |
| 46 | } |
| 47 | |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 48 | /* |
| 49 | * Since there appears to be no way to extract the sent/received alert |
| 50 | * from the SSL object directly, we use the info callback and stash |
| 51 | * the result in ex_data. |
| 52 | */ |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 53 | typedef struct handshake_ex_data_st { |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 54 | int alert_sent; |
Emilia Kasper | dd8e5a5 | 2016-08-12 14:29:24 +0200 | [diff] [blame] | 55 | int num_fatal_alerts_sent; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 56 | int alert_received; |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 57 | int session_ticket_do_not_call; |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 58 | ssl_servername_t servername; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 59 | } HANDSHAKE_EX_DATA; |
| 60 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 61 | typedef struct ctx_data_st { |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 62 | unsigned char *npn_protocols; |
| 63 | size_t npn_protocols_len; |
| 64 | unsigned char *alpn_protocols; |
| 65 | size_t alpn_protocols_len; |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 66 | char *srp_user; |
| 67 | char *srp_password; |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 68 | char *session_ticket_app_data; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 69 | } CTX_DATA; |
| 70 | |
| 71 | /* |ctx_data| itself is stack-allocated. */ |
| 72 | static void ctx_data_free_data(CTX_DATA *ctx_data) |
| 73 | { |
| 74 | OPENSSL_free(ctx_data->npn_protocols); |
| 75 | ctx_data->npn_protocols = NULL; |
| 76 | OPENSSL_free(ctx_data->alpn_protocols); |
| 77 | ctx_data->alpn_protocols = NULL; |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 78 | OPENSSL_free(ctx_data->srp_user); |
| 79 | ctx_data->srp_user = NULL; |
| 80 | OPENSSL_free(ctx_data->srp_password); |
| 81 | ctx_data->srp_password = NULL; |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 82 | OPENSSL_free(ctx_data->session_ticket_app_data); |
| 83 | ctx_data->session_ticket_app_data = NULL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 86 | static int ex_data_idx; |
| 87 | |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 88 | static void info_cb(const SSL *s, int where, int ret) |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 89 | { |
| 90 | if (where & SSL_CB_ALERT) { |
| 91 | HANDSHAKE_EX_DATA *ex_data = |
| 92 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); |
| 93 | if (where & SSL_CB_WRITE) { |
| 94 | ex_data->alert_sent = ret; |
Emilia Kasper | dd8e5a5 | 2016-08-12 14:29:24 +0200 | [diff] [blame] | 95 | if (strcmp(SSL_alert_type_string(ret), "F") == 0 |
| 96 | || strcmp(SSL_alert_desc_string(ret), "CN") == 0) |
| 97 | ex_data->num_fatal_alerts_sent++; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 98 | } else { |
| 99 | ex_data->alert_received = ret; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 104 | /* Select the appropriate server CTX. |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 105 | * Returns SSL_TLSEXT_ERR_OK if a match was found. |
| 106 | * If |ignore| is 1, returns SSL_TLSEXT_ERR_NOACK on mismatch. |
| 107 | * Otherwise, returns SSL_TLSEXT_ERR_ALERT_FATAL on mismatch. |
| 108 | * An empty SNI extension also returns SSL_TSLEXT_ERR_NOACK. |
| 109 | */ |
| 110 | static int select_server_ctx(SSL *s, void *arg, int ignore) |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 111 | { |
| 112 | const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 113 | HANDSHAKE_EX_DATA *ex_data = |
| 114 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); |
| 115 | |
| 116 | if (servername == NULL) { |
| 117 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; |
| 118 | return SSL_TLSEXT_ERR_NOACK; |
| 119 | } |
| 120 | |
| 121 | if (strcmp(servername, "server2") == 0) { |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 122 | SSL_CTX *new_ctx = (SSL_CTX*)arg; |
| 123 | SSL_set_SSL_CTX(s, new_ctx); |
| 124 | /* |
| 125 | * Copy over all the SSL_CTX options - reasonable behavior |
| 126 | * allows testing of cases where the options between two |
| 127 | * contexts differ/conflict |
| 128 | */ |
| 129 | SSL_clear_options(s, 0xFFFFFFFFL); |
| 130 | SSL_set_options(s, SSL_CTX_get_options(new_ctx)); |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 131 | |
| 132 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; |
| 133 | return SSL_TLSEXT_ERR_OK; |
| 134 | } else if (strcmp(servername, "server1") == 0) { |
| 135 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; |
| 136 | return SSL_TLSEXT_ERR_OK; |
| 137 | } else if (ignore) { |
| 138 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; |
| 139 | return SSL_TLSEXT_ERR_NOACK; |
| 140 | } else { |
| 141 | /* Don't set an explicit alert, to test library defaults. */ |
| 142 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 143 | } |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 144 | } |
| 145 | |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 146 | static int client_hello_select_server_ctx(SSL *s, void *arg, int ignore) |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 147 | { |
| 148 | const char *servername; |
| 149 | const unsigned char *p; |
| 150 | size_t len, remaining; |
| 151 | HANDSHAKE_EX_DATA *ex_data = |
| 152 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); |
| 153 | |
| 154 | /* |
| 155 | * The server_name extension was given too much extensibility when it |
| 156 | * was written, so parsing the normal case is a bit complex. |
| 157 | */ |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 158 | if (!SSL_client_hello_get0_ext(s, TLSEXT_TYPE_server_name, &p, |
| 159 | &remaining) || |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 160 | remaining <= 2) |
| 161 | return 0; |
| 162 | /* Extract the length of the supplied list of names. */ |
Benjamin Kaduk | c4604e9 | 2017-09-01 12:37:05 -0500 | [diff] [blame] | 163 | len = (*(p++) << 8); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 164 | len += *(p++); |
| 165 | if (len + 2 != remaining) |
| 166 | return 0; |
| 167 | remaining = len; |
| 168 | /* |
| 169 | * The list in practice only has a single element, so we only consider |
| 170 | * the first one. |
| 171 | */ |
| 172 | if (remaining == 0 || *p++ != TLSEXT_NAMETYPE_host_name) |
| 173 | return 0; |
| 174 | remaining--; |
| 175 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 176 | if (remaining <= 2) |
| 177 | return 0; |
Benjamin Kaduk | c4604e9 | 2017-09-01 12:37:05 -0500 | [diff] [blame] | 178 | len = (*(p++) << 8); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 179 | len += *(p++); |
| 180 | if (len + 2 > remaining) |
| 181 | return 0; |
| 182 | remaining = len; |
| 183 | servername = (const char *)p; |
| 184 | |
| 185 | if (len == strlen("server2") && strncmp(servername, "server2", len) == 0) { |
| 186 | SSL_CTX *new_ctx = arg; |
| 187 | SSL_set_SSL_CTX(s, new_ctx); |
| 188 | /* |
| 189 | * Copy over all the SSL_CTX options - reasonable behavior |
| 190 | * allows testing of cases where the options between two |
| 191 | * contexts differ/conflict |
| 192 | */ |
| 193 | SSL_clear_options(s, 0xFFFFFFFFL); |
| 194 | SSL_set_options(s, SSL_CTX_get_options(new_ctx)); |
| 195 | |
| 196 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; |
| 197 | return 1; |
| 198 | } else if (len == strlen("server1") && |
| 199 | strncmp(servername, "server1", len) == 0) { |
| 200 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; |
| 201 | return 1; |
| 202 | } else if (ignore) { |
| 203 | ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; |
| 204 | return 1; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 208 | /* |
| 209 | * (RFC 6066): |
| 210 | * If the server understood the ClientHello extension but |
| 211 | * does not recognize the server name, the server SHOULD take one of two |
| 212 | * actions: either abort the handshake by sending a fatal-level |
| 213 | * unrecognized_name(112) alert or continue the handshake. |
| 214 | * |
| 215 | * This behaviour is up to the application to configure; we test both |
| 216 | * configurations to ensure the state machine propagates the result |
| 217 | * correctly. |
| 218 | */ |
| 219 | static int servername_ignore_cb(SSL *s, int *ad, void *arg) |
| 220 | { |
| 221 | return select_server_ctx(s, arg, 1); |
| 222 | } |
| 223 | |
| 224 | static int servername_reject_cb(SSL *s, int *ad, void *arg) |
| 225 | { |
| 226 | return select_server_ctx(s, arg, 0); |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 227 | } |
| 228 | |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 229 | static int client_hello_ignore_cb(SSL *s, int *al, void *arg) |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 230 | { |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 231 | if (!client_hello_select_server_ctx(s, arg, 1)) { |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 232 | *al = SSL_AD_UNRECOGNIZED_NAME; |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 233 | return SSL_CLIENT_HELLO_ERROR; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 234 | } |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 235 | return SSL_CLIENT_HELLO_SUCCESS; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 236 | } |
| 237 | |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 238 | static int client_hello_reject_cb(SSL *s, int *al, void *arg) |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 239 | { |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 240 | if (!client_hello_select_server_ctx(s, arg, 0)) { |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 241 | *al = SSL_AD_UNRECOGNIZED_NAME; |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 242 | return SSL_CLIENT_HELLO_ERROR; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 243 | } |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 244 | return SSL_CLIENT_HELLO_SUCCESS; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 245 | } |
| 246 | |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 247 | static int client_hello_nov12_cb(SSL *s, int *al, void *arg) |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 248 | { |
| 249 | int ret; |
| 250 | unsigned int v; |
| 251 | const unsigned char *p; |
| 252 | |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 253 | v = SSL_client_hello_get0_legacy_version(s); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 254 | if (v > TLS1_2_VERSION || v < SSL3_VERSION) { |
| 255 | *al = SSL_AD_PROTOCOL_VERSION; |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 256 | return SSL_CLIENT_HELLO_ERROR; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 257 | } |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 258 | (void)SSL_client_hello_get0_session_id(s, &p); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 259 | if (p == NULL || |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 260 | SSL_client_hello_get0_random(s, &p) == 0 || |
| 261 | SSL_client_hello_get0_ciphers(s, &p) == 0 || |
| 262 | SSL_client_hello_get0_compression_methods(s, &p) == 0) { |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 263 | *al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 264 | return SSL_CLIENT_HELLO_ERROR; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 265 | } |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 266 | ret = client_hello_select_server_ctx(s, arg, 0); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 267 | SSL_set_max_proto_version(s, TLS1_1_VERSION); |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 268 | if (!ret) { |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 269 | *al = SSL_AD_UNRECOGNIZED_NAME; |
David Benjamin | f1b97da | 2017-09-07 18:53:05 -0400 | [diff] [blame] | 270 | return SSL_CLIENT_HELLO_ERROR; |
| 271 | } |
| 272 | return SSL_CLIENT_HELLO_SUCCESS; |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 273 | } |
| 274 | |
Matt Caswell | 767ccc3 | 2016-08-30 14:20:18 +0100 | [diff] [blame] | 275 | static unsigned char dummy_ocsp_resp_good_val = 0xff; |
| 276 | static unsigned char dummy_ocsp_resp_bad_val = 0xfe; |
| 277 | |
| 278 | static int server_ocsp_cb(SSL *s, void *arg) |
| 279 | { |
| 280 | unsigned char *resp; |
| 281 | |
| 282 | resp = OPENSSL_malloc(1); |
| 283 | if (resp == NULL) |
| 284 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 285 | /* |
| 286 | * For the purposes of testing we just send back a dummy OCSP response |
| 287 | */ |
| 288 | *resp = *(unsigned char *)arg; |
| 289 | if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1)) |
| 290 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 291 | |
| 292 | return SSL_TLSEXT_ERR_OK; |
| 293 | } |
| 294 | |
| 295 | static int client_ocsp_cb(SSL *s, void *arg) |
| 296 | { |
| 297 | const unsigned char *resp; |
| 298 | int len; |
| 299 | |
| 300 | len = SSL_get_tlsext_status_ocsp_resp(s, &resp); |
| 301 | if (len != 1 || *resp != dummy_ocsp_resp_good_val) |
| 302 | return 0; |
| 303 | |
| 304 | return 1; |
| 305 | } |
| 306 | |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 307 | static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) { |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 308 | X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 309 | return 0; |
| 310 | } |
| 311 | |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 312 | static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) { |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 316 | static int broken_session_ticket_cb(SSL *s, unsigned char *key_name, unsigned char *iv, |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 317 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc) |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 318 | { |
| 319 | return 0; |
| 320 | } |
| 321 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 322 | static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name, |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 323 | unsigned char *iv, |
| 324 | EVP_CIPHER_CTX *ctx, |
| 325 | HMAC_CTX *hctx, int enc) |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 326 | { |
| 327 | HANDSHAKE_EX_DATA *ex_data = |
| 328 | (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); |
| 329 | ex_data->session_ticket_do_not_call = 1; |
| 330 | return 0; |
| 331 | } |
| 332 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 333 | /* Parse the comma-separated list into TLS format. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 334 | static int parse_protos(const char *protos, unsigned char **out, size_t *outlen) |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 335 | { |
| 336 | size_t len, i, prefix; |
| 337 | |
| 338 | len = strlen(protos); |
| 339 | |
| 340 | /* Should never have reuse. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 341 | if (!TEST_ptr_null(*out) |
| 342 | /* Test values are small, so we omit length limit checks. */ |
| 343 | || !TEST_ptr(*out = OPENSSL_malloc(len + 1))) |
| 344 | return 0; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 345 | *outlen = len + 1; |
| 346 | |
| 347 | /* |
| 348 | * foo => '3', 'f', 'o', 'o' |
| 349 | * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r' |
| 350 | */ |
| 351 | memcpy(*out + 1, protos, len); |
| 352 | |
| 353 | prefix = 0; |
| 354 | i = prefix + 1; |
| 355 | while (i <= len) { |
| 356 | if ((*out)[i] == ',') { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 357 | if (!TEST_int_gt(i - 1, prefix)) |
| 358 | goto err; |
Andy Polyakov | 3a63c0e | 2017-11-11 22:23:12 +0100 | [diff] [blame] | 359 | (*out)[prefix] = (unsigned char)(i - 1 - prefix); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 360 | prefix = i; |
| 361 | } |
| 362 | i++; |
| 363 | } |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 364 | if (!TEST_int_gt(len, prefix)) |
| 365 | goto err; |
Andy Polyakov | 3a63c0e | 2017-11-11 22:23:12 +0100 | [diff] [blame] | 366 | (*out)[prefix] = (unsigned char)(len - prefix); |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 367 | return 1; |
| 368 | |
| 369 | err: |
| 370 | OPENSSL_free(*out); |
| 371 | *out = NULL; |
| 372 | return 0; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Emilia Kasper | 7b7cea6 | 2016-08-05 17:17:00 +0200 | [diff] [blame] | 375 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 376 | /* |
| 377 | * The client SHOULD select the first protocol advertised by the server that it |
| 378 | * also supports. In the event that the client doesn't support any of server's |
| 379 | * protocols, or the server doesn't advertise any, it SHOULD select the first |
| 380 | * protocol that it supports. |
| 381 | */ |
| 382 | static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen, |
| 383 | const unsigned char *in, unsigned int inlen, |
| 384 | void *arg) |
| 385 | { |
| 386 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); |
| 387 | int ret; |
| 388 | |
| 389 | ret = SSL_select_next_proto(out, outlen, in, inlen, |
| 390 | ctx_data->npn_protocols, |
| 391 | ctx_data->npn_protocols_len); |
| 392 | /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 393 | return TEST_true(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP) |
| 394 | ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int server_npn_cb(SSL *s, const unsigned char **data, |
| 398 | unsigned int *len, void *arg) |
| 399 | { |
| 400 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); |
| 401 | *data = ctx_data->npn_protocols; |
| 402 | *len = ctx_data->npn_protocols_len; |
| 403 | return SSL_TLSEXT_ERR_OK; |
| 404 | } |
Emilia Kasper | 7b7cea6 | 2016-08-05 17:17:00 +0200 | [diff] [blame] | 405 | #endif |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * The server SHOULD select the most highly preferred protocol that it supports |
| 409 | * and that is also advertised by the client. In the event that the server |
| 410 | * supports no protocols that the client advertises, then the server SHALL |
| 411 | * respond with a fatal "no_application_protocol" alert. |
| 412 | */ |
| 413 | static int server_alpn_cb(SSL *s, const unsigned char **out, |
| 414 | unsigned char *outlen, const unsigned char *in, |
| 415 | unsigned int inlen, void *arg) |
| 416 | { |
| 417 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); |
| 418 | int ret; |
| 419 | |
| 420 | /* SSL_select_next_proto isn't const-correct... */ |
| 421 | unsigned char *tmp_out; |
| 422 | |
| 423 | /* |
| 424 | * The result points either to |in| or to |ctx_data->alpn_protocols|. |
| 425 | * The callback is allowed to point to |in| or to a long-lived buffer, |
| 426 | * so we can return directly without storing a copy. |
| 427 | */ |
| 428 | ret = SSL_select_next_proto(&tmp_out, outlen, |
| 429 | ctx_data->alpn_protocols, |
| 430 | ctx_data->alpn_protocols_len, in, inlen); |
| 431 | |
| 432 | *out = tmp_out; |
| 433 | /* Unlike NPN, we don't tolerate a mismatch. */ |
| 434 | return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK |
Benjamin Kaduk | 8313a78 | 2017-02-07 16:23:16 -0600 | [diff] [blame] | 435 | : SSL_TLSEXT_ERR_ALERT_FATAL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 436 | } |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 437 | |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 438 | #ifndef OPENSSL_NO_SRP |
| 439 | static char *client_srp_cb(SSL *s, void *arg) |
| 440 | { |
| 441 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); |
| 442 | return OPENSSL_strdup(ctx_data->srp_password); |
| 443 | } |
| 444 | |
| 445 | static int server_srp_cb(SSL *s, int *ad, void *arg) |
| 446 | { |
| 447 | CTX_DATA *ctx_data = (CTX_DATA*)(arg); |
| 448 | if (strcmp(ctx_data->srp_user, SSL_get_srp_username(s)) != 0) |
| 449 | return SSL3_AL_FATAL; |
| 450 | if (SSL_set_srp_server_param_pw(s, ctx_data->srp_user, |
| 451 | ctx_data->srp_password, |
| 452 | "2048" /* known group */) < 0) { |
| 453 | *ad = SSL_AD_INTERNAL_ERROR; |
| 454 | return SSL3_AL_FATAL; |
| 455 | } |
| 456 | return SSL_ERROR_NONE; |
| 457 | } |
| 458 | #endif /* !OPENSSL_NO_SRP */ |
| 459 | |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 460 | static int generate_session_ticket_cb(SSL *s, void *arg) |
| 461 | { |
| 462 | CTX_DATA *server_ctx_data = arg; |
| 463 | SSL_SESSION *ss = SSL_get_session(s); |
| 464 | char *app_data = server_ctx_data->session_ticket_app_data; |
| 465 | |
| 466 | if (ss == NULL || app_data == NULL) |
| 467 | return 0; |
| 468 | |
| 469 | return SSL_SESSION_set1_ticket_appdata(ss, app_data, strlen(app_data)); |
| 470 | } |
| 471 | |
Matt Caswell | 61fb592 | 2018-05-09 18:22:36 +0100 | [diff] [blame] | 472 | static int decrypt_session_ticket_cb(SSL *s, SSL_SESSION *ss, |
| 473 | const unsigned char *keyname, |
| 474 | size_t keyname_len, |
| 475 | SSL_TICKET_STATUS status, |
| 476 | void *arg) |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 477 | { |
Matt Caswell | 61fb592 | 2018-05-09 18:22:36 +0100 | [diff] [blame] | 478 | switch (status) { |
| 479 | case SSL_TICKET_EMPTY: |
| 480 | case SSL_TICKET_NO_DECRYPT: |
| 481 | return SSL_TICKET_RETURN_IGNORE_RENEW; |
| 482 | case SSL_TICKET_SUCCESS: |
| 483 | return SSL_TICKET_RETURN_USE; |
| 484 | case SSL_TICKET_SUCCESS_RENEW: |
| 485 | return SSL_TICKET_RETURN_USE_RENEW; |
| 486 | default: |
| 487 | break; |
| 488 | } |
| 489 | return SSL_TICKET_RETURN_ABORT; |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 492 | /* |
| 493 | * Configure callbacks and other properties that can't be set directly |
| 494 | * in the server/client CONF. |
| 495 | */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 496 | static int configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, |
| 497 | SSL_CTX *client_ctx, |
| 498 | const SSL_TEST_CTX *test, |
| 499 | const SSL_TEST_EXTRA_CONF *extra, |
| 500 | CTX_DATA *server_ctx_data, |
| 501 | CTX_DATA *server2_ctx_data, |
| 502 | CTX_DATA *client_ctx_data) |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 503 | { |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 504 | unsigned char *ticket_keys; |
| 505 | size_t ticket_key_len; |
| 506 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 507 | if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server_ctx, |
| 508 | test->max_fragment_size), 1)) |
| 509 | goto err; |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 510 | if (server2_ctx != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 511 | if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server2_ctx, |
| 512 | test->max_fragment_size), |
| 513 | 1)) |
| 514 | goto err; |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 515 | } |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 516 | if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(client_ctx, |
| 517 | test->max_fragment_size), 1)) |
| 518 | goto err; |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 519 | |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 520 | switch (extra->client.verify_callback) { |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 521 | case SSL_TEST_VERIFY_ACCEPT_ALL: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 522 | SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb, NULL); |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 523 | break; |
| 524 | case SSL_TEST_VERIFY_REJECT_ALL: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 525 | SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, NULL); |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 526 | break; |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 527 | case SSL_TEST_VERIFY_NONE: |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 528 | break; |
| 529 | } |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 530 | |
FdaSilvaYY | cf72c75 | 2017-11-05 17:46:48 +0100 | [diff] [blame] | 531 | switch (extra->client.max_fragment_len_mode) { |
| 532 | case TLSEXT_max_fragment_length_512: |
| 533 | case TLSEXT_max_fragment_length_1024: |
| 534 | case TLSEXT_max_fragment_length_2048: |
| 535 | case TLSEXT_max_fragment_length_4096: |
| 536 | case TLSEXT_max_fragment_length_DISABLED: |
Benjamin Kaduk | 88e09fe | 2017-12-07 14:23:35 -0600 | [diff] [blame] | 537 | SSL_CTX_set_tlsext_max_fragment_length( |
| 538 | client_ctx, extra->client.max_fragment_len_mode); |
FdaSilvaYY | cf72c75 | 2017-11-05 17:46:48 +0100 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 542 | /* |
| 543 | * Link the two contexts for SNI purposes. |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 544 | * Also do ClientHello callbacks here, as setting both ClientHello and SNI |
| 545 | * is bad. |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 546 | */ |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 547 | switch (extra->server.servername_callback) { |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 548 | case SSL_TEST_SERVERNAME_IGNORE_MISMATCH: |
| 549 | SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb); |
| 550 | SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); |
| 551 | break; |
| 552 | case SSL_TEST_SERVERNAME_REJECT_MISMATCH: |
| 553 | SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb); |
| 554 | SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); |
| 555 | break; |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 556 | case SSL_TEST_SERVERNAME_CB_NONE: |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 557 | break; |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 558 | case SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH: |
| 559 | SSL_CTX_set_client_hello_cb(server_ctx, client_hello_ignore_cb, server2_ctx); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 560 | break; |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 561 | case SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH: |
| 562 | SSL_CTX_set_client_hello_cb(server_ctx, client_hello_reject_cb, server2_ctx); |
Benjamin Kaduk | 80de0c5 | 2017-01-31 16:06:30 -0600 | [diff] [blame] | 563 | break; |
David Benjamin | a9c0d8b | 2017-09-07 18:39:40 -0400 | [diff] [blame] | 564 | case SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12: |
| 565 | SSL_CTX_set_client_hello_cb(server_ctx, client_hello_nov12_cb, server2_ctx); |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Matt Caswell | 767ccc3 | 2016-08-30 14:20:18 +0100 | [diff] [blame] | 568 | if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) { |
| 569 | SSL_CTX_set_tlsext_status_type(client_ctx, TLSEXT_STATUSTYPE_ocsp); |
| 570 | SSL_CTX_set_tlsext_status_cb(client_ctx, client_ocsp_cb); |
| 571 | SSL_CTX_set_tlsext_status_arg(client_ctx, NULL); |
| 572 | SSL_CTX_set_tlsext_status_cb(server_ctx, server_ocsp_cb); |
| 573 | SSL_CTX_set_tlsext_status_arg(server_ctx, |
| 574 | ((extra->server.cert_status == SSL_TEST_CERT_STATUS_GOOD_RESPONSE) |
| 575 | ? &dummy_ocsp_resp_good_val : &dummy_ocsp_resp_bad_val)); |
| 576 | } |
| 577 | |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 578 | /* |
| 579 | * The initial_ctx/session_ctx always handles the encrypt/decrypt of the |
| 580 | * session ticket. This ticket_key callback is assigned to the second |
| 581 | * session (assigned via SNI), and should never be invoked |
| 582 | */ |
Emilia Kasper | d2b23cd | 2016-06-20 17:20:25 +0200 | [diff] [blame] | 583 | if (server2_ctx != NULL) |
| 584 | SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx, |
| 585 | do_not_call_session_ticket_cb); |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 586 | |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 587 | if (extra->server.broken_session_ticket) { |
Richard Levitte | a8c82fa | 2016-06-14 00:44:29 +0200 | [diff] [blame] | 588 | SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_cb); |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 589 | } |
Ben Laurie | 620c6ad | 2016-07-31 11:42:04 +0100 | [diff] [blame] | 590 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 591 | if (extra->server.npn_protocols != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 592 | if (!TEST_true(parse_protos(extra->server.npn_protocols, |
| 593 | &server_ctx_data->npn_protocols, |
| 594 | &server_ctx_data->npn_protocols_len))) |
| 595 | goto err; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 596 | SSL_CTX_set_npn_advertised_cb(server_ctx, server_npn_cb, |
| 597 | server_ctx_data); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 598 | } |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 599 | if (extra->server2.npn_protocols != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 600 | if (!TEST_true(parse_protos(extra->server2.npn_protocols, |
| 601 | &server2_ctx_data->npn_protocols, |
| 602 | &server2_ctx_data->npn_protocols_len)) |
| 603 | || !TEST_ptr(server2_ctx)) |
| 604 | goto err; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 605 | SSL_CTX_set_npn_advertised_cb(server2_ctx, server_npn_cb, |
| 606 | server2_ctx_data); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 607 | } |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 608 | if (extra->client.npn_protocols != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 609 | if (!TEST_true(parse_protos(extra->client.npn_protocols, |
| 610 | &client_ctx_data->npn_protocols, |
| 611 | &client_ctx_data->npn_protocols_len))) |
| 612 | goto err; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 613 | SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb, |
| 614 | client_ctx_data); |
| 615 | } |
Emilia Kasper | 7b7cea6 | 2016-08-05 17:17:00 +0200 | [diff] [blame] | 616 | #endif |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 617 | if (extra->server.alpn_protocols != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 618 | if (!TEST_true(parse_protos(extra->server.alpn_protocols, |
| 619 | &server_ctx_data->alpn_protocols, |
| 620 | &server_ctx_data->alpn_protocols_len))) |
| 621 | goto err; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 622 | SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data); |
| 623 | } |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 624 | if (extra->server2.alpn_protocols != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 625 | if (!TEST_ptr(server2_ctx) |
| 626 | || !TEST_true(parse_protos(extra->server2.alpn_protocols, |
| 627 | &server2_ctx_data->alpn_protocols, |
| 628 | &server2_ctx_data->alpn_protocols_len |
| 629 | ))) |
| 630 | goto err; |
| 631 | SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, |
| 632 | server2_ctx_data); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 633 | } |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 634 | if (extra->client.alpn_protocols != NULL) { |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 635 | unsigned char *alpn_protos = NULL; |
| 636 | size_t alpn_protos_len; |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 637 | if (!TEST_true(parse_protos(extra->client.alpn_protocols, |
| 638 | &alpn_protos, &alpn_protos_len)) |
| 639 | /* Reversed return value convention... */ |
| 640 | || !TEST_int_eq(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos, |
| 641 | alpn_protos_len), 0)) |
| 642 | goto err; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 643 | OPENSSL_free(alpn_protos); |
| 644 | } |
Emilia Kasper | 7b7cea6 | 2016-08-05 17:17:00 +0200 | [diff] [blame] | 645 | |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 646 | if (extra->server.session_ticket_app_data != NULL) { |
| 647 | server_ctx_data->session_ticket_app_data = |
| 648 | OPENSSL_strdup(extra->server.session_ticket_app_data); |
| 649 | SSL_CTX_set_session_ticket_cb(server_ctx, generate_session_ticket_cb, |
| 650 | decrypt_session_ticket_cb, server_ctx_data); |
| 651 | } |
| 652 | if (extra->server2.session_ticket_app_data != NULL) { |
| 653 | if (!TEST_ptr(server2_ctx)) |
| 654 | goto err; |
| 655 | server2_ctx_data->session_ticket_app_data = |
| 656 | OPENSSL_strdup(extra->server2.session_ticket_app_data); |
| 657 | SSL_CTX_set_session_ticket_cb(server2_ctx, NULL, |
| 658 | decrypt_session_ticket_cb, server2_ctx_data); |
| 659 | } |
| 660 | |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 661 | /* |
| 662 | * Use fixed session ticket keys so that we can decrypt a ticket created with |
| 663 | * one CTX in another CTX. Don't address server2 for the moment. |
| 664 | */ |
| 665 | ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0); |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 666 | if (!TEST_ptr(ticket_keys = OPENSSL_zalloc(ticket_key_len)) |
| 667 | || !TEST_int_eq(SSL_CTX_set_tlsext_ticket_keys(server_ctx, |
| 668 | ticket_keys, |
| 669 | ticket_key_len), 1)) { |
| 670 | OPENSSL_free(ticket_keys); |
| 671 | goto err; |
| 672 | } |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 673 | OPENSSL_free(ticket_keys); |
Emilia Kasper | da085d2 | 2016-08-09 16:47:26 +0200 | [diff] [blame] | 674 | |
Emilia Kasper | be82f7b | 2016-08-10 18:36:47 +0200 | [diff] [blame] | 675 | /* The default log list includes EC keys, so CT can't work without EC. */ |
| 676 | #if !defined(OPENSSL_NO_CT) && !defined(OPENSSL_NO_EC) |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 677 | if (!TEST_true(SSL_CTX_set_default_ctlog_list_file(client_ctx))) |
| 678 | goto err; |
Emilia Kasper | da085d2 | 2016-08-09 16:47:26 +0200 | [diff] [blame] | 679 | switch (extra->client.ct_validation) { |
| 680 | case SSL_TEST_CT_VALIDATION_PERMISSIVE: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 681 | if (!TEST_true(SSL_CTX_enable_ct(client_ctx, |
| 682 | SSL_CT_VALIDATION_PERMISSIVE))) |
| 683 | goto err; |
Emilia Kasper | da085d2 | 2016-08-09 16:47:26 +0200 | [diff] [blame] | 684 | break; |
| 685 | case SSL_TEST_CT_VALIDATION_STRICT: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 686 | if (!TEST_true(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT))) |
| 687 | goto err; |
Emilia Kasper | da085d2 | 2016-08-09 16:47:26 +0200 | [diff] [blame] | 688 | break; |
| 689 | case SSL_TEST_CT_VALIDATION_NONE: |
| 690 | break; |
| 691 | } |
| 692 | #endif |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 693 | #ifndef OPENSSL_NO_SRP |
| 694 | if (extra->server.srp_user != NULL) { |
| 695 | SSL_CTX_set_srp_username_callback(server_ctx, server_srp_cb); |
| 696 | server_ctx_data->srp_user = OPENSSL_strdup(extra->server.srp_user); |
| 697 | server_ctx_data->srp_password = OPENSSL_strdup(extra->server.srp_password); |
| 698 | SSL_CTX_set_srp_cb_arg(server_ctx, server_ctx_data); |
| 699 | } |
| 700 | if (extra->server2.srp_user != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 701 | if (!TEST_ptr(server2_ctx)) |
| 702 | goto err; |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 703 | SSL_CTX_set_srp_username_callback(server2_ctx, server_srp_cb); |
| 704 | server2_ctx_data->srp_user = OPENSSL_strdup(extra->server2.srp_user); |
| 705 | server2_ctx_data->srp_password = OPENSSL_strdup(extra->server2.srp_password); |
| 706 | SSL_CTX_set_srp_cb_arg(server2_ctx, server2_ctx_data); |
| 707 | } |
| 708 | if (extra->client.srp_user != NULL) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 709 | if (!TEST_true(SSL_CTX_set_srp_username(client_ctx, |
| 710 | extra->client.srp_user))) |
| 711 | goto err; |
Emilia Kasper | ea1ecd9 | 2017-03-14 13:48:54 +0100 | [diff] [blame] | 712 | SSL_CTX_set_srp_client_pwd_callback(client_ctx, client_srp_cb); |
| 713 | client_ctx_data->srp_password = OPENSSL_strdup(extra->client.srp_password); |
| 714 | SSL_CTX_set_srp_cb_arg(client_ctx, client_ctx_data); |
| 715 | } |
| 716 | #endif /* !OPENSSL_NO_SRP */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 717 | return 1; |
| 718 | err: |
| 719 | return 0; |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 720 | } |
| 721 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 722 | /* Configure per-SSL callbacks and other properties. */ |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 723 | static void configure_handshake_ssl(SSL *server, SSL *client, |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 724 | const SSL_TEST_EXTRA_CONF *extra) |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 725 | { |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 726 | if (extra->client.servername != SSL_TEST_SERVERNAME_NONE) |
Emilia Kasper | 81fc33c | 2016-06-10 00:39:22 +0200 | [diff] [blame] | 727 | SSL_set_tlsext_host_name(client, |
Emilia Kasper | 9f48bba | 2016-07-21 16:29:48 +0200 | [diff] [blame] | 728 | ssl_servername_name(extra->client.servername)); |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 729 | if (extra->client.force_pha) |
| 730 | SSL_force_post_handshake_auth(client); |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 733 | /* The status for each connection phase. */ |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 734 | typedef enum { |
| 735 | PEER_SUCCESS, |
| 736 | PEER_RETRY, |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 737 | PEER_ERROR, |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 738 | PEER_WAITING, |
| 739 | PEER_TEST_FAILURE |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 740 | } peer_status_t; |
| 741 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 742 | /* An SSL object and associated read-write buffers. */ |
| 743 | typedef struct peer_st { |
| 744 | SSL *ssl; |
| 745 | /* Buffer lengths are int to match the SSL read/write API. */ |
| 746 | unsigned char *write_buf; |
| 747 | int write_buf_len; |
| 748 | unsigned char *read_buf; |
| 749 | int read_buf_len; |
| 750 | int bytes_to_write; |
| 751 | int bytes_to_read; |
| 752 | peer_status_t status; |
| 753 | } PEER; |
| 754 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 755 | static int create_peer(PEER *peer, SSL_CTX *ctx) |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 756 | { |
| 757 | static const int peer_buffer_size = 64 * 1024; |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 758 | SSL *ssl = NULL; |
| 759 | unsigned char *read_buf = NULL, *write_buf = NULL; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 760 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 761 | if (!TEST_ptr(ssl = SSL_new(ctx)) |
| 762 | || !TEST_ptr(write_buf = OPENSSL_zalloc(peer_buffer_size)) |
| 763 | || !TEST_ptr(read_buf = OPENSSL_zalloc(peer_buffer_size))) |
| 764 | goto err; |
| 765 | |
| 766 | peer->ssl = ssl; |
| 767 | peer->write_buf = write_buf; |
| 768 | peer->read_buf = read_buf; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 769 | peer->write_buf_len = peer->read_buf_len = peer_buffer_size; |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 770 | return 1; |
| 771 | err: |
| 772 | SSL_free(ssl); |
| 773 | OPENSSL_free(write_buf); |
| 774 | OPENSSL_free(read_buf); |
| 775 | return 0; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void peer_free_data(PEER *peer) |
| 779 | { |
| 780 | SSL_free(peer->ssl); |
| 781 | OPENSSL_free(peer->write_buf); |
| 782 | OPENSSL_free(peer->read_buf); |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Note that we could do the handshake transparently under an SSL_write, |
| 787 | * but separating the steps is more helpful for debugging test failures. |
| 788 | */ |
| 789 | static void do_handshake_step(PEER *peer) |
| 790 | { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 791 | if (!TEST_int_eq(peer->status, PEER_RETRY)) { |
| 792 | peer->status = PEER_TEST_FAILURE; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 793 | } else { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 794 | int ret = SSL_do_handshake(peer->ssl); |
| 795 | |
| 796 | if (ret == 1) { |
| 797 | peer->status = PEER_SUCCESS; |
| 798 | } else if (ret == 0) { |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 799 | peer->status = PEER_ERROR; |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 800 | } else { |
| 801 | int error = SSL_get_error(peer->ssl, ret); |
| 802 | /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */ |
| 803 | if (error != SSL_ERROR_WANT_READ) |
| 804 | peer->status = PEER_ERROR; |
| 805 | } |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
| 809 | /*- |
| 810 | * Send/receive some application data. The read-write sequence is |
| 811 | * Peer A: (R) W - first read will yield no data |
| 812 | * Peer B: R W |
| 813 | * ... |
| 814 | * Peer A: R W |
| 815 | * Peer B: R W |
| 816 | * Peer A: R |
| 817 | */ |
| 818 | static void do_app_data_step(PEER *peer) |
| 819 | { |
| 820 | int ret = 1, write_bytes; |
| 821 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 822 | if (!TEST_int_eq(peer->status, PEER_RETRY)) { |
| 823 | peer->status = PEER_TEST_FAILURE; |
| 824 | return; |
| 825 | } |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 826 | |
| 827 | /* We read everything available... */ |
| 828 | while (ret > 0 && peer->bytes_to_read) { |
| 829 | ret = SSL_read(peer->ssl, peer->read_buf, peer->read_buf_len); |
| 830 | if (ret > 0) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 831 | if (!TEST_int_le(ret, peer->bytes_to_read)) { |
| 832 | peer->status = PEER_TEST_FAILURE; |
| 833 | return; |
| 834 | } |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 835 | peer->bytes_to_read -= ret; |
| 836 | } else if (ret == 0) { |
| 837 | peer->status = PEER_ERROR; |
| 838 | return; |
| 839 | } else { |
| 840 | int error = SSL_get_error(peer->ssl, ret); |
| 841 | if (error != SSL_ERROR_WANT_READ) { |
| 842 | peer->status = PEER_ERROR; |
| 843 | return; |
| 844 | } /* Else continue with write. */ |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* ... but we only write one write-buffer-full of data. */ |
| 849 | write_bytes = peer->bytes_to_write < peer->write_buf_len ? peer->bytes_to_write : |
| 850 | peer->write_buf_len; |
| 851 | if (write_bytes) { |
| 852 | ret = SSL_write(peer->ssl, peer->write_buf, write_bytes); |
| 853 | if (ret > 0) { |
| 854 | /* SSL_write will only succeed with a complete write. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 855 | if (!TEST_int_eq(ret, write_bytes)) { |
| 856 | peer->status = PEER_TEST_FAILURE; |
| 857 | return; |
| 858 | } |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 859 | peer->bytes_to_write -= ret; |
| 860 | } else { |
| 861 | /* |
| 862 | * We should perhaps check for SSL_ERROR_WANT_READ/WRITE here |
| 863 | * but this doesn't yet occur with current app data sizes. |
| 864 | */ |
| 865 | peer->status = PEER_ERROR; |
| 866 | return; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * We could simply finish when there was nothing to read, and we have |
| 872 | * nothing left to write. But keeping track of the expected number of bytes |
| 873 | * to read gives us somewhat better guarantees that all data sent is in fact |
| 874 | * received. |
| 875 | */ |
| 876 | if (!peer->bytes_to_write && !peer->bytes_to_read) { |
| 877 | peer->status = PEER_SUCCESS; |
| 878 | } |
| 879 | } |
| 880 | |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 881 | static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer) |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 882 | { |
| 883 | int ret; |
| 884 | char buf; |
| 885 | |
Todd Short | 84344ef | 2017-05-12 09:02:41 -0400 | [diff] [blame] | 886 | if (peer->status == PEER_SUCCESS) { |
| 887 | /* |
| 888 | * We are a client that succeeded this step previously, but the server |
| 889 | * wanted to retry. Probably there is a no_renegotiation warning alert |
| 890 | * waiting for us. Attempt to continue the handshake. |
| 891 | */ |
| 892 | peer->status = PEER_RETRY; |
| 893 | do_handshake_step(peer); |
| 894 | return; |
| 895 | } |
Pauli | c2500f6 | 2017-07-13 07:37:01 +1000 | [diff] [blame] | 896 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 897 | if (!TEST_int_eq(peer->status, PEER_RETRY) |
| 898 | || !TEST_true(test_ctx->handshake_mode |
| 899 | == SSL_TEST_HANDSHAKE_RENEG_SERVER |
| 900 | || test_ctx->handshake_mode |
| 901 | == SSL_TEST_HANDSHAKE_RENEG_CLIENT |
| 902 | || test_ctx->handshake_mode |
| 903 | == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER |
| 904 | || test_ctx->handshake_mode |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 905 | == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT |
| 906 | || test_ctx->handshake_mode |
| 907 | == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH)) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 908 | peer->status = PEER_TEST_FAILURE; |
| 909 | return; |
| 910 | } |
Matt Caswell | 9b92f16 | 2017-02-15 09:25:52 +0000 | [diff] [blame] | 911 | |
| 912 | /* Reset the count of the amount of app data we need to read/write */ |
| 913 | peer->bytes_to_write = peer->bytes_to_read = test_ctx->app_data_size; |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 914 | |
| 915 | /* Check if we are the peer that is going to initiate */ |
| 916 | if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER |
| 917 | && SSL_is_server(peer->ssl)) |
| 918 | || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT |
| 919 | && !SSL_is_server(peer->ssl))) { |
| 920 | /* |
| 921 | * If we already asked for a renegotiation then fall through to the |
| 922 | * SSL_read() below. |
| 923 | */ |
| 924 | if (!SSL_renegotiate_pending(peer->ssl)) { |
| 925 | /* |
| 926 | * If we are the client we will always attempt to resume the |
FdaSilvaYY | 44e6995 | 2017-08-11 10:15:22 -0400 | [diff] [blame] | 927 | * session. The server may or may not resume dependent on the |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 928 | * setting of SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 929 | */ |
Matt Caswell | cc22cd5 | 2017-02-03 11:21:07 +0000 | [diff] [blame] | 930 | if (SSL_is_server(peer->ssl)) { |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 931 | ret = SSL_renegotiate(peer->ssl); |
Matt Caswell | cc22cd5 | 2017-02-03 11:21:07 +0000 | [diff] [blame] | 932 | } else { |
| 933 | if (test_ctx->extra.client.reneg_ciphers != NULL) { |
| 934 | if (!SSL_set_cipher_list(peer->ssl, |
| 935 | test_ctx->extra.client.reneg_ciphers)) { |
| 936 | peer->status = PEER_ERROR; |
| 937 | return; |
| 938 | } |
| 939 | ret = SSL_renegotiate(peer->ssl); |
| 940 | } else { |
| 941 | ret = SSL_renegotiate_abbreviated(peer->ssl); |
| 942 | } |
| 943 | } |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 944 | if (!ret) { |
| 945 | peer->status = PEER_ERROR; |
| 946 | return; |
| 947 | } |
| 948 | do_handshake_step(peer); |
| 949 | /* |
| 950 | * If status is PEER_RETRY it means we're waiting on the peer to |
| 951 | * continue the handshake. As far as setting up the renegotiation is |
| 952 | * concerned that is a success. The next step will continue the |
| 953 | * handshake to its conclusion. |
| 954 | * |
| 955 | * If status is PEER_SUCCESS then we are the server and we have |
| 956 | * successfully sent the HelloRequest. We need to continue to wait |
| 957 | * until the handshake arrives from the client. |
| 958 | */ |
| 959 | if (peer->status == PEER_RETRY) |
| 960 | peer->status = PEER_SUCCESS; |
| 961 | else if (peer->status == PEER_SUCCESS) |
| 962 | peer->status = PEER_RETRY; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 963 | return; |
| 964 | } |
Matt Caswell | 9b92f16 | 2017-02-15 09:25:52 +0000 | [diff] [blame] | 965 | } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER |
| 966 | || test_ctx->handshake_mode |
| 967 | == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) { |
| 968 | if (SSL_is_server(peer->ssl) |
| 969 | != (test_ctx->handshake_mode |
| 970 | == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER)) { |
| 971 | peer->status = PEER_SUCCESS; |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | ret = SSL_key_update(peer->ssl, test_ctx->key_update_type); |
| 976 | if (!ret) { |
| 977 | peer->status = PEER_ERROR; |
| 978 | return; |
| 979 | } |
| 980 | do_handshake_step(peer); |
| 981 | /* |
| 982 | * This is a one step handshake. We shouldn't get anything other than |
| 983 | * PEER_SUCCESS |
| 984 | */ |
| 985 | if (peer->status != PEER_SUCCESS) |
| 986 | peer->status = PEER_ERROR; |
| 987 | return; |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 988 | } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH) { |
| 989 | if (SSL_is_server(peer->ssl)) { |
| 990 | /* Make the server believe it's received the extension */ |
| 991 | if (test_ctx->extra.server.force_pha) |
| 992 | peer->ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; |
| 993 | ret = SSL_verify_client_post_handshake(peer->ssl); |
| 994 | if (!ret) { |
| 995 | peer->status = PEER_ERROR; |
| 996 | return; |
| 997 | } |
| 998 | } |
| 999 | do_handshake_step(peer); |
| 1000 | /* |
| 1001 | * This is a one step handshake. We shouldn't get anything other than |
| 1002 | * PEER_SUCCESS |
| 1003 | */ |
| 1004 | if (peer->status != PEER_SUCCESS) |
| 1005 | peer->status = PEER_ERROR; |
| 1006 | return; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * The SSL object is still expecting app data, even though it's going to |
| 1011 | * get a handshake message. We try to read, and it should fail - after which |
| 1012 | * we should be in a handshake |
| 1013 | */ |
| 1014 | ret = SSL_read(peer->ssl, &buf, sizeof(buf)); |
| 1015 | if (ret >= 0) { |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1016 | /* |
| 1017 | * We're not actually expecting data - we're expecting a reneg to |
| 1018 | * start |
| 1019 | */ |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1020 | peer->status = PEER_ERROR; |
| 1021 | return; |
| 1022 | } else { |
| 1023 | int error = SSL_get_error(peer->ssl, ret); |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1024 | if (error != SSL_ERROR_WANT_READ) { |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1025 | peer->status = PEER_ERROR; |
| 1026 | return; |
| 1027 | } |
Matt Caswell | 9b92f16 | 2017-02-15 09:25:52 +0000 | [diff] [blame] | 1028 | /* If we're not in init yet then we're not done with setup yet */ |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1029 | if (!SSL_in_init(peer->ssl)) |
| 1030 | return; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | peer->status = PEER_SUCCESS; |
| 1034 | } |
| 1035 | |
| 1036 | |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1037 | /* |
| 1038 | * RFC 5246 says: |
| 1039 | * |
| 1040 | * Note that as of TLS 1.1, |
| 1041 | * failure to properly close a connection no longer requires that a |
| 1042 | * session not be resumed. This is a change from TLS 1.0 to conform |
| 1043 | * with widespread implementation practice. |
| 1044 | * |
| 1045 | * However, |
| 1046 | * (a) OpenSSL requires that a connection be shutdown for all protocol versions. |
| 1047 | * (b) We test lower versions, too. |
| 1048 | * So we just implement shutdown. We do a full bidirectional shutdown so that we |
| 1049 | * can compare sent and received close_notify alerts and get some test coverage |
| 1050 | * for SSL_shutdown as a bonus. |
| 1051 | */ |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1052 | static void do_shutdown_step(PEER *peer) |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1053 | { |
| 1054 | int ret; |
| 1055 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1056 | if (!TEST_int_eq(peer->status, PEER_RETRY)) { |
| 1057 | peer->status = PEER_TEST_FAILURE; |
| 1058 | return; |
| 1059 | } |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1060 | ret = SSL_shutdown(peer->ssl); |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1061 | |
| 1062 | if (ret == 1) { |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1063 | peer->status = PEER_SUCCESS; |
| 1064 | } else if (ret < 0) { /* On 0, we retry. */ |
| 1065 | int error = SSL_get_error(peer->ssl, ret); |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1066 | |
| 1067 | if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1068 | peer->status = PEER_ERROR; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | typedef enum { |
| 1073 | HANDSHAKE, |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1074 | RENEG_APPLICATION_DATA, |
| 1075 | RENEG_SETUP, |
| 1076 | RENEG_HANDSHAKE, |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1077 | APPLICATION_DATA, |
| 1078 | SHUTDOWN, |
| 1079 | CONNECTION_DONE |
| 1080 | } connect_phase_t; |
| 1081 | |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 1082 | |
| 1083 | static int renegotiate_op(const SSL_TEST_CTX *test_ctx) |
| 1084 | { |
| 1085 | switch (test_ctx->handshake_mode) { |
| 1086 | case SSL_TEST_HANDSHAKE_RENEG_SERVER: |
| 1087 | case SSL_TEST_HANDSHAKE_RENEG_CLIENT: |
| 1088 | return 1; |
| 1089 | default: |
| 1090 | return 0; |
| 1091 | } |
| 1092 | } |
| 1093 | static int post_handshake_op(const SSL_TEST_CTX *test_ctx) |
| 1094 | { |
| 1095 | switch (test_ctx->handshake_mode) { |
| 1096 | case SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT: |
| 1097 | case SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER: |
| 1098 | case SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH: |
| 1099 | return 1; |
| 1100 | default: |
| 1101 | return 0; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1105 | static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx, |
| 1106 | connect_phase_t phase) |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1107 | { |
| 1108 | switch (phase) { |
| 1109 | case HANDSHAKE: |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 1110 | if (renegotiate_op(test_ctx) || post_handshake_op(test_ctx)) |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1111 | return RENEG_APPLICATION_DATA; |
| 1112 | return APPLICATION_DATA; |
| 1113 | case RENEG_APPLICATION_DATA: |
| 1114 | return RENEG_SETUP; |
| 1115 | case RENEG_SETUP: |
Todd Short | 9d75dce | 2017-12-18 16:52:28 -0500 | [diff] [blame] | 1116 | if (post_handshake_op(test_ctx)) |
Matt Caswell | 9b92f16 | 2017-02-15 09:25:52 +0000 | [diff] [blame] | 1117 | return APPLICATION_DATA; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1118 | return RENEG_HANDSHAKE; |
| 1119 | case RENEG_HANDSHAKE: |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1120 | return APPLICATION_DATA; |
| 1121 | case APPLICATION_DATA: |
| 1122 | return SHUTDOWN; |
| 1123 | case SHUTDOWN: |
| 1124 | return CONNECTION_DONE; |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 1125 | case CONNECTION_DONE: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1126 | TEST_error("Trying to progress after connection done"); |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 1127 | break; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1128 | } |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 1129 | return -1; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1132 | static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer, |
| 1133 | connect_phase_t phase) |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1134 | { |
| 1135 | switch (phase) { |
| 1136 | case HANDSHAKE: |
| 1137 | do_handshake_step(peer); |
| 1138 | break; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1139 | case RENEG_APPLICATION_DATA: |
| 1140 | do_app_data_step(peer); |
| 1141 | break; |
| 1142 | case RENEG_SETUP: |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1143 | do_reneg_setup_step(test_ctx, peer); |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1144 | break; |
| 1145 | case RENEG_HANDSHAKE: |
| 1146 | do_handshake_step(peer); |
| 1147 | break; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1148 | case APPLICATION_DATA: |
| 1149 | do_app_data_step(peer); |
| 1150 | break; |
| 1151 | case SHUTDOWN: |
| 1152 | do_shutdown_step(peer); |
| 1153 | break; |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 1154 | case CONNECTION_DONE: |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1155 | TEST_error("Action after connection done"); |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 1156 | break; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | typedef enum { |
| 1161 | /* Both parties succeeded. */ |
| 1162 | HANDSHAKE_SUCCESS, |
| 1163 | /* Client errored. */ |
| 1164 | CLIENT_ERROR, |
| 1165 | /* Server errored. */ |
| 1166 | SERVER_ERROR, |
| 1167 | /* Peers are in inconsistent state. */ |
| 1168 | INTERNAL_ERROR, |
| 1169 | /* One or both peers not done. */ |
| 1170 | HANDSHAKE_RETRY |
| 1171 | } handshake_status_t; |
| 1172 | |
| 1173 | /* |
| 1174 | * Determine the handshake outcome. |
| 1175 | * last_status: the status of the peer to have acted last. |
| 1176 | * previous_status: the status of the peer that didn't act last. |
| 1177 | * client_spoke_last: 1 if the client went last. |
| 1178 | */ |
| 1179 | static handshake_status_t handshake_status(peer_status_t last_status, |
| 1180 | peer_status_t previous_status, |
| 1181 | int client_spoke_last) |
| 1182 | { |
| 1183 | switch (last_status) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1184 | case PEER_TEST_FAILURE: |
| 1185 | return INTERNAL_ERROR; |
| 1186 | |
Matt Caswell | 561f6f1 | 2017-04-24 14:15:49 +0100 | [diff] [blame] | 1187 | case PEER_WAITING: |
| 1188 | /* Shouldn't ever happen */ |
| 1189 | return INTERNAL_ERROR; |
| 1190 | |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1191 | case PEER_SUCCESS: |
| 1192 | switch (previous_status) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1193 | case PEER_TEST_FAILURE: |
| 1194 | return INTERNAL_ERROR; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1195 | case PEER_SUCCESS: |
| 1196 | /* Both succeeded. */ |
| 1197 | return HANDSHAKE_SUCCESS; |
Matt Caswell | 561f6f1 | 2017-04-24 14:15:49 +0100 | [diff] [blame] | 1198 | case PEER_WAITING: |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1199 | case PEER_RETRY: |
| 1200 | /* Let the first peer finish. */ |
| 1201 | return HANDSHAKE_RETRY; |
| 1202 | case PEER_ERROR: |
| 1203 | /* |
| 1204 | * Second peer succeeded despite the fact that the first peer |
| 1205 | * already errored. This shouldn't happen. |
| 1206 | */ |
| 1207 | return INTERNAL_ERROR; |
| 1208 | } |
Bernd Edlinger | 4d921bf | 2018-02-14 21:30:32 +0100 | [diff] [blame] | 1209 | break; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1210 | |
| 1211 | case PEER_RETRY: |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1212 | return HANDSHAKE_RETRY; |
| 1213 | |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1214 | case PEER_ERROR: |
| 1215 | switch (previous_status) { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1216 | case PEER_TEST_FAILURE: |
| 1217 | return INTERNAL_ERROR; |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1218 | case PEER_WAITING: |
| 1219 | /* The client failed immediately before sending the ClientHello */ |
| 1220 | return client_spoke_last ? CLIENT_ERROR : INTERNAL_ERROR; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1221 | case PEER_SUCCESS: |
| 1222 | /* |
| 1223 | * First peer succeeded but second peer errored. |
| 1224 | * TODO(emilia): we should be able to continue here (with some |
| 1225 | * application data?) to ensure the first peer receives the |
| 1226 | * alert / close_notify. |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1227 | * (No tests currently exercise this branch.) |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1228 | */ |
| 1229 | return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR; |
| 1230 | case PEER_RETRY: |
| 1231 | /* We errored; let the peer finish. */ |
| 1232 | return HANDSHAKE_RETRY; |
| 1233 | case PEER_ERROR: |
| 1234 | /* Both peers errored. Return the one that errored first. */ |
| 1235 | return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR; |
| 1236 | } |
| 1237 | } |
| 1238 | /* Control should never reach here. */ |
| 1239 | return INTERNAL_ERROR; |
| 1240 | } |
| 1241 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1242 | /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */ |
| 1243 | static char *dup_str(const unsigned char *in, size_t len) |
| 1244 | { |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1245 | char *ret = NULL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1246 | |
FdaSilvaYY | 28b86f3 | 2016-08-24 00:17:31 +0200 | [diff] [blame] | 1247 | if (len == 0) |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1248 | return NULL; |
| 1249 | |
| 1250 | /* Assert that the string does not contain NUL-bytes. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1251 | if (TEST_size_t_eq(OPENSSL_strnlen((const char*)(in), len), len)) |
| 1252 | TEST_ptr(ret = OPENSSL_strndup((const char*)(in), len)); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1253 | return ret; |
| 1254 | } |
| 1255 | |
Dr. Stephen Henson | 7f5f35a | 2017-01-08 19:30:41 +0000 | [diff] [blame] | 1256 | static int pkey_type(EVP_PKEY *pkey) |
| 1257 | { |
| 1258 | int nid = EVP_PKEY_id(pkey); |
| 1259 | |
| 1260 | #ifndef OPENSSL_NO_EC |
| 1261 | if (nid == EVP_PKEY_EC) { |
| 1262 | const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
| 1263 | return EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
| 1264 | } |
| 1265 | #endif |
| 1266 | return nid; |
| 1267 | } |
| 1268 | |
| 1269 | static int peer_pkey_type(SSL *s) |
| 1270 | { |
| 1271 | X509 *x = SSL_get_peer_certificate(s); |
| 1272 | |
| 1273 | if (x != NULL) { |
| 1274 | int nid = pkey_type(X509_get0_pubkey(x)); |
| 1275 | |
| 1276 | X509_free(x); |
| 1277 | return nid; |
| 1278 | } |
| 1279 | return NID_undef; |
| 1280 | } |
| 1281 | |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1282 | #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) |
| 1283 | static int set_sock_as_sctp(int sock) |
| 1284 | { |
| 1285 | /* |
| 1286 | * For SCTP we have to set various options on the socket prior to |
| 1287 | * connecting. This is done automatically by BIO_new_dgram_sctp(). |
| 1288 | * We don't actually need the created BIO though so we free it again |
| 1289 | * immediately. |
| 1290 | */ |
| 1291 | BIO *tmpbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE); |
| 1292 | |
| 1293 | if (tmpbio == NULL) |
| 1294 | return 0; |
| 1295 | BIO_free(tmpbio); |
| 1296 | |
| 1297 | return 1; |
| 1298 | } |
| 1299 | |
| 1300 | static int create_sctp_socks(int *ssock, int *csock) |
| 1301 | { |
| 1302 | BIO_ADDRINFO *res = NULL; |
| 1303 | const BIO_ADDRINFO *ai = NULL; |
| 1304 | int lsock = INVALID_SOCKET, asock = INVALID_SOCKET; |
| 1305 | int consock = INVALID_SOCKET; |
| 1306 | int ret = 0; |
| 1307 | int family = 0; |
| 1308 | |
Paul Yang | 9e1d5e8 | 2017-06-26 01:09:46 +0800 | [diff] [blame] | 1309 | if (BIO_sock_init() != 1) |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1310 | return 0; |
| 1311 | |
| 1312 | /* |
| 1313 | * Port is 4463. It could be anything. It will fail if it's already being |
| 1314 | * used for some other SCTP service. It seems unlikely though so we don't |
| 1315 | * worry about it here. |
| 1316 | */ |
| 1317 | if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_SERVER, family, SOCK_STREAM, |
| 1318 | IPPROTO_SCTP, &res)) |
| 1319 | return 0; |
| 1320 | |
| 1321 | for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) { |
| 1322 | family = BIO_ADDRINFO_family(ai); |
| 1323 | lsock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); |
| 1324 | if (lsock == INVALID_SOCKET) { |
| 1325 | /* Maybe the kernel doesn't support the socket family, even if |
| 1326 | * BIO_lookup() added it in the returned result... |
| 1327 | */ |
| 1328 | continue; |
| 1329 | } |
| 1330 | |
| 1331 | if (!set_sock_as_sctp(lsock) |
| 1332 | || !BIO_listen(lsock, BIO_ADDRINFO_address(ai), |
| 1333 | BIO_SOCK_REUSEADDR)) { |
| 1334 | BIO_closesocket(lsock); |
| 1335 | lsock = INVALID_SOCKET; |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | /* Success, don't try any more addresses */ |
| 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | if (lsock == INVALID_SOCKET) |
| 1344 | goto err; |
| 1345 | |
| 1346 | BIO_ADDRINFO_free(res); |
| 1347 | res = NULL; |
| 1348 | |
| 1349 | if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_CLIENT, family, SOCK_STREAM, |
| 1350 | IPPROTO_SCTP, &res)) |
| 1351 | goto err; |
| 1352 | |
| 1353 | consock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); |
| 1354 | if (consock == INVALID_SOCKET) |
| 1355 | goto err; |
| 1356 | |
| 1357 | if (!set_sock_as_sctp(consock) |
| 1358 | || !BIO_connect(consock, BIO_ADDRINFO_address(res), 0) |
| 1359 | || !BIO_socket_nbio(consock, 1)) |
| 1360 | goto err; |
| 1361 | |
| 1362 | asock = BIO_accept_ex(lsock, NULL, BIO_SOCK_NONBLOCK); |
| 1363 | if (asock == INVALID_SOCKET) |
| 1364 | goto err; |
| 1365 | |
| 1366 | *csock = consock; |
| 1367 | *ssock = asock; |
| 1368 | consock = asock = INVALID_SOCKET; |
| 1369 | ret = 1; |
| 1370 | |
| 1371 | err: |
| 1372 | BIO_ADDRINFO_free(res); |
| 1373 | if (consock != INVALID_SOCKET) |
| 1374 | BIO_closesocket(consock); |
| 1375 | if (lsock != INVALID_SOCKET) |
| 1376 | BIO_closesocket(lsock); |
| 1377 | if (asock != INVALID_SOCKET) |
| 1378 | BIO_closesocket(asock); |
| 1379 | return ret; |
| 1380 | } |
| 1381 | #endif |
| 1382 | |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 1383 | /* |
| 1384 | * Note that |extra| points to the correct client/server configuration |
| 1385 | * within |test_ctx|. When configuring the handshake, general mode settings |
| 1386 | * are taken from |test_ctx|, and client/server-specific settings should be |
| 1387 | * taken from |extra|. |
| 1388 | * |
| 1389 | * The configuration code should never reach into |test_ctx->extra| or |
| 1390 | * |test_ctx->resume_extra| directly. |
| 1391 | * |
| 1392 | * (We could refactor test mode settings into a substructure. This would result |
| 1393 | * in cleaner argument passing but would complicate the test configuration |
| 1394 | * parsing.) |
| 1395 | */ |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1396 | static HANDSHAKE_RESULT *do_handshake_internal( |
| 1397 | SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 1398 | const SSL_TEST_CTX *test_ctx, const SSL_TEST_EXTRA_CONF *extra, |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1399 | SSL_SESSION *session_in, SSL_SESSION **session_out) |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1400 | { |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1401 | PEER server, client; |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1402 | BIO *client_to_server = NULL, *server_to_client = NULL; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1403 | HANDSHAKE_EX_DATA server_ex_data, client_ex_data; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1404 | CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data; |
| 1405 | HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new(); |
Matt Caswell | 36ff232 | 2018-03-14 19:22:48 +0000 | [diff] [blame] | 1406 | int client_turn = 1, client_turn_count = 0, client_wait_count = 0; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1407 | connect_phase_t phase = HANDSHAKE; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1408 | handshake_status_t status = HANDSHAKE_RETRY; |
Matt Caswell | 48593cb | 2016-08-13 14:29:41 +0100 | [diff] [blame] | 1409 | const unsigned char* tick = NULL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1410 | size_t tick_len = 0; |
Todd Short | a84e5c9 | 2016-09-01 08:40:54 -0400 | [diff] [blame] | 1411 | const unsigned char* sess_id = NULL; |
| 1412 | unsigned int sess_id_len = 0; |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 1413 | SSL_SESSION* sess = NULL; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1414 | const unsigned char *proto = NULL; |
| 1415 | /* API dictates unsigned int rather than size_t. */ |
| 1416 | unsigned int proto_len = 0; |
Dr. Stephen Henson | b93ad05 | 2017-01-08 00:09:08 +0000 | [diff] [blame] | 1417 | EVP_PKEY *tmp_key; |
Dr. Stephen Henson | f15b50c | 2017-03-31 22:35:28 +0100 | [diff] [blame] | 1418 | const STACK_OF(X509_NAME) *names; |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1419 | time_t start; |
Todd Short | e1c7871 | 2015-12-21 15:19:29 -0500 | [diff] [blame] | 1420 | const char* cipher; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1421 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1422 | if (ret == NULL) |
| 1423 | return NULL; |
| 1424 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1425 | memset(&server_ctx_data, 0, sizeof(server_ctx_data)); |
| 1426 | memset(&server2_ctx_data, 0, sizeof(server2_ctx_data)); |
| 1427 | memset(&client_ctx_data, 0, sizeof(client_ctx_data)); |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1428 | memset(&server, 0, sizeof(server)); |
| 1429 | memset(&client, 0, sizeof(client)); |
Pauli | 20e237c | 2017-07-14 10:08:38 +1000 | [diff] [blame] | 1430 | memset(&server_ex_data, 0, sizeof(server_ex_data)); |
| 1431 | memset(&client_ex_data, 0, sizeof(client_ex_data)); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1432 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1433 | if (!configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, |
| 1434 | test_ctx, extra, &server_ctx_data, |
| 1435 | &server2_ctx_data, &client_ctx_data)) { |
| 1436 | TEST_note("configure_handshake_ctx"); |
| 1437 | return NULL; |
| 1438 | } |
Emilia Kasper | a263f32 | 2016-04-07 19:07:50 +0200 | [diff] [blame] | 1439 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1440 | /* Setup SSL and buffers; additional configuration happens below. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1441 | if (!create_peer(&server, server_ctx)) { |
| 1442 | TEST_note("creating server context"); |
| 1443 | goto err; |
| 1444 | } |
| 1445 | if (!create_peer(&client, client_ctx)) { |
| 1446 | TEST_note("creating client context"); |
| 1447 | goto err; |
| 1448 | } |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1449 | |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 1450 | server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size; |
| 1451 | client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1452 | |
| 1453 | configure_handshake_ssl(server.ssl, client.ssl, extra); |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1454 | if (session_in != NULL) { |
| 1455 | /* In case we're testing resumption without tickets. */ |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1456 | if (!TEST_true(SSL_CTX_add_session(server_ctx, session_in)) |
| 1457 | || !TEST_true(SSL_set_session(client.ssl, session_in))) |
| 1458 | goto err; |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1459 | } |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 1460 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1461 | ret->result = SSL_TEST_INTERNAL_ERROR; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1462 | |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1463 | if (test_ctx->use_sctp) { |
| 1464 | #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) |
| 1465 | int csock, ssock; |
| 1466 | |
| 1467 | if (create_sctp_socks(&ssock, &csock)) { |
| 1468 | client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE); |
| 1469 | server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE); |
| 1470 | } |
| 1471 | #endif |
| 1472 | } else { |
| 1473 | client_to_server = BIO_new(BIO_s_mem()); |
| 1474 | server_to_client = BIO_new(BIO_s_mem()); |
| 1475 | } |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1476 | |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1477 | if (!TEST_ptr(client_to_server) |
| 1478 | || !TEST_ptr(server_to_client)) |
| 1479 | goto err; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1480 | |
| 1481 | /* Non-blocking bio. */ |
| 1482 | BIO_set_nbio(client_to_server, 1); |
| 1483 | BIO_set_nbio(server_to_client, 1); |
| 1484 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1485 | SSL_set_connect_state(client.ssl); |
| 1486 | SSL_set_accept_state(server.ssl); |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1487 | |
| 1488 | /* The bios are now owned by the SSL object. */ |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1489 | if (test_ctx->use_sctp) { |
| 1490 | SSL_set_bio(client.ssl, client_to_server, client_to_server); |
| 1491 | SSL_set_bio(server.ssl, server_to_client, server_to_client); |
| 1492 | } else { |
| 1493 | SSL_set_bio(client.ssl, server_to_client, client_to_server); |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1494 | if (!TEST_int_gt(BIO_up_ref(server_to_client), 0) |
| 1495 | || !TEST_int_gt(BIO_up_ref(client_to_server), 0)) |
| 1496 | goto err; |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1497 | SSL_set_bio(server.ssl, client_to_server, server_to_client); |
| 1498 | } |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1499 | |
| 1500 | ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL); |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1501 | if (!TEST_int_ge(ex_data_idx, 0) |
| 1502 | || !TEST_int_eq(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data), 1) |
| 1503 | || !TEST_int_eq(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data), 1)) |
| 1504 | goto err; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1505 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1506 | SSL_set_info_callback(server.ssl, &info_cb); |
| 1507 | SSL_set_info_callback(client.ssl, &info_cb); |
| 1508 | |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1509 | client.status = PEER_RETRY; |
| 1510 | server.status = PEER_WAITING; |
| 1511 | |
| 1512 | start = time(NULL); |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Half-duplex handshake loop. |
| 1516 | * Client and server speak to each other synchronously in the same process. |
| 1517 | * We use non-blocking BIOs, so whenever one peer blocks for read, it |
| 1518 | * returns PEER_RETRY to indicate that it's the other peer's turn to write. |
| 1519 | * The handshake succeeds once both peers have succeeded. If one peer |
| 1520 | * errors out, we also let the other peer retry (and presumably fail). |
| 1521 | */ |
| 1522 | for(;;) { |
| 1523 | if (client_turn) { |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1524 | do_connect_step(test_ctx, &client, phase); |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1525 | status = handshake_status(client.status, server.status, |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1526 | 1 /* client went last */); |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1527 | if (server.status == PEER_WAITING) |
| 1528 | server.status = PEER_RETRY; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1529 | } else { |
Matt Caswell | fe7dd55 | 2016-09-27 11:50:43 +0100 | [diff] [blame] | 1530 | do_connect_step(test_ctx, &server, phase); |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1531 | status = handshake_status(server.status, client.status, |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1532 | 0 /* server went last */); |
| 1533 | } |
| 1534 | |
| 1535 | switch (status) { |
| 1536 | case HANDSHAKE_SUCCESS: |
Richard Levitte | ceb6d74 | 2016-12-16 11:18:47 +0100 | [diff] [blame] | 1537 | client_turn_count = 0; |
Matt Caswell | e42c454 | 2016-09-26 17:25:43 +0100 | [diff] [blame] | 1538 | phase = next_phase(test_ctx, phase); |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1539 | if (phase == CONNECTION_DONE) { |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1540 | ret->result = SSL_TEST_SUCCESS; |
| 1541 | goto err; |
| 1542 | } else { |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1543 | client.status = server.status = PEER_RETRY; |
| 1544 | /* |
| 1545 | * For now, client starts each phase. Since each phase is |
| 1546 | * started separately, we can later control this more |
| 1547 | * precisely, for example, to test client-initiated and |
| 1548 | * server-initiated shutdown. |
| 1549 | */ |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1550 | client_turn = 1; |
| 1551 | break; |
| 1552 | } |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1553 | case CLIENT_ERROR: |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1554 | ret->result = SSL_TEST_CLIENT_FAIL; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1555 | goto err; |
| 1556 | case SERVER_ERROR: |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1557 | ret->result = SSL_TEST_SERVER_FAIL; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1558 | goto err; |
| 1559 | case INTERNAL_ERROR: |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1560 | ret->result = SSL_TEST_INTERNAL_ERROR; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1561 | goto err; |
| 1562 | case HANDSHAKE_RETRY: |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1563 | if (test_ctx->use_sctp) { |
| 1564 | if (time(NULL) - start > 3) { |
| 1565 | /* |
| 1566 | * We've waited for too long. Give up. |
| 1567 | */ |
| 1568 | ret->result = SSL_TEST_INTERNAL_ERROR; |
| 1569 | goto err; |
| 1570 | } |
Richard Levitte | ceb6d74 | 2016-12-16 11:18:47 +0100 | [diff] [blame] | 1571 | /* |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1572 | * With "real" sockets we only swap to processing the peer |
| 1573 | * if they are expecting to retry. Otherwise we just retry the |
| 1574 | * same endpoint again. |
Richard Levitte | ceb6d74 | 2016-12-16 11:18:47 +0100 | [diff] [blame] | 1575 | */ |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1576 | if ((client_turn && server.status == PEER_RETRY) |
| 1577 | || (!client_turn && client.status == PEER_RETRY)) |
| 1578 | client_turn ^= 1; |
| 1579 | } else { |
| 1580 | if (client_turn_count++ >= 2000) { |
| 1581 | /* |
| 1582 | * At this point, there's been so many PEER_RETRY in a row |
| 1583 | * that it's likely both sides are stuck waiting for a read. |
| 1584 | * It's time to give up. |
| 1585 | */ |
| 1586 | ret->result = SSL_TEST_INTERNAL_ERROR; |
| 1587 | goto err; |
| 1588 | } |
Matt Caswell | 36ff232 | 2018-03-14 19:22:48 +0000 | [diff] [blame] | 1589 | if (client_turn && server.status == PEER_SUCCESS) { |
| 1590 | /* |
| 1591 | * The server may finish before the client because the |
| 1592 | * client spends some turns processing NewSessionTickets. |
| 1593 | */ |
| 1594 | if (client_wait_count++ >= 2) { |
| 1595 | ret->result = SSL_TEST_INTERNAL_ERROR; |
| 1596 | goto err; |
| 1597 | } |
| 1598 | } else { |
| 1599 | /* Continue. */ |
| 1600 | client_turn ^= 1; |
| 1601 | } |
Matt Caswell | 83964ca | 2017-04-24 09:42:28 +0100 | [diff] [blame] | 1602 | } |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | err: |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1607 | ret->server_alert_sent = server_ex_data.alert_sent; |
Emilia Kasper | dd8e5a5 | 2016-08-12 14:29:24 +0200 | [diff] [blame] | 1608 | ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1609 | ret->server_alert_received = client_ex_data.alert_received; |
| 1610 | ret->client_alert_sent = client_ex_data.alert_sent; |
Emilia Kasper | dd8e5a5 | 2016-08-12 14:29:24 +0200 | [diff] [blame] | 1611 | ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1612 | ret->client_alert_received = server_ex_data.alert_received; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1613 | ret->server_protocol = SSL_version(server.ssl); |
| 1614 | ret->client_protocol = SSL_version(client.ssl); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1615 | ret->servername = server_ex_data.servername; |
Todd Short | a84e5c9 | 2016-09-01 08:40:54 -0400 | [diff] [blame] | 1616 | if ((sess = SSL_get0_session(client.ssl)) != NULL) { |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1617 | SSL_SESSION_get0_ticket(sess, &tick, &tick_len); |
Todd Short | a84e5c9 | 2016-09-01 08:40:54 -0400 | [diff] [blame] | 1618 | sess_id = SSL_SESSION_get_id(sess, &sess_id_len); |
| 1619 | } |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1620 | if (tick == NULL || tick_len == 0) |
| 1621 | ret->session_ticket = SSL_TEST_SESSION_TICKET_NO; |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 1622 | else |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1623 | ret->session_ticket = SSL_TEST_SESSION_TICKET_YES; |
Matt Caswell | 439db0c | 2017-03-01 12:11:51 +0000 | [diff] [blame] | 1624 | ret->compression = (SSL_get_current_compression(client.ssl) == NULL) |
| 1625 | ? SSL_TEST_COMPRESSION_NO |
| 1626 | : SSL_TEST_COMPRESSION_YES; |
Todd Short | a84e5c9 | 2016-09-01 08:40:54 -0400 | [diff] [blame] | 1627 | if (sess_id == NULL || sess_id_len == 0) |
| 1628 | ret->session_id = SSL_TEST_SESSION_ID_NO; |
| 1629 | else |
| 1630 | ret->session_id = SSL_TEST_SESSION_ID_YES; |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1631 | ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call; |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1632 | |
Ben Laurie | 620c6ad | 2016-07-31 11:42:04 +0100 | [diff] [blame] | 1633 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1634 | SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1635 | ret->client_npn_negotiated = dup_str(proto, proto_len); |
| 1636 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1637 | SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1638 | ret->server_npn_negotiated = dup_str(proto, proto_len); |
Emilia Kasper | 7b7cea6 | 2016-08-05 17:17:00 +0200 | [diff] [blame] | 1639 | #endif |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1640 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1641 | SSL_get0_alpn_selected(client.ssl, &proto, &proto_len); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1642 | ret->client_alpn_negotiated = dup_str(proto, proto_len); |
| 1643 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1644 | SSL_get0_alpn_selected(server.ssl, &proto, &proto_len); |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1645 | ret->server_alpn_negotiated = dup_str(proto, proto_len); |
| 1646 | |
Todd Short | df0fed9 | 2017-03-15 13:25:55 -0400 | [diff] [blame] | 1647 | if ((sess = SSL_get0_session(server.ssl)) != NULL) { |
| 1648 | SSL_SESSION_get0_ticket_appdata(sess, (void**)&tick, &tick_len); |
| 1649 | ret->result_session_ticket_app_data = OPENSSL_strndup((const char*)tick, tick_len); |
| 1650 | } |
| 1651 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1652 | ret->client_resumed = SSL_session_reused(client.ssl); |
| 1653 | ret->server_resumed = SSL_session_reused(server.ssl); |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1654 | |
Todd Short | e1c7871 | 2015-12-21 15:19:29 -0500 | [diff] [blame] | 1655 | cipher = SSL_CIPHER_get_name(SSL_get_current_cipher(client.ssl)); |
| 1656 | ret->cipher = dup_str((const unsigned char*)cipher, strlen(cipher)); |
| 1657 | |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1658 | if (session_out != NULL) |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1659 | *session_out = SSL_get1_session(client.ssl); |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1660 | |
Dr. Stephen Henson | b93ad05 | 2017-01-08 00:09:08 +0000 | [diff] [blame] | 1661 | if (SSL_get_server_tmp_key(client.ssl, &tmp_key)) { |
Dr. Stephen Henson | 7f5f35a | 2017-01-08 19:30:41 +0000 | [diff] [blame] | 1662 | ret->tmp_key_type = pkey_type(tmp_key); |
Dr. Stephen Henson | b93ad05 | 2017-01-08 00:09:08 +0000 | [diff] [blame] | 1663 | EVP_PKEY_free(tmp_key); |
Dr. Stephen Henson | b93ad05 | 2017-01-08 00:09:08 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Dr. Stephen Henson | ee5b6a4 | 2017-01-13 15:20:42 +0000 | [diff] [blame] | 1666 | SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash); |
| 1667 | SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash); |
| 1668 | |
Dr. Stephen Henson | 54b7f2a | 2017-01-27 15:06:16 +0000 | [diff] [blame] | 1669 | SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type); |
| 1670 | SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type); |
| 1671 | |
Dr. Stephen Henson | f15b50c | 2017-03-31 22:35:28 +0100 | [diff] [blame] | 1672 | names = SSL_get0_peer_CA_list(client.ssl); |
Dr. Stephen Henson | 2e21539 | 2017-03-15 16:07:07 +0000 | [diff] [blame] | 1673 | if (names == NULL) |
| 1674 | ret->client_ca_names = NULL; |
| 1675 | else |
| 1676 | ret->client_ca_names = SSL_dup_CA_list(names); |
| 1677 | |
Dr. Stephen Henson | f15b50c | 2017-03-31 22:35:28 +0100 | [diff] [blame] | 1678 | names = SSL_get0_peer_CA_list(server.ssl); |
| 1679 | if (names == NULL) |
| 1680 | ret->server_ca_names = NULL; |
| 1681 | else |
| 1682 | ret->server_ca_names = SSL_dup_CA_list(names); |
| 1683 | |
Dr. Stephen Henson | 7f5f35a | 2017-01-08 19:30:41 +0000 | [diff] [blame] | 1684 | ret->server_cert_type = peer_pkey_type(client.ssl); |
| 1685 | ret->client_cert_type = peer_pkey_type(server.ssl); |
| 1686 | |
Emilia Kasper | ce2cdac | 2016-07-04 20:16:14 +0200 | [diff] [blame] | 1687 | ctx_data_free_data(&server_ctx_data); |
| 1688 | ctx_data_free_data(&server2_ctx_data); |
| 1689 | ctx_data_free_data(&client_ctx_data); |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1690 | |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1691 | peer_free_data(&server); |
| 1692 | peer_free_data(&client); |
Emilia Kasper | 453dfd8 | 2016-03-17 15:14:30 +0100 | [diff] [blame] | 1693 | return ret; |
| 1694 | } |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1695 | |
| 1696 | HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, |
| 1697 | SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx, |
Emilia Kasper | 11279b1 | 2016-07-21 14:04:00 +0200 | [diff] [blame] | 1698 | SSL_CTX *resume_client_ctx, |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1699 | const SSL_TEST_CTX *test_ctx) |
| 1700 | { |
| 1701 | HANDSHAKE_RESULT *result; |
| 1702 | SSL_SESSION *session = NULL; |
| 1703 | |
| 1704 | result = do_handshake_internal(server_ctx, server2_ctx, client_ctx, |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 1705 | test_ctx, &test_ctx->extra, |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1706 | NULL, &session); |
Pauli | ff281ee | 2017-07-04 13:44:52 +1000 | [diff] [blame] | 1707 | if (result == NULL |
| 1708 | || test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME |
| 1709 | || result->result == SSL_TEST_INTERNAL_ERROR) |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1710 | goto end; |
| 1711 | |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1712 | if (result->result != SSL_TEST_SUCCESS) { |
| 1713 | result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED; |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1714 | goto end; |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | HANDSHAKE_RESULT_free(result); |
| 1718 | /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */ |
Emilia Kasper | 11279b1 | 2016-07-21 14:04:00 +0200 | [diff] [blame] | 1719 | result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx, |
Emilia Kasper | 6dc9974 | 2016-08-16 15:11:08 +0200 | [diff] [blame] | 1720 | test_ctx, &test_ctx->resume_extra, |
Emilia Kasper | e0421bd | 2016-08-11 20:51:57 +0200 | [diff] [blame] | 1721 | session, NULL); |
Emilia Kasper | 590ed3d | 2016-07-05 19:06:23 +0200 | [diff] [blame] | 1722 | end: |
| 1723 | SSL_SESSION_free(session); |
| 1724 | return result; |
| 1725 | } |