s2n_server_key_exchange.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "api/s2n.h"
  16. #include "crypto/s2n_dhe.h"
  17. #include "crypto/s2n_fips.h"
  18. #include "error/s2n_errno.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "tls/s2n_async_pkey.h"
  21. #include "tls/s2n_cipher_preferences.h"
  22. #include "tls/s2n_cipher_suites.h"
  23. #include "tls/s2n_connection.h"
  24. #include "tls/s2n_kem.h"
  25. #include "tls/s2n_kex.h"
  26. #include "tls/s2n_security_policies.h"
  27. #include "tls/s2n_signature_algorithms.h"
  28. #include "utils/s2n_random.h"
  29. #include "utils/s2n_safety.h"
  30. static int s2n_server_key_send_write_signature(struct s2n_connection *conn, struct s2n_blob *signature);
  31. int s2n_server_key_recv(struct s2n_connection *conn)
  32. {
  33. POSIX_ENSURE_REF(conn);
  34. POSIX_ENSURE_REF(conn->secure);
  35. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  36. POSIX_ENSURE_REF(conn->secure->cipher_suite->key_exchange_alg);
  37. POSIX_ENSURE_REF(conn->handshake.hashes);
  38. struct s2n_hash_state *signature_hash = &conn->handshake.hashes->hash_workspace;
  39. const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg;
  40. struct s2n_stuffer *in = &conn->handshake.io;
  41. struct s2n_blob data_to_verify = { 0 };
  42. /* Read the KEX data */
  43. struct s2n_kex_raw_server_data kex_data = { 0 };
  44. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_read_data(key_exchange, conn, &data_to_verify, &kex_data));
  45. POSIX_GUARD_RESULT(s2n_signature_algorithm_recv(conn, in));
  46. const struct s2n_signature_scheme *active_sig_scheme = conn->handshake_params.server_cert_sig_scheme;
  47. POSIX_ENSURE_REF(active_sig_scheme);
  48. /* FIPS specifically allows MD5 for <TLS1.2 */
  49. if (s2n_is_in_fips_mode() && conn->actual_protocol_version < S2N_TLS12) {
  50. POSIX_GUARD(s2n_hash_allow_md5_for_fips(signature_hash));
  51. }
  52. POSIX_GUARD(s2n_hash_init(signature_hash, active_sig_scheme->hash_alg));
  53. POSIX_GUARD(s2n_hash_update(signature_hash, conn->handshake_params.client_random, S2N_TLS_RANDOM_DATA_LEN));
  54. POSIX_GUARD(s2n_hash_update(signature_hash, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN));
  55. /* Add KEX specific data */
  56. POSIX_GUARD(s2n_hash_update(signature_hash, data_to_verify.data, data_to_verify.size));
  57. /* Verify the signature */
  58. uint16_t signature_length;
  59. POSIX_GUARD(s2n_stuffer_read_uint16(in, &signature_length));
  60. struct s2n_blob signature = { 0 };
  61. POSIX_GUARD(s2n_blob_init(&signature, s2n_stuffer_raw_read(in, signature_length), signature_length));
  62. POSIX_ENSURE_REF(signature.data);
  63. POSIX_ENSURE_GT(signature_length, 0);
  64. S2N_ERROR_IF(s2n_pkey_verify(&conn->handshake_params.server_public_key, active_sig_scheme->sig_alg, signature_hash, &signature) < 0,
  65. S2N_ERR_BAD_MESSAGE);
  66. /* We don't need the key any more, so free it */
  67. POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key));
  68. /* Parse the KEX data into whatever form needed and save it to the connection object */
  69. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_parse_data(key_exchange, conn, &kex_data));
  70. return 0;
  71. }
  72. int s2n_ecdhe_server_key_recv_read_data(struct s2n_connection *conn, struct s2n_blob *data_to_verify,
  73. struct s2n_kex_raw_server_data *raw_server_data)
  74. {
  75. struct s2n_stuffer *in = &conn->handshake.io;
  76. POSIX_GUARD(s2n_ecc_evp_read_params(in, data_to_verify, &raw_server_data->ecdhe_data));
  77. return 0;
  78. }
  79. int s2n_ecdhe_server_key_recv_parse_data(struct s2n_connection *conn, struct s2n_kex_raw_server_data *raw_server_data)
  80. {
  81. POSIX_GUARD(s2n_ecc_evp_parse_params(conn, &raw_server_data->ecdhe_data, &conn->kex_params.server_ecc_evp_params));
  82. return 0;
  83. }
  84. int s2n_dhe_server_key_recv_read_data(struct s2n_connection *conn, struct s2n_blob *data_to_verify,
  85. struct s2n_kex_raw_server_data *raw_server_data)
  86. {
  87. struct s2n_stuffer *in = &conn->handshake.io;
  88. struct s2n_dhe_raw_server_points *dhe_data = &raw_server_data->dhe_data;
  89. uint16_t p_length;
  90. uint16_t g_length;
  91. uint16_t Ys_length;
  92. /* Keep a copy to the start of the whole structure for the signature check */
  93. data_to_verify->data = s2n_stuffer_raw_read(in, 0);
  94. POSIX_ENSURE_REF(data_to_verify->data);
  95. /* Read each of the three elements in */
  96. POSIX_GUARD(s2n_stuffer_read_uint16(in, &p_length));
  97. dhe_data->p.size = p_length;
  98. dhe_data->p.data = s2n_stuffer_raw_read(in, p_length);
  99. POSIX_ENSURE_REF(dhe_data->p.data);
  100. POSIX_GUARD(s2n_stuffer_read_uint16(in, &g_length));
  101. dhe_data->g.size = g_length;
  102. dhe_data->g.data = s2n_stuffer_raw_read(in, g_length);
  103. POSIX_ENSURE_REF(dhe_data->g.data);
  104. POSIX_GUARD(s2n_stuffer_read_uint16(in, &Ys_length));
  105. dhe_data->Ys.size = Ys_length;
  106. dhe_data->Ys.data = s2n_stuffer_raw_read(in, Ys_length);
  107. POSIX_ENSURE_REF(dhe_data->Ys.data);
  108. /* Now we know the total size of the structure */
  109. data_to_verify->size = 2 + p_length + 2 + g_length + 2 + Ys_length;
  110. return 0;
  111. }
  112. int s2n_dhe_server_key_recv_parse_data(struct s2n_connection *conn, struct s2n_kex_raw_server_data *raw_server_data)
  113. {
  114. struct s2n_dhe_raw_server_points dhe_data = raw_server_data->dhe_data;
  115. /* Copy the DH details */
  116. POSIX_GUARD(s2n_dh_p_g_Ys_to_dh_params(&conn->kex_params.server_dh_params, &dhe_data.p, &dhe_data.g, &dhe_data.Ys));
  117. return 0;
  118. }
  119. int s2n_kem_server_key_recv_read_data(struct s2n_connection *conn, struct s2n_blob *data_to_verify,
  120. struct s2n_kex_raw_server_data *raw_server_data)
  121. {
  122. struct s2n_kem_raw_server_params *kem_data = &raw_server_data->kem_data;
  123. struct s2n_stuffer *in = &conn->handshake.io;
  124. /* Keep a copy to the start of the whole structure for the signature check */
  125. data_to_verify->data = s2n_stuffer_raw_read(in, 0);
  126. POSIX_ENSURE_REF(data_to_verify->data);
  127. /* the server sends the KEM ID */
  128. kem_data->kem_name.data = s2n_stuffer_raw_read(in, 2);
  129. POSIX_ENSURE_REF(kem_data->kem_name.data);
  130. kem_data->kem_name.size = 2;
  131. struct s2n_stuffer kem_id_stuffer = { 0 };
  132. uint8_t kem_id_arr[2];
  133. kem_extension_size kem_id;
  134. struct s2n_blob kem_id_blob = { 0 };
  135. POSIX_GUARD(s2n_blob_init(&kem_id_blob, kem_id_arr, s2n_array_len(kem_id_arr)));
  136. POSIX_GUARD(s2n_stuffer_init(&kem_id_stuffer, &kem_id_blob));
  137. POSIX_GUARD(s2n_stuffer_write(&kem_id_stuffer, &(kem_data->kem_name)));
  138. POSIX_GUARD(s2n_stuffer_read_uint16(&kem_id_stuffer, &kem_id));
  139. POSIX_GUARD(s2n_get_kem_from_extension_id(kem_id, &(conn->kex_params.kem_params.kem)));
  140. conn->kex_params.kem_params.len_prefixed = true; /* PQ TLS 1.2 is always length prefixed. */
  141. POSIX_GUARD(s2n_kem_recv_public_key(in, &(conn->kex_params.kem_params)));
  142. kem_data->raw_public_key.data = conn->kex_params.kem_params.public_key.data;
  143. kem_data->raw_public_key.size = conn->kex_params.kem_params.public_key.size;
  144. data_to_verify->size = sizeof(kem_extension_size) + sizeof(kem_public_key_size) + kem_data->raw_public_key.size;
  145. return 0;
  146. }
  147. int s2n_kem_server_key_recv_parse_data(struct s2n_connection *conn, struct s2n_kex_raw_server_data *raw_server_data)
  148. {
  149. POSIX_ENSURE_REF(conn);
  150. POSIX_ENSURE_REF(conn->secure);
  151. struct s2n_kem_raw_server_params *kem_data = &raw_server_data->kem_data;
  152. /* Check that the server's requested kem is supported by the client */
  153. const struct s2n_kem_preferences *kem_preferences = NULL;
  154. POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_preferences));
  155. POSIX_ENSURE_REF(kem_preferences);
  156. const struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
  157. const struct s2n_kem *match = NULL;
  158. S2N_ERROR_IF(s2n_choose_kem_with_peer_pref_list(cipher_suite->iana_value, &kem_data->kem_name,
  159. kem_preferences->kems, kem_preferences->kem_count, &match)
  160. != 0,
  161. S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  162. conn->kex_params.kem_params.kem = match;
  163. S2N_ERROR_IF(kem_data->raw_public_key.size != conn->kex_params.kem_params.kem->public_key_length,
  164. S2N_ERR_BAD_MESSAGE);
  165. return 0;
  166. }
  167. int s2n_hybrid_server_key_recv_read_data(struct s2n_connection *conn, struct s2n_blob *total_data_to_verify,
  168. struct s2n_kex_raw_server_data *raw_server_data)
  169. {
  170. POSIX_ENSURE_REF(conn);
  171. POSIX_ENSURE_REF(conn->secure);
  172. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  173. const struct s2n_kex *kex = conn->secure->cipher_suite->key_exchange_alg;
  174. const struct s2n_kex *hybrid_kex_0 = kex->hybrid[0];
  175. const struct s2n_kex *hybrid_kex_1 = kex->hybrid[1];
  176. /* Keep a copy to the start of the whole structure for the signature check */
  177. total_data_to_verify->data = s2n_stuffer_raw_read(&conn->handshake.io, 0);
  178. POSIX_ENSURE_REF(total_data_to_verify->data);
  179. struct s2n_blob data_to_verify_0 = { 0 };
  180. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_read_data(hybrid_kex_0, conn, &data_to_verify_0, raw_server_data));
  181. struct s2n_blob data_to_verify_1 = { 0 };
  182. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_read_data(hybrid_kex_1, conn, &data_to_verify_1, raw_server_data));
  183. total_data_to_verify->size = data_to_verify_0.size + data_to_verify_1.size;
  184. return 0;
  185. }
  186. int s2n_hybrid_server_key_recv_parse_data(struct s2n_connection *conn, struct s2n_kex_raw_server_data *raw_server_data)
  187. {
  188. POSIX_ENSURE_REF(conn);
  189. POSIX_ENSURE_REF(conn->secure);
  190. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  191. const struct s2n_kex *kex = conn->secure->cipher_suite->key_exchange_alg;
  192. const struct s2n_kex *hybrid_kex_0 = kex->hybrid[0];
  193. const struct s2n_kex *hybrid_kex_1 = kex->hybrid[1];
  194. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_parse_data(hybrid_kex_0, conn, raw_server_data));
  195. POSIX_GUARD_RESULT(s2n_kex_server_key_recv_parse_data(hybrid_kex_1, conn, raw_server_data));
  196. return 0;
  197. }
  198. int s2n_server_key_send(struct s2n_connection *conn)
  199. {
  200. POSIX_ENSURE_REF(conn);
  201. POSIX_ENSURE_REF(conn->handshake.hashes);
  202. S2N_ASYNC_PKEY_GUARD(conn);
  203. struct s2n_hash_state *signature_hash = &conn->handshake.hashes->hash_workspace;
  204. const struct s2n_kex *key_exchange = conn->secure->cipher_suite->key_exchange_alg;
  205. const struct s2n_signature_scheme *sig_scheme = conn->handshake_params.server_cert_sig_scheme;
  206. POSIX_ENSURE_REF(sig_scheme);
  207. struct s2n_stuffer *out = &conn->handshake.io;
  208. struct s2n_blob data_to_sign = { 0 };
  209. /* Call the negotiated key exchange method to send it's data */
  210. POSIX_GUARD_RESULT(s2n_kex_server_key_send(key_exchange, conn, &data_to_sign));
  211. /* Add common signature data */
  212. if (conn->actual_protocol_version == S2N_TLS12) {
  213. POSIX_GUARD(s2n_stuffer_write_uint16(out, sig_scheme->iana_value));
  214. }
  215. /* FIPS specifically allows MD5 for <TLS1.2 */
  216. if (s2n_is_in_fips_mode() && conn->actual_protocol_version < S2N_TLS12) {
  217. POSIX_GUARD(s2n_hash_allow_md5_for_fips(signature_hash));
  218. }
  219. /* Add the random data to the hash */
  220. POSIX_GUARD(s2n_hash_init(signature_hash, sig_scheme->hash_alg));
  221. POSIX_GUARD(s2n_hash_update(signature_hash, conn->handshake_params.client_random, S2N_TLS_RANDOM_DATA_LEN));
  222. POSIX_GUARD(s2n_hash_update(signature_hash, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN));
  223. /* Add KEX specific data to the hash */
  224. POSIX_GUARD(s2n_hash_update(signature_hash, data_to_sign.data, data_to_sign.size));
  225. S2N_ASYNC_PKEY_SIGN(conn, sig_scheme->sig_alg, signature_hash,
  226. s2n_server_key_send_write_signature);
  227. }
  228. int s2n_ecdhe_server_key_send(struct s2n_connection *conn, struct s2n_blob *data_to_sign)
  229. {
  230. struct s2n_stuffer *out = &conn->handshake.io;
  231. /* Generate an ephemeral key and */
  232. POSIX_GUARD(s2n_ecc_evp_generate_ephemeral_key(&conn->kex_params.server_ecc_evp_params));
  233. /* Write it out and calculate the data to sign later */
  234. POSIX_GUARD(s2n_ecc_evp_write_params(&conn->kex_params.server_ecc_evp_params, out, data_to_sign));
  235. return 0;
  236. }
  237. int s2n_dhe_server_key_send(struct s2n_connection *conn, struct s2n_blob *data_to_sign)
  238. {
  239. struct s2n_stuffer *out = &conn->handshake.io;
  240. /* Duplicate the DH key from the config */
  241. POSIX_GUARD(s2n_dh_params_copy(conn->config->dhparams, &conn->kex_params.server_dh_params));
  242. /* Generate an ephemeral key */
  243. POSIX_GUARD(s2n_dh_generate_ephemeral_key(&conn->kex_params.server_dh_params));
  244. /* Write it out and calculate the data to sign later */
  245. POSIX_GUARD(s2n_dh_params_to_p_g_Ys(&conn->kex_params.server_dh_params, out, data_to_sign));
  246. return 0;
  247. }
  248. int s2n_kem_server_key_send(struct s2n_connection *conn, struct s2n_blob *data_to_sign)
  249. {
  250. struct s2n_stuffer *out = &conn->handshake.io;
  251. const struct s2n_kem *kem = conn->kex_params.kem_params.kem;
  252. data_to_sign->data = s2n_stuffer_raw_write(out, 0);
  253. POSIX_ENSURE_REF(data_to_sign->data);
  254. POSIX_GUARD(s2n_stuffer_write_uint16(out, kem->kem_extension_id));
  255. conn->kex_params.kem_params.len_prefixed = true; /* PQ TLS 1.2 is always length prefixed. */
  256. POSIX_GUARD(s2n_kem_send_public_key(out, &(conn->kex_params.kem_params)));
  257. data_to_sign->size = sizeof(kem_extension_size) + sizeof(kem_public_key_size) + kem->public_key_length;
  258. return 0;
  259. }
  260. int s2n_hybrid_server_key_send(struct s2n_connection *conn, struct s2n_blob *total_data_to_sign)
  261. {
  262. POSIX_ENSURE_REF(conn);
  263. POSIX_ENSURE_REF(conn->secure);
  264. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  265. const struct s2n_kex *kex = conn->secure->cipher_suite->key_exchange_alg;
  266. const struct s2n_kex *hybrid_kex_0 = kex->hybrid[0];
  267. const struct s2n_kex *hybrid_kex_1 = kex->hybrid[1];
  268. /* Keep a copy to the start of the whole structure for the signature check */
  269. total_data_to_sign->data = s2n_stuffer_raw_write(&conn->handshake.io, 0);
  270. POSIX_ENSURE_REF(total_data_to_sign->data);
  271. struct s2n_blob data_to_verify_0 = { 0 };
  272. POSIX_GUARD_RESULT(s2n_kex_server_key_send(hybrid_kex_0, conn, &data_to_verify_0));
  273. struct s2n_blob data_to_verify_1 = { 0 };
  274. POSIX_GUARD_RESULT(s2n_kex_server_key_send(hybrid_kex_1, conn, &data_to_verify_1));
  275. total_data_to_sign->size = data_to_verify_0.size + data_to_verify_1.size;
  276. return 0;
  277. }
  278. int s2n_server_key_send_write_signature(struct s2n_connection *conn, struct s2n_blob *signature)
  279. {
  280. struct s2n_stuffer *out = &conn->handshake.io;
  281. POSIX_GUARD(s2n_stuffer_write_uint16(out, signature->size));
  282. POSIX_GUARD(s2n_stuffer_write_bytes(out, signature->data, signature->size));
  283. return 0;
  284. }