s2n_early_data.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_early_data.h"
  16. #include <sys/param.h>
  17. #include "tls/s2n_cipher_suites.h"
  18. #include "tls/s2n_connection.h"
  19. #include "tls/s2n_psk.h"
  20. #include "utils/s2n_mem.h"
  21. #include "utils/s2n_safety.h"
  22. const s2n_early_data_state valid_previous_states[] = {
  23. [S2N_EARLY_DATA_REQUESTED] = S2N_UNKNOWN_EARLY_DATA_STATE,
  24. [S2N_EARLY_DATA_NOT_REQUESTED] = S2N_UNKNOWN_EARLY_DATA_STATE,
  25. [S2N_EARLY_DATA_REJECTED] = S2N_EARLY_DATA_REQUESTED,
  26. [S2N_EARLY_DATA_ACCEPTED] = S2N_EARLY_DATA_REQUESTED,
  27. [S2N_END_OF_EARLY_DATA] = S2N_EARLY_DATA_ACCEPTED,
  28. };
  29. S2N_RESULT s2n_connection_set_early_data_state(struct s2n_connection *conn, s2n_early_data_state next_state)
  30. {
  31. RESULT_ENSURE_REF(conn);
  32. if (conn->early_data_state == next_state) {
  33. return S2N_RESULT_OK;
  34. }
  35. RESULT_ENSURE(next_state < S2N_EARLY_DATA_STATES_COUNT, S2N_ERR_INVALID_EARLY_DATA_STATE);
  36. RESULT_ENSURE(next_state != S2N_UNKNOWN_EARLY_DATA_STATE, S2N_ERR_INVALID_EARLY_DATA_STATE);
  37. RESULT_ENSURE(conn->early_data_state == valid_previous_states[next_state], S2N_ERR_INVALID_EARLY_DATA_STATE);
  38. conn->early_data_state = next_state;
  39. return S2N_RESULT_OK;
  40. }
  41. int s2n_connection_set_early_data_expected(struct s2n_connection *conn)
  42. {
  43. POSIX_ENSURE_REF(conn);
  44. conn->early_data_expected = true;
  45. return S2N_SUCCESS;
  46. }
  47. int s2n_connection_set_end_of_early_data(struct s2n_connection *conn)
  48. {
  49. POSIX_ENSURE_REF(conn);
  50. conn->early_data_expected = false;
  51. return S2N_SUCCESS;
  52. }
  53. static S2N_RESULT s2n_early_data_validate(struct s2n_connection *conn)
  54. {
  55. RESULT_ENSURE_REF(conn);
  56. RESULT_ENSURE_REF(conn->secure);
  57. /**
  58. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  59. *# In order to accept early data, the server MUST have accepted a PSK
  60. *# cipher suite and selected the first key offered in the client's
  61. *# "pre_shared_key" extension.
  62. **/
  63. RESULT_ENSURE_REF(conn->psk_params.chosen_psk);
  64. RESULT_ENSURE_EQ(conn->psk_params.chosen_psk_wire_index, 0);
  65. struct s2n_early_data_config *config = &conn->psk_params.chosen_psk->early_data_config;
  66. RESULT_ENSURE_GT(config->max_early_data_size, 0);
  67. /**
  68. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  69. *# In addition, it MUST verify that the
  70. *# following values are the same as those associated with the
  71. *# selected PSK:
  72. *#
  73. *# - The TLS version number
  74. **/
  75. RESULT_ENSURE_EQ(config->protocol_version, s2n_connection_get_protocol_version(conn));
  76. /**
  77. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  78. *# - The selected cipher suite
  79. **/
  80. RESULT_ENSURE_EQ(config->cipher_suite, conn->secure->cipher_suite);
  81. /**
  82. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  83. *# - The selected ALPN [RFC7301] protocol, if any
  84. **/
  85. const size_t app_protocol_size = strlen(conn->application_protocol);
  86. if (app_protocol_size > 0 || config->application_protocol.size > 0) {
  87. RESULT_ENSURE_EQ(config->application_protocol.size, app_protocol_size + 1 /* null-terminating char */);
  88. RESULT_ENSURE_EQ(memcmp(config->application_protocol.data, conn->application_protocol, app_protocol_size), 0);
  89. }
  90. return S2N_RESULT_OK;
  91. }
  92. bool s2n_early_data_is_valid_for_connection(struct s2n_connection *conn)
  93. {
  94. return s2n_result_is_ok(s2n_early_data_validate(conn));
  95. }
  96. S2N_RESULT s2n_early_data_accept_or_reject(struct s2n_connection *conn)
  97. {
  98. RESULT_ENSURE_REF(conn);
  99. if (conn->early_data_state != S2N_EARLY_DATA_REQUESTED) {
  100. return S2N_RESULT_OK;
  101. }
  102. if (conn->handshake.early_data_async_state.conn) {
  103. RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED);
  104. }
  105. /**
  106. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  107. *# If any of these checks fail, the server MUST NOT respond with the
  108. *# extension
  109. **/
  110. if (!s2n_early_data_is_valid_for_connection(conn)) {
  111. RESULT_GUARD(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REJECTED));
  112. return S2N_RESULT_OK;
  113. }
  114. /* Even if the connection is valid for early data, the client can't consider
  115. * early data accepted until the server sends the early data indication. */
  116. if (conn->mode == S2N_CLIENT) {
  117. return S2N_RESULT_OK;
  118. }
  119. /* The server should reject early data if the application is not prepared to handle it. */
  120. if (!conn->early_data_expected) {
  121. RESULT_GUARD(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REJECTED));
  122. return S2N_RESULT_OK;
  123. }
  124. /* If early data would otherwise be accepted, let the application apply any additional restrictions.
  125. * For example, an application could use this callback to implement anti-replay protections.
  126. *
  127. * This callback can be either synchronous or asynchronous. The handshake will not proceed until
  128. * the application either accepts or rejects early data.
  129. */
  130. RESULT_ENSURE_REF(conn->config);
  131. if (conn->config->early_data_cb) {
  132. conn->handshake.early_data_async_state.conn = conn;
  133. RESULT_ENSURE(conn->config->early_data_cb(conn, &conn->handshake.early_data_async_state) >= S2N_SUCCESS,
  134. S2N_ERR_CANCELLED);
  135. if (conn->early_data_state == S2N_EARLY_DATA_REQUESTED) {
  136. RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED);
  137. }
  138. } else {
  139. RESULT_GUARD(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_ACCEPTED));
  140. }
  141. return S2N_RESULT_OK;
  142. }
  143. int s2n_config_set_server_max_early_data_size(struct s2n_config *config, uint32_t max_early_data_size)
  144. {
  145. POSIX_ENSURE_REF(config);
  146. config->server_max_early_data_size = max_early_data_size;
  147. return S2N_SUCCESS;
  148. }
  149. int s2n_connection_set_server_max_early_data_size(struct s2n_connection *conn, uint32_t max_early_data_size)
  150. {
  151. POSIX_ENSURE_REF(conn);
  152. conn->server_max_early_data_size = max_early_data_size;
  153. conn->server_max_early_data_size_overridden = true;
  154. return S2N_SUCCESS;
  155. }
  156. S2N_RESULT s2n_early_data_get_server_max_size(struct s2n_connection *conn, uint32_t *max_early_data_size)
  157. {
  158. RESULT_ENSURE_REF(conn);
  159. RESULT_ENSURE_REF(max_early_data_size);
  160. if (conn->server_max_early_data_size_overridden) {
  161. *max_early_data_size = conn->server_max_early_data_size;
  162. } else {
  163. RESULT_ENSURE_REF(conn->config);
  164. *max_early_data_size = conn->config->server_max_early_data_size;
  165. }
  166. return S2N_RESULT_OK;
  167. }
  168. int s2n_connection_set_server_early_data_context(struct s2n_connection *conn, const uint8_t *context, uint16_t context_size)
  169. {
  170. POSIX_ENSURE_REF(conn);
  171. if (context_size > 0) {
  172. POSIX_ENSURE_REF(context);
  173. }
  174. POSIX_GUARD(s2n_realloc(&conn->server_early_data_context, context_size));
  175. POSIX_CHECKED_MEMCPY(conn->server_early_data_context.data, context, context_size);
  176. return S2N_SUCCESS;
  177. }
  178. S2N_CLEANUP_RESULT s2n_early_data_config_free(struct s2n_early_data_config *config)
  179. {
  180. if (config == NULL) {
  181. return S2N_RESULT_OK;
  182. }
  183. RESULT_GUARD_POSIX(s2n_free(&config->application_protocol));
  184. RESULT_GUARD_POSIX(s2n_free(&config->context));
  185. return S2N_RESULT_OK;
  186. }
  187. int s2n_psk_configure_early_data(struct s2n_psk *psk, uint32_t max_early_data_size,
  188. uint8_t cipher_suite_first_byte, uint8_t cipher_suite_second_byte)
  189. {
  190. POSIX_ENSURE_REF(psk);
  191. const uint8_t cipher_suite_iana[] = { cipher_suite_first_byte, cipher_suite_second_byte };
  192. struct s2n_cipher_suite *cipher_suite = NULL;
  193. POSIX_GUARD_RESULT(s2n_cipher_suite_from_iana(cipher_suite_iana, sizeof(cipher_suite_iana), &cipher_suite));
  194. POSIX_ENSURE_REF(cipher_suite);
  195. POSIX_ENSURE(cipher_suite->prf_alg == psk->hmac_alg, S2N_ERR_INVALID_ARGUMENT);
  196. psk->early_data_config.max_early_data_size = max_early_data_size;
  197. psk->early_data_config.protocol_version = S2N_TLS13;
  198. psk->early_data_config.cipher_suite = cipher_suite;
  199. return S2N_SUCCESS;
  200. }
  201. int s2n_psk_set_application_protocol(struct s2n_psk *psk, const uint8_t *application_protocol, uint8_t size)
  202. {
  203. POSIX_ENSURE_REF(psk);
  204. if (size > 0) {
  205. POSIX_ENSURE_REF(application_protocol);
  206. }
  207. struct s2n_blob *protocol_blob = &psk->early_data_config.application_protocol;
  208. POSIX_GUARD(s2n_realloc(protocol_blob, size));
  209. POSIX_CHECKED_MEMCPY(protocol_blob->data, application_protocol, size);
  210. return S2N_SUCCESS;
  211. }
  212. int s2n_psk_set_early_data_context(struct s2n_psk *psk, const uint8_t *context, uint16_t size)
  213. {
  214. POSIX_ENSURE_REF(psk);
  215. if (size > 0) {
  216. POSIX_ENSURE_REF(context);
  217. }
  218. struct s2n_blob *context_blob = &psk->early_data_config.context;
  219. POSIX_GUARD(s2n_realloc(context_blob, size));
  220. POSIX_CHECKED_MEMCPY(context_blob->data, context, size);
  221. return S2N_SUCCESS;
  222. }
  223. S2N_RESULT s2n_early_data_config_clone(struct s2n_psk *new_psk, struct s2n_early_data_config *old_config)
  224. {
  225. RESULT_ENSURE_REF(old_config);
  226. RESULT_ENSURE_REF(new_psk);
  227. struct s2n_early_data_config config_copy = new_psk->early_data_config;
  228. /* Copy all fields from the old_config EXCEPT the blobs, which we need to reallocate. */
  229. new_psk->early_data_config = *old_config;
  230. new_psk->early_data_config.application_protocol = config_copy.application_protocol;
  231. new_psk->early_data_config.context = config_copy.context;
  232. /* Clone / realloc blobs */
  233. RESULT_GUARD_POSIX(s2n_psk_set_application_protocol(new_psk, old_config->application_protocol.data,
  234. old_config->application_protocol.size));
  235. RESULT_GUARD_POSIX(s2n_psk_set_early_data_context(new_psk, old_config->context.data,
  236. old_config->context.size));
  237. return S2N_RESULT_OK;
  238. }
  239. int s2n_connection_get_early_data_status(struct s2n_connection *conn, s2n_early_data_status_t *status)
  240. {
  241. POSIX_ENSURE_REF(conn);
  242. POSIX_ENSURE_REF(status);
  243. switch (conn->early_data_state) {
  244. case S2N_EARLY_DATA_STATES_COUNT:
  245. break;
  246. case S2N_EARLY_DATA_NOT_REQUESTED:
  247. *status = S2N_EARLY_DATA_STATUS_NOT_REQUESTED;
  248. return S2N_SUCCESS;
  249. case S2N_EARLY_DATA_REJECTED:
  250. *status = S2N_EARLY_DATA_STATUS_REJECTED;
  251. return S2N_SUCCESS;
  252. case S2N_END_OF_EARLY_DATA:
  253. *status = S2N_EARLY_DATA_STATUS_END;
  254. return S2N_SUCCESS;
  255. case S2N_UNKNOWN_EARLY_DATA_STATE:
  256. case S2N_EARLY_DATA_REQUESTED:
  257. case S2N_EARLY_DATA_ACCEPTED:
  258. *status = S2N_EARLY_DATA_STATUS_OK;
  259. return S2N_SUCCESS;
  260. }
  261. POSIX_BAIL(S2N_ERR_INVALID_EARLY_DATA_STATE);
  262. }
  263. static S2N_RESULT s2n_get_remaining_early_data_bytes(struct s2n_connection *conn, uint32_t *early_data_allowed)
  264. {
  265. RESULT_ENSURE_REF(conn);
  266. RESULT_ENSURE_REF(early_data_allowed);
  267. *early_data_allowed = 0;
  268. uint32_t max_early_data_size = 0;
  269. RESULT_GUARD_POSIX(s2n_connection_get_max_early_data_size(conn, &max_early_data_size));
  270. RESULT_ENSURE(max_early_data_size >= conn->early_data_bytes, S2N_ERR_MAX_EARLY_DATA_SIZE);
  271. *early_data_allowed = (max_early_data_size - conn->early_data_bytes);
  272. return S2N_RESULT_OK;
  273. }
  274. int s2n_connection_get_remaining_early_data_size(struct s2n_connection *conn, uint32_t *allowed_early_data_size)
  275. {
  276. POSIX_ENSURE_REF(conn);
  277. POSIX_ENSURE_REF(allowed_early_data_size);
  278. *allowed_early_data_size = 0;
  279. switch (conn->early_data_state) {
  280. case S2N_EARLY_DATA_STATES_COUNT:
  281. case S2N_EARLY_DATA_NOT_REQUESTED:
  282. case S2N_EARLY_DATA_REJECTED:
  283. case S2N_END_OF_EARLY_DATA:
  284. *allowed_early_data_size = 0;
  285. break;
  286. case S2N_UNKNOWN_EARLY_DATA_STATE:
  287. case S2N_EARLY_DATA_REQUESTED:
  288. case S2N_EARLY_DATA_ACCEPTED:
  289. POSIX_GUARD_RESULT(s2n_get_remaining_early_data_bytes(conn, allowed_early_data_size));
  290. break;
  291. }
  292. return S2N_SUCCESS;
  293. }
  294. int s2n_connection_get_max_early_data_size(struct s2n_connection *conn, uint32_t *max_early_data_size)
  295. {
  296. POSIX_ENSURE_REF(conn);
  297. POSIX_ENSURE_REF(max_early_data_size);
  298. *max_early_data_size = 0;
  299. uint32_t server_max_early_data_size = 0;
  300. POSIX_GUARD_RESULT(s2n_early_data_get_server_max_size(conn, &server_max_early_data_size));
  301. if (conn->psk_params.psk_list.len == 0) {
  302. /* This method may be called by the server before loading its PSKs.
  303. * The server can load its PSKs during the handshake, either via the PSK selection callback
  304. * or by receiving a stateless session ticket.
  305. *
  306. * Before that happens, we should make an optimistic assumption of the early data size.
  307. * That way, the max early data size always decreases (for example, it won't go from 0 -> UINT32_MAX
  308. * after receiving a PSK in the ClientHello).
  309. */
  310. if (conn->mode == S2N_SERVER && !IS_NEGOTIATED(conn)) {
  311. *max_early_data_size = server_max_early_data_size;
  312. }
  313. return S2N_SUCCESS;
  314. }
  315. struct s2n_psk *first_psk = NULL;
  316. POSIX_GUARD_RESULT(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &first_psk));
  317. POSIX_ENSURE_REF(first_psk);
  318. *max_early_data_size = first_psk->early_data_config.max_early_data_size;
  319. /* For the server, we should use the minimum of the limit retrieved from the ticket
  320. * and the current limit being set for new tickets.
  321. *
  322. * This is defensive: even if more early data was previously allowed, the server may not be
  323. * willing or able to handle that much early data now.
  324. *
  325. * We don't do this for external PSKs because the server has intentionally set the limit
  326. * while setting up this connection, not during a previous connection.
  327. */
  328. if (conn->mode == S2N_SERVER && first_psk->type == S2N_PSK_TYPE_RESUMPTION) {
  329. *max_early_data_size = MIN(*max_early_data_size, server_max_early_data_size);
  330. }
  331. return S2N_SUCCESS;
  332. }
  333. int s2n_config_set_early_data_cb(struct s2n_config *config, s2n_early_data_cb cb)
  334. {
  335. POSIX_ENSURE_REF(config);
  336. config->early_data_cb = cb;
  337. return S2N_SUCCESS;
  338. }
  339. int s2n_offered_early_data_get_context_length(struct s2n_offered_early_data *early_data, uint16_t *context_len)
  340. {
  341. POSIX_ENSURE_REF(context_len);
  342. POSIX_ENSURE_REF(early_data);
  343. struct s2n_connection *conn = early_data->conn;
  344. POSIX_ENSURE_REF(conn);
  345. POSIX_ENSURE_REF(conn->psk_params.chosen_psk);
  346. struct s2n_early_data_config *early_data_config = &conn->psk_params.chosen_psk->early_data_config;
  347. *context_len = early_data_config->context.size;
  348. return S2N_SUCCESS;
  349. }
  350. int s2n_offered_early_data_get_context(struct s2n_offered_early_data *early_data, uint8_t *context, uint16_t max_len)
  351. {
  352. POSIX_ENSURE_REF(context);
  353. POSIX_ENSURE_REF(early_data);
  354. struct s2n_connection *conn = early_data->conn;
  355. POSIX_ENSURE_REF(conn);
  356. POSIX_ENSURE_REF(conn->psk_params.chosen_psk);
  357. struct s2n_early_data_config *early_data_config = &conn->psk_params.chosen_psk->early_data_config;
  358. POSIX_ENSURE(early_data_config->context.size <= max_len, S2N_ERR_INSUFFICIENT_MEM_SIZE);
  359. POSIX_CHECKED_MEMCPY(context, early_data_config->context.data, early_data_config->context.size);
  360. return S2N_SUCCESS;
  361. }
  362. int s2n_offered_early_data_reject(struct s2n_offered_early_data *early_data)
  363. {
  364. POSIX_ENSURE_REF(early_data);
  365. struct s2n_connection *conn = early_data->conn;
  366. POSIX_ENSURE_REF(conn);
  367. POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REJECTED));
  368. return S2N_SUCCESS;
  369. }
  370. int s2n_offered_early_data_accept(struct s2n_offered_early_data *early_data)
  371. {
  372. POSIX_ENSURE_REF(early_data);
  373. struct s2n_connection *conn = early_data->conn;
  374. POSIX_ENSURE_REF(conn);
  375. POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_ACCEPTED));
  376. return S2N_SUCCESS;
  377. }