| 1 | /* |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * @file s2n.h |
| 18 | * s2n-tls is a C99 implementation of the TLS/SSL protocols that is designed to |
| 19 | * be simple, small, fast, and with security as a priority. <br> It is released and |
| 20 | * licensed under the Apache License 2.0. |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #ifndef S2N_API |
| 26 | /** |
| 27 | * Marks a function as belonging to the public s2n API. |
| 28 | */ |
| 29 | #define S2N_API |
| 30 | #endif |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | #include <stdbool.h> |
| 37 | #include <stdint.h> |
| 38 | #include <stdio.h> |
| 39 | #include <sys/types.h> |
| 40 | #ifndef _WIN32 |
| 41 | #include <sys/uio.h> |
| 42 | #else |
| 43 | /* struct iovec equivalent for Windows */ |
| 44 | struct iovec { |
| 45 | void *iov_base; |
| 46 | size_t iov_len; |
| 47 | }; |
| 48 | #endif |
| 49 | |
| 50 | /** |
| 51 | * Function return code |
| 52 | */ |
| 53 | #define S2N_SUCCESS 0 |
| 54 | /** |
| 55 | * Function return code |
| 56 | */ |
| 57 | #define S2N_FAILURE -1 |
| 58 | |
| 59 | /** |
| 60 | * Callback return code |
| 61 | */ |
| 62 | #define S2N_CALLBACK_BLOCKED -2 |
| 63 | |
| 64 | /** |
| 65 | * s2n minimum supported TLS record major version |
| 66 | */ |
| 67 | #define S2N_MINIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 2 |
| 68 | |
| 69 | /** |
| 70 | * s2n maximum supported TLS record major version |
| 71 | */ |
| 72 | #define S2N_MAXIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 3 |
| 73 | |
| 74 | /** |
| 75 | * s2n SSL 2.0 Version Constant |
| 76 | */ |
| 77 | #define S2N_SSLv2 20 |
| 78 | |
| 79 | /** |
| 80 | * s2n SSL 3.0 Version Constant |
| 81 | */ |
| 82 | #define S2N_SSLv3 30 |
| 83 | |
| 84 | /** |
| 85 | * s2n TLS 1.0 Version Constant |
| 86 | */ |
| 87 | #define S2N_TLS10 31 |
| 88 | |
| 89 | /** |
| 90 | * s2n TLS 1.1 Version Constant |
| 91 | */ |
| 92 | #define S2N_TLS11 32 |
| 93 | |
| 94 | /** |
| 95 | * s2n TLS 1.2 Version Constant |
| 96 | */ |
| 97 | #define S2N_TLS12 33 |
| 98 | |
| 99 | /** |
| 100 | * s2n TLS 1.3 Version Constant |
| 101 | */ |
| 102 | #define S2N_TLS13 34 |
| 103 | |
| 104 | /** |
| 105 | * s2n Unknown TLS Version |
| 106 | */ |
| 107 | #define S2N_UNKNOWN_PROTOCOL_VERSION 0 |
| 108 | |
| 109 | /** |
| 110 | * s2n-tls functions that return 'int' return 0 to indicate success and -1 to indicate |
| 111 | * failure. |
| 112 | * |
| 113 | * s2n-tls functions that return pointer types return NULL in the case of |
| 114 | * failure. |
| 115 | * |
| 116 | * When an s2n-tls function returns a failure, s2n_errno will be set to a value |
| 117 | * corresponding to the error. This error value can be translated into a string |
| 118 | * explaining the error in English by calling s2n_strerror(s2n_errno, "EN"). |
| 119 | * A string containing human readable error name; can be generated with `s2n_strerror_name`. |
| 120 | * A string containing internal debug information, including filename and line number, can be generated with `s2n_strerror_debug`. |
| 121 | * A string containing only the filename and line number can be generated with `s2n_strerror_source`. |
| 122 | * This string is useful to include when reporting issues to the s2n-tls development team. |
| 123 | * |
| 124 | * @warning To avoid possible confusion, s2n_errno should be cleared after processing an error: `s2n_errno = S2N_ERR_T_OK` |
| 125 | */ |
| 126 | S2N_API extern __thread int s2n_errno; |
| 127 | |
| 128 | /** |
| 129 | * This function can be used instead of trying to resolve `s2n_errno` directly |
| 130 | * in runtimes where thread-local variables may not be easily accessible. |
| 131 | * |
| 132 | * @returns The address of the thread-local `s2n_errno` variable |
| 133 | */ |
| 134 | S2N_API extern int *s2n_errno_location(void); |
| 135 | |
| 136 | /** |
| 137 | * Used to help applications determine why an s2n-tls function failed. |
| 138 | * |
| 139 | * This enum is optimized for use in C switch statements. Each value in the enum represents |
| 140 | * an error "category". |
| 141 | * |
| 142 | * s2n-tls organizes errors into different "types" to allow applications to handle error |
| 143 | * values without catching all possibilities. Applications using non-blocking I/O should check |
| 144 | * the error type to determine if the I/O operation failed because it would block or for some other |
| 145 | * error. To retrieve the type for a given error use `s2n_error_get_type()`. Applications should |
| 146 | * perform any error handling logic using these high level types. |
| 147 | * |
| 148 | * See the [Error Handling](https://github.com/aws/s2n-tls/blob/main/docs/usage-guide/topics/ch03-error-handling.md) section for how the errors should be interpreted. |
| 149 | */ |
| 150 | typedef enum { |
| 151 | /** No error */ |
| 152 | S2N_ERR_T_OK = 0, |
| 153 | /** Underlying I/O operation failed, check system errno */ |
| 154 | S2N_ERR_T_IO, |
| 155 | /** EOF */ |
| 156 | S2N_ERR_T_CLOSED, |
| 157 | /** Underlying I/O operation would block */ |
| 158 | S2N_ERR_T_BLOCKED, |
| 159 | /** Incoming Alert */ |
| 160 | S2N_ERR_T_ALERT, |
| 161 | /** Failure in some part of the TLS protocol. Ex: CBC verification failure */ |
| 162 | S2N_ERR_T_PROTO, |
| 163 | /** Error internal to s2n-tls. A precondition could have failed. */ |
| 164 | S2N_ERR_T_INTERNAL, |
| 165 | /** User input error. Ex: Providing an invalid cipher preference version */ |
| 166 | S2N_ERR_T_USAGE |
| 167 | } s2n_error_type; |
| 168 | |
| 169 | /** |
| 170 | * Gets the category of error from an error. |
| 171 | * |
| 172 | * s2n-tls organizes errors into different "types" to allow applications to do logic on error values without catching all possibilities. |
| 173 | * Applications using non-blocking I/O should check error type to determine if the I/O operation failed because |
| 174 | * it would block or for some other error. |
| 175 | * |
| 176 | * @param error The error from s2n. Usually this is `s2n_errno`. |
| 177 | * @returns An s2n_error_type |
| 178 | */ |
| 179 | S2N_API extern int s2n_error_get_type(int error); |
| 180 | |
| 181 | /** |
| 182 | * An opaque configuration object, used by clients and servers for holding cryptographic certificates, keys and preferences. |
| 183 | */ |
| 184 | struct s2n_config; |
| 185 | |
| 186 | /** |
| 187 | * An opaque connection. Used to track each s2n connection. |
| 188 | */ |
| 189 | struct s2n_connection; |
| 190 | |
| 191 | /** |
| 192 | * Prevents S2N from calling `OPENSSL_init_crypto`/`OPENSSL_cleanup`/`EVP_cleanup` on OpenSSL versions |
| 193 | * prior to 1.1.x. This allows applications or languages that also init OpenSSL to interoperate |
| 194 | * with S2N. |
| 195 | * |
| 196 | * @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error |
| 197 | * if s2n is already initialized. |
| 198 | * |
| 199 | * @note If you disable this and are using a version of OpenSSL/libcrypto < 1.1.x, you will |
| 200 | * be responsible for library init and cleanup (specifically `OPENSSL_add_all_algorithms()` |
| 201 | * or `OPENSSL_init_crypto()`, and EVP_* APIs will not be usable unless the library is initialized. |
| 202 | * |
| 203 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 204 | */ |
| 205 | S2N_API extern int s2n_crypto_disable_init(void); |
| 206 | |
| 207 | /** |
| 208 | * Prevents S2N from installing an atexit handler, which allows safe shutdown of S2N from within a |
| 209 | * re-entrant shared library |
| 210 | * |
| 211 | * @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error |
| 212 | * if s2n is already initialized. |
| 213 | * |
| 214 | * @note When the atexit handler is disabled, callers are responsible for calling |
| 215 | * s2n_cleanup_final() to perform full library cleanup before process exit. |
| 216 | * |
| 217 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 218 | */ |
| 219 | S2N_API extern int s2n_disable_atexit(void); |
| 220 | |
| 221 | /** |
| 222 | * Fetches the OpenSSL version s2n-tls was compiled with. This can be used by applications to validate at runtime |
| 223 | * that the versions of s2n-tls and Openssl that they have loaded are correct. |
| 224 | * |
| 225 | * @returns the version number of OpenSSL that s2n-tls was compiled with |
| 226 | */ |
| 227 | S2N_API extern unsigned long s2n_get_openssl_version(void); |
| 228 | |
| 229 | /** |
| 230 | * Initializes the s2n-tls library and should be called once in your application, before any other s2n-tls |
| 231 | * functions are called. Failure to call s2n_init() will result in errors from other s2n-tls functions. |
| 232 | * |
| 233 | * @warning This function is not thread safe and should only be called once. |
| 234 | * |
| 235 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 236 | */ |
| 237 | S2N_API extern int s2n_init(void); |
| 238 | |
| 239 | /** |
| 240 | * @deprecated This function is a no-op. It previously cleaned up thread-local |
| 241 | * DRBG state, but the custom DRBG has been removed. Retained for API |
| 242 | * compatibility. Use s2n_cleanup_final() for full library cleanup. |
| 243 | * |
| 244 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 245 | */ |
| 246 | S2N_API extern int s2n_cleanup(void); |
| 247 | |
| 248 | /** |
| 249 | * Performs a complete deinitialization and cleanup of the s2n-tls library. |
| 250 | * |
| 251 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 252 | */ |
| 253 | S2N_API extern int s2n_cleanup_final(void); |
| 254 | |
| 255 | /** |
| 256 | * Indicates whether s2n-tls is operating in FIPS mode. |
| 257 | */ |
| 258 | typedef enum { |
| 259 | S2N_FIPS_MODE_DISABLED = 0, |
| 260 | S2N_FIPS_MODE_ENABLED, |
| 261 | } s2n_fips_mode; |
| 262 | |
| 263 | /** |
| 264 | * Determines whether s2n-tls is operating in FIPS mode. |
| 265 | * |
| 266 | * s2n-tls enters FIPS mode on initialization when built with a version of AWS-LC that supports |
| 267 | * FIPS (https://github.com/aws/aws-lc/blob/main/crypto/fipsmodule/FIPS.md). FIPS mode controls |
| 268 | * some internal configuration related to FIPS support, like which random number generator is used. |
| 269 | * |
| 270 | * FIPS mode does not enforce the use of FIPS-approved cryptography. Applications attempting to use |
| 271 | * only FIPS-approved cryptography should also ensure that s2n-tls is configured to use a security |
| 272 | * policy that only supports FIPS-approved cryptography. |
| 273 | * |
| 274 | * @param fips_mode Set to the FIPS mode of s2n-tls. |
| 275 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 276 | */ |
| 277 | S2N_API extern int s2n_get_fips_mode(s2n_fips_mode *fips_mode); |
| 278 | |
| 279 | /** |
| 280 | * Creates a new s2n_config object. This object can (and should) be associated with many connection |
| 281 | * objects. |
| 282 | * |
| 283 | * The returned config will be initialized with default system certificates in its trust store. |
| 284 | * |
| 285 | * The returned config should be freed with `s2n_config_free()` after it's no longer in use by any |
| 286 | * connection. |
| 287 | * |
| 288 | * @returns A new configuration object suitable for configuring connections and associating certs |
| 289 | * and keys. |
| 290 | */ |
| 291 | S2N_API extern struct s2n_config *s2n_config_new(void); |
| 292 | |
| 293 | /** |
| 294 | * Creates a new s2n_config object with minimal default options. |
| 295 | * |
| 296 | * This function has better performance than `s2n_config_new()` because it does not load default |
| 297 | * system certificates into the trust store by default. To add system certificates to this config, |
| 298 | * call `s2n_config_load_system_certs()`. |
| 299 | * |
| 300 | * The returned config should be freed with `s2n_config_free()` after it's no longer in use by any |
| 301 | * connection. |
| 302 | * |
| 303 | * @returns A new configuration object suitable for configuring connections and associating certs |
| 304 | * and keys. |
| 305 | */ |
| 306 | S2N_API extern struct s2n_config *s2n_config_new_minimal(void); |
| 307 | |
| 308 | /** |
| 309 | * Frees the memory associated with an `s2n_config` object. |
| 310 | * |
| 311 | * @param config The configuration object being freed |
| 312 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 313 | */ |
| 314 | S2N_API extern int s2n_config_free(struct s2n_config *config); |
| 315 | |
| 316 | /** |
| 317 | * Frees the DH params associated with an `s2n_config` object. |
| 318 | * |
| 319 | * @param config The configuration object with DH params being freed |
| 320 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 321 | */ |
| 322 | S2N_API extern int s2n_config_free_dhparams(struct s2n_config *config); |
| 323 | |
| 324 | /** |
| 325 | * Frees the certificate chain and key associated with an `s2n_config` object. |
| 326 | * |
| 327 | * @param config The configuration object with DH params being freed |
| 328 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 329 | */ |
| 330 | S2N_API extern int s2n_config_free_cert_chain_and_key(struct s2n_config *config); |
| 331 | |
| 332 | /** |
| 333 | * Callback function type used to get the system time. |
| 334 | * |
| 335 | * The function should return 0 on success and -1 on failure. |
| 336 | */ |
| 337 | typedef int (*s2n_clock_time_nanoseconds)(void *, uint64_t *); |
| 338 | |
| 339 | /** |
| 340 | * Cache callback function that allows the caller to retrieve SSL session data |
| 341 | * from a cache. |
| 342 | * |
| 343 | * The callback function takes six arguments: |
| 344 | * a pointer to the s2n_connection object, |
| 345 | * a pointer to arbitrary data for use within the callback, |
| 346 | * a pointer to a key which can be used to retrieve the cached entry, |
| 347 | * a 64 bit unsigned integer specifying the size of this key, |
| 348 | * a pointer to a memory location where the value should be stored, |
| 349 | * and a pointer to a 64 bit unsigned integer specifying the size of this value. |
| 350 | * |
| 351 | * Initially *value_size will be set to the amount of space allocated for the value, |
| 352 | * the callback should set *value_size to the actual size of the data returned. |
| 353 | * If there is insufficient space, -1 should be returned. |
| 354 | * If the cache is not ready to provide data for the request, |
| 355 | * S2N_CALLBACK_BLOCKED should be returned. |
| 356 | * |
| 357 | * This will cause s2n_negotiate() to return S2N_BLOCKED_ON_APPLICATION_INPUT. |
| 358 | */ |
| 359 | typedef int (*s2n_cache_retrieve_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size, void *value, uint64_t *value_size); |
| 360 | |
| 361 | /** |
| 362 | * Cache callback function that allows the caller to store SSL session data in a |
| 363 | * cache. |
| 364 | * |
| 365 | * The callback function takes seven arguments: |
| 366 | * a pointer to the s2n_connection object, |
| 367 | * a pointer to arbitrary data for use within the callback, |
| 368 | * a 64-bit unsigned integer specifying the number of seconds the session data may be stored for, |
| 369 | * a pointer to a key which can be used to retrieve the cached entry, |
| 370 | * a 64 bit unsigned integer specifying the size of this key, |
| 371 | * a pointer to a value which should be stored, |
| 372 | * and a 64 bit unsigned integer specified the size of this value. |
| 373 | */ |
| 374 | typedef int (*s2n_cache_store_callback)(struct s2n_connection *conn, void *, uint64_t ttl_in_seconds, const void *key, uint64_t key_size, const void *value, uint64_t value_size); |
| 375 | |
| 376 | /** |
| 377 | * Cache callback function that allows the caller to set a callback function |
| 378 | * that will be used to delete SSL session data from a cache. |
| 379 | * |
| 380 | * The callback function takes four arguments: |
| 381 | * a pointer to s2n_connection object, |
| 382 | * a pointer to arbitrary data for use within the callback, |
| 383 | * a pointer to a key which can be used to delete the cached entry, |
| 384 | * and a 64 bit unsigned integer specifying the size of this key. |
| 385 | */ |
| 386 | typedef int (*s2n_cache_delete_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size); |
| 387 | |
| 388 | /** |
| 389 | * Allows the caller to set a callback function that will be used to get the |
| 390 | * system time. The time returned should be the number of nanoseconds since the |
| 391 | * Unix epoch (Midnight, January 1st, 1970). |
| 392 | * |
| 393 | * s2n-tls uses this clock for timestamps. |
| 394 | * |
| 395 | * @param config The configuration object being updated |
| 396 | * @param clock_fn The wall clock time callback function |
| 397 | * @param ctx An opaque pointer that the callback will be invoked with |
| 398 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 399 | */ |
| 400 | S2N_API extern int s2n_config_set_wall_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx); |
| 401 | |
| 402 | /** |
| 403 | * Allows the caller to set a callback function that will be used to get |
| 404 | * monotonic time. The monotonic time is the time since an arbitrary, unspecified |
| 405 | * point. Unlike wall clock time, it MUST never move backwards. |
| 406 | * |
| 407 | * s2n-tls uses this clock for timers. |
| 408 | * |
| 409 | * @param config The configuration object being updated |
| 410 | * @param clock_fn The monotonic time callback function |
| 411 | * @param ctx An opaque pointer that the callback will be invoked with |
| 412 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 413 | */ |
| 414 | S2N_API extern int s2n_config_set_monotonic_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx); |
| 415 | |
| 416 | /** |
| 417 | * Translates an s2n_error code to a human readable string explaining the error. |
| 418 | * |
| 419 | * @param error The error code to explain. Usually this is s2n_errno |
| 420 | * @param lang The language to explain the error code. Pass "EN" or NULL for English. |
| 421 | * @returns The error string |
| 422 | */ |
| 423 | S2N_API extern const char *s2n_strerror(int error, const char *lang); |
| 424 | |
| 425 | /** |
| 426 | * Translates an s2n_error code to a human readable string containing internal debug |
| 427 | * information, including file name and line number. This function is useful when |
| 428 | * reporting issues to the s2n-tls development team. |
| 429 | * |
| 430 | * @param error The error code to explain. Usually this is s2n_errno |
| 431 | * @param lang The language to explain the error code. Pass "EN" or NULL for English. |
| 432 | * @returns The error string |
| 433 | */ |
| 434 | S2N_API extern const char *s2n_strerror_debug(int error, const char *lang); |
| 435 | |
| 436 | /** |
| 437 | * Translates an s2n_error code to a human readable string. |
| 438 | * |
| 439 | * @param error The error code to explain. Usually this is s2n_errno |
| 440 | * @returns The error string |
| 441 | */ |
| 442 | S2N_API extern const char *s2n_strerror_name(int error); |
| 443 | |
| 444 | /** |
| 445 | * Translates an s2n_error code to a filename and line number. |
| 446 | * |
| 447 | * @param error The error code to explain. Usually this is s2n_errno. |
| 448 | * @returns The error string. |
| 449 | */ |
| 450 | S2N_API extern const char *s2n_strerror_source(int error); |
| 451 | |
| 452 | /** |
| 453 | * Opaque stack trace structure. |
| 454 | */ |
| 455 | struct s2n_stacktrace; |
| 456 | |
| 457 | /** |
| 458 | * Checks if s2n stack trace captures are enabled. |
| 459 | * |
| 460 | * @returns True if stack traces are enabled. False if they are disabled. |
| 461 | */ |
| 462 | S2N_API extern bool s2n_stack_traces_enabled(void); |
| 463 | |
| 464 | /** |
| 465 | * Configures the s2n stack trace captures option. |
| 466 | * |
| 467 | * @param newval Boolean to determine if stack traces should be enabled. True to enable them. False to disable them. |
| 468 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 469 | */ |
| 470 | S2N_API extern int s2n_stack_traces_enabled_set(bool newval); |
| 471 | |
| 472 | /** |
| 473 | * Calculates the s2n stack trace. |
| 474 | * |
| 475 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 476 | */ |
| 477 | S2N_API extern int s2n_calculate_stacktrace(void); |
| 478 | |
| 479 | /** |
| 480 | * Prints the s2n stack trace to a file. The file descriptor is expected to be |
| 481 | * open and ready for writing. |
| 482 | * |
| 483 | * @param fptr A pointer to the file s2n-tls should write the stack trace to. |
| 484 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 485 | */ |
| 486 | S2N_API extern int s2n_print_stacktrace(FILE *fptr); |
| 487 | |
| 488 | /** |
| 489 | * Clean up the memory used to contain the stack trace. |
| 490 | * |
| 491 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 492 | */ |
| 493 | S2N_API extern int s2n_free_stacktrace(void); |
| 494 | |
| 495 | /** |
| 496 | * Export the s2n_stacktrace. |
| 497 | * |
| 498 | * @param trace A pointer to the s2n_stacktrace to fill. |
| 499 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 500 | */ |
| 501 | S2N_API extern int s2n_get_stacktrace(struct s2n_stacktrace *trace); |
| 502 | |
| 503 | /** |
| 504 | * Allows the caller to set a callback function that will be used to store SSL |
| 505 | * session data in a cache. |
| 506 | * |
| 507 | * @param config The configuration object being updated |
| 508 | * @param cache_store_callback The cache store callback function. |
| 509 | * @param data An opaque context pointer that the callback will be invoked with. |
| 510 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 511 | */ |
| 512 | S2N_API extern int s2n_config_set_cache_store_callback(struct s2n_config *config, s2n_cache_store_callback cache_store_callback, void *data); |
| 513 | |
| 514 | /** |
| 515 | * Allows the caller to set a callback function that will be used to retrieve SSL |
| 516 | * session data from a cache. |
| 517 | * |
| 518 | * @param config The configuration object being updated |
| 519 | * @param cache_retrieve_callback The cache retrieve callback function. |
| 520 | * @param data An opaque context pointer that the callback will be invoked with. |
| 521 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 522 | */ |
| 523 | S2N_API extern int s2n_config_set_cache_retrieve_callback(struct s2n_config *config, s2n_cache_retrieve_callback cache_retrieve_callback, void *data); |
| 524 | |
| 525 | /** |
| 526 | * Allows the caller to set a callback function that will be used to delete SSL |
| 527 | * session data from a cache. |
| 528 | * |
| 529 | * @param config The configuration object being updated |
| 530 | * @param cache_delete_callback The cache delete callback function. |
| 531 | * @param data An opaque context pointer that the callback will be invoked with. |
| 532 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 533 | */ |
| 534 | S2N_API extern int s2n_config_set_cache_delete_callback(struct s2n_config *config, s2n_cache_delete_callback cache_delete_callback, void *data); |
| 535 | |
| 536 | /** |
| 537 | * Called when `s2n_init` is executed. |
| 538 | */ |
| 539 | typedef int (*s2n_mem_init_callback)(void); |
| 540 | |
| 541 | /** |
| 542 | * Will be called when `s2n_cleanup` is executed. |
| 543 | */ |
| 544 | typedef int (*s2n_mem_cleanup_callback)(void); |
| 545 | |
| 546 | /** |
| 547 | * A function that can allocate at least `requested` bytes of memory. |
| 548 | * |
| 549 | * It stores the location of that memory in **\*ptr** and the size of the allocated |
| 550 | * data in **\*allocated**. The function may choose to allocate more memory |
| 551 | * than was requested. s2n-tls will consider all allocated memory available for |
| 552 | * use, and will attempt to free all allocated memory when able. |
| 553 | */ |
| 554 | typedef int (*s2n_mem_malloc_callback)(void **ptr, uint32_t requested, uint32_t *allocated); |
| 555 | |
| 556 | /** |
| 557 | * Frees memory allocated by s2n_mem_malloc_callback. |
| 558 | */ |
| 559 | typedef int (*s2n_mem_free_callback)(void *ptr, uint32_t size); |
| 560 | |
| 561 | /** |
| 562 | * Allows the caller to override s2n-tls's internal memory handling functions. |
| 563 | * |
| 564 | * @warning This function must be called before s2n_init(). |
| 565 | * |
| 566 | * @param mem_init_callback The s2n_mem_init_callback |
| 567 | * @param mem_cleanup_callback The s2n_mem_cleanup_callback |
| 568 | * @param mem_malloc_callback The s2n_mem_malloc_callback |
| 569 | * @param mem_free_callback The s2n_mem_free_callback |
| 570 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 571 | */ |
| 572 | S2N_API extern int s2n_mem_set_callbacks(s2n_mem_init_callback mem_init_callback, s2n_mem_cleanup_callback mem_cleanup_callback, |
| 573 | s2n_mem_malloc_callback mem_malloc_callback, s2n_mem_free_callback mem_free_callback); |
| 574 | |
| 575 | /** |
| 576 | * @deprecated No longer used. See `s2n_rand_set_callbacks`. |
| 577 | */ |
| 578 | typedef int (*s2n_rand_init_callback)(void); |
| 579 | |
| 580 | /** |
| 581 | * @deprecated No longer used. See `s2n_rand_set_callbacks`. |
| 582 | */ |
| 583 | typedef int (*s2n_rand_cleanup_callback)(void); |
| 584 | |
| 585 | /** |
| 586 | * @deprecated No longer used. See `s2n_rand_set_callbacks`. |
| 587 | */ |
| 588 | typedef int (*s2n_rand_seed_callback)(void *data, uint32_t size); |
| 589 | |
| 590 | /** |
| 591 | * @deprecated No longer used. See `s2n_rand_set_callbacks`. |
| 592 | */ |
| 593 | typedef int (*s2n_rand_mix_callback)(void *data, uint32_t size); |
| 594 | |
| 595 | /** |
| 596 | * Allows the caller to override s2n-tls's entropy functions. |
| 597 | * |
| 598 | * @deprecated Custom random callbacks are no longer supported. Randomness is |
| 599 | * now delegated directly to libcrypto or /dev/urandom. This function is a |
| 600 | * no-op kept for backwards compatibility. |
| 601 | * |
| 602 | * @param rand_init_callback The s2n_rand_init_callback |
| 603 | * @param rand_cleanup_callback The s2n_rand_cleanup_callback |
| 604 | * @param rand_seed_callback The s2n_rand_seed_callback |
| 605 | * @param rand_mix_callback The s2n_rand_mix_callback |
| 606 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 607 | */ |
| 608 | S2N_API extern int s2n_rand_set_callbacks(s2n_rand_init_callback rand_init_callback, s2n_rand_cleanup_callback rand_cleanup_callback, |
| 609 | s2n_rand_seed_callback rand_seed_callback, s2n_rand_mix_callback rand_mix_callback); |
| 610 | |
| 611 | /** |
| 612 | * TLS extensions supported by s2n-tls |
| 613 | */ |
| 614 | typedef enum { |
| 615 | S2N_EXTENSION_SERVER_NAME = 0, |
| 616 | S2N_EXTENSION_MAX_FRAG_LEN = 1, |
| 617 | S2N_EXTENSION_OCSP_STAPLING = 5, |
| 618 | S2N_EXTENSION_SUPPORTED_GROUPS = 10, |
| 619 | S2N_EXTENSION_EC_POINT_FORMATS = 11, |
| 620 | S2N_EXTENSION_SIGNATURE_ALGORITHMS = 13, |
| 621 | S2N_EXTENSION_ALPN = 16, |
| 622 | S2N_EXTENSION_CERTIFICATE_TRANSPARENCY = 18, |
| 623 | S2N_EXTENSION_SUPPORTED_VERSIONS = 43, |
| 624 | S2N_EXTENSION_RENEGOTIATION_INFO = 65281, |
| 625 | } s2n_tls_extension_type; |
| 626 | |
| 627 | /** |
| 628 | * MFL configurations from https://datatracker.ietf.org/doc/html/rfc6066#section-4. |
| 629 | */ |
| 630 | typedef enum { |
| 631 | S2N_TLS_MAX_FRAG_LEN_512 = 1, |
| 632 | S2N_TLS_MAX_FRAG_LEN_1024 = 2, |
| 633 | S2N_TLS_MAX_FRAG_LEN_2048 = 3, |
| 634 | S2N_TLS_MAX_FRAG_LEN_4096 = 4, |
| 635 | } s2n_max_frag_len; |
| 636 | |
| 637 | /** |
| 638 | * Opaque certificate type. |
| 639 | */ |
| 640 | struct s2n_cert; |
| 641 | |
| 642 | /** |
| 643 | * Opaque certificate chain and key type. |
| 644 | */ |
| 645 | struct s2n_cert_chain_and_key; |
| 646 | |
| 647 | /** |
| 648 | * Opaque key type. |
| 649 | */ |
| 650 | struct s2n_pkey; |
| 651 | |
| 652 | /** |
| 653 | * Opaque public key type. |
| 654 | */ |
| 655 | typedef struct s2n_pkey s2n_cert_public_key; |
| 656 | |
| 657 | /** |
| 658 | * Opaque private key type. |
| 659 | */ |
| 660 | typedef struct s2n_pkey s2n_cert_private_key; |
| 661 | |
| 662 | /** |
| 663 | * Creates a new s2n_cert_chain_and_key object. This object can be associated |
| 664 | * with many config objects. It is used to represent a certificate and key pair. |
| 665 | * |
| 666 | * @returns A new object used to represent a certificate-chain/key pair |
| 667 | */ |
| 668 | S2N_API extern struct s2n_cert_chain_and_key *s2n_cert_chain_and_key_new(void); |
| 669 | |
| 670 | /** |
| 671 | * Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object. |
| 672 | * |
| 673 | * `cert_chain_pem` should be a PEM encoded certificate chain, with the first |
| 674 | * certificate in the chain being your leaf certificate. `private_key_pem` |
| 675 | * should be a PEM encoded private key corresponding to the leaf certificate. |
| 676 | * |
| 677 | * @note Prefer using s2n_cert_chain_and_key_load_pem_bytes. |
| 678 | * |
| 679 | * @param chain_and_key The certificate chain and private key handle |
| 680 | * @param chain_pem A byte array of a PEM encoded certificate chain. |
| 681 | * @param private_key_pem A byte array of a PEM encoded key. |
| 682 | * |
| 683 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 684 | */ |
| 685 | S2N_API extern int s2n_cert_chain_and_key_load_pem(struct s2n_cert_chain_and_key *chain_and_key, const char *chain_pem, const char *private_key_pem); |
| 686 | |
| 687 | /** |
| 688 | * Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object. |
| 689 | * |
| 690 | * `cert_chain_pem` should be a PEM encoded certificate chain, with the first |
| 691 | * certificate in the chain being your leaf certificate. `private_key_pem` |
| 692 | * should be a PEM encoded private key corresponding to the leaf certificate. |
| 693 | * |
| 694 | * @param chain_and_key The certificate chain and private key handle |
| 695 | * @param chain_pem A byte array of a PEM encoded certificate chain. |
| 696 | * @param chain_pem_len Size of `chain_pem` |
| 697 | * @param private_key_pem A byte array of a PEM encoded key. |
| 698 | * @param private_key_pem_len Size of `private_key_pem` |
| 699 | * |
| 700 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 701 | */ |
| 702 | S2N_API extern int s2n_cert_chain_and_key_load_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len, uint8_t *private_key_pem, uint32_t private_key_pem_len); |
| 703 | |
| 704 | /** |
| 705 | * Associates a public certificate chain with a `s2n_cert_chain_and_key` object. It does |
| 706 | * NOT set a private key, so the connection will need to be configured to |
| 707 | * [offload private key operations](https://github.com/aws/s2n-tls/blob/main/docs/usage-guide/topics/ch12-private-key-ops.md). |
| 708 | * |
| 709 | * @param chain_and_key The certificate chain and private key handle |
| 710 | * @param chain_pem A byte array of a PEM encoded certificate chain. |
| 711 | * @param chain_pem_len Size of `chain_pem` |
| 712 | * |
| 713 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 714 | */ |
| 715 | S2N_API extern int s2n_cert_chain_and_key_load_public_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len); |
| 716 | |
| 717 | /** |
| 718 | * Frees the memory associated with an `s2n_cert_chain_and_key` object. |
| 719 | * |
| 720 | * @param cert_and_key The certificate chain and private key handle |
| 721 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 722 | */ |
| 723 | S2N_API extern int s2n_cert_chain_and_key_free(struct s2n_cert_chain_and_key *cert_and_key); |
| 724 | |
| 725 | /** |
| 726 | * Adds a context to the `s2n_cert_chain_and_key` object. |
| 727 | * |
| 728 | * @param cert_and_key The certificate chain and private key handle |
| 729 | * @param ctx An opaque pointer to user supplied data. |
| 730 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 731 | */ |
| 732 | S2N_API extern int s2n_cert_chain_and_key_set_ctx(struct s2n_cert_chain_and_key *cert_and_key, void *ctx); |
| 733 | |
| 734 | /** |
| 735 | * Get the user supplied context from the `s2n_cert_chain_and_key` object. |
| 736 | * |
| 737 | * @param cert_and_key The certificate chain and private key handle |
| 738 | * @returns The user supplied pointer from s2n_cert_chain_and_key_set_ctx() |
| 739 | */ |
| 740 | S2N_API extern void *s2n_cert_chain_and_key_get_ctx(struct s2n_cert_chain_and_key *cert_and_key); |
| 741 | |
| 742 | /** |
| 743 | * Get the private key from the `s2n_cert_chain_and_key` object. |
| 744 | * |
| 745 | * @param cert_and_key The certificate chain and private key handle |
| 746 | * @returns A pointer to the `s2n_cert_private_key` |
| 747 | */ |
| 748 | S2N_API extern s2n_cert_private_key *s2n_cert_chain_and_key_get_private_key(struct s2n_cert_chain_and_key *cert_and_key); |
| 749 | |
| 750 | /** |
| 751 | * Set the raw OCSP stapling data for a certificate chain. |
| 752 | * |
| 753 | * @param chain_and_key The certificate chain handle |
| 754 | * @param data A pointer to the raw OCSP stapling data bytes. The data will be copied. |
| 755 | * @param length The length of the data bytes. |
| 756 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 757 | */ |
| 758 | S2N_API extern int s2n_cert_chain_and_key_set_ocsp_data(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length); |
| 759 | |
| 760 | /** |
| 761 | * Set the signed certificate timestamp (SCT) for a certificate chain. |
| 762 | * This is used for Certificate Transparency. |
| 763 | * |
| 764 | * @param chain_and_key The certificate chain handle |
| 765 | * @param data A pointer to the SCT data. The data will be copied. |
| 766 | * @param length The length of the data bytes. |
| 767 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 768 | */ |
| 769 | S2N_API extern int s2n_cert_chain_and_key_set_sct_list(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length); |
| 770 | |
| 771 | /** |
| 772 | * A callback function that is invoked if s2n-tls cannot resolve a conflict between |
| 773 | * two certificates with the same domain name. This function is invoked while certificates |
| 774 | * are added to an `s2n_config`. |
| 775 | * |
| 776 | * Currently, the only builtin resolution for domain name conflicts is certificate type(RSA, |
| 777 | * ECDSA, etc). The callback should return a pointer to the `s2n_cert_chain_and_key` that |
| 778 | * should be used for dns name `name`. |
| 779 | * |
| 780 | * If NULL is returned, the first certificate will be used. Typically an application |
| 781 | * will use properties like trust and expiry to implement tiebreaking. |
| 782 | */ |
| 783 | typedef struct s2n_cert_chain_and_key *(*s2n_cert_tiebreak_callback)(struct s2n_cert_chain_and_key *cert1, struct s2n_cert_chain_and_key *cert2, uint8_t *name, uint32_t name_len); |
| 784 | |
| 785 | /** |
| 786 | * Sets the `s2n_cert_tiebreak_callback` for resolving domain name conflicts. |
| 787 | * If no callback is set, the first certificate added for a domain name will always be preferred. |
| 788 | * |
| 789 | * @param config The configuration object being updated |
| 790 | * @param cert_tiebreak_cb The pointer to the certificate tiebreak function |
| 791 | * |
| 792 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 793 | */ |
| 794 | S2N_API extern int s2n_config_set_cert_tiebreak_callback(struct s2n_config *config, s2n_cert_tiebreak_callback cert_tiebreak_cb); |
| 795 | |
| 796 | /** |
| 797 | * Associates a certificate chain and private key with an `s2n_config` object. |
| 798 | * Using this API, only one cert chain of each type (like ECDSA or RSA) may be associated with a config. |
| 799 | * `cert_chain_pem` should be a PEM encoded certificate chain, with the first certificate |
| 800 | * in the chain being your server's certificate. `private_key_pem` should be a |
| 801 | * PEM encoded private key corresponding to the server certificate. |
| 802 | * |
| 803 | * @deprecated Use s2n_config_add_cert_chain_and_key_to_store instead. |
| 804 | * |
| 805 | * @param config The configuration object being updated |
| 806 | * @param cert_chain_pem A byte array of a PEM encoded certificate chain. |
| 807 | * @param private_key_pem A byte array of a PEM encoded key. |
| 808 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 809 | */ |
| 810 | S2N_API extern int s2n_config_add_cert_chain_and_key(struct s2n_config *config, const char *cert_chain_pem, const char *private_key_pem); |
| 811 | |
| 812 | /** |
| 813 | * The preferred method of associating a certificate chain and private key pair with an `s2n_config` object. |
| 814 | * This method may be called multiple times to support multiple key types (RSA, RSA-PSS, ECDSA) and multiple |
| 815 | * domains. On the server side, the certificate selected will be based on the incoming SNI value and the |
| 816 | * client's capabilities (supported ciphers). |
| 817 | * |
| 818 | * In the case of no certificate matching the client's SNI extension or if no SNI extension was sent by |
| 819 | * the client, the certificate from the `first` call to `s2n_config_add_cert_chain_and_key_to_store()` |
| 820 | * will be selected. Use `s2n_config_set_cert_chain_and_key_defaults()` to set different defaults. |
| 821 | * |
| 822 | * @warning It is not recommended to free or modify the `cert_key_pair` as any subsequent changes will be |
| 823 | * reflected in the config. |
| 824 | * |
| 825 | * @param config The configuration object being updated |
| 826 | * @param cert_key_pair The certificate chain and private key handle |
| 827 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 828 | */ |
| 829 | S2N_API extern int s2n_config_add_cert_chain_and_key_to_store(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair); |
| 830 | |
| 831 | /** |
| 832 | * Explicitly sets certificate chain and private key pairs to be used as defaults for each auth |
| 833 | * method (key type). A "default" certificate is used when there is not an SNI match with any other |
| 834 | * configured certificate. |
| 835 | * |
| 836 | * Only one certificate can be set as the default per auth method (one RSA default, one ECDSA default, |
| 837 | * etc.). All previous default certificates will be cleared and re-set when this API is called. |
| 838 | * |
| 839 | * This API is called for a specific `s2n_config` object. s2n-tls will attempt to automatically choose |
| 840 | * default certificates for each auth method (key type) based on the order that `s2n_cert_chain_and_key` |
| 841 | * are added to the `s2n_config` using one of the APIs listed above. |
| 842 | * `s2n_config_set_cert_chain_and_key_defaults` can be called at any time; s2n-tls will clear defaults |
| 843 | * and no longer attempt to automatically choose any default certificates. |
| 844 | * |
| 845 | * @param config The configuration object being updated |
| 846 | * @param cert_key_pairs An array of certificate chain and private key handles |
| 847 | * @param num_cert_key_pairs The amount of handles in cert_key_pairs |
| 848 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 849 | */ |
| 850 | S2N_API extern int s2n_config_set_cert_chain_and_key_defaults(struct s2n_config *config, |
| 851 | struct s2n_cert_chain_and_key **cert_key_pairs, uint32_t num_cert_key_pairs); |
| 852 | |
| 853 | /** |
| 854 | * Adds to the trust store from a CA file or directory containing trusted certificates. |
| 855 | * |
| 856 | * When configs are created with `s2n_config_new()`, the trust store is initialized with default |
| 857 | * system certificates. To completely override these certificates, call |
| 858 | * `s2n_config_wipe_trust_store()` before calling this function. |
| 859 | * |
| 860 | * @note The trust store will be initialized with the common locations for the host |
| 861 | * operating system by default. |
| 862 | * |
| 863 | * @warning This API uses the PEM parsing implementation from the linked libcrypto. This |
| 864 | * implementation will typically make a best-effort attempt to parse all of the certificates in the |
| 865 | * provided file or directory. This permissive approach may silently ignore malformed certificates, |
| 866 | * leading to possible connection failures if a certificate was expected to exist in the trust |
| 867 | * store but was skipped while parsing. As such, this API should only be used on PEMs that are |
| 868 | * known to be well-formed and parsable with the linked libcrypto, such as the system trust store. |
| 869 | * For all other PEMs, `s2n_config_add_pem_to_trust_store()` should be used instead, which parses |
| 870 | * more strictly. |
| 871 | * |
| 872 | * @param config The configuration object being updated |
| 873 | * @param ca_pem_filename A string for the file path of the CA PEM file. |
| 874 | * @param ca_dir A string for the directory of the CA PEM files. |
| 875 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 876 | */ |
| 877 | S2N_API extern int s2n_config_set_verification_ca_location(struct s2n_config *config, const char *ca_pem_filename, const char *ca_dir); |
| 878 | |
| 879 | /** |
| 880 | * Adds a PEM to the trust store. This will allocate memory, and load `pem` into the trust store. |
| 881 | * |
| 882 | * When configs are created with `s2n_config_new()`, the trust store is initialized with default |
| 883 | * system certificates. To completely override these certificates, call |
| 884 | * `s2n_config_wipe_trust_store()` before calling this function. |
| 885 | * |
| 886 | * @note This API uses the s2n-tls PEM parsing implementation, which is more strict than typical |
| 887 | * libcrypto implementations such as OpenSSL. An error is returned if any unexpected data is |
| 888 | * encountered while parsing `pem`. This allows applications to be made aware of any malformed |
| 889 | * certificates rather than attempt to negotiate with a partial trust store. However, some PEMs may |
| 890 | * need to be loaded that are not under control of the application, such as system trust stores. In |
| 891 | * this case, `s2n_config_set_verification_ca_location()` may be used, which performs more widely |
| 892 | * compatible and permissive parsing from the linked libcrypto. |
| 893 | * |
| 894 | * @param config The configuration object being updated |
| 895 | * @param pem The string value of the PEM certificate. |
| 896 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 897 | */ |
| 898 | S2N_API extern int s2n_config_add_pem_to_trust_store(struct s2n_config *config, const char *pem); |
| 899 | |
| 900 | /** |
| 901 | * Clears the trust store of all certificates. |
| 902 | * |
| 903 | * When configs are created with `s2n_config_new()`, the trust store is initialized with default |
| 904 | * system certificates. To completely override these certificates, call this function before |
| 905 | * functions like `s2n_config_set_verification_ca_location()` or |
| 906 | * `s2n_config_add_pem_to_trust_store()`. |
| 907 | * |
| 908 | * @param config The configuration object being updated |
| 909 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 910 | */ |
| 911 | S2N_API extern int s2n_config_wipe_trust_store(struct s2n_config *config); |
| 912 | |
| 913 | /** |
| 914 | * Loads default system certificates into the trust store. |
| 915 | * |
| 916 | * `s2n_config_new_minimal()` doesn't load default system certificates into the config's trust |
| 917 | * store by default. If `config` was created with `s2n_config_new_minimal`, this function can be |
| 918 | * used to load system certificates into the trust store. |
| 919 | * |
| 920 | * @note This API will error if called on a config that has already loaded system certificates |
| 921 | * into its trust store, which includes all configs created with `s2n_config_new()`. |
| 922 | * |
| 923 | * @param config The configuration object being updated |
| 924 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 925 | */ |
| 926 | S2N_API extern int s2n_config_load_system_certs(struct s2n_config *config); |
| 927 | |
| 928 | /** |
| 929 | * Toggles whether generated signatures are verified before being sent. |
| 930 | */ |
| 931 | typedef enum { |
| 932 | S2N_VERIFY_AFTER_SIGN_DISABLED, |
| 933 | S2N_VERIFY_AFTER_SIGN_ENABLED |
| 934 | } s2n_verify_after_sign; |
| 935 | |
| 936 | /** |
| 937 | * Toggle whether generated signatures are verified before being sent. |
| 938 | * |
| 939 | * Although signatures produced by the underlying libcrypto should always be valid, |
| 940 | * hardware faults, bugs in the signing implementation, or other uncommon factors |
| 941 | * can cause unexpected mistakes in the final signatures. Because these mistakes |
| 942 | * can leak information about the private key, applications with low trust in their |
| 943 | * hardware or libcrypto may want to verify signatures before sending them. |
| 944 | * |
| 945 | * However, this feature will significantly impact handshake latency. |
| 946 | * Additionally, most libcrypto implementations already check for common errors in signatures. |
| 947 | */ |
| 948 | S2N_API extern int s2n_config_set_verify_after_sign(struct s2n_config *config, s2n_verify_after_sign mode); |
| 949 | |
| 950 | /** |
| 951 | * Set a custom send buffer size. |
| 952 | * |
| 953 | * This buffer is used to stage records for sending. By default, |
| 954 | * enough memory is allocated to hold a single record of the maximum |
| 955 | * size configured for the connection. With the default fragment size, |
| 956 | * that is about 8K bytes. |
| 957 | * |
| 958 | * Less memory can be allocated for the send buffer, but this will result in |
| 959 | * smaller, more fragmented records and increased overhead. While the absolute |
| 960 | * minimum size required is 1034 bytes, at least 2K bytes is recommended for |
| 961 | * reasonable record sizes. |
| 962 | * |
| 963 | * More memory can be allocated for the send buffer. This will result in s2n-tls |
| 964 | * buffering multiple records before sending them, reducing system write calls. |
| 965 | * At least 17K bytes is recommended for this use case, or at least 35K bytes |
| 966 | * if larger fragment sizes are used via `s2n_connection_prefer_throughput()`. |
| 967 | * |
| 968 | * @param config The configuration object being updated |
| 969 | * @param size The desired custom buffer size. |
| 970 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 971 | */ |
| 972 | S2N_API extern int s2n_config_set_send_buffer_size(struct s2n_config *config, uint32_t size); |
| 973 | |
| 974 | /** |
| 975 | * Enable or disable receiving of multiple TLS records in a single s2n_recv call |
| 976 | * |
| 977 | * By default, s2n-tls returns from s2n_recv() after reading a single TLS record. |
| 978 | * Enabling receiving of multiple records will instead cause s2n_recv() to attempt |
| 979 | * to read until the application-provided output buffer is full. This may be more |
| 980 | * efficient, especially if larger receive buffers are used. |
| 981 | * |
| 982 | * @note If this option is enabled with blocking IO, the call to s2n_recv() will |
| 983 | * not return until either the application-provided output buffer is full or the |
| 984 | * peer closes the connection. This may lead to unintentionally long waits if the |
| 985 | * peer does not send enough data. |
| 986 | * |
| 987 | * @param config The configuration object being updated |
| 988 | * @param enabled Set to `true` if multiple record receive is to be enabled; `false` to disable. |
| 989 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 990 | */ |
| 991 | S2N_API extern int s2n_config_set_recv_multi_record(struct s2n_config *config, bool enabled); |
| 992 | |
| 993 | /** |
| 994 | * A callback function invoked (usually multiple times) during X.509 validation for each |
| 995 | * name encountered in the leaf certificate. |
| 996 | * |
| 997 | * Return 1 to trust that hostname or 0 to not trust the hostname. |
| 998 | * |
| 999 | * If this function returns 1, then the certificate is considered trusted and that portion |
| 1000 | * of the X.509 validation will succeed. |
| 1001 | * |
| 1002 | * If no hostname results in a 1 being returned, the certificate will be untrusted and the |
| 1003 | * validation will terminate immediately. |
| 1004 | * |
| 1005 | * Data is a opaque user context set in s2n_config_set_verify_host_callback() or s2n_connection_set_verify_host_callback(). |
| 1006 | */ |
| 1007 | typedef uint8_t (*s2n_verify_host_fn)(const char *host_name, size_t host_name_len, void *data); |
| 1008 | |
| 1009 | /** |
| 1010 | * Sets the callback to use for verifying that a hostname from an X.509 certificate is trusted. |
| 1011 | * |
| 1012 | * The default behavior is to require that the hostname match the server name set with s2n_set_server_name(). |
| 1013 | * This will likely lead to all client certificates being rejected, so the callback will need to be overridden when using |
| 1014 | * client authentication. |
| 1015 | * |
| 1016 | * This change will be inherited by s2n_connections using this config. If a separate callback for different connections |
| 1017 | * using the same config is desired, see s2n_connection_set_verify_host_callback(). |
| 1018 | * |
| 1019 | * @param config The configuration object being updated |
| 1020 | * @param data A user supplied opaque context to pass back to the callback |
| 1021 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1022 | */ |
| 1023 | S2N_API extern int s2n_config_set_verify_host_callback(struct s2n_config *config, s2n_verify_host_fn, void *data); |
| 1024 | |
| 1025 | /** |
| 1026 | * Toggles whether or not to validate stapled OCSP responses. |
| 1027 | * |
| 1028 | * 1 means OCSP responses will be validated when they are encountered, while 0 means this step will |
| 1029 | * be skipped. |
| 1030 | * |
| 1031 | * The default value is 1 if the underlying libCrypto implementation supports OCSP. |
| 1032 | * |
| 1033 | * @param config The configuration object being updated |
| 1034 | * @param check_ocsp The desired OCSP response check configuration |
| 1035 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1036 | */ |
| 1037 | S2N_API extern int s2n_config_set_check_stapled_ocsp_response(struct s2n_config *config, uint8_t check_ocsp); |
| 1038 | |
| 1039 | /** |
| 1040 | * Disables timestamp validation for received certificates. |
| 1041 | * |
| 1042 | * By default, s2n-tls checks the notBefore and notAfter fields on the certificates it receives |
| 1043 | * during the handshake. If the current date is not within the range of these fields for any |
| 1044 | * certificate in the chain of trust, `s2n_negotiate()` will error. This validation is in |
| 1045 | * accordance with RFC 5280, section 6.1.3 a.2: |
| 1046 | * https://datatracker.ietf.org/doc/html/rfc5280#section-6.1.3. |
| 1047 | * |
| 1048 | * This API will disable this timestamp validation, permitting negotiation with peers that send |
| 1049 | * expired certificates, or certificates that are not yet considered valid. |
| 1050 | * |
| 1051 | * @warning Applications calling this API should seriously consider the security implications of |
| 1052 | * disabling this validation. The validity period of a certificate corresponds to the range of time |
| 1053 | * in which the CA is guaranteed to maintain information regarding the certificate's revocation |
| 1054 | * status. As such, it may not be possible to obtain accurate revocation information for |
| 1055 | * certificates with invalid timestamps. Applications disabling this validation MUST implement |
| 1056 | * some external method for limiting certificate lifetime. |
| 1057 | * |
| 1058 | * @param config The associated connection config. |
| 1059 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 1060 | */ |
| 1061 | S2N_API extern int s2n_config_disable_x509_time_verification(struct s2n_config *config); |
| 1062 | |
| 1063 | /** Disable TLS intent verification for received certificates. |
| 1064 | * |
| 1065 | * By default, s2n-tls will verify that received certificates set Key Usage / Extended Key Usage |
| 1066 | * fields that are consistent with the current TLS context (e.g. checking that serverAuth is set |
| 1067 | * when verifying server certificates as a client, or checking that clientAuth is set when |
| 1068 | * verifying client certificates as a server). This verification ensures that received certificates |
| 1069 | * are being used for their intended purpose as specified by the issuer. |
| 1070 | * |
| 1071 | * This verification may be incompatible with some PKIs where intent is improperly specified. |
| 1072 | * `s2n_config_disable_x509_intent_verification()` may be called in this case to disable the |
| 1073 | * verification. This verification should only be disabled if it is known that all received |
| 1074 | * certificates will be issued from a CA that intended for the certificates to be used in the given |
| 1075 | * TLS context, despite what's indicated in the Key Usage / Extended Key Usage extensions. |
| 1076 | * |
| 1077 | * @note If a received certificate doesn't contain a Key Usage / Extended Key Usage extension, it's |
| 1078 | * assumed that the issuer permits the certificate to be used for any purpose. s2n-tls will |
| 1079 | * only reject a certificate due to invalid intent if the issuer explicitly indicates a |
| 1080 | * purpose that is invalid for the TLS context in which it is received. |
| 1081 | * |
| 1082 | * @param config The associated connection config. |
| 1083 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 1084 | */ |
| 1085 | S2N_API extern int s2n_config_disable_x509_intent_verification(struct s2n_config *config); |
| 1086 | |
| 1087 | /** |
| 1088 | * Turns off all X.509 validation during the negotiation phase of the connection. This should only |
| 1089 | * be used for testing or debugging purposes. |
| 1090 | * |
| 1091 | * @param config The configuration object being updated |
| 1092 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1093 | */ |
| 1094 | S2N_API extern int s2n_config_disable_x509_verification(struct s2n_config *config); |
| 1095 | |
| 1096 | /** |
| 1097 | * Sets the maximum allowed depth of a cert chain used for X509 validation. The default value is |
| 1098 | * 7. If this limit is exceeded, validation will fail if s2n_config_disable_x509_verification() |
| 1099 | * has not been called. 0 is an illegal value and will return an error. |
| 1100 | * 1 means only a root certificate will be used. |
| 1101 | * |
| 1102 | * @param config The configuration object being updated |
| 1103 | * @param max_depth The number of allowed certificates in the certificate chain |
| 1104 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1105 | */ |
| 1106 | S2N_API extern int s2n_config_set_max_cert_chain_depth(struct s2n_config *config, uint16_t max_depth); |
| 1107 | |
| 1108 | /** |
| 1109 | * Associates a set of Diffie-Hellman parameters with an `s2n_config` object. |
| 1110 | * @note `dhparams_pem` should be PEM encoded DH parameters. |
| 1111 | * |
| 1112 | * @param config The configuration object being updated |
| 1113 | * @param dhparams_pem A string containing the PEM encoded DH parameters. |
| 1114 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1115 | */ |
| 1116 | S2N_API extern int s2n_config_add_dhparams(struct s2n_config *config, const char *dhparams_pem); |
| 1117 | |
| 1118 | /** |
| 1119 | * Sets the security policy that includes the cipher/kem/signature/ecc preferences and |
| 1120 | * protocol version. |
| 1121 | * |
| 1122 | * See the [USAGE-GUIDE.md](https://github.com/aws/s2n-tls/blob/main/docs/usage-guide) for how to use security policies. |
| 1123 | */ |
| 1124 | S2N_API extern int s2n_config_set_cipher_preferences(struct s2n_config *config, const char *version); |
| 1125 | |
| 1126 | /** |
| 1127 | * Appends the provided application protocol to the preference list |
| 1128 | * |
| 1129 | * The data provided in `protocol` parameter will be copied into an internal buffer |
| 1130 | * |
| 1131 | * @param config The configuration object being updated |
| 1132 | * @param protocol A pointer to a byte array value |
| 1133 | * @param protocol_len The length of bytes that should be read from `protocol`. Note: this value cannot be 0, otherwise an error will be returned. |
| 1134 | */ |
| 1135 | S2N_API extern int s2n_config_append_protocol_preference(struct s2n_config *config, const uint8_t *protocol, uint8_t protocol_len); |
| 1136 | |
| 1137 | /** |
| 1138 | * Sets the application protocol preferences on an `s2n_config` object. |
| 1139 | * `protocols` is a list in order of preference, with most preferred protocol first, and of |
| 1140 | * length `protocol_count`. |
| 1141 | * |
| 1142 | * When acting as an `S2N_CLIENT` the protocol list is included in the Client Hello message |
| 1143 | * as the ALPN extension. |
| 1144 | * |
| 1145 | * As an `S2N_SERVER`, the list is used to negotiate a mutual application protocol with the |
| 1146 | * client. After the negotiation for the connection has completed, the agreed upon protocol |
| 1147 | * can be retrieved with s2n_get_application_protocol() |
| 1148 | * |
| 1149 | * @param config The configuration object being updated |
| 1150 | * @param protocols The list of preferred protocols, in order of preference |
| 1151 | * @param protocol_count The size of the protocols list |
| 1152 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1153 | */ |
| 1154 | S2N_API extern int s2n_config_set_protocol_preferences(struct s2n_config *config, const char *const *protocols, int protocol_count); |
| 1155 | |
| 1156 | /** |
| 1157 | * Enum used to define the type, if any, of certificate status request |
| 1158 | * a connection should make during the handshake. The only supported status request type is |
| 1159 | * OCSP, `S2N_STATUS_REQUEST_OCSP`. |
| 1160 | */ |
| 1161 | typedef enum { |
| 1162 | S2N_STATUS_REQUEST_NONE = 0, |
| 1163 | S2N_STATUS_REQUEST_OCSP = 1 |
| 1164 | } s2n_status_request_type; |
| 1165 | |
| 1166 | /** |
| 1167 | * Sets up a connection to request the certificate status of a peer during an SSL handshake. If set |
| 1168 | * to S2N_STATUS_REQUEST_NONE, no status request is made. |
| 1169 | * |
| 1170 | * @note SHA-1 is the only supported hash algorithm for the `certID` field. This is different |
| 1171 | * from the hash algorithm used for the OCSP signature. See |
| 1172 | * [RFC 6960](https://datatracker.ietf.org/doc/html/rfc6960#section-4.1.1) for more information. |
| 1173 | * While unlikely to be the case, if support for a different hash algorithm is required, the |
| 1174 | * s2n-tls validation can be disabled with `s2n_config_set_check_stapled_ocsp_response()` and the |
| 1175 | * response can be retrieved for manual validation with `s2n_connection_get_ocsp_response()`. |
| 1176 | * |
| 1177 | * @param config The configuration object being updated |
| 1178 | * @param type The desired request status type |
| 1179 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1180 | */ |
| 1181 | S2N_API extern int s2n_config_set_status_request_type(struct s2n_config *config, s2n_status_request_type type); |
| 1182 | |
| 1183 | /** |
| 1184 | * Enum to set Certificate Transparency Support level. |
| 1185 | */ |
| 1186 | typedef enum { |
| 1187 | S2N_CT_SUPPORT_NONE = 0, |
| 1188 | S2N_CT_SUPPORT_REQUEST = 1 |
| 1189 | } s2n_ct_support_level; |
| 1190 | |
| 1191 | /** |
| 1192 | * Set the Certificate Transparency Support level. |
| 1193 | * |
| 1194 | * @param config The configuration object being updated |
| 1195 | * @param level The desired Certificate Transparency Support configuration |
| 1196 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1197 | */ |
| 1198 | S2N_API extern int s2n_config_set_ct_support_level(struct s2n_config *config, s2n_ct_support_level level); |
| 1199 | |
| 1200 | /** |
| 1201 | * Sets whether or not a connection should terminate on receiving a WARNING alert from its peer. |
| 1202 | * |
| 1203 | * `alert_behavior` can take the following values: |
| 1204 | * - `S2N_ALERT_FAIL_ON_WARNINGS` default behavior: s2n-tls will terminate the connection if its peer sends a WARNING alert. |
| 1205 | * - `S2N_ALERT_IGNORE_WARNINGS` - with the exception of `close_notify` s2n-tls will ignore all WARNING alerts and keep communicating with its peer. This setting is ignored in TLS1.3 |
| 1206 | * |
| 1207 | * @note TLS1.3 terminates a connection for all alerts except user_canceled. |
| 1208 | * @warning S2N_ALERT_FAIL_ON_WARNINGS is the recommended behavior. Past TLS protocol vulnerabilities have involved downgrading alerts to warnings. |
| 1209 | */ |
| 1210 | typedef enum { |
| 1211 | S2N_ALERT_FAIL_ON_WARNINGS = 0, |
| 1212 | S2N_ALERT_IGNORE_WARNINGS = 1 |
| 1213 | } s2n_alert_behavior; |
| 1214 | |
| 1215 | /** |
| 1216 | * Sets the config's alert behavior based on the `s2n_alert_behavior` enum. |
| 1217 | * |
| 1218 | * @param config The configuration object being updated |
| 1219 | * @param alert_behavior The desired alert behavior. |
| 1220 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1221 | */ |
| 1222 | S2N_API extern int s2n_config_set_alert_behavior(struct s2n_config *config, s2n_alert_behavior alert_behavior); |
| 1223 | |
| 1224 | /** |
| 1225 | * Sets the extension data in the `s2n_config` object for the specified extension. |
| 1226 | * This method will clear any existing data that is set. If the data and length |
| 1227 | * parameters are set to NULL, no new data is set in the `s2n_config` object, |
| 1228 | * effectively clearing existing data. |
| 1229 | * |
| 1230 | * @deprecated Use s2n_cert_chain_and_key_set_ocsp_data and s2n_cert_chain_and_key_set_sct_list instead. |
| 1231 | * |
| 1232 | * @param config The configuration object being updated |
| 1233 | * @param type The extension type |
| 1234 | * @param data Data for the extension |
| 1235 | * @param length Length of the `data` buffer |
| 1236 | */ |
| 1237 | S2N_API extern int s2n_config_set_extension_data(struct s2n_config *config, s2n_tls_extension_type type, const uint8_t *data, uint32_t length); |
| 1238 | |
| 1239 | /** |
| 1240 | * Allows the caller to set a TLS Maximum Fragment Length extension that will be used |
| 1241 | * to fragment outgoing messages. s2n-tls currently does not reject fragments larger |
| 1242 | * than the configured maximum when in server mode. The TLS negotiated maximum fragment |
| 1243 | * length overrides the preference set by the `s2n_connection_prefer_throughput` and |
| 1244 | * `s2n_connection_prefer_low_latency`. |
| 1245 | * |
| 1246 | * @note Some TLS implementations do not respect their peer's max fragment length extension. |
| 1247 | * |
| 1248 | * @param config The configuration object being updated |
| 1249 | * @param mfl_code The selected MFL size |
| 1250 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1251 | */ |
| 1252 | S2N_API extern int s2n_config_send_max_fragment_length(struct s2n_config *config, s2n_max_frag_len mfl_code); |
| 1253 | |
| 1254 | /** |
| 1255 | * Allows the server to opt-in to accept client's TLS maximum fragment length extension |
| 1256 | * requests. If this API is not called, and client requests the extension, server will ignore |
| 1257 | * the request and continue TLS handshake with default maximum fragment length of 8k bytes |
| 1258 | * |
| 1259 | * @note Some TLS implementations do not respect their peer's max fragment length extension. |
| 1260 | * |
| 1261 | * @param config The configuration object being updated |
| 1262 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1263 | */ |
| 1264 | S2N_API extern int s2n_config_accept_max_fragment_length(struct s2n_config *config); |
| 1265 | |
| 1266 | /** |
| 1267 | * Sets the lifetime of the cached session state. The default value is 15 hours. |
| 1268 | * |
| 1269 | * @param config The configuration object being updated |
| 1270 | * @param lifetime_in_secs The desired lifetime of the session state in seconds |
| 1271 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1272 | */ |
| 1273 | S2N_API extern int s2n_config_set_session_state_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs); |
| 1274 | |
| 1275 | /** |
| 1276 | * Enable or disable session resumption using session ticket. |
| 1277 | * |
| 1278 | * @param config The configuration object being updated |
| 1279 | * @param enabled The configuration object being updated. Set to 1 to enable. Set to 0 to disable. |
| 1280 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1281 | */ |
| 1282 | S2N_API extern int s2n_config_set_session_tickets_onoff(struct s2n_config *config, uint8_t enabled); |
| 1283 | |
| 1284 | /** |
| 1285 | * Enable or disable session caching. |
| 1286 | * |
| 1287 | * @note Session caching will not be turned on unless all three session cache callbacks are set |
| 1288 | * prior to calling this function. |
| 1289 | * |
| 1290 | * @param config The configuration object being updated |
| 1291 | * @param enabled The configuration object being updated. Set to 1 to enable. Set to 0 to disable. |
| 1292 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1293 | */ |
| 1294 | S2N_API extern int s2n_config_set_session_cache_onoff(struct s2n_config *config, uint8_t enabled); |
| 1295 | |
| 1296 | /** |
| 1297 | * Sets how long a session ticket key will be in a state where it can be used for both encryption |
| 1298 | * and decryption of tickets on the server side. |
| 1299 | * |
| 1300 | * @note The default value is 2 hours. |
| 1301 | * @param config The configuration object being updated |
| 1302 | * @param lifetime_in_secs The desired lifetime of decrypting and encrypting tickets in seconds |
| 1303 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1304 | */ |
| 1305 | S2N_API extern int s2n_config_set_ticket_encrypt_decrypt_key_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs); |
| 1306 | |
| 1307 | /** |
| 1308 | * Sets how long a session ticket key will be in a state where it can used just for decryption of |
| 1309 | * already assigned tickets on the server side. Once decrypted, the session will resume and the |
| 1310 | * server will issue a new session ticket encrypted using a key in encrypt-decrypt state. |
| 1311 | * |
| 1312 | * @note The default value is 13 hours. |
| 1313 | * @param config The configuration object being updated |
| 1314 | * @param lifetime_in_secs The desired lifetime of decrypting and encrypting tickets in seconds |
| 1315 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1316 | */ |
| 1317 | S2N_API extern int s2n_config_set_ticket_decrypt_key_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs); |
| 1318 | |
| 1319 | /** |
| 1320 | * Adds session ticket key on the server side. It would be ideal to add new keys after every |
| 1321 | * (encrypt_decrypt_key_lifetime_in_nanos/2) nanos because this will allow for gradual and |
| 1322 | * linear transition of a key from encrypt-decrypt state to decrypt-only state. |
| 1323 | * |
| 1324 | * @param config The configuration object being updated |
| 1325 | * @param name Name of the session ticket key that should be randomly generated to avoid collisions |
| 1326 | * @param name_len Length of session ticket key name |
| 1327 | * @param key Key used to perform encryption/decryption of session ticket |
| 1328 | * @param key_len Length of the session ticket key |
| 1329 | * @param intro_time_in_seconds_from_epoch Time at which the session ticket key is introduced. If this is 0, then intro_time_in_seconds_from_epoch is set to now. |
| 1330 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1331 | */ |
| 1332 | S2N_API extern int s2n_config_add_ticket_crypto_key(struct s2n_config *config, const uint8_t *name, uint32_t name_len, |
| 1333 | uint8_t *key, uint32_t key_len, uint64_t intro_time_in_seconds_from_epoch); |
| 1334 | |
| 1335 | /** |
| 1336 | * Requires that session tickets are only used when forward secrecy is possible. |
| 1337 | * |
| 1338 | * Restricts session resumption to TLS1.3, as the tickets used in TLS1.2 resumption are |
| 1339 | * not forward secret. Clients should not expect to receive new session tickets and servers |
| 1340 | * will not send new session tickets when TLS1.2 is negotiated and ticket forward secrecy is required. |
| 1341 | * |
| 1342 | * @note The default behavior is that forward secrecy is not required. |
| 1343 | * |
| 1344 | * @param config The config object being updated |
| 1345 | * @param enabled Indicates if forward secrecy is required or not on tickets |
| 1346 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1347 | */ |
| 1348 | S2N_API extern int s2n_config_require_ticket_forward_secrecy(struct s2n_config *config, bool enabled); |
| 1349 | |
| 1350 | /** |
| 1351 | * Sets user defined context on the `s2n_config` object. |
| 1352 | * |
| 1353 | * @param config The configuration object being updated |
| 1354 | * @param ctx A pointer to the user defined ctx. |
| 1355 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1356 | */ |
| 1357 | S2N_API extern int s2n_config_set_ctx(struct s2n_config *config, void *ctx); |
| 1358 | |
| 1359 | /** |
| 1360 | * Gets the user defined context from the `s2n_config` object. |
| 1361 | * The context is set by calling s2n_config_set_ctx() |
| 1362 | * |
| 1363 | * @param config The configuration object being accessed |
| 1364 | * @param ctx A pointer to the user defined ctx. |
| 1365 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1366 | */ |
| 1367 | S2N_API extern int s2n_config_get_ctx(struct s2n_config *config, void **ctx); |
| 1368 | |
| 1369 | /** |
| 1370 | * Used to declare connections as server or client type, respectively. |
| 1371 | */ |
| 1372 | typedef enum { |
| 1373 | S2N_SERVER, |
| 1374 | S2N_CLIENT |
| 1375 | } s2n_mode; |
| 1376 | |
| 1377 | /** |
| 1378 | * Creates a new connection object. Each s2n-tls SSL/TLS connection uses |
| 1379 | * one of these objects. These connection objects can be operated on by up |
| 1380 | * to two threads at a time, one sender and one receiver, but neither sending |
| 1381 | * nor receiving are atomic, so if these objects are being called by multiple |
| 1382 | * sender or receiver threads, you must perform your own locking to ensure |
| 1383 | * that only one sender or receiver is active at a time. |
| 1384 | * |
| 1385 | * The `mode` parameters specifies if the caller is a server, or is a client. |
| 1386 | * Connections objects are re-usable across many connections, and should be |
| 1387 | * re-used (to avoid deallocating and allocating memory). You should wipe |
| 1388 | * connections immediately after use. |
| 1389 | * |
| 1390 | * @param mode The desired connection type |
| 1391 | * @returns A s2n_connection handle |
| 1392 | */ |
| 1393 | S2N_API extern struct s2n_connection *s2n_connection_new(s2n_mode mode); |
| 1394 | |
| 1395 | /** |
| 1396 | * Associates a configuration object with a connection. |
| 1397 | * |
| 1398 | * @param conn The connection object being associated |
| 1399 | * @param config The configuration object being associated |
| 1400 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1401 | */ |
| 1402 | S2N_API extern int s2n_connection_set_config(struct s2n_connection *conn, struct s2n_config *config); |
| 1403 | |
| 1404 | /** |
| 1405 | * Sets user defined context in `s2n_connection` object. |
| 1406 | * |
| 1407 | * @param conn The connection object being updated |
| 1408 | * @param ctx A pointer to the user defined context |
| 1409 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1410 | */ |
| 1411 | S2N_API extern int s2n_connection_set_ctx(struct s2n_connection *conn, void *ctx); |
| 1412 | |
| 1413 | /** |
| 1414 | * Gets user defined context from a `s2n_connection` object. |
| 1415 | * |
| 1416 | * @param conn The connection object that contains the desired context |
| 1417 | */ |
| 1418 | S2N_API extern void *s2n_connection_get_ctx(struct s2n_connection *conn); |
| 1419 | |
| 1420 | /** |
| 1421 | * The callback function takes a s2n-tls connection as input, which receives the ClientHello |
| 1422 | * and the context previously provided in `s2n_config_set_client_hello_cb`. The callback can |
| 1423 | * access any ClientHello information from the connection and use the `s2n_connection_set_config` |
| 1424 | * call to change the config of the connection. |
| 1425 | */ |
| 1426 | typedef int s2n_client_hello_fn(struct s2n_connection *conn, void *ctx); |
| 1427 | |
| 1428 | /** |
| 1429 | * Client Hello callback modes |
| 1430 | * - `S2N_CLIENT_HELLO_CB_BLOCKING` (default): |
| 1431 | * - In this mode s2n-tls expects the callback to complete its work and return the appropriate response code before the handshake continues. If any of the connection properties were changed based on the server_name extension the callback must either return a value greater than 0 or invoke `s2n_connection_server_name_extension_used`, otherwise the callback returns 0 to continue the handshake. |
| 1432 | * - `S2N_CLIENT_HELLO_CB_NONBLOCKING`: |
| 1433 | * - In non-blocking mode, s2n-tls expects the callback to not complete its work. If the callback returns a response code of 0, s2n-tls will return `S2N_FAILURE` with `S2N_ERR_T_BLOCKED` error type and `s2n_blocked_status` set to `S2N_BLOCKED_ON_APPLICATION_INPUT`. The handshake is paused and further calls to `s2n_negotiate` will continue to return the same error until `s2n_client_hello_cb_done` is invoked for the `s2n_connection` to resume the handshake. If any of the connection properties were changed on the basis of the server_name extension then `s2n_connection_server_name_extension_used` must be invoked before marking the callback done. |
| 1434 | */ |
| 1435 | typedef enum { |
| 1436 | S2N_CLIENT_HELLO_CB_BLOCKING, |
| 1437 | S2N_CLIENT_HELLO_CB_NONBLOCKING |
| 1438 | } s2n_client_hello_cb_mode; |
| 1439 | |
| 1440 | /** |
| 1441 | * Allows the caller to set a callback function that will be called after ClientHello was parsed. |
| 1442 | * |
| 1443 | * @param config The configuration object being updated |
| 1444 | * @param client_hello_callback The client hello callback function |
| 1445 | * @param ctx A pointer to a user defined context that the Client Hello callback will be invoked with. |
| 1446 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1447 | */ |
| 1448 | S2N_API extern int s2n_config_set_client_hello_cb(struct s2n_config *config, s2n_client_hello_fn client_hello_callback, void *ctx); |
| 1449 | |
| 1450 | /** |
| 1451 | * Sets the callback execution mode. |
| 1452 | * |
| 1453 | * See s2n_client_hello_cb_mode for each mode's behavior. |
| 1454 | * |
| 1455 | * @param config The configuration object being updated |
| 1456 | * @param cb_mode The desired callback mode |
| 1457 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1458 | */ |
| 1459 | S2N_API extern int s2n_config_set_client_hello_cb_mode(struct s2n_config *config, s2n_client_hello_cb_mode cb_mode); |
| 1460 | |
| 1461 | /** |
| 1462 | * Marks the non-blocking callback as complete. Can be invoked from within the callback when |
| 1463 | * operating in non-blocking mode to continue the handshake. |
| 1464 | * |
| 1465 | * @param conn The connection object being updated |
| 1466 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1467 | */ |
| 1468 | S2N_API extern int s2n_client_hello_cb_done(struct s2n_connection *conn); |
| 1469 | |
| 1470 | /** |
| 1471 | * Must be invoked if any of the connection properties were changed on the basis of the server_name |
| 1472 | * extension. This must be invoked before marking the Client Hello callback done. |
| 1473 | * |
| 1474 | * @param conn The connection object being updated |
| 1475 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1476 | */ |
| 1477 | S2N_API extern int s2n_connection_server_name_extension_used(struct s2n_connection *conn); |
| 1478 | |
| 1479 | /** |
| 1480 | * Opaque client hello handle |
| 1481 | */ |
| 1482 | struct s2n_client_hello; |
| 1483 | |
| 1484 | /** |
| 1485 | * Get the Client Hello from a s2n_connection. |
| 1486 | * |
| 1487 | * Earliest point during the handshake when this structure is available for use is in the |
| 1488 | * client_hello_callback (see s2n_config_set_client_hello_cb()). |
| 1489 | * |
| 1490 | * @param conn The connection object containing the client hello |
| 1491 | * @returns A handle to the s2n_client_hello structure holding the client hello message sent by the client during the handshake. NULL is returned if a Client Hello has not yet been received and parsed. |
| 1492 | */ |
| 1493 | S2N_API extern struct s2n_client_hello *s2n_connection_get_client_hello(struct s2n_connection *conn); |
| 1494 | |
| 1495 | /** |
| 1496 | * Creates an s2n_client_hello from bytes representing a ClientHello message. |
| 1497 | * |
| 1498 | * The input bytes should include the message header (message type and length), |
| 1499 | * but not the record header. |
| 1500 | * |
| 1501 | * Unlike s2n_connection_get_client_hello, the s2n_client_hello returned by this |
| 1502 | * method is owned by the application and must be freed with s2n_client_hello_free. |
| 1503 | * |
| 1504 | * This method does not support SSLv2 ClientHellos. |
| 1505 | * |
| 1506 | * @param bytes The raw bytes representing the ClientHello. |
| 1507 | * @param size The size of raw_message. |
| 1508 | * @returns A new s2n_client_hello on success, or NULL on failure. |
| 1509 | */ |
| 1510 | S2N_API extern struct s2n_client_hello *s2n_client_hello_parse_message(const uint8_t *bytes, uint32_t size); |
| 1511 | |
| 1512 | /** |
| 1513 | * Frees an s2n_client_hello structure. |
| 1514 | * |
| 1515 | * This method should be called to free s2n_client_hellos returned by |
| 1516 | * s2n_client_hello_parse_message. It will error if passed an s2n_client_hello |
| 1517 | * returned by s2n_connection_get_client_hello and owned by the connection. |
| 1518 | * |
| 1519 | * @param ch The structure to be freed. |
| 1520 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 1521 | */ |
| 1522 | S2N_API extern int s2n_client_hello_free(struct s2n_client_hello **ch); |
| 1523 | |
| 1524 | /** |
| 1525 | * Function to determine the size of the raw Client Hello buffer. |
| 1526 | * |
| 1527 | * Can be used to determine the necessary size of the `out` buffer for |
| 1528 | * s2n_client_hello_get_raw_message() |
| 1529 | * |
| 1530 | * @param ch The Client Hello handle |
| 1531 | * @returns The size of the ClientHello message received by the server |
| 1532 | */ |
| 1533 | S2N_API extern ssize_t s2n_client_hello_get_raw_message_length(struct s2n_client_hello *ch); |
| 1534 | |
| 1535 | /** |
| 1536 | * Copies `max_length` bytes of the ClientHello message into the `out` buffer. |
| 1537 | * The ClientHello instrumented using this function will have the Random bytes |
| 1538 | * zero-ed out. Use s2n_client_hello_get_random to access the random bytes. |
| 1539 | * |
| 1540 | * Note: SSLv2 ClientHello messages follow a different structure than more modern |
| 1541 | * ClientHello messages. See [RFC5246](https://tools.ietf.org/html/rfc5246#appendix-E.2). |
| 1542 | * In addition, due to how s2n-tls parses SSLv2 ClientHellos, the raw message is |
| 1543 | * missing the first three bytes (the msg_type and version) and instead begins with |
| 1544 | * the cipher_specs. To determine whether a ClientHello is an SSLv2 ClientHello, |
| 1545 | * you will need to use s2n_connection_get_client_hello_version(). To get the |
| 1546 | * protocol version advertised in the SSLv2 ClientHello (which may be higher |
| 1547 | * than SSLv2), you will need to use s2n_connection_get_client_protocol_version(). |
| 1548 | * |
| 1549 | * @param ch The Client Hello handle |
| 1550 | * @param out The destination buffer for the raw Client Hello |
| 1551 | * @param max_length The size of out in bytes |
| 1552 | * @returns The number of copied bytes |
| 1553 | */ |
| 1554 | S2N_API extern ssize_t s2n_client_hello_get_raw_message(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length); |
| 1555 | |
| 1556 | /** |
| 1557 | * Function to determine the size of the Client Hello cipher suites. |
| 1558 | * This can be used to allocate the `out` buffer for s2n_client_hello_get_cipher_suites(). |
| 1559 | * |
| 1560 | * @param ch The Client Hello handle |
| 1561 | * @returns the number of bytes the cipher_suites takes on the ClientHello message received by the server |
| 1562 | */ |
| 1563 | S2N_API extern ssize_t s2n_client_hello_get_cipher_suites_length(struct s2n_client_hello *ch); |
| 1564 | |
| 1565 | /** |
| 1566 | * Copies into the `out` buffer `max_length` bytes of the cipher_suites on the ClientHello. |
| 1567 | * |
| 1568 | * Note: SSLv2 ClientHello cipher suites follow a different structure than modern |
| 1569 | * ClientHello messages. See [RFC5246](https://tools.ietf.org/html/rfc5246#appendix-E.2). |
| 1570 | * To determine whether a ClientHello is an SSLv2 ClientHello, |
| 1571 | * you will need to use s2n_connection_get_client_hello_version(). |
| 1572 | * |
| 1573 | * @param ch The Client Hello handle |
| 1574 | * @param out The destination buffer for the raw Client Hello cipher suites |
| 1575 | * @param max_length The size of out in bytes |
| 1576 | * @returns The number of copied bytes |
| 1577 | */ |
| 1578 | S2N_API extern ssize_t s2n_client_hello_get_cipher_suites(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length); |
| 1579 | |
| 1580 | /** |
| 1581 | * Function to determine the size of the Client Hello extensions. |
| 1582 | * This can be used to allocate the `out` buffer for s2n_client_hello_get_extensions(). |
| 1583 | * |
| 1584 | * @param ch The Client Hello handle |
| 1585 | * @returns the number of bytes the extensions take in the ClientHello message received by the server |
| 1586 | */ |
| 1587 | S2N_API extern ssize_t s2n_client_hello_get_extensions_length(struct s2n_client_hello *ch); |
| 1588 | |
| 1589 | /** |
| 1590 | * Copies into the `out` buffer `max_length` bytes of the extensions in the ClientHello. |
| 1591 | * |
| 1592 | * @param ch The Client Hello handle |
| 1593 | * @param out The destination buffer for the raw Client Hello extensions |
| 1594 | * @param max_length The size of out in bytes |
| 1595 | * @returns The number of copied bytes |
| 1596 | */ |
| 1597 | S2N_API extern ssize_t s2n_client_hello_get_extensions(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length); |
| 1598 | |
| 1599 | /** |
| 1600 | * Query the ClientHello message received by the server. Use this function to allocate the `out` buffer for |
| 1601 | * other client hello extension functions. |
| 1602 | * |
| 1603 | * @param ch A pointer to the Client Hello |
| 1604 | * @param extension_type Indicates the desired extension |
| 1605 | * @returns The number of bytes the given extension type takes |
| 1606 | */ |
| 1607 | S2N_API extern ssize_t s2n_client_hello_get_extension_length(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type); |
| 1608 | |
| 1609 | /** |
| 1610 | * Copies into the `out` buffer `max_length` bytes of a given extension type on the ClientHello |
| 1611 | * |
| 1612 | * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello(). |
| 1613 | * |
| 1614 | * @param ch A pointer to the Client Hello |
| 1615 | * @param extension_type Indicates the desired extension |
| 1616 | * @param out A pointer to the buffer that s2n will write the client session id to. This buffer MUST be the size of `max_length` |
| 1617 | * @param max_length The size of `out`. |
| 1618 | * @returns The number of copied bytes |
| 1619 | */ |
| 1620 | S2N_API extern ssize_t s2n_client_hello_get_extension_by_id(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type, uint8_t *out, uint32_t max_length); |
| 1621 | |
| 1622 | /** |
| 1623 | * Used to check if a particular extension exists in the client hello. |
| 1624 | * |
| 1625 | * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello(). |
| 1626 | * |
| 1627 | * @param ch A pointer to the client hello object |
| 1628 | * @param extension_iana The iana value of the extension |
| 1629 | * @param exists A pointer that will be set to whether or not the extension exists |
| 1630 | */ |
| 1631 | S2N_API extern int s2n_client_hello_has_extension(struct s2n_client_hello *ch, uint16_t extension_iana, bool *exists); |
| 1632 | |
| 1633 | /** |
| 1634 | * Get the the ClientHello session id length in bytes |
| 1635 | * |
| 1636 | * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello(). |
| 1637 | * |
| 1638 | * @param ch A pointer to the Client Hello |
| 1639 | * @param out_length An out pointer. s2n will set it's value to the size of the session_id in bytes. |
| 1640 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1641 | */ |
| 1642 | S2N_API extern int s2n_client_hello_get_session_id_length(struct s2n_client_hello *ch, uint32_t *out_length); |
| 1643 | |
| 1644 | /** |
| 1645 | * Copies up to `max_length` bytes of the ClientHello session_id into the `out` buffer and stores the number of copied bytes in `out_length`. |
| 1646 | * |
| 1647 | * Retrieve the session id as sent by the client in the ClientHello message. The session id on the `s2n_connection` may change later |
| 1648 | * when the server sends the ServerHello; see `s2n_connection_get_session_id` for how to get the final session id used for future session resumption. |
| 1649 | * |
| 1650 | * Use s2n_client_hello_get_session_id_length() to get the the ClientHello session id length in bytes. `ch` is a pointer to the `s2n_client_hello` |
| 1651 | * of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello(). |
| 1652 | * |
| 1653 | * @param ch A pointer to the Client Hello |
| 1654 | * @param out A pointer to the buffer that s2n will write the client session id to. This buffer MUST be the size of `max_length` |
| 1655 | * @param out_length An out pointer. s2n will set it's value to the size of the session_id in bytes. |
| 1656 | * @param max_length The size of `out`. |
| 1657 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1658 | */ |
| 1659 | S2N_API extern int s2n_client_hello_get_session_id(struct s2n_client_hello *ch, uint8_t *out, uint32_t *out_length, uint32_t max_length); |
| 1660 | |
| 1661 | /** |
| 1662 | * Get the length of the compression methods list sent in the Client Hello. |
| 1663 | * |
| 1664 | * @param ch A pointer to the Client Hello |
| 1665 | * @param out_length An out pointer. Will be set to the length of the compression methods list in bytes. |
| 1666 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1667 | */ |
| 1668 | S2N_API extern int s2n_client_hello_get_compression_methods_length(struct s2n_client_hello *ch, uint32_t *out_length); |
| 1669 | |
| 1670 | /** |
| 1671 | * Retrieves the list of compression methods sent in the Client Hello. |
| 1672 | * |
| 1673 | * Use `s2n_client_hello_get_compression_methods_length()` |
| 1674 | * to retrieve how much memory should be allocated for the buffer in advance. |
| 1675 | * |
| 1676 | * @note Compression methods were removed in TLS1.3 and therefore the only valid value in this list is the |
| 1677 | * "null" compression method when TLS1.3 is negotiated. |
| 1678 | * |
| 1679 | * @note s2n-tls has never supported compression methods in any TLS version and therefore a |
| 1680 | * compression method will never be negotiated or used. |
| 1681 | * |
| 1682 | * @param ch A pointer to the Client Hello |
| 1683 | * @param list A pointer to some memory that s2n will write the compression methods to. This memory MUST be the size of `list_length` |
| 1684 | * @param list_length The size of `list`. |
| 1685 | * @param out_length An out pointer. s2n will set its value to the size of the compression methods list in bytes. |
| 1686 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1687 | */ |
| 1688 | S2N_API extern int s2n_client_hello_get_compression_methods(struct s2n_client_hello *ch, uint8_t *list, uint32_t list_length, uint32_t *out_length); |
| 1689 | |
| 1690 | /** |
| 1691 | * Access the Client Hello protocol version |
| 1692 | * |
| 1693 | * @note This field is a legacy field in TLS1.3 and is no longer used to negotiate the |
| 1694 | * protocol version of the connection. It will be set to TLS1.2 even if TLS1.3 is negotiated. |
| 1695 | * Therefore this method should only be used for logging or fingerprinting. |
| 1696 | * |
| 1697 | * @param ch A pointer to the client hello struct |
| 1698 | * @param out The protocol version in the client hello. |
| 1699 | */ |
| 1700 | S2N_API extern int s2n_client_hello_get_legacy_protocol_version(struct s2n_client_hello *ch, uint8_t *out); |
| 1701 | |
| 1702 | /** |
| 1703 | * Retrieves the client random value from the Client Hello. |
| 1704 | * |
| 1705 | * The client random is a 32-byte value sent by the client in the ClientHello message. |
| 1706 | * It is used in the TLS handshake for key derivation and to prevent replay attacks. |
| 1707 | * |
| 1708 | * Copies up to `max_length` bytes of the client random into the `out` buffer. The number |
| 1709 | * of bytes copied will be the minimum of `max_length` and S2N_TLS_RANDOM_DATA_LEN (32 bytes). |
| 1710 | * |
| 1711 | * @param ch A pointer to the client hello struct |
| 1712 | * @param out A pointer to a buffer where the client random will be copied. |
| 1713 | * @param max_length The maximum number of bytes to copy into the `out` buffer. |
| 1714 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 1715 | */ |
| 1716 | S2N_API extern int s2n_client_hello_get_random(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length); |
| 1717 | |
| 1718 | /** |
| 1719 | * Retrieves the supported groups received from the client in the supported groups extension. |
| 1720 | * |
| 1721 | * IANA values for each of the received supported groups are written to the provided `groups` |
| 1722 | * array, and `groups_count` is set to the number of received supported groups. |
| 1723 | * |
| 1724 | * `groups_count_max` should be set to the maximum capacity of the `groups` array. If |
| 1725 | * `groups_count_max` is less than the number of received supported groups, this function will |
| 1726 | * error. To determine how large `groups` should be in advance, use |
| 1727 | * `s2n_client_hello_get_extension_length()` with the S2N_EXTENSION_SUPPORTED_GROUPS extension |
| 1728 | * type, and divide the value by 2. |
| 1729 | * |
| 1730 | * If no supported groups extension was received from the peer, or the received supported groups |
| 1731 | * extension is malformed, this function will error. |
| 1732 | * |
| 1733 | * @param ch A pointer to the ClientHello. Can be retrieved from a connection via |
| 1734 | * `s2n_connection_get_client_hello()`. |
| 1735 | * @param groups The array to populate with the received supported groups. |
| 1736 | * @param groups_count_max The maximum number of supported groups that can fit in the `groups` array. |
| 1737 | * @param groups_count Returns the number of received supported groups. |
| 1738 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 1739 | */ |
| 1740 | S2N_API extern int s2n_client_hello_get_supported_groups(struct s2n_client_hello *ch, uint16_t *groups, |
| 1741 | uint16_t groups_count_max, uint16_t *groups_count); |
| 1742 | |
| 1743 | /** |
| 1744 | * Gets the length of the first server name in a Client Hello. |
| 1745 | * |
| 1746 | * @param ch A pointer to the ClientHello |
| 1747 | * @param length A pointer which will be populated with the length of the server name |
| 1748 | */ |
| 1749 | S2N_API extern int s2n_client_hello_get_server_name_length(struct s2n_client_hello *ch, uint16_t *length); |
| 1750 | |
| 1751 | /** |
| 1752 | * Gets the first server name in a Client Hello. |
| 1753 | * |
| 1754 | * Use `s2n_client_hello_get_server_name_length()` to get the amount of memory needed for the buffer. |
| 1755 | * |
| 1756 | * @param ch A pointer to the ClientHello |
| 1757 | * @param server_name A pointer to the memory which will be populated with the server name |
| 1758 | * @param length The maximum amount of data that can be written to `server_name` |
| 1759 | * @param out_length A pointer which will be populated with the size of the server name |
| 1760 | */ |
| 1761 | S2N_API extern int s2n_client_hello_get_server_name(struct s2n_client_hello *ch, uint8_t *server_name, uint16_t length, uint16_t *out_length); |
| 1762 | |
| 1763 | /** |
| 1764 | * Sets the file descriptor for a s2n connection. |
| 1765 | * |
| 1766 | * @warning If the read end of the pipe is closed unexpectedly, writing to the pipe will raise a SIGPIPE signal. |
| 1767 | * **s2n-tls does NOT handle SIGPIPE.** A SIGPIPE signal will cause the process to terminate unless it is handled |
| 1768 | * or ignored by the application. |
| 1769 | * @note This file-descriptor should be active and connected |
| 1770 | * @param conn A pointer to the s2n connection |
| 1771 | * @param fd The new file descriptor |
| 1772 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1773 | */ |
| 1774 | S2N_API extern int s2n_connection_set_fd(struct s2n_connection *conn, int fd); |
| 1775 | |
| 1776 | /** |
| 1777 | * Sets the file descriptor for the read channel of an s2n connection. |
| 1778 | * |
| 1779 | * @warning If the read end of the pipe is closed unexpectedly, writing to the pipe will raise a SIGPIPE signal. |
| 1780 | * **s2n-tls does NOT handle SIGPIPE.** A SIGPIPE signal will cause the process to terminate unless it is handled |
| 1781 | * or ignored by the application. |
| 1782 | * @note This file-descriptor should be active and connected |
| 1783 | * @param conn A pointer to the s2n connection |
| 1784 | * @param readfd The new read file descriptor |
| 1785 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1786 | */ |
| 1787 | S2N_API extern int s2n_connection_set_read_fd(struct s2n_connection *conn, int readfd); |
| 1788 | |
| 1789 | /** |
| 1790 | * Sets the assigned file descriptor for the write channel of an s2n connection. |
| 1791 | * |
| 1792 | * @note This file-descriptor should be active and connected |
| 1793 | * @param conn A pointer to the s2n connection |
| 1794 | * @param writefd The new write file descriptor |
| 1795 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1796 | */ |
| 1797 | S2N_API extern int s2n_connection_set_write_fd(struct s2n_connection *conn, int writefd); |
| 1798 | |
| 1799 | /** |
| 1800 | * Gets the assigned file descriptor for the read channel of an s2n connection. |
| 1801 | * |
| 1802 | * @param conn A pointer to the s2n connection |
| 1803 | * @param readfd pointer to place the used file descriptor. |
| 1804 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1805 | */ |
| 1806 | S2N_API extern int s2n_connection_get_read_fd(struct s2n_connection *conn, int *readfd); |
| 1807 | |
| 1808 | /** |
| 1809 | * Gets the assigned file descriptor for the write channel of an s2n connection. |
| 1810 | * |
| 1811 | * @param conn A pointer to the s2n connection |
| 1812 | * @param writefd pointer to place the used file descriptor. |
| 1813 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1814 | */ |
| 1815 | S2N_API extern int s2n_connection_get_write_fd(struct s2n_connection *conn, int *writefd); |
| 1816 | |
| 1817 | /** |
| 1818 | * Indicates to s2n that the connection is using corked IO. |
| 1819 | * |
| 1820 | * @warning This API should only be used when using managed send IO. |
| 1821 | * |
| 1822 | * @param conn The connection object being updated |
| 1823 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1824 | */ |
| 1825 | S2N_API extern int s2n_connection_use_corked_io(struct s2n_connection *conn); |
| 1826 | |
| 1827 | /** |
| 1828 | * Function pointer for a user provided recv callback. |
| 1829 | */ |
| 1830 | typedef int s2n_recv_fn(void *io_context, uint8_t *buf, uint32_t len); |
| 1831 | |
| 1832 | /** |
| 1833 | * Function pointer for a user provided send callback. |
| 1834 | */ |
| 1835 | typedef int s2n_send_fn(void *io_context, const uint8_t *buf, uint32_t len); |
| 1836 | |
| 1837 | /** |
| 1838 | * Set a context containing anything needed in the recv callback function (for example, |
| 1839 | * a file descriptor), the buffer holding data to be sent or received, and the length of the buffer. |
| 1840 | * |
| 1841 | * @note The `io_context` passed to the callbacks may be set separately using `s2n_connection_set_recv_ctx` and `s2n_connection_set_send_ctx`. |
| 1842 | * |
| 1843 | * @param conn The connection object being updated |
| 1844 | * @param ctx A user provided context that the callback will be invoked with |
| 1845 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1846 | */ |
| 1847 | S2N_API extern int s2n_connection_set_recv_ctx(struct s2n_connection *conn, void *ctx); |
| 1848 | |
| 1849 | /** |
| 1850 | * Set a context containing anything needed in the send callback function (for example, |
| 1851 | * a file descriptor), the buffer holding data to be sent or received, and the length of the buffer. |
| 1852 | * |
| 1853 | * @note The `io_context` passed to the callbacks may be set separately using `s2n_connection_set_recv_ctx` and `s2n_connection_set_send_ctx`. |
| 1854 | * |
| 1855 | * @param conn The connection object being updated |
| 1856 | * @param ctx A user provided context that the callback will be invoked with |
| 1857 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1858 | */ |
| 1859 | S2N_API extern int s2n_connection_set_send_ctx(struct s2n_connection *conn, void *ctx); |
| 1860 | |
| 1861 | /** |
| 1862 | * Configure a connection to use a recv callback to receive data. |
| 1863 | * |
| 1864 | * @note This callback may be blocking or nonblocking. |
| 1865 | * @note The callback may receive less than the requested length. The function should return the number |
| 1866 | * of bytes received, or set errno and return an error code < 0. |
| 1867 | * |
| 1868 | * @param conn The connection object being updated |
| 1869 | * @param recv A recv callback function pointer |
| 1870 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1871 | */ |
| 1872 | S2N_API extern int s2n_connection_set_recv_cb(struct s2n_connection *conn, s2n_recv_fn recv); |
| 1873 | |
| 1874 | /** |
| 1875 | * Configure a connection to use a send callback to send data. |
| 1876 | * |
| 1877 | * @note This callback may be blocking or nonblocking. |
| 1878 | * @note The callback may send less than the requested length. The function should return the |
| 1879 | * number of bytes sent or set errno and return an error code < 0. |
| 1880 | * |
| 1881 | * @param conn The connection object being updated |
| 1882 | * @param send A send callback function pointer |
| 1883 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1884 | */ |
| 1885 | S2N_API extern int s2n_connection_set_send_cb(struct s2n_connection *conn, s2n_send_fn send); |
| 1886 | |
| 1887 | /** |
| 1888 | * Change the behavior of s2n-tls when sending data to prefer high throughput. |
| 1889 | * |
| 1890 | * Connections preferring throughput will use |
| 1891 | * large record sizes that minimize overhead. |
| 1892 | * |
| 1893 | * @param conn The connection object being updated |
| 1894 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1895 | */ |
| 1896 | S2N_API extern int s2n_connection_prefer_throughput(struct s2n_connection *conn); |
| 1897 | |
| 1898 | /** |
| 1899 | * Change the behavior of s2n-tls when sending data to prefer low latency. |
| 1900 | * |
| 1901 | * Connections preferring low latency will be encrypted |
| 1902 | * using small record sizes that can be decrypted sooner by the recipient. |
| 1903 | * |
| 1904 | * @param conn The connection object being updated |
| 1905 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1906 | */ |
| 1907 | S2N_API extern int s2n_connection_prefer_low_latency(struct s2n_connection *conn); |
| 1908 | |
| 1909 | /** |
| 1910 | * Configure the connection to reduce potentially expensive calls to recv. |
| 1911 | * |
| 1912 | * If this setting is disabled, s2n-tls will call read twice for every TLS record, |
| 1913 | * which can be expensive but ensures that s2n-tls will always attempt to read the |
| 1914 | * exact number of bytes it requires. If this setting is enabled, s2n-tls will |
| 1915 | * instead reduce the number of calls to read by attempting to read as much data |
| 1916 | * as possible in each read call, storing the extra in the existing IO buffers. |
| 1917 | * This may cause it to request more data than will ever actually be available. |
| 1918 | * |
| 1919 | * There is no additional memory cost of enabling this setting. It reuses the |
| 1920 | * existing IO buffers. |
| 1921 | * |
| 1922 | * This setting is disabled by default. Depending on how your application detects |
| 1923 | * data available for reading, buffering reads may break your event loop. |
| 1924 | * In particular, note that: |
| 1925 | * |
| 1926 | * 1. File descriptor reads or calls to your custom s2n_recv_cb may request more |
| 1927 | * data than is available. Reads must return partial data when available rather |
| 1928 | * than blocking until all requested data is available. |
| 1929 | * |
| 1930 | * 2. s2n_negotiate may read and buffer application data records. |
| 1931 | * You must call s2n_recv at least once after negotiation to ensure that you |
| 1932 | * handle any buffered data. |
| 1933 | * |
| 1934 | * 3. s2n_recv may read and buffer more records than it parses and decrypts. |
| 1935 | * You must call s2n_recv until it reports S2N_ERR_T_BLOCKED, rather than just |
| 1936 | * until it reports S2N_SUCCESS. |
| 1937 | * |
| 1938 | * 4. s2n_peek reports available decrypted data. It does not report any data |
| 1939 | * buffered by this feature. However, s2n_peek_buffered does report data |
| 1940 | * buffered by this feature. |
| 1941 | * |
| 1942 | * 5. s2n_connection_release_buffers will not release the input buffer if it |
| 1943 | * contains buffered data. |
| 1944 | * |
| 1945 | * For example: if your event loop uses `poll`, you will receive a POLLIN event |
| 1946 | * for your read file descriptor when new data is available. When you call s2n_recv |
| 1947 | * to read that data, s2n-tls reads one or more TLS records from the file descriptor. |
| 1948 | * If you stop calling s2n_recv before it reports S2N_ERR_T_BLOCKED, some of those |
| 1949 | * records may remain in s2n-tls's read buffer. If you read part of a record, |
| 1950 | * s2n_peek will report the remainder of that record as available. But if you don't |
| 1951 | * read any of a record, it remains encrypted and is not reported by s2n_peek, but |
| 1952 | * is still reported by s2n_peek_buffered. And because the data is buffered in s2n-tls |
| 1953 | * instead of in the file descriptor, another call to `poll` will NOT report any |
| 1954 | * more data available. Your application may hang waiting for more data. |
| 1955 | * |
| 1956 | * @warning This feature cannot be enabled for a connection that will enable kTLS for receiving. |
| 1957 | * |
| 1958 | * @warning This feature may work with blocking IO, if used carefully. Your blocking |
| 1959 | * IO must support partial reads (so MSG_WAITALL cannot be used). You will either |
| 1960 | * need to know exactly how much data your peer is sending, or will need to use |
| 1961 | * `s2n_peek` and `s2n_peek_buffered` rather than relying on S2N_ERR_T_BLOCKED |
| 1962 | * as noted in #3 above. |
| 1963 | * |
| 1964 | * @param conn The connection object being updated |
| 1965 | * @param enabled Set to `true` to enable, `false` to disable. |
| 1966 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 1967 | */ |
| 1968 | S2N_API extern int s2n_connection_set_recv_buffering(struct s2n_connection *conn, bool enabled); |
| 1969 | |
| 1970 | /** |
| 1971 | * Reports how many bytes of unprocessed TLS records are buffered after the current |
| 1972 | * record. |
| 1973 | * |
| 1974 | * This is only useful when `s2n_connection_set_recv_buffering` is enabled, and |
| 1975 | * will return 0 otherwise. |
| 1976 | * |
| 1977 | * `s2n_peek_buffered` is not a replacement for `s2n_peek`. While `s2n_peek` reports |
| 1978 | * application data that is ready for the application to read with no additional |
| 1979 | * processing, `s2n_peek_buffered` reports raw TLS records that still need to be |
| 1980 | * parsed and likely decrypted. Those records may contain application data, but |
| 1981 | * they may also only contain TLS control messages. |
| 1982 | * |
| 1983 | * When receive buffering is enabled, it is useful to imagine that an s2n-tls |
| 1984 | * connection behaves as if it has two buffers, one for the "current record", |
| 1985 | * and one for "additional data". |
| 1986 | * ```text |
| 1987 | * c -> ciphertext |
| 1988 | * p -> plaintext |
| 1989 | * |
| 1990 | * |-----current record-----|-------additional------------| |
| 1991 | * case 1 - |ccccccccccccc |
| 1992 | * case 2 - |pppppppppppppppppppppppp| |
| 1993 | * case 3 - |pppppppppppppppppppppppp|ccccccccccccccccccc |
| 1994 | * ``` |
| 1995 | * In case 1, we received a record fragment. Records can only be decrypted |
| 1996 | * once the full record is available. So "current record" just stores the record |
| 1997 | * fragment (ciphertext). There are currently no APIs to determine if there |
| 1998 | * is a record fragment stored. https://github.com/aws/s2n-tls/issues/5863. `s2n_peek_buffered` |
| 1999 | * and `s2n_peek` will both return 0 in this case. |
| 2000 | * |
| 2001 | * In case 2, we received a full record, which is decrypted and stored in current |
| 2002 | * record. `s2n_peek` will return the count of plaintext bytes |
| 2003 | * yet to be read from "current record", and `s2n_peek_buffered` will return 0 |
| 2004 | * |
| 2005 | * In case 3, additional bytes (another record, complete or incomplete) were |
| 2006 | * also read off the wire. In this case `s2n_peek_buffered` will return the length |
| 2007 | * of ciphertext bytes buffered in "additional". This is the only case in which |
| 2008 | * `s2n_peek_buffered` will return a non-zero result. |
| 2009 | * |
| 2010 | * @param conn A pointer to the s2n_connection object |
| 2011 | * @returns The number of buffered encrypted bytes |
| 2012 | */ |
| 2013 | S2N_API extern uint32_t s2n_peek_buffered(struct s2n_connection *conn); |
| 2014 | |
| 2015 | /** |
| 2016 | * Configure the connection to free IO buffers when they are not currently in use. |
| 2017 | * |
| 2018 | * This configuration can be used to minimize connection memory footprint size, at the cost |
| 2019 | * of more calls to alloc and free. Some of these costs can be mitigated by configuring s2n-tls |
| 2020 | * to use an allocator that includes thread-local caches or lock-free allocation patterns. |
| 2021 | * |
| 2022 | * @param conn The connection object being update |
| 2023 | * @param enabled Set to `true` if dynamic buffers are enabled; `false` if disabled |
| 2024 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2025 | */ |
| 2026 | S2N_API extern int s2n_connection_set_dynamic_buffers(struct s2n_connection *conn, bool enabled); |
| 2027 | |
| 2028 | /** |
| 2029 | * Changes the behavior of s2n-tls when sending data to initially prefer records |
| 2030 | * small enough to fit in single ethernet frames. |
| 2031 | * |
| 2032 | * When dynamic record sizing is active, the connection sends records small enough |
| 2033 | * to fit in a single standard 1500 byte ethernet frame. Otherwise, the connection |
| 2034 | * chooses record sizes according to the configured maximum fragment length. |
| 2035 | * |
| 2036 | * Dynamic record sizing is active for the first resize_threshold bytes of a connection, |
| 2037 | * and is reactivated whenever timeout_threshold seconds pass without sending data. |
| 2038 | * |
| 2039 | * @param conn The connection object being updated |
| 2040 | * @param resize_threshold The number of bytes to send before changing the record size. Maximum 8MiB. |
| 2041 | * @param timeout_threshold Reset record size back to a single segment after threshold seconds of inactivity |
| 2042 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2043 | */ |
| 2044 | S2N_API extern int s2n_connection_set_dynamic_record_threshold(struct s2n_connection *conn, uint32_t resize_threshold, uint16_t timeout_threshold); |
| 2045 | |
| 2046 | /** |
| 2047 | * Sets the callback to use for verifying that a hostname from an X.509 certificate is trusted. |
| 2048 | * |
| 2049 | * The default behavior is to require that the hostname match the server name set with s2n_set_server_name(). This will |
| 2050 | * likely lead to all client certificates being rejected, so the callback will need to be overridden when using client authentication. |
| 2051 | * |
| 2052 | * If a single callback for different connections using the same config is desired, see s2n_config_set_verify_host_callback(). |
| 2053 | * |
| 2054 | * @param conn A pointer to a s2n_connection object |
| 2055 | * @param host_fn A pointer to a callback function that s2n will invoke in order to verify the hostname of an X.509 certificate |
| 2056 | * @param data Opaque pointer to data that the verify host function will be invoked with |
| 2057 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2058 | */ |
| 2059 | S2N_API extern int s2n_connection_set_verify_host_callback(struct s2n_connection *conn, s2n_verify_host_fn host_fn, void *data); |
| 2060 | |
| 2061 | /** |
| 2062 | * Used to opt-out of s2n-tls's built-in blinding. Blinding is a |
| 2063 | * mitigation against timing side-channels which in some cases can leak information |
| 2064 | * about encrypted data. By default s2n-tls will cause a thread to sleep between 10 and |
| 2065 | * 30 seconds whenever tampering is detected. |
| 2066 | * |
| 2067 | * Setting the S2N_SELF_SERVICE_BLINDING option with s2n_connection_set_blinding() |
| 2068 | * turns off this behavior. This is useful for applications that are handling many connections |
| 2069 | * in a single thread. In that case, if s2n_recv() or s2n_negotiate() return an error, |
| 2070 | * self-service applications should call s2n_connection_get_delay() and pause |
| 2071 | * activity on the connection for the specified number of nanoseconds before calling |
| 2072 | * close() or shutdown(). |
| 2073 | */ |
| 2074 | typedef enum { |
| 2075 | S2N_BUILT_IN_BLINDING, |
| 2076 | S2N_SELF_SERVICE_BLINDING |
| 2077 | } s2n_blinding; |
| 2078 | |
| 2079 | /** |
| 2080 | * Used to configure s2n-tls to either use built-in blinding (set blinding to S2N_BUILT_IN_BLINDING) or |
| 2081 | * self-service blinding (set blinding to S2N_SELF_SERVICE_BLINDING). |
| 2082 | * |
| 2083 | * @param conn The connection object being updated |
| 2084 | * @param blinding The desired blinding mode for the connection |
| 2085 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2086 | */ |
| 2087 | S2N_API extern int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blinding); |
| 2088 | |
| 2089 | /** |
| 2090 | * Query the connection object for the configured blinding delay. |
| 2091 | * @param conn The connection object being updated |
| 2092 | * @returns the number of nanoseconds an application using self-service blinding should pause before calling close() or shutdown(). |
| 2093 | */ |
| 2094 | S2N_API extern uint64_t s2n_connection_get_delay(struct s2n_connection *conn); |
| 2095 | |
| 2096 | /** |
| 2097 | * Configures the maximum blinding delay enforced after errors. |
| 2098 | * |
| 2099 | * Blinding protects your application from timing side channel attacks like Lucky13. While s2n-tls |
| 2100 | * implements other, more specific mitigations for known timing side channels, blinding is important |
| 2101 | * as a defense against currently unknown or unreported timing attacks. |
| 2102 | * |
| 2103 | * Setting a maximum delay lower than the recommended default (30s) will make timing attacks against |
| 2104 | * your application easier. The lower you set the delay, the fewer requests and less total time an |
| 2105 | * attacker will require to execute an attack. If you must lower the delay for reasons such as client |
| 2106 | * timeouts, then you should choose the highest value practically possible to limit your risk. |
| 2107 | * |
| 2108 | * If you lower the blinding delay, you should also consider implementing monitoring and filtering |
| 2109 | * to detect and reject suspicious traffic that could be gathering timing information from a potential |
| 2110 | * side channel. Timing attacks usually involve repeatedly triggering TLS errors. |
| 2111 | * |
| 2112 | * @warning Do NOT set a lower blinding delay unless you understand the risks and have other |
| 2113 | * mitigations for timing side channels in place. |
| 2114 | * |
| 2115 | * @note This delay needs to be set lower than any timeouts, such as your TCP socket timeout. |
| 2116 | * |
| 2117 | * @param config The config object being updated. |
| 2118 | * @param seconds The maximum number of seconds that s2n-tls will delay for in the event of a |
| 2119 | * sensitive error. |
| 2120 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 2121 | */ |
| 2122 | S2N_API extern int s2n_config_set_max_blinding_delay(struct s2n_config *config, uint32_t seconds); |
| 2123 | |
| 2124 | /** |
| 2125 | * Sets the cipher preference override for the s2n_connection. Calling this function is not necessary |
| 2126 | * unless you want to set the cipher preferences on the connection to something different than what is in the s2n_config. |
| 2127 | * |
| 2128 | * @param conn The connection object being updated |
| 2129 | * @param version The human readable string representation of the security policy version. |
| 2130 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2131 | */ |
| 2132 | S2N_API extern int s2n_connection_set_cipher_preferences(struct s2n_connection *conn, const char *version); |
| 2133 | |
| 2134 | /** |
| 2135 | * Used to indicate the type of key update that is being requested. For further |
| 2136 | * information refer to `s2n_connection_request_key_update`. |
| 2137 | */ |
| 2138 | typedef enum { |
| 2139 | S2N_KEY_UPDATE_NOT_REQUESTED = 0, |
| 2140 | S2N_KEY_UPDATE_REQUESTED |
| 2141 | } s2n_peer_key_update; |
| 2142 | |
| 2143 | /** |
| 2144 | * Signals the connection to do a key_update at the next possible opportunity. Note that the resulting key update message |
| 2145 | * will not be sent until `s2n_send` is called. |
| 2146 | * |
| 2147 | * @param conn The connection object to trigger the key update on. |
| 2148 | * @param peer_request Indicates if a key update should also be requested |
| 2149 | * of the peer. When set to `S2N_KEY_UPDATE_NOT_REQUESTED`, then only the sending |
| 2150 | * key of `conn` will be updated. If set to `S2N_KEY_UPDATE_REQUESTED`, then |
| 2151 | * the sending key of conn will be updated AND the peer will be requested to |
| 2152 | * update their sending key. Note that s2n-tls currently only supports |
| 2153 | * `peer_request` being set to `S2N_KEY_UPDATE_NOT_REQUESTED` and will return |
| 2154 | * S2N_FAILURE if any other value is used. |
| 2155 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2156 | */ |
| 2157 | S2N_API extern int s2n_connection_request_key_update(struct s2n_connection *conn, s2n_peer_key_update peer_request); |
| 2158 | /** |
| 2159 | * Appends the provided application protocol to the preference list |
| 2160 | * |
| 2161 | * The data provided in `protocol` parameter will be copied into an internal buffer |
| 2162 | * |
| 2163 | * @param conn The connection object being updated |
| 2164 | * @param protocol A pointer to a slice of bytes |
| 2165 | * @param protocol_len The length of bytes that should be read from `protocol`. Note: this value cannot be 0, otherwise an error will be returned. |
| 2166 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2167 | */ |
| 2168 | S2N_API extern int s2n_connection_append_protocol_preference(struct s2n_connection *conn, const uint8_t *protocol, uint8_t protocol_len); |
| 2169 | |
| 2170 | /** |
| 2171 | * Sets the protocol preference override for the s2n_connection. Calling this function is not necessary unless you want |
| 2172 | * to set the protocol preferences on the connection to something different than what is in the s2n_config. |
| 2173 | * |
| 2174 | * @param conn The connection object being updated |
| 2175 | * @param protocols A pointer to an array of protocol strings |
| 2176 | * @param protocol_count The number of protocols contained in protocols |
| 2177 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2178 | */ |
| 2179 | S2N_API extern int s2n_connection_set_protocol_preferences(struct s2n_connection *conn, const char *const *protocols, int protocol_count); |
| 2180 | |
| 2181 | /** |
| 2182 | * Sets the server name for the connection. |
| 2183 | * |
| 2184 | * The provided server name will be sent by the client to the server in the |
| 2185 | * server_name ClientHello extension. It may be desirable for clients |
| 2186 | * to provide this information to facilitate secure connections to |
| 2187 | * servers that host multiple 'virtual' servers at a single underlying |
| 2188 | * network address. |
| 2189 | * |
| 2190 | * s2n-tls does not place any restrictions on the provided server name. However, |
| 2191 | * other TLS implementations might. Specifically, the TLS specification for the |
| 2192 | * server_name extension requires that it be an ASCII-encoded DNS name without a |
| 2193 | * trailing dot, and explicitly forbids literal IPv4 or IPv6 addresses. |
| 2194 | * |
| 2195 | * @param conn The connection object being queried |
| 2196 | * @param server_name A pointer to a string containing the desired server name |
| 2197 | * @warning `server_name` must be a NULL terminated string. |
| 2198 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2199 | */ |
| 2200 | S2N_API extern int s2n_set_server_name(struct s2n_connection *conn, const char *server_name); |
| 2201 | |
| 2202 | /** |
| 2203 | * Query the connection for the selected server name. |
| 2204 | * |
| 2205 | * This can be used by a server to determine which server name the client is using. This function returns the first ServerName entry |
| 2206 | * in the ServerNameList sent by the client. Subsequent entries are not returned. |
| 2207 | * |
| 2208 | * @param conn The connection object being queried |
| 2209 | * @returns The server name associated with a connection, or NULL if none is found. |
| 2210 | */ |
| 2211 | S2N_API extern const char *s2n_get_server_name(struct s2n_connection *conn); |
| 2212 | |
| 2213 | /** |
| 2214 | * Query the connection for the selected application protocol. |
| 2215 | * |
| 2216 | * @param conn The connection object being queried |
| 2217 | * @returns The negotiated application protocol for a `s2n_connection`. In the event of no protocol being negotiated, NULL is returned. |
| 2218 | */ |
| 2219 | S2N_API extern const char *s2n_get_application_protocol(struct s2n_connection *conn); |
| 2220 | |
| 2221 | /** |
| 2222 | * Query the connection for a buffer containing the OCSP response. |
| 2223 | * |
| 2224 | * @param conn The connection object being queried |
| 2225 | * @param length A pointer that is set to the certificate transparency response buffer's size |
| 2226 | * @returns A pointer to the OCSP response sent by a server during the handshake. If no status response is received, NULL is returned. |
| 2227 | */ |
| 2228 | S2N_API extern const uint8_t *s2n_connection_get_ocsp_response(struct s2n_connection *conn, uint32_t *length); |
| 2229 | |
| 2230 | /** |
| 2231 | * Query the connection for a buffer containing the Certificate Transparency response. |
| 2232 | * |
| 2233 | * @param conn The connection object being queried |
| 2234 | * @param length A pointer that is set to the certificate transparency response buffer's size |
| 2235 | * @returns A pointer to the certificate transparency response buffer. |
| 2236 | */ |
| 2237 | S2N_API extern const uint8_t *s2n_connection_get_sct_list(struct s2n_connection *conn, uint32_t *length); |
| 2238 | |
| 2239 | /** |
| 2240 | * Used in non-blocking mode to indicate in which direction s2n-tls became blocked on I/O before it |
| 2241 | * returned control to the caller. This allows an application to avoid retrying s2n-tls operations |
| 2242 | * until I/O is possible in that direction. |
| 2243 | */ |
| 2244 | typedef enum { |
| 2245 | S2N_NOT_BLOCKED = 0, |
| 2246 | S2N_BLOCKED_ON_READ, |
| 2247 | S2N_BLOCKED_ON_WRITE, |
| 2248 | S2N_BLOCKED_ON_APPLICATION_INPUT, |
| 2249 | S2N_BLOCKED_ON_EARLY_DATA, |
| 2250 | } s2n_blocked_status; |
| 2251 | |
| 2252 | /** |
| 2253 | * Performs the initial "handshake" phase of a TLS connection and must be called before any s2n_recv() or s2n_send() calls. |
| 2254 | * |
| 2255 | * @note When using client authentication with TLS1.3, s2n_negotiate() will report a successful |
| 2256 | * handshake to clients before the server validates the client certificate. If the server then |
| 2257 | * rejects the client certificate, the client may later receive an alert while calling s2n_recv, |
| 2258 | * potentially after already having sent application data with s2n_send. |
| 2259 | * |
| 2260 | * See the following example for guidance on calling `s2n_negotiate()`: |
| 2261 | * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_negotiate.c |
| 2262 | * |
| 2263 | * @param conn A pointer to the s2n_connection object |
| 2264 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2265 | * @returns S2N_SUCCESS if the handshake completed. S2N_FAILURE if the handshake encountered an error or is blocked. |
| 2266 | */ |
| 2267 | S2N_API extern int s2n_negotiate(struct s2n_connection *conn, s2n_blocked_status *blocked); |
| 2268 | |
| 2269 | /** |
| 2270 | * Writes and encrypts `size` of `buf` data to the associated connection. s2n_send() will return the number of bytes |
| 2271 | * written, and may indicate a partial write. |
| 2272 | * |
| 2273 | * @note Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active. |
| 2274 | * @note Unlike OpenSSL, repeated calls to s2n_send() should not duplicate the original parameters, but should |
| 2275 | * update `buf` and `size` per the indication of size written. |
| 2276 | * |
| 2277 | * See the following example for guidance on calling `s2n_send()`: |
| 2278 | * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_send.c |
| 2279 | * |
| 2280 | * @param conn A pointer to the s2n_connection object |
| 2281 | * @param buf A pointer to a buffer that s2n will write data from |
| 2282 | * @param size The size of buf |
| 2283 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2284 | * @returns The number of bytes written on success, which may indicate a partial write. S2N_FAILURE on failure. |
| 2285 | */ |
| 2286 | S2N_API extern ssize_t s2n_send(struct s2n_connection *conn, const void *buf, ssize_t size, s2n_blocked_status *blocked); |
| 2287 | |
| 2288 | #ifndef _WIN32 |
| 2289 | /** |
| 2290 | * Works in the same way as s2n_sendv_with_offset() but with the `offs` parameter implicitly assumed to be 0. |
| 2291 | * Therefore in the partial write case, the caller would have to make sure that the `bufs` and `count` fields are modified in a way that takes |
| 2292 | * the partial writes into account. |
| 2293 | * |
| 2294 | * @param conn A pointer to the s2n_connection object |
| 2295 | * @param bufs A pointer to a vector of buffers that s2n will write data from. |
| 2296 | * @param count The number of buffers in `bufs` |
| 2297 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2298 | * @returns The number of bytes written on success, which may indicate a partial write. S2N_FAILURE on failure. |
| 2299 | */ |
| 2300 | S2N_API extern ssize_t s2n_sendv(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count, s2n_blocked_status *blocked); |
| 2301 | |
| 2302 | /** |
| 2303 | * Works in the same way as s2n_send() except that it accepts vectorized buffers. Will return the number of bytes written, and may indicate a partial write. Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active. |
| 2304 | * |
| 2305 | * @note Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active. |
| 2306 | * |
| 2307 | * @note Unlike OpenSSL, repeated calls to s2n_sendv_with_offset() should not duplicate the original parameters, but should update `bufs` and `count` per the indication of size written. |
| 2308 | * |
| 2309 | * See the following example for guidance on calling `s2n_sendv_with_offset()`: |
| 2310 | * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_send.c |
| 2311 | * |
| 2312 | * @param conn A pointer to the s2n_connection object |
| 2313 | * @param bufs A pointer to a vector of buffers that s2n will write data from. |
| 2314 | * @param count The number of buffers in `bufs` |
| 2315 | * @param offs The write cursor offset. This is the byte offset, not a buffer offset. |
| 2316 | * This should be updated as data is written. See the example code. |
| 2317 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2318 | * @returns The number of bytes written on success, which may indicate a partial write. S2N_FAILURE on failure. |
| 2319 | */ |
| 2320 | S2N_API extern ssize_t s2n_sendv_with_offset(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count, ssize_t offs, s2n_blocked_status *blocked); |
| 2321 | #endif |
| 2322 | |
| 2323 | /** |
| 2324 | * Decrypts and reads **size* to `buf` data from the associated |
| 2325 | * connection. |
| 2326 | * |
| 2327 | * @note Unlike OpenSSL, repeated calls to `s2n_recv` should not duplicate the original parameters, but should update `buf` and `size` per the indication of size read. |
| 2328 | * |
| 2329 | * See the following example for guidance on calling `s2n_recv()`: |
| 2330 | * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_recv.c |
| 2331 | * |
| 2332 | * @param conn A pointer to the s2n_connection object |
| 2333 | * @param buf A pointer to a buffer that s2n will place read data into. |
| 2334 | * @param size Size of `buf` |
| 2335 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2336 | * @returns The number of bytes read on success. 0 if the connection was shutdown by the peer. S2N_FAILURE on failure. |
| 2337 | */ |
| 2338 | S2N_API extern ssize_t s2n_recv(struct s2n_connection *conn, void *buf, ssize_t size, s2n_blocked_status *blocked); |
| 2339 | |
| 2340 | /** |
| 2341 | * Allows users of s2n-tls to peek inside the data buffer of an s2n-tls connection to see if there more data to be read without actually reading it. |
| 2342 | * |
| 2343 | * This is useful when using select() on the underlying s2n-tls file descriptor with a message based application layer protocol. As a single call |
| 2344 | * to s2n_recv may read all data off the underlying file descriptor, select() will be unable to tell you there if there is more application data |
| 2345 | * ready for processing already loaded into the s2n-tls buffer. |
| 2346 | * |
| 2347 | * @note can then be used to determine if s2n_recv() needs to be called before more data comes in on the raw fd |
| 2348 | * @param conn A pointer to the s2n_connection object |
| 2349 | * @returns The number of bytes that can be read from the connection |
| 2350 | */ |
| 2351 | S2N_API extern uint32_t s2n_peek(struct s2n_connection *conn); |
| 2352 | |
| 2353 | /** |
| 2354 | * Wipes and releases buffers and memory allocated during the TLS handshake. |
| 2355 | * |
| 2356 | * @note This function should be called after the handshake is successfully negotiated and logging or recording of handshake data is complete. |
| 2357 | * |
| 2358 | * @param conn A pointer to the s2n_connection object |
| 2359 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2360 | */ |
| 2361 | S2N_API extern int s2n_connection_free_handshake(struct s2n_connection *conn); |
| 2362 | |
| 2363 | /** |
| 2364 | * Wipes and free the `in` and `out` buffers associated with a connection. |
| 2365 | * |
| 2366 | * @note This function may be called when a connection is |
| 2367 | * in keep-alive or idle state to reduce memory overhead of long lived connections. |
| 2368 | * |
| 2369 | * @param conn A pointer to the s2n_connection object |
| 2370 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2371 | */ |
| 2372 | S2N_API extern int s2n_connection_release_buffers(struct s2n_connection *conn); |
| 2373 | |
| 2374 | /** |
| 2375 | * Wipes an existing connection and allows it to be reused. Erases all data associated with a connection including |
| 2376 | * pending reads. |
| 2377 | * |
| 2378 | * @note This function should be called after all I/O is completed and s2n_shutdown has been called. |
| 2379 | * @note Reusing the same connection handle(s) is more performant than repeatedly calling s2n_connection_new() and s2n_connection_free(). |
| 2380 | * |
| 2381 | * @param conn A pointer to the s2n_connection object |
| 2382 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2383 | */ |
| 2384 | S2N_API extern int s2n_connection_wipe(struct s2n_connection *conn); |
| 2385 | |
| 2386 | /** |
| 2387 | * Frees the memory associated with an s2n_connection |
| 2388 | * handle. The handle is considered invalid after `s2n_connection_free` is used. |
| 2389 | * s2n_connection_wipe() does not need to be called prior to this function. `s2n_connection_free` performs its own wipe |
| 2390 | * of sensitive data. |
| 2391 | * |
| 2392 | * @param conn A pointer to the s2n_connection object |
| 2393 | * @returns 0 on success. -1 on failure |
| 2394 | */ |
| 2395 | S2N_API extern int s2n_connection_free(struct s2n_connection *conn); |
| 2396 | |
| 2397 | /** |
| 2398 | * Attempts a closure at the TLS layer. Does not close the underlying transport. This call may block in either direction. |
| 2399 | * |
| 2400 | * Unlike other TLS implementations, `s2n_shutdown` attempts a graceful shutdown by default. It will not return with success unless a close_notify alert is successfully |
| 2401 | * sent and received. As a result, `s2n_shutdown` may fail when interacting with a non-conformant TLS implementation. |
| 2402 | * |
| 2403 | * Once `s2n_shutdown` is complete: |
| 2404 | * * The s2n_connection handle cannot be used for reading for writing. |
| 2405 | * * The underlying transport can be closed. Most likely via `shutdown()` or `close()`. |
| 2406 | * * The s2n_connection handle can be freed via s2n_connection_free() or reused via s2n_connection_wipe() |
| 2407 | * |
| 2408 | * @param conn A pointer to the s2n_connection object |
| 2409 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2410 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2411 | */ |
| 2412 | S2N_API extern int s2n_shutdown(struct s2n_connection *conn, s2n_blocked_status *blocked); |
| 2413 | |
| 2414 | /** |
| 2415 | * Attempts to close the write side of the TLS connection. |
| 2416 | * |
| 2417 | * TLS1.3 supports closing the write side of a TLS connection while leaving the read |
| 2418 | * side unaffected. This feature is usually referred to as "half-close". We send |
| 2419 | * a close_notify alert, but do not wait for the peer to respond. |
| 2420 | * |
| 2421 | * Like `s2n_shutdown()`, this method does not affect the underlying transport. |
| 2422 | * |
| 2423 | * `s2n_shutdown_send()` may still be called for earlier TLS versions, but most |
| 2424 | * TLS implementations will react by immediately discarding any pending writes and |
| 2425 | * closing the connection. |
| 2426 | * |
| 2427 | * Once `s2n_shutdown_send()` is complete: |
| 2428 | * * The s2n_connection handle CANNOT be used for writing. |
| 2429 | * * The s2n_connection handle CAN be used for reading. |
| 2430 | * * The write side of the underlying transport can be closed. Most likely via `shutdown()`. |
| 2431 | * |
| 2432 | * The application should still call `s2n_shutdown()` or wait for `s2n_recv()` to |
| 2433 | * return 0 to indicate end-of-data before cleaning up the connection or closing |
| 2434 | * the read side of the underlying transport. |
| 2435 | * |
| 2436 | * @param conn A pointer to the s2n_connection object |
| 2437 | * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 2438 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2439 | */ |
| 2440 | S2N_API extern int s2n_shutdown_send(struct s2n_connection *conn, s2n_blocked_status *blocked); |
| 2441 | |
| 2442 | /** |
| 2443 | * Used to declare what type of client certificate authentication to use. |
| 2444 | * |
| 2445 | * A s2n_connection will enforce client certificate authentication (mTLS) differently based on |
| 2446 | * the `s2n_cert_auth_type` and `s2n_mode` (client/server) of the connection, as described below. |
| 2447 | * |
| 2448 | * Server behavior: |
| 2449 | * - None (default): Will not request client authentication. |
| 2450 | * - Optional: Request the client's certificate and validate it. If no certificate is received then |
| 2451 | * no validation is performed. |
| 2452 | * - Required: Request the client's certificate and validate it. Abort the handshake if a client |
| 2453 | * certificate is not received. |
| 2454 | * |
| 2455 | * Client behavior: |
| 2456 | * - None: Abort the handshake if the server requests client authentication. |
| 2457 | * - Optional (default): Sends the client certificate if the server requests client |
| 2458 | * authentication. No certificate is sent if the application hasn't provided a certificate. |
| 2459 | * - Required: Send the client certificate. Abort the handshake if the server doesn't request |
| 2460 | * client authentication or if the application hasn't provided a certificate. |
| 2461 | */ |
| 2462 | typedef enum { |
| 2463 | S2N_CERT_AUTH_NONE, |
| 2464 | S2N_CERT_AUTH_REQUIRED, |
| 2465 | S2N_CERT_AUTH_OPTIONAL |
| 2466 | } s2n_cert_auth_type; |
| 2467 | |
| 2468 | /** |
| 2469 | * Gets Client Certificate authentication method the s2n_config object is using. |
| 2470 | * |
| 2471 | * @param config A pointer to a s2n_config object |
| 2472 | * @param client_auth_type A pointer to a client auth policy. This will be updated to the s2n_config value. |
| 2473 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2474 | */ |
| 2475 | S2N_API extern int s2n_config_get_client_auth_type(struct s2n_config *config, s2n_cert_auth_type *client_auth_type); |
| 2476 | |
| 2477 | /** |
| 2478 | * Sets whether or not a Client Certificate should be required to complete the TLS Connection. |
| 2479 | * |
| 2480 | * If this is set to `S2N_CERT_AUTH_OPTIONAL` the server will request a client certificate but allow the client to not provide one. |
| 2481 | * Rejecting a client certificate when using `S2N_CERT_AUTH_OPTIONAL` will terminate the handshake. |
| 2482 | * |
| 2483 | * @param config A pointer to a s2n_config object |
| 2484 | * @param client_auth_type The client auth policy for the connection |
| 2485 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2486 | */ |
| 2487 | S2N_API extern int s2n_config_set_client_auth_type(struct s2n_config *config, s2n_cert_auth_type client_auth_type); |
| 2488 | |
| 2489 | /** |
| 2490 | * Gets Client Certificate authentication method the s2n_connection object is using. |
| 2491 | * |
| 2492 | * @param conn A pointer to the s2n_connection object |
| 2493 | * @param client_auth_type A pointer to a client auth policy. This will be updated to the s2n_connection value. |
| 2494 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2495 | */ |
| 2496 | S2N_API extern int s2n_connection_get_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type *client_auth_type); |
| 2497 | |
| 2498 | /** |
| 2499 | * Sets whether or not a Client Certificate should be required to complete the TLS Connection. |
| 2500 | * |
| 2501 | * If this is set to `S2N_CERT_AUTH_OPTIONAL` the server will request a client certificate but allow the client to not provide one. |
| 2502 | * Rejecting a client certificate when using `S2N_CERT_AUTH_OPTIONAL` will terminate the handshake. |
| 2503 | * |
| 2504 | * @param conn A pointer to the s2n_connection object |
| 2505 | * @param client_auth_type The client auth policy for the connection |
| 2506 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2507 | */ |
| 2508 | S2N_API extern int s2n_connection_set_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type client_auth_type); |
| 2509 | |
| 2510 | /** |
| 2511 | * Gets the raw certificate chain received from the client. |
| 2512 | * |
| 2513 | * The retrieved certificate chain has the format described by the TLS 1.2 RFC: |
| 2514 | * https://datatracker.ietf.org/doc/html/rfc5246#section-7.4.2. Each certificate is a DER-encoded ASN.1 X.509, |
| 2515 | * prepended by a 3 byte network-endian length value. Note that this format is used regardless of the connection's |
| 2516 | * protocol version. |
| 2517 | * |
| 2518 | * @warning The buffer pointed to by `cert_chain_out` shares its lifetime with the s2n_connection object. |
| 2519 | * |
| 2520 | * @param conn A pointer to the s2n_connection object |
| 2521 | * @param der_cert_chain_out A pointer that's set to the client certificate chain. |
| 2522 | * @param cert_chain_len A pointer that's set to the size of the `der_cert_chain_out` buffer. |
| 2523 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2524 | */ |
| 2525 | S2N_API extern int s2n_connection_get_client_cert_chain(struct s2n_connection *conn, uint8_t **der_cert_chain_out, uint32_t *cert_chain_len); |
| 2526 | |
| 2527 | /** |
| 2528 | * Sets the initial number of session tickets to send after a >=TLS1.3 handshake. The default value is one ticket. |
| 2529 | * |
| 2530 | * @param config A pointer to the config object. |
| 2531 | * @param num The number of session tickets that will be sent. |
| 2532 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2533 | */ |
| 2534 | S2N_API extern int s2n_config_set_initial_ticket_count(struct s2n_config *config, uint8_t num); |
| 2535 | |
| 2536 | /** |
| 2537 | * Increases the number of session tickets to send after a >=TLS1.3 handshake. |
| 2538 | * |
| 2539 | * @param conn A pointer to the connection object. |
| 2540 | * @param num The number of additional session tickets to send. |
| 2541 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2542 | */ |
| 2543 | S2N_API extern int s2n_connection_add_new_tickets_to_send(struct s2n_connection *conn, uint8_t num); |
| 2544 | |
| 2545 | /** |
| 2546 | * Returns the number of session tickets issued by the server. |
| 2547 | * |
| 2548 | * In TLS1.3, this number can be up to the limit configured by s2n_config_set_initial_ticket_count |
| 2549 | * and s2n_connection_add_new_tickets_to_send. In earlier versions of TLS, this number will be either 0 or 1. |
| 2550 | * |
| 2551 | * This method only works for server connections. |
| 2552 | * |
| 2553 | * @param conn A pointer to the connection object. |
| 2554 | * @param num The number of additional session tickets sent. |
| 2555 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2556 | */ |
| 2557 | S2N_API extern int s2n_connection_get_tickets_sent(struct s2n_connection *conn, uint16_t *num); |
| 2558 | |
| 2559 | /** |
| 2560 | * Sets the keying material lifetime for >=TLS1.3 session tickets so that one session doesn't get re-used ad infinitum. |
| 2561 | * The default value is one week. |
| 2562 | * |
| 2563 | * @param conn A pointer to the connection object. |
| 2564 | * @param lifetime_in_secs Lifetime of keying material in seconds. |
| 2565 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 2566 | */ |
| 2567 | S2N_API extern int s2n_connection_set_server_keying_material_lifetime(struct s2n_connection *conn, uint32_t lifetime_in_secs); |
| 2568 | |
| 2569 | struct s2n_session_ticket; |
| 2570 | |
| 2571 | /** |
| 2572 | * Callback function for receiving a session ticket. |
| 2573 | * |
| 2574 | * This function will be called each time a session ticket is received, which may be multiple times for TLS1.3. |
| 2575 | * |
| 2576 | * # Safety |
| 2577 | * |
| 2578 | * `ctx` is a void pointer and the caller is responsible for ensuring it is cast to the correct type. |
| 2579 | * `ticket` is valid only within the scope of this callback. |
| 2580 | * |
| 2581 | * @param conn A pointer to the connection object. |
| 2582 | * @param ctx Context for the session ticket callback function. |
| 2583 | * @param ticket Pointer to the received session ticket object. |
| 2584 | */ |
| 2585 | typedef int (*s2n_session_ticket_fn)(struct s2n_connection *conn, void *ctx, struct s2n_session_ticket *ticket); |
| 2586 | |
| 2587 | /** |
| 2588 | * Sets a session ticket callback to be called when a client receives a new session ticket. |
| 2589 | * |
| 2590 | * # Safety |
| 2591 | * |
| 2592 | * `callback` MUST cast `ctx` into the same type of pointer that was originally created. |
| 2593 | * `ctx` MUST be valid for the lifetime of the config, or until a different context is set. |
| 2594 | * |
| 2595 | * @param config A pointer to the config object. |
| 2596 | * @param callback The function that should be called when the callback is triggered. |
| 2597 | * @param ctx The context to be passed when the callback is called. |
| 2598 | */ |
| 2599 | S2N_API extern int s2n_config_set_session_ticket_cb(struct s2n_config *config, s2n_session_ticket_fn callback, void *ctx); |
| 2600 | |
| 2601 | /** |
| 2602 | * Gets the length of the session ticket from a session ticket object. |
| 2603 | * |
| 2604 | * @param ticket Pointer to the session ticket object. |
| 2605 | * @param data_len Pointer to be set to the length of the session ticket on success. |
| 2606 | */ |
| 2607 | S2N_API extern int s2n_session_ticket_get_data_len(struct s2n_session_ticket *ticket, size_t *data_len); |
| 2608 | |
| 2609 | /** |
| 2610 | * Gets the session ticket data from a session ticket object. |
| 2611 | * |
| 2612 | * # Safety |
| 2613 | * The entire session ticket will be copied into `data` on success. Therefore, `data` MUST have enough |
| 2614 | * memory to store the session ticket data. |
| 2615 | * |
| 2616 | * @param ticket Pointer to the session ticket object. |
| 2617 | * @param max_data_len Maximum length of data that can be written to the 'data' pointer. |
| 2618 | * @param data Pointer to where the session ticket data will be stored. |
| 2619 | */ |
| 2620 | S2N_API extern int s2n_session_ticket_get_data(struct s2n_session_ticket *ticket, size_t max_data_len, uint8_t *data); |
| 2621 | |
| 2622 | /** |
| 2623 | * Gets the lifetime in seconds of the session ticket from a session ticket object. |
| 2624 | * |
| 2625 | * @param ticket Pointer to the session ticket object. |
| 2626 | * @param session_lifetime Pointer to a variable where the lifetime of the session ticket will be stored. |
| 2627 | */ |
| 2628 | S2N_API extern int s2n_session_ticket_get_lifetime(struct s2n_session_ticket *ticket, uint32_t *session_lifetime); |
| 2629 | |
| 2630 | /** |
| 2631 | * De-serializes the session state and updates the connection accordingly. |
| 2632 | * |
| 2633 | * If this method fails, the connection should not be affected: calling s2n_negotiate |
| 2634 | * with the connection should simply result in a full handshake. |
| 2635 | * |
| 2636 | * @param conn A pointer to the s2n_connection object |
| 2637 | * @param session A pointer to a buffer of size `length` |
| 2638 | * @param length The size of the `session` buffer |
| 2639 | * |
| 2640 | * @returns The number of copied bytes |
| 2641 | */ |
| 2642 | S2N_API extern int s2n_connection_set_session(struct s2n_connection *conn, const uint8_t *session, size_t length); |
| 2643 | |
| 2644 | /** |
| 2645 | * Serializes the session state from connection and copies into the `session` buffer and returns the number of copied bytes |
| 2646 | * |
| 2647 | * @note This function is not recommended for > TLS 1.2 because in TLS1.3 |
| 2648 | * servers can send multiple session tickets and this function will only |
| 2649 | * return the most recently received ticket. |
| 2650 | * |
| 2651 | * @param conn A pointer to the s2n_connection object |
| 2652 | * @param session A pointer to a buffer of size `max_length` |
| 2653 | * @param max_length The size of the `session` buffer |
| 2654 | * |
| 2655 | * @returns The number of copied bytes |
| 2656 | */ |
| 2657 | S2N_API extern int s2n_connection_get_session(struct s2n_connection *conn, uint8_t *session, size_t max_length); |
| 2658 | |
| 2659 | /** |
| 2660 | * Retrieves a hint from the server indicating how long this ticket's lifetime is. |
| 2661 | * |
| 2662 | * @note This function is not recommended for > TLS 1.2 because in TLS1.3 |
| 2663 | * servers can send multiple session tickets and this function will only |
| 2664 | * return the most recently received ticket lifetime hint. |
| 2665 | * |
| 2666 | * @param conn A pointer to the s2n_connection object |
| 2667 | * |
| 2668 | * @returns The session ticket lifetime hint in seconds from the server or -1 when session ticket was not used for resumption. |
| 2669 | */ |
| 2670 | S2N_API extern int s2n_connection_get_session_ticket_lifetime_hint(struct s2n_connection *conn); |
| 2671 | |
| 2672 | /** |
| 2673 | * Use this to query the serialized session state size before copying it into a buffer. |
| 2674 | * |
| 2675 | * @param conn A pointer to the s2n_connection object |
| 2676 | * |
| 2677 | * @returns number of bytes needed to store serialized session state |
| 2678 | */ |
| 2679 | S2N_API extern int s2n_connection_get_session_length(struct s2n_connection *conn); |
| 2680 | |
| 2681 | /** |
| 2682 | * Gets the latest session id's length from the connection. |
| 2683 | * |
| 2684 | * Use this to query the session id size before copying it into a buffer. |
| 2685 | * |
| 2686 | * @param conn A pointer to the s2n_connection object |
| 2687 | * |
| 2688 | * @returns The latest session id length from the connection. Session id length will be 0 for TLS versions >= TLS1.3 as stateful session resumption has not yet been implemented in TLS1.3. |
| 2689 | */ |
| 2690 | S2N_API extern int s2n_connection_get_session_id_length(struct s2n_connection *conn); |
| 2691 | |
| 2692 | /** |
| 2693 | * Gets the latest session id from the connection, copies it into the `session_id` buffer, and returns the number of copied bytes. |
| 2694 | * |
| 2695 | * The session id may change between s2n receiving the ClientHello and sending the ServerHello, but this function will always describe the latest session id. |
| 2696 | * |
| 2697 | * See s2n_client_hello_get_session_id() to get the session id as it was sent by the client in the ClientHello message. |
| 2698 | * |
| 2699 | * @param conn A pointer to the s2n_connection object |
| 2700 | * @param session_id A pointer to a buffer of size `max_length` |
| 2701 | * @param max_length The size of the `session_id` buffer |
| 2702 | * |
| 2703 | * @returns The number of copied bytes. |
| 2704 | */ |
| 2705 | S2N_API extern int s2n_connection_get_session_id(struct s2n_connection *conn, uint8_t *session_id, size_t max_length); |
| 2706 | |
| 2707 | /** |
| 2708 | * Check if the connection was resumed from an earlier handshake. |
| 2709 | * |
| 2710 | * @param conn A pointer to the s2n_connection object |
| 2711 | * |
| 2712 | * @returns returns 1 if the handshake was abbreviated, otherwise returns 0 |
| 2713 | */ |
| 2714 | S2N_API extern int s2n_connection_is_session_resumed(struct s2n_connection *conn); |
| 2715 | |
| 2716 | /** |
| 2717 | * Check if the connection is OCSP stapled. |
| 2718 | * |
| 2719 | * @param conn A pointer to the s2n_connection object |
| 2720 | * |
| 2721 | * @returns 1 if OCSP response was sent (if connection is in S2N_SERVER mode) or received (if connection is in S2N_CLIENT mode) during handshake, otherwise it returns 0. |
| 2722 | */ |
| 2723 | S2N_API extern int s2n_connection_is_ocsp_stapled(struct s2n_connection *conn); |
| 2724 | |
| 2725 | /** |
| 2726 | * TLS Signature Algorithms - RFC 5246 7.4.1.4.1 |
| 2727 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16 |
| 2728 | */ |
| 2729 | typedef enum { |
| 2730 | S2N_TLS_SIGNATURE_ANONYMOUS = 0, |
| 2731 | S2N_TLS_SIGNATURE_RSA = 1, |
| 2732 | S2N_TLS_SIGNATURE_ECDSA = 3, |
| 2733 | S2N_TLS_SIGNATURE_MLDSA = 9, |
| 2734 | |
| 2735 | /* Use Private Range for RSA PSS since it's not defined there */ |
| 2736 | S2N_TLS_SIGNATURE_RSA_PSS_RSAE = 224, |
| 2737 | S2N_TLS_SIGNATURE_RSA_PSS_PSS |
| 2738 | } s2n_tls_signature_algorithm; |
| 2739 | |
| 2740 | /** TLS Hash Algorithms - https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
| 2741 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18 |
| 2742 | */ |
| 2743 | typedef enum { |
| 2744 | S2N_TLS_HASH_NONE = 0, |
| 2745 | S2N_TLS_HASH_MD5 = 1, |
| 2746 | S2N_TLS_HASH_SHA1 = 2, |
| 2747 | S2N_TLS_HASH_SHA224 = 3, |
| 2748 | S2N_TLS_HASH_SHA256 = 4, |
| 2749 | S2N_TLS_HASH_SHA384 = 5, |
| 2750 | S2N_TLS_HASH_SHA512 = 6, |
| 2751 | |
| 2752 | /* Use Private Range for MD5_SHA1 */ |
| 2753 | S2N_TLS_HASH_MD5_SHA1 = 224 |
| 2754 | } s2n_tls_hash_algorithm; |
| 2755 | |
| 2756 | /** |
| 2757 | * Get the connection's selected signature algorithm. |
| 2758 | * |
| 2759 | * @param conn A pointer to the s2n_connection object |
| 2760 | * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter. |
| 2761 | * |
| 2762 | * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received. |
| 2763 | */ |
| 2764 | S2N_API extern int s2n_connection_get_selected_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg); |
| 2765 | |
| 2766 | /** |
| 2767 | * Get the connection's selected digest algorithm. |
| 2768 | * |
| 2769 | * @param conn A pointer to the s2n_connection object |
| 2770 | * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter. |
| 2771 | * |
| 2772 | * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received. |
| 2773 | */ |
| 2774 | S2N_API extern int s2n_connection_get_selected_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg); |
| 2775 | |
| 2776 | /** |
| 2777 | * Get the client certificate's signature algorithm. |
| 2778 | * |
| 2779 | * @param conn A pointer to the s2n_connection object |
| 2780 | * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter. |
| 2781 | * |
| 2782 | * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received. |
| 2783 | */ |
| 2784 | S2N_API extern int s2n_connection_get_selected_client_cert_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg); |
| 2785 | |
| 2786 | /** |
| 2787 | * Get the client certificate's digest algorithm. |
| 2788 | * |
| 2789 | * @param conn A pointer to the s2n_connection object |
| 2790 | * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter. |
| 2791 | * |
| 2792 | * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received. |
| 2793 | */ |
| 2794 | S2N_API extern int s2n_connection_get_selected_client_cert_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg); |
| 2795 | |
| 2796 | /** |
| 2797 | * Get the human readable signature scheme for the connection. |
| 2798 | * |
| 2799 | * This method will return: |
| 2800 | * 1. The IANA "description" for the negotiated signature scheme. |
| 2801 | * For example, rsa_pss_rsae_sha384 or ecdsa_secp256r1_sha256. |
| 2802 | * 2. An unofficial description, if the server signature did not use an official |
| 2803 | * IANA signature scheme. This description will take the form |
| 2804 | * "legacy_<signature_algorithm>_<hash_algorithm>". |
| 2805 | * For example, legacy_rsa_sha224 or legacy_ecdsa_sha256. |
| 2806 | * 3. "none", if the handshake did not include a server signature. |
| 2807 | * This may happen if session resumption or RSA key exchange are used. |
| 2808 | * |
| 2809 | * If the connection has not yet performed a handshake, this method will error. |
| 2810 | * |
| 2811 | * A note on unofficial descriptions: If TLS1.2 or earlier is negotiated, |
| 2812 | * an official IANA signature scheme may not be chosen. Before TLS1.3, a combination |
| 2813 | * of "signature algorithm" and "hash algorithm" were used instead of signature schemes. |
| 2814 | * Not all combinations were later assigned to official signature schemes. |
| 2815 | * |
| 2816 | * A note on ECDSA signature schemes: TLS1.3 and TLS1.2 ECDSA "signature schemes" |
| 2817 | * share the same IANA value. However, this method assigns them different descriptions |
| 2818 | * because the TLS1.3 versions (like ecdsa_secp256r1_sha256) imply specific curves, |
| 2819 | * while the TLS1.2 versions (like legacy_ecdsa_sha256) do not. |
| 2820 | * |
| 2821 | * IANA signature schemes: |
| 2822 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme |
| 2823 | * IANA signature algorithms: |
| 2824 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16 |
| 2825 | * IANA hash algorithms: |
| 2826 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18 |
| 2827 | * |
| 2828 | * @param conn A pointer to the s2n connection |
| 2829 | * @param scheme_name A pointer that will be set to the signature scheme name. |
| 2830 | * @returns S2N_SUCCESS on success, S2N_FAILURE otherwise. |
| 2831 | */ |
| 2832 | S2N_API extern int s2n_connection_get_signature_scheme(struct s2n_connection *conn, const char **scheme_name); |
| 2833 | |
| 2834 | /** |
| 2835 | * Get the certificate used during the TLS handshake |
| 2836 | * |
| 2837 | * - If `conn` is a server connection, the certificate selected will depend on the |
| 2838 | * ServerName sent by the client and supported ciphers. |
| 2839 | * - If `conn` is a client connection, the certificate sent in response to a CertificateRequest |
| 2840 | * message is returned. Currently s2n-tls supports loading only one certificate in client mode. Note that |
| 2841 | * not all TLS endpoints will request a certificate. |
| 2842 | * |
| 2843 | * @param conn A pointer to the s2n_connection object |
| 2844 | * |
| 2845 | * @returns NULL if the certificate selection phase of the handshake has not completed or if a certificate was not requested by the peer |
| 2846 | */ |
| 2847 | S2N_API extern struct s2n_cert_chain_and_key *s2n_connection_get_selected_cert(struct s2n_connection *conn); |
| 2848 | |
| 2849 | /** |
| 2850 | * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read. |
| 2851 | * @param cert_length This return value represents the length of the s2n certificate chain `chain_and_key`. |
| 2852 | * @returns the length of the s2n certificate chain `chain_and_key`. |
| 2853 | */ |
| 2854 | S2N_API extern int s2n_cert_chain_get_length(const struct s2n_cert_chain_and_key *chain_and_key, uint32_t *cert_length); |
| 2855 | |
| 2856 | /** |
| 2857 | * Returns the certificate `out_cert` present at the index `cert_idx` of the certificate chain `chain_and_key`. |
| 2858 | * |
| 2859 | * Note that the index of the leaf certificate is zero. If the certificate chain `chain_and_key` is NULL or the |
| 2860 | * certificate index value is not in the acceptable range for the input certificate chain, an error is returned. |
| 2861 | * |
| 2862 | * # Safety |
| 2863 | * |
| 2864 | * There is no memory allocation required for `out_cert` buffer prior to calling the `s2n_cert_chain_get_cert` API. |
| 2865 | * The `out_cert` will contain the pointer to the s2n_cert initialized within the input s2n_cert_chain_and_key `chain_and_key`. |
| 2866 | * The pointer to the output s2n certificate `out_cert` is valid until `chain_and_key` is freed up. |
| 2867 | * If a caller wishes to persist the `out_cert` beyond the lifetime of `chain_and_key`, the contents would need to be |
| 2868 | * copied prior to freeing `chain_and_key`. |
| 2869 | * |
| 2870 | * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read. |
| 2871 | * @param out_cert A pointer to the output s2n_cert `out_cert` present at the index `cert_idx` of the certificate chain `chain_and_key`. |
| 2872 | * @param cert_idx The certificate index for the requested certificate within the s2n certificate chain. |
| 2873 | */ |
| 2874 | S2N_API extern int s2n_cert_chain_get_cert(const struct s2n_cert_chain_and_key *chain_and_key, struct s2n_cert **out_cert, const uint32_t cert_idx); |
| 2875 | |
| 2876 | /** |
| 2877 | * Returns the s2n certificate in DER format along with its length. |
| 2878 | * |
| 2879 | * The API gets the s2n certificate `cert` in DER format. The certificate is returned in the `out_cert_der` buffer. |
| 2880 | * Here, `cert_len` represents the length of the certificate. |
| 2881 | * |
| 2882 | * A caller can use certificate parsing tools such as the ones provided by OpenSSL to parse the DER encoded certificate chain returned. |
| 2883 | * |
| 2884 | * # Safety |
| 2885 | * |
| 2886 | * The memory for the `out_cert_der` buffer is allocated and owned by s2n-tls. |
| 2887 | * Since the size of the certificate can potentially be very large, a pointer to internal connection data is returned instead of |
| 2888 | * copying the contents into a caller-provided buffer. |
| 2889 | * |
| 2890 | * The pointer to the output buffer `out_cert_der` is valid only while the connection exists. |
| 2891 | * The `s2n_connection_free` API frees the memory associated with the out_cert_der buffer and after the `s2n_connection_wipe` API is |
| 2892 | * called the memory pointed by out_cert_der is invalid. |
| 2893 | * |
| 2894 | * If a caller wishes to persist the `out_cert_der` beyond the lifetime of the connection, the contents would need to be |
| 2895 | * copied prior to the connection termination. |
| 2896 | * |
| 2897 | * @param cert A pointer to the s2n_cert object being read. |
| 2898 | * @param out_cert_der A pointer to the output buffer which will hold the s2n certificate `cert` in DER format. |
| 2899 | * @param cert_length This return value represents the length of the certificate. |
| 2900 | */ |
| 2901 | S2N_API extern int s2n_cert_get_der(const struct s2n_cert *cert, const uint8_t **out_cert_der, uint32_t *cert_length); |
| 2902 | |
| 2903 | /** |
| 2904 | * Returns the validated peer certificate chain as a `s2n_cert_chain_and_key` opaque object. |
| 2905 | * |
| 2906 | * The `s2n_cert_chain_and_key` parameter must be allocated by the caller using the `s2n_cert_chain_and_key_new` API |
| 2907 | * prior to this function call and must be empty. To free the memory associated with the `s2n_cert_chain_and_key` object use the |
| 2908 | * `s2n_cert_chain_and_key_free` API. |
| 2909 | * |
| 2910 | * @param conn A pointer to the s2n_connection object being read. |
| 2911 | * @param cert_chain The returned validated peer certificate chain `cert_chain` retrieved from the s2n connection. |
| 2912 | */ |
| 2913 | S2N_API extern int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain); |
| 2914 | |
| 2915 | /** |
| 2916 | * Returns the length of the DER encoded extension value of the ASN.1 X.509 certificate extension. |
| 2917 | * |
| 2918 | * @param cert A pointer to the s2n_cert object being read. |
| 2919 | * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read. |
| 2920 | * @param ext_value_len This return value contains the length of DER encoded extension value of the ASN.1 X.509 certificate extension. |
| 2921 | */ |
| 2922 | S2N_API extern int s2n_cert_get_x509_extension_value_length(struct s2n_cert *cert, const uint8_t *oid, uint32_t *ext_value_len); |
| 2923 | |
| 2924 | /** |
| 2925 | * Returns the DER encoding of an ASN.1 X.509 certificate extension value, it's length and a boolean critical. |
| 2926 | * |
| 2927 | * @param cert A pointer to the s2n_cert object being read. |
| 2928 | * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read. |
| 2929 | * @param ext_value A pointer to the output buffer which will hold the DER encoding of an ASN.1 X.509 certificate extension value returned. |
| 2930 | * @param ext_value_len This value is both an input and output parameter and represents the length of the output buffer `ext_value`. |
| 2931 | * When used as an input parameter, the caller must use this parameter to convey the maximum length of `ext_value`. |
| 2932 | * When used as an output parameter, `ext_value_len` holds the actual length of the DER encoding of the ASN.1 X.509 certificate extension value returned. |
| 2933 | * @param critical This return value contains the boolean value for `critical`. |
| 2934 | */ |
| 2935 | S2N_API extern int s2n_cert_get_x509_extension_value(struct s2n_cert *cert, const uint8_t *oid, uint8_t *ext_value, uint32_t *ext_value_len, bool *critical); |
| 2936 | |
| 2937 | /** |
| 2938 | * Returns the UTF8 String length of the ASN.1 X.509 certificate extension data. |
| 2939 | * |
| 2940 | * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read. |
| 2941 | * @param extension_len represents the length of the input buffer `extension_data`. |
| 2942 | * @param utf8_str_len This return value contains the UTF8 String length of the ASN.1 X.509 certificate extension data. |
| 2943 | */ |
| 2944 | S2N_API extern int s2n_cert_get_utf8_string_from_extension_data_length(const uint8_t *extension_data, uint32_t extension_len, uint32_t *utf8_str_len); |
| 2945 | |
| 2946 | /** |
| 2947 | * Returns the UTF8 String representation of the DER encoded ASN.1 X.509 certificate extension data. |
| 2948 | * |
| 2949 | * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read. |
| 2950 | * @param extension_len represents the length of the input buffer `extension_data`. |
| 2951 | * @param out_data A pointer to the output buffer which will hold the UTF8 String representation of the DER encoded ASN.1 X.509 |
| 2952 | * certificate extension data returned. |
| 2953 | * @param out_len This value is both an input and output parameter and represents the length of the output buffer `out_data`. |
| 2954 | * When used as an input parameter, the caller must use this parameter to convey the maximum length of `out_data`. |
| 2955 | * When used as an output parameter, `out_len` holds the actual length of UTF8 String returned. |
| 2956 | */ |
| 2957 | S2N_API extern int s2n_cert_get_utf8_string_from_extension_data(const uint8_t *extension_data, uint32_t extension_len, uint8_t *out_data, uint32_t *out_len); |
| 2958 | |
| 2959 | /** |
| 2960 | * Pre-shared key (PSK) Hash Algorithm - RFC 8446 Section-2.2 |
| 2961 | */ |
| 2962 | typedef enum { |
| 2963 | S2N_PSK_HMAC_SHA256, |
| 2964 | S2N_PSK_HMAC_SHA384, |
| 2965 | } s2n_psk_hmac; |
| 2966 | |
| 2967 | /** |
| 2968 | * Opaque pre shared key handle |
| 2969 | */ |
| 2970 | struct s2n_psk; |
| 2971 | |
| 2972 | /** |
| 2973 | * Creates a new s2n external pre-shared key (PSK) object with `S2N_PSK_HMAC_SHA256` as the default |
| 2974 | * PSK hash algorithm. An external PSK is a key established outside of TLS using a secure mutually agreed upon mechanism. |
| 2975 | * |
| 2976 | * Use `s2n_psk_free` to free the memory allocated to the s2n external PSK object created by this API. |
| 2977 | * |
| 2978 | * @returns struct s2n_psk* Returns a pointer to the newly created external PSK object. |
| 2979 | */ |
| 2980 | S2N_API struct s2n_psk *s2n_external_psk_new(void); |
| 2981 | |
| 2982 | /** |
| 2983 | * Frees the memory associated with the external PSK object. |
| 2984 | * |
| 2985 | * @param psk Pointer to the PSK object to be freed. |
| 2986 | */ |
| 2987 | S2N_API int s2n_psk_free(struct s2n_psk **psk); |
| 2988 | |
| 2989 | /** |
| 2990 | * Sets the identity for a given external PSK object. |
| 2991 | * The identity is a unique identifier for the pre-shared secret. |
| 2992 | * It is a non-secret value represented by raw bytes. |
| 2993 | * |
| 2994 | * # Safety |
| 2995 | * |
| 2996 | * The identity is transmitted over the network unencrypted and is a non-secret value. |
| 2997 | * Do not include confidential information in the identity. |
| 2998 | * |
| 2999 | * Note that the identity is copied into s2n-tls memory and the caller is responsible for |
| 3000 | * freeing the memory associated with the identity input. |
| 3001 | * |
| 3002 | * @param psk A pointer to a PSK object to be updated with the identity. |
| 3003 | * @param identity The identity in raw bytes format to be copied. |
| 3004 | * @param identity_size The length of the PSK identity being set. |
| 3005 | */ |
| 3006 | S2N_API int s2n_psk_set_identity(struct s2n_psk *psk, const uint8_t *identity, uint16_t identity_size); |
| 3007 | |
| 3008 | /** |
| 3009 | * Sets the out-of-band/externally provisioned secret for a given external PSK object. |
| 3010 | * |
| 3011 | * # Safety |
| 3012 | * |
| 3013 | * Note that the secret is copied into s2n-tls memory and the caller is responsible for |
| 3014 | * freeing the memory associated with the `secret` input. |
| 3015 | * |
| 3016 | * Deriving a shared secret from a password or other low-entropy source |
| 3017 | * is not secure and is subject to dictionary attacks. |
| 3018 | * See https://tools.ietf.org/rfc/rfc8446#section-2.2 for more information. |
| 3019 | * |
| 3020 | * @param psk A pointer to a PSK object to be updated with the secret. |
| 3021 | * @param secret The secret in raw bytes format to be copied. |
| 3022 | * @param secret_size The length of the pre-shared secret being set. |
| 3023 | */ |
| 3024 | S2N_API int s2n_psk_set_secret(struct s2n_psk *psk, const uint8_t *secret, uint16_t secret_size); |
| 3025 | |
| 3026 | /** |
| 3027 | * Sets the hash algorithm for a given external PSK object. The supported PSK hash |
| 3028 | * algorithms are as listed in the enum `s2n_psk_hmac` above. |
| 3029 | * |
| 3030 | * @param psk A pointer to the external PSK object to be updated with the PSK hash algorithm. |
| 3031 | * @param hmac The PSK hash algorithm being set. |
| 3032 | */ |
| 3033 | S2N_API int s2n_psk_set_hmac(struct s2n_psk *psk, s2n_psk_hmac hmac); |
| 3034 | |
| 3035 | /** |
| 3036 | * Appends a PSK object to the list of PSKs supported by the s2n connection. |
| 3037 | * If a PSK with a duplicate identity is found, an error is returned and the PSK is not added to the list. |
| 3038 | * Note that a copy of `psk` is stored on the connection. The user is still responsible for freeing the |
| 3039 | * memory associated with `psk`. |
| 3040 | * |
| 3041 | * @param conn A pointer to the s2n_connection object that contains the list of PSKs supported. |
| 3042 | * @param psk A pointer to the `s2n_psk` object to be appended to the list of PSKs on the s2n connection. |
| 3043 | */ |
| 3044 | S2N_API int s2n_connection_append_psk(struct s2n_connection *conn, struct s2n_psk *psk); |
| 3045 | |
| 3046 | /** |
| 3047 | * The list of PSK modes supported by s2n-tls for TLS versions >= TLS1.3. |
| 3048 | * Currently s2n-tls supports two modes - `S2N_PSK_MODE_RESUMPTION`, which represents the PSKs established |
| 3049 | * using the previous connection via session resumption, and `S2N_PSK_MODE_EXTERNAL`, which represents PSKs |
| 3050 | * established out-of-band/externally using a secure mutually agreed upon mechanism. |
| 3051 | */ |
| 3052 | typedef enum { |
| 3053 | S2N_PSK_MODE_RESUMPTION, |
| 3054 | S2N_PSK_MODE_EXTERNAL |
| 3055 | } s2n_psk_mode; |
| 3056 | |
| 3057 | /** |
| 3058 | * Sets the PSK mode on the s2n config object. |
| 3059 | * The supported PSK modes are listed in the enum `s2n_psk_mode` above. |
| 3060 | * |
| 3061 | * @param config A pointer to the s2n_config object being updated. |
| 3062 | * @param mode The PSK mode to be set. |
| 3063 | */ |
| 3064 | S2N_API int s2n_config_set_psk_mode(struct s2n_config *config, s2n_psk_mode mode); |
| 3065 | |
| 3066 | /** |
| 3067 | * Sets the PSK mode on the s2n connection object. |
| 3068 | * The supported PSK modes are listed in the enum `s2n_psk_mode` above. |
| 3069 | * This API overrides the PSK mode set on config for this connection. |
| 3070 | * |
| 3071 | * @param conn A pointer to the s2n_connection object being updated. |
| 3072 | * @param mode The PSK mode to be set. |
| 3073 | */ |
| 3074 | S2N_API int s2n_connection_set_psk_mode(struct s2n_connection *conn, s2n_psk_mode mode); |
| 3075 | |
| 3076 | /** |
| 3077 | * Gets the negotiated PSK identity length from the s2n connection object. The negotiated PSK |
| 3078 | * refers to the chosen PSK by the server to be used for the connection. |
| 3079 | * |
| 3080 | * This API can be used to determine if the negotiated PSK exists. If negotiated PSK exists a |
| 3081 | * call to this API returns a value greater than zero. If the negotiated PSK does not exist, the |
| 3082 | * value `0` is returned. |
| 3083 | * |
| 3084 | * @param conn A pointer to the s2n_connection object that successfully negotiated a PSK connection. |
| 3085 | * @param identity_length The length of the negotiated PSK identity. |
| 3086 | */ |
| 3087 | S2N_API int s2n_connection_get_negotiated_psk_identity_length(struct s2n_connection *conn, uint16_t *identity_length); |
| 3088 | |
| 3089 | /** |
| 3090 | * Gets the negotiated PSK identity from the s2n connection object. |
| 3091 | * If the negotiated PSK does not exist, the PSK identity will not be obtained and no error will be returned. |
| 3092 | * Prior to this API call, use `s2n_connection_get_negotiated_psk_identity_length` to determine if a |
| 3093 | * negotiated PSK exists or not. |
| 3094 | * |
| 3095 | * # Safety |
| 3096 | * |
| 3097 | * The negotiated PSK identity will be copied into the identity buffer on success. |
| 3098 | * Therefore, the identity buffer must have enough memory to fit the identity length. |
| 3099 | * |
| 3100 | * @param conn A pointer to the s2n_connection object. |
| 3101 | * @param identity The negotiated PSK identity obtained from the s2n_connection object. |
| 3102 | * @param max_identity_length The maximum length for the PSK identity. If the negotiated psk_identity length is |
| 3103 | * greater than this `max_identity_length` value an error will be returned. |
| 3104 | */ |
| 3105 | S2N_API int s2n_connection_get_negotiated_psk_identity(struct s2n_connection *conn, uint8_t *identity, uint16_t max_identity_length); |
| 3106 | |
| 3107 | struct s2n_offered_psk; |
| 3108 | |
| 3109 | /** |
| 3110 | * Creates a new s2n offered PSK object. |
| 3111 | * An offered PSK object represents a single PSK sent by the client. |
| 3112 | * |
| 3113 | * # Safety |
| 3114 | * |
| 3115 | * Use `s2n_offered_psk_free` to free the memory allocated to the s2n offered PSK object created by this API. |
| 3116 | * |
| 3117 | * @returns struct s2n_offered_psk* Returns a pointer to the newly created offered PSK object. |
| 3118 | */ |
| 3119 | S2N_API struct s2n_offered_psk *s2n_offered_psk_new(void); |
| 3120 | |
| 3121 | /** |
| 3122 | * Frees the memory associated with the `s2n_offered_psk` object. |
| 3123 | * |
| 3124 | * @param psk A pointer to the `s2n_offered_psk` object to be freed. |
| 3125 | */ |
| 3126 | S2N_API int s2n_offered_psk_free(struct s2n_offered_psk **psk); |
| 3127 | |
| 3128 | /** |
| 3129 | * Gets the PSK identity and PSK identity length for a given offered PSK object. |
| 3130 | * |
| 3131 | * @param psk A pointer to the offered PSK object being read. |
| 3132 | * @param identity The PSK identity being obtained. |
| 3133 | * @param size The length of the PSK identity being obtained. |
| 3134 | */ |
| 3135 | S2N_API int s2n_offered_psk_get_identity(struct s2n_offered_psk *psk, uint8_t **identity, uint16_t *size); |
| 3136 | |
| 3137 | struct s2n_offered_psk_list; |
| 3138 | |
| 3139 | /** |
| 3140 | * Checks whether the offered PSK list has an offered psk object next in line in the list. |
| 3141 | * An offered PSK list contains all the PSKs offered by the client for the server to select. |
| 3142 | * |
| 3143 | * # Safety |
| 3144 | * |
| 3145 | * This API returns a pointer to the s2n-tls internal memory with limited lifetime. |
| 3146 | * After the completion of `s2n_psk_selection_callback` this pointer is invalid. |
| 3147 | * |
| 3148 | * @param psk_list A pointer to the offered PSK list being read. |
| 3149 | * @returns bool A boolean value representing whether an offered psk object is present next in line in the offered PSK list. |
| 3150 | */ |
| 3151 | S2N_API bool s2n_offered_psk_list_has_next(struct s2n_offered_psk_list *psk_list); |
| 3152 | |
| 3153 | /** |
| 3154 | * Obtains the next offered PSK object from the list of offered PSKs. Use `s2n_offered_psk_list_has_next` |
| 3155 | * prior to this API call to ensure we have not reached the end of the list. |
| 3156 | * |
| 3157 | * @param psk_list A pointer to the offered PSK list being read. |
| 3158 | * @param psk A pointer to the next offered PSK object being obtained. |
| 3159 | */ |
| 3160 | S2N_API int s2n_offered_psk_list_next(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk); |
| 3161 | |
| 3162 | /** |
| 3163 | * Returns the offered PSK list to its original read state. |
| 3164 | * |
| 3165 | * When `s2n_offered_psk_list_reread` is called, `s2n_offered_psk_list_next` will return the first PSK |
| 3166 | * in the offered PSK list. |
| 3167 | * |
| 3168 | * @param psk_list A pointer to the offered PSK list being reread. |
| 3169 | */ |
| 3170 | S2N_API int s2n_offered_psk_list_reread(struct s2n_offered_psk_list *psk_list); |
| 3171 | |
| 3172 | /** |
| 3173 | * Chooses a PSK from the offered PSK list to be used for the connection. |
| 3174 | * This API matches the PSK identity received from the client against the server's known PSK identities |
| 3175 | * list, in order to choose the PSK to be used for the connection. If the PSK identity sent from the client |
| 3176 | * is NULL, no PSK is chosen for the connection. If the client offered PSK identity has no matching PSK identity |
| 3177 | * with the server, an error will be returned. Use this API along with the `s2n_psk_selection_callback` callback |
| 3178 | * to select a PSK identity. |
| 3179 | * |
| 3180 | * @param psk_list A pointer to the server's known PSK list used to compare for a matching PSK with the client. |
| 3181 | * @param psk A pointer to the client's PSK object used to compare with the server's known PSK identities. |
| 3182 | */ |
| 3183 | S2N_API int s2n_offered_psk_list_choose_psk(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk); |
| 3184 | |
| 3185 | /** |
| 3186 | * Callback function to select a PSK from a list of offered PSKs. |
| 3187 | * Use this callback to implement custom PSK selection logic. The s2n-tls default PSK selection logic |
| 3188 | * chooses the first matching PSK from the list of offered PSKs sent by the client. |
| 3189 | * |
| 3190 | * # Safety |
| 3191 | * |
| 3192 | * `context` is a void pointer and the caller is responsible for ensuring it is cast to the correct type. |
| 3193 | * After the completion of this callback, the pointer to `psk_list` is invalid. |
| 3194 | * |
| 3195 | * @param conn A pointer to the s2n_connection object. |
| 3196 | * @param context A pointer to a context for the caller to pass state to the callback, if needed. |
| 3197 | * @param psk_list A pointer to the offered PSK list being read. |
| 3198 | */ |
| 3199 | typedef int (*s2n_psk_selection_callback)(struct s2n_connection *conn, void *context, |
| 3200 | struct s2n_offered_psk_list *psk_list); |
| 3201 | |
| 3202 | /** |
| 3203 | * Sets the callback to select the matching PSK. |
| 3204 | * If this callback is not set s2n-tls uses a default PSK selection logic that selects the first matching |
| 3205 | * server PSK. |
| 3206 | * |
| 3207 | * @param config A pointer to the s2n_config object. |
| 3208 | * @param cb The function that should be called when the callback is triggered. |
| 3209 | * @param context A pointer to a context for the caller to pass state to the callback, if needed. |
| 3210 | */ |
| 3211 | S2N_API int s2n_config_set_psk_selection_callback(struct s2n_config *config, s2n_psk_selection_callback cb, void *context); |
| 3212 | |
| 3213 | /** |
| 3214 | * Get the number of bytes the connection has received. |
| 3215 | * |
| 3216 | * @param conn A pointer to the connection |
| 3217 | * @returns return the number of bytes received by s2n-tls "on the wire" |
| 3218 | */ |
| 3219 | S2N_API extern uint64_t s2n_connection_get_wire_bytes_in(struct s2n_connection *conn); |
| 3220 | |
| 3221 | /** |
| 3222 | * Get the number of bytes the connection has transmitted out. |
| 3223 | * |
| 3224 | * @param conn A pointer to the connection |
| 3225 | * @returns return the number of bytes transmitted out by s2n-tls "on the wire" |
| 3226 | */ |
| 3227 | S2N_API extern uint64_t s2n_connection_get_wire_bytes_out(struct s2n_connection *conn); |
| 3228 | |
| 3229 | /** |
| 3230 | * Access the protocol version supported by the client. |
| 3231 | * |
| 3232 | * @note The return value corresponds to the macros defined as S2N_SSLv2, |
| 3233 | * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13. |
| 3234 | * |
| 3235 | * @param conn A pointer to the connection |
| 3236 | * @returns returns the highest protocol version supported by the client |
| 3237 | */ |
| 3238 | S2N_API extern int s2n_connection_get_client_protocol_version(struct s2n_connection *conn); |
| 3239 | |
| 3240 | /** |
| 3241 | * Access the protocol version supported by the server. |
| 3242 | * |
| 3243 | * @note The return value corresponds to the macros defined as S2N_SSLv2, |
| 3244 | * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13. |
| 3245 | * |
| 3246 | * @param conn A pointer to the connection |
| 3247 | * @returns Returns the highest protocol version supported by the server |
| 3248 | */ |
| 3249 | S2N_API extern int s2n_connection_get_server_protocol_version(struct s2n_connection *conn); |
| 3250 | |
| 3251 | /** |
| 3252 | * Access the protocol version selected for the connection. |
| 3253 | * |
| 3254 | * @note The return value corresponds to the macros defined as S2N_SSLv2, |
| 3255 | * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13. |
| 3256 | * |
| 3257 | * @param conn A pointer to the connection |
| 3258 | * @returns The protocol version actually negotiated by the handshake |
| 3259 | */ |
| 3260 | S2N_API extern int s2n_connection_get_actual_protocol_version(struct s2n_connection *conn); |
| 3261 | |
| 3262 | /** |
| 3263 | * Access the client hello protocol version for the connection. |
| 3264 | * |
| 3265 | * @note The return value corresponds to the macros defined as S2N_SSLv2, |
| 3266 | * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13. |
| 3267 | * |
| 3268 | * @param conn A pointer to the connection |
| 3269 | * @returns The protocol version used to send the initial client hello message. |
| 3270 | */ |
| 3271 | S2N_API extern int s2n_connection_get_client_hello_version(struct s2n_connection *conn); |
| 3272 | |
| 3273 | /** |
| 3274 | * Access the protocol version from the header of the first record that contained the ClientHello message. |
| 3275 | * |
| 3276 | * @note This field has been deprecated and should not be confused with the client hello |
| 3277 | * version. It is often set very low, usually to TLS1.0 for compatibility reasons, |
| 3278 | * and should never be set higher than TLS1.2. Therefore this method should only be used |
| 3279 | * for logging or fingerprinting. |
| 3280 | * |
| 3281 | * @param ch A pointer to the client hello struct |
| 3282 | * @param out The protocol version in the record header containing the Client Hello. |
| 3283 | */ |
| 3284 | S2N_API extern int s2n_client_hello_get_legacy_record_version(struct s2n_client_hello *ch, uint8_t *out); |
| 3285 | |
| 3286 | /** |
| 3287 | * Check if Client Auth was used for a connection. |
| 3288 | * |
| 3289 | * @param conn A pointer to the connection |
| 3290 | * @returns 1 if the handshake completed and Client Auth was negotiated during then |
| 3291 | * handshake. |
| 3292 | */ |
| 3293 | S2N_API extern int s2n_connection_client_cert_used(struct s2n_connection *conn); |
| 3294 | |
| 3295 | /** |
| 3296 | * A function that provides a human readable string of the cipher suite that was chosen |
| 3297 | * for a connection. |
| 3298 | * |
| 3299 | * @warning The string "TLS_NULL_WITH_NULL_NULL" is returned before the TLS handshake has been performed. |
| 3300 | * This does not mean that the ciphersuite "TLS_NULL_WITH_NULL_NULL" will be used by the connection, |
| 3301 | * it is merely being used as a placeholder. |
| 3302 | * |
| 3303 | * @note This function is only accurate after the TLS handshake. |
| 3304 | * |
| 3305 | * @param conn A pointer to the connection |
| 3306 | * @returns A string indicating the cipher suite negotiated by s2n in OpenSSL format. |
| 3307 | */ |
| 3308 | S2N_API extern const char *s2n_connection_get_cipher(struct s2n_connection *conn); |
| 3309 | |
| 3310 | /** |
| 3311 | * A metric to determine whether or not the server found a certificate that matched |
| 3312 | * the client's SNI extension. |
| 3313 | * |
| 3314 | * S2N_SNI_NONE: Client did not send the SNI extension. |
| 3315 | * S2N_SNI_EXACT_MATCH: Server had a certificate that matched the client's SNI extension. |
| 3316 | * S2N_SNI_WILDCARD_MATCH: Server had a certificate with a domain name containing a wildcard character |
| 3317 | * that could be matched to the client's SNI extension. |
| 3318 | * S2N_SNI_NO_MATCH: Server did not have a certificate that could be matched to the client's |
| 3319 | * SNI extension. |
| 3320 | */ |
| 3321 | typedef enum { |
| 3322 | S2N_SNI_NONE = 1, |
| 3323 | S2N_SNI_EXACT_MATCH, |
| 3324 | S2N_SNI_WILDCARD_MATCH, |
| 3325 | S2N_SNI_NO_MATCH, |
| 3326 | } s2n_cert_sni_match; |
| 3327 | |
| 3328 | /** |
| 3329 | * A function that provides insight into whether or not the server was able to send a certificate that |
| 3330 | * partially or completely matched the client's SNI extension. |
| 3331 | * |
| 3332 | * @note This function can be used as a metric in a failed connection as long as the failure |
| 3333 | * occurs after certificate selection. |
| 3334 | * |
| 3335 | * @param conn A pointer to the connection |
| 3336 | * @param match_status An enum indicating whether or not the server found a certificate |
| 3337 | * that matched the client's SNI extension. |
| 3338 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 3339 | */ |
| 3340 | S2N_API extern int s2n_connection_get_certificate_match(struct s2n_connection *conn, s2n_cert_sni_match *match_status); |
| 3341 | |
| 3342 | /** |
| 3343 | * Provides access to the TLS master secret. |
| 3344 | * |
| 3345 | * This is a dangerous method and should not be used unless absolutely necessary. |
| 3346 | * Mishandling the master secret can compromise both the current connection |
| 3347 | * and any past or future connections that use the same master secret due to |
| 3348 | * session resumption. |
| 3349 | * |
| 3350 | * This method is only supported for older TLS versions, and will report an S2N_ERR_INVALID_STATE |
| 3351 | * usage error if called for a TLS1.3 connection. TLS1.3 includes a new key schedule |
| 3352 | * that derives independent secrets from the master secret for specific purposes, |
| 3353 | * such as separate traffic, session ticket, and exporter secrets. Using the master |
| 3354 | * secret directly circumvents that security feature, reducing the security of |
| 3355 | * the protocol. |
| 3356 | * |
| 3357 | * If you need cryptographic material tied to the current TLS session, consider |
| 3358 | * `s2n_connection_tls_exporter` instead. Although s2n_connection_tls_exporter |
| 3359 | * currently only supports TLS1.3, there is also an RFC that describes exporters |
| 3360 | * for older TLS versions: https://datatracker.ietf.org/doc/html/rfc5705 |
| 3361 | * Using the master secret as-is or defining your own exporter is dangerous. |
| 3362 | * |
| 3363 | * @param conn A pointer to the connection. |
| 3364 | * @param secret_bytes Memory to copy the master secret into. The secret |
| 3365 | * is always 48 bytes long. |
| 3366 | * @param max_size The size of the memory available at `secret_bytes`. Must be |
| 3367 | * at least 48 bytes. |
| 3368 | * @returns S2N_SUCCESS on success, S2N_FAILURE otherwise. `secret_bytes` |
| 3369 | * will be set on success. |
| 3370 | */ |
| 3371 | S2N_API extern int s2n_connection_get_master_secret(const struct s2n_connection *conn, |
| 3372 | uint8_t *secret_bytes, size_t max_size); |
| 3373 | |
| 3374 | /** |
| 3375 | * Provides access to the TLS-Exporter functionality. |
| 3376 | * |
| 3377 | * See https://datatracker.ietf.org/doc/html/rfc5705 and https://www.rfc-editor.org/rfc/rfc8446. |
| 3378 | * |
| 3379 | * @note This is currently only available with TLS 1.3 connections which have finished a handshake. |
| 3380 | * |
| 3381 | * @param conn A pointer to the connection |
| 3382 | * @param label The exporter label used to generate the keying material. |
| 3383 | * @param label_length The length of the label. |
| 3384 | * @param context The context value used to generate the keying material. |
| 3385 | * @param context_length The length of the context. |
| 3386 | * @param output A pointer to the buffer to write the exported keying material into. |
| 3387 | * @param output_length The desired length of the keying material to export. |
| 3388 | * @returns A POSIX error signal. If an error was returned, the value contained in `output` should be considered invalid. |
| 3389 | */ |
| 3390 | S2N_API extern int s2n_connection_tls_exporter(struct s2n_connection *conn, |
| 3391 | const uint8_t *label, uint32_t label_length, const uint8_t *context, uint32_t context_length, |
| 3392 | uint8_t *output, uint32_t output_length); |
| 3393 | |
| 3394 | /** |
| 3395 | * Returns the IANA value for the connection's negotiated cipher suite. |
| 3396 | * |
| 3397 | * The value is returned in the form of `first,second`, in order to closely match |
| 3398 | * the values defined in the [IANA Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#table-tls-parameters-4). |
| 3399 | * For example if the connection's negotiated cipher suite is `TLS_AES_128_GCM_SHA256`, |
| 3400 | * which is registered as `0x13,0x01`, then `first = 0x13` and `second = 0x01`. |
| 3401 | * |
| 3402 | * This method will only succeed after the cipher suite has been negotiated with the peer. |
| 3403 | * |
| 3404 | * @param conn A pointer to the connection being read |
| 3405 | * @param first A pointer to a single byte, which will be updated with the first byte in the registered IANA value. |
| 3406 | * @param second A pointer to a single byte, which will be updated with the second byte in the registered IANA value. |
| 3407 | * @returns A POSIX error signal. If an error was returned, the values contained in `first` and `second` should be considered invalid. |
| 3408 | */ |
| 3409 | S2N_API extern int s2n_connection_get_cipher_iana_value(struct s2n_connection *conn, uint8_t *first, uint8_t *second); |
| 3410 | |
| 3411 | /** |
| 3412 | * Function to check if the cipher used by current connection is supported by the current |
| 3413 | * cipher preferences. |
| 3414 | * @param conn A pointer to the s2n connection |
| 3415 | * @param version A string representing the security policy to check against. |
| 3416 | * @returns 1 if the connection satisfies the cipher suite. 0 if the connection does not satisfy the cipher suite. -1 if there is an error. |
| 3417 | */ |
| 3418 | S2N_API extern int s2n_connection_is_valid_for_cipher_preferences(struct s2n_connection *conn, const char *version); |
| 3419 | |
| 3420 | /** |
| 3421 | * Function to get the human readable elliptic curve name for the connection. |
| 3422 | * |
| 3423 | * @deprecated Use `s2n_connection_get_key_exchange_group` instead |
| 3424 | * |
| 3425 | * @param conn A pointer to the s2n connection |
| 3426 | * @returns A string indicating the elliptic curve used during ECDHE key exchange. The string "NONE" is returned if no curve was used. |
| 3427 | */ |
| 3428 | S2N_API extern const char *s2n_connection_get_curve(struct s2n_connection *conn); |
| 3429 | |
| 3430 | /** |
| 3431 | * Function to get the human readable KEM name for the connection. |
| 3432 | * |
| 3433 | * @deprecated This function was previously used to retrieve the negotiated PQ group in TLS 1.2. |
| 3434 | * PQ key exchange in TLS1.2 was experimental and is now deprecated. Use s2n_connection_get_kem_group_name() |
| 3435 | * to retrieve the PQ TLS 1.3 Group name. |
| 3436 | * |
| 3437 | * @param conn A pointer to the s2n connection |
| 3438 | * @returns A human readable string for the KEM group. If there is no KEM configured returns "NONE" |
| 3439 | */ |
| 3440 | S2N_API extern const char *s2n_connection_get_kem_name(struct s2n_connection *conn); |
| 3441 | |
| 3442 | /** |
| 3443 | * Function to get the human readable KEM group name for the connection. |
| 3444 | * |
| 3445 | * @note PQ key exchange will not occur if the connection is < TLS1.3 or the configured security |
| 3446 | * policy has no KEM groups on it. It also will not occur if the peer does not support PQ key exchange. |
| 3447 | * In these instances this function will return "NONE". |
| 3448 | * |
| 3449 | * @param conn A pointer to the s2n connection |
| 3450 | * @returns A human readable string for the KEM group. Returns "NONE" if no PQ key exchange occurred. |
| 3451 | */ |
| 3452 | S2N_API extern const char *s2n_connection_get_kem_group_name(struct s2n_connection *conn); |
| 3453 | |
| 3454 | /** |
| 3455 | * Function to get the human readable key exchange group name for the connection, for example: |
| 3456 | * `secp521r1` or `SecP256r1MLKEM768`. If an EC curve or KEM was not negotiated, S2N_FAILURE will be |
| 3457 | * returned. |
| 3458 | * |
| 3459 | * @note This function replaces `s2n_connection_get_curve` and `s2n_connection_get_kem_group_name`, returning |
| 3460 | * the named group regardless if a hybrid PQ group was negotiated or not. |
| 3461 | * |
| 3462 | * @param conn A pointer to the s2n connection |
| 3463 | * @param group_name A pointer that will be set to point to a const char* containing the group name |
| 3464 | * @returns S2N_SUCCESS on success, S2N_FAILURE otherwise. `group_name` will be set on success. |
| 3465 | */ |
| 3466 | S2N_API extern int s2n_connection_get_key_exchange_group(struct s2n_connection *conn, const char **group_name); |
| 3467 | |
| 3468 | /** |
| 3469 | * Function to get the alert that caused a connection to close. s2n-tls considers all |
| 3470 | * TLS alerts fatal and shuts down a connection whenever one is received. |
| 3471 | * |
| 3472 | * @param conn A pointer to the s2n connection |
| 3473 | * @returns The TLS alert code that caused a connection to be shut down |
| 3474 | */ |
| 3475 | S2N_API extern int s2n_connection_get_alert(struct s2n_connection *conn); |
| 3476 | |
| 3477 | /** |
| 3478 | * Function to return the last TLS handshake type that was processed. The returned format is a human readable string. |
| 3479 | * |
| 3480 | * @param conn A pointer to the s2n connection |
| 3481 | * @returns A human-readable handshake type name, e.g. "NEGOTIATED|FULL_HANDSHAKE|PERFECT_FORWARD_SECRECY" |
| 3482 | */ |
| 3483 | S2N_API extern const char *s2n_connection_get_handshake_type_name(struct s2n_connection *conn); |
| 3484 | |
| 3485 | /** |
| 3486 | * Function to return the last TLS message that was processed. The returned format is a human readable string. |
| 3487 | * @param conn A pointer to the s2n connection |
| 3488 | * @returns The last message name in the TLS state machine, e.g. "SERVER_HELLO", "APPLICATION_DATA". |
| 3489 | */ |
| 3490 | S2N_API extern const char *s2n_connection_get_last_message_name(struct s2n_connection *conn); |
| 3491 | |
| 3492 | /** |
| 3493 | * Opaque async private key operation handle |
| 3494 | */ |
| 3495 | struct s2n_async_pkey_op; |
| 3496 | |
| 3497 | /** |
| 3498 | * Sets whether or not a connection should enforce strict signature validation during the |
| 3499 | * `s2n_async_pkey_op_apply` call. |
| 3500 | * |
| 3501 | * `mode` can take the following values: |
| 3502 | * - `S2N_ASYNC_PKEY_VALIDATION_FAST` - default behavior: s2n-tls will perform only the minimum validation required for safe use of the asyn pkey operation. |
| 3503 | * - `S2N_ASYNC_PKEY_VALIDATION_STRICT` - in addition to the previous checks, s2n-tls will also ensure that the signature created as a result of the async private key sign operation matches the public key on the connection. |
| 3504 | */ |
| 3505 | typedef enum { |
| 3506 | S2N_ASYNC_PKEY_VALIDATION_FAST, |
| 3507 | S2N_ASYNC_PKEY_VALIDATION_STRICT |
| 3508 | } s2n_async_pkey_validation_mode; |
| 3509 | |
| 3510 | /** |
| 3511 | * The type of private key operation |
| 3512 | */ |
| 3513 | typedef enum { |
| 3514 | S2N_ASYNC_DECRYPT, |
| 3515 | S2N_ASYNC_SIGN |
| 3516 | } s2n_async_pkey_op_type; |
| 3517 | |
| 3518 | /** |
| 3519 | * Callback function for handling private key operations |
| 3520 | * |
| 3521 | * Invoked every time an operation requiring the private key is encountered |
| 3522 | * during the handshake. |
| 3523 | * |
| 3524 | * # Safety |
| 3525 | * * `op` is owned by the application and MUST be freed. |
| 3526 | * |
| 3527 | * @param conn Connection which triggered the callback |
| 3528 | * @param op An opaque object representing the private key operation |
| 3529 | */ |
| 3530 | typedef int (*s2n_async_pkey_fn)(struct s2n_connection *conn, struct s2n_async_pkey_op *op); |
| 3531 | |
| 3532 | /** |
| 3533 | * Sets up the callback to invoke when private key operations occur. |
| 3534 | * |
| 3535 | * @param config Config to set the callback |
| 3536 | * @param fn The function that should be called for each private key operation |
| 3537 | */ |
| 3538 | S2N_API extern int s2n_config_set_async_pkey_callback(struct s2n_config *config, s2n_async_pkey_fn fn); |
| 3539 | |
| 3540 | /** |
| 3541 | * Performs a private key operation using the given private key. |
| 3542 | * |
| 3543 | * # Safety |
| 3544 | * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error. |
| 3545 | * * Safe to call from inside s2n_async_pkey_fn |
| 3546 | * * Safe to call from a different thread, as long as no other thread is operating on `op`. |
| 3547 | * |
| 3548 | * @param op An opaque object representing the private key operation |
| 3549 | * @param key The private key used for the operation. It can be extracted from |
| 3550 | * `conn` through the `s2n_connection_get_selected_cert` and `s2n_cert_chain_and_key_get_private_key` calls |
| 3551 | */ |
| 3552 | S2N_API extern int s2n_async_pkey_op_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *key); |
| 3553 | |
| 3554 | /** |
| 3555 | * Finalizes a private key operation and unblocks the connection. |
| 3556 | * |
| 3557 | * # Safety |
| 3558 | * * `conn` must match the connection that originally triggered the callback. |
| 3559 | * * Must be called after the operation is performed. |
| 3560 | * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error. |
| 3561 | * * Safe to call from inside s2n_async_pkey_fn |
| 3562 | * * Safe to call from a different thread, as long as no other thread is operating on `op`. |
| 3563 | * |
| 3564 | * @param op An opaque object representing the private key operation |
| 3565 | * @param conn The connection associated with the operation that should be unblocked |
| 3566 | */ |
| 3567 | S2N_API extern int s2n_async_pkey_op_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn); |
| 3568 | |
| 3569 | /** |
| 3570 | * Frees the opaque structure representing a private key operation. |
| 3571 | * |
| 3572 | * # Safety |
| 3573 | * * MUST be called for every operation passed to s2n_async_pkey_fn |
| 3574 | * * Safe to call before or after the connection that created the operation is freed |
| 3575 | * |
| 3576 | * @param op An opaque object representing the private key operation |
| 3577 | */ |
| 3578 | S2N_API extern int s2n_async_pkey_op_free(struct s2n_async_pkey_op *op); |
| 3579 | |
| 3580 | /** |
| 3581 | * Configures whether or not s2n-tls will perform potentially expensive validation of |
| 3582 | * the results of a private key operation. |
| 3583 | * |
| 3584 | * @param config Config to set the validation mode for |
| 3585 | * @param mode What level of validation to perform |
| 3586 | */ |
| 3587 | S2N_API extern int s2n_config_set_async_pkey_validation_mode(struct s2n_config *config, s2n_async_pkey_validation_mode mode); |
| 3588 | |
| 3589 | /** |
| 3590 | * Returns the type of the private key operation. |
| 3591 | * |
| 3592 | * @param op An opaque object representing the private key operation |
| 3593 | * @param type A pointer to be set to the type |
| 3594 | */ |
| 3595 | S2N_API extern int s2n_async_pkey_op_get_op_type(struct s2n_async_pkey_op *op, s2n_async_pkey_op_type *type); |
| 3596 | |
| 3597 | /** |
| 3598 | * Returns the size of the input to the private key operation. |
| 3599 | * |
| 3600 | * @param op An opaque object representing the private key operation |
| 3601 | * @param data_len A pointer to be set to the size |
| 3602 | */ |
| 3603 | S2N_API extern int s2n_async_pkey_op_get_input_size(struct s2n_async_pkey_op *op, uint32_t *data_len); |
| 3604 | |
| 3605 | /** |
| 3606 | * Returns the input to the private key operation. |
| 3607 | * |
| 3608 | * When signing, the input is the digest to sign. |
| 3609 | * When decrypting, the input is the data to decrypt. |
| 3610 | * |
| 3611 | * When signing with ML-DSA, the input is the "external mu" pre-hash value described in |
| 3612 | * https://www.ietf.org/archive/id/draft-ietf-lamps-dilithium-certificates-09.html#appendix-D |
| 3613 | * |
| 3614 | * # Safety |
| 3615 | * * `data` must be sufficiently large to contain the input. |
| 3616 | * `s2n_async_pkey_op_get_input_size` can be called to determine how much memory is required. |
| 3617 | * * s2n-tls does not take ownership of `data`. |
| 3618 | * The application still owns the memory and must free it if necessary. |
| 3619 | * |
| 3620 | * @param op An opaque object representing the private key operation |
| 3621 | * @param data A pointer to a buffer to copy the input into |
| 3622 | * @param data_len The maximum size of the `data` buffer |
| 3623 | */ |
| 3624 | S2N_API extern int s2n_async_pkey_op_get_input(struct s2n_async_pkey_op *op, uint8_t *data, uint32_t data_len); |
| 3625 | |
| 3626 | /** |
| 3627 | * Sets the output of the private key operation. |
| 3628 | * |
| 3629 | * # Safety |
| 3630 | * * s2n-tls does not take ownership of `data`. |
| 3631 | * The application still owns the memory and must free it if necessary. |
| 3632 | * |
| 3633 | * @param op An opaque object representing the private key operation |
| 3634 | * @param data A pointer to a buffer containing the output |
| 3635 | * @param data_len The size of the `data` buffer |
| 3636 | */ |
| 3637 | S2N_API extern int s2n_async_pkey_op_set_output(struct s2n_async_pkey_op *op, const uint8_t *data, uint32_t data_len); |
| 3638 | |
| 3639 | /** |
| 3640 | * Callback function for handling key log events |
| 3641 | * |
| 3642 | * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY! |
| 3643 | * |
| 3644 | * Each log line is formatted with the |
| 3645 | * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format) |
| 3646 | * without a newline. |
| 3647 | * |
| 3648 | * # Safety |
| 3649 | * |
| 3650 | * * `ctx` MUST be cast into the same type of pointer that was originally created |
| 3651 | * * `logline` bytes MUST be copied or discarded before this function returns |
| 3652 | * |
| 3653 | * @param ctx Context for the callback |
| 3654 | * @param conn Connection for which the log line is being emitted |
| 3655 | * @param logline Pointer to the log line data |
| 3656 | * @param len Length of the log line data |
| 3657 | */ |
| 3658 | typedef int (*s2n_key_log_fn)(void *ctx, struct s2n_connection *conn, uint8_t *logline, size_t len); |
| 3659 | |
| 3660 | /** |
| 3661 | * Sets a key logging callback on the provided config |
| 3662 | * |
| 3663 | * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY! |
| 3664 | * |
| 3665 | * Setting this function enables configurations to emit secrets in the |
| 3666 | * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format) |
| 3667 | * |
| 3668 | * # Safety |
| 3669 | * |
| 3670 | * * `callback` MUST cast `ctx` into the same type of pointer that was originally created |
| 3671 | * * `ctx` MUST live for at least as long as it is set on the config |
| 3672 | * |
| 3673 | * @param config Config to set the callback |
| 3674 | * @param callback The function that should be called for each secret log entry |
| 3675 | * @param ctx The context to be passed when the callback is called |
| 3676 | */ |
| 3677 | S2N_API extern int s2n_config_set_key_log_cb(struct s2n_config *config, s2n_key_log_fn callback, void *ctx); |
| 3678 | |
| 3679 | /** |
| 3680 | * s2n_config_enable_cert_req_dss_legacy_compat adds a dss cert type in the server certificate request when being called. |
| 3681 | * It only sends the dss cert type in the cert request but does not succeed the handshake if a dss cert is received. |
| 3682 | * Please DO NOT call this api unless you know you actually need legacy DSS certificate type compatibility |
| 3683 | * @param config Config to enable legacy DSS certificates for |
| 3684 | */ |
| 3685 | S2N_API extern int s2n_config_enable_cert_req_dss_legacy_compat(struct s2n_config *config); |
| 3686 | |
| 3687 | /** |
| 3688 | * Sets the maximum bytes of early data the server will accept. |
| 3689 | * |
| 3690 | * The default maximum is 0. If the maximum is 0, the server rejects all early data requests. |
| 3691 | * The config maximum can be overridden by the connection maximum or the maximum on an external pre-shared key. |
| 3692 | * |
| 3693 | * @param config A pointer to the config |
| 3694 | * @param max_early_data_size The maximum early data that the server will accept |
| 3695 | * @returns A POSIX error signal. If successful, the maximum early data size was updated. |
| 3696 | */ |
| 3697 | S2N_API int s2n_config_set_server_max_early_data_size(struct s2n_config *config, uint32_t max_early_data_size); |
| 3698 | |
| 3699 | /** |
| 3700 | * Sets the maximum bytes of early data the server will accept. |
| 3701 | * |
| 3702 | * The default maximum is 0. If the maximum is 0, the server rejects all early data requests. |
| 3703 | * The connection maximum can be overridden by the maximum on an external pre-shared key. |
| 3704 | * |
| 3705 | * @param conn A pointer to the connection |
| 3706 | * @param max_early_data_size The maximum early data the server will accept |
| 3707 | * @returns A POSIX error signal. If successful, the maximum early data size was updated. |
| 3708 | */ |
| 3709 | S2N_API int s2n_connection_set_server_max_early_data_size(struct s2n_connection *conn, uint32_t max_early_data_size); |
| 3710 | |
| 3711 | /** |
| 3712 | * Sets the user context associated with early data on a server. |
| 3713 | * |
| 3714 | * This context is passed to the `s2n_early_data_cb` callback to help decide whether to accept or reject early data. |
| 3715 | * |
| 3716 | * Unlike most contexts, the early data context is a byte buffer instead of a void pointer. |
| 3717 | * This is because we need to serialize the context into session tickets. |
| 3718 | * |
| 3719 | * This API is intended for use with session resumption, and will not affect pre-shared keys. |
| 3720 | * |
| 3721 | * @param conn A pointer to the connection |
| 3722 | * @param context A pointer to the user context data. This data will be copied. |
| 3723 | * @param context_size The size of the data to read from the `context` pointer. |
| 3724 | * @returns A POSIX error signal. If successful, the context was updated. |
| 3725 | */ |
| 3726 | S2N_API int s2n_connection_set_server_early_data_context(struct s2n_connection *conn, const uint8_t *context, uint16_t context_size); |
| 3727 | |
| 3728 | /** |
| 3729 | * Configures a particular pre-shared key to allow early data. |
| 3730 | * |
| 3731 | * `max_early_data_size` must be set to the maximum early data accepted by the server. |
| 3732 | * |
| 3733 | * In order to use early data, the cipher suite set on the pre-shared key must match the cipher suite |
| 3734 | * ultimately negotiated by the TLS handshake. Additionally, the cipher suite must have the same |
| 3735 | * hmac algorithm as the pre-shared key. |
| 3736 | * |
| 3737 | * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`. |
| 3738 | * @param max_early_data_size The maximum early data that can be sent or received using this key. |
| 3739 | * @param cipher_suite_first_byte The first byte in the registered IANA value of the associated cipher suite. |
| 3740 | * @param cipher_suite_second_byte The second byte in the registered IANA value of the associated cipher suite. |
| 3741 | * @returns A POSIX error signal. If successful, `psk` was updated. |
| 3742 | */ |
| 3743 | S2N_API int s2n_psk_configure_early_data(struct s2n_psk *psk, uint32_t max_early_data_size, |
| 3744 | uint8_t cipher_suite_first_byte, uint8_t cipher_suite_second_byte); |
| 3745 | |
| 3746 | /** |
| 3747 | * Sets the optional `application_protocol` associated with the given pre-shared key. |
| 3748 | * |
| 3749 | * In order to use early data, the `application_protocol` set on the pre-shared key must match |
| 3750 | * the `application_protocol` ultimately negotiated by the TLS handshake. |
| 3751 | * |
| 3752 | * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`. |
| 3753 | * @param application_protocol A pointer to the associated application protocol data. This data will be copied. |
| 3754 | * @param size The size of the data to read from the `application_protocol` pointer. |
| 3755 | * @returns A POSIX error signal. If successful, the application protocol was set. |
| 3756 | */ |
| 3757 | S2N_API int s2n_psk_set_application_protocol(struct s2n_psk *psk, const uint8_t *application_protocol, uint8_t size); |
| 3758 | |
| 3759 | /** |
| 3760 | * Sets the optional user early data context associated with the given pre-shared key. |
| 3761 | * |
| 3762 | * The early data context is passed to the `s2n_early_data_cb` callback to help decide whether |
| 3763 | * to accept or reject early data. |
| 3764 | * |
| 3765 | * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`. |
| 3766 | * @param context A pointer to the associated user context data. This data will be copied. |
| 3767 | * @param size The size of the data to read from the `context` pointer. |
| 3768 | * @returns A POSIX error signal. If successful, the context was set. |
| 3769 | */ |
| 3770 | S2N_API int s2n_psk_set_early_data_context(struct s2n_psk *psk, const uint8_t *context, uint16_t size); |
| 3771 | |
| 3772 | /** |
| 3773 | * The status of early data on a connection. |
| 3774 | * |
| 3775 | * S2N_EARLY_DATA_STATUS_OK: Early data is in progress. |
| 3776 | * S2N_EARLY_DATA_STATUS_NOT_REQUESTED: The client did not request early data, so none was sent or received. |
| 3777 | * S2N_EARLY_DATA_STATUS_REJECTED: The client requested early data, but the server rejected the request. |
| 3778 | * Early data may have been sent, but was not received. |
| 3779 | * S2N_EARLY_DATA_STATUS_END: All early data was successfully sent and received. |
| 3780 | */ |
| 3781 | typedef enum { |
| 3782 | S2N_EARLY_DATA_STATUS_OK, |
| 3783 | S2N_EARLY_DATA_STATUS_NOT_REQUESTED, |
| 3784 | S2N_EARLY_DATA_STATUS_REJECTED, |
| 3785 | S2N_EARLY_DATA_STATUS_END, |
| 3786 | } s2n_early_data_status_t; |
| 3787 | |
| 3788 | /** |
| 3789 | * Reports the current state of early data for a connection. |
| 3790 | * |
| 3791 | * See `s2n_early_data_status_t` for all possible states. |
| 3792 | * |
| 3793 | * @param conn A pointer to the connection |
| 3794 | * @param status A pointer which will be set to the current early data status |
| 3795 | * @returns A POSIX error signal. |
| 3796 | */ |
| 3797 | S2N_API int s2n_connection_get_early_data_status(struct s2n_connection *conn, s2n_early_data_status_t *status); |
| 3798 | |
| 3799 | /** |
| 3800 | * Reports the remaining size of the early data allowed by a connection. |
| 3801 | * |
| 3802 | * If early data was rejected or not requested, the remaining early data size is 0. |
| 3803 | * Otherwise, the remaining early data size is the maximum early data allowed by the connection, |
| 3804 | * minus the early data sent or received so far. |
| 3805 | * |
| 3806 | * @param conn A pointer to the connection |
| 3807 | * @param allowed_early_data_size A pointer which will be set to the remaining early data currently allowed by `conn` |
| 3808 | * @returns A POSIX error signal. |
| 3809 | */ |
| 3810 | S2N_API int s2n_connection_get_remaining_early_data_size(struct s2n_connection *conn, uint32_t *allowed_early_data_size); |
| 3811 | |
| 3812 | /** |
| 3813 | * Reports the maximum size of the early data allowed by a connection. |
| 3814 | * |
| 3815 | * This is the maximum amount of early data that can ever be sent and received for a connection. |
| 3816 | * It is not affected by the actual status of the early data, so can be non-zero even if early data |
| 3817 | * is rejected or not requested. |
| 3818 | * |
| 3819 | * @param conn A pointer to the connection |
| 3820 | * @param max_early_data_size A pointer which will be set to the maximum early data allowed by `conn` |
| 3821 | * @returns A POSIX error signal. |
| 3822 | */ |
| 3823 | S2N_API int s2n_connection_get_max_early_data_size(struct s2n_connection *conn, uint32_t *max_early_data_size); |
| 3824 | |
| 3825 | /** |
| 3826 | * Called by the client to begin negotiation and send early data. |
| 3827 | * |
| 3828 | * See https://github.com/aws/s2n-tls/blob/main/docs/usage-guide/topics/ch14-early-data.md |
| 3829 | * for usage and examples. DO NOT USE unless you have considered the security issues and |
| 3830 | * implemented mitigation for anti-replay attacks. |
| 3831 | * |
| 3832 | * @param conn A pointer to the connection |
| 3833 | * @param data A pointer to the early data to be sent |
| 3834 | * @param data_len The size of the early data to send |
| 3835 | * @param data_sent A pointer which will be set to the size of the early data sent |
| 3836 | * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`. |
| 3837 | * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`. |
| 3838 | */ |
| 3839 | S2N_API int s2n_send_early_data(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len, |
| 3840 | ssize_t *data_sent, s2n_blocked_status *blocked); |
| 3841 | |
| 3842 | /** |
| 3843 | * Called by the server to begin negotiation and accept any early data the client sends. |
| 3844 | * |
| 3845 | * See https://github.com/aws/s2n-tls/blob/main/docs/usage-guide/topics/ch14-early-data.md |
| 3846 | * for usage and examples. DO NOT USE unless you have considered the security issues and |
| 3847 | * implemented mitigation for anti-replay attacks. |
| 3848 | * |
| 3849 | * @param conn A pointer to the connection |
| 3850 | * @param data A pointer to a buffer to store the early data received |
| 3851 | * @param max_data_len The size of the early data buffer |
| 3852 | * @param data_received A pointer which will be set to the size of the early data received |
| 3853 | * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`. |
| 3854 | * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`. |
| 3855 | */ |
| 3856 | S2N_API int s2n_recv_early_data(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len, |
| 3857 | ssize_t *data_received, s2n_blocked_status *blocked); |
| 3858 | |
| 3859 | struct s2n_offered_early_data; |
| 3860 | |
| 3861 | /** |
| 3862 | * A callback which can be implemented to accept or reject early data. |
| 3863 | * |
| 3864 | * This callback is triggered only after the server has determined early data is otherwise acceptable according |
| 3865 | * to the TLS early data specification. Implementations therefore only need to cover application-specific checks, |
| 3866 | * not the standard TLS early data validation. |
| 3867 | * |
| 3868 | * This callback can be synchronous or asynchronous. For asynchronous behavior, return success without |
| 3869 | * calling `s2n_offered_early_data_reject` or `s2n_offered_early_data_accept`. `early_data` will |
| 3870 | * still be a valid reference, and the connection will block until `s2n_offered_early_data_reject` or |
| 3871 | * `s2n_offered_early_data_accept` is called. |
| 3872 | * |
| 3873 | * @param conn A pointer to the connection |
| 3874 | * @param early_data A pointer which can be used to access information about the proposed early data |
| 3875 | * and then accept or reject it. |
| 3876 | * @returns A POSIX error signal. If unsuccessful, the connection will be closed with an error. |
| 3877 | */ |
| 3878 | typedef int (*s2n_early_data_cb)(struct s2n_connection *conn, struct s2n_offered_early_data *early_data); |
| 3879 | |
| 3880 | /** |
| 3881 | * Set a callback to accept or reject early data. |
| 3882 | * |
| 3883 | * @param config A pointer to the connection config |
| 3884 | * @param cb A pointer to the implementation of the callback. |
| 3885 | * @returns A POSIX error signal. If successful, the callback was set. |
| 3886 | */ |
| 3887 | S2N_API int s2n_config_set_early_data_cb(struct s2n_config *config, s2n_early_data_cb cb); |
| 3888 | |
| 3889 | /** |
| 3890 | * Get the length of the early data context set by the user. |
| 3891 | * |
| 3892 | * @param early_data A pointer to the early data information |
| 3893 | * @param context_len The length of the user context |
| 3894 | * @returns A POSIX error signal. |
| 3895 | */ |
| 3896 | S2N_API int s2n_offered_early_data_get_context_length(struct s2n_offered_early_data *early_data, uint16_t *context_len); |
| 3897 | |
| 3898 | /** |
| 3899 | * Get the early data context set by the user. |
| 3900 | * |
| 3901 | * @param early_data A pointer to the early data information |
| 3902 | * @param context A byte buffer to copy the user context into |
| 3903 | * @param max_len The size of `context`. Must be >= to the result of `s2n_offered_early_data_get_context_length`. |
| 3904 | * @returns A POSIX error signal. |
| 3905 | */ |
| 3906 | S2N_API int s2n_offered_early_data_get_context(struct s2n_offered_early_data *early_data, uint8_t *context, uint16_t max_len); |
| 3907 | |
| 3908 | /** |
| 3909 | * Reject early data offered by the client. |
| 3910 | * |
| 3911 | * @param early_data A pointer to the early data information |
| 3912 | * @returns A POSIX error signal. If success, the client's early data will be rejected. |
| 3913 | */ |
| 3914 | S2N_API int s2n_offered_early_data_reject(struct s2n_offered_early_data *early_data); |
| 3915 | |
| 3916 | /** |
| 3917 | * Accept early data offered by the client. |
| 3918 | * |
| 3919 | * @param early_data A pointer to the early data information |
| 3920 | * @returns A POSIX error signal. If success, the client's early data will be accepted. |
| 3921 | */ |
| 3922 | S2N_API int s2n_offered_early_data_accept(struct s2n_offered_early_data *early_data); |
| 3923 | |
| 3924 | /** |
| 3925 | * Retrieves the list of supported groups configured by the security policy associated with `config`. |
| 3926 | * |
| 3927 | * The retrieved list of groups will contain all of the supported groups for a security policy that are compatible |
| 3928 | * with the build of s2n-tls. For instance, PQ kem groups that are not supported by the linked libcrypto will not |
| 3929 | * be written. Otherwise, all of the supported groups configured for the security policy will be written. This API |
| 3930 | * can be used with the s2n_client_hello_get_supported_groups() API as a means of comparing compatibility between |
| 3931 | * a client and server. |
| 3932 | * |
| 3933 | * IANA values for each of the supported groups are written to the provided `groups` array, and `groups_count` is |
| 3934 | * set to the number of written supported groups. |
| 3935 | * |
| 3936 | * `groups_count_max` should be set to the maximum capacity of the `groups` array. If `groups_count_max` is less |
| 3937 | * than the number of supported groups configured by the security policy, this function will error. |
| 3938 | * |
| 3939 | * Note that this API retrieves only the groups from a security policy that are available to negotiate via the |
| 3940 | * supported groups extension, and does not return TLS 1.2 PQ kem groups that are negotiated in the supported PQ |
| 3941 | * kem parameters extension. |
| 3942 | * |
| 3943 | * @param config A pointer to the s2n_config object from which the supported groups will be retrieved. |
| 3944 | * @param groups The array to populate with the supported groups. |
| 3945 | * @param groups_count_max The maximum number of supported groups that can fit in the `groups` array. |
| 3946 | * @param groups_count Set to the number of supported groups written to `groups`. |
| 3947 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 3948 | */ |
| 3949 | S2N_API int s2n_config_get_supported_groups(struct s2n_config *config, uint16_t *groups, uint16_t groups_count_max, |
| 3950 | uint16_t *groups_count); |
| 3951 | |
| 3952 | /** Indicates which serialized connection version will be provided. The default value is |
| 3953 | * S2N_SERIALIZED_CONN_NONE, which indicates the feature is off. |
| 3954 | */ |
| 3955 | typedef enum { |
| 3956 | S2N_SERIALIZED_CONN_NONE = 0, |
| 3957 | S2N_SERIALIZED_CONN_V1 = 1 |
| 3958 | } s2n_serialization_version; |
| 3959 | |
| 3960 | /** |
| 3961 | * Set what version to use when serializing connections |
| 3962 | * |
| 3963 | * A version is required to serialize connections. Versioning ensures that all features negotiated |
| 3964 | * during the handshake will be available wherever the connection is deserialized. Applications may |
| 3965 | * need to update this version to pick up new features, since versioning may disable newer TLS |
| 3966 | * features to ensure compatibility. |
| 3967 | * |
| 3968 | * @param config A pointer to the config object. |
| 3969 | * @param version The requested version. |
| 3970 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 3971 | */ |
| 3972 | S2N_API int s2n_config_set_serialization_version(struct s2n_config *config, s2n_serialization_version version); |
| 3973 | |
| 3974 | /** |
| 3975 | * Retrieves the length of the serialized connection from `s2n_connection_serialize()`. Should be |
| 3976 | * used to allocate enough memory for the serialized connection buffer. |
| 3977 | * |
| 3978 | * @note The size of the serialized connection changes based on parameters negotiated in the TLS |
| 3979 | * handshake. Do not expect the size to always remain the same. |
| 3980 | * |
| 3981 | * @param conn A pointer to the connection object. |
| 3982 | * @param length Output parameter where the length will be written. |
| 3983 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 3984 | */ |
| 3985 | S2N_API int s2n_connection_serialization_length(struct s2n_connection *conn, uint32_t *length); |
| 3986 | |
| 3987 | /** |
| 3988 | * Serializes the s2n_connection into the provided buffer. |
| 3989 | * |
| 3990 | * This API takes an established s2n-tls connection object and "serializes" it |
| 3991 | * into a transferable object to be sent off-box or to another process. This transferable object can |
| 3992 | * then be "deserialized" using the `s2n_connection_deserialize` method to instantiate an s2n-tls |
| 3993 | * connection object that can talk to the original peer with the same encryption keys. |
| 3994 | * |
| 3995 | * @warning This feature is dangerous because it provides cryptographic material from a TLS session |
| 3996 | * in plaintext. The serialized blob also does not include a MAC or signature, so a modified blob |
| 3997 | * will be deserialized and used as-is. Users MUST both encrypt and MAC the serialized connection to |
| 3998 | * provide secrecy and integrity before storing or sending it off-box. |
| 3999 | * |
| 4000 | * @note You MUST have used `s2n_config_set_serialization_version()` to set a version on the |
| 4001 | * s2n_config object associated with this connection before this connection began its TLS handshake. |
| 4002 | * @note Call `s2n_connection_serialization_length` to retrieve the amount of memory needed for the |
| 4003 | * buffer parameter. |
| 4004 | * @note This API will error if the handshake is not yet complete. Additionally it will error if there |
| 4005 | * is still application data in the IO buffers given that this data does not get serialized by s2n-tls. |
| 4006 | * You can use `s2n_send` to drain the send buffer and `s2n_peek` + `s2n_recv` to drain the read buffer. |
| 4007 | * Note that s2n-tls will buffer record fragments until the complete record is available. `s2n_peek` |
| 4008 | * will _not_ report buffered record fragments. To avoid buffered record fragments, applications should |
| 4009 | * continue to call s2n_recv until a non-blocked status is returned, after which point s2n_peek and s2n_recv |
| 4010 | * can be used to drain the read buffer. |
| 4011 | * @warning Due to the above read/buffering interaction, this API must not be used |
| 4012 | * with recv_buffering (s2n_connection_set_recv_buffering), because there is no |
| 4013 | * way to ensure that an s2n-tls connection hasn't buffered any record fragments. |
| 4014 | * @note Stream cipher (RC4) connections cannot be serialized. RC4 keystream |
| 4015 | * position is held in libcrypto state that this API does not capture, so a |
| 4016 | * deserialized RC4 connection cannot decrypt records from its peer. |
| 4017 | * |
| 4018 | * @param conn A pointer to the connection object. |
| 4019 | * @param buffer A pointer to the buffer where the serialized connection will be written. |
| 4020 | * @param buffer_length Maximum amount of data that can be written to the buffer param. |
| 4021 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 4022 | */ |
| 4023 | S2N_API int s2n_connection_serialize(struct s2n_connection *conn, uint8_t *buffer, uint32_t buffer_length); |
| 4024 | |
| 4025 | /** |
| 4026 | * Deserializes the provided buffer into the `s2n_connection` parameter. |
| 4027 | * |
| 4028 | * @warning s2n-tls does not check the integrity of the provided buffer. The serialized blob does |
| 4029 | * not include a MAC or signature, so a modified buffer will be deserialized and used as-is. A |
| 4030 | * corrupted buffer will cause a connection failure when attempting to resume sending or receiving |
| 4031 | * encrypted data. To avoid this, callers MUST MAC and encrypt the serialized connection before |
| 4032 | * storing or sending it off-box. Encrypt-then-MAC using a key not stored alongside the blob is |
| 4033 | * the recommended approach. |
| 4034 | * |
| 4035 | * @warning Only a minimal amount of information about the original TLS connection is serialized. |
| 4036 | * Therefore, after deserialization, the connection will behave like a new `s2n_connection` from the |
| 4037 | * `s2n_connection_new()` call, except that it can read/write encrypted data from a peer. Any desired |
| 4038 | * config-level or connection-level configuration will need to be re-applied to the deserialized connection. |
| 4039 | * For this same reason none of the connection getters will return useful information about the |
| 4040 | * original connection after deserialization. Any information about the original connection needs to |
| 4041 | * be retrieved before serialization. |
| 4042 | * |
| 4043 | * @param conn A pointer to the connection object. Should be a new s2n_connection object. |
| 4044 | * @param buffer A pointer to the buffer where the serialized connection will be read from. |
| 4045 | * @param buffer_length Maximum amount of data that can be read from the buffer parameter. |
| 4046 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 4047 | */ |
| 4048 | S2N_API int s2n_connection_deserialize(struct s2n_connection *conn, uint8_t *buffer, uint32_t buffer_length); |
| 4049 | |
| 4050 | /** Load all acceptable certificate authorities from the currently configured trust store. |
| 4051 | * |
| 4052 | * The loaded certificate authorities will be advertised during the handshake. |
| 4053 | * This can help your peer select a certificate if they have multiple certificate |
| 4054 | * chains available. |
| 4055 | * |
| 4056 | * For now, s2n-tls only supports advertising certificate authorities to support |
| 4057 | * client auth, so only servers will send the list of certificate authorities. |
| 4058 | * |
| 4059 | * To avoid configuration mistakes, certificate authorities cannot be loaded from |
| 4060 | * a trust store that includes the default system certificates. That means that |
| 4061 | * s2n_config_new_minimal or s2n_config_wipe_trust_store should be used. |
| 4062 | * |
| 4063 | * s2n-tls currently limits the total certificate authorities size to 10k bytes. |
| 4064 | * This method will fail if the certificate authorities retrieved from the trust |
| 4065 | * store exceed that limit. |
| 4066 | * |
| 4067 | * @param config A pointer to the s2n_config object. |
| 4068 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 4069 | */ |
| 4070 | S2N_API int s2n_config_set_cert_authorities_from_trust_store(struct s2n_config *config); |
| 4071 | |
| 4072 | /** |
| 4073 | * Get the mode of this connection, client or server. |
| 4074 | */ |
| 4075 | S2N_API s2n_mode s2n_connection_get_mode(struct s2n_connection *conn); |
| 4076 | |
| 4077 | #ifdef __cplusplus |
| 4078 | } |
| 4079 | #endif |