| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <errno.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
#include "api/s2n.h" |
| 22 |
#include "s2n_test.h" |
| 23 |
#include "testlib/s2n_testlib.h" |
| 24 |
#include "tls/s2n_connection.h" |
| 25 |
#include "tls/s2n_handshake.h" |
| 26 |
#include "tls/s2n_tls.h" |
| 27 |
#include "tls/s2n_tls_parameters.h" |
| 28 |
|
| 29 |
#define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \ |
| 30 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F |
| 31 |
|
| 32 |
int main(int argc, char **argv) |
| 33 |
{ |
| 34 |
char *cert_chain = NULL; |
| 35 |
char *private_key = NULL; |
| 36 |
BEGIN_TEST(); |
| 37 |
EXPECT_SUCCESS(s2n_disable_tls13_in_test()); |
| 38 |
|
| 39 |
EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 40 |
EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 41 |
EXPECT_SUCCESS(setenv("S2N_DONT_MLOCK", "1", 0)); |
| 42 |
|
| 43 |
|
| 44 |
{ |
| 45 |
struct s2n_connection *client_conn = NULL; |
| 46 |
struct s2n_config *client_config = NULL; |
| 47 |
s2n_blocked_status client_blocked; |
| 48 |
|
| 49 |
uint8_t server_hello_message[] = { |
| 50 |
|
| 51 |
0x03, 0x03, |
| 52 |
|
| 53 |
ZERO_TO_THIRTY_ONE, |
| 54 |
|
| 55 |
0x20, |
| 56 |
|
| 57 |
ZERO_TO_THIRTY_ONE, |
| 58 |
|
| 59 |
0x00, 0x3C, |
| 60 |
|
| 61 |
0x00, |
| 62 |
|
| 63 |
0x00, 0x00 |
| 64 |
}; |
| 65 |
int body_len = sizeof(server_hello_message); |
| 66 |
uint8_t message_header[] = { |
| 67 |
|
| 68 |
0x02, |
| 69 |
|
| 70 |
(body_len >> 16) & 0xff, |
| 71 |
(body_len >> 8) & 0xff, |
| 72 |
(body_len & 0xff), |
| 73 |
}; |
| 74 |
int message_len = sizeof(message_header) + body_len; |
| 75 |
uint8_t record_header[] = { |
| 76 |
|
| 77 |
0x16, |
| 78 |
|
| 79 |
0x03, |
| 80 |
0x03, |
| 81 |
|
| 82 |
(message_len >> 8) & 0xff, |
| 83 |
(message_len & 0xff), |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
struct s2n_test_io_pair io_pair; |
| 88 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 89 |
|
| 90 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 91 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 92 |
|
| 93 |
EXPECT_NOT_NULL(client_config = s2n_config_new()); |
| 94 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all")); |
| 95 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config)); |
| 96 |
|
| 97 |
|
| 98 |
EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1); |
| 99 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 100 |
EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ); |
| 101 |
|
| 102 |
|
| 103 |
uint8_t buf[1024]; |
| 104 |
size_t buf_occupied = 0; |
| 105 |
|
| 106 |
|
| 107 |
while (buf_occupied < 10) { |
| 108 |
ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied); |
| 109 |
|
| 110 |
|
| 111 |
EXPECT_TRUE(n > 0); |
| 112 |
buf_occupied += n; |
| 113 |
} |
| 114 |
|
| 115 |
EXPECT_EQUAL(buf[0], 0x16); |
| 116 |
|
| 117 |
EXPECT_EQUAL(buf[1], 0x03); |
| 118 |
EXPECT_EQUAL(buf[2], 0x01); |
| 119 |
|
| 120 |
EXPECT_EQUAL(buf[5], 0x01); |
| 121 |
|
| 122 |
EXPECT_EQUAL(buf[9], 0x03); |
| 123 |
EXPECT_EQUAL(buf[10], 0x03); |
| 124 |
|
| 125 |
|
| 126 |
while (1) { |
| 127 |
ssize_t n = s2n_test_recv(io_pair.server, buf, sizeof(buf)); |
| 128 |
|
| 129 |
if (n > 0) { |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
EXPECT_EQUAL(n, -1); |
| 134 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header)); |
| 141 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header)); |
| 142 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message)); |
| 143 |
|
| 144 |
|
| 145 |
EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1); |
| 146 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 147 |
EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ); |
| 148 |
|
| 149 |
|
| 150 |
EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12); |
| 151 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12); |
| 152 |
EXPECT_EQUAL(client_conn->server_protocol_version, S2N_TLS12); |
| 153 |
|
| 154 |
|
| 155 |
EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_blocked)); |
| 156 |
|
| 157 |
|
| 158 |
buf_occupied = 0; |
| 159 |
|
| 160 |
while (buf_occupied < 5) { |
| 161 |
ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied); |
| 162 |
|
| 163 |
|
| 164 |
EXPECT_TRUE(n > 0); |
| 165 |
buf_occupied += n; |
| 166 |
} |
| 167 |
|
| 168 |
EXPECT_EQUAL(buf[1], 0x03); |
| 169 |
EXPECT_EQUAL(buf[2], 0x03); |
| 170 |
|
| 171 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 172 |
EXPECT_SUCCESS(s2n_config_free(client_config)); |
| 173 |
|
| 174 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
{ |
| 179 |
struct s2n_connection *client_conn = NULL; |
| 180 |
struct s2n_config *client_config = NULL; |
| 181 |
s2n_blocked_status client_blocked; |
| 182 |
|
| 183 |
uint8_t server_hello_message[] = { |
| 184 |
|
| 185 |
0x03, 0x00, |
| 186 |
|
| 187 |
ZERO_TO_THIRTY_ONE, |
| 188 |
|
| 189 |
0x20, |
| 190 |
|
| 191 |
ZERO_TO_THIRTY_ONE, |
| 192 |
|
| 193 |
0x00, 0x0A, |
| 194 |
|
| 195 |
0x00, |
| 196 |
|
| 197 |
0x00, 0x00 |
| 198 |
}; |
| 199 |
int body_len = sizeof(server_hello_message); |
| 200 |
uint8_t message_header[] = { |
| 201 |
|
| 202 |
0x02, |
| 203 |
|
| 204 |
(body_len >> 16) & 0xff, |
| 205 |
(body_len >> 8) & 0xff, |
| 206 |
(body_len & 0xff), |
| 207 |
}; |
| 208 |
int message_len = sizeof(message_header) + body_len; |
| 209 |
uint8_t record_header[] = { |
| 210 |
|
| 211 |
0x16, |
| 212 |
|
| 213 |
0x03, |
| 214 |
0x00, |
| 215 |
|
| 216 |
(message_len >> 8) & 0xff, |
| 217 |
(message_len & 0xff), |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
struct s2n_test_io_pair io_pair; |
| 222 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 223 |
|
| 224 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 225 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 226 |
|
| 227 |
EXPECT_NOT_NULL(client_config = s2n_config_new()); |
| 228 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all")); |
| 229 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config)); |
| 230 |
|
| 231 |
|
| 232 |
EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1); |
| 233 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 234 |
EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ); |
| 235 |
|
| 236 |
|
| 237 |
uint8_t buf[1024]; |
| 238 |
size_t buf_occupied = 0; |
| 239 |
|
| 240 |
|
| 241 |
while (buf_occupied < 10) { |
| 242 |
ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied); |
| 243 |
|
| 244 |
|
| 245 |
EXPECT_TRUE(n > 0); |
| 246 |
buf_occupied += n; |
| 247 |
} |
| 248 |
|
| 249 |
EXPECT_EQUAL(buf[0], 0x16); |
| 250 |
|
| 251 |
EXPECT_EQUAL(buf[1], 0x03); |
| 252 |
EXPECT_EQUAL(buf[2], 0x01); |
| 253 |
|
| 254 |
EXPECT_EQUAL(buf[5], 0x01); |
| 255 |
|
| 256 |
EXPECT_EQUAL(buf[9], 0x03); |
| 257 |
EXPECT_EQUAL(buf[10], 0x03); |
| 258 |
|
| 259 |
|
| 260 |
while (1) { |
| 261 |
ssize_t n = s2n_test_recv(io_pair.server, buf, sizeof(buf)); |
| 262 |
|
| 263 |
if (n > 0) { |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
EXPECT_EQUAL(n, -1); |
| 268 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, record_header, sizeof(record_header)), sizeof(record_header)); |
| 275 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, message_header, sizeof(message_header)), sizeof(message_header)); |
| 276 |
EXPECT_EQUAL(s2n_test_send(io_pair.server, server_hello_message, sizeof(server_hello_message)), sizeof(server_hello_message)); |
| 277 |
|
| 278 |
|
| 279 |
EXPECT_EQUAL(s2n_negotiate(client_conn, &client_blocked), -1); |
| 280 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 281 |
EXPECT_EQUAL(client_blocked, S2N_BLOCKED_ON_READ); |
| 282 |
|
| 283 |
|
| 284 |
EXPECT_EQUAL(client_conn->client_protocol_version, S2N_TLS12); |
| 285 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_SSLv3); |
| 286 |
EXPECT_EQUAL(client_conn->server_protocol_version, S2N_SSLv3); |
| 287 |
|
| 288 |
|
| 289 |
EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_blocked)); |
| 290 |
|
| 291 |
|
| 292 |
buf_occupied = 0; |
| 293 |
|
| 294 |
while (buf_occupied < 5) { |
| 295 |
ssize_t n = s2n_test_recv(io_pair.server, buf + buf_occupied, sizeof(buf) - buf_occupied); |
| 296 |
|
| 297 |
|
| 298 |
EXPECT_TRUE(n > 0); |
| 299 |
buf_occupied += n; |
| 300 |
} |
| 301 |
|
| 302 |
EXPECT_EQUAL(buf[1], 0x03); |
| 303 |
EXPECT_EQUAL(buf[2], 0x00); |
| 304 |
|
| 305 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 306 |
EXPECT_SUCCESS(s2n_config_free(client_config)); |
| 307 |
|
| 308 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 309 |
}; |
| 310 |
|
| 311 |
free(cert_chain); |
| 312 |
free(private_key); |
| 313 |
END_TEST(); |
| 314 |
} |