Debian Codegraph @

crt/aws-c-cal/source/shared/lccrypto_common.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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 /*Error defines were part of evp.h in 1.0.x and were moved to evperr.h in 1.1.0*/
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 * Transforms evp error code into crt error code and raises it as necessary.
24 * All evp functions follow the same:
25 * >= 1 for success
26 * <= 0 for failure
27 * -2 always indicates incorrect algo for operation
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 /* AWS-LC/BoringSSL error code is uint32_t, but OpenSSL uses unsigned long. */
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 /* Validate at runtime that we're linked against the same libcrypto we compiled against. */
78 void aws_validate_libcrypto_linkage(void) {
79 /* NOTE: the choice of stack buffer size is somewhat arbitrary. it's
80 * possible, but unlikely, that libcrypto version strings may exceed this in
81 * the future. we guard against buffer overflow by limiting write size in
82 * snprintf with the size of the buffer itself. if libcrypto version strings
83 * do eventually exceed the chosen size, this runtime check will fail and
84 * will need to be addressed by increasing buffer size.*/
85 char expected_version[64] = {0};
86 #if defined(OPENSSL_IS_AWSLC)
87 /* get FIPS mode at runtime because headers don't give any indication of
88 * AWS-LC's FIPSness at aws-c-cal compile time. version number can still be
89 * captured at preprocess/compile time from AWSLC_VERSION_NUMBER_STRING.*/
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 /* Validate that the string "AWS-LC" doesn't appear in OpenSSL version str. */
107 AWS_FATAL_ASSERT(strstr("AWS-LC", expected_version) == NULL);
108 AWS_FATAL_ASSERT(strstr("AWS-LC", runtime_version) == NULL);
109 /* Validate both expected and runtime versions begin with OpenSSL's version str prefix. */
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 }