s2n_server_new_session_ticket.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <time.h>
  17. #include "api/s2n.h"
  18. #include "error/s2n_errno.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "tls/s2n_alerts.h"
  21. #include "tls/s2n_connection.h"
  22. #include "tls/s2n_record.h"
  23. #include "tls/s2n_resume.h"
  24. #include "tls/s2n_tls.h"
  25. #include "tls/s2n_tls13_handshake.h"
  26. #include "utils/s2n_random.h"
  27. #include "utils/s2n_safety.h"
  28. /*
  29. * The maximum size of the NewSessionTicket message, not taking into account the
  30. * ticket itself.
  31. *
  32. * To get the actual maximum size required for the NewSessionTicket message, we'll need
  33. * to add the size of the ticket, which is much less predictable.
  34. *
  35. * This constant is enforced via unit tests.
  36. */
  37. #define S2N_TLS13_MAX_FIXED_NEW_SESSION_TICKET_SIZE 79
  38. int s2n_server_nst_recv(struct s2n_connection *conn)
  39. {
  40. POSIX_GUARD(s2n_stuffer_read_uint32(&conn->handshake.io, &conn->ticket_lifetime_hint));
  41. uint16_t session_ticket_len;
  42. POSIX_GUARD(s2n_stuffer_read_uint16(&conn->handshake.io, &session_ticket_len));
  43. if (session_ticket_len > 0) {
  44. POSIX_GUARD(s2n_realloc(&conn->client_ticket, session_ticket_len));
  45. POSIX_GUARD(s2n_stuffer_read(&conn->handshake.io, &conn->client_ticket));
  46. if (conn->config->session_ticket_cb != NULL) {
  47. size_t session_len = s2n_connection_get_session_length(conn);
  48. /* Alloc some memory for the serialized session ticket */
  49. DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
  50. POSIX_GUARD(s2n_alloc(&mem,
  51. S2N_STATE_FORMAT_LEN + S2N_SESSION_TICKET_SIZE_LEN + conn->client_ticket.size + S2N_TLS12_STATE_SIZE_IN_BYTES));
  52. POSIX_GUARD(s2n_connection_get_session(conn, mem.data, session_len));
  53. uint32_t session_lifetime = s2n_connection_get_session_ticket_lifetime_hint(conn);
  54. struct s2n_session_ticket ticket = { .ticket_data = mem, .session_lifetime = session_lifetime };
  55. POSIX_ENSURE(conn->config->session_ticket_cb(conn, conn->config->session_ticket_ctx, &ticket) >= S2N_SUCCESS,
  56. S2N_ERR_CANCELLED);
  57. }
  58. }
  59. return S2N_SUCCESS;
  60. }
  61. int s2n_server_nst_send(struct s2n_connection *conn)
  62. {
  63. uint16_t session_ticket_len = S2N_TLS12_TICKET_SIZE_IN_BYTES;
  64. uint8_t data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
  65. struct s2n_blob entry = { 0 };
  66. POSIX_GUARD(s2n_blob_init(&entry, data, sizeof(data)));
  67. struct s2n_stuffer to = { 0 };
  68. uint32_t lifetime_hint_in_secs =
  69. (conn->config->encrypt_decrypt_key_lifetime_in_nanos + conn->config->decrypt_key_lifetime_in_nanos) / ONE_SEC_IN_NANOS;
  70. /* When server changes it's mind mid handshake send lifetime hint and session ticket length as zero */
  71. if (!conn->config->use_tickets) {
  72. POSIX_GUARD(s2n_stuffer_write_uint32(&conn->handshake.io, 0));
  73. POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, 0));
  74. return 0;
  75. }
  76. if (!s2n_server_sending_nst(conn)) {
  77. POSIX_BAIL(S2N_ERR_SENDING_NST);
  78. }
  79. POSIX_GUARD(s2n_stuffer_init(&to, &entry));
  80. POSIX_GUARD(s2n_stuffer_write_uint32(&conn->handshake.io, lifetime_hint_in_secs));
  81. POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, session_ticket_len));
  82. POSIX_GUARD(s2n_encrypt_session_ticket(conn, &to));
  83. POSIX_GUARD(s2n_stuffer_write(&conn->handshake.io, &to.blob));
  84. /* For parity with TLS1.3, track the single ticket sent.
  85. * This simplifies s2n_connection_get_tickets_sent.
  86. */
  87. conn->tickets_sent++;
  88. return S2N_SUCCESS;
  89. }
  90. S2N_RESULT s2n_tls13_server_nst_send(struct s2n_connection *conn, s2n_blocked_status *blocked)
  91. {
  92. RESULT_ENSURE_REF(conn);
  93. RESULT_ENSURE_GTE(conn->actual_protocol_version, S2N_TLS13);
  94. /* Usually tickets are sent immediately after the handshake.
  95. * If possible, reuse the handshake IO stuffer before it's wiped.
  96. *
  97. * Note: handshake.io isn't explicitly dedicated to only reading or only writing,
  98. * so we have to be careful using it outside of s2n_negotiate.
  99. * If we use it for writing here, we CAN'T use it for reading any post-handshake messages.
  100. */
  101. struct s2n_stuffer *nst_stuffer = &conn->handshake.io;
  102. if (conn->mode != S2N_SERVER || !conn->config->use_tickets) {
  103. return S2N_RESULT_OK;
  104. }
  105. /* Legacy behavior is that the s2n server sends a NST even if the client did not indicate support
  106. * for resumption or does not support the psk_dhe_ke mode. This is potentially wasteful so we
  107. * choose to not extend this behavior to QUIC.
  108. */
  109. if (conn->quic_enabled && conn->psk_params.psk_ke_mode != S2N_PSK_DHE_KE) {
  110. return S2N_RESULT_OK;
  111. }
  112. /* No-op if all tickets already sent.
  113. * Clean up the stuffer used for the ticket to conserve memory. */
  114. if (conn->tickets_to_send == conn->tickets_sent) {
  115. RESULT_GUARD_POSIX(s2n_stuffer_resize(nst_stuffer, 0));
  116. return S2N_RESULT_OK;
  117. }
  118. /**
  119. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  120. *# Note that in principle it is possible to continue issuing new tickets
  121. *# which indefinitely extend the lifetime of the keying material
  122. *# originally derived from an initial non-PSK handshake (which was most
  123. *# likely tied to the peer's certificate). It is RECOMMENDED that
  124. *# implementations place limits on the total lifetime of such keying
  125. *# material; these limits should take into account the lifetime of the
  126. *# peer's certificate, the likelihood of intervening revocation, and the
  127. *# time since the peer's online CertificateVerify signature.
  128. */
  129. if (s2n_result_is_error(s2n_psk_validate_keying_material(conn))) {
  130. conn->tickets_to_send = conn->tickets_sent;
  131. return S2N_RESULT_OK;
  132. }
  133. RESULT_ENSURE(conn->tickets_sent <= conn->tickets_to_send, S2N_ERR_INTEGER_OVERFLOW);
  134. size_t session_state_size = 0;
  135. RESULT_GUARD(s2n_connection_get_session_state_size(conn, &session_state_size));
  136. const size_t maximum_nst_size = session_state_size + S2N_TLS13_MAX_FIXED_NEW_SESSION_TICKET_SIZE;
  137. if (s2n_stuffer_space_remaining(nst_stuffer) < maximum_nst_size) {
  138. RESULT_GUARD_POSIX(s2n_stuffer_resize(nst_stuffer, maximum_nst_size));
  139. }
  140. while (conn->tickets_to_send - conn->tickets_sent > 0) {
  141. if (s2n_result_is_error(s2n_tls13_server_nst_write(conn, nst_stuffer))) {
  142. return S2N_RESULT_OK;
  143. }
  144. RESULT_GUARD(s2n_post_handshake_write_records(conn, blocked));
  145. }
  146. return S2N_RESULT_OK;
  147. }
  148. /**
  149. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  150. *# Indicates the lifetime in seconds as a 32-bit
  151. *# unsigned integer in network byte order from the time of ticket
  152. *# issuance.
  153. **/
  154. static S2N_RESULT s2n_generate_ticket_lifetime(struct s2n_connection *conn, uint32_t *ticket_lifetime)
  155. {
  156. RESULT_ENSURE_REF(conn);
  157. RESULT_ENSURE_MUT(ticket_lifetime);
  158. uint32_t key_lifetime_in_secs =
  159. (conn->config->encrypt_decrypt_key_lifetime_in_nanos + conn->config->decrypt_key_lifetime_in_nanos) / ONE_SEC_IN_NANOS;
  160. uint32_t session_lifetime_in_secs = conn->config->session_state_lifetime_in_nanos / ONE_SEC_IN_NANOS;
  161. uint32_t key_and_session_min_lifetime = MIN(key_lifetime_in_secs, session_lifetime_in_secs);
  162. /**
  163. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  164. *# Servers MUST NOT use any value greater than
  165. *# 604800 seconds (7 days).
  166. **/
  167. *ticket_lifetime = MIN(key_and_session_min_lifetime, ONE_WEEK_IN_SEC);
  168. return S2N_RESULT_OK;
  169. }
  170. /**
  171. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  172. *# A per-ticket value that is unique across all tickets
  173. *# issued on this connection.
  174. **/
  175. static S2N_RESULT s2n_generate_ticket_nonce(uint16_t value, struct s2n_blob *output)
  176. {
  177. RESULT_ENSURE_MUT(output);
  178. struct s2n_stuffer stuffer = { 0 };
  179. RESULT_GUARD_POSIX(s2n_stuffer_init(&stuffer, output));
  180. RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&stuffer, value));
  181. return S2N_RESULT_OK;
  182. }
  183. /**
  184. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  185. *# A securely generated, random 32-bit value that is
  186. *# used to obscure the age of the ticket that the client includes in
  187. *# the "pre_shared_key" extension.
  188. **/
  189. static S2N_RESULT s2n_generate_ticket_age_add(struct s2n_blob *random_data, uint32_t *ticket_age_add)
  190. {
  191. RESULT_ENSURE_REF(random_data);
  192. RESULT_ENSURE_REF(ticket_age_add);
  193. struct s2n_stuffer stuffer = { 0 };
  194. RESULT_GUARD_POSIX(s2n_stuffer_init(&stuffer, random_data));
  195. RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&stuffer, random_data->size));
  196. RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(&stuffer, ticket_age_add));
  197. return S2N_RESULT_OK;
  198. }
  199. /**
  200. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  201. *# The PSK associated with the ticket is computed as:
  202. *#
  203. *# HKDF-Expand-Label(resumption_master_secret,
  204. *# "resumption", ticket_nonce, Hash.length)
  205. **/
  206. static int s2n_generate_session_secret(struct s2n_connection *conn, struct s2n_blob *nonce, struct s2n_blob *output)
  207. {
  208. POSIX_ENSURE_REF(conn);
  209. POSIX_ENSURE_REF(nonce);
  210. POSIX_ENSURE_REF(output);
  211. s2n_tls13_connection_keys(secrets, conn);
  212. struct s2n_blob master_secret = { 0 };
  213. POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls13.resumption_master_secret, secrets.size));
  214. POSIX_GUARD(s2n_realloc(output, secrets.size));
  215. POSIX_GUARD_RESULT(s2n_tls13_derive_session_ticket_secret(&secrets, &master_secret, nonce, output));
  216. return S2N_SUCCESS;
  217. }
  218. S2N_RESULT s2n_tls13_server_nst_write(struct s2n_connection *conn, struct s2n_stuffer *output)
  219. {
  220. RESULT_ENSURE_REF(conn);
  221. RESULT_ENSURE_REF(output);
  222. struct s2n_ticket_fields *ticket_fields = &conn->tls13_ticket_fields;
  223. /* Write message type because session resumption in TLS13 is a post-handshake message */
  224. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, TLS_SERVER_NEW_SESSION_TICKET));
  225. struct s2n_stuffer_reservation message_size = { 0 };
  226. RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint24(output, &message_size));
  227. uint32_t ticket_lifetime_in_secs = 0;
  228. RESULT_GUARD(s2n_generate_ticket_lifetime(conn, &ticket_lifetime_in_secs));
  229. RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(output, ticket_lifetime_in_secs));
  230. /* Get random data to use as ticket_age_add value */
  231. uint8_t data[sizeof(uint32_t)] = { 0 };
  232. struct s2n_blob random_data = { 0 };
  233. RESULT_GUARD_POSIX(s2n_blob_init(&random_data, data, sizeof(data)));
  234. /**
  235. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  236. *# The server MUST generate a fresh value
  237. *# for each ticket it sends.
  238. **/
  239. RESULT_GUARD(s2n_get_private_random_data(&random_data));
  240. RESULT_GUARD(s2n_generate_ticket_age_add(&random_data, &ticket_fields->ticket_age_add));
  241. RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(output, ticket_fields->ticket_age_add));
  242. /* Write ticket nonce */
  243. uint8_t nonce_data[sizeof(uint16_t)] = { 0 };
  244. struct s2n_blob nonce = { 0 };
  245. RESULT_GUARD_POSIX(s2n_blob_init(&nonce, nonce_data, sizeof(nonce_data)));
  246. RESULT_GUARD(s2n_generate_ticket_nonce(conn->tickets_sent, &nonce));
  247. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, nonce.size));
  248. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, nonce.data, nonce.size));
  249. /* Derive individual session ticket secret */
  250. RESULT_GUARD_POSIX(s2n_generate_session_secret(conn, &nonce, &ticket_fields->session_secret));
  251. /* Write ticket */
  252. struct s2n_stuffer_reservation ticket_size = { 0 };
  253. RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint16(output, &ticket_size));
  254. RESULT_GUARD_POSIX(s2n_encrypt_session_ticket(conn, output));
  255. RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&ticket_size));
  256. RESULT_GUARD_POSIX(s2n_extension_list_send(S2N_EXTENSION_LIST_NST, conn, output));
  257. RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&message_size));
  258. RESULT_ENSURE(conn->tickets_sent < UINT16_MAX, S2N_ERR_INTEGER_OVERFLOW);
  259. conn->tickets_sent++;
  260. return S2N_RESULT_OK;
  261. }
  262. /**
  263. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  264. *# struct {
  265. *# uint32 ticket_lifetime;
  266. *# uint32 ticket_age_add;
  267. *# opaque ticket_nonce<0..255>;
  268. *# opaque ticket<1..2^16-1>;
  269. *# Extension extensions<0..2^16-2>;
  270. *# } NewSessionTicket;
  271. **/
  272. S2N_RESULT s2n_tls13_server_nst_recv(struct s2n_connection *conn, struct s2n_stuffer *input)
  273. {
  274. RESULT_ENSURE_REF(conn);
  275. RESULT_ENSURE_REF(input);
  276. RESULT_ENSURE_REF(conn->config);
  277. RESULT_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_BAD_MESSAGE);
  278. RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_BAD_MESSAGE);
  279. if (!conn->config->use_tickets) {
  280. return S2N_RESULT_OK;
  281. }
  282. struct s2n_ticket_fields *ticket_fields = &conn->tls13_ticket_fields;
  283. /* Handle `ticket_lifetime` field */
  284. uint32_t ticket_lifetime = 0;
  285. RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(input, &ticket_lifetime));
  286. /**
  287. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  288. *# Servers MUST NOT use any value greater than
  289. *# 604800 seconds (7 days).
  290. */
  291. RESULT_ENSURE(ticket_lifetime <= ONE_WEEK_IN_SEC, S2N_ERR_BAD_MESSAGE);
  292. /**
  293. *= https://tools.ietf.org/rfc/rfc8446#section-4.6.1
  294. *# The value of zero indicates that the
  295. *# ticket should be discarded immediately.
  296. */
  297. if (ticket_lifetime == 0) {
  298. return S2N_RESULT_OK;
  299. }
  300. conn->ticket_lifetime_hint = ticket_lifetime;
  301. /* Handle `ticket_age_add` field */
  302. RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(input, &ticket_fields->ticket_age_add));
  303. /* Handle `ticket_nonce` field */
  304. uint8_t ticket_nonce_len = 0;
  305. RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(input, &ticket_nonce_len));
  306. uint8_t nonce_data[UINT8_MAX] = { 0 };
  307. struct s2n_blob nonce = { 0 };
  308. RESULT_GUARD_POSIX(s2n_blob_init(&nonce, nonce_data, ticket_nonce_len));
  309. RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, nonce.data, ticket_nonce_len));
  310. RESULT_GUARD_POSIX(s2n_generate_session_secret(conn, &nonce, &ticket_fields->session_secret));
  311. /* Handle `ticket` field */
  312. uint16_t session_ticket_len = 0;
  313. RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(input, &session_ticket_len));
  314. RESULT_ENSURE(session_ticket_len > 0, S2N_ERR_SAFETY);
  315. RESULT_GUARD_POSIX(s2n_realloc(&conn->client_ticket, session_ticket_len));
  316. RESULT_GUARD_POSIX(s2n_stuffer_read(input, &conn->client_ticket));
  317. /* Handle `extensions` field */
  318. RESULT_GUARD_POSIX(s2n_extension_list_recv(S2N_EXTENSION_LIST_NST, conn, input));
  319. if (conn->config->session_ticket_cb != NULL) {
  320. /* Retrieve serialized session data */
  321. const uint16_t session_state_size = s2n_connection_get_session_length(conn);
  322. DEFER_CLEANUP(struct s2n_blob session_state = { 0 }, s2n_free);
  323. RESULT_GUARD_POSIX(s2n_realloc(&session_state, session_state_size));
  324. RESULT_GUARD_POSIX(s2n_connection_get_session(conn, session_state.data, session_state.size));
  325. struct s2n_session_ticket ticket = {
  326. .ticket_data = session_state,
  327. .session_lifetime = ticket_lifetime
  328. };
  329. RESULT_ENSURE(conn->config->session_ticket_cb(conn, conn->config->session_ticket_ctx, &ticket) >= S2N_SUCCESS,
  330. S2N_ERR_CANCELLED);
  331. }
  332. return S2N_RESULT_OK;
  333. }