s2n_libcrypto.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "crypto/s2n_libcrypto.h"
  16. #include <openssl/crypto.h>
  17. #include <openssl/opensslv.h>
  18. #include <string.h>
  19. #include "crypto/s2n_crypto.h"
  20. #include "crypto/s2n_fips.h"
  21. #include "crypto/s2n_openssl.h"
  22. #include "utils/s2n_safety.h"
  23. #include "utils/s2n_safety_macros.h"
  24. /* Note: OpenSSL 1.0.2 -> 1.1.0 implemented a new API to get the version number
  25. * and version name. We have to handle that by using old functions
  26. * (named "SSLea*"). Newer version of OpenSSL luckily define these symbols to
  27. * the new API. When dropping OpenSSL 1.0.2 support, we can move to the new API.
  28. */
  29. /* The result of SSLeay_version(SSLEAY_VERSION) for OpenSSL and AWS-LC depends on the
  30. * version. AWS-LC and BoringSSL have consistent prefixes that can be statically asserted.
  31. *
  32. * https://github.com/awslabs/aws-lc/commit/8f184f5d69604cc4645bafec47c2d6d9929cb50f
  33. * has not been pushed to the fips branch of AWS-LC. In addition, we can't
  34. * distinguish AWS-LC fips and non-fips at pre-processing time since AWS-LC
  35. * doesn't distribute fips-specific header files.
  36. */
  37. #define EXPECTED_AWSLC_VERSION_PREFIX_OLD "BoringSSL"
  38. #define EXPECTED_AWSLC_VERSION_PREFIX_NEW "AWS-LC"
  39. #define EXPECTED_BORINGSSL_VERSION_PREFIX "BoringSSL"
  40. /* https://www.openssl.org/docs/man{1.0.2, 1.1.1, 3.0}/man3/OPENSSL_VERSION_NUMBER.html
  41. * OPENSSL_VERSION_NUMBER in hex is: MNNFFPPS major minor fix patch status.
  42. * Bitwise: MMMMNNNNNNNNFFFFFFFFPPPPPPPPSSSS
  43. * To not be overly restrictive, we only care about the major version.
  44. * From OpenSSL 3.0 the "fix" part is also deprecated and is always a flat 0x00.
  45. */
  46. #define VERSION_NUMBER_MASK 0xF0000000L
  47. /* Returns the version name of the libcrypto containing the definition that the
  48. * symbol OpenSSL_version binded to at link-time. This can be used as
  49. * verification at run-time that s2n linked against the expected libcrypto.
  50. */
  51. static const char *s2n_libcrypto_get_version_name(void)
  52. {
  53. return SSLeay_version(SSLEAY_VERSION);
  54. }
  55. static S2N_RESULT s2n_libcrypto_validate_expected_version_prefix(const char *expected_name_prefix)
  56. {
  57. RESULT_ENSURE_REF(expected_name_prefix);
  58. RESULT_ENSURE_REF(s2n_libcrypto_get_version_name());
  59. RESULT_ENSURE_LTE(strlen(expected_name_prefix), strlen(s2n_libcrypto_get_version_name()));
  60. RESULT_ENSURE(s2n_constant_time_equals((const uint8_t *) expected_name_prefix, (const uint8_t *) s2n_libcrypto_get_version_name(), (const uint32_t) strlen(expected_name_prefix)), S2N_ERR_LIBCRYPTO_VERSION_NAME_MISMATCH);
  61. return S2N_RESULT_OK;
  62. }
  63. /* Compare compile-time version number with the version number of the libcrypto
  64. * containing the definition that the symbol OpenSSL_version_num binded to at
  65. * link-time.
  66. *
  67. * This is an imperfect check for AWS-LC and BoringSSL, since their version
  68. * number is basically never incremented. However, for these we have a strong
  69. * check through s2n_libcrypto_validate_expected_version_name(), so it is not
  70. * of great importance.
  71. */
  72. static S2N_RESULT s2n_libcrypto_validate_expected_version_number(void)
  73. {
  74. /* We mutate the version number in s2n_openssl.h when detecting Libressl. This
  75. * value is cached by s2n_get_openssl_version(). Hence, for libressl, the
  76. * run-time version number will always be different from what
  77. * s2n_get_openssl_version() returns. We cater for this here by just getting
  78. * what ever we cached instead of asking Libressl libcrypto.
  79. */
  80. #if defined(LIBRESSL_VERSION_NUMBER)
  81. unsigned long run_time_version_number = s2n_get_openssl_version() & VERSION_NUMBER_MASK;
  82. #else
  83. unsigned long run_time_version_number = SSLeay() & VERSION_NUMBER_MASK;
  84. #endif
  85. unsigned long compile_time_version_number = s2n_get_openssl_version() & VERSION_NUMBER_MASK;
  86. RESULT_ENSURE(compile_time_version_number == run_time_version_number, S2N_ERR_LIBCRYPTO_VERSION_NUMBER_MISMATCH);
  87. return S2N_RESULT_OK;
  88. }
  89. /* s2n_libcrypto_is_*() encodes the libcrypto version used at build-time.
  90. * Currently only captures AWS-LC and BoringSSL. When a libcrypto-dependent
  91. * branch is required, we prefer these functions where possible to reduce
  92. # #ifs and avoid potential bugs where the header containing the #define is not
  93. * included.
  94. */
  95. #if defined(OPENSSL_IS_AWSLC) && defined(OPENSSL_IS_BORINGSSL)
  96. #error "Both OPENSSL_IS_AWSLC and OPENSSL_IS_BORINGSSL are defined at the same time!"
  97. #endif
  98. bool s2n_libcrypto_is_awslc()
  99. {
  100. #if defined(OPENSSL_IS_AWSLC)
  101. return true;
  102. #else
  103. return false;
  104. #endif
  105. }
  106. uint64_t s2n_libcrypto_awslc_api_version(void)
  107. {
  108. #if defined(OPENSSL_IS_AWSLC)
  109. return AWSLC_API_VERSION;
  110. #else
  111. return 0;
  112. #endif
  113. }
  114. bool s2n_libcrypto_is_boringssl()
  115. {
  116. #if defined(OPENSSL_IS_BORINGSSL)
  117. return true;
  118. #else
  119. return false;
  120. #endif
  121. }
  122. bool s2n_libcrypto_is_libressl()
  123. {
  124. #if defined(LIBRESSL_VERSION_NUMBER)
  125. return true;
  126. #else
  127. return false;
  128. #endif
  129. }
  130. /* Performs various checks to validate that the libcrypto used at compile-time
  131. * is the same libcrypto being used at run-time.
  132. */
  133. S2N_RESULT s2n_libcrypto_validate_runtime(void)
  134. {
  135. /* Sanity check that we don't think we built against AWS-LC and BoringSSL at
  136. * the same time.
  137. */
  138. RESULT_ENSURE_EQ(s2n_libcrypto_is_boringssl() && s2n_libcrypto_is_awslc(), false);
  139. /* If we know the expected version name, we can validate it. */
  140. if (s2n_libcrypto_is_awslc()) {
  141. const char *expected_awslc_name_prefix = NULL;
  142. /* For backwards compatability, also check the AWS-LC API version see
  143. * https://github.com/awslabs/aws-lc/pull/467. When we are confident we
  144. * don't meet anymore "old" AWS-LC libcrypto's, this API version check
  145. * can be removed.
  146. */
  147. if (s2n_libcrypto_awslc_api_version() < 17) {
  148. expected_awslc_name_prefix = EXPECTED_AWSLC_VERSION_PREFIX_OLD;
  149. } else {
  150. expected_awslc_name_prefix = EXPECTED_AWSLC_VERSION_PREFIX_NEW;
  151. }
  152. RESULT_GUARD(s2n_libcrypto_validate_expected_version_prefix(expected_awslc_name_prefix));
  153. } else if (s2n_libcrypto_is_boringssl()) {
  154. RESULT_GUARD(s2n_libcrypto_validate_expected_version_prefix(EXPECTED_BORINGSSL_VERSION_PREFIX));
  155. }
  156. RESULT_GUARD(s2n_libcrypto_validate_expected_version_number());
  157. return S2N_RESULT_OK;
  158. }
  159. bool s2n_libcrypto_is_interned(void)
  160. {
  161. #if defined(S2N_INTERN_LIBCRYPTO)
  162. return true;
  163. #else
  164. return false;
  165. #endif
  166. }
  167. unsigned long s2n_get_openssl_version(void)
  168. {
  169. return OPENSSL_VERSION_NUMBER;
  170. }
  171. bool s2n_libcrypto_supports_flag_no_check_time()
  172. {
  173. #ifdef S2N_LIBCRYPTO_SUPPORTS_FLAG_NO_CHECK_TIME
  174. return true;
  175. #else
  176. return false;
  177. #endif
  178. }