| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_fips.h" |
| 17 |
|
| 18 |
#include <openssl/crypto.h> |
| 19 |
|
| 20 |
#include "crypto/s2n_libcrypto.h" |
| 21 |
#include "crypto/s2n_openssl.h" |
| 22 |
#include "utils/s2n_init.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
#if defined(S2N_INTERN_LIBCRYPTO) && defined(OPENSSL_FIPS) |
| 26 |
#error "Interning with OpenSSL fips-validated libcrypto is not currently supported. See https://github.com/aws/s2n-tls/issues/2741" |
| 27 |
#endif |
| 28 |
|
| 29 |
static bool s2n_fips_mode_enabled = false; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
bool s2n_libcrypto_is_fips(void) |
| 45 |
{ |
| 46 |
#if defined(OPENSSL_FIPS) || defined(OPENSSL_IS_AWSLC) |
| 47 |
if (FIPS_mode() == 1) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
#elif S2N_OPENSSL_VERSION_AT_LEAST(3, 0, 0) |
| 51 |
return EVP_default_properties_is_fips_enabled(NULL); |
| 52 |
#endif |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
int s2n_fips_init(void) |
| 57 |
{ |
| 58 |
s2n_fips_mode_enabled = s2n_libcrypto_is_fips(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#if defined(OPENSSL_FIPS) |
| 64 |
POSIX_ENSURE(!s2n_fips_mode_enabled, S2N_ERR_FIPS_MODE_UNSUPPORTED); |
| 65 |
#endif |
| 66 |
|
| 67 |
return S2N_SUCCESS; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
bool s2n_is_in_fips_mode(void) |
| 72 |
{ |
| 73 |
return s2n_fips_mode_enabled; |
| 74 |
} |
| 75 |
|
| 76 |
int s2n_get_fips_mode(s2n_fips_mode *fips_mode) |
| 77 |
{ |
| 78 |
POSIX_ENSURE_REF(fips_mode); |
| 79 |
*fips_mode = S2N_FIPS_MODE_DISABLED; |
| 80 |
POSIX_ENSURE(s2n_is_initialized(), S2N_ERR_NOT_INITIALIZED); |
| 81 |
|
| 82 |
if (s2n_is_in_fips_mode()) { |
| 83 |
*fips_mode = S2N_FIPS_MODE_ENABLED; |
| 84 |
} |
| 85 |
|
| 86 |
return S2N_SUCCESS; |
| 87 |
} |