s2n_handshake.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <stdint.h>
  16. #include "error/s2n_errno.h"
  17. #include "stuffer/s2n_stuffer.h"
  18. #include "tls/s2n_cipher_suites.h"
  19. #include "tls/s2n_connection.h"
  20. #include "tls/s2n_record.h"
  21. #include "tls/s2n_tls.h"
  22. #include "utils/s2n_map.h"
  23. #include "utils/s2n_safety.h"
  24. int s2n_handshake_write_header(struct s2n_stuffer *out, uint8_t message_type)
  25. {
  26. S2N_ERROR_IF(s2n_stuffer_data_available(out), S2N_ERR_HANDSHAKE_STATE);
  27. /* Write the message header */
  28. POSIX_GUARD(s2n_stuffer_write_uint8(out, message_type));
  29. /* Leave the length blank for now */
  30. uint16_t length = 0;
  31. POSIX_GUARD(s2n_stuffer_write_uint24(out, length));
  32. return S2N_SUCCESS;
  33. }
  34. int s2n_handshake_finish_header(struct s2n_stuffer *out)
  35. {
  36. uint16_t length = s2n_stuffer_data_available(out);
  37. S2N_ERROR_IF(length < TLS_HANDSHAKE_HEADER_LENGTH, S2N_ERR_SIZE_MISMATCH);
  38. uint16_t payload = length - TLS_HANDSHAKE_HEADER_LENGTH;
  39. /* Write the message header */
  40. POSIX_GUARD(s2n_stuffer_rewrite(out));
  41. POSIX_GUARD(s2n_stuffer_skip_write(out, 1));
  42. POSIX_GUARD(s2n_stuffer_write_uint24(out, payload));
  43. POSIX_GUARD(s2n_stuffer_skip_write(out, payload));
  44. return S2N_SUCCESS;
  45. }
  46. S2N_RESULT s2n_handshake_parse_header(struct s2n_stuffer *io, uint8_t *message_type, uint32_t *length)
  47. {
  48. RESULT_ENSURE(s2n_stuffer_data_available(io) >= TLS_HANDSHAKE_HEADER_LENGTH, S2N_ERR_SIZE_MISMATCH);
  49. /* read the message header */
  50. RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(io, message_type));
  51. RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(io, length));
  52. return S2N_RESULT_OK;
  53. }
  54. static int s2n_handshake_get_hash_state_ptr(struct s2n_connection *conn, s2n_hash_algorithm hash_alg, struct s2n_hash_state **hash_state)
  55. {
  56. POSIX_ENSURE_REF(conn);
  57. POSIX_ENSURE_REF(conn->handshake.hashes);
  58. switch (hash_alg) {
  59. case S2N_HASH_MD5:
  60. *hash_state = &conn->handshake.hashes->md5;
  61. break;
  62. case S2N_HASH_SHA1:
  63. *hash_state = &conn->handshake.hashes->sha1;
  64. break;
  65. case S2N_HASH_SHA224:
  66. *hash_state = &conn->handshake.hashes->sha224;
  67. break;
  68. case S2N_HASH_SHA256:
  69. *hash_state = &conn->handshake.hashes->sha256;
  70. break;
  71. case S2N_HASH_SHA384:
  72. *hash_state = &conn->handshake.hashes->sha384;
  73. break;
  74. case S2N_HASH_SHA512:
  75. *hash_state = &conn->handshake.hashes->sha512;
  76. break;
  77. case S2N_HASH_MD5_SHA1:
  78. *hash_state = &conn->handshake.hashes->md5_sha1;
  79. break;
  80. default:
  81. POSIX_BAIL(S2N_ERR_HASH_INVALID_ALGORITHM);
  82. break;
  83. }
  84. return S2N_SUCCESS;
  85. }
  86. S2N_RESULT s2n_handshake_reset_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg)
  87. {
  88. struct s2n_hash_state *hash_state = NULL;
  89. RESULT_GUARD_POSIX(s2n_handshake_get_hash_state_ptr(conn, hash_alg, &hash_state));
  90. RESULT_GUARD_POSIX(s2n_hash_reset(hash_state));
  91. return S2N_RESULT_OK;
  92. }
  93. S2N_RESULT s2n_handshake_copy_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg, struct s2n_hash_state *copy)
  94. {
  95. struct s2n_hash_state *hash_state = NULL;
  96. RESULT_GUARD_POSIX(s2n_handshake_get_hash_state_ptr(conn, hash_alg, &hash_state));
  97. RESULT_GUARD_POSIX(s2n_hash_copy(copy, hash_state));
  98. return S2N_RESULT_OK;
  99. }
  100. int s2n_handshake_require_all_hashes(struct s2n_handshake *handshake)
  101. {
  102. memset(handshake->required_hash_algs, 1, sizeof(handshake->required_hash_algs));
  103. return S2N_SUCCESS;
  104. }
  105. static int s2n_handshake_require_hash(struct s2n_handshake *handshake, s2n_hash_algorithm hash_alg)
  106. {
  107. handshake->required_hash_algs[hash_alg] = 1;
  108. return S2N_SUCCESS;
  109. }
  110. uint8_t s2n_handshake_is_hash_required(struct s2n_handshake *handshake, s2n_hash_algorithm hash_alg)
  111. {
  112. return handshake->required_hash_algs[hash_alg];
  113. }
  114. /* Update the required handshake hash algs depending on current handshake session state.
  115. * This function must called at the end of a handshake message handler. Additionally it must be called after the
  116. * ClientHello or ServerHello is processed in client and server mode respectively. The relevant handshake parameters
  117. * are not available until those messages are processed.
  118. */
  119. int s2n_conn_update_required_handshake_hashes(struct s2n_connection *conn)
  120. {
  121. POSIX_ENSURE_REF(conn);
  122. POSIX_ENSURE_REF(conn->secure);
  123. /* Clear all of the required hashes */
  124. memset(conn->handshake.required_hash_algs, 0, sizeof(conn->handshake.required_hash_algs));
  125. message_type_t handshake_message = s2n_conn_get_current_message_type(conn);
  126. const uint8_t client_cert_verify_done = (handshake_message >= CLIENT_CERT_VERIFY) ? 1 : 0;
  127. s2n_cert_auth_type client_cert_auth_type;
  128. POSIX_GUARD(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type));
  129. /* If client authentication is possible, all hashes are needed until we're past CLIENT_CERT_VERIFY. */
  130. if ((client_cert_auth_type != S2N_CERT_AUTH_NONE) && !client_cert_verify_done) {
  131. POSIX_GUARD(s2n_handshake_require_all_hashes(&conn->handshake));
  132. return S2N_SUCCESS;
  133. }
  134. /* We don't need all of the hashes. Set the hash alg(s) required for the PRF */
  135. switch (conn->actual_protocol_version) {
  136. case S2N_SSLv3:
  137. case S2N_TLS10:
  138. case S2N_TLS11:
  139. POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, S2N_HASH_MD5));
  140. POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, S2N_HASH_SHA1));
  141. break;
  142. case S2N_TLS12:
  143. /* fall through */
  144. case S2N_TLS13: {
  145. /* For TLS 1.2 and TLS 1.3, the cipher suite defines the PRF hash alg */
  146. s2n_hmac_algorithm prf_alg = conn->secure->cipher_suite->prf_alg;
  147. s2n_hash_algorithm hash_alg;
  148. POSIX_GUARD(s2n_hmac_hash_alg(prf_alg, &hash_alg));
  149. POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, hash_alg));
  150. break;
  151. }
  152. }
  153. return S2N_SUCCESS;
  154. }
  155. /*
  156. * Take a hostname and return a single "simple" wildcard domain name that matches it.
  157. * The output wildcard representation is meant to be compared directly against a wildcard domain in a certificate.
  158. * We take a restrictive definition of wildcard here to achieve a single unique wildcard representation
  159. * given any input hostname.
  160. * No embedded or trailing wildcards are supported. Additionally, we only support one level of wildcard matching.
  161. * Thus the output should be a single wildcard character in the first(left-most) DNS label.
  162. *
  163. * Example:
  164. * - my.domain.name -> *.domain.name
  165. *
  166. * Not supported:
  167. * - my.domain.name -> m*.domain.name
  168. * - my.domain.name -> my.*.name
  169. * etc.
  170. *
  171. * The motivation for using a constrained definition of wildcard:
  172. * - Support for issuing non-simple wildcard certificates is insignificant.
  173. * - Certificate selection can be implemented with a constant number of lookups(two).
  174. */
  175. int s2n_create_wildcard_hostname(struct s2n_stuffer *hostname_stuffer, struct s2n_stuffer *output)
  176. {
  177. /* Find the end of the first label */
  178. POSIX_GUARD(s2n_stuffer_skip_to_char(hostname_stuffer, '.'));
  179. /* No first label found */
  180. if (s2n_stuffer_data_available(hostname_stuffer) == 0) {
  181. return S2N_SUCCESS;
  182. }
  183. /* Slap a single wildcard character to be the first label in output */
  184. POSIX_GUARD(s2n_stuffer_write_uint8(output, '*'));
  185. /* Simply copy the rest of the input to the output. */
  186. POSIX_GUARD(s2n_stuffer_copy(hostname_stuffer, output, s2n_stuffer_data_available(hostname_stuffer)));
  187. return S2N_SUCCESS;
  188. }
  189. static int s2n_find_cert_matches(struct s2n_map *domain_name_to_cert_map,
  190. struct s2n_blob *dns_name,
  191. struct s2n_cert_chain_and_key *matches[S2N_CERT_TYPE_COUNT],
  192. uint8_t *match_exists)
  193. {
  194. struct s2n_blob map_value = { 0 };
  195. bool key_found = false;
  196. POSIX_GUARD_RESULT(s2n_map_lookup(domain_name_to_cert_map, dns_name, &map_value, &key_found));
  197. if (key_found) {
  198. struct certs_by_type *value = (void *) map_value.data;
  199. for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
  200. matches[i] = value->certs[i];
  201. }
  202. *match_exists = 1;
  203. }
  204. return S2N_SUCCESS;
  205. }
  206. /* Find certificates that match the ServerName TLS extension sent by the client.
  207. * For a given ServerName there can be multiple matching certificates based on the
  208. * type of key in the certificate.
  209. *
  210. * A match is determined using s2n_map lookup by DNS name.
  211. * Wildcards that have a single * in the left most label are supported.
  212. */
  213. int s2n_conn_find_name_matching_certs(struct s2n_connection *conn)
  214. {
  215. if (!s2n_server_received_server_name(conn)) {
  216. return S2N_SUCCESS;
  217. }
  218. const char *name = conn->server_name;
  219. struct s2n_blob hostname_blob = { 0 };
  220. POSIX_GUARD(s2n_blob_init(&hostname_blob, (uint8_t *) (uintptr_t) name, strlen(name)));
  221. POSIX_ENSURE_LTE(hostname_blob.size, S2N_MAX_SERVER_NAME);
  222. char normalized_hostname[S2N_MAX_SERVER_NAME + 1] = { 0 };
  223. POSIX_CHECKED_MEMCPY(normalized_hostname, hostname_blob.data, hostname_blob.size);
  224. struct s2n_blob normalized_name = { 0 };
  225. POSIX_GUARD(s2n_blob_init(&normalized_name, (uint8_t *) normalized_hostname, hostname_blob.size));
  226. POSIX_GUARD(s2n_blob_char_to_lower(&normalized_name));
  227. struct s2n_stuffer normalized_hostname_stuffer = { 0 };
  228. POSIX_GUARD(s2n_stuffer_init(&normalized_hostname_stuffer, &normalized_name));
  229. POSIX_GUARD(s2n_stuffer_skip_write(&normalized_hostname_stuffer, normalized_name.size));
  230. /* Find the exact matches for the ServerName */
  231. POSIX_GUARD(s2n_find_cert_matches(conn->config->domain_name_to_cert_map,
  232. &normalized_name,
  233. conn->handshake_params.exact_sni_matches,
  234. &(conn->handshake_params.exact_sni_match_exists)));
  235. if (!conn->handshake_params.exact_sni_match_exists) {
  236. /* We have not yet found an exact domain match. Try to find wildcard matches. */
  237. char wildcard_hostname[S2N_MAX_SERVER_NAME + 1] = { 0 };
  238. struct s2n_blob wildcard_blob = { 0 };
  239. POSIX_GUARD(s2n_blob_init(&wildcard_blob, (uint8_t *) wildcard_hostname, sizeof(wildcard_hostname)));
  240. struct s2n_stuffer wildcard_stuffer = { 0 };
  241. POSIX_GUARD(s2n_stuffer_init(&wildcard_stuffer, &wildcard_blob));
  242. POSIX_GUARD(s2n_create_wildcard_hostname(&normalized_hostname_stuffer, &wildcard_stuffer));
  243. const uint32_t wildcard_len = s2n_stuffer_data_available(&wildcard_stuffer);
  244. /* Couldn't create a valid wildcard from the input */
  245. if (wildcard_len == 0) {
  246. return S2N_SUCCESS;
  247. }
  248. /* The client's SNI is wildcardified, do an exact match against the set of server certs. */
  249. wildcard_blob.size = wildcard_len;
  250. POSIX_GUARD(s2n_find_cert_matches(conn->config->domain_name_to_cert_map,
  251. &wildcard_blob,
  252. conn->handshake_params.wc_sni_matches,
  253. &(conn->handshake_params.wc_sni_match_exists)));
  254. }
  255. /* If we found a suitable cert, we should send back the ServerName extension.
  256. * Note that this may have already been set by the client hello callback, so we won't override its value
  257. */
  258. conn->server_name_used = conn->server_name_used
  259. || conn->handshake_params.exact_sni_match_exists
  260. || conn->handshake_params.wc_sni_match_exists;
  261. return S2N_SUCCESS;
  262. }
  263. /* Find the optimal certificate of a specific type.
  264. * The priority of set of certificates to choose from:
  265. * 1. Certificates that match the client's ServerName extension.
  266. * 2. Default certificates
  267. */
  268. struct s2n_cert_chain_and_key *s2n_get_compatible_cert_chain_and_key(struct s2n_connection *conn, const s2n_pkey_type cert_type)
  269. {
  270. if (conn->handshake_params.exact_sni_match_exists) {
  271. /* This may return NULL if there was an SNI match, but not a match the cipher_suite's authentication type. */
  272. return conn->handshake_params.exact_sni_matches[cert_type];
  273. }
  274. if (conn->handshake_params.wc_sni_match_exists) {
  275. return conn->handshake_params.wc_sni_matches[cert_type];
  276. } else {
  277. /* We don't have any name matches. Use the default certificate that works with the key type. */
  278. return conn->config->default_certs_by_type.certs[cert_type];
  279. }
  280. }
  281. /* This method will work when testing S2N, and for the EndOfEarlyData message.
  282. *
  283. * However, it will NOT work for arbitrary message types when potentially receiving records
  284. * that contain multiple messages, like when talking to a non-S2N TLS implementation. If the "end_message"
  285. * is not the first message in a multi-message record, negotiation will not stop.
  286. * (This is not an issue for EndOfEarlyData because encryption and message order requirements force
  287. * EndOfEarlyData to always be the first and only handshake message in its handshake record)
  288. */
  289. S2N_RESULT s2n_negotiate_until_message(struct s2n_connection *conn, s2n_blocked_status *blocked, message_type_t end_message)
  290. {
  291. RESULT_ENSURE_REF(conn);
  292. conn->handshake.end_of_messages = end_message;
  293. int r = s2n_negotiate(conn, blocked);
  294. conn->handshake.end_of_messages = APPLICATION_DATA;
  295. RESULT_GUARD_POSIX(r);
  296. return S2N_RESULT_OK;
  297. }
  298. S2N_RESULT s2n_handshake_validate(const struct s2n_handshake *s2n_handshake)
  299. {
  300. RESULT_ENSURE_REF(s2n_handshake);
  301. RESULT_DEBUG_ENSURE(s2n_handshake->handshake_type < 256, S2N_ERR_SAFETY);
  302. RESULT_DEBUG_ENSURE(s2n_handshake->message_number >= 0 && s2n_handshake->message_number < 32, S2N_ERR_SAFETY);
  303. return S2N_RESULT_OK;
  304. }
  305. S2N_RESULT s2n_handshake_set_finished_len(struct s2n_connection *conn, uint8_t len)
  306. {
  307. RESULT_ENSURE_REF(conn);
  308. RESULT_ENSURE_GT(len, 0);
  309. RESULT_ENSURE_LTE(len, sizeof(conn->handshake.server_finished));
  310. RESULT_ENSURE_LTE(len, sizeof(conn->handshake.client_finished));
  311. /*
  312. * We maintain a version of the "finished" / "verify_data" field
  313. * for both the client and server, so this method will be called
  314. * once for the client version and once for the server version.
  315. *
  316. * The lengths of both versions must match, or something has
  317. * gone wrong in our implementation.
  318. */
  319. uint8_t *finished_length = &conn->handshake.finished_len;
  320. if (*finished_length == 0) {
  321. *finished_length = len;
  322. }
  323. RESULT_ENSURE_EQ(*finished_length, len);
  324. return S2N_RESULT_OK;
  325. }
  326. bool s2n_handshake_is_renegotiation(struct s2n_connection *conn)
  327. {
  328. return conn && conn->handshake.renegotiation;
  329. }