| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "common.h" |
| 17 |
|
| 18 |
#include <errno.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <getopt.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <strings.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include "api/s2n.h" |
| 30 |
#include "error/s2n_errno.h" |
| 31 |
#include "utils/s2n_safety.h" |
| 32 |
uint8_t ticket_key_name[16] = "2016.07.26.15\0"; |
| 33 |
|
| 34 |
uint8_t default_ticket_key[32] = { 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, |
| 35 |
0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, 0x90, 0xb6, 0xc7, 0x3b, |
| 36 |
0xb5, 0x0f, 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, |
| 37 |
0xb3, 0xe5 }; |
| 38 |
|
| 39 |
struct session_cache_entry session_cache[256]; |
| 40 |
|
| 41 |
static char dhparams[] = |
| 42 |
"-----BEGIN DH PARAMETERS-----\n" |
| 43 |
"MIIBCAKCAQEAy1+hVWCfNQoPB+NA733IVOONl8fCumiz9zdRRu1hzVa2yvGseUSq\n" |
| 44 |
"Bbn6k0FQ7yMED6w5XWQKDC0z2m0FI/BPE3AjUfuPzEYGqTDf9zQZ2Lz4oAN90Sud\n" |
| 45 |
"luOoEhYR99cEbCn0T4eBvEf9IUtczXUZ/wj7gzGbGG07dLfT+CmCRJxCjhrosenJ\n" |
| 46 |
"gzucyS7jt1bobgU66JKkgMNm7hJY4/nhR5LWTCzZyzYQh2HM2Vk4K5ZqILpj/n0S\n" |
| 47 |
"5JYTQ2PVhxP+Uu8+hICs/8VvM72DznjPZzufADipjC7CsQ4S6x/ecZluFtbb+ZTv\n" |
| 48 |
"HI5CnYmkAwJ6+FSWGaZQDi8bgerFk9RWwwIBAg==\n" |
| 49 |
"-----END DH PARAMETERS-----\n"; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static uint8_t unsafe_verify_host_fn(const char *host_name, size_t host_name_len, void *data) |
| 57 |
{ |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
int write_array_to_file(const char *path, uint8_t *data, size_t length) |
| 62 |
{ |
| 63 |
GUARD_EXIT_NULL(path); |
| 64 |
GUARD_EXIT_NULL(data); |
| 65 |
|
| 66 |
FILE *file = fopen(path, "wb"); |
| 67 |
if (!file) { |
| 68 |
return S2N_FAILURE; |
| 69 |
} |
| 70 |
|
| 71 |
if (fwrite(data, sizeof(char), length, file) != length) { |
| 72 |
fclose(file); |
| 73 |
return S2N_FAILURE; |
| 74 |
} |
| 75 |
fclose(file); |
| 76 |
|
| 77 |
return S2N_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
int get_file_size(const char *path, size_t *length) |
| 81 |
{ |
| 82 |
GUARD_EXIT_NULL(path); |
| 83 |
GUARD_EXIT_NULL(length); |
| 84 |
|
| 85 |
FILE *file = fopen(path, "rb"); |
| 86 |
if (!file) { |
| 87 |
return S2N_FAILURE; |
| 88 |
} |
| 89 |
|
| 90 |
if (fseek(file, 0, SEEK_END) != 0) { |
| 91 |
fclose(file); |
| 92 |
return S2N_FAILURE; |
| 93 |
} |
| 94 |
|
| 95 |
long file_length = ftell(file); |
| 96 |
if (file_length < 0) { |
| 97 |
fclose(file); |
| 98 |
return S2N_FAILURE; |
| 99 |
} |
| 100 |
|
| 101 |
*length = file_length; |
| 102 |
fclose(file); |
| 103 |
return S2N_SUCCESS; |
| 104 |
} |
| 105 |
|
| 106 |
int load_file_to_array(const char *path, uint8_t *data, size_t max_length) |
| 107 |
{ |
| 108 |
GUARD_EXIT_NULL(path); |
| 109 |
GUARD_EXIT_NULL(data); |
| 110 |
|
| 111 |
size_t file_size = 0; |
| 112 |
if (get_file_size(path, &file_size) < 0 || file_size > max_length) { |
| 113 |
return S2N_FAILURE; |
| 114 |
} |
| 115 |
|
| 116 |
FILE *file = fopen(path, "rb"); |
| 117 |
if (!file) { |
| 118 |
return S2N_FAILURE; |
| 119 |
} |
| 120 |
|
| 121 |
if (fread(data, sizeof(char), file_size, file) < file_size) { |
| 122 |
fclose(file); |
| 123 |
return S2N_FAILURE; |
| 124 |
} |
| 125 |
|
| 126 |
fclose(file); |
| 127 |
return S2N_SUCCESS; |
| 128 |
} |
| 129 |
|
| 130 |
char *load_file_to_cstring(const char *path) |
| 131 |
{ |
| 132 |
FILE *pem_file = fopen(path, "rb"); |
| 133 |
if (!pem_file) { |
| 134 |
fprintf(stderr, "Failed to open file %s: '%s'\n", path, strerror(errno)); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if (fseek(pem_file, 0, SEEK_END) < 0) { |
| 140 |
fprintf(stderr, "Failed calling fseek: '%s'\n", strerror(errno)); |
| 141 |
fclose(pem_file); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
const ssize_t pem_file_size = ftell(pem_file); |
| 146 |
if (pem_file_size < 0) { |
| 147 |
fprintf(stderr, "Failed calling ftell: '%s'\n", strerror(errno)); |
| 148 |
fclose(pem_file); |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
rewind(pem_file); |
| 153 |
|
| 154 |
char *pem_out = malloc(pem_file_size + 1); |
| 155 |
if (pem_out == NULL) { |
| 156 |
fprintf(stderr, "Failed allocating memory\n"); |
| 157 |
fclose(pem_file); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
if (fread(pem_out, sizeof(char), pem_file_size, pem_file) < (size_t) pem_file_size) { |
| 162 |
fprintf(stderr, "Failed reading file: '%s'\n", strerror(errno)); |
| 163 |
free(pem_out); |
| 164 |
fclose(pem_file); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
pem_out[pem_file_size] = '\0'; |
| 169 |
fclose(pem_file); |
| 170 |
|
| 171 |
return pem_out; |
| 172 |
} |
| 173 |
|
| 174 |
int key_log_callback(void *file, struct s2n_connection *conn, uint8_t *logline, size_t len) |
| 175 |
{ |
| 176 |
if (fwrite(logline, 1, len, (FILE *) file) != len) { |
| 177 |
return S2N_FAILURE; |
| 178 |
} |
| 179 |
|
| 180 |
if (fprintf((FILE *) file, "\n") < 0) { |
| 181 |
return S2N_FAILURE; |
| 182 |
} |
| 183 |
|
| 184 |
return fflush((FILE *) file); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static const uint8_t hex_inverse[256] = { |
| 190 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 191 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 192 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 193 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, |
| 194 |
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 195 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 196 |
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 197 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 198 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 199 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 200 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 201 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 202 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 203 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 204 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 205 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
| 206 |
}; |
| 207 |
|
| 208 |
int s2n_str_hex_to_bytes(const unsigned char *hex, uint8_t *out_bytes, uint32_t max_out_bytes_len) |
| 209 |
{ |
| 210 |
GUARD_EXIT_NULL(hex); |
| 211 |
GUARD_EXIT_NULL(out_bytes); |
| 212 |
|
| 213 |
uint32_t len_with_spaces = strlen((const char *) hex); |
| 214 |
size_t i = 0, j = 0; |
| 215 |
while (j < len_with_spaces) { |
| 216 |
if (hex[j] == ' ') { |
| 217 |
j++; |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
uint8_t high_nibble = hex_inverse[hex[j]]; |
| 222 |
if (high_nibble == 255) { |
| 223 |
fprintf(stderr, "Invalid HEX encountered\n"); |
| 224 |
return S2N_FAILURE; |
| 225 |
} |
| 226 |
|
| 227 |
uint8_t low_nibble = hex_inverse[hex[j + 1]]; |
| 228 |
if (low_nibble == 255) { |
| 229 |
fprintf(stderr, "Invalid HEX encountered\n"); |
| 230 |
return S2N_FAILURE; |
| 231 |
} |
| 232 |
|
| 233 |
if (max_out_bytes_len < i) { |
| 234 |
fprintf(stderr, "Insufficient memory for bytes buffer, try increasing the allocation size\n"); |
| 235 |
return S2N_FAILURE; |
| 236 |
} |
| 237 |
out_bytes[i] = high_nibble << 4 | low_nibble; |
| 238 |
|
| 239 |
i++; |
| 240 |
j += 2; |
| 241 |
} |
| 242 |
|
| 243 |
return S2N_SUCCESS; |
| 244 |
} |
| 245 |
|
| 246 |
static int s2n_get_psk_hmac_alg(s2n_psk_hmac *psk_hmac, char *hmac_str) |
| 247 |
{ |
| 248 |
GUARD_EXIT_NULL(psk_hmac); |
| 249 |
GUARD_EXIT_NULL(hmac_str); |
| 250 |
|
| 251 |
if (strcmp(hmac_str, "SHA256") == 0) { |
| 252 |
*psk_hmac = S2N_PSK_HMAC_SHA256; |
| 253 |
} else if (strcmp(hmac_str, "SHA384") == 0) { |
| 254 |
*psk_hmac = S2N_PSK_HMAC_SHA384; |
| 255 |
} else { |
| 256 |
return S2N_FAILURE; |
| 257 |
} |
| 258 |
return S2N_SUCCESS; |
| 259 |
} |
| 260 |
|
| 261 |
static int s2n_setup_external_psk(struct s2n_psk **psk, char *params) |
| 262 |
{ |
| 263 |
GUARD_EXIT_NULL(psk); |
| 264 |
GUARD_EXIT_NULL(params); |
| 265 |
|
| 266 |
|
| 267 |
char *params_dup = malloc(strlen(params) + 1); |
| 268 |
GUARD_EXIT_NULL(params_dup); |
| 269 |
strcpy(params_dup, params); |
| 270 |
|
| 271 |
size_t token_idx = 0; |
| 272 |
for (char *token = strtok(params_dup, ","); token != NULL; token = strtok(NULL, ","), token_idx++) { |
| 273 |
switch (token_idx) { |
| 274 |
case 0: |
| 275 |
GUARD_EXIT(s2n_psk_set_identity(*psk, (const uint8_t *) token, strlen(token)), |
| 276 |
"Error setting psk identity\n"); |
| 277 |
break; |
| 278 |
case 1: { |
| 279 |
uint32_t max_secret_len = strlen(token) / 2; |
| 280 |
uint8_t *secret = malloc(max_secret_len); |
| 281 |
GUARD_EXIT_NULL(secret); |
| 282 |
GUARD_EXIT(s2n_str_hex_to_bytes((const unsigned char *) token, secret, max_secret_len), "Error converting hex-encoded psk secret to bytes\n"); |
| 283 |
GUARD_EXIT(s2n_psk_set_secret(*psk, secret, max_secret_len), "Error setting psk secret\n"); |
| 284 |
free(secret); |
| 285 |
} break; |
| 286 |
case 2: { |
| 287 |
s2n_psk_hmac psk_hmac_alg = 0; |
| 288 |
GUARD_EXIT(s2n_get_psk_hmac_alg(&psk_hmac_alg, token), "Invalid psk hmac algorithm\n"); |
| 289 |
GUARD_EXIT(s2n_psk_set_hmac(*psk, psk_hmac_alg), "Error setting psk hmac algorithm\n"); |
| 290 |
} break; |
| 291 |
default: |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
free(params_dup); |
| 297 |
return S2N_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
int s2n_setup_external_psk_list(struct s2n_connection *conn, char *psk_optarg_list[S2N_MAX_PSK_LIST_LENGTH], size_t psk_list_len) |
| 301 |
{ |
| 302 |
GUARD_EXIT_NULL(conn); |
| 303 |
GUARD_EXIT_NULL(psk_optarg_list); |
| 304 |
|
| 305 |
for (size_t i = 0; i < psk_list_len; i++) { |
| 306 |
struct s2n_psk *psk = s2n_external_psk_new(); |
| 307 |
GUARD_EXIT_NULL(psk); |
| 308 |
GUARD_EXIT(s2n_setup_external_psk(&psk, psk_optarg_list[i]), "Error setting external PSK parameters\n"); |
| 309 |
GUARD_EXIT(s2n_connection_append_psk(conn, psk), "Error appending psk to the connection\n"); |
| 310 |
GUARD_EXIT(s2n_psk_free(&psk), "Error freeing psk\n"); |
| 311 |
} |
| 312 |
return S2N_SUCCESS; |
| 313 |
} |
| 314 |
|
| 315 |
int s2n_set_common_server_config(int max_early_data, struct s2n_config *config, struct conn_settings conn_settings, const char *cipher_prefs, const char *session_ticket_key_file_path) |
| 316 |
{ |
| 317 |
|
| 318 |
GUARD_EXIT(s2n_config_set_max_blinding_delay(config, 0), "Error setting blinding delay"); |
| 319 |
|
| 320 |
GUARD_EXIT(s2n_config_set_server_max_early_data_size(config, max_early_data), "Error setting max early data"); |
| 321 |
|
| 322 |
GUARD_EXIT(s2n_config_add_dhparams(config, dhparams), "Error adding DH parameters"); |
| 323 |
|
| 324 |
GUARD_EXIT(s2n_config_set_cipher_preferences(config, cipher_prefs), "Error setting cipher prefs"); |
| 325 |
|
| 326 |
GUARD_EXIT(s2n_config_set_cache_store_callback(config, cache_store_callback, session_cache), "Error setting cache store callback"); |
| 327 |
|
| 328 |
GUARD_EXIT(s2n_config_set_cache_retrieve_callback(config, cache_retrieve_callback, session_cache), "Error setting cache retrieve callback"); |
| 329 |
|
| 330 |
GUARD_EXIT(s2n_config_set_cache_delete_callback(config, cache_delete_callback, session_cache), "Error setting cache retrieve callback"); |
| 331 |
|
| 332 |
if (conn_settings.enable_mfl) { |
| 333 |
GUARD_EXIT(s2n_config_accept_max_fragment_length(config), "Error enabling TLS maximum fragment length extension in server"); |
| 334 |
} |
| 335 |
|
| 336 |
if (s2n_config_set_verify_host_callback(config, unsafe_verify_host_fn, NULL)) { |
| 337 |
print_s2n_error("Failure to set hostname verification callback"); |
| 338 |
exit(1); |
| 339 |
} |
| 340 |
|
| 341 |
if (conn_settings.session_ticket) { |
| 342 |
GUARD_EXIT(s2n_config_set_session_tickets_onoff(config, 1), "Error enabling session tickets"); |
| 343 |
} |
| 344 |
|
| 345 |
if (conn_settings.session_cache) { |
| 346 |
GUARD_EXIT(s2n_config_set_session_cache_onoff(config, 1), "Error enabling session cache using id"); |
| 347 |
} |
| 348 |
|
| 349 |
if (conn_settings.session_ticket || conn_settings.session_cache) { |
| 350 |
|
| 351 |
uint8_t *st_key = NULL; |
| 352 |
uint32_t st_key_length = 0; |
| 353 |
|
| 354 |
if (session_ticket_key_file_path) { |
| 355 |
int fd = open(session_ticket_key_file_path, O_RDONLY); |
| 356 |
GUARD_EXIT(fd, "Error opening session ticket key file"); |
| 357 |
|
| 358 |
struct stat st; |
| 359 |
GUARD_EXIT(fstat(fd, &st), "Error fstat-ing session ticket key file"); |
| 360 |
|
| 361 |
st_key = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 362 |
POSIX_ENSURE(st_key != MAP_FAILED, S2N_ERR_MMAP); |
| 363 |
|
| 364 |
st_key_length = st.st_size; |
| 365 |
|
| 366 |
close(fd); |
| 367 |
} else { |
| 368 |
st_key = default_ticket_key; |
| 369 |
st_key_length = sizeof(default_ticket_key); |
| 370 |
} |
| 371 |
|
| 372 |
if (s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name), st_key, st_key_length, 0) != 0) { |
| 373 |
fprintf(stderr, "Error adding ticket key: '%s'\n", s2n_strerror(s2n_errno, "EN")); |
| 374 |
exit(1); |
| 375 |
} |
| 376 |
} |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
int s2n_setup_server_connection(struct s2n_connection *conn, int fd, struct s2n_config *config, struct conn_settings settings) |
| 381 |
{ |
| 382 |
if (settings.deserialize_in) { |
| 383 |
GUARD_RETURN(s2n_connection_deserialize_in(conn, settings.deserialize_in), "Failed to deserialize file"); |
| 384 |
} |
| 385 |
|
| 386 |
if (settings.self_service_blinding) { |
| 387 |
s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING); |
| 388 |
} |
| 389 |
|
| 390 |
if (settings.mutual_auth) { |
| 391 |
GUARD_RETURN(s2n_config_set_client_auth_type(config, S2N_CERT_AUTH_REQUIRED), "Error setting client auth type"); |
| 392 |
|
| 393 |
if (settings.ca_dir || settings.ca_file) { |
| 394 |
GUARD_RETURN(s2n_config_set_verification_ca_location(config, settings.ca_file, settings.ca_dir), "Error adding verify location"); |
| 395 |
} |
| 396 |
|
| 397 |
if (settings.insecure) { |
| 398 |
GUARD_RETURN(s2n_config_disable_x509_verification(config), "Error disabling X.509 validation"); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
GUARD_RETURN(s2n_connection_set_config(conn, config), "Error setting configuration"); |
| 403 |
|
| 404 |
if (settings.prefer_throughput) { |
| 405 |
GUARD_RETURN(s2n_connection_prefer_throughput(conn), "Error setting prefer throughput"); |
| 406 |
} |
| 407 |
|
| 408 |
if (settings.prefer_low_latency) { |
| 409 |
GUARD_RETURN(s2n_connection_prefer_low_latency(conn), "Error setting prefer low latency"); |
| 410 |
} |
| 411 |
|
| 412 |
GUARD_RETURN(s2n_connection_set_fd(conn, fd), "Error setting file descriptor"); |
| 413 |
|
| 414 |
if (settings.use_corked_io) { |
| 415 |
GUARD_RETURN(s2n_connection_use_corked_io(conn), "Error setting corked io"); |
| 416 |
} |
| 417 |
|
| 418 |
GUARD_RETURN( |
| 419 |
s2n_setup_external_psk_list(conn, settings.psk_optarg_list, settings.psk_list_len), |
| 420 |
"Error setting external psk list"); |
| 421 |
|
| 422 |
GUARD_RETURN(early_data_recv(conn), "Error receiving early data"); |
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
int cache_store_callback(struct s2n_connection *conn, void *ctx, uint64_t ttl, const void *key, uint64_t key_size, const void *value, uint64_t value_size) |
| 427 |
{ |
| 428 |
struct session_cache_entry *cache = ctx; |
| 429 |
|
| 430 |
POSIX_ENSURE_INCLUSIVE_RANGE(1, key_size, MAX_KEY_LEN); |
| 431 |
POSIX_ENSURE_INCLUSIVE_RANGE(1, value_size, MAX_VAL_LEN); |
| 432 |
|
| 433 |
uint8_t idx = ((const uint8_t *) key)[0]; |
| 434 |
|
| 435 |
memmove(cache[idx].key, key, key_size); |
| 436 |
memmove(cache[idx].value, value, value_size); |
| 437 |
|
| 438 |
cache[idx].key_len = key_size; |
| 439 |
cache[idx].value_len = value_size; |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
int cache_retrieve_callback(struct s2n_connection *conn, void *ctx, const void *key, uint64_t key_size, void *value, uint64_t *value_size) |
| 445 |
{ |
| 446 |
struct session_cache_entry *cache = ctx; |
| 447 |
|
| 448 |
POSIX_ENSURE_INCLUSIVE_RANGE(1, key_size, MAX_KEY_LEN); |
| 449 |
|
| 450 |
uint8_t idx = ((const uint8_t *) key)[0]; |
| 451 |
|
| 452 |
POSIX_ENSURE(cache[idx].key_len == key_size, S2N_ERR_INVALID_ARGUMENT); |
| 453 |
POSIX_ENSURE(memcmp(cache[idx].key, key, key_size) == 0, S2N_ERR_INVALID_ARGUMENT); |
| 454 |
POSIX_ENSURE(*value_size >= cache[idx].value_len, S2N_ERR_INVALID_ARGUMENT); |
| 455 |
|
| 456 |
*value_size = cache[idx].value_len; |
| 457 |
memmove(value, cache[idx].value, cache[idx].value_len); |
| 458 |
|
| 459 |
for (uint64_t i = 0; i < key_size; i++) { |
| 460 |
printf("%02x", ((const uint8_t *) key)[i]); |
| 461 |
} |
| 462 |
printf("\n"); |
| 463 |
|
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
int cache_delete_callback(struct s2n_connection *conn, void *ctx, const void *key, uint64_t key_size) |
| 468 |
{ |
| 469 |
struct session_cache_entry *cache = ctx; |
| 470 |
|
| 471 |
POSIX_ENSURE_INCLUSIVE_RANGE(1, key_size, MAX_KEY_LEN); |
| 472 |
|
| 473 |
uint8_t idx = ((const uint8_t *) key)[0]; |
| 474 |
|
| 475 |
if (cache[idx].key_len != 0) { |
| 476 |
POSIX_ENSURE(cache[idx].key_len == key_size, S2N_ERR_INVALID_ARGUMENT); |
| 477 |
POSIX_ENSURE(memcmp(cache[idx].key, key, key_size) == 0, S2N_ERR_INVALID_ARGUMENT); |
| 478 |
} |
| 479 |
|
| 480 |
cache[idx].key_len = 0; |
| 481 |
cache[idx].value_len = 0; |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
uint8_t unsafe_verify_host(const char *host_name, size_t host_name_len, void *data) |
| 487 |
{ |
| 488 |
struct verify_data *verify_data = (struct verify_data *) data; |
| 489 |
|
| 490 |
if (host_name_len > 2 && host_name[0] == '*' && host_name[1] == '.') { |
| 491 |
char *suffix = strstr(verify_data->trusted_host, "."); |
| 492 |
return (uint8_t) (strcasecmp(suffix, host_name + 1) == 0); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
bool is_localhost = (strcasecmp(verify_data->trusted_host, "localhost") == 0); |
| 497 |
is_localhost |= (strcasecmp(verify_data->trusted_host, "127.0.0.1") == 0); |
| 498 |
if (is_localhost) { |
| 499 |
bool match = (strcasecmp(host_name, "localhost") == 0); |
| 500 |
match |= (strcasecmp(host_name, "127.0.0.1") == 0); |
| 501 |
|
| 502 |
match |= (strcasecmp(host_name, "s2nTestServer") == 0); |
| 503 |
return (uint8_t) match; |
| 504 |
} |
| 505 |
|
| 506 |
return (uint8_t) (strcasecmp(host_name, verify_data->trusted_host) == 0); |
| 507 |
} |
| 508 |
|
| 509 |
int wait_for_shutdown(struct s2n_connection *conn, int fd) |
| 510 |
{ |
| 511 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 512 |
while (s2n_shutdown(conn, &blocked) != S2N_SUCCESS) { |
| 513 |
int errno_val = errno; |
| 514 |
switch (s2n_error_get_type(s2n_errno)) { |
| 515 |
case S2N_ERR_T_BLOCKED: |
| 516 |
GUARD_RETURN(wait_for_event(fd, blocked), "Error polling IO for shutdown"); |
| 517 |
break; |
| 518 |
case S2N_ERR_T_CLOSED: |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
fprintf(stdout, "Connection closed by peer\n"); |
| 527 |
return S2N_SUCCESS; |
| 528 |
case S2N_ERR_T_IO: |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
if (errno_val == ECONNRESET) { |
| 533 |
fprintf(stdout, "Connection reset by peer\n"); |
| 534 |
return S2N_SUCCESS; |
| 535 |
} |
| 536 |
|
| 537 |
fprintf(stderr, "Unexpected IO error during shutdown: %s\n", strerror(errno_val)); |
| 538 |
return S2N_FAILURE; |
| 539 |
default: |
| 540 |
return S2N_FAILURE; |
| 541 |
} |
| 542 |
} |
| 543 |
return S2N_SUCCESS; |
| 544 |
} |
| 545 |
|
| 546 |
int s2n_connection_serialize_out(struct s2n_connection *conn, const char *file_path) |
| 547 |
{ |
| 548 |
uint32_t serialize_length = 0; |
| 549 |
GUARD_RETURN(s2n_connection_serialization_length(conn, &serialize_length), "Failed to get serialized connection length"); |
| 550 |
uint8_t *mem = malloc(serialize_length); |
| 551 |
GUARD_RETURN_NULL(mem); |
| 552 |
GUARD_RETURN(s2n_connection_serialize(conn, mem, serialize_length), "Failed to get serialized connection"); |
| 553 |
GUARD_RETURN(write_array_to_file(file_path, mem, serialize_length), "Failed to write serialized connection to file"); |
| 554 |
free(mem); |
| 555 |
|
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
int s2n_connection_deserialize_in(struct s2n_connection *conn, const char *file_path) |
| 560 |
{ |
| 561 |
size_t deserialize_length = 0; |
| 562 |
GUARD_RETURN(get_file_size(file_path, &deserialize_length), "Failed to read deserialize-in file size"); |
| 563 |
ENSURE_RETURN(deserialize_length <= UINT32_MAX, "deserialize-in file size is too large"); |
| 564 |
uint8_t *mem = malloc(deserialize_length); |
| 565 |
GUARD_RETURN_NULL(mem); |
| 566 |
GUARD_RETURN(load_file_to_array(file_path, mem, deserialize_length), "Failed to read deserialize-in file"); |
| 567 |
GUARD_RETURN(s2n_connection_deserialize(conn, mem, (uint32_t) deserialize_length), "Failed to deserialize connection"); |
| 568 |
free(mem); |
| 569 |
|
| 570 |
return 0; |
| 571 |
} |