| 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 <getopt.h> |
| 19 |
#include <inttypes.h> |
| 20 |
#include <netdb.h> |
| 21 |
#include <signal.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <sys/mman.h> |
| 25 |
#include <sys/socket.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#ifndef S2N_INTERN_LIBCRYPTO |
| 30 |
#include <openssl/crypto.h> |
| 31 |
#include <openssl/err.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "api/s2n.h" |
| 35 |
#include "api/unstable/npn.h" |
| 36 |
#include "common.h" |
| 37 |
#include "crypto/s2n_libcrypto.h" |
| 38 |
#include "utils/s2n_safety.h" |
| 39 |
|
| 40 |
#define MAX_CERTIFICATES 50 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static char default_certificate_chain[] = |
| 48 |
"-----BEGIN CERTIFICATE-----" |
| 49 |
"MIIDHTCCAgWgAwIBAgIUPxywpg3/+VHmj8jJSvK62XC06zMwDQYJKoZIhvcNAQEL" |
| 50 |
"BQAwHjEcMBoGA1UEAwwTczJuVGVzdEludGVybWVkaWF0ZTAgFw0yMDAxMjQwMTEw" |
| 51 |
"MjFaGA8yMTE5MTIzMTAxMTAyMVowGDEWMBQGA1UEAwwNczJuVGVzdFNlcnZlcjCC" |
| 52 |
"ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJUbMpdROM6cjb8xgr5kgKHn" |
| 53 |
"JVDfhbLg4pxBwWwlayb6/N60JLG9KzWAWhZBmz+Px6kr/1dL6+bL3mLuNBCQpYBS" |
| 54 |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WHC6VnC4mY8qubhx95FpX02ERnz" |
| 101 |
"1Cw2YWtntyO8P52dEJD1+0EJjtVX4Bj5wwgJHHbDkPP1IzFrR/uBC2LCjtRY+UtZ" |
| 102 |
"kfoDfWu2tslkLK7/LaC5qZyCPKnpPHLLz8gUWKlvbuejM99FTlBg/tcH+bv5x7WB" |
| 103 |
"MZ8=" |
| 104 |
"-----END CERTIFICATE-----"; |
| 105 |
|
| 106 |
static char default_private_key[] = |
| 107 |
"-----BEGIN RSA PRIVATE KEY-----" |
| 108 |
"MIIEogIBAAKCAQEAlRsyl1E4zpyNvzGCvmSAoeclUN+FsuDinEHBbCVrJvr83rQk" |
| 109 |
"sb0rNYBaFkGbP4/HqSv/V0vr5sveYu40EJClgFI957afsov0++wxhmadgXKf3htd" |
| 110 |
"uMEJHEZH5r2VwYcuVngL6uss2L2lTDF/tUhGCVHPAdYAWDnFNfqUKbOI0zg4ySry" |
| 111 |
"UeQuikcNSDnIjhVNzcQQaoMh/f7QGF4gdxQVshaQU6NEN0veUwYoSPT4sSvPNuEl" |
| 112 |
"zMGVEypIyK5h5paA5EulE20x42ZtHygskVyGkXQD18g7b9iD6dyVwMOGxQGSR9wg" |
| 113 |
"pHJIYO6You5gz6E4enm9Cjy1y/JDqzdTlhQtBQIDAQABAoIBAGTaSJXg8jON4LJ5" |
| 114 |
"op11DSx1U+An0B71zVEziMjFZnyvN2rLHia6dQdzEXwMVB3h+oKKp+M8DwvEyV7R" |
| 115 |
"D5ZEwCzTc9vOwqXZ1JKxZ64oqlBsX4WzrOjSaH8fanK/uRN1g/ooqKb0+xh+7ddj" |
| 116 |
"g6XyhKy5EPOE9Ca4rJOeMakjLmDuleQecT/DixYV6azhfaJoD70XZJWv3YzSpu/X" |
| 117 |
"Ma+i3of0alsG/lROjNtEXE3nKzcTUgyAUoQeYRwCVpgssg/4VAUPJNDP4dVmxW8f" |
| 118 |
"eNmjlTyXmR9S08SXkqmCHe2mBUsZY9nqcDE6ZWILZKFWIfZD9W+j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----"; |
| 134 |
|
| 135 |
#define OPT_BUFFERED_SEND 1000 |
| 136 |
#define OPT_SERIALIZE_OUT 1001 |
| 137 |
#define OPT_DESERIALIZE_IN 1002 |
| 138 |
|
| 139 |
void usage() |
| 140 |
{ |
| 141 |
|
| 142 |
fprintf(stderr, "s2nd is an s2n-tls server testing utility.\n"); |
| 143 |
fprintf(stderr, "It is not intended for production use.\n"); |
| 144 |
fprintf(stderr, "\n"); |
| 145 |
fprintf(stderr, "usage: s2nd [options] host port\n"); |
| 146 |
fprintf(stderr, " host: hostname or IP address to listen on\n"); |
| 147 |
fprintf(stderr, " port: port to listen on\n"); |
| 148 |
fprintf(stderr, "\n Options:\n\n"); |
| 149 |
fprintf(stderr, " -a [protocol]\n"); |
| 150 |
fprintf(stderr, " --alpn [protocol]\n"); |
| 151 |
fprintf(stderr, " Sets a single application protocol supported by this server.\n"); |
| 152 |
fprintf(stderr, " -c [version_string]\n"); |
| 153 |
fprintf(stderr, " --ciphers [version_string]\n"); |
| 154 |
fprintf(stderr, " Set the cipher preference version string. Defaults to \"default\" \n"); |
| 155 |
fprintf(stderr, " --enter-fips-mode\n"); |
| 156 |
fprintf(stderr, " Enter libcrypto's FIPS mode. The linked version of OpenSSL must be built with the FIPS module.\n"); |
| 157 |
fprintf(stderr, " --cert\n"); |
| 158 |
fprintf(stderr, " Path to a PEM encoded certificate [chain]. Option can be repeated to load multiple certs.\n"); |
| 159 |
fprintf(stderr, " --key\n"); |
| 160 |
fprintf(stderr, " Path to a PEM encoded private key that matches cert. Option can be repeated to load multiple certs.\n"); |
| 161 |
fprintf(stderr, " -m\n"); |
| 162 |
fprintf(stderr, " --mutualAuth\n"); |
| 163 |
fprintf(stderr, " Request a Client Certificate. Any RSA Certificate will be accepted.\n"); |
| 164 |
fprintf(stderr, " -n\n"); |
| 165 |
fprintf(stderr, " --negotiate\n"); |
| 166 |
fprintf(stderr, " Only perform tls handshake and then shutdown the connection\n"); |
| 167 |
fprintf(stderr, " --parallelize\n"); |
| 168 |
fprintf(stderr, " Create a new Connection handler thread for each new connection. Useful for tests with lots of connections.\n"); |
| 169 |
fprintf(stderr, " Warning: this option isn't compatible with TLS Resumption, since each thread gets its own Session cache.\n"); |
| 170 |
fprintf(stderr, " --prefer-low-latency\n"); |
| 171 |
fprintf(stderr, " Prefer low latency by clamping maximum outgoing record size at 1500.\n"); |
| 172 |
fprintf(stderr, " --prefer-throughput\n"); |
| 173 |
fprintf(stderr, " Prefer throughput by raising maximum outgoing record size to 16k\n"); |
| 174 |
fprintf(stderr, " --enable-mfl\n"); |
| 175 |
fprintf(stderr, " Accept client's TLS maximum fragment length extension request\n"); |
| 176 |
fprintf(stderr, " --ocsp\n"); |
| 177 |
fprintf(stderr, " Path to a DER formatted OCSP response for stapling\n"); |
| 178 |
fprintf(stderr, " -s\n"); |
| 179 |
fprintf(stderr, " --self-service-blinding\n"); |
| 180 |
fprintf(stderr, " Don't introduce 10-30 second delays on TLS Handshake errors. \n"); |
| 181 |
fprintf(stderr, " Warning: this should only be used for testing since skipping blinding may allow timing side channels.\n"); |
| 182 |
fprintf(stderr, " -t,--ca-file [file path]\n"); |
| 183 |
fprintf(stderr, " Location of trust store CA file (PEM format). If neither -t or -d are specified. System defaults will be used."); |
| 184 |
fprintf(stderr, " This option is only used if mutual auth is enabled.\n"); |
| 185 |
fprintf(stderr, " -d,--ca-dir [directory path]\n"); |
| 186 |
fprintf(stderr, " Directory containing hashed trusted certs. If neither -t or -d are specified. System defaults will be used."); |
| 187 |
fprintf(stderr, " This option is only used if mutual auth is enabled.\n"); |
| 188 |
fprintf(stderr, " -i,--insecure\n"); |
| 189 |
fprintf(stderr, " Turns off certification validation altogether.\n"); |
| 190 |
fprintf(stderr, " --stk-file\n"); |
| 191 |
fprintf(stderr, " Location of key file used for encryption and decryption of session ticket.\n"); |
| 192 |
fprintf(stderr, " -T,--no-session-ticket\n"); |
| 193 |
fprintf(stderr, " Disable session ticket for resumption.\n"); |
| 194 |
fprintf(stderr, " -C,--corked-io\n"); |
| 195 |
fprintf(stderr, " Turn on corked io\n"); |
| 196 |
fprintf(stderr, " --non-blocking\n"); |
| 197 |
fprintf(stderr, " Set the non-blocking flag on the connection's socket.\n"); |
| 198 |
fprintf(stderr, " -w --https-server\n"); |
| 199 |
fprintf(stderr, " Run s2nd in a simple https server mode.\n"); |
| 200 |
fprintf(stderr, " -b --https-bench <bytes>\n"); |
| 201 |
fprintf(stderr, " Send number of bytes in https server mode to test throughput.\n"); |
| 202 |
fprintf(stderr, " -L --key-log <path>\n"); |
| 203 |
fprintf(stderr, " Enable NSS key logging into the provided path\n"); |
| 204 |
fprintf(stderr, " -P --psk <psk-identity,psk-secret,psk-hmac-alg> \n" |
| 205 |
" A comma-separated list of psk parameters in this order: psk_identity, psk_secret and psk_hmac_alg.\n" |
| 206 |
" Note that the maximum number of permitted psks is 10, the psk-secret is hex-encoded, and whitespace is not allowed before or after the commas.\n" |
| 207 |
" Ex: --psk psk_id,psk_secret,SHA256 --psk shared_id,shared_secret,SHA384.\n"); |
| 208 |
fprintf(stderr, " -E, --max-early-data \n"); |
| 209 |
fprintf(stderr, " Sets maximum early data allowed in session tickets. \n"); |
| 210 |
fprintf(stderr, " -N --npn \n"); |
| 211 |
fprintf(stderr, " Indicates support for the NPN extension. The '--alpn' option MUST be used with this option to signal the protocols supported."); |
| 212 |
fprintf(stderr, " -h,--help\n"); |
| 213 |
fprintf(stderr, " Display this message and quit.\n"); |
| 214 |
fprintf(stderr, " --buffered-send <buffer size>\n"); |
| 215 |
fprintf(stderr, " Set s2n_send to buffer up to <buffer size> bytes before sending records over the wire.\n"); |
| 216 |
fprintf(stderr, " -X, --max-conns <max connections>\n"); |
| 217 |
fprintf(stderr, " Sets the max number of connections s2nd will accept before shutting down.\n"); |
| 218 |
|
| 219 |
exit(1); |
| 220 |
} |
| 221 |
|
| 222 |
int handle_connection(int fd, struct s2n_config *config, struct conn_settings settings) |
| 223 |
{ |
| 224 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 225 |
if (!conn) { |
| 226 |
print_s2n_error("Error getting new s2n connection"); |
| 227 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 228 |
} |
| 229 |
|
| 230 |
GUARD_EXIT(s2n_setup_server_connection(conn, fd, config, settings), "Error setting up connection"); |
| 231 |
|
| 232 |
if (!settings.deserialize_in && negotiate(conn, fd) != S2N_SUCCESS) { |
| 233 |
if (settings.mutual_auth) { |
| 234 |
if (!s2n_connection_client_cert_used(conn)) { |
| 235 |
print_s2n_error("Error: Mutual Auth was required, but not negotiated"); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 241 |
} |
| 242 |
|
| 243 |
GUARD_EXIT(s2n_connection_free_handshake(conn), "Error freeing handshake memory after negotiation"); |
| 244 |
|
| 245 |
if (settings.https_server) { |
| 246 |
https(conn, settings.https_bench); |
| 247 |
} else if (!settings.only_negotiate) { |
| 248 |
bool stop_echo = false; |
| 249 |
echo(conn, fd, &stop_echo); |
| 250 |
} |
| 251 |
|
| 252 |
if (settings.serialize_out) { |
| 253 |
GUARD_RETURN(s2n_connection_serialize_out(conn, settings.serialize_out), "Error serializing connection"); |
| 254 |
} else { |
| 255 |
GUARD_RETURN(wait_for_shutdown(conn, fd), "Error closing connection"); |
| 256 |
} |
| 257 |
|
| 258 |
GUARD_RETURN(s2n_connection_wipe(conn), "Error wiping connection"); |
| 259 |
|
| 260 |
GUARD_RETURN(s2n_connection_free(conn), "Error freeing connection"); |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
int main(int argc, char *const *argv) |
| 266 |
{ |
| 267 |
struct addrinfo hints, *ai = NULL; |
| 268 |
int r = 0, sockfd = 0; |
| 269 |
|
| 270 |
|
| 271 |
const char *host = NULL; |
| 272 |
const char *port = NULL; |
| 273 |
|
| 274 |
const char *ocsp_response_file_path = NULL; |
| 275 |
const char *session_ticket_key_file_path = NULL; |
| 276 |
const char *cipher_prefs = "default"; |
| 277 |
const char *alpn = NULL; |
| 278 |
const char *key_log_path = NULL; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
int num_user_certificates = 0; |
| 285 |
int num_user_private_keys = 0; |
| 286 |
const char *certificates[MAX_CERTIFICATES] = { 0 }; |
| 287 |
const char *private_keys[MAX_CERTIFICATES] = { 0 }; |
| 288 |
|
| 289 |
struct conn_settings conn_settings = { 0 }; |
| 290 |
int fips_mode = 0; |
| 291 |
int parallelize = 0; |
| 292 |
int non_blocking = 0; |
| 293 |
long int bytes = 0; |
| 294 |
conn_settings.session_ticket = 1; |
| 295 |
conn_settings.session_cache = 1; |
| 296 |
conn_settings.max_conns = -1; |
| 297 |
conn_settings.psk_list_len = 0; |
| 298 |
int max_early_data = 0; |
| 299 |
uint32_t send_buffer_size = 0; |
| 300 |
bool npn = false; |
| 301 |
|
| 302 |
struct option long_options[] = { |
| 303 |
{ "ciphers", required_argument, NULL, 'c' }, |
| 304 |
{ "enable-mfl", no_argument, NULL, 'e' }, |
| 305 |
{ "enter-fips-mode", no_argument, NULL, 'f' }, |
| 306 |
{ "help", no_argument, NULL, 'h' }, |
| 307 |
{ "key", required_argument, NULL, 'k' }, |
| 308 |
{ "prefer-low-latency", no_argument, NULL, 'l' }, |
| 309 |
{ "mutualAuth", no_argument, NULL, 'm' }, |
| 310 |
{ "negotiate", no_argument, NULL, 'n' }, |
| 311 |
{ "ocsp", required_argument, NULL, 'o' }, |
| 312 |
{ "parallelize", no_argument, ¶llelize, 1 }, |
| 313 |
{ "prefer-throughput", no_argument, NULL, 'p' }, |
| 314 |
{ "cert", required_argument, NULL, 'r' }, |
| 315 |
{ "self-service-blinding", no_argument, NULL, 's' }, |
| 316 |
{ "ca-dir", required_argument, 0, 'd' }, |
| 317 |
{ "ca-file", required_argument, 0, 't' }, |
| 318 |
{ "insecure", no_argument, 0, 'i' }, |
| 319 |
{ "stk-file", required_argument, 0, 'a' }, |
| 320 |
{ "serialize-out", required_argument, 0, OPT_SERIALIZE_OUT }, |
| 321 |
{ "deserialize-in", required_argument, 0, OPT_DESERIALIZE_IN }, |
| 322 |
{ "no-session-ticket", no_argument, 0, 'T' }, |
| 323 |
{ "corked-io", no_argument, 0, 'C' }, |
| 324 |
{ "max-conns", optional_argument, 0, 'X' }, |
| 325 |
{ "tls13", no_argument, 0, '3' }, |
| 326 |
{ "https-server", no_argument, 0, 'w' }, |
| 327 |
{ "https-bench", required_argument, 0, 'b' }, |
| 328 |
{ "alpn", required_argument, 0, 'A' }, |
| 329 |
{ "npn", no_argument, 0, 'N' }, |
| 330 |
{ "non-blocking", no_argument, 0, 'B' }, |
| 331 |
{ "key-log", required_argument, 0, 'L' }, |
| 332 |
{ "psk", required_argument, 0, 'P' }, |
| 333 |
{ "max-early-data", required_argument, 0, 'E' }, |
| 334 |
{ "buffered-send", required_argument, 0, OPT_BUFFERED_SEND }, |
| 335 |
|
| 336 |
{ 0 }, |
| 337 |
}; |
| 338 |
while (1) { |
| 339 |
int option_index = 0; |
| 340 |
int c = getopt_long(argc, argv, "c:hmnst:d:iTCX::wb:A:P:E:", long_options, &option_index); |
| 341 |
if (c == -1) { |
| 342 |
break; |
| 343 |
} |
| 344 |
|
| 345 |
switch (c) { |
| 346 |
case 0: |
| 347 |
|
| 348 |
break; |
| 349 |
case 'C': |
| 350 |
conn_settings.use_corked_io = 1; |
| 351 |
break; |
| 352 |
case 'c': |
| 353 |
cipher_prefs = optarg; |
| 354 |
break; |
| 355 |
case 'e': |
| 356 |
conn_settings.enable_mfl = 1; |
| 357 |
break; |
| 358 |
case 'f': |
| 359 |
fips_mode = 1; |
| 360 |
break; |
| 361 |
case 'h': |
| 362 |
usage(); |
| 363 |
break; |
| 364 |
case 'k': |
| 365 |
if (num_user_private_keys == MAX_CERTIFICATES) { |
| 366 |
fprintf(stderr, "Cannot support more than %d certificates!\n", MAX_CERTIFICATES); |
| 367 |
exit(1); |
| 368 |
} |
| 369 |
private_keys[num_user_private_keys] = load_file_to_cstring(optarg); |
| 370 |
num_user_private_keys++; |
| 371 |
break; |
| 372 |
case 'l': |
| 373 |
conn_settings.prefer_low_latency = 1; |
| 374 |
break; |
| 375 |
case 'm': |
| 376 |
conn_settings.mutual_auth = 1; |
| 377 |
break; |
| 378 |
case 'n': |
| 379 |
conn_settings.only_negotiate = 1; |
| 380 |
break; |
| 381 |
case 'o': |
| 382 |
ocsp_response_file_path = optarg; |
| 383 |
break; |
| 384 |
case 'p': |
| 385 |
conn_settings.prefer_throughput = 1; |
| 386 |
break; |
| 387 |
case 'r': |
| 388 |
if (num_user_certificates == MAX_CERTIFICATES) { |
| 389 |
fprintf(stderr, "Cannot support more than %d certificates!\n", MAX_CERTIFICATES); |
| 390 |
exit(1); |
| 391 |
} |
| 392 |
certificates[num_user_certificates] = load_file_to_cstring(optarg); |
| 393 |
num_user_certificates++; |
| 394 |
break; |
| 395 |
case 's': |
| 396 |
conn_settings.self_service_blinding = 1; |
| 397 |
break; |
| 398 |
case 'd': |
| 399 |
conn_settings.ca_dir = optarg; |
| 400 |
break; |
| 401 |
case 't': |
| 402 |
conn_settings.ca_file = optarg; |
| 403 |
break; |
| 404 |
case 'i': |
| 405 |
conn_settings.insecure = 1; |
| 406 |
break; |
| 407 |
case 'a': |
| 408 |
session_ticket_key_file_path = optarg; |
| 409 |
break; |
| 410 |
case 'T': |
| 411 |
conn_settings.session_ticket = 0; |
| 412 |
break; |
| 413 |
case '3': |
| 414 |
|
| 415 |
break; |
| 416 |
case 'X': |
| 417 |
if (optarg == NULL) { |
| 418 |
conn_settings.max_conns = 1; |
| 419 |
} else { |
| 420 |
conn_settings.max_conns = atoi(optarg); |
| 421 |
} |
| 422 |
break; |
| 423 |
case 'w': |
| 424 |
fprintf(stdout, "Running s2nd in simple https server mode\n"); |
| 425 |
conn_settings.https_server = 1; |
| 426 |
break; |
| 427 |
case 'b': |
| 428 |
bytes = strtoul(optarg, NULL, 10); |
| 429 |
GUARD_EXIT(bytes, "https-bench bytes needs to be some positive long value."); |
| 430 |
conn_settings.https_bench = bytes; |
| 431 |
break; |
| 432 |
case OPT_BUFFERED_SEND: { |
| 433 |
intmax_t send_buffer_size_scanned_value = strtoimax(optarg, 0, 10); |
| 434 |
if (send_buffer_size_scanned_value > UINT32_MAX || send_buffer_size_scanned_value < 0) { |
| 435 |
fprintf(stderr, "<buffer size> must be a positive 32 bit value\n"); |
| 436 |
exit(1); |
| 437 |
} |
| 438 |
send_buffer_size = (uint32_t) send_buffer_size_scanned_value; |
| 439 |
break; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
case OPT_SERIALIZE_OUT: |
| 452 |
conn_settings.serialize_out = optarg; |
| 453 |
break; |
| 454 |
case OPT_DESERIALIZE_IN: |
| 455 |
conn_settings.deserialize_in = optarg; |
| 456 |
break; |
| 457 |
case 'A': |
| 458 |
alpn = optarg; |
| 459 |
break; |
| 460 |
case 'B': |
| 461 |
non_blocking = 1; |
| 462 |
break; |
| 463 |
case 'L': |
| 464 |
key_log_path = optarg; |
| 465 |
break; |
| 466 |
case 'P': |
| 467 |
if (conn_settings.psk_list_len >= S2N_MAX_PSK_LIST_LENGTH) { |
| 468 |
fprintf(stderr, "Error setting psks, maximum number of psks permitted is 10.\n"); |
| 469 |
exit(1); |
| 470 |
} |
| 471 |
conn_settings.psk_optarg_list[conn_settings.psk_list_len++] = optarg; |
| 472 |
break; |
| 473 |
case 'E': |
| 474 |
max_early_data = atoi(optarg); |
| 475 |
break; |
| 476 |
case 'N': |
| 477 |
npn = true; |
| 478 |
break; |
| 479 |
case '?': |
| 480 |
default: |
| 481 |
fprintf(stdout, "getopt_long returned: %d", c); |
| 482 |
usage(); |
| 483 |
break; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
if (conn_settings.prefer_throughput && conn_settings.prefer_low_latency) { |
| 488 |
fprintf(stderr, "prefer-throughput and prefer-low-latency options are mutually exclusive\n"); |
| 489 |
exit(1); |
| 490 |
} |
| 491 |
|
| 492 |
if (optind < argc) { |
| 493 |
host = argv[optind++]; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
if (optind < argc) { |
| 498 |
port = argv[optind++]; |
| 499 |
} |
| 500 |
|
| 501 |
if (!host || !port) { |
| 502 |
usage(); |
| 503 |
} |
| 504 |
|
| 505 |
if (setvbuf(stdin, NULL, _IONBF, 0) < 0) { |
| 506 |
fprintf(stderr, "Error disabling buffering for stdin\n"); |
| 507 |
exit(1); |
| 508 |
} |
| 509 |
|
| 510 |
if (setvbuf(stdout, NULL, _IONBF, 0) < 0) { |
| 511 |
fprintf(stderr, "Error disabling buffering for stdout\n"); |
| 512 |
exit(1); |
| 513 |
} |
| 514 |
|
| 515 |
memset(&hints, 0, sizeof(hints)); |
| 516 |
|
| 517 |
hints.ai_family = AF_UNSPEC; |
| 518 |
hints.ai_socktype = SOCK_STREAM; |
| 519 |
|
| 520 |
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { |
| 521 |
fprintf(stderr, "Error disabling SIGPIPE\n"); |
| 522 |
exit(1); |
| 523 |
} |
| 524 |
|
| 525 |
if ((r = getaddrinfo(host, port, &hints, &ai)) < 0) { |
| 526 |
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(r)); |
| 527 |
exit(1); |
| 528 |
} |
| 529 |
|
| 530 |
if ((sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { |
| 531 |
fprintf(stderr, "socket error: %s\n", strerror(errno)); |
| 532 |
exit(1); |
| 533 |
} |
| 534 |
|
| 535 |
r = 1; |
| 536 |
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(int)) < 0) { |
| 537 |
fprintf(stderr, "setsockopt error: %s\n", strerror(errno)); |
| 538 |
exit(1); |
| 539 |
} |
| 540 |
|
| 541 |
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 542 |
fprintf(stderr, "bind error: %s\n", strerror(errno)); |
| 543 |
exit(1); |
| 544 |
} |
| 545 |
|
| 546 |
if (listen(sockfd, 1) == -1) { |
| 547 |
fprintf(stderr, "listen error: %s\n", strerror(errno)); |
| 548 |
exit(1); |
| 549 |
} |
| 550 |
|
| 551 |
GUARD_EXIT(s2n_init(), "Error running s2n_init()"); |
| 552 |
printf("libcrypto: %s\n", s2n_libcrypto_get_version_name()); |
| 553 |
|
| 554 |
if (fips_mode) { |
| 555 |
s2n_fips_mode mode = 0; |
| 556 |
GUARD_EXIT(s2n_get_fips_mode(&mode), "Unable to retrieve FIPS mode"); |
| 557 |
if (mode != S2N_FIPS_MODE_ENABLED) { |
| 558 |
fprintf(stderr, "FIPS mode not enabled: libcrypto does not support FIPS\n"); |
| 559 |
exit(1); |
| 560 |
} |
| 561 |
printf("s2nd entered FIPS mode\n"); |
| 562 |
} |
| 563 |
|
| 564 |
printf("Listening on %s:%s\n", host, port); |
| 565 |
|
| 566 |
struct s2n_config *config = s2n_config_new(); |
| 567 |
if (!config) { |
| 568 |
print_s2n_error("Error getting new s2n config"); |
| 569 |
exit(1); |
| 570 |
} |
| 571 |
|
| 572 |
if (num_user_certificates != num_user_private_keys) { |
| 573 |
fprintf(stderr, "Mismatched certificate(%d) and private key(%d) count!\n", num_user_certificates, num_user_private_keys); |
| 574 |
exit(1); |
| 575 |
} |
| 576 |
|
| 577 |
int num_certificates = 0; |
| 578 |
if (num_user_certificates == 0) { |
| 579 |
certificates[0] = default_certificate_chain; |
| 580 |
private_keys[0] = default_private_key; |
| 581 |
num_certificates = 1; |
| 582 |
} else { |
| 583 |
num_certificates = num_user_certificates; |
| 584 |
} |
| 585 |
|
| 586 |
for (int i = 0; i < num_certificates; i++) { |
| 587 |
struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(); |
| 588 |
GUARD_EXIT(s2n_cert_chain_and_key_load_pem(chain_and_key, certificates[i], private_keys[i]), "Error getting certificate/key"); |
| 589 |
|
| 590 |
if (ocsp_response_file_path) { |
| 591 |
int fd = open(ocsp_response_file_path, O_RDONLY); |
| 592 |
if (fd < 0) { |
| 593 |
fprintf(stderr, "Error opening OCSP response file: '%s'\n", strerror(errno)); |
| 594 |
exit(1); |
| 595 |
} |
| 596 |
|
| 597 |
struct stat st = { 0 }; |
| 598 |
if (fstat(fd, &st) < 0) { |
| 599 |
fprintf(stderr, "Error fstat-ing OCSP response file: '%s'\n", strerror(errno)); |
| 600 |
exit(1); |
| 601 |
} |
| 602 |
|
| 603 |
uint8_t *ocsp_response = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 604 |
if (s2n_cert_chain_and_key_set_ocsp_data(chain_and_key, ocsp_response, st.st_size) < 0) { |
| 605 |
fprintf(stderr, "Error adding ocsp response: '%s'\n", s2n_strerror(s2n_errno, "EN")); |
| 606 |
exit(1); |
| 607 |
} |
| 608 |
|
| 609 |
close(fd); |
| 610 |
} |
| 611 |
|
| 612 |
GUARD_EXIT(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key), "Error setting certificate/key"); |
| 613 |
} |
| 614 |
|
| 615 |
s2n_set_common_server_config(max_early_data, config, conn_settings, cipher_prefs, session_ticket_key_file_path); |
| 616 |
|
| 617 |
if (parallelize) { |
| 618 |
struct sigaction sa; |
| 619 |
|
| 620 |
sa.sa_handler = SIG_IGN; |
| 621 |
#if defined(SA_NOCLDWAIT) |
| 622 |
sa.sa_flags = SA_NOCLDWAIT; |
| 623 |
#endif |
| 624 |
sigemptyset(&sa.sa_mask); |
| 625 |
sigaction(SIGCHLD, &sa, NULL); |
| 626 |
} |
| 627 |
|
| 628 |
if (alpn) { |
| 629 |
const char *protocols[] = { alpn }; |
| 630 |
GUARD_EXIT(s2n_config_set_protocol_preferences(config, protocols, s2n_array_len(protocols)), "Failed to set alpn"); |
| 631 |
} |
| 632 |
|
| 633 |
if (send_buffer_size != 0) { |
| 634 |
GUARD_EXIT(s2n_config_set_send_buffer_size(config, send_buffer_size), "Error setting send buffer size."); |
| 635 |
} |
| 636 |
|
| 637 |
if (npn) { |
| 638 |
GUARD_EXIT(s2n_config_set_npn(config, 1), "Error setting npn support"); |
| 639 |
} |
| 640 |
|
| 641 |
if (conn_settings.serialize_out) { |
| 642 |
GUARD_EXIT(s2n_config_set_serialization_version(config, S2N_SERIALIZED_CONN_V1), |
| 643 |
"Error setting serialized version"); |
| 644 |
} |
| 645 |
|
| 646 |
FILE *key_log_file = NULL; |
| 647 |
|
| 648 |
if (key_log_path) { |
| 649 |
key_log_file = fopen(key_log_path, "a"); |
| 650 |
GUARD_EXIT(key_log_file == NULL ? S2N_FAILURE : S2N_SUCCESS, "Failed to open key log file"); |
| 651 |
GUARD_EXIT( |
| 652 |
s2n_config_set_key_log_cb( |
| 653 |
config, |
| 654 |
key_log_callback, |
| 655 |
(void *) key_log_file), |
| 656 |
"Failed to set key log callback"); |
| 657 |
} |
| 658 |
|
| 659 |
int fd = 0; |
| 660 |
while ((fd = accept(sockfd, ai->ai_addr, &ai->ai_addrlen)) > 0) { |
| 661 |
if (non_blocking) { |
| 662 |
int flags = fcntl(sockfd, F_GETFL, 0); |
| 663 |
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 664 |
fprintf(stderr, "fcntl error: %s\n", strerror(errno)); |
| 665 |
exit(1); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
if (!parallelize) { |
| 670 |
int rc = handle_connection(fd, config, conn_settings); |
| 671 |
close(fd); |
| 672 |
if (rc < 0) { |
| 673 |
exit(rc); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
if (conn_settings.max_conns > 0) { |
| 679 |
if (conn_settings.max_conns-- == 1) { |
| 680 |
GUARD_EXIT(s2n_cleanup(), "Error running s2n_cleanup()"); |
| 681 |
exit(0); |
| 682 |
} |
| 683 |
} |
| 684 |
} else { |
| 685 |
|
| 686 |
pid_t child_pid = fork(); |
| 687 |
|
| 688 |
if (child_pid == 0) { |
| 689 |
|
| 690 |
int rc = handle_connection(fd, config, conn_settings); |
| 691 |
close(fd); |
| 692 |
_exit(rc); |
| 693 |
} else if (child_pid == -1) { |
| 694 |
close(fd); |
| 695 |
print_s2n_error("Error calling fork(). Acceptor unable to start handler."); |
| 696 |
exit(1); |
| 697 |
} else { |
| 698 |
|
| 699 |
close(fd); |
| 700 |
continue; |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
if (key_log_file) { |
| 706 |
fclose(key_log_file); |
| 707 |
} |
| 708 |
|
| 709 |
GUARD_EXIT(s2n_cleanup(), "Error running s2n_cleanup()"); |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |