s2n_tls13.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_tls13.h"
  16. #include "api/s2n.h"
  17. #include "crypto/s2n_rsa_pss.h"
  18. #include "crypto/s2n_rsa_signing.h"
  19. #include "tls/s2n_tls.h"
  20. bool s2n_use_default_tls13_config_flag = false;
  21. bool s2n_use_default_tls13_config()
  22. {
  23. return s2n_use_default_tls13_config_flag;
  24. }
  25. bool s2n_is_tls13_fully_supported()
  26. {
  27. /* Older versions of Openssl (eg 1.0.2) do not support RSA PSS, which is required for TLS 1.3. */
  28. return s2n_is_rsa_pss_signing_supported() && s2n_is_rsa_pss_certs_supported();
  29. }
  30. int s2n_get_highest_fully_supported_tls_version()
  31. {
  32. return s2n_is_tls13_fully_supported() ? S2N_TLS13 : S2N_TLS12;
  33. }
  34. /* Allow TLS1.3 to be negotiated, and use the default TLS1.3 security policy.
  35. * This is NOT the default behavior, and this method is deprecated.
  36. *
  37. * Please consider using the default behavior and configuring
  38. * TLS1.2/TLS1.3 via explicit security policy instead.
  39. */
  40. int s2n_enable_tls13()
  41. {
  42. return s2n_enable_tls13_in_test();
  43. }
  44. /* Allow TLS1.3 to be negotiated, and use the default TLS1.3 security policy.
  45. * This is NOT the default behavior, and this method is deprecated.
  46. *
  47. * Please consider using the default behavior and configuring
  48. * TLS1.2/TLS1.3 via explicit security policy instead.
  49. */
  50. int s2n_enable_tls13_in_test()
  51. {
  52. s2n_highest_protocol_version = S2N_TLS13;
  53. s2n_use_default_tls13_config_flag = true;
  54. return S2N_SUCCESS;
  55. }
  56. /* Do NOT allow TLS1.3 to be negotiated, regardless of security policy.
  57. * This is NOT the default behavior, and this method is deprecated.
  58. *
  59. * Please consider using the default behavior and configuring
  60. * TLS1.2/TLS1.3 via explicit security policy instead.
  61. */
  62. int s2n_disable_tls13_in_test()
  63. {
  64. POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
  65. s2n_highest_protocol_version = S2N_TLS12;
  66. s2n_use_default_tls13_config_flag = false;
  67. return S2N_SUCCESS;
  68. }
  69. /* Reset S2N to the default protocol version behavior.
  70. *
  71. * This method is intended for use in existing unit tests when the APIs
  72. * to enable/disable TLS1.3 have already been called.
  73. */
  74. int s2n_reset_tls13_in_test()
  75. {
  76. POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
  77. s2n_highest_protocol_version = S2N_TLS13;
  78. s2n_use_default_tls13_config_flag = false;
  79. return S2N_SUCCESS;
  80. }
  81. /* Returns whether a uint16 iana value is a valid TLS 1.3 cipher suite */
  82. bool s2n_is_valid_tls13_cipher(const uint8_t version[2])
  83. {
  84. /* Valid TLS 1.3 Ciphers are
  85. * 0x1301, 0x1302, 0x1303, 0x1304, 0x1305.
  86. * (https://tools.ietf.org/html/rfc8446#appendix-B.4)
  87. */
  88. return version[0] == 0x13 && version[1] >= 0x01 && version[1] <= 0x05;
  89. }
  90. /* Use middlebox compatibility mode for TLS1.3 by default.
  91. * For now, only disable it when QUIC support is enabled.
  92. */
  93. bool s2n_is_middlebox_compat_enabled(struct s2n_connection *conn)
  94. {
  95. return s2n_connection_get_protocol_version(conn) >= S2N_TLS13
  96. && !s2n_connection_is_quic_enabled(conn);
  97. }
  98. S2N_RESULT s2n_connection_validate_tls13_support(struct s2n_connection *conn)
  99. {
  100. RESULT_ENSURE_REF(conn);
  101. /* If the underlying libcrypto supports all features of TLS1.3
  102. * (including RSA-PSS, which is unsupported by some libraries),
  103. * then we can always support TLS1.3.
  104. */
  105. if (s2n_is_tls13_fully_supported()) {
  106. return S2N_RESULT_OK;
  107. }
  108. /*
  109. * If the underlying libcrypto doesn't support all features...
  110. */
  111. /* There are some TLS servers in the wild that will choose options not offered by the client.
  112. * So a server might choose to use RSA-PSS even if even if the client does not advertise support for RSA-PSS.
  113. * Therefore, only servers can perform TLS1.3 without full feature support.
  114. */
  115. RESULT_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_RSA_PSS_NOT_SUPPORTED);
  116. /* RSA signatures must use RSA-PSS in TLS1.3.
  117. * So RSA-PSS is required for TLS1.3 servers if an RSA certificate is used.
  118. */
  119. RESULT_ENSURE(!conn->config->is_rsa_cert_configured, S2N_ERR_RSA_PSS_NOT_SUPPORTED);
  120. /* RSA-PSS is also required for TLS1.3 servers if client auth is requested, because the
  121. * client might offer an RSA certificate.
  122. */
  123. s2n_cert_auth_type client_auth_status = S2N_CERT_AUTH_NONE;
  124. RESULT_GUARD_POSIX(s2n_connection_get_client_auth_type(conn, &client_auth_status));
  125. RESULT_ENSURE(client_auth_status == S2N_CERT_AUTH_NONE, S2N_ERR_RSA_PSS_NOT_SUPPORTED);
  126. return S2N_RESULT_OK;
  127. }
  128. bool s2n_connection_supports_tls13(struct s2n_connection *conn)
  129. {
  130. return s2n_result_is_ok(s2n_connection_validate_tls13_support(conn));
  131. }