s2n_tls13_keys.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "crypto/s2n_tls13_keys.h"
  16. #include <stdio.h>
  17. #include "crypto/s2n_hkdf.h"
  18. #include "crypto/s2n_hmac.h"
  19. #include "error/s2n_errno.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #include "utils/s2n_blob.h"
  22. #include "utils/s2n_mem.h"
  23. #include "utils/s2n_safety.h"
  24. /*
  25. * There are 9 keys that can be generated by the end of a TLS 1.3 handshake.
  26. * We currently support the following, more will be supported
  27. * when the relevant TLS 1.3 features are worked on.
  28. *
  29. * [x] binder_key
  30. * [x] client_early_traffic_secret
  31. * [ ] early_exporter_master_secret
  32. * [x] client_handshake_traffic_secret
  33. * [x] server_handshake_traffic_secret
  34. * [x] client_application_traffic_secret_0
  35. * [x] server_application_traffic_secret_0
  36. * [x] exporter_master_secret
  37. * [x] resumption_master_secret
  38. *
  39. * The TLS 1.3 key generation can be divided into 3 phases
  40. * 1. early secrets
  41. * 2. handshake secrets
  42. * 3. master secrets
  43. *
  44. * In each phase, secrets are first extracted with HKDF-Extract that takes in
  45. * both an ikm (input keying material) and a salt. Some keys can be derived/expanded
  46. * from the extract before a "tls13 derived" Derive-Secret is used to
  47. * derive the input salt for the next phase.
  48. */
  49. /*
  50. * Define TLS 1.3 HKDF labels as specified in
  51. * https://tools.ietf.org/html/rfc8446#section-7.1
  52. */
  53. S2N_BLOB_LABEL(s2n_tls13_label_derived_secret, "derived")
  54. S2N_BLOB_LABEL(s2n_tls13_label_external_psk_binder_key, "ext binder")
  55. S2N_BLOB_LABEL(s2n_tls13_label_resumption_psk_binder_key, "res binder")
  56. S2N_BLOB_LABEL(s2n_tls13_label_client_early_traffic_secret, "c e traffic")
  57. S2N_BLOB_LABEL(s2n_tls13_label_early_exporter_master_secret, "e exp master")
  58. S2N_BLOB_LABEL(s2n_tls13_label_client_handshake_traffic_secret, "c hs traffic")
  59. S2N_BLOB_LABEL(s2n_tls13_label_server_handshake_traffic_secret, "s hs traffic")
  60. S2N_BLOB_LABEL(s2n_tls13_label_client_application_traffic_secret, "c ap traffic")
  61. S2N_BLOB_LABEL(s2n_tls13_label_server_application_traffic_secret, "s ap traffic")
  62. S2N_BLOB_LABEL(s2n_tls13_label_exporter_master_secret, "exp master")
  63. S2N_BLOB_LABEL(s2n_tls13_label_resumption_master_secret, "res master")
  64. S2N_BLOB_LABEL(s2n_tls13_label_session_ticket_secret, "resumption")
  65. /*
  66. * Traffic secret labels
  67. */
  68. S2N_BLOB_LABEL(s2n_tls13_label_traffic_secret_key, "key")
  69. S2N_BLOB_LABEL(s2n_tls13_label_traffic_secret_iv, "iv")
  70. /*
  71. * TLS 1.3 Exporter label
  72. */
  73. S2N_BLOB_LABEL(s2n_tls13_label_exporter, "exporter")
  74. /*
  75. * TLS 1.3 Finished label
  76. */
  77. S2N_BLOB_LABEL(s2n_tls13_label_finished, "finished")
  78. /*
  79. * TLS 1.3 KeyUpdate label
  80. */
  81. S2N_BLOB_LABEL(s2n_tls13_label_application_traffic_secret_update, "traffic upd")
  82. static const struct s2n_blob zero_length_blob = { .data = NULL, .size = 0 };
  83. /*
  84. * Initializes the tls13_keys struct
  85. */
  86. int s2n_tls13_keys_init(struct s2n_tls13_keys *keys, s2n_hmac_algorithm alg)
  87. {
  88. POSIX_ENSURE_REF(keys);
  89. keys->hmac_algorithm = alg;
  90. POSIX_GUARD(s2n_hmac_hash_alg(alg, &keys->hash_algorithm));
  91. POSIX_GUARD(s2n_hash_digest_size(keys->hash_algorithm, &keys->size));
  92. POSIX_GUARD(s2n_blob_init(&keys->extract_secret, keys->extract_secret_bytes, keys->size));
  93. POSIX_GUARD(s2n_blob_init(&keys->derive_secret, keys->derive_secret_bytes, keys->size));
  94. POSIX_GUARD(s2n_hmac_new(&keys->hmac));
  95. return 0;
  96. }
  97. /*
  98. * Frees any allocation
  99. */
  100. int s2n_tls13_keys_free(struct s2n_tls13_keys *keys)
  101. {
  102. POSIX_ENSURE_REF(keys);
  103. POSIX_GUARD(s2n_hmac_free(&keys->hmac));
  104. return 0;
  105. }
  106. /*
  107. * Derive Traffic Key and IV based on input secret
  108. */
  109. int s2n_tls13_derive_traffic_keys(struct s2n_tls13_keys *keys, struct s2n_blob *secret, struct s2n_blob *key, struct s2n_blob *iv)
  110. {
  111. POSIX_ENSURE_REF(keys);
  112. POSIX_ENSURE_REF(secret);
  113. POSIX_ENSURE_REF(key);
  114. POSIX_ENSURE_REF(iv);
  115. POSIX_GUARD(s2n_hkdf_expand_label(&keys->hmac, keys->hmac_algorithm, secret,
  116. &s2n_tls13_label_traffic_secret_key, &zero_length_blob, key));
  117. POSIX_GUARD(s2n_hkdf_expand_label(&keys->hmac, keys->hmac_algorithm, secret,
  118. &s2n_tls13_label_traffic_secret_iv, &zero_length_blob, iv));
  119. return 0;
  120. }
  121. /*
  122. * Generate finished key for compute finished hashes/MACs
  123. * https://tools.ietf.org/html/rfc8446#section-4.4.4
  124. */
  125. int s2n_tls13_derive_finished_key(struct s2n_tls13_keys *keys, struct s2n_blob *secret_key, struct s2n_blob *output_finish_key)
  126. {
  127. POSIX_GUARD(s2n_hkdf_expand_label(&keys->hmac, keys->hmac_algorithm, secret_key, &s2n_tls13_label_finished, &zero_length_blob, output_finish_key));
  128. return 0;
  129. }
  130. /*
  131. * Compute finished verify data using HMAC
  132. * with a finished key and hash state
  133. * https://tools.ietf.org/html/rfc8446#section-4.4.4
  134. */
  135. int s2n_tls13_calculate_finished_mac(struct s2n_tls13_keys *keys, struct s2n_blob *finished_key, struct s2n_hash_state *hash_state, struct s2n_blob *finished_verify)
  136. {
  137. s2n_tls13_key_blob(transcript_hash, keys->size);
  138. POSIX_GUARD(s2n_hash_digest(hash_state, transcript_hash.data, transcript_hash.size));
  139. POSIX_GUARD(s2n_hkdf_extract(&keys->hmac, keys->hmac_algorithm, finished_key, &transcript_hash, finished_verify));
  140. return S2N_SUCCESS;
  141. }
  142. /*
  143. * Derives next generation of traffic secret
  144. */
  145. int s2n_tls13_update_application_traffic_secret(struct s2n_tls13_keys *keys, struct s2n_blob *old_secret, struct s2n_blob *new_secret)
  146. {
  147. POSIX_ENSURE_REF(keys);
  148. POSIX_ENSURE_REF(old_secret);
  149. POSIX_ENSURE_REF(new_secret);
  150. POSIX_GUARD(s2n_hkdf_expand_label(&keys->hmac, keys->hmac_algorithm, old_secret,
  151. &s2n_tls13_label_application_traffic_secret_update, &zero_length_blob, new_secret));
  152. return 0;
  153. }
  154. S2N_RESULT s2n_tls13_derive_session_ticket_secret(struct s2n_tls13_keys *keys, struct s2n_blob *resumption_secret,
  155. struct s2n_blob *ticket_nonce, struct s2n_blob *secret_blob)
  156. {
  157. RESULT_ENSURE_REF(keys);
  158. RESULT_ENSURE_REF(resumption_secret);
  159. RESULT_ENSURE_REF(ticket_nonce);
  160. RESULT_ENSURE_REF(secret_blob);
  161. /* Derive session ticket secret from master session resumption secret */
  162. RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&keys->hmac, keys->hmac_algorithm, resumption_secret,
  163. &s2n_tls13_label_session_ticket_secret, ticket_nonce, secret_blob));
  164. return S2N_RESULT_OK;
  165. }