| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/cal/cal.h> |
| 7 |
#include <aws/cal/private/opensslcrypto_common.h> |
| 8 |
|
| 9 |
#define OPENSSL_SUPPRESS_DEPRECATED |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/evp.h> |
| 12 |
|
| 13 |
#if defined(OPENSSL_IS_OPENSSL) |
| 14 |
|
| 15 |
# if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 16 |
# include <openssl/evperr.h> |
| 17 |
# endif |
| 18 |
#else |
| 19 |
# include <openssl/evp_errors.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
int aws_reinterpret_lc_evp_error_as_crt(int evp_error, const char *function_name, enum aws_cal_log_subject subject) { |
| 30 |
if (evp_error > 0) { |
| 31 |
return AWS_OP_SUCCESS; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
#if defined(OPENSSL_IS_OPENSSL) |
| 36 |
uint32_t error = ERR_peek_error(); |
| 37 |
#else |
| 38 |
unsigned long error = ERR_peek_error(); |
| 39 |
#endif |
| 40 |
|
| 41 |
int crt_error = AWS_OP_ERR; |
| 42 |
const char *error_message = ERR_reason_error_string(error); |
| 43 |
|
| 44 |
if (evp_error == -2) { |
| 45 |
crt_error = AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM; |
| 46 |
goto on_error; |
| 47 |
} |
| 48 |
|
| 49 |
if (ERR_GET_LIB(error) == ERR_LIB_EVP) { |
| 50 |
switch (ERR_GET_REASON(error)) { |
| 51 |
case EVP_R_BUFFER_TOO_SMALL: { |
| 52 |
crt_error = AWS_ERROR_SHORT_BUFFER; |
| 53 |
goto on_error; |
| 54 |
} |
| 55 |
case EVP_R_UNSUPPORTED_ALGORITHM: { |
| 56 |
crt_error = AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM; |
| 57 |
goto on_error; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
crt_error = AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED; |
| 63 |
|
| 64 |
on_error: |
| 65 |
AWS_LOGF_ERROR( |
| 66 |
subject, |
| 67 |
"%s() failed. returned: %d extended error:%lu(%s) aws_error:%s", |
| 68 |
function_name, |
| 69 |
evp_error, |
| 70 |
(unsigned long)error, |
| 71 |
error_message == NULL ? "" : error_message, |
| 72 |
aws_error_name(crt_error)); |
| 73 |
|
| 74 |
return aws_raise_error(crt_error); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
void aws_validate_libcrypto_linkage(void) { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
char expected_version[64] = {0}; |
| 86 |
#if defined(OPENSSL_IS_AWSLC) |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const char *mode = FIPS_mode() ? "AWS-LC FIPS" : "AWS-LC"; |
| 91 |
snprintf(expected_version, sizeof(expected_version), "%s %s", mode, AWSLC_VERSION_NUMBER_STRING); |
| 92 |
#elif defined(OPENSSL_IS_BORINGSSL) |
| 93 |
snprintf(expected_version, sizeof(expected_version), "BoringSSL"); |
| 94 |
#elif defined(OPENSSL_IS_OPENSSL) |
| 95 |
snprintf(expected_version, sizeof(expected_version), OPENSSL_VERSION_TEXT); |
| 96 |
#elif !defined(BYO_CRYPTO) |
| 97 |
# error Unsupported libcrypto! |
| 98 |
#endif |
| 99 |
const char *runtime_version = SSLeay_version(SSLEAY_VERSION); |
| 100 |
AWS_LOGF_DEBUG( |
| 101 |
AWS_LS_CAL_LIBCRYPTO_RESOLVE, |
| 102 |
"Compiled with libcrypto %s, linked to libcrypto %s", |
| 103 |
expected_version, |
| 104 |
runtime_version); |
| 105 |
#if defined(OPENSSL_IS_OPENSSL) |
| 106 |
|
| 107 |
AWS_FATAL_ASSERT(strstr("AWS-LC", expected_version) == NULL); |
| 108 |
AWS_FATAL_ASSERT(strstr("AWS-LC", runtime_version) == NULL); |
| 109 |
|
| 110 |
const char *openssl_prefix = "OpenSSL "; |
| 111 |
AWS_FATAL_ASSERT(strncmp(openssl_prefix, expected_version, strlen(openssl_prefix)) == 0); |
| 112 |
AWS_FATAL_ASSERT(strncmp(openssl_prefix, runtime_version, strlen(openssl_prefix)) == 0); |
| 113 |
#else |
| 114 |
AWS_FATAL_ASSERT(strcmp(expected_version, runtime_version) == 0 && "libcrypto mislink"); |
| 115 |
#endif |
| 116 |
} |