| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_libcrypto.h" |
| 17 |
|
| 18 |
#include <openssl/crypto.h> |
| 19 |
#include <openssl/opensslv.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "crypto/s2n_crypto.h" |
| 23 |
#include "crypto/s2n_fips.h" |
| 24 |
#include "crypto/s2n_openssl.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
#include "utils/s2n_safety_macros.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define EXPECTED_AWSLC_VERSION_PREFIX_OLD "BoringSSL" |
| 43 |
#define EXPECTED_AWSLC_VERSION_PREFIX_NEW "AWS-LC" |
| 44 |
#define EXPECTED_BORINGSSL_VERSION_PREFIX "BoringSSL" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define VERSION_NUMBER_MASK 0xF0000000L |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
const char *s2n_libcrypto_get_version_name(void) |
| 59 |
{ |
| 60 |
return SSLeay_version(SSLEAY_VERSION); |
| 61 |
} |
| 62 |
|
| 63 |
static S2N_RESULT s2n_libcrypto_validate_expected_version_prefix(const char *expected_name_prefix) |
| 64 |
{ |
| 65 |
RESULT_ENSURE_REF(expected_name_prefix); |
| 66 |
RESULT_ENSURE_REF(s2n_libcrypto_get_version_name()); |
| 67 |
RESULT_ENSURE_LTE(strlen(expected_name_prefix), strlen(s2n_libcrypto_get_version_name())); |
| 68 |
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); |
| 69 |
|
| 70 |
return S2N_RESULT_OK; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static S2N_RESULT s2n_libcrypto_validate_expected_version_number(void) |
| 83 |
{ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#if defined(LIBRESSL_VERSION_NUMBER) |
| 91 |
unsigned long run_time_version_number = s2n_get_openssl_version() & VERSION_NUMBER_MASK; |
| 92 |
#else |
| 93 |
unsigned long run_time_version_number = SSLeay() & VERSION_NUMBER_MASK; |
| 94 |
#endif |
| 95 |
unsigned long compile_time_version_number = s2n_get_openssl_version() & VERSION_NUMBER_MASK; |
| 96 |
RESULT_ENSURE(compile_time_version_number == run_time_version_number, S2N_ERR_LIBCRYPTO_VERSION_NUMBER_MISMATCH); |
| 97 |
|
| 98 |
return S2N_RESULT_OK; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
#if defined(OPENSSL_IS_AWSLC) && defined(OPENSSL_IS_BORINGSSL) |
| 109 |
#error "Both OPENSSL_IS_AWSLC and OPENSSL_IS_BORINGSSL are defined at the same time!" |
| 110 |
#endif |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
bool s2n_libcrypto_is_openssl(void) |
| 121 |
{ |
| 122 |
bool is_other_libcrypto_variant = |
| 123 |
s2n_libcrypto_is_boringssl() || s2n_libcrypto_is_libressl() || s2n_libcrypto_is_awslc(); |
| 124 |
|
| 125 |
return !is_other_libcrypto_variant; |
| 126 |
} |
| 127 |
|
| 128 |
bool s2n_libcrypto_is_openssl_fips(void) |
| 129 |
{ |
| 130 |
return s2n_libcrypto_is_openssl() && s2n_is_in_fips_mode(); |
| 131 |
} |
| 132 |
|
| 133 |
bool s2n_libcrypto_is_awslc() |
| 134 |
{ |
| 135 |
#if defined(OPENSSL_IS_AWSLC) |
| 136 |
return true; |
| 137 |
#else |
| 138 |
return false; |
| 139 |
#endif |
| 140 |
} |
| 141 |
|
| 142 |
bool s2n_libcrypto_is_awslc_fips(void) |
| 143 |
{ |
| 144 |
return s2n_libcrypto_is_awslc() && s2n_is_in_fips_mode(); |
| 145 |
} |
| 146 |
|
| 147 |
uint64_t s2n_libcrypto_awslc_api_version(void) |
| 148 |
{ |
| 149 |
#if defined(OPENSSL_IS_AWSLC) |
| 150 |
return AWSLC_API_VERSION; |
| 151 |
#else |
| 152 |
return 0; |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
bool s2n_libcrypto_is_boringssl() |
| 157 |
{ |
| 158 |
#if defined(OPENSSL_IS_BORINGSSL) |
| 159 |
return true; |
| 160 |
#else |
| 161 |
return false; |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
bool s2n_libcrypto_is_libressl() |
| 166 |
{ |
| 167 |
#if defined(LIBRESSL_VERSION_NUMBER) |
| 168 |
return true; |
| 169 |
#else |
| 170 |
return false; |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
S2N_RESULT s2n_libcrypto_validate_runtime(void) |
| 178 |
{ |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
RESULT_ENSURE_EQ(s2n_libcrypto_is_boringssl() && s2n_libcrypto_is_awslc(), false); |
| 183 |
|
| 184 |
|
| 185 |
if (s2n_libcrypto_is_awslc()) { |
| 186 |
const char *expected_awslc_name_prefix = NULL; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (s2n_libcrypto_awslc_api_version() < 17) { |
| 193 |
expected_awslc_name_prefix = EXPECTED_AWSLC_VERSION_PREFIX_OLD; |
| 194 |
} else { |
| 195 |
expected_awslc_name_prefix = EXPECTED_AWSLC_VERSION_PREFIX_NEW; |
| 196 |
} |
| 197 |
RESULT_GUARD(s2n_libcrypto_validate_expected_version_prefix(expected_awslc_name_prefix)); |
| 198 |
} else if (s2n_libcrypto_is_boringssl()) { |
| 199 |
RESULT_GUARD(s2n_libcrypto_validate_expected_version_prefix(EXPECTED_BORINGSSL_VERSION_PREFIX)); |
| 200 |
} |
| 201 |
|
| 202 |
RESULT_GUARD(s2n_libcrypto_validate_expected_version_number()); |
| 203 |
|
| 204 |
return S2N_RESULT_OK; |
| 205 |
} |
| 206 |
|
| 207 |
bool s2n_libcrypto_is_interned(void) |
| 208 |
{ |
| 209 |
#if defined(S2N_INTERN_LIBCRYPTO) |
| 210 |
return true; |
| 211 |
#else |
| 212 |
return false; |
| 213 |
#endif |
| 214 |
} |
| 215 |
|
| 216 |
unsigned long s2n_get_openssl_version(void) |
| 217 |
{ |
| 218 |
return OPENSSL_VERSION_NUMBER; |
| 219 |
} |
| 220 |
|
| 221 |
bool s2n_libcrypto_supports_flag_no_check_time() |
| 222 |
{ |
| 223 |
#ifdef S2N_LIBCRYPTO_SUPPORTS_FLAG_NO_CHECK_TIME |
| 224 |
return true; |
| 225 |
#else |
| 226 |
return false; |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
bool s2n_libcrypto_supports_providers(void) |
| 231 |
{ |
| 232 |
#if S2N_LIBCRYPTO_SUPPORTS_PROVIDERS |
| 233 |
return true; |
| 234 |
#else |
| 235 |
return false; |
| 236 |
#endif |
| 237 |
} |
| 238 |
|
| 239 |
bool s2n_libcrypto_supports_custom_oid(void) |
| 240 |
{ |
| 241 |
#if S2N_LIBCRYPTO_SUPPORTS_CUSTOM_OID |
| 242 |
return true; |
| 243 |
#else |
| 244 |
return false; |
| 245 |
#endif |
| 246 |
} |