s2n_handshake.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <stdint.h>
  17. #include "api/s2n.h"
  18. #include "crypto/s2n_certificate.h"
  19. #include "crypto/s2n_hash.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #include "tls/s2n_crypto.h"
  22. #include "tls/s2n_handshake_hashes.h"
  23. #include "tls/s2n_handshake_type.h"
  24. #include "tls/s2n_signature_algorithms.h"
  25. #include "tls/s2n_tls_parameters.h"
  26. /* From RFC 8446: https://tools.ietf.org/html/rfc8446#appendix-B.3 */
  27. #define TLS_HELLO_REQUEST 0
  28. #define TLS_CLIENT_HELLO 1
  29. #define TLS_SERVER_HELLO 2
  30. #define TLS_SERVER_NEW_SESSION_TICKET 4
  31. #define TLS_END_OF_EARLY_DATA 5
  32. #define TLS_ENCRYPTED_EXTENSIONS 8
  33. #define TLS_CERTIFICATE 11
  34. #define TLS_SERVER_KEY 12
  35. #define TLS_CERT_REQ 13
  36. #define TLS_SERVER_HELLO_DONE 14
  37. #define TLS_CERT_VERIFY 15
  38. #define TLS_CLIENT_KEY 16
  39. #define TLS_FINISHED 20
  40. #define TLS_SERVER_CERT_STATUS 22
  41. #define TLS_SERVER_SESSION_LOOKUP 23
  42. #define TLS_KEY_UPDATE 24
  43. #define TLS_NPN 67
  44. #define TLS_MESSAGE_HASH 254
  45. /* This is the list of message types that we support */
  46. typedef enum {
  47. CLIENT_HELLO = 0,
  48. SERVER_HELLO,
  49. SERVER_CERT,
  50. SERVER_NEW_SESSION_TICKET,
  51. SERVER_CERT_STATUS,
  52. SERVER_KEY,
  53. SERVER_CERT_REQ,
  54. SERVER_HELLO_DONE,
  55. CLIENT_CERT,
  56. CLIENT_KEY,
  57. CLIENT_CERT_VERIFY,
  58. CLIENT_CHANGE_CIPHER_SPEC,
  59. /* Not a standardized message. Defined: https://datatracker.ietf.org/doc/html/draft-agl-tls-nextprotoneg-04 */
  60. CLIENT_NPN,
  61. CLIENT_FINISHED,
  62. SERVER_CHANGE_CIPHER_SPEC,
  63. SERVER_FINISHED,
  64. /* TLS1.3 message types. Defined: https://tools.ietf.org/html/rfc8446#appendix-B.3 */
  65. ENCRYPTED_EXTENSIONS,
  66. SERVER_CERT_VERIFY,
  67. HELLO_RETRY_MSG,
  68. END_OF_EARLY_DATA,
  69. APPLICATION_DATA,
  70. } message_type_t;
  71. typedef enum {
  72. S2N_ASYNC_NOT_INVOKED = 0,
  73. S2N_ASYNC_INVOKED,
  74. S2N_ASYNC_COMPLETE,
  75. } s2n_async_state;
  76. /* Indicates which state machine is being used. The handshake
  77. * starts off on the initial enum, which indicates we're using
  78. * the TLS12 state machine. Once the handshake version is determined
  79. * the enum is set to either the TLS12 or TLS13 state machine.
  80. * This works because the initial entries in both the TLS12 and
  81. * TLS13 state machines are the same. */
  82. typedef enum {
  83. S2N_STATE_MACHINE_INITIAL = 0,
  84. S2N_STATE_MACHINE_TLS12,
  85. S2N_STATE_MACHINE_TLS13,
  86. } s2n_state_machine;
  87. struct s2n_handshake_parameters {
  88. /* Public keys for server / client */
  89. struct s2n_pkey server_public_key;
  90. struct s2n_pkey client_public_key;
  91. struct s2n_blob client_cert_chain;
  92. s2n_pkey_type client_cert_pkey_type;
  93. /* Signature/hash algorithm pairs offered by the client in the signature_algorithms extension */
  94. struct s2n_sig_scheme_list client_sig_hash_algs;
  95. /* Signature scheme chosen by the server */
  96. const struct s2n_signature_scheme *server_cert_sig_scheme;
  97. /* Signature/hash algorithm pairs offered by the server in the certificate request */
  98. struct s2n_sig_scheme_list server_sig_hash_algs;
  99. /* Signature scheme chosen by the client */
  100. const struct s2n_signature_scheme *client_cert_sig_scheme;
  101. /* The cert chain we will send the peer. */
  102. struct s2n_cert_chain_and_key *our_chain_and_key;
  103. /* The subset of certificates that match the server_name presented in the ClientHello.
  104. * In the case of multiple certificates matching a server_name, s2n will prefer certificates
  105. * in FIFO order based on calls to s2n_config_add_cert_chain_and_key_to_store
  106. *
  107. * Note that in addition to domain matching, the key type for the certificate must also be
  108. * suitable for a negotiation in order to be selected. The set of matching certs here are indexed
  109. * by s2n_authentication_method.
  110. *
  111. * Example:
  112. * - Assume certA is added to s2n_config via s2n_config_add_cert_chain_and_key_to_store
  113. * - Next certB is added.
  114. * - if certA matches www.foo.com and certB matches www.foo.com, s2n will prefer certA
  115. *
  116. * Note that in addition to domain matching, the key type for the certificate must also be
  117. * suitable for a negotiation in order to be selected.
  118. *
  119. * Example:
  120. * - Assume certA and certB match server_name www.foo.com
  121. * - certA is ECDSA and certB is RSA.
  122. * - Client only supports RSA ciphers
  123. * - certB will be selected.
  124. */
  125. struct s2n_cert_chain_and_key *exact_sni_matches[S2N_CERT_TYPE_COUNT];
  126. struct s2n_cert_chain_and_key *wc_sni_matches[S2N_CERT_TYPE_COUNT];
  127. uint8_t exact_sni_match_exists;
  128. uint8_t wc_sni_match_exists;
  129. uint8_t client_random[S2N_TLS_RANDOM_DATA_LEN];
  130. uint8_t server_random[S2N_TLS_RANDOM_DATA_LEN];
  131. };
  132. struct s2n_handshake {
  133. struct s2n_stuffer io;
  134. struct s2n_handshake_hashes *hashes;
  135. /* Hash algorithms required for this handshake. The set of required hashes can be reduced as session parameters are
  136. * negotiated, i.e. cipher suite and protocol version.
  137. */
  138. uint8_t required_hash_algs[S2N_HASH_SENTINEL];
  139. /*
  140. * Data required by the Finished messages.
  141. * In TLS1.2 and earlier, the data is the verify_data.
  142. * In TLS1.3, the data is the finished_key used to calculate the verify_data.
  143. *
  144. * The data will be different for the client and server.
  145. * The length of the data will be the same for the client and server.
  146. * The length of the data depends on protocol version and cipher suite.
  147. */
  148. uint8_t server_finished[S2N_TLS_SECRET_LEN];
  149. uint8_t client_finished[S2N_TLS_SECRET_LEN];
  150. uint8_t finished_len;
  151. /* Which message-order affecting features are enabled */
  152. uint32_t handshake_type;
  153. /* Which handshake message number are we processing */
  154. int message_number;
  155. /* Last message in the handshake. Unless using early data or testing,
  156. * should always be APPLICATION_DATA. */
  157. message_type_t end_of_messages;
  158. /* State of the async pkey operation during handshake */
  159. s2n_async_state async_state;
  160. /* State of the async early data callback.
  161. * If not initialized, then the callback has not been triggered yet. */
  162. struct s2n_offered_early_data early_data_async_state;
  163. /* Indicates the CLIENT_HELLO message has been completely received */
  164. unsigned client_hello_received : 1;
  165. /* Indicates the handshake blocked while trying to read or write data, and has been paused */
  166. unsigned paused : 1;
  167. /* Set to 1 if the RSA verification failed */
  168. unsigned rsa_failed : 1;
  169. /* Indicates that this is a renegotiation handshake */
  170. unsigned renegotiation : 1;
  171. s2n_state_machine state_machine;
  172. };
  173. /* Only used in our test cases. */
  174. message_type_t s2n_conn_get_current_message_type(struct s2n_connection *conn);
  175. /* s2n_handshake */
  176. int s2n_handshake_require_all_hashes(struct s2n_handshake *handshake);
  177. uint8_t s2n_handshake_is_hash_required(struct s2n_handshake *handshake, s2n_hash_algorithm hash_alg);
  178. int s2n_conn_update_required_handshake_hashes(struct s2n_connection *conn);
  179. S2N_RESULT s2n_handshake_copy_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg, struct s2n_hash_state *hash_state);
  180. S2N_RESULT s2n_handshake_reset_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg);
  181. int s2n_conn_find_name_matching_certs(struct s2n_connection *conn);
  182. int s2n_create_wildcard_hostname(struct s2n_stuffer *hostname, struct s2n_stuffer *output);
  183. struct s2n_cert_chain_and_key *s2n_get_compatible_cert_chain_and_key(struct s2n_connection *conn, const s2n_pkey_type cert_type);
  184. S2N_RESULT s2n_negotiate_until_message(struct s2n_connection *conn, s2n_blocked_status *blocked, message_type_t end_message);
  185. S2N_RESULT s2n_handshake_validate(const struct s2n_handshake *s2n_handshake);
  186. S2N_RESULT s2n_handshake_set_finished_len(struct s2n_connection *conn, uint8_t len);
  187. bool s2n_handshake_is_renegotiation(struct s2n_connection *conn);
  188. S2N_RESULT s2n_handshake_message_send(struct s2n_connection *conn, uint8_t content_type, s2n_blocked_status *blocked);
  189. /* s2n_handshake_io */
  190. int s2n_conn_set_handshake_type(struct s2n_connection *conn);
  191. int s2n_conn_set_handshake_no_client_cert(struct s2n_connection *conn);
  192. S2N_RESULT s2n_conn_choose_state_machine(struct s2n_connection *conn, uint8_t protocol_version);
  193. bool s2n_handshake_is_complete(struct s2n_connection *conn);
  194. /* s2n_handshake_transcript */
  195. S2N_RESULT s2n_handshake_transcript_update(struct s2n_connection *conn);
  196. int s2n_conn_update_handshake_hashes(struct s2n_connection *conn, struct s2n_blob *data);
  197. /* s2n_quic_support */
  198. S2N_RESULT s2n_quic_read_handshake_message(struct s2n_connection *conn, uint8_t *message_type);
  199. S2N_RESULT s2n_quic_write_handshake_message(struct s2n_connection *conn);