| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef _S2N_PRELUDE_INCLUDED |
| 17 |
|
| 18 |
#error "Expected s2n_prelude.h to be included as part of the compiler flags" |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <strings.h> |
| 22 |
#include <time.h> |
| 23 |
|
| 24 |
#include "api/unstable/allow_ip_in_cn.h" |
| 25 |
#include "api/unstable/custom_x509_extensions.h" |
| 26 |
#include "api/unstable/npn.h" |
| 27 |
#include "crypto/s2n_certificate.h" |
| 28 |
#include "crypto/s2n_fips.h" |
| 29 |
#include "crypto/s2n_hkdf.h" |
| 30 |
#include "crypto/s2n_libcrypto.h" |
| 31 |
#include "crypto/s2n_pq.h" |
| 32 |
#include "error/s2n_errno.h" |
| 33 |
#include "tls/s2n_cipher_preferences.h" |
| 34 |
#include "tls/s2n_internal.h" |
| 35 |
#include "tls/s2n_ktls.h" |
| 36 |
#include "tls/s2n_security_policies.h" |
| 37 |
#include "tls/s2n_tls13.h" |
| 38 |
#include "utils/s2n_blob.h" |
| 39 |
#include "utils/s2n_map.h" |
| 40 |
#include "utils/s2n_safety.h" |
| 41 |
|
| 42 |
#if defined(CLOCK_MONOTONIC_RAW) |
| 43 |
#define S2N_CLOCK_HW CLOCK_MONOTONIC_RAW |
| 44 |
#else |
| 45 |
#define S2N_CLOCK_HW CLOCK_MONOTONIC |
| 46 |
#endif |
| 47 |
|
| 48 |
#define S2N_CLOCK_SYS CLOCK_REALTIME |
| 49 |
|
| 50 |
int s2n_default_monotonic_clock(void *unused_data, uint64_t *nanoseconds) |
| 51 |
{ |
| 52 |
struct timespec current_time = { 0 }; |
| 53 |
|
| 54 |
POSIX_GUARD(clock_gettime(S2N_CLOCK_HW, ¤t_time)); |
| 55 |
|
| 56 |
*nanoseconds = (uint64_t) current_time.tv_sec * 1000000000ull; |
| 57 |
*nanoseconds += current_time.tv_nsec; |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static int wall_clock(void *data, uint64_t *nanoseconds) |
| 63 |
{ |
| 64 |
struct timespec current_time = { 0 }; |
| 65 |
|
| 66 |
POSIX_GUARD(clock_gettime(S2N_CLOCK_SYS, ¤t_time)); |
| 67 |
|
| 68 |
*nanoseconds = (uint64_t) current_time.tv_sec * 1000000000ull; |
| 69 |
*nanoseconds += current_time.tv_nsec; |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static struct s2n_config s2n_default_config = { 0 }; |
| 75 |
static struct s2n_config s2n_default_fips_config = { 0 }; |
| 76 |
static struct s2n_config s2n_default_tls13_config = { 0 }; |
| 77 |
|
| 78 |
static int s2n_config_setup_default(struct s2n_config *config) |
| 79 |
{ |
| 80 |
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default")); |
| 81 |
return S2N_SUCCESS; |
| 82 |
} |
| 83 |
|
| 84 |
static int s2n_config_setup_tls13(struct s2n_config *config) |
| 85 |
{ |
| 86 |
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 87 |
return S2N_SUCCESS; |
| 88 |
} |
| 89 |
|
| 90 |
static int s2n_config_setup_fips(struct s2n_config *config) |
| 91 |
{ |
| 92 |
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default_fips")); |
| 93 |
return S2N_SUCCESS; |
| 94 |
} |
| 95 |
|
| 96 |
static int s2n_config_init(struct s2n_config *config) |
| 97 |
{ |
| 98 |
config->wall_clock = wall_clock; |
| 99 |
config->monotonic_clock = s2n_default_monotonic_clock; |
| 100 |
config->ct_type = S2N_CT_SUPPORT_NONE; |
| 101 |
config->mfl_code = S2N_TLS_MAX_FRAG_LEN_EXT_NONE; |
| 102 |
config->alert_behavior = S2N_ALERT_FAIL_ON_WARNINGS; |
| 103 |
config->session_state_lifetime_in_nanos = S2N_STATE_LIFETIME_IN_NANOS; |
| 104 |
config->encrypt_decrypt_key_lifetime_in_nanos = S2N_TICKET_ENCRYPT_DECRYPT_KEY_LIFETIME_IN_NANOS; |
| 105 |
config->decrypt_key_lifetime_in_nanos = S2N_TICKET_DECRYPT_KEY_LIFETIME_IN_NANOS; |
| 106 |
config->async_pkey_validation_mode = S2N_ASYNC_PKEY_VALIDATION_FAST; |
| 107 |
config->check_ocsp = 1; |
| 108 |
|
| 109 |
config->client_hello_cb_mode = S2N_CLIENT_HELLO_CB_BLOCKING; |
| 110 |
|
| 111 |
POSIX_GUARD(s2n_config_setup_default(config)); |
| 112 |
if (s2n_use_default_tls13_config()) { |
| 113 |
POSIX_GUARD(s2n_config_setup_tls13(config)); |
| 114 |
} else if (s2n_is_in_fips_mode()) { |
| 115 |
POSIX_GUARD(s2n_config_setup_fips(config)); |
| 116 |
} |
| 117 |
|
| 118 |
POSIX_GUARD_PTR(config->domain_name_to_cert_map = s2n_map_new_with_initial_capacity(1)); |
| 119 |
POSIX_GUARD_RESULT(s2n_map_complete(config->domain_name_to_cert_map)); |
| 120 |
|
| 121 |
s2n_x509_trust_store_init_empty(&config->trust_store); |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int s2n_config_cleanup(struct s2n_config *config) |
| 127 |
{ |
| 128 |
s2n_x509_trust_store_wipe(&config->trust_store); |
| 129 |
config->check_ocsp = 0; |
| 130 |
|
| 131 |
if (config->custom_x509_extension_oids) { |
| 132 |
sk_ASN1_OBJECT_pop_free(config->custom_x509_extension_oids, ASN1_OBJECT_free); |
| 133 |
config->custom_x509_extension_oids = NULL; |
| 134 |
} |
| 135 |
|
| 136 |
POSIX_GUARD(s2n_config_free_session_ticket_keys(config)); |
| 137 |
POSIX_GUARD(s2n_config_free_cert_chain_and_key(config)); |
| 138 |
POSIX_GUARD(s2n_config_free_dhparams(config)); |
| 139 |
POSIX_GUARD(s2n_free(&config->application_protocols)); |
| 140 |
POSIX_GUARD(s2n_free(&config->cert_authorities)); |
| 141 |
POSIX_GUARD_RESULT(s2n_map_free(config->domain_name_to_cert_map)); |
| 142 |
|
| 143 |
POSIX_CHECKED_MEMSET(config, 0, sizeof(struct s2n_config)); |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int s2n_config_update_domain_name_to_cert_map(struct s2n_config *config, |
| 149 |
struct s2n_blob *name, |
| 150 |
struct s2n_cert_chain_and_key *cert_key_pair) |
| 151 |
{ |
| 152 |
POSIX_ENSURE_REF(config); |
| 153 |
POSIX_ENSURE_REF(name); |
| 154 |
|
| 155 |
struct s2n_map *domain_name_to_cert_map = config->domain_name_to_cert_map; |
| 156 |
|
| 157 |
if (name->size == 0) { |
| 158 |
return 0; |
| 159 |
} |
| 160 |
s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pair); |
| 161 |
struct s2n_blob s2n_map_value = { 0 }; |
| 162 |
bool key_found = false; |
| 163 |
POSIX_GUARD_RESULT(s2n_map_lookup(domain_name_to_cert_map, name, &s2n_map_value, &key_found)); |
| 164 |
if (!key_found) { |
| 165 |
struct certs_by_type value = { { 0 } }; |
| 166 |
value.certs[cert_type] = cert_key_pair; |
| 167 |
s2n_map_value.data = (uint8_t *) &value; |
| 168 |
s2n_map_value.size = sizeof(struct certs_by_type); |
| 169 |
|
| 170 |
POSIX_GUARD_RESULT(s2n_map_unlock(domain_name_to_cert_map)); |
| 171 |
|
| 172 |
int add_result = s2n_result_is_ok(s2n_map_add(domain_name_to_cert_map, name, &s2n_map_value)) ? S2N_SUCCESS : S2N_FAILURE; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
POSIX_GUARD_RESULT(s2n_map_complete(domain_name_to_cert_map)); |
| 181 |
POSIX_GUARD(add_result); |
| 182 |
} else { |
| 183 |
struct certs_by_type *value = (void *) s2n_map_value.data; |
| 184 |
if (value->certs[cert_type] == NULL) { |
| 185 |
value->certs[cert_type] = cert_key_pair; |
| 186 |
} else if (config->cert_tiebreak_cb) { |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
struct s2n_cert_chain_and_key *winner = config->cert_tiebreak_cb( |
| 193 |
value->certs[cert_type], |
| 194 |
cert_key_pair, |
| 195 |
name->data, |
| 196 |
name->size); |
| 197 |
if (winner) { |
| 198 |
value->certs[cert_type] = winner; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
int s2n_config_build_domain_name_to_cert_map(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair) |
| 207 |
{ |
| 208 |
uint32_t cn_len = 0; |
| 209 |
POSIX_GUARD_RESULT(s2n_array_num_elements(cert_key_pair->cn_names, &cn_len)); |
| 210 |
uint32_t san_len = 0; |
| 211 |
POSIX_GUARD_RESULT(s2n_array_num_elements(cert_key_pair->san_names, &san_len)); |
| 212 |
|
| 213 |
if (san_len == 0) { |
| 214 |
for (uint32_t i = 0; i < cn_len; i++) { |
| 215 |
struct s2n_blob *cn_name = NULL; |
| 216 |
POSIX_GUARD_RESULT(s2n_array_get(cert_key_pair->cn_names, i, (void **) &cn_name)); |
| 217 |
POSIX_GUARD(s2n_config_update_domain_name_to_cert_map(config, cn_name, cert_key_pair)); |
| 218 |
} |
| 219 |
} else { |
| 220 |
for (uint32_t i = 0; i < san_len; i++) { |
| 221 |
struct s2n_blob *san_name = NULL; |
| 222 |
POSIX_GUARD_RESULT(s2n_array_get(cert_key_pair->san_names, i, (void **) &san_name)); |
| 223 |
POSIX_GUARD(s2n_config_update_domain_name_to_cert_map(config, san_name, cert_key_pair)); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
struct s2n_config *s2n_fetch_default_config(void) |
| 231 |
{ |
| 232 |
if (s2n_use_default_tls13_config()) { |
| 233 |
return &s2n_default_tls13_config; |
| 234 |
} |
| 235 |
if (s2n_is_in_fips_mode()) { |
| 236 |
return &s2n_default_fips_config; |
| 237 |
} |
| 238 |
return &s2n_default_config; |
| 239 |
} |
| 240 |
|
| 241 |
int s2n_config_set_unsafe_for_testing(struct s2n_config *config) |
| 242 |
{ |
| 243 |
POSIX_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST); |
| 244 |
config->check_ocsp = 0; |
| 245 |
config->disable_x509_validation = 1; |
| 246 |
|
| 247 |
return S2N_SUCCESS; |
| 248 |
} |
| 249 |
|
| 250 |
int s2n_config_defaults_init(void) |
| 251 |
{ |
| 252 |
|
| 253 |
if (s2n_is_in_fips_mode()) { |
| 254 |
POSIX_GUARD(s2n_config_init(&s2n_default_fips_config)); |
| 255 |
POSIX_GUARD(s2n_config_setup_fips(&s2n_default_fips_config)); |
| 256 |
POSIX_GUARD(s2n_config_load_system_certs(&s2n_default_fips_config)); |
| 257 |
} else { |
| 258 |
|
| 259 |
POSIX_GUARD(s2n_config_init(&s2n_default_config)); |
| 260 |
POSIX_GUARD(s2n_config_setup_default(&s2n_default_config)); |
| 261 |
POSIX_GUARD(s2n_config_load_system_certs(&s2n_default_config)); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
POSIX_GUARD(s2n_config_init(&s2n_default_tls13_config)); |
| 266 |
POSIX_GUARD(s2n_config_setup_tls13(&s2n_default_tls13_config)); |
| 267 |
|
| 268 |
return S2N_SUCCESS; |
| 269 |
} |
| 270 |
|
| 271 |
void s2n_wipe_static_configs(void) |
| 272 |
{ |
| 273 |
s2n_config_cleanup(&s2n_default_fips_config); |
| 274 |
s2n_config_cleanup(&s2n_default_config); |
| 275 |
s2n_config_cleanup(&s2n_default_tls13_config); |
| 276 |
} |
| 277 |
|
| 278 |
int s2n_config_load_system_certs(struct s2n_config *config) |
| 279 |
{ |
| 280 |
POSIX_ENSURE_REF(config); |
| 281 |
|
| 282 |
struct s2n_x509_trust_store *store = &config->trust_store; |
| 283 |
POSIX_ENSURE(!store->loaded_system_certs, S2N_ERR_X509_TRUST_STORE); |
| 284 |
|
| 285 |
if (!store->trust_store) { |
| 286 |
store->trust_store = X509_STORE_new(); |
| 287 |
POSIX_ENSURE_REF(store->trust_store); |
| 288 |
} |
| 289 |
|
| 290 |
int err_code = X509_STORE_set_default_paths(store->trust_store); |
| 291 |
if (!err_code) { |
| 292 |
s2n_x509_trust_store_wipe(store); |
| 293 |
POSIX_BAIL(S2N_ERR_X509_TRUST_STORE); |
| 294 |
} |
| 295 |
store->loaded_system_certs = true; |
| 296 |
|
| 297 |
return S2N_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
struct s2n_config *s2n_config_new_minimal(void) |
| 301 |
{ |
| 302 |
struct s2n_blob allocator = { 0 }; |
| 303 |
struct s2n_config *new_config = NULL; |
| 304 |
|
| 305 |
PTR_GUARD_POSIX(s2n_alloc(&allocator, sizeof(struct s2n_config))); |
| 306 |
PTR_GUARD_POSIX(s2n_blob_zero(&allocator)); |
| 307 |
|
| 308 |
new_config = (struct s2n_config *) (void *) allocator.data; |
| 309 |
if (s2n_config_init(new_config) != S2N_SUCCESS) { |
| 310 |
s2n_free(&allocator); |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
|
| 314 |
return new_config; |
| 315 |
} |
| 316 |
|
| 317 |
struct s2n_config *s2n_config_new(void) |
| 318 |
{ |
| 319 |
struct s2n_config *new_config = s2n_config_new_minimal(); |
| 320 |
PTR_ENSURE_REF(new_config); |
| 321 |
|
| 322 |
|
| 323 |
PTR_GUARD_POSIX(s2n_config_load_system_certs(new_config)); |
| 324 |
|
| 325 |
return new_config; |
| 326 |
} |
| 327 |
|
| 328 |
int s2n_config_init_session_ticket_keys(struct s2n_config *config) |
| 329 |
{ |
| 330 |
if (config->ticket_keys == NULL) { |
| 331 |
POSIX_ENSURE_REF(config->ticket_keys = s2n_array_new_with_capacity(sizeof(struct s2n_ticket_key), S2N_MAX_TICKET_KEYS)); |
| 332 |
} |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
int s2n_config_free_session_ticket_keys(struct s2n_config *config) |
| 338 |
{ |
| 339 |
if (config->ticket_keys != NULL) { |
| 340 |
POSIX_GUARD_RESULT(s2n_array_free_p(&config->ticket_keys)); |
| 341 |
} |
| 342 |
|
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
int s2n_config_free_cert_chain_and_key(struct s2n_config *config) |
| 347 |
{ |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
POSIX_ENSURE_REF(config); |
| 357 |
if (config->cert_ownership != S2N_LIB_OWNED) { |
| 358 |
return S2N_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 364 |
s2n_cert_chain_and_key_free(config->default_certs_by_type.certs[i]); |
| 365 |
config->default_certs_by_type.certs[i] = NULL; |
| 366 |
} |
| 367 |
|
| 368 |
config->cert_ownership = S2N_NOT_OWNED; |
| 369 |
return S2N_SUCCESS; |
| 370 |
} |
| 371 |
|
| 372 |
int s2n_config_free_dhparams(struct s2n_config *config) |
| 373 |
{ |
| 374 |
POSIX_ENSURE_REF(config); |
| 375 |
if (config->dhparams) { |
| 376 |
POSIX_GUARD(s2n_dh_params_free(config->dhparams)); |
| 377 |
} |
| 378 |
|
| 379 |
POSIX_GUARD(s2n_free_object((uint8_t **) &config->dhparams, sizeof(struct s2n_dh_params))); |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
S2N_CLEANUP_RESULT s2n_config_ptr_free(struct s2n_config **config) |
| 384 |
{ |
| 385 |
RESULT_ENSURE_REF(config); |
| 386 |
RESULT_GUARD_POSIX(s2n_config_free(*config)); |
| 387 |
*config = NULL; |
| 388 |
return S2N_RESULT_OK; |
| 389 |
} |
| 390 |
|
| 391 |
int s2n_config_free(struct s2n_config *config) |
| 392 |
{ |
| 393 |
s2n_config_cleanup(config); |
| 394 |
|
| 395 |
POSIX_GUARD(s2n_free_object((uint8_t **) &config, sizeof(struct s2n_config))); |
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
int s2n_config_get_client_auth_type(struct s2n_config *config, s2n_cert_auth_type *client_auth_type) |
| 400 |
{ |
| 401 |
POSIX_ENSURE_REF(config); |
| 402 |
POSIX_ENSURE_REF(client_auth_type); |
| 403 |
*client_auth_type = config->client_cert_auth_type; |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
int s2n_config_set_client_auth_type(struct s2n_config *config, s2n_cert_auth_type client_auth_type) |
| 408 |
{ |
| 409 |
POSIX_ENSURE_REF(config); |
| 410 |
config->client_cert_auth_type_overridden = 1; |
| 411 |
config->client_cert_auth_type = client_auth_type; |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
int s2n_config_set_ct_support_level(struct s2n_config *config, s2n_ct_support_level type) |
| 416 |
{ |
| 417 |
POSIX_ENSURE_REF(config); |
| 418 |
config->ct_type = type; |
| 419 |
|
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
int s2n_config_set_alert_behavior(struct s2n_config *config, s2n_alert_behavior alert_behavior) |
| 424 |
{ |
| 425 |
POSIX_ENSURE_REF(config); |
| 426 |
|
| 427 |
switch (alert_behavior) { |
| 428 |
case S2N_ALERT_FAIL_ON_WARNINGS: |
| 429 |
case S2N_ALERT_IGNORE_WARNINGS: |
| 430 |
config->alert_behavior = alert_behavior; |
| 431 |
break; |
| 432 |
default: |
| 433 |
POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT); |
| 434 |
} |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
int s2n_config_set_verify_host_callback(struct s2n_config *config, s2n_verify_host_fn verify_host_fn, void *data) |
| 440 |
{ |
| 441 |
POSIX_ENSURE_REF(config); |
| 442 |
config->verify_host_fn = verify_host_fn; |
| 443 |
config->data_for_verify_host = data; |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
int s2n_config_set_check_stapled_ocsp_response(struct s2n_config *config, uint8_t check_ocsp) |
| 448 |
{ |
| 449 |
POSIX_ENSURE_REF(config); |
| 450 |
S2N_ERROR_IF(check_ocsp && !s2n_x509_ocsp_stapling_supported(), S2N_ERR_OCSP_NOT_SUPPORTED); |
| 451 |
config->check_ocsp = check_ocsp; |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
int s2n_config_disable_x509_time_verification(struct s2n_config *config) |
| 456 |
{ |
| 457 |
POSIX_ENSURE_REF(config); |
| 458 |
config->disable_x509_time_validation = true; |
| 459 |
return S2N_SUCCESS; |
| 460 |
} |
| 461 |
|
| 462 |
int s2n_config_disable_x509_intent_verification(struct s2n_config *config) |
| 463 |
{ |
| 464 |
POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT); |
| 465 |
config->disable_x509_intent_verification = true; |
| 466 |
return S2N_SUCCESS; |
| 467 |
} |
| 468 |
|
| 469 |
int s2n_config_allow_ip_in_cn(struct s2n_config *config) |
| 470 |
{ |
| 471 |
POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT); |
| 472 |
config->allow_ip_in_cn = true; |
| 473 |
return S2N_SUCCESS; |
| 474 |
} |
| 475 |
|
| 476 |
int s2n_config_disable_x509_verification(struct s2n_config *config) |
| 477 |
{ |
| 478 |
POSIX_ENSURE_REF(config); |
| 479 |
s2n_x509_trust_store_wipe(&config->trust_store); |
| 480 |
config->disable_x509_validation = 1; |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
int s2n_config_set_max_cert_chain_depth(struct s2n_config *config, uint16_t max_depth) |
| 485 |
{ |
| 486 |
POSIX_ENSURE_REF(config); |
| 487 |
S2N_ERROR_IF(max_depth == 0, S2N_ERR_INVALID_ARGUMENT); |
| 488 |
|
| 489 |
config->max_verify_cert_chain_depth = max_depth; |
| 490 |
config->max_verify_cert_chain_depth_set = 1; |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int s2n_config_set_status_request_type(struct s2n_config *config, s2n_status_request_type type) |
| 495 |
{ |
| 496 |
S2N_ERROR_IF(type == S2N_STATUS_REQUEST_OCSP && !s2n_x509_ocsp_stapling_supported(), S2N_ERR_OCSP_NOT_SUPPORTED); |
| 497 |
|
| 498 |
POSIX_ENSURE_REF(config); |
| 499 |
config->ocsp_status_requested_by_user = (type == S2N_STATUS_REQUEST_OCSP); |
| 500 |
|
| 501 |
|
| 502 |
if (type == S2N_STATUS_REQUEST_NONE) { |
| 503 |
config->ocsp_status_requested_by_s2n = false; |
| 504 |
} |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
int s2n_config_wipe_trust_store(struct s2n_config *config) |
| 510 |
{ |
| 511 |
POSIX_ENSURE_REF(config); |
| 512 |
|
| 513 |
s2n_x509_trust_store_wipe(&config->trust_store); |
| 514 |
|
| 515 |
return S2N_SUCCESS; |
| 516 |
} |
| 517 |
|
| 518 |
int s2n_config_add_pem_to_trust_store(struct s2n_config *config, const char *pem) |
| 519 |
{ |
| 520 |
POSIX_ENSURE_REF(config); |
| 521 |
POSIX_ENSURE_REF(pem); |
| 522 |
|
| 523 |
POSIX_GUARD(s2n_x509_trust_store_add_pem(&config->trust_store, pem)); |
| 524 |
|
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
int s2n_config_set_verification_ca_location(struct s2n_config *config, const char *ca_pem_filename, const char *ca_dir) |
| 529 |
{ |
| 530 |
POSIX_ENSURE_REF(config); |
| 531 |
int err_code = s2n_x509_trust_store_from_ca_file(&config->trust_store, ca_pem_filename, ca_dir); |
| 532 |
|
| 533 |
if (!err_code) { |
| 534 |
config->ocsp_status_requested_by_s2n = s2n_x509_ocsp_stapling_supported() ? S2N_STATUS_REQUEST_OCSP : S2N_STATUS_REQUEST_NONE; |
| 535 |
} |
| 536 |
|
| 537 |
return err_code; |
| 538 |
} |
| 539 |
|
| 540 |
static int s2n_config_add_cert_chain_and_key_impl(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair) |
| 541 |
{ |
| 542 |
POSIX_ENSURE_REF(config->domain_name_to_cert_map); |
| 543 |
POSIX_ENSURE_REF(cert_key_pair); |
| 544 |
|
| 545 |
POSIX_GUARD_RESULT(s2n_security_policy_validate_certificate_chain(config->security_policy, cert_key_pair)); |
| 546 |
|
| 547 |
s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pair); |
| 548 |
config->is_rsa_cert_configured |= (cert_type == S2N_PKEY_TYPE_RSA); |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
if (!config->default_certs_are_explicit) { |
| 556 |
POSIX_ENSURE(cert_type >= 0, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 557 |
POSIX_ENSURE(cert_type < S2N_CERT_TYPE_COUNT, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 558 |
if (config->default_certs_by_type.certs[cert_type] != NULL) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, |
| 564 |
S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
POSIX_GUARD(s2n_config_build_domain_name_to_cert_map(config, cert_key_pair)); |
| 569 |
|
| 570 |
if (!config->default_certs_are_explicit) { |
| 571 |
if (config->default_certs_by_type.certs[cert_type] == NULL) { |
| 572 |
config->default_certs_by_type.certs[cert_type] = cert_key_pair; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
if (s2n_pkey_check_key_exists(cert_key_pair->private_key) != S2N_SUCCESS) { |
| 577 |
config->no_signing_key = true; |
| 578 |
} |
| 579 |
|
| 580 |
return S2N_SUCCESS; |
| 581 |
} |
| 582 |
|
| 583 |
S2N_RESULT s2n_config_validate_loaded_certificates(const struct s2n_config *config, |
| 584 |
const struct s2n_security_policy *security_policy) |
| 585 |
{ |
| 586 |
RESULT_ENSURE_REF(config); |
| 587 |
RESULT_ENSURE_REF(security_policy); |
| 588 |
|
| 589 |
if (security_policy->certificate_key_preferences == NULL |
| 590 |
&& security_policy->certificate_signature_preferences == NULL) { |
| 591 |
return S2N_RESULT_OK; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
if (!security_policy->certificate_preferences_apply_locally) { |
| 600 |
return S2N_RESULT_OK; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 605 |
struct s2n_cert_chain_and_key *cert = config->default_certs_by_type.certs[i]; |
| 606 |
if (cert == NULL) { |
| 607 |
continue; |
| 608 |
} |
| 609 |
RESULT_GUARD(s2n_security_policy_validate_certificate_chain(security_policy, cert)); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
if (config->domain_name_to_cert_map == NULL) { |
| 614 |
return S2N_RESULT_OK; |
| 615 |
} |
| 616 |
|
| 617 |
struct s2n_map_iterator iter = { 0 }; |
| 618 |
RESULT_GUARD(s2n_map_iterator_init(&iter, config->domain_name_to_cert_map)); |
| 619 |
|
| 620 |
while (s2n_map_iterator_has_next(&iter)) { |
| 621 |
struct s2n_blob value = { 0 }; |
| 622 |
RESULT_GUARD(s2n_map_iterator_next(&iter, &value)); |
| 623 |
|
| 624 |
struct certs_by_type *domain_certs = (void *) value.data; |
| 625 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 626 |
struct s2n_cert_chain_and_key *cert = domain_certs->certs[i]; |
| 627 |
if (cert == NULL) { |
| 628 |
continue; |
| 629 |
} |
| 630 |
RESULT_GUARD(s2n_security_policy_validate_certificate_chain(security_policy, cert)); |
| 631 |
} |
| 632 |
} |
| 633 |
return S2N_RESULT_OK; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
int s2n_config_add_cert_chain_and_key(struct s2n_config *config, const char *cert_chain_pem, const char *private_key_pem) |
| 638 |
{ |
| 639 |
POSIX_ENSURE_REF(config); |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
POSIX_ENSURE(config->cert_ownership == S2N_NOT_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 646 |
|
| 647 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), |
| 648 |
s2n_cert_chain_and_key_ptr_free); |
| 649 |
POSIX_ENSURE_REF(chain_and_key); |
| 650 |
POSIX_GUARD(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem)); |
| 651 |
POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, chain_and_key)); |
| 652 |
config->cert_ownership = S2N_LIB_OWNED; |
| 653 |
|
| 654 |
ZERO_TO_DISABLE_DEFER_CLEANUP(chain_and_key); |
| 655 |
return S2N_SUCCESS; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
int s2n_config_add_cert_chain(struct s2n_config *config, |
| 660 |
uint8_t *cert_chain_pem, uint32_t cert_chain_pem_size) |
| 661 |
{ |
| 662 |
POSIX_ENSURE_REF(config); |
| 663 |
|
| 664 |
POSIX_ENSURE(config->cert_ownership == S2N_NOT_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 665 |
|
| 666 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), |
| 667 |
s2n_cert_chain_and_key_ptr_free); |
| 668 |
POSIX_ENSURE_REF(chain_and_key); |
| 669 |
POSIX_GUARD(s2n_cert_chain_and_key_load_public_pem_bytes(chain_and_key, |
| 670 |
cert_chain_pem, cert_chain_pem_size)); |
| 671 |
POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, chain_and_key)); |
| 672 |
config->cert_ownership = S2N_LIB_OWNED; |
| 673 |
|
| 674 |
ZERO_TO_DISABLE_DEFER_CLEANUP(chain_and_key); |
| 675 |
return S2N_SUCCESS; |
| 676 |
} |
| 677 |
|
| 678 |
int s2n_config_add_cert_chain_and_key_to_store(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair) |
| 679 |
{ |
| 680 |
POSIX_ENSURE_REF(config); |
| 681 |
POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 682 |
|
| 683 |
POSIX_ENSURE_REF(cert_key_pair); |
| 684 |
POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, cert_key_pair)); |
| 685 |
config->cert_ownership = S2N_APP_OWNED; |
| 686 |
|
| 687 |
return S2N_SUCCESS; |
| 688 |
} |
| 689 |
|
| 690 |
int s2n_config_set_async_offload_callback(struct s2n_config *config, uint32_t allow_list, s2n_async_offload_cb fn, void *ctx) |
| 691 |
{ |
| 692 |
POSIX_ENSURE_REF(config); |
| 693 |
POSIX_ENSURE_REF(fn); |
| 694 |
POSIX_ENSURE(allow_list != 0, S2N_ERR_INVALID_ARGUMENT); |
| 695 |
|
| 696 |
config->async_offload_allow_list = allow_list; |
| 697 |
config->async_offload_cb = fn; |
| 698 |
config->async_offload_ctx = ctx; |
| 699 |
|
| 700 |
return S2N_SUCCESS; |
| 701 |
} |
| 702 |
|
| 703 |
int s2n_config_set_async_pkey_callback(struct s2n_config *config, s2n_async_pkey_fn fn) |
| 704 |
{ |
| 705 |
POSIX_ENSURE_REF(config); |
| 706 |
|
| 707 |
config->async_pkey_cb = fn; |
| 708 |
|
| 709 |
return S2N_SUCCESS; |
| 710 |
} |
| 711 |
|
| 712 |
static int s2n_config_clear_default_certificates(struct s2n_config *config) |
| 713 |
{ |
| 714 |
POSIX_ENSURE_REF(config); |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 720 |
|
| 721 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 722 |
config->default_certs_by_type.certs[i] = NULL; |
| 723 |
} |
| 724 |
config->cert_ownership = S2N_NOT_OWNED; |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
int s2n_config_set_cert_chain_and_key_defaults(struct s2n_config *config, |
| 729 |
struct s2n_cert_chain_and_key **cert_key_pairs, |
| 730 |
uint32_t num_cert_key_pairs) |
| 731 |
{ |
| 732 |
POSIX_ENSURE_REF(config); |
| 733 |
POSIX_ENSURE_REF(cert_key_pairs); |
| 734 |
S2N_ERROR_IF(num_cert_key_pairs < 1 || num_cert_key_pairs > S2N_CERT_TYPE_COUNT, |
| 735 |
S2N_ERR_NUM_DEFAULT_CERTIFICATES); |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 741 |
|
| 742 |
|
| 743 |
struct certs_by_type new_defaults = { { 0 } }; |
| 744 |
for (size_t i = 0; i < num_cert_key_pairs; i++) { |
| 745 |
POSIX_ENSURE_REF(cert_key_pairs[i]); |
| 746 |
s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pairs[i]); |
| 747 |
S2N_ERROR_IF(new_defaults.certs[cert_type] != NULL, S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE); |
| 748 |
new_defaults.certs[cert_type] = cert_key_pairs[i]; |
| 749 |
} |
| 750 |
|
| 751 |
POSIX_GUARD(s2n_config_clear_default_certificates(config)); |
| 752 |
for (size_t i = 0; i < num_cert_key_pairs; i++) { |
| 753 |
s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pairs[i]); |
| 754 |
config->is_rsa_cert_configured |= (cert_type == S2N_PKEY_TYPE_RSA); |
| 755 |
config->default_certs_by_type.certs[cert_type] = cert_key_pairs[i]; |
| 756 |
} |
| 757 |
|
| 758 |
config->default_certs_are_explicit = 1; |
| 759 |
config->cert_ownership = S2N_APP_OWNED; |
| 760 |
return 0; |
| 761 |
} |
| 762 |
|
| 763 |
int s2n_config_add_dhparams(struct s2n_config *config, const char *dhparams_pem) |
| 764 |
{ |
| 765 |
POSIX_ENSURE_REF(config); |
| 766 |
DEFER_CLEANUP(struct s2n_stuffer dhparams_in_stuffer = { 0 }, s2n_stuffer_free); |
| 767 |
DEFER_CLEANUP(struct s2n_stuffer dhparams_out_stuffer = { 0 }, s2n_stuffer_free); |
| 768 |
struct s2n_blob dhparams_blob = { 0 }; |
| 769 |
struct s2n_blob mem = { 0 }; |
| 770 |
|
| 771 |
|
| 772 |
POSIX_GUARD(s2n_alloc(&mem, sizeof(struct s2n_dh_params))); |
| 773 |
config->dhparams = (struct s2n_dh_params *) (void *) mem.data; |
| 774 |
|
| 775 |
if (s2n_stuffer_alloc_ro_from_string(&dhparams_in_stuffer, dhparams_pem) != S2N_SUCCESS) { |
| 776 |
s2n_free(&mem); |
| 777 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 778 |
} |
| 779 |
if (s2n_stuffer_growable_alloc(&dhparams_out_stuffer, strlen(dhparams_pem)) != S2N_SUCCESS) { |
| 780 |
s2n_free(&mem); |
| 781 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
POSIX_GUARD(s2n_stuffer_dhparams_from_pem(&dhparams_in_stuffer, &dhparams_out_stuffer)); |
| 786 |
|
| 787 |
dhparams_blob.size = s2n_stuffer_data_available(&dhparams_out_stuffer); |
| 788 |
dhparams_blob.data = s2n_stuffer_raw_read(&dhparams_out_stuffer, dhparams_blob.size); |
| 789 |
POSIX_ENSURE_REF(dhparams_blob.data); |
| 790 |
|
| 791 |
POSIX_GUARD(s2n_pkcs3_to_dh_params(config->dhparams, &dhparams_blob)); |
| 792 |
|
| 793 |
return 0; |
| 794 |
} |
| 795 |
|
| 796 |
int s2n_config_set_wall_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx) |
| 797 |
{ |
| 798 |
POSIX_ENSURE_REF(config); |
| 799 |
POSIX_ENSURE_REF(clock_fn); |
| 800 |
|
| 801 |
config->wall_clock = clock_fn; |
| 802 |
config->sys_clock_ctx = ctx; |
| 803 |
|
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
int s2n_config_set_monotonic_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx) |
| 808 |
{ |
| 809 |
POSIX_ENSURE_REF(config); |
| 810 |
POSIX_ENSURE_REF(clock_fn); |
| 811 |
|
| 812 |
config->monotonic_clock = clock_fn; |
| 813 |
config->monotonic_clock_ctx = ctx; |
| 814 |
|
| 815 |
return 0; |
| 816 |
} |
| 817 |
|
| 818 |
int s2n_config_set_cache_store_callback(struct s2n_config *config, s2n_cache_store_callback cache_store_callback, void *data) |
| 819 |
{ |
| 820 |
POSIX_ENSURE_REF(config); |
| 821 |
POSIX_ENSURE_REF(cache_store_callback); |
| 822 |
|
| 823 |
config->cache_store = cache_store_callback; |
| 824 |
config->cache_store_data = data; |
| 825 |
|
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
int s2n_config_set_cache_retrieve_callback(struct s2n_config *config, s2n_cache_retrieve_callback cache_retrieve_callback, void *data) |
| 830 |
{ |
| 831 |
POSIX_ENSURE_REF(config); |
| 832 |
POSIX_ENSURE_REF(cache_retrieve_callback); |
| 833 |
|
| 834 |
config->cache_retrieve = cache_retrieve_callback; |
| 835 |
config->cache_retrieve_data = data; |
| 836 |
|
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
int s2n_config_set_cache_delete_callback(struct s2n_config *config, s2n_cache_delete_callback cache_delete_callback, void *data) |
| 841 |
{ |
| 842 |
POSIX_ENSURE_REF(config); |
| 843 |
POSIX_ENSURE_REF(cache_delete_callback); |
| 844 |
|
| 845 |
config->cache_delete = cache_delete_callback; |
| 846 |
config->cache_delete_data = data; |
| 847 |
|
| 848 |
return 0; |
| 849 |
} |
| 850 |
|
| 851 |
int s2n_config_set_extension_data(struct s2n_config *config, s2n_tls_extension_type type, const uint8_t *data, uint32_t length) |
| 852 |
{ |
| 853 |
POSIX_ENSURE_REF(config); |
| 854 |
|
| 855 |
if (s2n_config_get_num_default_certs(config) == 0) { |
| 856 |
POSIX_BAIL(S2N_ERR_UPDATING_EXTENSION); |
| 857 |
} |
| 858 |
struct s2n_cert_chain_and_key *config_chain_and_key = s2n_config_get_single_default_cert(config); |
| 859 |
POSIX_ENSURE_REF(config_chain_and_key); |
| 860 |
POSIX_ENSURE(config->cert_ownership == S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP); |
| 861 |
|
| 862 |
switch (type) { |
| 863 |
case S2N_EXTENSION_CERTIFICATE_TRANSPARENCY: |
| 864 |
POSIX_GUARD(s2n_cert_chain_and_key_set_sct_list(config_chain_and_key, data, length)); |
| 865 |
break; |
| 866 |
case S2N_EXTENSION_OCSP_STAPLING: |
| 867 |
POSIX_GUARD(s2n_cert_chain_and_key_set_ocsp_data(config_chain_and_key, data, length)); |
| 868 |
break; |
| 869 |
default: |
| 870 |
POSIX_BAIL(S2N_ERR_UNRECOGNIZED_EXTENSION); |
| 871 |
} |
| 872 |
|
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
int s2n_config_add_custom_x509_extension(struct s2n_config *config, uint8_t *extension_oid, uint32_t extension_oid_len) |
| 877 |
{ |
| 878 |
POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT); |
| 879 |
POSIX_ENSURE(extension_oid, S2N_ERR_INVALID_ARGUMENT); |
| 880 |
|
| 881 |
POSIX_ENSURE(s2n_libcrypto_supports_custom_oid(), S2N_ERR_API_UNSUPPORTED_BY_LIBCRYPTO); |
| 882 |
|
| 883 |
if (config->custom_x509_extension_oids == NULL) { |
| 884 |
config->custom_x509_extension_oids = sk_ASN1_OBJECT_new_null(); |
| 885 |
} |
| 886 |
POSIX_ENSURE_REF(config->custom_x509_extension_oids); |
| 887 |
|
| 888 |
DEFER_CLEANUP(struct s2n_blob oid_buffer = { 0 }, s2n_free); |
| 889 |
POSIX_GUARD(s2n_alloc(&oid_buffer, extension_oid_len + 1)); |
| 890 |
|
| 891 |
POSIX_GUARD(s2n_blob_zero(&oid_buffer)); |
| 892 |
POSIX_CHECKED_MEMCPY(oid_buffer.data, extension_oid, extension_oid_len); |
| 893 |
oid_buffer.data[extension_oid_len] = '\0'; |
| 894 |
|
| 895 |
const char *oid_string = (const char *) oid_buffer.data; |
| 896 |
POSIX_ENSURE_REF(oid_string); |
| 897 |
|
| 898 |
ASN1_OBJECT *critical_oid = OBJ_txt2obj(oid_string, 1); |
| 899 |
POSIX_ENSURE_REF(critical_oid); |
| 900 |
POSIX_ENSURE(sk_ASN1_OBJECT_push(config->custom_x509_extension_oids, critical_oid) > 0, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 901 |
|
| 902 |
return S2N_SUCCESS; |
| 903 |
} |
| 904 |
|
| 905 |
int s2n_config_set_cert_request_callback(struct s2n_config *config, s2n_cert_request_callback cert_request_cb, void *ctx) |
| 906 |
{ |
| 907 |
POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT); |
| 908 |
|
| 909 |
config->cert_request_cb = cert_request_cb; |
| 910 |
config->cert_request_cb_ctx = ctx; |
| 911 |
|
| 912 |
return S2N_SUCCESS; |
| 913 |
} |
| 914 |
|
| 915 |
int s2n_config_set_client_hello_cb(struct s2n_config *config, s2n_client_hello_fn client_hello_cb, void *ctx) |
| 916 |
{ |
| 917 |
POSIX_ENSURE_REF(config); |
| 918 |
|
| 919 |
config->client_hello_cb = client_hello_cb; |
| 920 |
config->client_hello_cb_ctx = ctx; |
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
int s2n_config_set_client_hello_cb_mode(struct s2n_config *config, s2n_client_hello_cb_mode cb_mode) |
| 925 |
{ |
| 926 |
POSIX_ENSURE_REF(config); |
| 927 |
POSIX_ENSURE(cb_mode == S2N_CLIENT_HELLO_CB_BLOCKING || cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING, S2N_ERR_INVALID_STATE); |
| 928 |
|
| 929 |
config->client_hello_cb_mode = cb_mode; |
| 930 |
return S2N_SUCCESS; |
| 931 |
} |
| 932 |
|
| 933 |
int s2n_config_send_max_fragment_length(struct s2n_config *config, s2n_max_frag_len mfl_code) |
| 934 |
{ |
| 935 |
POSIX_ENSURE_REF(config); |
| 936 |
|
| 937 |
S2N_ERROR_IF(mfl_code > S2N_TLS_MAX_FRAG_LEN_4096, S2N_ERR_INVALID_MAX_FRAG_LEN); |
| 938 |
|
| 939 |
config->mfl_code = mfl_code; |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
int s2n_config_accept_max_fragment_length(struct s2n_config *config) |
| 945 |
{ |
| 946 |
POSIX_ENSURE_REF(config); |
| 947 |
|
| 948 |
config->accept_mfl = 1; |
| 949 |
|
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
int s2n_config_set_session_state_lifetime(struct s2n_config *config, |
| 954 |
uint64_t lifetime_in_secs) |
| 955 |
{ |
| 956 |
POSIX_ENSURE_REF(config); |
| 957 |
|
| 958 |
config->session_state_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS); |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
int s2n_config_set_session_tickets_onoff(struct s2n_config *config, uint8_t enabled) |
| 963 |
{ |
| 964 |
POSIX_ENSURE_REF(config); |
| 965 |
|
| 966 |
if (config->use_tickets == enabled) { |
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
config->use_tickets = enabled; |
| 971 |
|
| 972 |
if (config->initial_tickets_to_send == 0) { |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
config->initial_tickets_to_send = 1; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
if (enabled) { |
| 981 |
POSIX_GUARD(s2n_config_init_session_ticket_keys(config)); |
| 982 |
} else if (!config->use_session_cache) { |
| 983 |
POSIX_GUARD(s2n_config_free_session_ticket_keys(config)); |
| 984 |
} |
| 985 |
|
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
int s2n_config_set_session_cache_onoff(struct s2n_config *config, uint8_t enabled) |
| 990 |
{ |
| 991 |
POSIX_ENSURE_REF(config); |
| 992 |
if (enabled && config->cache_store && config->cache_retrieve && config->cache_delete) { |
| 993 |
POSIX_GUARD(s2n_config_init_session_ticket_keys(config)); |
| 994 |
config->use_session_cache = 1; |
| 995 |
} else { |
| 996 |
if (!config->use_tickets) { |
| 997 |
POSIX_GUARD(s2n_config_free_session_ticket_keys(config)); |
| 998 |
} |
| 999 |
config->use_session_cache = 0; |
| 1000 |
} |
| 1001 |
return 0; |
| 1002 |
} |
| 1003 |
|
| 1004 |
int s2n_config_set_ticket_encrypt_decrypt_key_lifetime(struct s2n_config *config, |
| 1005 |
uint64_t lifetime_in_secs) |
| 1006 |
{ |
| 1007 |
POSIX_ENSURE_REF(config); |
| 1008 |
|
| 1009 |
config->encrypt_decrypt_key_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS); |
| 1010 |
return 0; |
| 1011 |
} |
| 1012 |
|
| 1013 |
int s2n_config_set_ticket_decrypt_key_lifetime(struct s2n_config *config, |
| 1014 |
uint64_t lifetime_in_secs) |
| 1015 |
{ |
| 1016 |
POSIX_ENSURE_REF(config); |
| 1017 |
|
| 1018 |
config->decrypt_key_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS); |
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
|
| 1022 |
int s2n_config_add_ticket_crypto_key(struct s2n_config *config, |
| 1023 |
const uint8_t *name, uint32_t name_len, |
| 1024 |
uint8_t *key, uint32_t key_len, |
| 1025 |
uint64_t intro_time_in_seconds_from_epoch) |
| 1026 |
{ |
| 1027 |
POSIX_ENSURE_REF(config); |
| 1028 |
POSIX_ENSURE_REF(name); |
| 1029 |
POSIX_ENSURE_REF(key); |
| 1030 |
|
| 1031 |
|
| 1032 |
if (!config->use_tickets && !config->use_session_cache) { |
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
|
| 1036 |
POSIX_GUARD(s2n_config_wipe_expired_ticket_crypto_keys(config, -1)); |
| 1037 |
|
| 1038 |
POSIX_ENSURE(key_len != 0, S2N_ERR_INVALID_TICKET_KEY_LENGTH); |
| 1039 |
|
| 1040 |
uint32_t ticket_keys_len = 0; |
| 1041 |
POSIX_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len)); |
| 1042 |
POSIX_ENSURE(ticket_keys_len < S2N_MAX_TICKET_KEYS, S2N_ERR_TICKET_KEY_LIMIT); |
| 1043 |
|
| 1044 |
POSIX_ENSURE(name_len != 0, S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH); |
| 1045 |
POSIX_ENSURE(name_len <= S2N_TICKET_KEY_NAME_LEN, S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH); |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
uint8_t name_data[S2N_TICKET_KEY_NAME_LEN] = { 0 }; |
| 1050 |
POSIX_CHECKED_MEMCPY(name_data, name, name_len); |
| 1051 |
|
| 1052 |
uint8_t output_pad[S2N_AES256_KEY_LEN + S2N_TICKET_AAD_IMPLICIT_LEN] = { 0 }; |
| 1053 |
struct s2n_blob out_key = { 0 }; |
| 1054 |
POSIX_GUARD(s2n_blob_init(&out_key, output_pad, s2n_array_len(output_pad))); |
| 1055 |
struct s2n_blob in_key = { 0 }; |
| 1056 |
POSIX_GUARD(s2n_blob_init(&in_key, key, key_len)); |
| 1057 |
struct s2n_blob salt = { 0 }; |
| 1058 |
POSIX_GUARD(s2n_blob_init(&salt, NULL, 0)); |
| 1059 |
struct s2n_blob info = { 0 }; |
| 1060 |
POSIX_GUARD(s2n_blob_init(&info, NULL, 0)); |
| 1061 |
|
| 1062 |
struct s2n_ticket_key *session_ticket_key = { 0 }; |
| 1063 |
DEFER_CLEANUP(struct s2n_blob allocator = { 0 }, s2n_free); |
| 1064 |
POSIX_GUARD(s2n_alloc(&allocator, sizeof(struct s2n_ticket_key))); |
| 1065 |
session_ticket_key = (struct s2n_ticket_key *) (void *) allocator.data; |
| 1066 |
|
| 1067 |
DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free); |
| 1068 |
|
| 1069 |
POSIX_GUARD(s2n_hmac_new(&hmac)); |
| 1070 |
POSIX_GUARD(s2n_hkdf(&hmac, S2N_HMAC_SHA256, &salt, &in_key, &info, &out_key)); |
| 1071 |
|
| 1072 |
POSIX_CHECKED_MEMCPY(session_ticket_key->key_name, name_data, s2n_array_len(name_data)); |
| 1073 |
POSIX_CHECKED_MEMCPY(session_ticket_key->aes_key, out_key.data, S2N_AES256_KEY_LEN); |
| 1074 |
out_key.data = output_pad + S2N_AES256_KEY_LEN; |
| 1075 |
POSIX_CHECKED_MEMCPY(session_ticket_key->implicit_aad, out_key.data, S2N_TICKET_AAD_IMPLICIT_LEN); |
| 1076 |
|
| 1077 |
if (intro_time_in_seconds_from_epoch == 0) { |
| 1078 |
uint64_t now = 0; |
| 1079 |
POSIX_GUARD_RESULT(s2n_config_wall_clock(config, &now)); |
| 1080 |
session_ticket_key->intro_timestamp = now; |
| 1081 |
} else { |
| 1082 |
session_ticket_key->intro_timestamp = (intro_time_in_seconds_from_epoch * ONE_SEC_IN_NANOS); |
| 1083 |
} |
| 1084 |
|
| 1085 |
POSIX_GUARD(s2n_config_store_ticket_key(config, session_ticket_key)); |
| 1086 |
|
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
|
| 1090 |
int s2n_config_require_ticket_forward_secrecy(struct s2n_config *config, bool enabled) |
| 1091 |
{ |
| 1092 |
POSIX_ENSURE_REF(config); |
| 1093 |
|
| 1094 |
config->ticket_forward_secrecy = enabled; |
| 1095 |
|
| 1096 |
return S2N_SUCCESS; |
| 1097 |
} |
| 1098 |
|
| 1099 |
int s2n_config_set_cert_tiebreak_callback(struct s2n_config *config, s2n_cert_tiebreak_callback cert_tiebreak_cb) |
| 1100 |
{ |
| 1101 |
POSIX_ENSURE_REF(config); |
| 1102 |
config->cert_tiebreak_cb = cert_tiebreak_cb; |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
struct s2n_cert_chain_and_key *s2n_config_get_single_default_cert(struct s2n_config *config) |
| 1107 |
{ |
| 1108 |
PTR_ENSURE_REF(config); |
| 1109 |
struct s2n_cert_chain_and_key *cert = NULL; |
| 1110 |
|
| 1111 |
for (int i = S2N_CERT_TYPE_COUNT - 1; i >= 0; i--) { |
| 1112 |
if (config->default_certs_by_type.certs[i] != NULL) { |
| 1113 |
cert = config->default_certs_by_type.certs[i]; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
return cert; |
| 1117 |
} |
| 1118 |
|
| 1119 |
int s2n_config_get_num_default_certs(const struct s2n_config *config) |
| 1120 |
{ |
| 1121 |
POSIX_ENSURE_REF(config); |
| 1122 |
int num_certs = 0; |
| 1123 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 1124 |
if (config->default_certs_by_type.certs[i] != NULL) { |
| 1125 |
num_certs++; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
return num_certs; |
| 1130 |
} |
| 1131 |
|
| 1132 |
int s2n_config_enable_cert_req_dss_legacy_compat(struct s2n_config *config) |
| 1133 |
{ |
| 1134 |
POSIX_ENSURE_REF(config); |
| 1135 |
config->cert_req_dss_legacy_compat_enabled = 1; |
| 1136 |
return S2N_SUCCESS; |
| 1137 |
} |
| 1138 |
|
| 1139 |
int s2n_config_set_psk_selection_callback(struct s2n_config *config, s2n_psk_selection_callback cb, void *context) |
| 1140 |
{ |
| 1141 |
POSIX_ENSURE_REF(config); |
| 1142 |
config->psk_selection_cb = cb; |
| 1143 |
config->psk_selection_ctx = context; |
| 1144 |
return S2N_SUCCESS; |
| 1145 |
} |
| 1146 |
|
| 1147 |
int s2n_config_set_key_log_cb(struct s2n_config *config, s2n_key_log_fn callback, void *ctx) |
| 1148 |
{ |
| 1149 |
POSIX_ENSURE_MUT(config); |
| 1150 |
|
| 1151 |
config->key_log_cb = callback; |
| 1152 |
config->key_log_ctx = ctx; |
| 1153 |
|
| 1154 |
return S2N_SUCCESS; |
| 1155 |
} |
| 1156 |
|
| 1157 |
int s2n_config_set_async_pkey_validation_mode(struct s2n_config *config, s2n_async_pkey_validation_mode mode) |
| 1158 |
{ |
| 1159 |
POSIX_ENSURE_REF(config); |
| 1160 |
|
| 1161 |
switch (mode) { |
| 1162 |
case S2N_ASYNC_PKEY_VALIDATION_FAST: |
| 1163 |
case S2N_ASYNC_PKEY_VALIDATION_STRICT: |
| 1164 |
config->async_pkey_validation_mode = mode; |
| 1165 |
return S2N_SUCCESS; |
| 1166 |
} |
| 1167 |
|
| 1168 |
POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT); |
| 1169 |
} |
| 1170 |
|
| 1171 |
int s2n_config_set_ctx(struct s2n_config *config, void *ctx) |
| 1172 |
{ |
| 1173 |
POSIX_ENSURE_REF(config); |
| 1174 |
|
| 1175 |
config->context = ctx; |
| 1176 |
|
| 1177 |
return S2N_SUCCESS; |
| 1178 |
} |
| 1179 |
|
| 1180 |
int s2n_config_get_ctx(struct s2n_config *config, void **ctx) |
| 1181 |
{ |
| 1182 |
POSIX_ENSURE_REF(config); |
| 1183 |
POSIX_ENSURE_REF(ctx); |
| 1184 |
|
| 1185 |
*ctx = config->context; |
| 1186 |
|
| 1187 |
return S2N_SUCCESS; |
| 1188 |
} |
| 1189 |
|
| 1190 |
int s2n_config_set_send_buffer_size(struct s2n_config *config, uint32_t size) |
| 1191 |
{ |
| 1192 |
POSIX_ENSURE_REF(config); |
| 1193 |
POSIX_ENSURE(size >= S2N_MIN_SEND_BUFFER_SIZE, S2N_ERR_INVALID_ARGUMENT); |
| 1194 |
config->send_buffer_size_override = size; |
| 1195 |
return S2N_SUCCESS; |
| 1196 |
} |
| 1197 |
|
| 1198 |
int s2n_config_set_verify_after_sign(struct s2n_config *config, s2n_verify_after_sign mode) |
| 1199 |
{ |
| 1200 |
POSIX_ENSURE_REF(config); |
| 1201 |
switch (mode) { |
| 1202 |
case S2N_VERIFY_AFTER_SIGN_DISABLED: |
| 1203 |
config->verify_after_sign = false; |
| 1204 |
break; |
| 1205 |
case S2N_VERIFY_AFTER_SIGN_ENABLED: |
| 1206 |
config->verify_after_sign = true; |
| 1207 |
break; |
| 1208 |
default: |
| 1209 |
POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT); |
| 1210 |
} |
| 1211 |
return S2N_SUCCESS; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
int s2n_config_set_renegotiate_request_cb(struct s2n_config *config, s2n_renegotiate_request_cb cb, void *ctx) |
| 1220 |
{ |
| 1221 |
POSIX_ENSURE_REF(config); |
| 1222 |
|
| 1223 |
|
| 1224 |
POSIX_ENSURE(config->serialized_connection_version == S2N_SERIALIZED_CONN_NONE, S2N_ERR_INVALID_STATE); |
| 1225 |
|
| 1226 |
config->renegotiate_request_cb = cb; |
| 1227 |
config->renegotiate_request_ctx = ctx; |
| 1228 |
return S2N_SUCCESS; |
| 1229 |
} |
| 1230 |
|
| 1231 |
int s2n_config_set_npn(struct s2n_config *config, bool enable) |
| 1232 |
{ |
| 1233 |
POSIX_ENSURE_REF(config); |
| 1234 |
|
| 1235 |
config->npn_supported = enable; |
| 1236 |
|
| 1237 |
return S2N_SUCCESS; |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
S2N_RESULT s2n_config_wall_clock(struct s2n_config *config, uint64_t *output) |
| 1245 |
{ |
| 1246 |
RESULT_ENSURE_REF(config); |
| 1247 |
RESULT_ENSURE(config->wall_clock(config->sys_clock_ctx, output) >= S2N_SUCCESS, S2N_ERR_CANCELLED); |
| 1248 |
return S2N_RESULT_OK; |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
S2N_RESULT s2n_config_monotonic_clock(struct s2n_config *config, uint64_t *output) |
| 1257 |
{ |
| 1258 |
RESULT_ENSURE_REF(config); |
| 1259 |
RESULT_ENSURE(config->monotonic_clock(config->monotonic_clock_ctx, output) >= S2N_SUCCESS, S2N_ERR_CANCELLED); |
| 1260 |
return S2N_RESULT_OK; |
| 1261 |
} |
| 1262 |
|
| 1263 |
int s2n_config_set_crl_lookup_cb(struct s2n_config *config, s2n_crl_lookup_callback cb, void *ctx) |
| 1264 |
{ |
| 1265 |
POSIX_ENSURE_REF(config); |
| 1266 |
config->crl_lookup_cb = cb; |
| 1267 |
config->crl_lookup_ctx = ctx; |
| 1268 |
return S2N_SUCCESS; |
| 1269 |
} |
| 1270 |
|
| 1271 |
int s2n_config_set_recv_multi_record(struct s2n_config *config, bool enabled) |
| 1272 |
{ |
| 1273 |
POSIX_ENSURE_REF(config); |
| 1274 |
|
| 1275 |
config->recv_multi_record = enabled; |
| 1276 |
|
| 1277 |
return S2N_SUCCESS; |
| 1278 |
} |
| 1279 |
|
| 1280 |
int s2n_config_set_cert_validation_cb(struct s2n_config *config, s2n_cert_validation_callback cb, void *ctx) |
| 1281 |
{ |
| 1282 |
POSIX_ENSURE_REF(config); |
| 1283 |
|
| 1284 |
config->cert_validation_cb = cb; |
| 1285 |
config->cert_validation_ctx = ctx; |
| 1286 |
|
| 1287 |
return S2N_SUCCESS; |
| 1288 |
} |
| 1289 |
|
| 1290 |
int s2n_config_get_supported_groups(struct s2n_config *config, uint16_t *groups, uint16_t groups_count_max, |
| 1291 |
uint16_t *groups_count_out) |
| 1292 |
{ |
| 1293 |
POSIX_ENSURE_REF(groups_count_out); |
| 1294 |
*groups_count_out = 0; |
| 1295 |
POSIX_ENSURE_REF(config); |
| 1296 |
POSIX_ENSURE_REF(groups); |
| 1297 |
|
| 1298 |
const struct s2n_security_policy *security_policy = config->security_policy; |
| 1299 |
POSIX_ENSURE_REF(security_policy); |
| 1300 |
const struct s2n_kem_preferences *kem_preferences = security_policy->kem_preferences; |
| 1301 |
POSIX_ENSURE_REF(kem_preferences); |
| 1302 |
const struct s2n_ecc_preferences *ecc_preferences = security_policy->ecc_preferences; |
| 1303 |
POSIX_ENSURE_REF(ecc_preferences); |
| 1304 |
|
| 1305 |
uint16_t groups_count = 0; |
| 1306 |
for (uint8_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) { |
| 1307 |
const struct s2n_kem_group *kem_group = kem_preferences->tls13_kem_groups[i]; |
| 1308 |
POSIX_ENSURE_REF(kem_group); |
| 1309 |
if (!s2n_kem_group_is_available(kem_group)) { |
| 1310 |
continue; |
| 1311 |
} |
| 1312 |
|
| 1313 |
POSIX_ENSURE(groups_count < groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 1314 |
groups[groups_count] = kem_group->iana_id; |
| 1315 |
groups_count += 1; |
| 1316 |
} |
| 1317 |
|
| 1318 |
for (uint8_t i = 0; i < ecc_preferences->count; i++) { |
| 1319 |
const struct s2n_ecc_named_curve *ecc_curve = ecc_preferences->ecc_curves[i]; |
| 1320 |
POSIX_ENSURE_REF(ecc_curve); |
| 1321 |
|
| 1322 |
POSIX_ENSURE(groups_count < groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 1323 |
groups[groups_count] = ecc_curve->iana_id; |
| 1324 |
groups_count += 1; |
| 1325 |
} |
| 1326 |
|
| 1327 |
*groups_count_out = groups_count; |
| 1328 |
|
| 1329 |
return S2N_SUCCESS; |
| 1330 |
} |
| 1331 |
|
| 1332 |
int s2n_config_set_serialization_version(struct s2n_config *config, s2n_serialization_version version) |
| 1333 |
{ |
| 1334 |
POSIX_ENSURE_REF(config); |
| 1335 |
|
| 1336 |
|
| 1337 |
POSIX_ENSURE(config->renegotiate_request_cb == NULL, S2N_ERR_INVALID_STATE); |
| 1338 |
|
| 1339 |
|
| 1340 |
POSIX_ENSURE_EQ(version, S2N_SERIALIZED_CONN_V1); |
| 1341 |
config->serialized_connection_version = version; |
| 1342 |
|
| 1343 |
return S2N_SUCCESS; |
| 1344 |
} |
| 1345 |
|
| 1346 |
int s2n_config_set_max_blinding_delay(struct s2n_config *config, uint32_t seconds) |
| 1347 |
{ |
| 1348 |
POSIX_ENSURE_REF(config); |
| 1349 |
|
| 1350 |
config->custom_blinding_set = 1; |
| 1351 |
config->max_blinding = seconds; |
| 1352 |
|
| 1353 |
return S2N_SUCCESS; |
| 1354 |
} |
| 1355 |
|
| 1356 |
int s2n_config_set_subscriber(struct s2n_config *config, void *subscriber) |
| 1357 |
{ |
| 1358 |
POSIX_ENSURE_REF(config); |
| 1359 |
config->subscriber = subscriber; |
| 1360 |
return S2N_SUCCESS; |
| 1361 |
} |
| 1362 |
|
| 1363 |
int s2n_config_set_handshake_event(struct s2n_config *config, s2n_event_on_handshake_cb callback) |
| 1364 |
{ |
| 1365 |
POSIX_ENSURE_REF(config); |
| 1366 |
config->on_handshake_event = callback; |
| 1367 |
return S2N_SUCCESS; |
| 1368 |
} |