s2n_signature_algorithms.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "tls/s2n_signature_algorithms.h"
  16. #include "crypto/s2n_fips.h"
  17. #include "crypto/s2n_rsa_pss.h"
  18. #include "crypto/s2n_rsa_signing.h"
  19. #include "error/s2n_errno.h"
  20. #include "tls/s2n_auth_selection.h"
  21. #include "tls/s2n_cipher_suites.h"
  22. #include "tls/s2n_kex.h"
  23. #include "tls/s2n_security_policies.h"
  24. #include "tls/s2n_signature_scheme.h"
  25. #include "utils/s2n_safety.h"
  26. static S2N_RESULT s2n_signature_scheme_validate_for_send(struct s2n_connection *conn,
  27. const struct s2n_signature_scheme *scheme)
  28. {
  29. RESULT_ENSURE_REF(conn);
  30. /* If no protocol has been negotiated yet, the actual_protocol_version will
  31. * be equivalent to the client_protocol_version and represent the highest
  32. * version supported.
  33. */
  34. RESULT_ENSURE_GTE(conn->actual_protocol_version, scheme->minimum_protocol_version);
  35. /* QUIC only supports TLS1.3 */
  36. if (s2n_connection_is_quic_enabled(conn) && scheme->maximum_protocol_version) {
  37. RESULT_ENSURE_GTE(scheme->maximum_protocol_version, S2N_TLS13);
  38. }
  39. if (!s2n_is_rsa_pss_signing_supported()) {
  40. RESULT_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_RSAE);
  41. }
  42. if (!s2n_is_rsa_pss_certs_supported()) {
  43. RESULT_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_PSS);
  44. }
  45. return S2N_RESULT_OK;
  46. }
  47. static bool s2n_signature_scheme_is_valid_for_send(struct s2n_connection *conn,
  48. const struct s2n_signature_scheme *scheme)
  49. {
  50. return s2n_result_is_ok(s2n_signature_scheme_validate_for_send(conn, scheme));
  51. }
  52. static S2N_RESULT s2n_signature_scheme_validate_for_recv(struct s2n_connection *conn,
  53. const struct s2n_signature_scheme *scheme)
  54. {
  55. RESULT_ENSURE_REF(scheme);
  56. RESULT_ENSURE_REF(conn);
  57. RESULT_GUARD(s2n_signature_scheme_validate_for_send(conn, scheme));
  58. if (scheme->maximum_protocol_version != S2N_UNKNOWN_PROTOCOL_VERSION) {
  59. RESULT_ENSURE_LTE(conn->actual_protocol_version, scheme->maximum_protocol_version);
  60. }
  61. RESULT_ENSURE_NE(conn->actual_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
  62. if (conn->actual_protocol_version >= S2N_TLS13) {
  63. RESULT_ENSURE_NE(scheme->hash_alg, S2N_HASH_SHA1);
  64. RESULT_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA);
  65. } else {
  66. RESULT_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_PSS);
  67. }
  68. return S2N_RESULT_OK;
  69. }
  70. static bool s2n_signature_scheme_is_valid_for_recv(struct s2n_connection *conn,
  71. const struct s2n_signature_scheme *scheme)
  72. {
  73. return s2n_result_is_ok(s2n_signature_scheme_validate_for_recv(conn, scheme));
  74. }
  75. static int s2n_is_signature_scheme_usable(struct s2n_connection *conn, const struct s2n_signature_scheme *candidate)
  76. {
  77. POSIX_ENSURE_REF(conn);
  78. POSIX_ENSURE_REF(candidate);
  79. POSIX_GUARD_RESULT(s2n_signature_scheme_validate_for_recv(conn, candidate));
  80. POSIX_GUARD(s2n_is_sig_scheme_valid_for_auth(conn, candidate));
  81. return S2N_SUCCESS;
  82. }
  83. static int s2n_choose_sig_scheme(struct s2n_connection *conn, struct s2n_sig_scheme_list *peer_wire_prefs,
  84. const struct s2n_signature_scheme **chosen_scheme_out)
  85. {
  86. POSIX_ENSURE_REF(conn);
  87. POSIX_ENSURE_REF(conn->secure);
  88. const struct s2n_signature_preferences *signature_preferences = NULL;
  89. POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
  90. POSIX_ENSURE_REF(signature_preferences);
  91. struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
  92. POSIX_ENSURE_REF(cipher_suite);
  93. for (size_t i = 0; i < signature_preferences->count; i++) {
  94. const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
  95. if (s2n_is_signature_scheme_usable(conn, candidate) != S2N_SUCCESS) {
  96. continue;
  97. }
  98. for (size_t j = 0; j < peer_wire_prefs->len; j++) {
  99. uint16_t their_iana_val = peer_wire_prefs->iana_list[j];
  100. if (candidate->iana_value == their_iana_val) {
  101. *chosen_scheme_out = candidate;
  102. return S2N_SUCCESS;
  103. }
  104. }
  105. }
  106. /* do not error even if there's no match */
  107. return S2N_SUCCESS;
  108. }
  109. /* similar to s2n_choose_sig_scheme() without matching client's preference */
  110. int s2n_tls13_default_sig_scheme(struct s2n_connection *conn,
  111. const struct s2n_signature_scheme **chosen_scheme_out)
  112. {
  113. POSIX_ENSURE_REF(conn);
  114. POSIX_ENSURE_REF(conn->secure);
  115. const struct s2n_signature_preferences *signature_preferences = NULL;
  116. POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
  117. POSIX_ENSURE_REF(signature_preferences);
  118. struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
  119. POSIX_ENSURE_REF(cipher_suite);
  120. for (size_t i = 0; i < signature_preferences->count; i++) {
  121. const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
  122. if (s2n_is_signature_scheme_usable(conn, candidate) != S2N_SUCCESS) {
  123. continue;
  124. }
  125. *chosen_scheme_out = candidate;
  126. return S2N_SUCCESS;
  127. }
  128. POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_SCHEME);
  129. }
  130. static S2N_RESULT s2n_signature_algorithms_get_legacy_default(struct s2n_connection *conn,
  131. const struct s2n_signature_scheme **default_sig_scheme)
  132. {
  133. RESULT_ENSURE_REF(conn);
  134. RESULT_ENSURE_REF(default_sig_scheme);
  135. s2n_authentication_method auth_method = 0;
  136. if (conn->mode == S2N_SERVER) {
  137. RESULT_GUARD_POSIX(s2n_get_auth_method_for_cert_type(
  138. conn->handshake_params.client_cert_pkey_type, &auth_method));
  139. } else {
  140. RESULT_ENSURE_REF(conn->secure);
  141. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  142. auth_method = conn->secure->cipher_suite->auth_method;
  143. }
  144. if (auth_method == S2N_AUTHENTICATION_ECDSA) {
  145. *default_sig_scheme = &s2n_ecdsa_sha1;
  146. } else {
  147. *default_sig_scheme = &s2n_rsa_pkcs1_md5_sha1;
  148. }
  149. return S2N_RESULT_OK;
  150. }
  151. S2N_RESULT s2n_signature_algorithm_recv(struct s2n_connection *conn, struct s2n_stuffer *in)
  152. {
  153. RESULT_ENSURE_REF(conn);
  154. const struct s2n_signature_scheme **chosen_sig_scheme = NULL;
  155. if (conn->mode == S2N_SERVER) {
  156. chosen_sig_scheme = &conn->handshake_params.client_cert_sig_scheme;
  157. } else {
  158. chosen_sig_scheme = &conn->handshake_params.server_cert_sig_scheme;
  159. }
  160. /* Before TLS1.2, signature algorithms were fixed instead of negotiated */
  161. if (conn->actual_protocol_version < S2N_TLS12) {
  162. return s2n_signature_algorithms_get_legacy_default(conn, chosen_sig_scheme);
  163. }
  164. uint16_t iana_value = 0;
  165. RESULT_ENSURE(s2n_stuffer_read_uint16(in, &iana_value) == S2N_SUCCESS,
  166. S2N_ERR_BAD_MESSAGE);
  167. const struct s2n_signature_preferences *signature_preferences = NULL;
  168. RESULT_GUARD_POSIX(s2n_connection_get_signature_preferences(conn, &signature_preferences));
  169. RESULT_ENSURE_REF(signature_preferences);
  170. for (size_t i = 0; i < signature_preferences->count; i++) {
  171. const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
  172. if (candidate->iana_value != iana_value) {
  173. continue;
  174. }
  175. if (!s2n_signature_scheme_is_valid_for_recv(conn, candidate)) {
  176. continue;
  177. }
  178. *chosen_sig_scheme = candidate;
  179. return S2N_RESULT_OK;
  180. }
  181. RESULT_BAIL(S2N_ERR_INVALID_SIGNATURE_SCHEME);
  182. }
  183. int s2n_choose_default_sig_scheme(struct s2n_connection *conn,
  184. const struct s2n_signature_scheme **sig_scheme_out, s2n_mode signer)
  185. {
  186. POSIX_ENSURE_REF(conn);
  187. POSIX_ENSURE_REF(conn->secure);
  188. POSIX_ENSURE_REF(sig_scheme_out);
  189. s2n_authentication_method auth_method = 0;
  190. if (signer == S2N_CLIENT) {
  191. POSIX_GUARD(s2n_get_auth_method_for_cert_type(conn->handshake_params.client_cert_pkey_type, &auth_method));
  192. } else {
  193. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  194. auth_method = conn->secure->cipher_suite->auth_method;
  195. }
  196. /* Default our signature digest algorithms.
  197. * For >=TLS 1.2 this default may be overridden by the signature_algorithms extension.
  198. */
  199. const struct s2n_signature_scheme *default_sig_scheme = &s2n_rsa_pkcs1_md5_sha1;
  200. if (auth_method == S2N_AUTHENTICATION_ECDSA) {
  201. default_sig_scheme = &s2n_ecdsa_sha1;
  202. } else if (conn->actual_protocol_version >= S2N_TLS12) {
  203. default_sig_scheme = &s2n_rsa_pkcs1_sha1;
  204. }
  205. if (conn->actual_protocol_version < S2N_TLS12) {
  206. /* Before TLS1.2, signature algorithms were fixed, not chosen / negotiated. */
  207. *sig_scheme_out = default_sig_scheme;
  208. return S2N_SUCCESS;
  209. } else {
  210. /* If we attempt to negotiate a default in TLS1.2, we should ensure that
  211. * default is allowed by the local security policy.
  212. */
  213. const struct s2n_signature_preferences *signature_preferences = NULL;
  214. POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
  215. POSIX_ENSURE_REF(signature_preferences);
  216. for (size_t i = 0; i < signature_preferences->count; i++) {
  217. if (signature_preferences->signature_schemes[i]->iana_value == default_sig_scheme->iana_value) {
  218. *sig_scheme_out = default_sig_scheme;
  219. return S2N_SUCCESS;
  220. }
  221. }
  222. /* We cannot bail with an error here because existing logic assumes
  223. * that this method should always succeed and calls it even when no default
  224. * is actually necessary.
  225. * If no valid default exists, set an unusable, invalid empty scheme.
  226. */
  227. *sig_scheme_out = &s2n_null_sig_scheme;
  228. return S2N_SUCCESS;
  229. }
  230. }
  231. int s2n_choose_sig_scheme_from_peer_preference_list(struct s2n_connection *conn,
  232. struct s2n_sig_scheme_list *peer_wire_prefs,
  233. const struct s2n_signature_scheme **sig_scheme_out)
  234. {
  235. POSIX_ENSURE_REF(conn);
  236. POSIX_ENSURE_REF(sig_scheme_out);
  237. const struct s2n_signature_scheme *chosen_scheme = &s2n_null_sig_scheme;
  238. if (conn->actual_protocol_version < S2N_TLS13) {
  239. POSIX_GUARD(s2n_choose_default_sig_scheme(conn, &chosen_scheme, conn->mode));
  240. } else {
  241. /* Pick a default signature algorithm in TLS 1.3 https://tools.ietf.org/html/rfc8446#section-4.4.2.2 */
  242. POSIX_GUARD(s2n_tls13_default_sig_scheme(conn, &chosen_scheme));
  243. }
  244. /* SignatureScheme preference list was first added in TLS 1.2. It will be empty in older TLS versions. */
  245. if (conn->actual_protocol_version >= S2N_TLS12 && peer_wire_prefs != NULL && peer_wire_prefs->len > 0) {
  246. /* Use a best effort approach to selecting a signature scheme matching client's preferences */
  247. POSIX_GUARD(s2n_choose_sig_scheme(conn, peer_wire_prefs, &chosen_scheme));
  248. }
  249. *sig_scheme_out = chosen_scheme;
  250. return S2N_SUCCESS;
  251. }
  252. S2N_RESULT s2n_signature_algorithms_supported_list_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  253. {
  254. const struct s2n_signature_preferences *signature_preferences = NULL;
  255. RESULT_GUARD_POSIX(s2n_connection_get_signature_preferences(conn, &signature_preferences));
  256. RESULT_ENSURE_REF(signature_preferences);
  257. struct s2n_stuffer_reservation size = { 0 };
  258. RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint16(out, &size));
  259. for (size_t i = 0; i < signature_preferences->count; i++) {
  260. const struct s2n_signature_scheme *const scheme = signature_preferences->signature_schemes[i];
  261. RESULT_ENSURE_REF(scheme);
  262. if (s2n_signature_scheme_is_valid_for_send(conn, scheme)) {
  263. RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(out, scheme->iana_value));
  264. }
  265. }
  266. RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&size));
  267. return S2N_RESULT_OK;
  268. }
  269. int s2n_recv_supported_sig_scheme_list(struct s2n_stuffer *in, struct s2n_sig_scheme_list *sig_hash_algs)
  270. {
  271. uint16_t length_of_all_pairs;
  272. POSIX_GUARD(s2n_stuffer_read_uint16(in, &length_of_all_pairs));
  273. if (length_of_all_pairs > s2n_stuffer_data_available(in)) {
  274. /* Malformed length, ignore the extension */
  275. return 0;
  276. }
  277. if (length_of_all_pairs % 2) {
  278. /* Pairs occur in two byte lengths. Malformed length, ignore the extension and skip ahead */
  279. POSIX_GUARD(s2n_stuffer_skip_read(in, length_of_all_pairs));
  280. return 0;
  281. }
  282. int pairs_available = length_of_all_pairs / 2;
  283. if (pairs_available > TLS_SIGNATURE_SCHEME_LIST_MAX_LEN) {
  284. POSIX_BAIL(S2N_ERR_TOO_MANY_SIGNATURE_SCHEMES);
  285. }
  286. sig_hash_algs->len = 0;
  287. for (size_t i = 0; i < (size_t) pairs_available; i++) {
  288. uint16_t sig_scheme = 0;
  289. POSIX_GUARD(s2n_stuffer_read_uint16(in, &sig_scheme));
  290. sig_hash_algs->iana_list[sig_hash_algs->len] = sig_scheme;
  291. sig_hash_algs->len += 1;
  292. }
  293. return 0;
  294. }