s2n_ecc_preferences.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_ecc_preferences.h"
  16. #include "api/s2n.h"
  17. #include "crypto/s2n_ecc_evp.h"
  18. #include "tls/s2n_connection.h"
  19. #include "utils/s2n_safety.h"
  20. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20140601[] = {
  21. &s2n_ecc_curve_secp256r1,
  22. &s2n_ecc_curve_secp384r1,
  23. };
  24. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20200310[] = {
  25. #if EVP_APIS_SUPPORTED
  26. &s2n_ecc_curve_x25519,
  27. #endif
  28. &s2n_ecc_curve_secp256r1,
  29. &s2n_ecc_curve_secp384r1,
  30. };
  31. /* Curve p256 is at the top of the list in order to minimize HRR */
  32. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20230623[] = {
  33. &s2n_ecc_curve_secp256r1,
  34. #if EVP_APIS_SUPPORTED
  35. &s2n_ecc_curve_x25519,
  36. #endif
  37. &s2n_ecc_curve_secp384r1,
  38. };
  39. /*
  40. * These curves were chosen based on the following specification:
  41. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r2.pdf
  42. */
  43. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_default_fips[] = {
  44. &s2n_ecc_curve_secp256r1,
  45. &s2n_ecc_curve_secp384r1,
  46. };
  47. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20201021[] = {
  48. &s2n_ecc_curve_secp256r1,
  49. &s2n_ecc_curve_secp384r1,
  50. &s2n_ecc_curve_secp521r1,
  51. };
  52. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20210816[] = {
  53. &s2n_ecc_curve_secp384r1,
  54. };
  55. const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_test_all[] = {
  56. #if EVP_APIS_SUPPORTED
  57. &s2n_ecc_curve_x25519,
  58. #endif
  59. &s2n_ecc_curve_secp256r1,
  60. &s2n_ecc_curve_secp384r1,
  61. &s2n_ecc_curve_secp521r1,
  62. };
  63. const struct s2n_ecc_preferences s2n_ecc_preferences_20140601 = {
  64. .count = s2n_array_len(s2n_ecc_pref_list_20140601),
  65. .ecc_curves = s2n_ecc_pref_list_20140601,
  66. };
  67. const struct s2n_ecc_preferences s2n_ecc_preferences_20200310 = {
  68. .count = s2n_array_len(s2n_ecc_pref_list_20200310),
  69. .ecc_curves = s2n_ecc_pref_list_20200310,
  70. };
  71. const struct s2n_ecc_preferences s2n_ecc_preferences_20230623 = {
  72. .count = s2n_array_len(s2n_ecc_pref_list_20230623),
  73. .ecc_curves = s2n_ecc_pref_list_20230623,
  74. };
  75. const struct s2n_ecc_preferences s2n_ecc_preferences_default_fips = {
  76. .count = s2n_array_len(s2n_ecc_pref_list_default_fips),
  77. .ecc_curves = s2n_ecc_pref_list_default_fips,
  78. };
  79. const struct s2n_ecc_preferences s2n_ecc_preferences_20201021 = {
  80. .count = s2n_array_len(s2n_ecc_pref_list_20201021),
  81. .ecc_curves = s2n_ecc_pref_list_20201021,
  82. };
  83. const struct s2n_ecc_preferences s2n_ecc_preferences_20210816 = {
  84. .count = s2n_array_len(s2n_ecc_pref_list_20210816),
  85. .ecc_curves = s2n_ecc_pref_list_20210816,
  86. };
  87. const struct s2n_ecc_preferences s2n_ecc_preferences_test_all = {
  88. .count = s2n_array_len(s2n_ecc_pref_list_test_all),
  89. .ecc_curves = s2n_ecc_pref_list_test_all,
  90. };
  91. const struct s2n_ecc_preferences s2n_ecc_preferences_null = {
  92. .count = 0,
  93. .ecc_curves = NULL,
  94. };
  95. /* Checks if the ecc_curves present in s2n_ecc_preferences list is a subset of s2n_all_supported_curves_list
  96. * maintained in s2n_ecc_evp.c */
  97. int s2n_check_ecc_preferences_curves_list(const struct s2n_ecc_preferences *ecc_preferences)
  98. {
  99. int check = 1;
  100. for (int i = 0; i < ecc_preferences->count; i++) {
  101. const struct s2n_ecc_named_curve *named_curve = ecc_preferences->ecc_curves[i];
  102. int curve_found = 0;
  103. for (size_t j = 0; j < s2n_all_supported_curves_list_len; j++) {
  104. if (named_curve->iana_id == s2n_all_supported_curves_list[j]->iana_id) {
  105. curve_found = 1;
  106. break;
  107. }
  108. }
  109. check *= curve_found;
  110. if (check == 0) {
  111. POSIX_BAIL(S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
  112. }
  113. }
  114. return S2N_SUCCESS;
  115. }
  116. /* Determines if query_iana_id corresponds to a curve for these ECC preferences. */
  117. bool s2n_ecc_preferences_includes_curve(const struct s2n_ecc_preferences *ecc_preferences, uint16_t query_iana_id)
  118. {
  119. if (ecc_preferences == NULL) {
  120. return false;
  121. }
  122. for (size_t i = 0; i < ecc_preferences->count; i++) {
  123. if (query_iana_id == ecc_preferences->ecc_curves[i]->iana_id) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }