s2n_resume.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #pragma once
  16. #include "stuffer/s2n_stuffer.h"
  17. #include "utils/s2n_blob.h"
  18. #define S2N_STATE_LIFETIME_IN_NANOS 54000000000000 /* 15 hours */
  19. #define S2N_TLS12_STATE_SIZE_IN_BYTES (1 + 8 + 1 + S2N_TLS_CIPHER_SUITE_LEN + S2N_TLS_SECRET_LEN + 1)
  20. #define S2N_TLS13_FIXED_STATE_SIZE 21
  21. #define S2N_TLS13_FIXED_EARLY_DATA_STATE_SIZE 3
  22. #define S2N_TLS_SESSION_CACHE_TTL (6 * 60 * 60)
  23. #define S2N_TICKET_KEY_NAME_LEN 16
  24. #define S2N_TICKET_AAD_IMPLICIT_LEN 12
  25. #define S2N_TICKET_AAD_LEN (S2N_TICKET_AAD_IMPLICIT_LEN + S2N_TICKET_KEY_NAME_LEN)
  26. #define S2N_AES256_KEY_LEN 32
  27. #define ONE_SEC_IN_NANOS 1000000000
  28. #define ONE_MILLISEC_IN_NANOS 1000000
  29. #define ONE_WEEK_IN_SEC 604800
  30. #define S2N_TLS12_TICKET_SIZE_IN_BYTES (S2N_TICKET_KEY_NAME_LEN + S2N_TLS_GCM_IV_LEN \
  31. + S2N_TLS12_STATE_SIZE_IN_BYTES + S2N_TLS_GCM_TAG_LEN)
  32. #define S2N_TICKET_ENCRYPT_DECRYPT_KEY_LIFETIME_IN_NANOS 7200000000000 /* 2 hours */
  33. #define S2N_TICKET_DECRYPT_KEY_LIFETIME_IN_NANOS 46800000000000 /* 13 hours */
  34. #define S2N_STATE_FORMAT_LEN 1
  35. #define S2N_TICKET_LIFETIME_HINT_LEN 4
  36. #define S2N_SESSION_TICKET_SIZE_LEN 2
  37. #define S2N_GREATER_OR_EQUAL 1
  38. #define S2N_LESS_THAN -1
  39. #define S2N_TLS12_SESSION_SIZE S2N_STATE_FORMAT_LEN + S2N_SESSION_TICKET_SIZE_LEN \
  40. + S2N_TLS12_TICKET_SIZE_IN_BYTES + S2N_TLS12_STATE_SIZE_IN_BYTES
  41. struct s2n_connection;
  42. struct s2n_config;
  43. struct s2n_ticket_key {
  44. unsigned char key_name[S2N_TICKET_KEY_NAME_LEN];
  45. uint8_t aes_key[S2N_AES256_KEY_LEN];
  46. uint8_t implicit_aad[S2N_TICKET_AAD_IMPLICIT_LEN];
  47. uint64_t intro_timestamp;
  48. };
  49. struct s2n_ticket_key_weight {
  50. double key_weight;
  51. uint8_t key_index;
  52. };
  53. struct s2n_ticket_fields {
  54. struct s2n_blob session_secret;
  55. uint32_t ticket_age_add;
  56. };
  57. struct s2n_session_ticket {
  58. struct s2n_blob ticket_data;
  59. uint32_t session_lifetime;
  60. };
  61. struct s2n_ticket_key *s2n_find_ticket_key(struct s2n_config *config, const uint8_t name[S2N_TICKET_KEY_NAME_LEN]);
  62. int s2n_encrypt_session_ticket(struct s2n_connection *conn, struct s2n_stuffer *to);
  63. int s2n_decrypt_session_ticket(struct s2n_connection *conn, struct s2n_stuffer *from);
  64. int s2n_encrypt_session_cache(struct s2n_connection *conn, struct s2n_stuffer *to);
  65. int s2n_decrypt_session_cache(struct s2n_connection *conn, struct s2n_stuffer *from);
  66. int s2n_config_is_encrypt_decrypt_key_available(struct s2n_config *config);
  67. int s2n_verify_unique_ticket_key(struct s2n_config *config, uint8_t *hash, uint16_t *insert_index);
  68. int s2n_config_wipe_expired_ticket_crypto_keys(struct s2n_config *config, int8_t expired_key_index);
  69. int s2n_config_store_ticket_key(struct s2n_config *config, struct s2n_ticket_key *key);
  70. typedef enum {
  71. S2N_STATE_WITH_SESSION_ID = 0,
  72. S2N_STATE_WITH_SESSION_TICKET
  73. } s2n_client_tls_session_state_format;
  74. typedef enum {
  75. S2N_SERIALIZED_FORMAT_TLS12_V1 = 1,
  76. S2N_SERIALIZED_FORMAT_TLS13_V1,
  77. S2N_SERIALIZED_FORMAT_TLS12_V2,
  78. S2N_SERIALIZED_FORMAT_TLS12_V3,
  79. } s2n_serial_format_version;
  80. int s2n_allowed_to_cache_connection(struct s2n_connection *conn);
  81. int s2n_resume_from_cache(struct s2n_connection *conn);
  82. S2N_RESULT s2n_store_to_cache(struct s2n_connection *conn);
  83. S2N_RESULT s2n_connection_get_session_state_size(struct s2n_connection *conn, size_t *state_size);
  84. S2N_RESULT s2n_deserialize_resumption_state(struct s2n_connection *conn, struct s2n_blob *psk_identity,
  85. struct s2n_stuffer *from);