s2n_client_psk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/extensions/s2n_client_psk.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "crypto/s2n_hash.h"
  19. #include "tls/s2n_psk.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls_parameters.h"
  22. #include "utils/s2n_bitmap.h"
  23. #include "utils/s2n_safety.h"
  24. #define SIZE_OF_BINDER_SIZE sizeof(uint8_t)
  25. #define SIZE_OF_BINDER_LIST_SIZE sizeof(uint16_t)
  26. /* To avoid a DoS attack triggered by decrypting too many session tickets,
  27. * set a limit on the number of tickets we will attempt to decrypt before giving up.
  28. * We may want to make this configurable someday, but just set a reasonable maximum for now. */
  29. #define MAX_REJECTED_TICKETS 3
  30. static int s2n_client_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  31. static int s2n_client_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  32. static int s2n_client_psk_is_missing(struct s2n_connection *conn);
  33. const s2n_extension_type s2n_client_psk_extension = {
  34. .iana_value = TLS_EXTENSION_PRE_SHARED_KEY,
  35. .minimum_version = S2N_TLS13,
  36. .is_response = false,
  37. .send = s2n_client_psk_send,
  38. .recv = s2n_client_psk_recv,
  39. .should_send = s2n_client_psk_should_send,
  40. .if_missing = s2n_client_psk_is_missing,
  41. };
  42. int s2n_client_psk_is_missing(struct s2n_connection *conn)
  43. {
  44. POSIX_ENSURE_REF(conn);
  45. /* If the PSK extension is missing, we must not have received
  46. * a request for early data.
  47. *
  48. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  49. *# When a PSK is used and early data is allowed for that PSK, the client
  50. *# can send Application Data in its first flight of messages. If the
  51. *# client opts to do so, it MUST supply both the "pre_shared_key" and
  52. *# "early_data" extensions.
  53. */
  54. POSIX_ENSURE(conn->early_data_state != S2N_EARLY_DATA_REQUESTED, S2N_ERR_UNSUPPORTED_EXTENSION);
  55. return S2N_SUCCESS;
  56. }
  57. bool s2n_client_psk_should_send(struct s2n_connection *conn)
  58. {
  59. if (!conn || !conn->secure) {
  60. return false;
  61. }
  62. /* If this is NOT the second ClientHello after a retry, then all PSKs are viable.
  63. * Send the extension if any PSKs are configured.
  64. */
  65. if (!s2n_is_hello_retry_handshake(conn)) {
  66. return conn->psk_params.psk_list.len > 0;
  67. }
  68. /* If this is the second ClientHello after a retry, then only PSKs that match the cipher suite
  69. * are viable. Only send the extension if at least one configured PSK matches the cipher suite.
  70. */
  71. for (size_t i = 0; i < conn->psk_params.psk_list.len; i++) {
  72. struct s2n_psk *psk = NULL;
  73. if (s2n_result_is_ok(s2n_array_get(&conn->psk_params.psk_list, i, (void **) &psk))
  74. && psk != NULL
  75. && conn->secure->cipher_suite->prf_alg == psk->hmac_alg) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11.1
  83. *# The "obfuscated_ticket_age"
  84. *# field of each PskIdentity contains an obfuscated version of the
  85. *# ticket age formed by taking the age in milliseconds and adding the
  86. *# "ticket_age_add" value that was included with the ticket (see
  87. *# Section 4.6.1), modulo 2^32.
  88. */
  89. static S2N_RESULT s2n_generate_obfuscated_ticket_age(struct s2n_psk *psk, uint64_t current_time, uint32_t *output)
  90. {
  91. RESULT_ENSURE_REF(psk);
  92. RESULT_ENSURE_MUT(output);
  93. /**
  94. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  95. *# For identities
  96. *# established externally, an obfuscated_ticket_age of 0 SHOULD be
  97. *# used,
  98. **/
  99. if (psk->type == S2N_PSK_TYPE_EXTERNAL) {
  100. *output = 0;
  101. return S2N_RESULT_OK;
  102. }
  103. RESULT_ENSURE(current_time >= psk->ticket_issue_time, S2N_ERR_SAFETY);
  104. /* Calculate ticket age */
  105. uint64_t ticket_age_in_nanos = current_time - psk->ticket_issue_time;
  106. /* Convert ticket age to milliseconds */
  107. uint64_t ticket_age_in_millis = ticket_age_in_nanos / ONE_MILLISEC_IN_NANOS;
  108. RESULT_ENSURE(ticket_age_in_millis <= UINT32_MAX, S2N_ERR_SAFETY);
  109. /* Add the ticket_age_add value to the ticket age in milliseconds. The resulting uint32_t value
  110. * may wrap, resulting in the modulo 2^32 operation. */
  111. *output = ticket_age_in_millis + psk->ticket_age_add;
  112. return S2N_RESULT_OK;
  113. }
  114. static int s2n_client_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  115. {
  116. POSIX_ENSURE_REF(conn);
  117. POSIX_ENSURE_REF(conn->secure);
  118. struct s2n_psk_parameters *psk_params = &conn->psk_params;
  119. struct s2n_array *psk_list = &psk_params->psk_list;
  120. struct s2n_stuffer_reservation identity_list_size;
  121. POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &identity_list_size));
  122. uint16_t binder_list_size = SIZE_OF_BINDER_LIST_SIZE;
  123. for (size_t i = 0; i < psk_list->len; i++) {
  124. struct s2n_psk *psk = NULL;
  125. POSIX_GUARD_RESULT(s2n_array_get(psk_list, i, (void **) &psk));
  126. POSIX_ENSURE_REF(psk);
  127. /**
  128. *= https://tools.ietf.org/rfc/rfc8446#section-4.1.4
  129. *# In addition, in its updated ClientHello, the client SHOULD NOT offer
  130. *# any pre-shared keys associated with a hash other than that of the
  131. *# selected cipher suite.
  132. */
  133. if (s2n_is_hello_retry_handshake(conn) && conn->secure->cipher_suite->prf_alg != psk->hmac_alg) {
  134. continue;
  135. }
  136. /* Write the identity */
  137. POSIX_GUARD(s2n_stuffer_write_uint16(out, psk->identity.size));
  138. POSIX_GUARD(s2n_stuffer_write(out, &psk->identity));
  139. /* Write obfuscated ticket age */
  140. uint32_t obfuscated_ticket_age = 0;
  141. uint64_t current_time = 0;
  142. POSIX_GUARD_RESULT(s2n_config_wall_clock(conn->config, &current_time));
  143. POSIX_GUARD_RESULT(s2n_generate_obfuscated_ticket_age(psk, current_time, &obfuscated_ticket_age));
  144. POSIX_GUARD(s2n_stuffer_write_uint32(out, obfuscated_ticket_age));
  145. /* Calculate binder size */
  146. uint8_t hash_size = 0;
  147. POSIX_GUARD(s2n_hmac_digest_size(psk->hmac_alg, &hash_size));
  148. binder_list_size += hash_size + SIZE_OF_BINDER_SIZE;
  149. }
  150. POSIX_GUARD(s2n_stuffer_write_vector_size(&identity_list_size));
  151. /* Calculating the binders requires a complete ClientHello, and at this point
  152. * the extension size, extension list size, and message size are all blank.
  153. *
  154. * We'll write placeholder data to ensure the extension and extension list sizes
  155. * are calculated correctly, then rewrite the binders with real data later. */
  156. psk_params->binder_list_size = binder_list_size;
  157. POSIX_GUARD(s2n_stuffer_skip_write(out, binder_list_size));
  158. return S2N_SUCCESS;
  159. }
  160. /* Find the first of the server's PSK identities that matches the client's identities.
  161. * This method compares all server identities to all client identities.
  162. *
  163. * While both the client's identities and whether a match was found are public, we should make an attempt
  164. * to keep the server's identities a secret. We will make comparisons to the server's identities constant
  165. * time (to hide partial matches) and not end the search early when a match is found (to hide the ordering).
  166. *
  167. * Keeping these comparisons constant time is not high priority. There's no known attack using these timings,
  168. * and an attacker could probably guess the server's known identities just by observing the public identities
  169. * sent by clients.
  170. */
  171. static S2N_RESULT s2n_select_external_psk(struct s2n_connection *conn, struct s2n_offered_psk_list *client_identity_list)
  172. {
  173. RESULT_ENSURE_REF(conn);
  174. RESULT_ENSURE_REF(client_identity_list);
  175. struct s2n_array *server_psks = &conn->psk_params.psk_list;
  176. conn->psk_params.chosen_psk = NULL;
  177. for (size_t i = 0; i < server_psks->len; i++) {
  178. struct s2n_psk *server_psk = NULL;
  179. RESULT_GUARD(s2n_array_get(server_psks, i, (void **) &server_psk));
  180. RESULT_ENSURE_REF(server_psk);
  181. struct s2n_offered_psk client_psk = { 0 };
  182. uint16_t wire_index = 0;
  183. RESULT_GUARD_POSIX(s2n_offered_psk_list_reread(client_identity_list));
  184. while (s2n_offered_psk_list_has_next(client_identity_list)) {
  185. RESULT_GUARD_POSIX(s2n_offered_psk_list_next(client_identity_list, &client_psk));
  186. uint16_t compare_size = MIN(client_psk.identity.size, server_psk->identity.size);
  187. if (s2n_constant_time_equals(client_psk.identity.data, server_psk->identity.data, compare_size)
  188. & (client_psk.identity.size == server_psk->identity.size)
  189. & (conn->psk_params.chosen_psk == NULL)) {
  190. conn->psk_params.chosen_psk = server_psk;
  191. conn->psk_params.chosen_psk_wire_index = wire_index;
  192. }
  193. wire_index++;
  194. };
  195. }
  196. RESULT_ENSURE_REF(conn->psk_params.chosen_psk);
  197. return S2N_RESULT_OK;
  198. }
  199. static S2N_RESULT s2n_select_resumption_psk(struct s2n_connection *conn, struct s2n_offered_psk_list *client_identity_list)
  200. {
  201. RESULT_ENSURE_REF(conn);
  202. RESULT_ENSURE_REF(client_identity_list);
  203. struct s2n_offered_psk client_psk = { 0 };
  204. conn->psk_params.chosen_psk = NULL;
  205. uint8_t rejected_count = 0;
  206. while (s2n_offered_psk_list_has_next(client_identity_list) && (rejected_count < MAX_REJECTED_TICKETS)) {
  207. RESULT_GUARD_POSIX(s2n_offered_psk_list_next(client_identity_list, &client_psk));
  208. /* Select the first resumption PSK that can be decrypted */
  209. if (s2n_offered_psk_list_choose_psk(client_identity_list, &client_psk) == S2N_SUCCESS) {
  210. return S2N_RESULT_OK;
  211. }
  212. rejected_count++;
  213. }
  214. RESULT_BAIL(S2N_ERR_INVALID_SESSION_TICKET);
  215. }
  216. static S2N_RESULT s2n_client_psk_recv_identity_list(struct s2n_connection *conn, struct s2n_stuffer *wire_identities_in)
  217. {
  218. RESULT_ENSURE_REF(conn);
  219. RESULT_ENSURE_REF(conn->config);
  220. RESULT_ENSURE_REF(wire_identities_in);
  221. struct s2n_offered_psk_list identity_list = {
  222. .conn = conn,
  223. .wire_data = *wire_identities_in,
  224. };
  225. if (conn->config->psk_selection_cb) {
  226. RESULT_GUARD_POSIX(conn->config->psk_selection_cb(conn, conn->config->psk_selection_ctx, &identity_list));
  227. } else if (conn->psk_params.type == S2N_PSK_TYPE_EXTERNAL) {
  228. RESULT_GUARD(s2n_select_external_psk(conn, &identity_list));
  229. } else if (conn->psk_params.type == S2N_PSK_TYPE_RESUMPTION) {
  230. RESULT_GUARD(s2n_select_resumption_psk(conn, &identity_list));
  231. }
  232. RESULT_ENSURE_REF(conn->psk_params.chosen_psk);
  233. return S2N_RESULT_OK;
  234. }
  235. static S2N_RESULT s2n_client_psk_recv_binder_list(struct s2n_connection *conn, struct s2n_blob *partial_client_hello,
  236. struct s2n_stuffer *wire_binders_in)
  237. {
  238. RESULT_ENSURE_REF(conn);
  239. RESULT_ENSURE_REF(wire_binders_in);
  240. uint16_t wire_index = 0;
  241. while (s2n_stuffer_data_available(wire_binders_in) > 0) {
  242. uint8_t wire_binder_size = 0;
  243. RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(wire_binders_in, &wire_binder_size));
  244. uint8_t *wire_binder_data;
  245. RESULT_ENSURE_REF(wire_binder_data = s2n_stuffer_raw_read(wire_binders_in, wire_binder_size));
  246. struct s2n_blob wire_binder = { 0 };
  247. RESULT_GUARD_POSIX(s2n_blob_init(&wire_binder, wire_binder_data, wire_binder_size));
  248. if (wire_index == conn->psk_params.chosen_psk_wire_index) {
  249. RESULT_GUARD_POSIX(s2n_psk_verify_binder(conn, conn->psk_params.chosen_psk,
  250. partial_client_hello, &wire_binder));
  251. return S2N_RESULT_OK;
  252. }
  253. wire_index++;
  254. }
  255. RESULT_BAIL(S2N_ERR_BAD_MESSAGE);
  256. }
  257. static S2N_RESULT s2n_client_psk_recv_identities(struct s2n_connection *conn, struct s2n_stuffer *extension)
  258. {
  259. RESULT_ENSURE_REF(conn);
  260. uint16_t identity_list_size = 0;
  261. RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &identity_list_size));
  262. uint8_t *identity_list_data;
  263. RESULT_ENSURE_REF(identity_list_data = s2n_stuffer_raw_read(extension, identity_list_size));
  264. struct s2n_blob identity_list_blob = { 0 };
  265. RESULT_GUARD_POSIX(s2n_blob_init(&identity_list_blob, identity_list_data, identity_list_size));
  266. struct s2n_stuffer identity_list = { 0 };
  267. RESULT_GUARD_POSIX(s2n_stuffer_init(&identity_list, &identity_list_blob));
  268. RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&identity_list, identity_list_blob.size));
  269. return s2n_client_psk_recv_identity_list(conn, &identity_list);
  270. }
  271. static S2N_RESULT s2n_client_psk_recv_binders(struct s2n_connection *conn, struct s2n_stuffer *extension)
  272. {
  273. RESULT_ENSURE_REF(conn);
  274. uint16_t binder_list_size = 0;
  275. RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &binder_list_size));
  276. uint8_t *binder_list_data;
  277. RESULT_ENSURE_REF(binder_list_data = s2n_stuffer_raw_read(extension, binder_list_size));
  278. struct s2n_blob binder_list_blob = { 0 };
  279. RESULT_GUARD_POSIX(s2n_blob_init(&binder_list_blob, binder_list_data, binder_list_size));
  280. struct s2n_stuffer binder_list = { 0 };
  281. RESULT_GUARD_POSIX(s2n_stuffer_init(&binder_list, &binder_list_blob));
  282. RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&binder_list, binder_list_blob.size));
  283. /* Record the ClientHello message up to but not including the binder list.
  284. * This is required to calculate the binder for the chosen PSK. */
  285. struct s2n_blob partial_client_hello = { 0 };
  286. const struct s2n_stuffer *client_hello = &conn->handshake.io;
  287. uint32_t binders_size = binder_list_blob.size + SIZE_OF_BINDER_LIST_SIZE;
  288. RESULT_ENSURE_GTE(client_hello->write_cursor, binders_size);
  289. uint16_t partial_client_hello_size = client_hello->write_cursor - binders_size;
  290. RESULT_GUARD_POSIX(s2n_blob_slice(&client_hello->blob, &partial_client_hello, 0, partial_client_hello_size));
  291. return s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &binder_list);
  292. }
  293. int s2n_client_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  294. {
  295. POSIX_ENSURE_REF(conn);
  296. /**
  297. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  298. *# The "pre_shared_key" extension MUST be the last extension in the
  299. *# ClientHello (this facilitates implementation as described below).
  300. *# Servers MUST check that it is the last extension and otherwise fail
  301. *# the handshake with an "illegal_parameter" alert.
  302. */
  303. s2n_extension_type_id psk_ext_id;
  304. POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PRE_SHARED_KEY, &psk_ext_id));
  305. POSIX_ENSURE_NE(conn->client_hello.extensions.count, 0);
  306. uint16_t last_wire_index = conn->client_hello.extensions.count - 1;
  307. uint16_t extension_wire_index = conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index;
  308. POSIX_ENSURE(extension_wire_index == last_wire_index, S2N_ERR_UNSUPPORTED_EXTENSION);
  309. /**
  310. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.9
  311. *# If clients offer "pre_shared_key" without a "psk_key_exchange_modes" extension,
  312. *# servers MUST abort the handshake.
  313. *
  314. * We can safely do this check here because s2n_client_psk is
  315. * required to be the last extension sent in the list.
  316. */
  317. s2n_extension_type_id psk_ke_mode_ext_id;
  318. POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_ext_id));
  319. POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_requests_received, psk_ke_mode_ext_id), S2N_ERR_MISSING_EXTENSION);
  320. if (conn->psk_params.psk_ke_mode == S2N_PSK_DHE_KE) {
  321. s2n_extension_type_id key_share_ext_id;
  322. POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_ext_id));
  323. /* A key_share extension must have been received in order to use a pre-shared key
  324. * in (EC)DHE key exchange mode.
  325. */
  326. POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_requests_received, key_share_ext_id), S2N_ERR_MISSING_EXTENSION);
  327. } else {
  328. /* s2n currently only supports pre-shared keys in (EC)DHE key exchange mode. If we receive keys with any other
  329. * exchange mode we fall back to a full handshake.
  330. */
  331. return S2N_SUCCESS;
  332. }
  333. if (s2n_result_is_error(s2n_client_psk_recv_identities(conn, extension))) {
  334. /**
  335. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  336. *# If no acceptable PSKs are found, the server SHOULD perform a non-PSK
  337. *# handshake if possible.
  338. */
  339. conn->psk_params.chosen_psk = NULL;
  340. }
  341. if (conn->psk_params.chosen_psk) {
  342. /**
  343. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  344. *# Prior to accepting PSK key establishment, the server MUST validate
  345. *# the corresponding binder value (see Section 4.2.11.2 below). If this
  346. *# value is not present or does not validate, the server MUST abort the
  347. *# handshake.
  348. */
  349. POSIX_GUARD_RESULT(s2n_client_psk_recv_binders(conn, extension));
  350. }
  351. /* At this point, we have either chosen a PSK or fallen back to a full handshake. */
  352. return S2N_SUCCESS;
  353. }