s2n_client_session_ticket.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_session_ticket.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "tls/extensions/s2n_client_psk.h"
  19. #include "tls/s2n_resume.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls_parameters.h"
  22. #include "utils/s2n_safety.h"
  23. static bool s2n_client_session_ticket_should_send(struct s2n_connection *conn);
  24. static int s2n_client_session_ticket_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  25. static int s2n_client_session_ticket_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  26. const s2n_extension_type s2n_client_session_ticket_extension = {
  27. .iana_value = TLS_EXTENSION_SESSION_TICKET,
  28. .is_response = false,
  29. .send = s2n_client_session_ticket_send,
  30. .recv = s2n_client_session_ticket_recv,
  31. .should_send = s2n_client_session_ticket_should_send,
  32. .if_missing = s2n_extension_noop_if_missing,
  33. };
  34. static bool s2n_client_session_ticket_should_send(struct s2n_connection *conn)
  35. {
  36. return conn->config->use_tickets && !s2n_client_psk_should_send(conn);
  37. }
  38. static int s2n_client_session_ticket_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  39. {
  40. POSIX_GUARD(s2n_stuffer_write(out, &conn->client_ticket));
  41. return S2N_SUCCESS;
  42. }
  43. static int s2n_client_session_ticket_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  44. {
  45. if (conn->config->use_tickets != 1 || conn->actual_protocol_version > S2N_TLS12) {
  46. /* Ignore the extension. */
  47. return S2N_SUCCESS;
  48. }
  49. /* s2n server does not support session ticket with CLIENT_AUTH enabled */
  50. if (s2n_connection_is_client_auth_enabled(conn) > 0) {
  51. return S2N_SUCCESS;
  52. }
  53. if (s2n_stuffer_data_available(extension) == S2N_TLS12_TICKET_SIZE_IN_BYTES) {
  54. conn->session_ticket_status = S2N_DECRYPT_TICKET;
  55. POSIX_GUARD(s2n_stuffer_copy(extension, &conn->client_ticket_to_decrypt, S2N_TLS12_TICKET_SIZE_IN_BYTES));
  56. } else if (s2n_config_is_encrypt_decrypt_key_available(conn->config) == 1) {
  57. conn->session_ticket_status = S2N_NEW_TICKET;
  58. }
  59. return S2N_SUCCESS;
  60. }