s2n_client_key_exchange.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <sys/param.h>
  16. #include "api/s2n.h"
  17. #include "crypto/s2n_dhe.h"
  18. #include "crypto/s2n_pkey.h"
  19. #include "crypto/s2n_rsa.h"
  20. #include "error/s2n_errno.h"
  21. #include "stuffer/s2n_stuffer.h"
  22. #include "tls/s2n_async_pkey.h"
  23. #include "tls/s2n_cipher_suites.h"
  24. #include "tls/s2n_connection.h"
  25. #include "tls/s2n_handshake.h"
  26. #include "tls/s2n_kem.h"
  27. #include "tls/s2n_kex.h"
  28. #include "tls/s2n_key_log.h"
  29. #include "tls/s2n_resume.h"
  30. #include "utils/s2n_random.h"
  31. #include "utils/s2n_safety.h"
  32. #define get_client_hello_protocol_version(conn) (conn->client_hello_version == S2N_SSLv2 ? conn->client_protocol_version : conn->client_hello_version)
  33. typedef S2N_RESULT s2n_kex_client_key_method(const struct s2n_kex *kex, struct s2n_connection *conn, struct s2n_blob *shared_key);
  34. typedef void *s2n_stuffer_action(struct s2n_stuffer *stuffer, uint32_t data_len);
  35. static int s2n_rsa_client_key_recv_complete(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *shared_key);
  36. static int s2n_hybrid_client_action(struct s2n_connection *conn, struct s2n_blob *combined_shared_key,
  37. s2n_kex_client_key_method kex_method, uint32_t *cursor, s2n_stuffer_action stuffer_action)
  38. {
  39. POSIX_ENSURE_REF(conn);
  40. POSIX_ENSURE_REF(conn->secure);
  41. POSIX_ENSURE_REF(kex_method);
  42. POSIX_ENSURE_REF(stuffer_action);
  43. struct s2n_stuffer *io = &conn->handshake.io;
  44. const struct s2n_kex *hybrid_kex_0 = conn->secure->cipher_suite->key_exchange_alg->hybrid[0];
  45. const struct s2n_kex *hybrid_kex_1 = conn->secure->cipher_suite->key_exchange_alg->hybrid[1];
  46. /* Keep a copy to the start of the entire hybrid client key exchange message for the hybrid PRF */
  47. struct s2n_blob *client_key_exchange_message = &conn->kex_params.client_key_exchange_message;
  48. client_key_exchange_message->data = stuffer_action(io, 0);
  49. POSIX_ENSURE_REF(client_key_exchange_message->data);
  50. const uint32_t start_cursor = *cursor;
  51. DEFER_CLEANUP(struct s2n_blob shared_key_0 = { 0 }, s2n_free);
  52. POSIX_GUARD_RESULT(kex_method(hybrid_kex_0, conn, &shared_key_0));
  53. struct s2n_blob *shared_key_1 = &(conn->kex_params.kem_params.shared_secret);
  54. POSIX_GUARD_RESULT(kex_method(hybrid_kex_1, conn, shared_key_1));
  55. const uint32_t end_cursor = *cursor;
  56. POSIX_ENSURE_GTE(end_cursor, start_cursor);
  57. client_key_exchange_message->size = end_cursor - start_cursor;
  58. POSIX_GUARD(s2n_alloc(combined_shared_key, shared_key_0.size + shared_key_1->size));
  59. struct s2n_stuffer stuffer_combiner = { 0 };
  60. POSIX_GUARD(s2n_stuffer_init(&stuffer_combiner, combined_shared_key));
  61. POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, &shared_key_0));
  62. POSIX_GUARD(s2n_stuffer_write(&stuffer_combiner, shared_key_1));
  63. POSIX_GUARD(s2n_kem_free(&conn->kex_params.kem_params));
  64. return 0;
  65. }
  66. static int s2n_calculate_keys(struct s2n_connection *conn, struct s2n_blob *shared_key)
  67. {
  68. POSIX_ENSURE_REF(conn);
  69. POSIX_ENSURE_REF(conn->secure);
  70. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  71. /* Turn the pre-master secret into a master secret */
  72. POSIX_GUARD_RESULT(s2n_kex_tls_prf(conn->secure->cipher_suite->key_exchange_alg, conn, shared_key));
  73. /* Expand the keys */
  74. POSIX_GUARD(s2n_prf_key_expansion(conn));
  75. /* Save the master secret in the cache.
  76. * Failing to cache the session should not affect the current handshake.
  77. */
  78. if (s2n_allowed_to_cache_connection(conn)) {
  79. s2n_result_ignore(s2n_store_to_cache(conn));
  80. }
  81. /* log the secret, if needed */
  82. s2n_result_ignore(s2n_key_log_tls12_secret(conn));
  83. return 0;
  84. }
  85. int s2n_rsa_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key)
  86. {
  87. /* Set shared_key before async guard to pass the proper shared_key to the caller upon async completion */
  88. POSIX_ENSURE_REF(shared_key);
  89. shared_key->data = conn->secrets.version.tls12.rsa_premaster_secret;
  90. shared_key->size = S2N_TLS_SECRET_LEN;
  91. S2N_ASYNC_PKEY_GUARD(conn);
  92. struct s2n_stuffer *in = &conn->handshake.io;
  93. uint8_t client_hello_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
  94. uint16_t length;
  95. if (conn->actual_protocol_version == S2N_SSLv3) {
  96. length = s2n_stuffer_data_available(in);
  97. } else {
  98. POSIX_GUARD(s2n_stuffer_read_uint16(in, &length));
  99. }
  100. S2N_ERROR_IF(length > s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE);
  101. /* Keep a copy of the client hello version in wire format, which should be
  102. * either the protocol version supported by client if the supported version is <= TLS1.2,
  103. * or TLS1.2 (the legacy version) if client supported version is TLS1.3
  104. */
  105. uint8_t legacy_client_hello_protocol_version = get_client_hello_protocol_version(conn);
  106. client_hello_protocol_version[0] = legacy_client_hello_protocol_version / 10;
  107. client_hello_protocol_version[1] = legacy_client_hello_protocol_version % 10;
  108. /* Decrypt the pre-master secret */
  109. struct s2n_blob encrypted = { 0 };
  110. POSIX_GUARD(s2n_blob_init(&encrypted, s2n_stuffer_raw_read(in, length), length));
  111. POSIX_ENSURE_REF(encrypted.data);
  112. POSIX_ENSURE_GT(encrypted.size, 0);
  113. /* First: use a random pre-master secret */
  114. POSIX_GUARD_RESULT(s2n_get_private_random_data(shared_key));
  115. conn->secrets.version.tls12.rsa_premaster_secret[0] = client_hello_protocol_version[0];
  116. conn->secrets.version.tls12.rsa_premaster_secret[1] = client_hello_protocol_version[1];
  117. S2N_ASYNC_PKEY_DECRYPT(conn, &encrypted, shared_key, s2n_rsa_client_key_recv_complete);
  118. }
  119. int s2n_rsa_client_key_recv_complete(struct s2n_connection *conn, bool rsa_failed, struct s2n_blob *decrypted)
  120. {
  121. S2N_ERROR_IF(decrypted->size != S2N_TLS_SECRET_LEN, S2N_ERR_SIZE_MISMATCH);
  122. /* Avoid copying the same buffer for the case where async pkey is not used */
  123. if (conn->secrets.version.tls12.rsa_premaster_secret != decrypted->data) {
  124. /* Copy (maybe) decrypted data into shared key */
  125. POSIX_CHECKED_MEMCPY(conn->secrets.version.tls12.rsa_premaster_secret, decrypted->data, S2N_TLS_SECRET_LEN);
  126. }
  127. /* Get client hello protocol version for comparison with decrypted data */
  128. uint8_t legacy_client_hello_protocol_version = get_client_hello_protocol_version(conn);
  129. uint8_t client_hello_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
  130. client_hello_protocol_version[0] = legacy_client_hello_protocol_version / 10;
  131. client_hello_protocol_version[1] = legacy_client_hello_protocol_version % 10;
  132. conn->handshake.rsa_failed = rsa_failed;
  133. /* Set rsa_failed to true, if it isn't already, if the protocol version isn't what we expect */
  134. conn->handshake.rsa_failed |= !s2n_constant_time_equals(client_hello_protocol_version,
  135. conn->secrets.version.tls12.rsa_premaster_secret, S2N_TLS_PROTOCOL_VERSION_LEN);
  136. return 0;
  137. }
  138. int s2n_dhe_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key)
  139. {
  140. struct s2n_stuffer *in = &conn->handshake.io;
  141. /* Get the shared key */
  142. POSIX_GUARD(s2n_dh_compute_shared_secret_as_server(&conn->kex_params.server_dh_params, in, shared_key));
  143. /* We don't need the server params any more */
  144. POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params));
  145. return 0;
  146. }
  147. int s2n_ecdhe_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key)
  148. {
  149. struct s2n_stuffer *in = &conn->handshake.io;
  150. /* Get the shared key */
  151. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_as_server(&conn->kex_params.server_ecc_evp_params, in, shared_key));
  152. /* We don't need the server params any more */
  153. POSIX_GUARD(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params));
  154. return 0;
  155. }
  156. int s2n_kem_client_key_recv(struct s2n_connection *conn, struct s2n_blob *shared_key)
  157. {
  158. /* s2n_kem_recv_ciphertext() writes the KEM shared secret directly to
  159. * conn->kex_params.kem_params. However, the calling function
  160. * likely expects *shared_key to point to the shared secret. We
  161. * can't reassign *shared_key to point to kem_params.shared_secret,
  162. * because that would require us to take struct s2n_blob **shared_key
  163. * as the argument, but we can't (easily) change the function signature
  164. * because it has to be consistent with what is defined in s2n_kex.
  165. *
  166. * So, we assert that the caller already has *shared_key pointing
  167. * to kem_params.shared_secret. */
  168. POSIX_ENSURE_REF(shared_key);
  169. S2N_ERROR_IF(shared_key != &(conn->kex_params.kem_params.shared_secret), S2N_ERR_SAFETY);
  170. conn->kex_params.kem_params.len_prefixed = true; /* PQ TLS 1.2 is always length prefixed. */
  171. POSIX_GUARD(s2n_kem_recv_ciphertext(&(conn->handshake.io), &(conn->kex_params.kem_params)));
  172. return 0;
  173. }
  174. int s2n_hybrid_client_key_recv(struct s2n_connection *conn, struct s2n_blob *combined_shared_key)
  175. {
  176. return s2n_hybrid_client_action(conn, combined_shared_key, &s2n_kex_client_key_recv, &conn->handshake.io.read_cursor,
  177. &s2n_stuffer_raw_read);
  178. }
  179. int s2n_client_key_recv(struct s2n_connection *conn)
  180. {
  181. POSIX_ENSURE_REF(conn);
  182. POSIX_ENSURE_REF(conn->secure);
  183. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  184. const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg;
  185. DEFER_CLEANUP(struct s2n_blob shared_key = { 0 }, s2n_free_or_wipe);
  186. POSIX_GUARD_RESULT(s2n_kex_client_key_recv(key_exchange, conn, &shared_key));
  187. POSIX_GUARD(s2n_calculate_keys(conn, &shared_key));
  188. return 0;
  189. }
  190. int s2n_dhe_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key)
  191. {
  192. struct s2n_stuffer *out = &conn->handshake.io;
  193. POSIX_GUARD(s2n_dh_compute_shared_secret_as_client(&conn->kex_params.server_dh_params, out, shared_key));
  194. /* We don't need the server params any more */
  195. POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params));
  196. return 0;
  197. }
  198. int s2n_ecdhe_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key)
  199. {
  200. struct s2n_stuffer *out = &conn->handshake.io;
  201. POSIX_GUARD(s2n_ecc_evp_compute_shared_secret_as_client(&conn->kex_params.server_ecc_evp_params, out, shared_key));
  202. /* We don't need the server params any more */
  203. POSIX_GUARD(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params));
  204. return 0;
  205. }
  206. int s2n_rsa_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key)
  207. {
  208. uint8_t client_hello_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
  209. uint8_t legacy_client_hello_protocol_version = get_client_hello_protocol_version(conn);
  210. client_hello_protocol_version[0] = legacy_client_hello_protocol_version / 10;
  211. client_hello_protocol_version[1] = legacy_client_hello_protocol_version % 10;
  212. shared_key->data = conn->secrets.version.tls12.rsa_premaster_secret;
  213. shared_key->size = S2N_TLS_SECRET_LEN;
  214. POSIX_GUARD_RESULT(s2n_get_private_random_data(shared_key));
  215. /* Over-write the first two bytes with the client hello version, per RFC2246/RFC4346/RFC5246 7.4.7.1.
  216. * The latest version supported by client (as seen from the the client hello version) are <= TLS1.2
  217. * for all clients, because TLS 1.3 clients freezes the TLS1.2 legacy version in client hello.
  218. */
  219. POSIX_CHECKED_MEMCPY(conn->secrets.version.tls12.rsa_premaster_secret, client_hello_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN);
  220. uint32_t encrypted_size = 0;
  221. POSIX_GUARD_RESULT(s2n_pkey_size(&conn->handshake_params.server_public_key, &encrypted_size));
  222. S2N_ERROR_IF(encrypted_size > 0xffff, S2N_ERR_SIZE_MISMATCH);
  223. if (conn->actual_protocol_version > S2N_SSLv3) {
  224. POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, encrypted_size));
  225. }
  226. struct s2n_blob encrypted = { 0 };
  227. encrypted.data = s2n_stuffer_raw_write(&conn->handshake.io, encrypted_size);
  228. encrypted.size = encrypted_size;
  229. POSIX_ENSURE_REF(encrypted.data);
  230. /* Encrypt the secret and send it on */
  231. POSIX_GUARD(s2n_pkey_encrypt(&conn->handshake_params.server_public_key, shared_key, &encrypted));
  232. /* We don't need the key any more, so free it */
  233. POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key));
  234. return 0;
  235. }
  236. int s2n_kem_client_key_send(struct s2n_connection *conn, struct s2n_blob *shared_key)
  237. {
  238. /* s2n_kem_send_ciphertext() writes the KEM shared secret directly to
  239. * conn->kex_params.kem_params. However, the calling function
  240. * likely expects *shared_key to point to the shared secret. We
  241. * can't reassign *shared_key to point to kem_params.shared_secret,
  242. * because that would require us to take struct s2n_blob **shared_key
  243. * as the argument, but we can't (easily) change the function signature
  244. * because it has to be consistent with what is defined in s2n_kex.
  245. *
  246. * So, we assert that the caller already has *shared_key pointing
  247. * to kem_params.shared_secret. */
  248. POSIX_ENSURE_REF(shared_key);
  249. S2N_ERROR_IF(shared_key != &(conn->kex_params.kem_params.shared_secret), S2N_ERR_SAFETY);
  250. conn->kex_params.kem_params.len_prefixed = true; /* PQ TLS 1.2 is always length prefixed */
  251. POSIX_GUARD(s2n_kem_send_ciphertext(&(conn->handshake.io), &(conn->kex_params.kem_params)));
  252. return 0;
  253. }
  254. int s2n_hybrid_client_key_send(struct s2n_connection *conn, struct s2n_blob *combined_shared_key)
  255. {
  256. return s2n_hybrid_client_action(conn, combined_shared_key, &s2n_kex_client_key_send, &conn->handshake.io.write_cursor,
  257. s2n_stuffer_raw_write);
  258. }
  259. int s2n_client_key_send(struct s2n_connection *conn)
  260. {
  261. POSIX_ENSURE_REF(conn);
  262. POSIX_ENSURE_REF(conn->secure);
  263. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  264. const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg;
  265. DEFER_CLEANUP(struct s2n_blob shared_key = { 0 }, s2n_free_or_wipe);
  266. POSIX_GUARD_RESULT(s2n_kex_client_key_send(key_exchange, conn, &shared_key));
  267. POSIX_GUARD(s2n_calculate_keys(conn, &shared_key));
  268. return 0;
  269. }