s2n_psk.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 <sys/param.h>
  16. #include "crypto/s2n_tls13_keys.h"
  17. #include "tls/extensions/s2n_extension_type.h"
  18. #include "tls/s2n_handshake.h"
  19. #include "tls/s2n_tls.h"
  20. #include "tls/s2n_tls13_handshake.h"
  21. #include "tls/s2n_tls13_secrets.h"
  22. #include "utils/s2n_array.h"
  23. #include "utils/s2n_mem.h"
  24. #include "utils/s2n_safety.h"
  25. #define S2N_HASH_ALG_COUNT S2N_HASH_SENTINEL
  26. S2N_RESULT s2n_psk_init(struct s2n_psk *psk, s2n_psk_type type)
  27. {
  28. RESULT_ENSURE_MUT(psk);
  29. RESULT_CHECKED_MEMSET(psk, 0, sizeof(struct s2n_psk));
  30. psk->hmac_alg = S2N_HMAC_SHA256;
  31. psk->type = type;
  32. return S2N_RESULT_OK;
  33. }
  34. struct s2n_psk *s2n_external_psk_new()
  35. {
  36. DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
  37. PTR_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_psk)));
  38. struct s2n_psk *psk = (struct s2n_psk *) (void *) mem.data;
  39. PTR_GUARD_RESULT(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL));
  40. ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
  41. return psk;
  42. }
  43. int s2n_psk_set_identity(struct s2n_psk *psk, const uint8_t *identity, uint16_t identity_size)
  44. {
  45. POSIX_ENSURE_REF(psk);
  46. POSIX_ENSURE_REF(identity);
  47. POSIX_ENSURE(identity_size != 0, S2N_ERR_INVALID_ARGUMENT);
  48. POSIX_GUARD(s2n_realloc(&psk->identity, identity_size));
  49. POSIX_CHECKED_MEMCPY(psk->identity.data, identity, identity_size);
  50. return S2N_SUCCESS;
  51. }
  52. int s2n_psk_set_secret(struct s2n_psk *psk, const uint8_t *secret, uint16_t secret_size)
  53. {
  54. POSIX_ENSURE_REF(psk);
  55. POSIX_ENSURE_REF(secret);
  56. POSIX_ENSURE(secret_size != 0, S2N_ERR_INVALID_ARGUMENT);
  57. POSIX_GUARD(s2n_realloc(&psk->secret, secret_size));
  58. POSIX_CHECKED_MEMCPY(psk->secret.data, secret, secret_size);
  59. return S2N_SUCCESS;
  60. }
  61. S2N_RESULT s2n_psk_clone(struct s2n_psk *new_psk, struct s2n_psk *original_psk)
  62. {
  63. if (original_psk == NULL) {
  64. return S2N_RESULT_OK;
  65. }
  66. RESULT_ENSURE_REF(new_psk);
  67. struct s2n_psk psk_copy = *new_psk;
  68. /* Copy all fields from the old_config EXCEPT the blobs, which we need to reallocate. */
  69. *new_psk = *original_psk;
  70. new_psk->identity = psk_copy.identity;
  71. new_psk->secret = psk_copy.secret;
  72. new_psk->early_secret = psk_copy.early_secret;
  73. new_psk->early_data_config = psk_copy.early_data_config;
  74. /* Clone / realloc blobs */
  75. RESULT_GUARD_POSIX(s2n_psk_set_identity(new_psk, original_psk->identity.data, original_psk->identity.size));
  76. RESULT_GUARD_POSIX(s2n_psk_set_secret(new_psk, original_psk->secret.data, original_psk->secret.size));
  77. RESULT_GUARD_POSIX(s2n_realloc(&new_psk->early_secret, original_psk->early_secret.size));
  78. RESULT_CHECKED_MEMCPY(new_psk->early_secret.data, original_psk->early_secret.data, original_psk->early_secret.size);
  79. RESULT_GUARD(s2n_early_data_config_clone(new_psk, &original_psk->early_data_config));
  80. return S2N_RESULT_OK;
  81. }
  82. S2N_CLEANUP_RESULT s2n_psk_wipe(struct s2n_psk *psk)
  83. {
  84. if (psk == NULL) {
  85. return S2N_RESULT_OK;
  86. }
  87. RESULT_GUARD_POSIX(s2n_free(&psk->early_secret));
  88. RESULT_GUARD_POSIX(s2n_free(&psk->identity));
  89. RESULT_GUARD_POSIX(s2n_free(&psk->secret));
  90. RESULT_GUARD(s2n_early_data_config_free(&psk->early_data_config));
  91. return S2N_RESULT_OK;
  92. }
  93. int s2n_psk_free(struct s2n_psk **psk)
  94. {
  95. if (psk == NULL) {
  96. return S2N_SUCCESS;
  97. }
  98. POSIX_GUARD_RESULT(s2n_psk_wipe(*psk));
  99. return s2n_free_object((uint8_t **) psk, sizeof(struct s2n_psk));
  100. }
  101. S2N_RESULT s2n_psk_parameters_init(struct s2n_psk_parameters *params)
  102. {
  103. RESULT_ENSURE_REF(params);
  104. RESULT_CHECKED_MEMSET(params, 0, sizeof(struct s2n_psk_parameters));
  105. RESULT_GUARD(s2n_array_init(&params->psk_list, sizeof(struct s2n_psk)));
  106. return S2N_RESULT_OK;
  107. }
  108. static S2N_RESULT s2n_psk_offered_psk_size(struct s2n_psk *psk, uint32_t *size)
  109. {
  110. *size = sizeof(uint16_t) /* identity size */
  111. + sizeof(uint32_t) /* obfuscated ticket age */
  112. + sizeof(uint8_t); /* binder size */
  113. RESULT_GUARD_POSIX(s2n_add_overflow(*size, psk->identity.size, size));
  114. uint8_t binder_size = 0;
  115. RESULT_GUARD_POSIX(s2n_hmac_digest_size(psk->hmac_alg, &binder_size));
  116. RESULT_GUARD_POSIX(s2n_add_overflow(*size, binder_size, size));
  117. return S2N_RESULT_OK;
  118. }
  119. S2N_RESULT s2n_psk_parameters_offered_psks_size(struct s2n_psk_parameters *params, uint32_t *size)
  120. {
  121. RESULT_ENSURE_REF(params);
  122. RESULT_ENSURE_REF(size);
  123. *size = sizeof(uint16_t) /* identity list size */
  124. + sizeof(uint16_t) /* binder list size */;
  125. for (uint32_t i = 0; i < params->psk_list.len; i++) {
  126. struct s2n_psk *psk = NULL;
  127. RESULT_GUARD(s2n_array_get(&params->psk_list, i, (void **) &psk));
  128. RESULT_ENSURE_REF(psk);
  129. uint32_t psk_size = 0;
  130. RESULT_GUARD(s2n_psk_offered_psk_size(psk, &psk_size));
  131. RESULT_GUARD_POSIX(s2n_add_overflow(*size, psk_size, size));
  132. }
  133. return S2N_RESULT_OK;
  134. }
  135. S2N_CLEANUP_RESULT s2n_psk_parameters_wipe(struct s2n_psk_parameters *params)
  136. {
  137. RESULT_ENSURE_REF(params);
  138. for (size_t i = 0; i < params->psk_list.len; i++) {
  139. struct s2n_psk *psk = NULL;
  140. RESULT_GUARD(s2n_array_get(&params->psk_list, i, (void **) &psk));
  141. RESULT_GUARD(s2n_psk_wipe(psk));
  142. }
  143. RESULT_GUARD_POSIX(s2n_free(&params->psk_list.mem));
  144. RESULT_GUARD(s2n_psk_parameters_init(params));
  145. return S2N_RESULT_OK;
  146. }
  147. S2N_CLEANUP_RESULT s2n_psk_parameters_wipe_secrets(struct s2n_psk_parameters *params)
  148. {
  149. RESULT_ENSURE_REF(params);
  150. for (size_t i = 0; i < params->psk_list.len; i++) {
  151. struct s2n_psk *psk = NULL;
  152. RESULT_GUARD(s2n_array_get(&params->psk_list, i, (void **) &psk));
  153. RESULT_ENSURE_REF(psk);
  154. RESULT_GUARD_POSIX(s2n_free(&psk->early_secret));
  155. RESULT_GUARD_POSIX(s2n_free(&psk->secret));
  156. }
  157. return S2N_RESULT_OK;
  158. }
  159. bool s2n_offered_psk_list_has_next(struct s2n_offered_psk_list *psk_list)
  160. {
  161. return psk_list != NULL && s2n_stuffer_data_available(&psk_list->wire_data) > 0;
  162. }
  163. S2N_RESULT s2n_offered_psk_list_read_next(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk)
  164. {
  165. RESULT_ENSURE_REF(psk_list);
  166. RESULT_ENSURE_REF(psk_list->conn);
  167. RESULT_ENSURE_MUT(psk);
  168. uint16_t identity_size = 0;
  169. RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&psk_list->wire_data, &identity_size));
  170. RESULT_ENSURE_GT(identity_size, 0);
  171. uint8_t *identity_data = NULL;
  172. identity_data = s2n_stuffer_raw_read(&psk_list->wire_data, identity_size);
  173. RESULT_ENSURE_REF(identity_data);
  174. /**
  175. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  176. *# For identities established externally, an obfuscated_ticket_age of 0 SHOULD be
  177. *# used, and servers MUST ignore the value.
  178. */
  179. if (psk_list->conn->psk_params.type == S2N_PSK_TYPE_EXTERNAL) {
  180. RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&psk_list->wire_data, sizeof(uint32_t)));
  181. } else {
  182. RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(&psk_list->wire_data, &psk->obfuscated_ticket_age));
  183. }
  184. RESULT_GUARD_POSIX(s2n_blob_init(&psk->identity, identity_data, identity_size));
  185. psk->wire_index = psk_list->wire_index;
  186. RESULT_ENSURE(psk_list->wire_index < UINT16_MAX, S2N_ERR_INTEGER_OVERFLOW);
  187. psk_list->wire_index++;
  188. return S2N_RESULT_OK;
  189. }
  190. int s2n_offered_psk_list_next(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk)
  191. {
  192. POSIX_ENSURE_REF(psk_list);
  193. POSIX_ENSURE_REF(psk);
  194. *psk = (struct s2n_offered_psk){ 0 };
  195. POSIX_ENSURE(s2n_offered_psk_list_has_next(psk_list), S2N_ERR_STUFFER_OUT_OF_DATA);
  196. POSIX_ENSURE(s2n_result_is_ok(s2n_offered_psk_list_read_next(psk_list, psk)), S2N_ERR_BAD_MESSAGE);
  197. return S2N_SUCCESS;
  198. }
  199. int s2n_offered_psk_list_reread(struct s2n_offered_psk_list *psk_list)
  200. {
  201. POSIX_ENSURE_REF(psk_list);
  202. psk_list->wire_index = 0;
  203. return s2n_stuffer_reread(&psk_list->wire_data);
  204. }
  205. /* Match a PSK identity received from the client against the server's known PSK identities.
  206. * This method compares a single client identity to all server identities.
  207. *
  208. * While both the client's offered identities and whether a match was found are public, we should make an attempt
  209. * to keep the server's known identities a secret. We will make comparisons to the server's identities constant
  210. * time (to hide partial matches) and not end the search early when a match is found (to hide the ordering).
  211. *
  212. * Keeping these comparisons constant time is not high priority. There's no known attack using these timings,
  213. * and an attacker could probably guess the server's known identities just by observing the public identities
  214. * sent by clients.
  215. */
  216. static S2N_RESULT s2n_match_psk_identity(struct s2n_array *known_psks, const struct s2n_blob *wire_identity,
  217. struct s2n_psk **match)
  218. {
  219. RESULT_ENSURE_REF(match);
  220. RESULT_ENSURE_REF(wire_identity);
  221. RESULT_ENSURE_REF(known_psks);
  222. *match = NULL;
  223. for (size_t i = 0; i < known_psks->len; i++) {
  224. struct s2n_psk *psk = NULL;
  225. RESULT_GUARD(s2n_array_get(known_psks, i, (void **) &psk));
  226. RESULT_ENSURE_REF(psk);
  227. RESULT_ENSURE_REF(psk->identity.data);
  228. RESULT_ENSURE_REF(wire_identity->data);
  229. uint32_t compare_size = MIN(wire_identity->size, psk->identity.size);
  230. if (s2n_constant_time_equals(psk->identity.data, wire_identity->data, compare_size)
  231. & (psk->identity.size == wire_identity->size) & (!*match)) {
  232. *match = psk;
  233. }
  234. }
  235. return S2N_RESULT_OK;
  236. }
  237. /**
  238. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  239. *# For PSKs provisioned via NewSessionTicket, a server MUST validate
  240. *# that the ticket age for the selected PSK identity (computed by
  241. *# subtracting ticket_age_add from PskIdentity.obfuscated_ticket_age
  242. *# modulo 2^32) is within a small tolerance of the time since the ticket
  243. *# was issued (see Section 8).
  244. **/
  245. static S2N_RESULT s2n_validate_ticket_lifetime(struct s2n_connection *conn, uint32_t obfuscated_ticket_age, uint32_t ticket_age_add)
  246. {
  247. RESULT_ENSURE_REF(conn);
  248. if (conn->psk_params.type == S2N_PSK_TYPE_EXTERNAL) {
  249. return S2N_RESULT_OK;
  250. }
  251. /* Subtract the ticket_age_add value from the ticket age in milliseconds. The resulting uint32_t value
  252. * may wrap, resulting in the modulo 2^32 operation. */
  253. uint32_t ticket_age_in_millis = obfuscated_ticket_age - ticket_age_add;
  254. uint32_t session_lifetime_in_millis = conn->config->session_state_lifetime_in_nanos / ONE_MILLISEC_IN_NANOS;
  255. RESULT_ENSURE(ticket_age_in_millis < session_lifetime_in_millis, S2N_ERR_INVALID_SESSION_TICKET);
  256. return S2N_RESULT_OK;
  257. }
  258. int s2n_offered_psk_list_choose_psk(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk)
  259. {
  260. POSIX_ENSURE_REF(psk_list);
  261. POSIX_ENSURE_REF(psk_list->conn);
  262. struct s2n_psk_parameters *psk_params = &psk_list->conn->psk_params;
  263. struct s2n_stuffer ticket_stuffer = { 0 };
  264. if (!psk) {
  265. psk_params->chosen_psk = NULL;
  266. return S2N_SUCCESS;
  267. }
  268. if (psk_params->type == S2N_PSK_TYPE_RESUMPTION && psk_list->conn->config->use_tickets) {
  269. POSIX_GUARD(s2n_stuffer_init(&ticket_stuffer, &psk->identity));
  270. POSIX_GUARD(s2n_stuffer_skip_write(&ticket_stuffer, psk->identity.size));
  271. /* s2n_decrypt_session_ticket appends a new PSK with the decrypted values. */
  272. POSIX_GUARD(s2n_decrypt_session_ticket(psk_list->conn, &ticket_stuffer));
  273. }
  274. struct s2n_psk *chosen_psk = NULL;
  275. POSIX_GUARD_RESULT(s2n_match_psk_identity(&psk_params->psk_list, &psk->identity, &chosen_psk));
  276. POSIX_ENSURE_REF(chosen_psk);
  277. POSIX_GUARD_RESULT(s2n_validate_ticket_lifetime(psk_list->conn, psk->obfuscated_ticket_age, chosen_psk->ticket_age_add));
  278. psk_params->chosen_psk = chosen_psk;
  279. psk_params->chosen_psk_wire_index = psk->wire_index;
  280. return S2N_SUCCESS;
  281. }
  282. struct s2n_offered_psk *s2n_offered_psk_new()
  283. {
  284. DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
  285. PTR_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_offered_psk)));
  286. PTR_GUARD_POSIX(s2n_blob_zero(&mem));
  287. struct s2n_offered_psk *psk = (struct s2n_offered_psk *) (void *) mem.data;
  288. ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
  289. return psk;
  290. }
  291. int s2n_offered_psk_free(struct s2n_offered_psk **psk)
  292. {
  293. if (psk == NULL) {
  294. return S2N_SUCCESS;
  295. }
  296. return s2n_free_object((uint8_t **) psk, sizeof(struct s2n_offered_psk));
  297. }
  298. int s2n_offered_psk_get_identity(struct s2n_offered_psk *psk, uint8_t **identity, uint16_t *size)
  299. {
  300. POSIX_ENSURE_REF(psk);
  301. POSIX_ENSURE_REF(identity);
  302. POSIX_ENSURE_REF(size);
  303. *identity = psk->identity.data;
  304. *size = psk->identity.size;
  305. return S2N_SUCCESS;
  306. }
  307. /* The binder hash is computed by hashing the concatenation of the current transcript
  308. * and a partial ClientHello that does not include the binders themselves.
  309. */
  310. int s2n_psk_calculate_binder_hash(struct s2n_connection *conn, s2n_hmac_algorithm hmac_alg,
  311. const struct s2n_blob *partial_client_hello, struct s2n_blob *output_binder_hash)
  312. {
  313. POSIX_ENSURE_REF(conn);
  314. POSIX_ENSURE_REF(partial_client_hello);
  315. POSIX_ENSURE_REF(output_binder_hash);
  316. struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
  317. POSIX_ENSURE_REF(hashes);
  318. /* Retrieve the current transcript.
  319. * The current transcript will be empty unless this handshake included a HelloRetryRequest. */
  320. s2n_hash_algorithm hash_alg = S2N_HASH_NONE;
  321. struct s2n_hash_state *hash_state = &hashes->hash_workspace;
  322. POSIX_GUARD(s2n_hmac_hash_alg(hmac_alg, &hash_alg));
  323. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, hash_alg, hash_state));
  324. /* Add the partial client hello to the transcript. */
  325. POSIX_GUARD(s2n_hash_update(hash_state, partial_client_hello->data, partial_client_hello->size));
  326. /* Get the transcript digest */
  327. POSIX_GUARD(s2n_hash_digest(hash_state, output_binder_hash->data, output_binder_hash->size));
  328. return S2N_SUCCESS;
  329. }
  330. /* The binder is computed in the same way as the Finished message
  331. * (https://tools.ietf.org/html/rfc8446#section-4.4.4) but with the BaseKey being the binder_key
  332. * derived via the key schedule from the corresponding PSK which is being offered
  333. * (https://tools.ietf.org/html/rfc8446#section-7.1)
  334. */
  335. int s2n_psk_calculate_binder(struct s2n_psk *psk, const struct s2n_blob *binder_hash,
  336. struct s2n_blob *output_binder)
  337. {
  338. POSIX_ENSURE_REF(psk);
  339. POSIX_ENSURE_REF(binder_hash);
  340. POSIX_ENSURE_REF(output_binder);
  341. DEFER_CLEANUP(struct s2n_tls13_keys psk_keys, s2n_tls13_keys_free);
  342. POSIX_GUARD(s2n_tls13_keys_init(&psk_keys, psk->hmac_alg));
  343. POSIX_ENSURE_EQ(binder_hash->size, psk_keys.size);
  344. POSIX_ENSURE_EQ(output_binder->size, psk_keys.size);
  345. /* Derive the binder key */
  346. POSIX_GUARD_RESULT(s2n_derive_binder_key(psk, &psk_keys.derive_secret));
  347. POSIX_GUARD(s2n_blob_init(&psk_keys.extract_secret, psk->early_secret.data, psk_keys.size));
  348. struct s2n_blob *binder_key = &psk_keys.derive_secret;
  349. /* Expand the binder key into the finished key */
  350. s2n_tls13_key_blob(finished_key, psk_keys.size);
  351. POSIX_GUARD(s2n_tls13_derive_finished_key(&psk_keys, binder_key, &finished_key));
  352. /* HMAC the binder hash with the binder finished key */
  353. POSIX_GUARD(s2n_hkdf_extract(&psk_keys.hmac, psk_keys.hmac_algorithm, &finished_key, binder_hash, output_binder));
  354. return S2N_SUCCESS;
  355. }
  356. int s2n_psk_verify_binder(struct s2n_connection *conn, struct s2n_psk *psk,
  357. const struct s2n_blob *partial_client_hello, struct s2n_blob *binder_to_verify)
  358. {
  359. POSIX_ENSURE_REF(psk);
  360. POSIX_ENSURE_REF(binder_to_verify);
  361. DEFER_CLEANUP(struct s2n_tls13_keys psk_keys, s2n_tls13_keys_free);
  362. POSIX_GUARD(s2n_tls13_keys_init(&psk_keys, psk->hmac_alg));
  363. POSIX_ENSURE_EQ(binder_to_verify->size, psk_keys.size);
  364. /* Calculate the binder hash from the transcript */
  365. s2n_tls13_key_blob(binder_hash, psk_keys.size);
  366. POSIX_GUARD(s2n_psk_calculate_binder_hash(conn, psk->hmac_alg, partial_client_hello, &binder_hash));
  367. /* Calculate the expected binder from the binder hash */
  368. s2n_tls13_key_blob(expected_binder, psk_keys.size);
  369. POSIX_GUARD(s2n_psk_calculate_binder(psk, &binder_hash, &expected_binder));
  370. /* Verify the expected binder matches the given binder.
  371. * This operation must be constant time. */
  372. POSIX_GUARD(s2n_tls13_mac_verify(&psk_keys, &expected_binder, binder_to_verify));
  373. return S2N_SUCCESS;
  374. }
  375. static S2N_RESULT s2n_psk_write_binder(struct s2n_connection *conn, struct s2n_psk *psk,
  376. const struct s2n_blob *binder_hash, struct s2n_stuffer *out)
  377. {
  378. RESULT_ENSURE_REF(binder_hash);
  379. struct s2n_blob binder = { 0 };
  380. uint8_t binder_data[S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  381. RESULT_GUARD_POSIX(s2n_blob_init(&binder, binder_data, binder_hash->size));
  382. RESULT_GUARD_POSIX(s2n_psk_calculate_binder(psk, binder_hash, &binder));
  383. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(out, binder.size));
  384. RESULT_GUARD_POSIX(s2n_stuffer_write(out, &binder));
  385. return S2N_RESULT_OK;
  386. }
  387. static S2N_RESULT s2n_psk_write_binder_list(struct s2n_connection *conn, const struct s2n_blob *partial_client_hello,
  388. struct s2n_stuffer *out)
  389. {
  390. RESULT_ENSURE_REF(conn);
  391. RESULT_ENSURE_REF(partial_client_hello);
  392. RESULT_ENSURE_REF(conn->secure);
  393. struct s2n_psk_parameters *psk_params = &conn->psk_params;
  394. struct s2n_array *psk_list = &psk_params->psk_list;
  395. /* Setup memory to hold the binder hashes. We potentially need one for
  396. * every hash algorithm. */
  397. uint8_t binder_hashes_data[S2N_HASH_ALG_COUNT][S2N_TLS13_SECRET_MAX_LEN] = { 0 };
  398. struct s2n_blob binder_hashes[S2N_HASH_ALG_COUNT] = { 0 };
  399. struct s2n_stuffer_reservation binder_list_size = { 0 };
  400. RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint16(out, &binder_list_size));
  401. /* Write binder for every psk */
  402. for (size_t i = 0; i < psk_list->len; i++) {
  403. struct s2n_psk *psk = NULL;
  404. RESULT_GUARD(s2n_array_get(psk_list, i, (void **) &psk));
  405. RESULT_ENSURE_REF(psk);
  406. /**
  407. *= https://tools.ietf.org/rfc/rfc8446#section-4.1.4
  408. *# In addition, in its updated ClientHello, the client SHOULD NOT offer
  409. *# any pre-shared keys associated with a hash other than that of the
  410. *# selected cipher suite. This allows the client to avoid having to
  411. *# compute partial hash transcripts for multiple hashes in the second
  412. *# ClientHello.
  413. */
  414. if (s2n_is_hello_retry_handshake(conn) && conn->secure->cipher_suite->prf_alg != psk->hmac_alg) {
  415. continue;
  416. }
  417. /* Retrieve or calculate the binder hash. */
  418. struct s2n_blob *binder_hash = &binder_hashes[psk->hmac_alg];
  419. if (binder_hash->size == 0) {
  420. uint8_t hash_size = 0;
  421. RESULT_GUARD_POSIX(s2n_hmac_digest_size(psk->hmac_alg, &hash_size));
  422. RESULT_GUARD_POSIX(s2n_blob_init(binder_hash, binder_hashes_data[psk->hmac_alg], hash_size));
  423. RESULT_GUARD_POSIX(s2n_psk_calculate_binder_hash(conn, psk->hmac_alg, partial_client_hello, binder_hash));
  424. }
  425. RESULT_GUARD(s2n_psk_write_binder(conn, psk, binder_hash, out));
  426. }
  427. RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&binder_list_size));
  428. return S2N_RESULT_OK;
  429. }
  430. S2N_RESULT s2n_finish_psk_extension(struct s2n_connection *conn)
  431. {
  432. RESULT_ENSURE_REF(conn);
  433. if (!conn->psk_params.binder_list_size) {
  434. return S2N_RESULT_OK;
  435. }
  436. struct s2n_stuffer *client_hello = &conn->handshake.io;
  437. struct s2n_psk_parameters *psk_params = &conn->psk_params;
  438. /* Fill in the correct message size. */
  439. RESULT_GUARD_POSIX(s2n_handshake_finish_header(client_hello));
  440. /* Remove the empty space allocated for the binder list.
  441. * It was originally added to ensure the extension / extension list / message sizes
  442. * were properly calculated. */
  443. RESULT_GUARD_POSIX(s2n_stuffer_wipe_n(client_hello, psk_params->binder_list_size));
  444. /* Store the partial client hello for use in calculating the binder hash. */
  445. struct s2n_blob partial_client_hello = { 0 };
  446. RESULT_GUARD_POSIX(s2n_blob_init(&partial_client_hello, client_hello->blob.data,
  447. s2n_stuffer_data_available(client_hello)));
  448. RESULT_GUARD(s2n_psk_write_binder_list(conn, &partial_client_hello, client_hello));
  449. /* Reset binder list size.
  450. * This is important because the psk extension can be removed during a retry.
  451. */
  452. conn->psk_params.binder_list_size = 0;
  453. return S2N_RESULT_OK;
  454. }
  455. int s2n_psk_set_hmac(struct s2n_psk *psk, s2n_psk_hmac hmac)
  456. {
  457. POSIX_ENSURE_REF(psk);
  458. switch (hmac) {
  459. case S2N_PSK_HMAC_SHA256:
  460. psk->hmac_alg = S2N_HMAC_SHA256;
  461. break;
  462. case S2N_PSK_HMAC_SHA384:
  463. psk->hmac_alg = S2N_HMAC_SHA384;
  464. break;
  465. default:
  466. POSIX_BAIL(S2N_ERR_HMAC_INVALID_ALGORITHM);
  467. }
  468. return S2N_SUCCESS;
  469. }
  470. S2N_RESULT s2n_connection_set_psk_type(struct s2n_connection *conn, s2n_psk_type type)
  471. {
  472. RESULT_ENSURE_REF(conn);
  473. if (conn->psk_params.psk_list.len != 0) {
  474. RESULT_ENSURE(conn->psk_params.type == type, S2N_ERR_PSK_MODE);
  475. }
  476. conn->psk_params.type = type;
  477. return S2N_RESULT_OK;
  478. }
  479. int s2n_connection_append_psk(struct s2n_connection *conn, struct s2n_psk *input_psk)
  480. {
  481. POSIX_ENSURE_REF(conn);
  482. POSIX_ENSURE_REF(input_psk);
  483. POSIX_GUARD_RESULT(s2n_connection_set_psk_type(conn, input_psk->type));
  484. struct s2n_array *psk_list = &conn->psk_params.psk_list;
  485. /* Check for duplicate identities */
  486. for (uint32_t j = 0; j < psk_list->len; j++) {
  487. struct s2n_psk *existing_psk = NULL;
  488. POSIX_GUARD_RESULT(s2n_array_get(psk_list, j, (void **) &existing_psk));
  489. POSIX_ENSURE_REF(existing_psk);
  490. bool duplicate = existing_psk->identity.size == input_psk->identity.size
  491. && memcmp(existing_psk->identity.data, input_psk->identity.data, existing_psk->identity.size) == 0;
  492. POSIX_ENSURE(!duplicate, S2N_ERR_DUPLICATE_PSK_IDENTITIES);
  493. }
  494. /* Verify the PSK list will fit in the ClientHello pre_shared_key extension */
  495. if (conn->mode == S2N_CLIENT) {
  496. uint32_t list_size = 0;
  497. POSIX_GUARD_RESULT(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &list_size));
  498. uint32_t psk_size = 0;
  499. POSIX_GUARD_RESULT(s2n_psk_offered_psk_size(input_psk, &psk_size));
  500. POSIX_ENSURE(list_size + psk_size + S2N_EXTENSION_HEADER_LENGTH <= UINT16_MAX, S2N_ERR_OFFERED_PSKS_TOO_LONG);
  501. }
  502. DEFER_CLEANUP(struct s2n_psk new_psk = { 0 }, s2n_psk_wipe);
  503. POSIX_ENSURE(s2n_result_is_ok(s2n_psk_clone(&new_psk, input_psk)), S2N_ERR_INVALID_ARGUMENT);
  504. POSIX_GUARD_RESULT(s2n_array_insert_and_copy(psk_list, psk_list->len, &new_psk));
  505. ZERO_TO_DISABLE_DEFER_CLEANUP(new_psk);
  506. return S2N_SUCCESS;
  507. }
  508. int s2n_config_set_psk_mode(struct s2n_config *config, s2n_psk_mode mode)
  509. {
  510. POSIX_ENSURE_REF(config);
  511. config->psk_mode = mode;
  512. return S2N_SUCCESS;
  513. }
  514. int s2n_connection_set_psk_mode(struct s2n_connection *conn, s2n_psk_mode mode)
  515. {
  516. POSIX_ENSURE_REF(conn);
  517. s2n_psk_type type = 0;
  518. switch (mode) {
  519. case S2N_PSK_MODE_RESUMPTION:
  520. type = S2N_PSK_TYPE_RESUMPTION;
  521. break;
  522. case S2N_PSK_MODE_EXTERNAL:
  523. type = S2N_PSK_TYPE_EXTERNAL;
  524. break;
  525. default:
  526. POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT);
  527. break;
  528. }
  529. POSIX_GUARD_RESULT(s2n_connection_set_psk_type(conn, type));
  530. conn->psk_mode_overridden = true;
  531. return S2N_SUCCESS;
  532. }
  533. int s2n_connection_get_negotiated_psk_identity_length(struct s2n_connection *conn, uint16_t *identity_length)
  534. {
  535. POSIX_ENSURE_REF(conn);
  536. POSIX_ENSURE_REF(identity_length);
  537. struct s2n_psk *chosen_psk = conn->psk_params.chosen_psk;
  538. if (chosen_psk == NULL) {
  539. *identity_length = 0;
  540. } else {
  541. *identity_length = chosen_psk->identity.size;
  542. }
  543. return S2N_SUCCESS;
  544. }
  545. int s2n_connection_get_negotiated_psk_identity(struct s2n_connection *conn, uint8_t *identity,
  546. uint16_t max_identity_length)
  547. {
  548. POSIX_ENSURE_REF(conn);
  549. POSIX_ENSURE_REF(identity);
  550. struct s2n_psk *chosen_psk = conn->psk_params.chosen_psk;
  551. if (chosen_psk == NULL) {
  552. return S2N_SUCCESS;
  553. }
  554. POSIX_ENSURE(chosen_psk->identity.size <= max_identity_length, S2N_ERR_INSUFFICIENT_MEM_SIZE);
  555. POSIX_CHECKED_MEMCPY(identity, chosen_psk->identity.data, chosen_psk->identity.size);
  556. return S2N_SUCCESS;
  557. }
  558. S2N_RESULT s2n_psk_validate_keying_material(struct s2n_connection *conn)
  559. {
  560. RESULT_ENSURE_REF(conn);
  561. struct s2n_psk *chosen_psk = conn->psk_params.chosen_psk;
  562. if (!chosen_psk || chosen_psk->type != S2N_PSK_TYPE_RESUMPTION) {
  563. return S2N_RESULT_OK;
  564. }
  565. /*
  566. * The minimum ticket lifetime is 1s, because ticket_lifetime is given
  567. * in seconds and 0 indicates that the ticket should be immediately discarded.
  568. */
  569. uint32_t min_lifetime = ONE_SEC_IN_NANOS;
  570. uint64_t current_time = 0;
  571. RESULT_GUARD(s2n_config_wall_clock(conn->config, &current_time));
  572. RESULT_ENSURE(chosen_psk->keying_material_expiration > current_time + min_lifetime, S2N_ERR_KEYING_MATERIAL_EXPIRED);
  573. return S2N_RESULT_OK;
  574. }