s2n_connection.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  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. #include "tls/s2n_connection.h"
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <sys/param.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include "api/s2n.h"
  25. #include "crypto/s2n_certificate.h"
  26. #include "crypto/s2n_cipher.h"
  27. #include "crypto/s2n_crypto.h"
  28. #include "crypto/s2n_fips.h"
  29. #include "crypto/s2n_openssl_x509.h"
  30. #include "error/s2n_errno.h"
  31. #include "tls/extensions/s2n_client_server_name.h"
  32. #include "tls/extensions/s2n_client_supported_versions.h"
  33. #include "tls/s2n_alerts.h"
  34. #include "tls/s2n_cipher_suites.h"
  35. #include "tls/s2n_handshake.h"
  36. #include "tls/s2n_internal.h"
  37. #include "tls/s2n_kem.h"
  38. #include "tls/s2n_prf.h"
  39. #include "tls/s2n_record.h"
  40. #include "tls/s2n_resume.h"
  41. #include "tls/s2n_security_policies.h"
  42. #include "tls/s2n_tls.h"
  43. #include "tls/s2n_tls_parameters.h"
  44. #include "utils/s2n_atomic.h"
  45. #include "utils/s2n_blob.h"
  46. #include "utils/s2n_compiler.h"
  47. #include "utils/s2n_io.h"
  48. #include "utils/s2n_mem.h"
  49. #include "utils/s2n_random.h"
  50. #include "utils/s2n_safety.h"
  51. #include "utils/s2n_socket.h"
  52. #include "utils/s2n_timer.h"
  53. #define S2N_SET_KEY_SHARE_LIST_EMPTY(keyshares) (keyshares |= 1)
  54. #define S2N_SET_KEY_SHARE_REQUEST(keyshares, i) (keyshares |= (1 << (i + 1)))
  55. /* Allocates and initializes memory for a new connection.
  56. *
  57. * Since customers can reuse a connection, ensure that values on the connection are
  58. * initialized in `s2n_connection_wipe` where possible. */
  59. struct s2n_connection *s2n_connection_new(s2n_mode mode)
  60. {
  61. struct s2n_blob blob = { 0 };
  62. PTR_GUARD_POSIX(s2n_alloc(&blob, sizeof(struct s2n_connection)));
  63. PTR_GUARD_POSIX(s2n_blob_zero(&blob));
  64. /* Cast 'through' void to acknowledge that we are changing alignment,
  65. * which is ok, as blob.data is always aligned.
  66. */
  67. struct s2n_connection *conn = (struct s2n_connection *) (void *) blob.data;
  68. PTR_GUARD_POSIX(s2n_connection_set_config(conn, s2n_fetch_default_config()));
  69. /* `mode` is initialized here since it's passed in as a parameter. */
  70. conn->mode = mode;
  71. /* Allocate the fixed-size stuffers */
  72. blob = (struct s2n_blob){ 0 };
  73. PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->alert_in_data, S2N_ALERT_LENGTH));
  74. PTR_GUARD_POSIX(s2n_stuffer_init(&conn->alert_in, &blob));
  75. blob = (struct s2n_blob){ 0 };
  76. PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->ticket_ext_data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
  77. PTR_GUARD_POSIX(s2n_stuffer_init(&conn->client_ticket_to_decrypt, &blob));
  78. /* Allocate long term hash and HMAC memory */
  79. PTR_GUARD_RESULT(s2n_prf_new(conn));
  80. PTR_GUARD_RESULT(s2n_handshake_hashes_new(&conn->handshake.hashes));
  81. /* Initialize the growable stuffers. Zero length at first, but the resize
  82. * in _wipe will fix that
  83. */
  84. blob = (struct s2n_blob){ 0 };
  85. PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->header_in_data, S2N_TLS_RECORD_HEADER_LENGTH));
  86. PTR_GUARD_POSIX(s2n_stuffer_init(&conn->header_in, &blob));
  87. PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->out, 0));
  88. PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->in, 0));
  89. PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->handshake.io, 0));
  90. PTR_GUARD_RESULT(s2n_timer_start(conn->config, &conn->write_timer));
  91. /* NOTE: s2n_connection_wipe MUST be called last in this function.
  92. *
  93. * s2n_connection_wipe is used for initializing values but also used by customers to
  94. * reset/reuse the connection. Calling it last ensures that s2n_connection_wipe is
  95. * implemented correctly and safe.
  96. */
  97. PTR_GUARD_POSIX(s2n_connection_wipe(conn));
  98. return conn;
  99. }
  100. static int s2n_connection_zero(struct s2n_connection *conn, int mode, struct s2n_config *config)
  101. {
  102. /* Zero the whole connection structure */
  103. POSIX_CHECKED_MEMSET(conn, 0, sizeof(struct s2n_connection));
  104. conn->mode = mode;
  105. conn->max_outgoing_fragment_length = S2N_DEFAULT_FRAGMENT_LENGTH;
  106. conn->handshake.end_of_messages = APPLICATION_DATA;
  107. s2n_connection_set_config(conn, config);
  108. return 0;
  109. }
  110. S2N_RESULT s2n_connection_wipe_all_keyshares(struct s2n_connection *conn)
  111. {
  112. RESULT_ENSURE_REF(conn);
  113. RESULT_GUARD_POSIX(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params));
  114. RESULT_GUARD_POSIX(s2n_ecc_evp_params_free(&conn->kex_params.client_ecc_evp_params));
  115. RESULT_GUARD_POSIX(s2n_kem_group_free(&conn->kex_params.server_kem_group_params));
  116. RESULT_GUARD_POSIX(s2n_kem_group_free(&conn->kex_params.client_kem_group_params));
  117. return S2N_RESULT_OK;
  118. }
  119. static int s2n_connection_wipe_keys(struct s2n_connection *conn)
  120. {
  121. /* Free any server key received (we may not have completed a
  122. * handshake, so this may not have been free'd yet) */
  123. POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key));
  124. POSIX_GUARD(s2n_pkey_zero_init(&conn->handshake_params.server_public_key));
  125. POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.client_public_key));
  126. POSIX_GUARD(s2n_pkey_zero_init(&conn->handshake_params.client_public_key));
  127. s2n_x509_validator_wipe(&conn->x509_validator);
  128. POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params));
  129. POSIX_GUARD_RESULT(s2n_connection_wipe_all_keyshares(conn));
  130. POSIX_GUARD(s2n_kem_free(&conn->kex_params.kem_params));
  131. POSIX_GUARD(s2n_free(&conn->handshake_params.client_cert_chain));
  132. POSIX_GUARD(s2n_free(&conn->ct_response));
  133. return 0;
  134. }
  135. static int s2n_connection_free_managed_recv_io(struct s2n_connection *conn)
  136. {
  137. POSIX_ENSURE_REF(conn);
  138. if (conn->managed_recv_io) {
  139. POSIX_GUARD(s2n_free_object((uint8_t **) &conn->recv_io_context, sizeof(struct s2n_socket_read_io_context)));
  140. conn->managed_recv_io = false;
  141. conn->recv = NULL;
  142. }
  143. return S2N_SUCCESS;
  144. }
  145. static int s2n_connection_free_managed_send_io(struct s2n_connection *conn)
  146. {
  147. POSIX_ENSURE_REF(conn);
  148. if (conn->managed_send_io) {
  149. POSIX_GUARD(s2n_free_object((uint8_t **) &conn->send_io_context, sizeof(struct s2n_socket_write_io_context)));
  150. conn->managed_send_io = false;
  151. conn->send = NULL;
  152. }
  153. return S2N_SUCCESS;
  154. }
  155. static int s2n_connection_free_managed_io(struct s2n_connection *conn)
  156. {
  157. POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
  158. POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
  159. return S2N_SUCCESS;
  160. }
  161. static int s2n_connection_wipe_io(struct s2n_connection *conn)
  162. {
  163. if (s2n_connection_is_managed_corked(conn) && conn->recv) {
  164. POSIX_GUARD(s2n_socket_read_restore(conn));
  165. }
  166. if (s2n_connection_is_managed_corked(conn) && conn->send) {
  167. POSIX_GUARD(s2n_socket_write_restore(conn));
  168. }
  169. /* Remove all I/O-related members */
  170. POSIX_GUARD(s2n_connection_free_managed_io(conn));
  171. return 0;
  172. }
  173. static uint8_t s2n_default_verify_host(const char *host_name, size_t len, void *data)
  174. {
  175. /* if present, match server_name of the connection using rules
  176. * outlined in RFC6125 6.4. */
  177. struct s2n_connection *conn = data;
  178. if (conn->server_name[0] == '\0') {
  179. return 0;
  180. }
  181. /* complete match */
  182. if (strlen(conn->server_name) == len && strncasecmp(conn->server_name, host_name, len) == 0) {
  183. return 1;
  184. }
  185. /* match 1 level of wildcard */
  186. if (len > 2 && host_name[0] == '*' && host_name[1] == '.') {
  187. const char *suffix = strchr(conn->server_name, '.');
  188. if (suffix == NULL) {
  189. return 0;
  190. }
  191. if (strlen(suffix) == len - 1 && strncasecmp(suffix, host_name + 1, len - 1) == 0) {
  192. return 1;
  193. }
  194. }
  195. return 0;
  196. }
  197. S2N_CLEANUP_RESULT s2n_connection_ptr_free(struct s2n_connection **conn)
  198. {
  199. RESULT_ENSURE_REF(conn);
  200. RESULT_GUARD_POSIX(s2n_connection_free(*conn));
  201. *conn = NULL;
  202. return S2N_RESULT_OK;
  203. }
  204. int s2n_connection_free(struct s2n_connection *conn)
  205. {
  206. POSIX_GUARD(s2n_connection_wipe_keys(conn));
  207. POSIX_GUARD_RESULT(s2n_psk_parameters_wipe(&conn->psk_params));
  208. POSIX_GUARD_RESULT(s2n_prf_free(conn));
  209. POSIX_GUARD_RESULT(s2n_handshake_hashes_free(&conn->handshake.hashes));
  210. POSIX_GUARD(s2n_connection_free_managed_io(conn));
  211. POSIX_GUARD(s2n_free(&conn->client_ticket));
  212. POSIX_GUARD(s2n_free(&conn->status_response));
  213. POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
  214. POSIX_GUARD(s2n_free(&conn->peer_quic_transport_parameters));
  215. POSIX_GUARD(s2n_free(&conn->server_early_data_context));
  216. POSIX_GUARD(s2n_free(&conn->tls13_ticket_fields.session_secret));
  217. POSIX_GUARD(s2n_stuffer_free(&conn->in));
  218. POSIX_GUARD(s2n_stuffer_free(&conn->out));
  219. POSIX_GUARD(s2n_stuffer_free(&conn->handshake.io));
  220. POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
  221. s2n_x509_validator_wipe(&conn->x509_validator);
  222. POSIX_GUARD(s2n_client_hello_free_raw_message(&conn->client_hello));
  223. POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
  224. POSIX_GUARD(s2n_free(&conn->cookie));
  225. POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial));
  226. POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->secure));
  227. POSIX_GUARD(s2n_free_object((uint8_t **) &conn, sizeof(struct s2n_connection)));
  228. return 0;
  229. }
  230. int s2n_connection_set_config(struct s2n_connection *conn, struct s2n_config *config)
  231. {
  232. POSIX_ENSURE_REF(conn);
  233. POSIX_ENSURE_REF(config);
  234. if (conn->config == config) {
  235. return 0;
  236. }
  237. /* We only support one client certificate */
  238. if (s2n_config_get_num_default_certs(config) > 1 && conn->mode == S2N_CLIENT) {
  239. POSIX_BAIL(S2N_ERR_TOO_MANY_CERTIFICATES);
  240. }
  241. s2n_x509_validator_wipe(&conn->x509_validator);
  242. s2n_cert_auth_type auth_type = config->client_cert_auth_type;
  243. if (conn->client_cert_auth_type_overridden) {
  244. auth_type = conn->client_cert_auth_type;
  245. }
  246. int8_t dont_need_x509_validation = (conn->mode == S2N_SERVER) && (auth_type == S2N_CERT_AUTH_NONE);
  247. if (config->disable_x509_validation || dont_need_x509_validation) {
  248. POSIX_GUARD(s2n_x509_validator_init_no_x509_validation(&conn->x509_validator));
  249. } else {
  250. POSIX_GUARD(s2n_x509_validator_init(&conn->x509_validator, &config->trust_store, config->check_ocsp));
  251. if (!conn->verify_host_fn_overridden) {
  252. if (config->verify_host_fn != NULL) {
  253. conn->verify_host_fn = config->verify_host_fn;
  254. conn->data_for_verify_host = config->data_for_verify_host;
  255. } else {
  256. conn->verify_host_fn = s2n_default_verify_host;
  257. conn->data_for_verify_host = conn;
  258. }
  259. }
  260. if (config->max_verify_cert_chain_depth_set) {
  261. POSIX_GUARD(s2n_x509_validator_set_max_chain_depth(&conn->x509_validator, config->max_verify_cert_chain_depth));
  262. }
  263. }
  264. conn->tickets_to_send = config->initial_tickets_to_send;
  265. if (conn->psk_params.psk_list.len == 0 && !conn->psk_mode_overridden) {
  266. POSIX_GUARD(s2n_connection_set_psk_mode(conn, config->psk_mode));
  267. conn->psk_mode_overridden = false;
  268. }
  269. /* If at least one certificate does not have a private key configured,
  270. * the config must provide an async pkey callback.
  271. * The handshake could still fail if the callback doesn't offload the
  272. * signature, but this at least catches configuration mistakes.
  273. */
  274. if (config->no_signing_key) {
  275. POSIX_ENSURE(config->async_pkey_cb, S2N_ERR_NO_PRIVATE_KEY);
  276. }
  277. if (config->quic_enabled) {
  278. /* If QUIC is ever enabled for a connection via the config,
  279. * we should enforce that it can never be disabled by
  280. * changing the config.
  281. *
  282. * Enabling QUIC indicates that the connection is being used by
  283. * a QUIC implementation, which never changes. Disabling QUIC
  284. * partially through a connection could also potentially be
  285. * dangerous, as QUIC handles encryption.
  286. */
  287. POSIX_GUARD(s2n_connection_enable_quic(conn));
  288. }
  289. if (config->send_buffer_size_override) {
  290. conn->multirecord_send = true;
  291. }
  292. /* Historically, calling s2n_config_set_verification_ca_location enabled OCSP stapling
  293. * regardless of the value set by an application calling s2n_config_set_status_request_type.
  294. * We maintain this behavior for backwards compatibility.
  295. *
  296. * However, the s2n_config_set_verification_ca_location behavior predates client authentication
  297. * support for OCSP stapling, so could only affect whether clients requested OCSP stapling. We
  298. * therefore only have to maintain the legacy behavior for clients, not servers.
  299. *
  300. * Note: The Rust bindings do not maintain the legacy behavior.
  301. */
  302. conn->request_ocsp_status = config->ocsp_status_requested_by_user;
  303. if (config->ocsp_status_requested_by_s2n && conn->mode == S2N_CLIENT) {
  304. conn->request_ocsp_status = true;
  305. }
  306. conn->config = config;
  307. return S2N_SUCCESS;
  308. }
  309. int s2n_connection_server_name_extension_used(struct s2n_connection *conn)
  310. {
  311. POSIX_ENSURE_REF(conn);
  312. POSIX_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_INVALID_STATE);
  313. POSIX_ENSURE(!(conn->handshake.client_hello_received), S2N_ERR_INVALID_STATE);
  314. conn->server_name_used = 1;
  315. return S2N_SUCCESS;
  316. }
  317. int s2n_connection_set_ctx(struct s2n_connection *conn, void *ctx)
  318. {
  319. POSIX_ENSURE_REF(conn);
  320. conn->context = ctx;
  321. return S2N_SUCCESS;
  322. }
  323. void *s2n_connection_get_ctx(struct s2n_connection *conn)
  324. {
  325. return conn->context;
  326. }
  327. int s2n_connection_release_buffers(struct s2n_connection *conn)
  328. {
  329. POSIX_ENSURE_REF(conn);
  330. POSIX_PRECONDITION(s2n_stuffer_validate(&conn->out));
  331. POSIX_PRECONDITION(s2n_stuffer_validate(&conn->in));
  332. POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->out), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
  333. POSIX_GUARD(s2n_stuffer_resize(&conn->out, 0));
  334. POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->in), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
  335. POSIX_GUARD(s2n_stuffer_resize(&conn->in, 0));
  336. POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->post_handshake.in), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
  337. POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
  338. POSIX_POSTCONDITION(s2n_stuffer_validate(&conn->out));
  339. POSIX_POSTCONDITION(s2n_stuffer_validate(&conn->in));
  340. return S2N_SUCCESS;
  341. }
  342. int s2n_connection_free_handshake(struct s2n_connection *conn)
  343. {
  344. /* We are done with the handshake */
  345. POSIX_GUARD_RESULT(s2n_handshake_hashes_free(&conn->handshake.hashes));
  346. POSIX_GUARD_RESULT(s2n_prf_free(conn));
  347. /* All IO should use conn->secure after the handshake.
  348. * However, if this method is called before the handshake completes,
  349. * the connection may still be using conn->initial.
  350. */
  351. if (conn->client != conn->initial && conn->server != conn->initial) {
  352. POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial));
  353. }
  354. /* Wipe the buffers we are going to free */
  355. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  356. POSIX_GUARD(s2n_blob_zero(&conn->client_hello.raw_message));
  357. /* Truncate buffers to save memory, we are done with the handshake */
  358. POSIX_GUARD(s2n_stuffer_resize(&conn->handshake.io, 0));
  359. POSIX_GUARD(s2n_free(&conn->client_hello.raw_message));
  360. /* We can free extension data we no longer need */
  361. POSIX_GUARD(s2n_free(&conn->client_ticket));
  362. POSIX_GUARD(s2n_free(&conn->status_response));
  363. POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
  364. POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
  365. POSIX_GUARD(s2n_free(&conn->cookie));
  366. return 0;
  367. }
  368. /* An idempotent operation which initializes values on the connection.
  369. *
  370. * Called in order to reuse a connection structure for a new connection. Should wipe
  371. * any persistent memory, free any temporary memory, and set all fields back to their
  372. * defaults.
  373. */
  374. int s2n_connection_wipe(struct s2n_connection *conn)
  375. {
  376. /* First make a copy of everything we'd like to save, which isn't very much. */
  377. int mode = conn->mode;
  378. struct s2n_config *config = conn->config;
  379. struct s2n_stuffer alert_in = { 0 };
  380. struct s2n_stuffer client_ticket_to_decrypt = { 0 };
  381. struct s2n_stuffer handshake_io = { 0 };
  382. struct s2n_stuffer header_in = { 0 };
  383. struct s2n_stuffer in = { 0 };
  384. struct s2n_stuffer out = { 0 };
  385. /* Some required structures might have been freed to conserve memory between handshakes.
  386. * Restore them.
  387. */
  388. if (!conn->handshake.hashes) {
  389. POSIX_GUARD_RESULT(s2n_handshake_hashes_new(&conn->handshake.hashes));
  390. }
  391. POSIX_GUARD_RESULT(s2n_handshake_hashes_wipe(conn->handshake.hashes));
  392. struct s2n_handshake_hashes *handshake_hashes = conn->handshake.hashes;
  393. if (!conn->prf_space) {
  394. POSIX_GUARD_RESULT(s2n_prf_new(conn));
  395. }
  396. POSIX_GUARD_RESULT(s2n_prf_wipe(conn));
  397. struct s2n_prf_working_space *prf_workspace = conn->prf_space;
  398. if (!conn->initial) {
  399. POSIX_GUARD_RESULT(s2n_crypto_parameters_new(&conn->initial));
  400. } else {
  401. POSIX_GUARD_RESULT(s2n_crypto_parameters_wipe(conn->initial));
  402. }
  403. struct s2n_crypto_parameters *initial = conn->initial;
  404. if (!conn->secure) {
  405. POSIX_GUARD_RESULT(s2n_crypto_parameters_new(&conn->secure));
  406. } else {
  407. POSIX_GUARD_RESULT(s2n_crypto_parameters_wipe(conn->secure));
  408. }
  409. struct s2n_crypto_parameters *secure = conn->secure;
  410. /* Wipe all of the sensitive stuff */
  411. POSIX_GUARD(s2n_connection_wipe_keys(conn));
  412. POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));
  413. POSIX_GUARD(s2n_stuffer_wipe(&conn->client_ticket_to_decrypt));
  414. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  415. POSIX_GUARD(s2n_stuffer_wipe(&conn->post_handshake.in));
  416. POSIX_GUARD(s2n_blob_zero(&conn->client_hello.raw_message));
  417. POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
  418. POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
  419. POSIX_GUARD(s2n_stuffer_wipe(&conn->out));
  420. /* Free stuffers we plan to just recreate */
  421. POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
  422. POSIX_GUARD_RESULT(s2n_psk_parameters_wipe(&conn->psk_params));
  423. /* Wipe the I/O-related info and restore the original socket if necessary */
  424. POSIX_GUARD(s2n_connection_wipe_io(conn));
  425. POSIX_GUARD(s2n_free(&conn->client_ticket));
  426. POSIX_GUARD(s2n_free(&conn->status_response));
  427. POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
  428. POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
  429. POSIX_GUARD(s2n_free(&conn->peer_quic_transport_parameters));
  430. POSIX_GUARD(s2n_free(&conn->server_early_data_context));
  431. POSIX_GUARD(s2n_free(&conn->tls13_ticket_fields.session_secret));
  432. POSIX_GUARD(s2n_free(&conn->cookie));
  433. /* Allocate memory for handling handshakes */
  434. POSIX_GUARD(s2n_stuffer_resize(&conn->handshake.io, S2N_LARGE_RECORD_LENGTH));
  435. /* Truncate the message buffers to save memory, we will dynamically resize it as needed */
  436. POSIX_GUARD(s2n_free(&conn->client_hello.raw_message));
  437. POSIX_GUARD(s2n_stuffer_resize(&conn->in, 0));
  438. POSIX_GUARD(s2n_stuffer_resize(&conn->out, 0));
  439. /* Remove context associated with connection */
  440. conn->context = NULL;
  441. conn->verify_host_fn_overridden = 0;
  442. conn->verify_host_fn = NULL;
  443. conn->data_for_verify_host = NULL;
  444. /* Clone the stuffers */
  445. /* ignore address warnings because dest is allocated on the stack */
  446. #ifdef S2N_DIAGNOSTICS_PUSH_SUPPORTED
  447. #pragma GCC diagnostic push
  448. #pragma GCC diagnostic ignored "-Waddress"
  449. #endif
  450. POSIX_CHECKED_MEMCPY(&alert_in, &conn->alert_in, sizeof(struct s2n_stuffer));
  451. POSIX_CHECKED_MEMCPY(&client_ticket_to_decrypt, &conn->client_ticket_to_decrypt, sizeof(struct s2n_stuffer));
  452. POSIX_CHECKED_MEMCPY(&handshake_io, &conn->handshake.io, sizeof(struct s2n_stuffer));
  453. POSIX_CHECKED_MEMCPY(&header_in, &conn->header_in, sizeof(struct s2n_stuffer));
  454. POSIX_CHECKED_MEMCPY(&in, &conn->in, sizeof(struct s2n_stuffer));
  455. POSIX_CHECKED_MEMCPY(&out, &conn->out, sizeof(struct s2n_stuffer));
  456. #ifdef S2N_DIAGNOSTICS_POP_SUPPORTED
  457. #pragma GCC diagnostic pop
  458. #endif
  459. POSIX_GUARD(s2n_connection_zero(conn, mode, config));
  460. POSIX_CHECKED_MEMCPY(&conn->alert_in, &alert_in, sizeof(struct s2n_stuffer));
  461. POSIX_CHECKED_MEMCPY(&conn->client_ticket_to_decrypt, &client_ticket_to_decrypt, sizeof(struct s2n_stuffer));
  462. POSIX_CHECKED_MEMCPY(&conn->handshake.io, &handshake_io, sizeof(struct s2n_stuffer));
  463. POSIX_CHECKED_MEMCPY(&conn->header_in, &header_in, sizeof(struct s2n_stuffer));
  464. POSIX_CHECKED_MEMCPY(&conn->in, &in, sizeof(struct s2n_stuffer));
  465. POSIX_CHECKED_MEMCPY(&conn->out, &out, sizeof(struct s2n_stuffer));
  466. conn->handshake.hashes = handshake_hashes;
  467. conn->prf_space = prf_workspace;
  468. conn->initial = initial;
  469. conn->secure = secure;
  470. conn->client = conn->initial;
  471. conn->server = conn->initial;
  472. conn->handshake_params.client_cert_sig_scheme = &s2n_null_sig_scheme;
  473. conn->handshake_params.server_cert_sig_scheme = &s2n_null_sig_scheme;
  474. POSIX_GUARD_RESULT(s2n_psk_parameters_init(&conn->psk_params));
  475. conn->server_keying_material_lifetime = ONE_WEEK_IN_SEC;
  476. /* Require all handshakes hashes. This set can be reduced as the handshake progresses. */
  477. POSIX_GUARD(s2n_handshake_require_all_hashes(&conn->handshake));
  478. if (conn->mode == S2N_SERVER) {
  479. /* Start with the highest protocol version so that the highest common protocol version can be selected */
  480. /* during handshake. */
  481. conn->server_protocol_version = s2n_highest_protocol_version;
  482. conn->client_protocol_version = s2n_unknown_protocol_version;
  483. conn->actual_protocol_version = s2n_unknown_protocol_version;
  484. } else {
  485. /* For clients, also set actual_protocol_version. Record generation uses that value for the initial */
  486. /* ClientHello record version. Not all servers ignore the record version in ClientHello. */
  487. conn->server_protocol_version = s2n_unknown_protocol_version;
  488. conn->client_protocol_version = s2n_highest_protocol_version;
  489. conn->actual_protocol_version = s2n_highest_protocol_version;
  490. }
  491. /* Initialize remaining values */
  492. conn->blinding = S2N_BUILT_IN_BLINDING;
  493. conn->session_ticket_status = S2N_NO_TICKET;
  494. return 0;
  495. }
  496. int s2n_connection_set_recv_ctx(struct s2n_connection *conn, void *ctx)
  497. {
  498. POSIX_ENSURE_REF(conn);
  499. POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
  500. conn->recv_io_context = ctx;
  501. return S2N_SUCCESS;
  502. }
  503. int s2n_connection_set_send_ctx(struct s2n_connection *conn, void *ctx)
  504. {
  505. POSIX_ENSURE_REF(conn);
  506. POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
  507. conn->send_io_context = ctx;
  508. return S2N_SUCCESS;
  509. }
  510. int s2n_connection_set_recv_cb(struct s2n_connection *conn, s2n_recv_fn recv)
  511. {
  512. POSIX_ENSURE_REF(conn);
  513. POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
  514. conn->recv = recv;
  515. return S2N_SUCCESS;
  516. }
  517. int s2n_connection_set_send_cb(struct s2n_connection *conn, s2n_send_fn send)
  518. {
  519. POSIX_ENSURE_REF(conn);
  520. POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
  521. conn->send = send;
  522. return S2N_SUCCESS;
  523. }
  524. int s2n_connection_get_client_cert_chain(struct s2n_connection *conn, uint8_t **cert_chain_out, uint32_t *cert_chain_len)
  525. {
  526. POSIX_ENSURE_REF(conn);
  527. POSIX_ENSURE_REF(cert_chain_out);
  528. POSIX_ENSURE_REF(cert_chain_len);
  529. POSIX_ENSURE_REF(conn->handshake_params.client_cert_chain.data);
  530. *cert_chain_out = conn->handshake_params.client_cert_chain.data;
  531. *cert_chain_len = conn->handshake_params.client_cert_chain.size;
  532. return S2N_SUCCESS;
  533. }
  534. int s2n_connection_get_cipher_preferences(struct s2n_connection *conn, const struct s2n_cipher_preferences **cipher_preferences)
  535. {
  536. POSIX_ENSURE_REF(conn);
  537. POSIX_ENSURE_REF(conn->config);
  538. POSIX_ENSURE_REF(cipher_preferences);
  539. if (conn->security_policy_override != NULL) {
  540. *cipher_preferences = conn->security_policy_override->cipher_preferences;
  541. } else if (conn->config->security_policy != NULL) {
  542. *cipher_preferences = conn->config->security_policy->cipher_preferences;
  543. } else {
  544. POSIX_BAIL(S2N_ERR_INVALID_CIPHER_PREFERENCES);
  545. }
  546. POSIX_ENSURE_REF(*cipher_preferences);
  547. return 0;
  548. }
  549. int s2n_connection_get_security_policy(struct s2n_connection *conn, const struct s2n_security_policy **security_policy)
  550. {
  551. POSIX_ENSURE_REF(conn);
  552. POSIX_ENSURE_REF(conn->config);
  553. POSIX_ENSURE_REF(security_policy);
  554. if (conn->security_policy_override != NULL) {
  555. *security_policy = conn->security_policy_override;
  556. } else if (conn->config->security_policy != NULL) {
  557. *security_policy = conn->config->security_policy;
  558. } else {
  559. POSIX_BAIL(S2N_ERR_INVALID_SECURITY_POLICY);
  560. }
  561. POSIX_ENSURE_REF(*security_policy);
  562. return 0;
  563. }
  564. int s2n_connection_get_kem_preferences(struct s2n_connection *conn, const struct s2n_kem_preferences **kem_preferences)
  565. {
  566. POSIX_ENSURE_REF(conn);
  567. POSIX_ENSURE_REF(conn->config);
  568. POSIX_ENSURE_REF(kem_preferences);
  569. if (conn->security_policy_override != NULL) {
  570. *kem_preferences = conn->security_policy_override->kem_preferences;
  571. } else if (conn->config->security_policy != NULL) {
  572. *kem_preferences = conn->config->security_policy->kem_preferences;
  573. } else {
  574. POSIX_BAIL(S2N_ERR_INVALID_KEM_PREFERENCES);
  575. }
  576. POSIX_ENSURE_REF(*kem_preferences);
  577. return 0;
  578. }
  579. int s2n_connection_get_signature_preferences(struct s2n_connection *conn, const struct s2n_signature_preferences **signature_preferences)
  580. {
  581. POSIX_ENSURE_REF(conn);
  582. POSIX_ENSURE_REF(conn->config);
  583. POSIX_ENSURE_REF(signature_preferences);
  584. if (conn->security_policy_override != NULL) {
  585. *signature_preferences = conn->security_policy_override->signature_preferences;
  586. } else if (conn->config->security_policy != NULL) {
  587. *signature_preferences = conn->config->security_policy->signature_preferences;
  588. } else {
  589. POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_ALGORITHMS_PREFERENCES);
  590. }
  591. POSIX_ENSURE_REF(*signature_preferences);
  592. return 0;
  593. }
  594. int s2n_connection_get_ecc_preferences(struct s2n_connection *conn, const struct s2n_ecc_preferences **ecc_preferences)
  595. {
  596. POSIX_ENSURE_REF(conn);
  597. POSIX_ENSURE_REF(conn->config);
  598. POSIX_ENSURE_REF(ecc_preferences);
  599. if (conn->security_policy_override != NULL) {
  600. *ecc_preferences = conn->security_policy_override->ecc_preferences;
  601. } else if (conn->config->security_policy != NULL) {
  602. *ecc_preferences = conn->config->security_policy->ecc_preferences;
  603. } else {
  604. POSIX_BAIL(S2N_ERR_INVALID_ECC_PREFERENCES);
  605. }
  606. POSIX_ENSURE_REF(*ecc_preferences);
  607. return 0;
  608. }
  609. int s2n_connection_get_protocol_preferences(struct s2n_connection *conn, struct s2n_blob **protocol_preferences)
  610. {
  611. POSIX_ENSURE_REF(conn);
  612. POSIX_ENSURE_REF(protocol_preferences);
  613. *protocol_preferences = NULL;
  614. if (conn->application_protocols_overridden.size > 0) {
  615. *protocol_preferences = &conn->application_protocols_overridden;
  616. } else {
  617. POSIX_ENSURE_REF(conn->config);
  618. *protocol_preferences = &conn->config->application_protocols;
  619. }
  620. POSIX_ENSURE_REF(*protocol_preferences);
  621. return 0;
  622. }
  623. int s2n_connection_get_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type *client_cert_auth_type)
  624. {
  625. POSIX_ENSURE_REF(conn);
  626. POSIX_ENSURE_REF(client_cert_auth_type);
  627. if (conn->client_cert_auth_type_overridden) {
  628. *client_cert_auth_type = conn->client_cert_auth_type;
  629. } else {
  630. POSIX_ENSURE_REF(conn->config);
  631. *client_cert_auth_type = conn->config->client_cert_auth_type;
  632. }
  633. return 0;
  634. }
  635. int s2n_connection_set_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type client_cert_auth_type)
  636. {
  637. conn->client_cert_auth_type_overridden = 1;
  638. conn->client_cert_auth_type = client_cert_auth_type;
  639. return 0;
  640. }
  641. int s2n_connection_set_read_fd(struct s2n_connection *conn, int rfd)
  642. {
  643. struct s2n_blob ctx_mem = { 0 };
  644. struct s2n_socket_read_io_context *peer_socket_ctx;
  645. POSIX_ENSURE_REF(conn);
  646. POSIX_GUARD(s2n_alloc(&ctx_mem, sizeof(struct s2n_socket_read_io_context)));
  647. POSIX_GUARD(s2n_blob_zero(&ctx_mem));
  648. peer_socket_ctx = (struct s2n_socket_read_io_context *) (void *) ctx_mem.data;
  649. peer_socket_ctx->fd = rfd;
  650. POSIX_GUARD(s2n_connection_set_recv_cb(conn, s2n_socket_read));
  651. POSIX_GUARD(s2n_connection_set_recv_ctx(conn, peer_socket_ctx));
  652. conn->managed_recv_io = true;
  653. /* This is only needed if the user is using corked io.
  654. * Take the snapshot in case optimized io is enabled after setting the fd.
  655. */
  656. POSIX_GUARD(s2n_socket_read_snapshot(conn));
  657. return 0;
  658. }
  659. int s2n_connection_get_read_fd(struct s2n_connection *conn, int *readfd)
  660. {
  661. POSIX_ENSURE_REF(conn);
  662. POSIX_ENSURE_REF(readfd);
  663. POSIX_ENSURE((conn->managed_recv_io && conn->recv_io_context), S2N_ERR_INVALID_STATE);
  664. const struct s2n_socket_read_io_context *peer_socket_ctx = conn->recv_io_context;
  665. *readfd = peer_socket_ctx->fd;
  666. return S2N_SUCCESS;
  667. }
  668. int s2n_connection_set_write_fd(struct s2n_connection *conn, int wfd)
  669. {
  670. struct s2n_blob ctx_mem = { 0 };
  671. struct s2n_socket_write_io_context *peer_socket_ctx;
  672. POSIX_ENSURE_REF(conn);
  673. POSIX_GUARD(s2n_alloc(&ctx_mem, sizeof(struct s2n_socket_write_io_context)));
  674. peer_socket_ctx = (struct s2n_socket_write_io_context *) (void *) ctx_mem.data;
  675. peer_socket_ctx->fd = wfd;
  676. POSIX_GUARD(s2n_connection_set_send_cb(conn, s2n_socket_write));
  677. POSIX_GUARD(s2n_connection_set_send_ctx(conn, peer_socket_ctx));
  678. conn->managed_send_io = true;
  679. /* This is only needed if the user is using corked io.
  680. * Take the snapshot in case optimized io is enabled after setting the fd.
  681. */
  682. POSIX_GUARD(s2n_socket_write_snapshot(conn));
  683. uint8_t ipv6;
  684. if (0 == s2n_socket_is_ipv6(wfd, &ipv6)) {
  685. conn->ipv6 = (ipv6 ? 1 : 0);
  686. }
  687. conn->write_fd_broken = 0;
  688. return 0;
  689. }
  690. int s2n_connection_get_write_fd(struct s2n_connection *conn, int *writefd)
  691. {
  692. POSIX_ENSURE_REF(conn);
  693. POSIX_ENSURE_REF(writefd);
  694. POSIX_ENSURE((conn->managed_send_io && conn->send_io_context), S2N_ERR_INVALID_STATE);
  695. const struct s2n_socket_write_io_context *peer_socket_ctx = conn->send_io_context;
  696. *writefd = peer_socket_ctx->fd;
  697. return S2N_SUCCESS;
  698. }
  699. int s2n_connection_set_fd(struct s2n_connection *conn, int fd)
  700. {
  701. POSIX_GUARD(s2n_connection_set_read_fd(conn, fd));
  702. POSIX_GUARD(s2n_connection_set_write_fd(conn, fd));
  703. return 0;
  704. }
  705. int s2n_connection_use_corked_io(struct s2n_connection *conn)
  706. {
  707. POSIX_ENSURE_REF(conn);
  708. /* Caller shouldn't be trying to set s2n IO corked on non-s2n-managed IO */
  709. POSIX_ENSURE(conn->managed_send_io, S2N_ERR_CORK_SET_ON_UNMANAGED);
  710. conn->corked_io = 1;
  711. return 0;
  712. }
  713. uint64_t s2n_connection_get_wire_bytes_in(struct s2n_connection *conn)
  714. {
  715. if (conn->ktls_recv_enabled) {
  716. return 0;
  717. }
  718. return conn->wire_bytes_in;
  719. }
  720. uint64_t s2n_connection_get_wire_bytes_out(struct s2n_connection *conn)
  721. {
  722. if (conn->ktls_send_enabled) {
  723. return 0;
  724. }
  725. return conn->wire_bytes_out;
  726. }
  727. const char *s2n_connection_get_cipher(struct s2n_connection *conn)
  728. {
  729. PTR_ENSURE_REF(conn);
  730. PTR_ENSURE_REF(conn->secure);
  731. PTR_ENSURE_REF(conn->secure->cipher_suite);
  732. return conn->secure->cipher_suite->name;
  733. }
  734. int s2n_connection_get_cipher_iana_value(struct s2n_connection *conn, uint8_t *first, uint8_t *second)
  735. {
  736. POSIX_ENSURE_REF(conn);
  737. POSIX_ENSURE_REF(conn->secure);
  738. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  739. POSIX_ENSURE_MUT(first);
  740. POSIX_ENSURE_MUT(second);
  741. /* ensure we've negotiated a cipher suite */
  742. POSIX_ENSURE(memcmp(conn->secure->cipher_suite->iana_value,
  743. s2n_null_cipher_suite.iana_value, sizeof(s2n_null_cipher_suite.iana_value))
  744. != 0,
  745. S2N_ERR_INVALID_STATE);
  746. const uint8_t *iana_value = conn->secure->cipher_suite->iana_value;
  747. *first = iana_value[0];
  748. *second = iana_value[1];
  749. return S2N_SUCCESS;
  750. }
  751. const char *s2n_connection_get_curve(struct s2n_connection *conn)
  752. {
  753. PTR_ENSURE_REF(conn);
  754. PTR_ENSURE_REF(conn->secure);
  755. PTR_ENSURE_REF(conn->secure->cipher_suite);
  756. if (conn->kex_params.server_ecc_evp_params.negotiated_curve) {
  757. /* TLS1.3 currently only uses ECC groups. */
  758. if (conn->actual_protocol_version >= S2N_TLS13 || s2n_kex_includes(conn->secure->cipher_suite->key_exchange_alg, &s2n_ecdhe)) {
  759. return conn->kex_params.server_ecc_evp_params.negotiated_curve->name;
  760. }
  761. }
  762. return "NONE";
  763. }
  764. const char *s2n_connection_get_kem_name(struct s2n_connection *conn)
  765. {
  766. PTR_ENSURE_REF(conn);
  767. if (!conn->kex_params.kem_params.kem) {
  768. return "NONE";
  769. }
  770. return conn->kex_params.kem_params.kem->name;
  771. }
  772. const char *s2n_connection_get_kem_group_name(struct s2n_connection *conn)
  773. {
  774. PTR_ENSURE_REF(conn);
  775. if (conn->actual_protocol_version < S2N_TLS13 || !conn->kex_params.client_kem_group_params.kem_group) {
  776. return "NONE";
  777. }
  778. return conn->kex_params.client_kem_group_params.kem_group->name;
  779. }
  780. static S2N_RESULT s2n_connection_get_client_supported_version(struct s2n_connection *conn,
  781. uint8_t *client_supported_version)
  782. {
  783. RESULT_ENSURE_REF(conn);
  784. RESULT_ENSURE_EQ(conn->mode, S2N_SERVER);
  785. struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
  786. RESULT_ENSURE_REF(client_hello);
  787. s2n_parsed_extension *supported_versions_extension = NULL;
  788. RESULT_GUARD_POSIX(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SUPPORTED_VERSIONS, &client_hello->extensions,
  789. &supported_versions_extension));
  790. RESULT_ENSURE_REF(supported_versions_extension);
  791. struct s2n_stuffer supported_versions_stuffer = { 0 };
  792. RESULT_GUARD_POSIX(s2n_stuffer_init_written(&supported_versions_stuffer, &supported_versions_extension->extension));
  793. uint8_t client_protocol_version = s2n_unknown_protocol_version;
  794. uint8_t actual_protocol_version = s2n_unknown_protocol_version;
  795. RESULT_GUARD_POSIX(s2n_extensions_client_supported_versions_process(conn, &supported_versions_stuffer,
  796. &client_protocol_version, &actual_protocol_version));
  797. RESULT_ENSURE_NE(client_protocol_version, s2n_unknown_protocol_version);
  798. *client_supported_version = client_protocol_version;
  799. return S2N_RESULT_OK;
  800. }
  801. int s2n_connection_get_client_protocol_version(struct s2n_connection *conn)
  802. {
  803. POSIX_ENSURE_REF(conn);
  804. /* For backwards compatibility, the client_protocol_version field isn't updated via the
  805. * supported versions extension on TLS 1.2 servers. See
  806. * https://github.com/aws/s2n-tls/issues/4240.
  807. *
  808. * The extension is processed here to ensure that TLS 1.2 servers report the same client
  809. * protocol version to applications as TLS 1.3 servers.
  810. */
  811. if (conn->mode == S2N_SERVER && conn->server_protocol_version <= S2N_TLS12) {
  812. uint8_t client_supported_version = s2n_unknown_protocol_version;
  813. s2n_result result = s2n_connection_get_client_supported_version(conn, &client_supported_version);
  814. /* If the extension wasn't received, or if a client protocol version couldn't be determined
  815. * after processing the extension, the extension is ignored.
  816. */
  817. if (s2n_result_is_ok(result)) {
  818. return client_supported_version;
  819. }
  820. }
  821. return conn->client_protocol_version;
  822. }
  823. int s2n_connection_get_server_protocol_version(struct s2n_connection *conn)
  824. {
  825. POSIX_ENSURE_REF(conn);
  826. return conn->server_protocol_version;
  827. }
  828. int s2n_connection_get_actual_protocol_version(struct s2n_connection *conn)
  829. {
  830. POSIX_ENSURE_REF(conn);
  831. return conn->actual_protocol_version;
  832. }
  833. int s2n_connection_get_client_hello_version(struct s2n_connection *conn)
  834. {
  835. POSIX_ENSURE_REF(conn);
  836. return conn->client_hello_version;
  837. }
  838. int s2n_connection_client_cert_used(struct s2n_connection *conn)
  839. {
  840. POSIX_ENSURE_REF(conn);
  841. if (IS_CLIENT_AUTH_HANDSHAKE(conn) && is_handshake_complete(conn)) {
  842. if (IS_CLIENT_AUTH_NO_CERT(conn)) {
  843. return 0;
  844. }
  845. return 1;
  846. }
  847. return 0;
  848. }
  849. int s2n_connection_get_alert(struct s2n_connection *conn)
  850. {
  851. POSIX_ENSURE_REF(conn);
  852. S2N_ERROR_IF(s2n_stuffer_data_available(&conn->alert_in) != 2, S2N_ERR_NO_ALERT);
  853. uint8_t alert_code = 0;
  854. POSIX_GUARD(s2n_stuffer_read_uint8(&conn->alert_in, &alert_code));
  855. POSIX_GUARD(s2n_stuffer_read_uint8(&conn->alert_in, &alert_code));
  856. return alert_code;
  857. }
  858. int s2n_set_server_name(struct s2n_connection *conn, const char *server_name)
  859. {
  860. POSIX_ENSURE_REF(conn);
  861. POSIX_ENSURE_REF(server_name);
  862. S2N_ERROR_IF(conn->mode != S2N_CLIENT, S2N_ERR_CLIENT_MODE);
  863. int len = strlen(server_name);
  864. S2N_ERROR_IF(len > S2N_MAX_SERVER_NAME, S2N_ERR_SERVER_NAME_TOO_LONG);
  865. POSIX_CHECKED_MEMCPY(conn->server_name, server_name, len);
  866. return 0;
  867. }
  868. const char *s2n_get_server_name(struct s2n_connection *conn)
  869. {
  870. PTR_ENSURE_REF(conn);
  871. if (conn->server_name[0]) {
  872. return conn->server_name;
  873. }
  874. PTR_GUARD_POSIX(s2n_extension_process(&s2n_client_server_name_extension, conn, &conn->client_hello.extensions));
  875. if (!conn->server_name[0]) {
  876. return NULL;
  877. }
  878. return conn->server_name;
  879. }
  880. const char *s2n_get_application_protocol(struct s2n_connection *conn)
  881. {
  882. PTR_ENSURE_REF(conn);
  883. if (strlen(conn->application_protocol) == 0) {
  884. return NULL;
  885. }
  886. return conn->application_protocol;
  887. }
  888. int s2n_connection_get_session_id_length(struct s2n_connection *conn)
  889. {
  890. POSIX_ENSURE_REF(conn);
  891. /* Stateful session resumption in TLS1.3 using session id is not yet supported. */
  892. if (conn->actual_protocol_version >= S2N_TLS13) {
  893. return 0;
  894. }
  895. return conn->session_id_len;
  896. }
  897. int s2n_connection_get_session_id(struct s2n_connection *conn, uint8_t *session_id, size_t max_length)
  898. {
  899. POSIX_ENSURE_REF(conn);
  900. POSIX_ENSURE_REF(session_id);
  901. const int session_id_len = s2n_connection_get_session_id_length(conn);
  902. POSIX_GUARD(session_id_len);
  903. POSIX_ENSURE((size_t) session_id_len <= max_length, S2N_ERR_SESSION_ID_TOO_LONG);
  904. POSIX_CHECKED_MEMCPY(session_id, conn->session_id, session_id_len);
  905. return session_id_len;
  906. }
  907. int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blinding)
  908. {
  909. POSIX_ENSURE_REF(conn);
  910. conn->blinding = blinding;
  911. return 0;
  912. }
  913. #define ONE_S INT64_C(1000000000)
  914. #define TEN_S INT64_C(10000000000)
  915. static S2N_RESULT s2n_connection_get_delay_impl(struct s2n_connection *conn, uint64_t *delay)
  916. {
  917. RESULT_ENSURE_REF(conn);
  918. RESULT_ENSURE_REF(delay);
  919. if (!conn->delay) {
  920. *delay = 0;
  921. return S2N_RESULT_OK;
  922. }
  923. uint64_t elapsed = 0;
  924. RESULT_GUARD(s2n_timer_elapsed(conn->config, &conn->write_timer, &elapsed));
  925. if (elapsed > conn->delay) {
  926. *delay = 0;
  927. return S2N_RESULT_OK;
  928. }
  929. *delay = conn->delay - elapsed;
  930. return S2N_RESULT_OK;
  931. }
  932. uint64_t s2n_connection_get_delay(struct s2n_connection *conn)
  933. {
  934. uint64_t delay = 0;
  935. if (s2n_result_is_ok(s2n_connection_get_delay_impl(conn, &delay))) {
  936. return delay;
  937. } else {
  938. return UINT64_MAX;
  939. }
  940. }
  941. static S2N_RESULT s2n_connection_kill(struct s2n_connection *conn)
  942. {
  943. RESULT_ENSURE_REF(conn);
  944. RESULT_GUARD(s2n_connection_set_closed(conn));
  945. /* Delay between 10 and 30 seconds in nanoseconds */
  946. int64_t min = TEN_S, max = 3 * TEN_S;
  947. /* Keep track of the delay so that it can be enforced */
  948. uint64_t rand_delay = 0;
  949. RESULT_GUARD(s2n_public_random(max - min, &rand_delay));
  950. conn->delay = min + rand_delay;
  951. /* Restart the write timer */
  952. RESULT_GUARD(s2n_timer_start(conn->config, &conn->write_timer));
  953. if (conn->blinding == S2N_BUILT_IN_BLINDING) {
  954. struct timespec sleep_time = { .tv_sec = conn->delay / ONE_S, .tv_nsec = conn->delay % ONE_S };
  955. int r = 0;
  956. do {
  957. r = nanosleep(&sleep_time, &sleep_time);
  958. } while (r != 0);
  959. }
  960. return S2N_RESULT_OK;
  961. }
  962. S2N_CLEANUP_RESULT s2n_connection_apply_error_blinding(struct s2n_connection **conn)
  963. {
  964. RESULT_ENSURE_REF(conn);
  965. if (*conn == NULL) {
  966. return S2N_RESULT_OK;
  967. }
  968. int error_code = s2n_errno;
  969. int error_type = s2n_error_get_type(error_code);
  970. switch (error_type) {
  971. case S2N_ERR_T_OK:
  972. /* Ignore no error */
  973. return S2N_RESULT_OK;
  974. case S2N_ERR_T_BLOCKED:
  975. /* All blocking errors are retriable and should trigger no further action. */
  976. return S2N_RESULT_OK;
  977. default:
  978. break;
  979. }
  980. switch (error_code) {
  981. /* Don't invoke blinding on some of the common errors.
  982. *
  983. * Be careful adding new errors here. Disabling blinding for an
  984. * error that can be triggered by secret / encrypted values can
  985. * potentially lead to a side channel attack.
  986. *
  987. * We may want to someday add an explicit error type for these errors.
  988. */
  989. case S2N_ERR_CLOSED:
  990. case S2N_ERR_CANCELLED:
  991. case S2N_ERR_CIPHER_NOT_SUPPORTED:
  992. case S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED:
  993. RESULT_GUARD(s2n_connection_set_closed(*conn));
  994. break;
  995. default:
  996. /* Apply blinding to all other errors */
  997. RESULT_GUARD(s2n_connection_kill(*conn));
  998. break;
  999. }
  1000. return S2N_RESULT_OK;
  1001. }
  1002. S2N_RESULT s2n_connection_set_closed(struct s2n_connection *conn)
  1003. {
  1004. RESULT_ENSURE_REF(conn);
  1005. s2n_atomic_flag_set(&conn->read_closed);
  1006. s2n_atomic_flag_set(&conn->write_closed);
  1007. return S2N_RESULT_OK;
  1008. }
  1009. const uint8_t *s2n_connection_get_ocsp_response(struct s2n_connection *conn, uint32_t *length)
  1010. {
  1011. PTR_ENSURE_REF(conn);
  1012. PTR_ENSURE_REF(length);
  1013. *length = conn->status_response.size;
  1014. return conn->status_response.data;
  1015. }
  1016. S2N_RESULT s2n_connection_set_max_fragment_length(struct s2n_connection *conn, uint16_t max_frag_length)
  1017. {
  1018. RESULT_ENSURE_REF(conn);
  1019. if (conn->negotiated_mfl_code) {
  1020. /* Respect the upper limit agreed on with the peer */
  1021. RESULT_ENSURE_LT(conn->negotiated_mfl_code, s2n_array_len(mfl_code_to_length));
  1022. conn->max_outgoing_fragment_length = MIN(mfl_code_to_length[conn->negotiated_mfl_code], max_frag_length);
  1023. } else {
  1024. conn->max_outgoing_fragment_length = max_frag_length;
  1025. }
  1026. /* If no buffer has been initialized yet, no need to resize.
  1027. * The standard I/O logic will handle initializing the buffer.
  1028. */
  1029. if (s2n_stuffer_is_freed(&conn->out)) {
  1030. return S2N_RESULT_OK;
  1031. }
  1032. uint16_t max_wire_record_size = 0;
  1033. RESULT_GUARD(s2n_record_max_write_size(conn, conn->max_outgoing_fragment_length, &max_wire_record_size));
  1034. if ((conn->out.blob.size < max_wire_record_size)) {
  1035. RESULT_GUARD_POSIX(s2n_realloc(&conn->out.blob, max_wire_record_size));
  1036. }
  1037. return S2N_RESULT_OK;
  1038. }
  1039. int s2n_connection_prefer_throughput(struct s2n_connection *conn)
  1040. {
  1041. POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, S2N_LARGE_FRAGMENT_LENGTH));
  1042. return S2N_SUCCESS;
  1043. }
  1044. int s2n_connection_prefer_low_latency(struct s2n_connection *conn)
  1045. {
  1046. POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, S2N_SMALL_FRAGMENT_LENGTH));
  1047. return S2N_SUCCESS;
  1048. }
  1049. int s2n_connection_set_dynamic_buffers(struct s2n_connection *conn, bool enabled)
  1050. {
  1051. POSIX_ENSURE_REF(conn);
  1052. conn->dynamic_buffers = enabled;
  1053. return S2N_SUCCESS;
  1054. }
  1055. int s2n_connection_set_dynamic_record_threshold(struct s2n_connection *conn, uint32_t resize_threshold, uint16_t timeout_threshold)
  1056. {
  1057. POSIX_ENSURE_REF(conn);
  1058. S2N_ERROR_IF(resize_threshold > S2N_TLS_MAX_RESIZE_THRESHOLD, S2N_ERR_INVALID_DYNAMIC_THRESHOLD);
  1059. conn->dynamic_record_resize_threshold = resize_threshold;
  1060. conn->dynamic_record_timeout_threshold = timeout_threshold;
  1061. return 0;
  1062. }
  1063. int s2n_connection_set_verify_host_callback(struct s2n_connection *conn, s2n_verify_host_fn verify_host_fn, void *data)
  1064. {
  1065. POSIX_ENSURE_REF(conn);
  1066. conn->verify_host_fn = verify_host_fn;
  1067. conn->data_for_verify_host = data;
  1068. conn->verify_host_fn_overridden = 1;
  1069. return 0;
  1070. }
  1071. int s2n_connection_recv_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len)
  1072. {
  1073. POSIX_ENSURE_REF(conn->recv);
  1074. /* Make sure we have enough space to write */
  1075. POSIX_GUARD(s2n_stuffer_reserve_space(stuffer, len));
  1076. int r = 0;
  1077. S2N_IO_RETRY_EINTR(r,
  1078. conn->recv(conn->recv_io_context, stuffer->blob.data + stuffer->write_cursor, len));
  1079. POSIX_ENSURE(r >= 0, S2N_ERR_RECV_STUFFER_FROM_CONN);
  1080. /* Record just how many bytes we have written */
  1081. POSIX_GUARD(s2n_stuffer_skip_write(stuffer, r));
  1082. return r;
  1083. }
  1084. int s2n_connection_send_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len)
  1085. {
  1086. POSIX_ENSURE_REF(conn);
  1087. POSIX_ENSURE_REF(conn->send);
  1088. if (conn->write_fd_broken) {
  1089. POSIX_BAIL(S2N_ERR_SEND_STUFFER_TO_CONN);
  1090. }
  1091. /* Make sure we even have the data */
  1092. S2N_ERROR_IF(s2n_stuffer_data_available(stuffer) < len, S2N_ERR_STUFFER_OUT_OF_DATA);
  1093. int w = 0;
  1094. S2N_IO_RETRY_EINTR(w,
  1095. conn->send(conn->send_io_context, stuffer->blob.data + stuffer->read_cursor, len));
  1096. if (w < 0 && errno == EPIPE) {
  1097. conn->write_fd_broken = 1;
  1098. }
  1099. POSIX_ENSURE(w >= 0, S2N_ERR_SEND_STUFFER_TO_CONN);
  1100. POSIX_GUARD(s2n_stuffer_skip_read(stuffer, w));
  1101. return w;
  1102. }
  1103. int s2n_connection_is_managed_corked(const struct s2n_connection *s2n_connection)
  1104. {
  1105. POSIX_ENSURE_REF(s2n_connection);
  1106. return (s2n_connection->managed_send_io && s2n_connection->corked_io);
  1107. }
  1108. const uint8_t *s2n_connection_get_sct_list(struct s2n_connection *conn, uint32_t *length)
  1109. {
  1110. if (!length) {
  1111. return NULL;
  1112. }
  1113. *length = conn->ct_response.size;
  1114. return conn->ct_response.data;
  1115. }
  1116. int s2n_connection_is_client_auth_enabled(struct s2n_connection *s2n_connection)
  1117. {
  1118. s2n_cert_auth_type auth_type;
  1119. POSIX_GUARD(s2n_connection_get_client_auth_type(s2n_connection, &auth_type));
  1120. return (auth_type != S2N_CERT_AUTH_NONE);
  1121. }
  1122. struct s2n_cert_chain_and_key *s2n_connection_get_selected_cert(struct s2n_connection *conn)
  1123. {
  1124. PTR_ENSURE_REF(conn);
  1125. return conn->handshake_params.our_chain_and_key;
  1126. }
  1127. uint8_t s2n_connection_get_protocol_version(const struct s2n_connection *conn)
  1128. {
  1129. if (conn == NULL) {
  1130. return S2N_UNKNOWN_PROTOCOL_VERSION;
  1131. }
  1132. if (conn->actual_protocol_version != S2N_UNKNOWN_PROTOCOL_VERSION) {
  1133. return conn->actual_protocol_version;
  1134. }
  1135. if (conn->mode == S2N_CLIENT) {
  1136. return conn->client_protocol_version;
  1137. }
  1138. return conn->server_protocol_version;
  1139. }
  1140. DEFINE_POINTER_CLEANUP_FUNC(struct s2n_cert_chain *, s2n_cert_chain_free);
  1141. int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain_and_key)
  1142. {
  1143. POSIX_ENSURE_REF(conn);
  1144. POSIX_ENSURE_REF(cert_chain_and_key);
  1145. POSIX_ENSURE_REF(cert_chain_and_key->cert_chain);
  1146. /* Ensure that cert_chain_and_key is empty BEFORE we modify it in any way.
  1147. * That includes before tying its cert_chain to DEFER_CLEANUP.
  1148. */
  1149. POSIX_ENSURE(cert_chain_and_key->cert_chain->head == NULL, S2N_ERR_INVALID_ARGUMENT);
  1150. DEFER_CLEANUP(struct s2n_cert_chain *cert_chain = cert_chain_and_key->cert_chain, s2n_cert_chain_free_pointer);
  1151. struct s2n_cert **insert = &cert_chain->head;
  1152. const struct s2n_x509_validator *validator = &conn->x509_validator;
  1153. POSIX_ENSURE_REF(validator);
  1154. POSIX_ENSURE(s2n_x509_validator_is_cert_chain_validated(validator), S2N_ERR_CERT_NOT_VALIDATED);
  1155. /* X509_STORE_CTX_get1_chain() returns a validated cert chain if a previous call to X509_verify_cert() was successful.
  1156. * X509_STORE_CTX_get0_chain() is a better API because it doesn't return a copy. But it's not available for Openssl 1.0.2.
  1157. * See the comments here:
  1158. * https://www.openssl.org/docs/man1.0.2/man3/X509_STORE_CTX_get1_chain.html
  1159. */
  1160. DEFER_CLEANUP(STACK_OF(X509) *cert_chain_validated = X509_STORE_CTX_get1_chain(validator->store_ctx),
  1161. s2n_openssl_x509_stack_pop_free);
  1162. POSIX_ENSURE_REF(cert_chain_validated);
  1163. int cert_count = sk_X509_num(cert_chain_validated);
  1164. POSIX_ENSURE_GTE(cert_count, 0);
  1165. for (size_t cert_idx = 0; cert_idx < (size_t) cert_count; cert_idx++) {
  1166. X509 *cert = sk_X509_value(cert_chain_validated, cert_idx);
  1167. POSIX_ENSURE_REF(cert);
  1168. DEFER_CLEANUP(uint8_t *cert_data = NULL, s2n_crypto_free);
  1169. int cert_size = i2d_X509(cert, &cert_data);
  1170. POSIX_ENSURE_GT(cert_size, 0);
  1171. struct s2n_blob mem = { 0 };
  1172. POSIX_GUARD(s2n_alloc(&mem, sizeof(struct s2n_cert)));
  1173. struct s2n_cert *new_node = (struct s2n_cert *) (void *) mem.data;
  1174. POSIX_ENSURE_REF(new_node);
  1175. new_node->next = NULL;
  1176. *insert = new_node;
  1177. insert = &new_node->next;
  1178. POSIX_GUARD(s2n_alloc(&new_node->raw, cert_size));
  1179. POSIX_CHECKED_MEMCPY(new_node->raw.data, cert_data, cert_size);
  1180. }
  1181. ZERO_TO_DISABLE_DEFER_CLEANUP(cert_chain);
  1182. return S2N_SUCCESS;
  1183. }
  1184. static S2N_RESULT s2n_signature_scheme_to_tls_iana(const struct s2n_signature_scheme *sig_scheme,
  1185. s2n_tls_hash_algorithm *converted_scheme)
  1186. {
  1187. RESULT_ENSURE_REF(sig_scheme);
  1188. RESULT_ENSURE_REF(converted_scheme);
  1189. switch (sig_scheme->hash_alg) {
  1190. case S2N_HASH_MD5:
  1191. *converted_scheme = S2N_TLS_HASH_MD5;
  1192. break;
  1193. case S2N_HASH_SHA1:
  1194. *converted_scheme = S2N_TLS_HASH_SHA1;
  1195. break;
  1196. case S2N_HASH_SHA224:
  1197. *converted_scheme = S2N_TLS_HASH_SHA224;
  1198. break;
  1199. case S2N_HASH_SHA256:
  1200. *converted_scheme = S2N_TLS_HASH_SHA256;
  1201. break;
  1202. case S2N_HASH_SHA384:
  1203. *converted_scheme = S2N_TLS_HASH_SHA384;
  1204. break;
  1205. case S2N_HASH_SHA512:
  1206. *converted_scheme = S2N_TLS_HASH_SHA512;
  1207. break;
  1208. case S2N_HASH_MD5_SHA1:
  1209. *converted_scheme = S2N_TLS_HASH_MD5_SHA1;
  1210. break;
  1211. default:
  1212. *converted_scheme = S2N_TLS_HASH_NONE;
  1213. break;
  1214. }
  1215. return S2N_RESULT_OK;
  1216. }
  1217. int s2n_connection_get_selected_digest_algorithm(struct s2n_connection *conn,
  1218. s2n_tls_hash_algorithm *converted_scheme)
  1219. {
  1220. POSIX_ENSURE_REF(conn);
  1221. POSIX_ENSURE_REF(converted_scheme);
  1222. POSIX_GUARD_RESULT(s2n_signature_scheme_to_tls_iana(
  1223. conn->handshake_params.server_cert_sig_scheme, converted_scheme));
  1224. return S2N_SUCCESS;
  1225. }
  1226. int s2n_connection_get_selected_client_cert_digest_algorithm(struct s2n_connection *conn,
  1227. s2n_tls_hash_algorithm *converted_scheme)
  1228. {
  1229. POSIX_ENSURE_REF(conn);
  1230. POSIX_ENSURE_REF(converted_scheme);
  1231. POSIX_GUARD_RESULT(s2n_signature_scheme_to_tls_iana(
  1232. conn->handshake_params.client_cert_sig_scheme, converted_scheme));
  1233. return S2N_SUCCESS;
  1234. }
  1235. static S2N_RESULT s2n_signature_scheme_to_signature_algorithm(const struct s2n_signature_scheme *sig_scheme,
  1236. s2n_tls_signature_algorithm *converted_scheme)
  1237. {
  1238. RESULT_ENSURE_REF(sig_scheme);
  1239. RESULT_ENSURE_REF(converted_scheme);
  1240. switch (sig_scheme->sig_alg) {
  1241. case S2N_SIGNATURE_RSA:
  1242. *converted_scheme = S2N_TLS_SIGNATURE_RSA;
  1243. break;
  1244. case S2N_SIGNATURE_ECDSA:
  1245. *converted_scheme = S2N_TLS_SIGNATURE_ECDSA;
  1246. break;
  1247. case S2N_SIGNATURE_RSA_PSS_RSAE:
  1248. *converted_scheme = S2N_TLS_SIGNATURE_RSA_PSS_RSAE;
  1249. break;
  1250. case S2N_SIGNATURE_RSA_PSS_PSS:
  1251. *converted_scheme = S2N_TLS_SIGNATURE_RSA_PSS_PSS;
  1252. break;
  1253. default:
  1254. *converted_scheme = S2N_TLS_SIGNATURE_ANONYMOUS;
  1255. break;
  1256. }
  1257. return S2N_RESULT_OK;
  1258. }
  1259. int s2n_connection_get_selected_signature_algorithm(struct s2n_connection *conn,
  1260. s2n_tls_signature_algorithm *converted_scheme)
  1261. {
  1262. POSIX_ENSURE_REF(conn);
  1263. POSIX_ENSURE_REF(converted_scheme);
  1264. POSIX_GUARD_RESULT(s2n_signature_scheme_to_signature_algorithm(
  1265. conn->handshake_params.server_cert_sig_scheme, converted_scheme));
  1266. return S2N_SUCCESS;
  1267. }
  1268. int s2n_connection_get_selected_client_cert_signature_algorithm(struct s2n_connection *conn,
  1269. s2n_tls_signature_algorithm *converted_scheme)
  1270. {
  1271. POSIX_ENSURE_REF(conn);
  1272. POSIX_ENSURE_REF(converted_scheme);
  1273. POSIX_GUARD_RESULT(s2n_signature_scheme_to_signature_algorithm(
  1274. conn->handshake_params.client_cert_sig_scheme, converted_scheme));
  1275. return S2N_SUCCESS;
  1276. }
  1277. /*
  1278. * Gets the config set on the connection.
  1279. */
  1280. int s2n_connection_get_config(struct s2n_connection *conn, struct s2n_config **config)
  1281. {
  1282. POSIX_ENSURE_REF(conn);
  1283. POSIX_ENSURE_REF(config);
  1284. if (s2n_fetch_default_config() == conn->config) {
  1285. POSIX_BAIL(S2N_ERR_NULL);
  1286. }
  1287. *config = conn->config;
  1288. return S2N_SUCCESS;
  1289. }
  1290. S2N_RESULT s2n_connection_dynamic_free_out_buffer(struct s2n_connection *conn)
  1291. {
  1292. RESULT_ENSURE_REF(conn);
  1293. /* free the out buffer if we're in dynamic mode and it's completely flushed */
  1294. if (conn->dynamic_buffers && s2n_stuffer_is_consumed(&conn->out)) {
  1295. /* since outgoing buffers are already encrypted, the buffers don't need to be zeroed, which saves some overhead */
  1296. RESULT_GUARD_POSIX(s2n_stuffer_free_without_wipe(&conn->out));
  1297. /* reset the stuffer to its initial state */
  1298. RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->out, 0));
  1299. }
  1300. return S2N_RESULT_OK;
  1301. }
  1302. S2N_RESULT s2n_connection_dynamic_free_in_buffer(struct s2n_connection *conn)
  1303. {
  1304. RESULT_ENSURE_REF(conn);
  1305. /* free the `in` buffer if we're in dynamic mode and it's completely flushed */
  1306. if (conn->dynamic_buffers && s2n_stuffer_is_consumed(&conn->in)) {
  1307. /* when copying the buffer into the application, we use `s2n_stuffer_erase_and_read`, which already zeroes the memory */
  1308. RESULT_GUARD_POSIX(s2n_stuffer_free_without_wipe(&conn->in));
  1309. /* reset the stuffer to its initial state */
  1310. RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->in, 0));
  1311. }
  1312. return S2N_RESULT_OK;
  1313. }
  1314. bool s2n_connection_check_io_status(struct s2n_connection *conn, s2n_io_status status)
  1315. {
  1316. if (!conn) {
  1317. return false;
  1318. }
  1319. bool read_closed = s2n_atomic_flag_test(&conn->read_closed);
  1320. bool write_closed = s2n_atomic_flag_test(&conn->write_closed);
  1321. bool full_duplex = !read_closed && !write_closed;
  1322. /*
  1323. *= https://tools.ietf.org/rfc/rfc8446#section-6.1
  1324. *# Note that this is a change from versions of TLS prior to TLS 1.3 in
  1325. *# which implementations were required to react to a "close_notify" by
  1326. *# discarding pending writes and sending an immediate "close_notify"
  1327. *# alert of their own.
  1328. */
  1329. if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
  1330. switch (status) {
  1331. case S2N_IO_WRITABLE:
  1332. case S2N_IO_READABLE:
  1333. case S2N_IO_FULL_DUPLEX:
  1334. return full_duplex;
  1335. case S2N_IO_CLOSED:
  1336. return !full_duplex;
  1337. }
  1338. }
  1339. switch (status) {
  1340. case S2N_IO_WRITABLE:
  1341. return !write_closed;
  1342. case S2N_IO_READABLE:
  1343. return !read_closed;
  1344. case S2N_IO_FULL_DUPLEX:
  1345. return full_duplex;
  1346. case S2N_IO_CLOSED:
  1347. return read_closed && write_closed;
  1348. }
  1349. return false;
  1350. }
  1351. S2N_RESULT s2n_connection_get_secure_cipher(struct s2n_connection *conn, const struct s2n_cipher **cipher)
  1352. {
  1353. RESULT_ENSURE_REF(conn);
  1354. RESULT_ENSURE_REF(cipher);
  1355. RESULT_ENSURE_REF(conn->secure);
  1356. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  1357. RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  1358. *cipher = conn->secure->cipher_suite->record_alg->cipher;
  1359. return S2N_RESULT_OK;
  1360. }