Skip to content

Commit cdd052b

Browse files
Fix cmake4 macos builds (#226)
1 parent c3fd3ed commit cdd052b

File tree

10 files changed

+101
-74
lines changed

10 files changed

+101
-74
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ jobs:
310310
macos-ed25519:
311311
strategy:
312312
matrix:
313-
image: [macos-14-large, macos-14]
314-
name: ${{ matrix.image == 'macos-14' && 'macos' || 'macos-x64' }} with lc ed25519
313+
image: [macos-15-large, macos-15]
314+
name: ${{ matrix.image == 'macos-15' && 'macos' || 'macos-x64' }} with lc ed25519
315315
runs-on: ${{ matrix.image }}
316316
steps:
317317
- uses: aws-actions/configure-aws-credentials@v4

CMakeLists.txt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ if (WIN32)
6262
endif ()
6363

6464
elseif (APPLE)
65+
if(DEFINED CMAKE_OSX_SYSROOT)
66+
message(STATUS "CMAKE_OSX_SYSROOT is defined and set to: ${CMAKE_OSX_SYSROOT} this value")
67+
else()
68+
message(STATUS "CMAKE_OSX_SYSROOT is not defined")
69+
endif()
70+
71+
# As of CMake 4.0 isysroot is no longer set on mac and instead /usr/local is checked
72+
# On systems where custom headers are installed at that location (homebrew loves doing that)
73+
# we can end up building against random libcrypto headers, which are not guaranteed to be forward compatible.
74+
# This tries to set isysroot in a way that works on as many combinations of cmake and os as possible.
75+
if(NOT CMAKE_OSX_SYSROOT)
76+
execute_process(
77+
COMMAND xcrun --show-sdk-path
78+
RESULT_VARIABLE XCRUN_RESULT
79+
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
80+
OUTPUT_STRIP_TRAILING_WHITESPACE
81+
ERROR_QUIET
82+
)
83+
if(NOT XCRUN_RESULT EQUAL 0)
84+
message(WARNING "Failed to determine SDK path using xcrun")
85+
endif()
86+
endif()
87+
6588
if (NOT BYO_CRYPTO)
6689
file(GLOB AWS_CAL_OS_SRC
6790
"source/darwin/*.c"
@@ -79,7 +102,6 @@ elseif (APPLE)
79102
message(FATAL_ERROR "Security Framework not found")
80103
endif ()
81104

82-
83105
find_library(COREFOUNDATION_LIB CoreFoundation)
84106
if(NOT COREFOUNDATION_LIB)
85107
message(FATAL_ERROR "CoreFoundation Framework not found")
@@ -116,7 +138,7 @@ if (NOT BYO_CRYPTO)
116138
message(FATAL_ERROR "Target crypto is not defined, failed to find libcrypto.")
117139
endif()
118140
else()
119-
# note aws_use_package() does this for you, except it appends to the public link targets
141+
# note aws_use_package() does this for you, except it appends to the public link targets
120142
# which we probably don't want for this case where we want the crypto dependency private
121143
if (IN_SOURCE_BUILD)
122144
list(APPEND PLATFORM_LIBS crypto)

include/aws/cal/private/opensslcrypto_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ extern struct openssl_evp_md_ctx_table *g_aws_openssl_evp_md_ctx_table;
8181

8282
int aws_reinterpret_lc_evp_error_as_crt(int evp_error, const char *function_name, enum aws_cal_log_subject subject);
8383

84+
void aws_validate_libcrypto_linkage(void);
85+
8486
#endif /* AWS_C_CAL_OPENSSLCRYPTO_COMMON_H */

source/darwin/commoncrypto_platform_init.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
*/
55

66
#include <aws/common/allocator.h>
7-
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) && defined(OPENSSL_IS_AWSLC)
8-
# include <openssl/thread.h>
7+
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
8+
# include <aws/cal/private/opensslcrypto_common.h>
9+
# if defined(OPENSSL_IS_AWSLC)
10+
# include <openssl/thread.h>
11+
# endif
912
#endif
1013

1114
void aws_cal_platform_init(struct aws_allocator *allocator) {
15+
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
16+
aws_validate_libcrypto_linkage();
17+
#endif
1218
(void)allocator;
1319
}
1420

source/shared/ed25519.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <aws/common/encoding.h>
1111

1212
#include <openssl/evp.h>
13+
#include <openssl/objects.h>
1314

1415
#if defined(OPENSSL_IS_OPENSSL) && OPENSSL_VERSION_NUMBER < 0x10101000L
1516
/* ed25519 support does not exist prior to 1.1.1 */
@@ -61,7 +62,16 @@ struct aws_ed25519_key_pair_impl *aws_ed25519_key_pair_new_generate_impl(struct
6162
#else
6263
EVP_PKEY *pkey = NULL;
6364

64-
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL);
65+
/* Note: nids are not consistent between versions, so we need to do runtime retrieval
66+
* to avoid weird issues when building against one version and running against different version. */
67+
int nid = OBJ_sn2nid("ED25519");
68+
if (nid == NID_undef) {
69+
aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
70+
return NULL;
71+
}
72+
73+
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(nid, NULL);
74+
6575
if (ctx == NULL) {
6676
aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM);
6777
return NULL;

source/shared/lccrypto_common.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,44 @@ int aws_reinterpret_lc_evp_error_as_crt(int evp_error, const char *function_name
7373

7474
return aws_raise_error(crt_error);
7575
}
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+
}

source/unix/openssl_platform_init.c

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -653,47 +653,6 @@ static enum aws_libcrypto_version s_resolve_libcrypto_sharedlib(void) {
653653
return AWS_LIBCRYPTO_NONE;
654654
}
655655

656-
/* Validate at runtime that we're linked against the same libcrypto we compiled against. */
657-
static void s_validate_libcrypto_linkage(void) {
658-
/* NOTE: the choice of stack buffer size is somewhat arbitrary. it's
659-
* possible, but unlikely, that libcrypto version strings may exceed this in
660-
* the future. we guard against buffer overflow by limiting write size in
661-
* snprintf with the size of the buffer itself. if libcrypto version strings
662-
* do eventually exceed the chosen size, this runtime check will fail and
663-
* will need to be addressed by increasing buffer size.*/
664-
char expected_version[64] = {0};
665-
#if defined(OPENSSL_IS_AWSLC)
666-
/* get FIPS mode at runtime because headers don't give any indication of
667-
* AWS-LC's FIPSness at aws-c-cal compile time. version number can still be
668-
* captured at preprocess/compile time from AWSLC_VERSION_NUMBER_STRING.*/
669-
const char *mode = FIPS_mode() ? "AWS-LC FIPS" : "AWS-LC";
670-
snprintf(expected_version, sizeof(expected_version), "%s %s", mode, AWSLC_VERSION_NUMBER_STRING);
671-
#elif defined(OPENSSL_IS_BORINGSSL)
672-
snprintf(expected_version, sizeof(expected_version), "BoringSSL");
673-
#elif defined(OPENSSL_IS_OPENSSL)
674-
snprintf(expected_version, sizeof(expected_version), OPENSSL_VERSION_TEXT);
675-
#elif !defined(BYO_CRYPTO)
676-
# error Unsupported libcrypto!
677-
#endif
678-
const char *runtime_version = SSLeay_version(SSLEAY_VERSION);
679-
AWS_LOGF_DEBUG(
680-
AWS_LS_CAL_LIBCRYPTO_RESOLVE,
681-
"Compiled with libcrypto %s, linked to libcrypto %s",
682-
expected_version,
683-
runtime_version);
684-
#if defined(OPENSSL_IS_OPENSSL)
685-
/* Validate that the string "AWS-LC" doesn't appear in OpenSSL version str. */
686-
AWS_FATAL_ASSERT(strstr("AWS-LC", expected_version) == NULL);
687-
AWS_FATAL_ASSERT(strstr("AWS-LC", runtime_version) == NULL);
688-
/* Validate both expected and runtime versions begin with OpenSSL's version str prefix. */
689-
const char *openssl_prefix = "OpenSSL ";
690-
AWS_FATAL_ASSERT(strncmp(openssl_prefix, expected_version, strlen(openssl_prefix)) == 0);
691-
AWS_FATAL_ASSERT(strncmp(openssl_prefix, runtime_version, strlen(openssl_prefix)) == 0);
692-
#else
693-
AWS_FATAL_ASSERT(strcmp(expected_version, runtime_version) == 0 && "libcrypto mislink");
694-
#endif
695-
}
696-
697656
static enum aws_libcrypto_version s_resolve_libcrypto(void) {
698657
/* Try to auto-resolve against what's linked in/process space */
699658
AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "searching process and loaded modules");
@@ -719,7 +678,7 @@ static enum aws_libcrypto_version s_resolve_libcrypto(void) {
719678
result = s_resolve_libcrypto_sharedlib();
720679
}
721680

722-
s_validate_libcrypto_linkage();
681+
aws_validate_libcrypto_linkage();
723682

724683
return result;
725684
}

source/windows/bcrypt_platform_init.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,18 @@
44
*/
55

66
#include <aws/common/allocator.h>
7-
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) && defined(OPENSSL_IS_AWSLC)
8-
# include <openssl/thread.h>
9-
# include <windows.h>
7+
8+
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
9+
# include <aws/cal/private/opensslcrypto_common.h>
1010
#endif
1111

1212
void aws_cal_platform_init(struct aws_allocator *allocator) {
13+
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
14+
aws_validate_libcrypto_linkage();
15+
#endif
1316
(void)allocator;
1417
}
1518

16-
void aws_cal_platform_clean_up(void) {
17-
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) && defined(OPENSSL_IS_AWSLC)
18-
AWSLC_thread_local_clear();
19-
#endif
20-
}
19+
void aws_cal_platform_clean_up(void) {}
2120

22-
void aws_cal_platform_thread_clean_up(void) {
23-
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) && defined(OPENSSL_IS_AWSLC)
24-
AWSLC_thread_local_clear();
25-
#endif
26-
}
27-
28-
#if defined(AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE) && defined(OPENSSL_IS_AWSLC)
29-
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
30-
switch (fdwReason) {
31-
case DLL_PROCESS_DETACH:
32-
AWSLC_thread_local_shutdown();
33-
break;
34-
}
35-
return TRUE;
36-
}
37-
#endif
21+
void aws_cal_platform_thread_clean_up(void) {}

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ add_test_case(ed25519_key_pair_generate_test)
159159

160160
generate_test_driver(${PROJECT_NAME}-tests)
161161

162+
if (AWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
163+
target_compile_definitions(${PROJECT_NAME}-tests PRIVATE -DAWS_USE_LIBCRYPTO_TO_SUPPORT_ED25519_EVERYWHERE)
164+
endif()
165+
162166
# OpenBSD 7.4+ defaults to linking with --execute-only which is not always safe for AWS-LC.
163167
# We have similar link flags in bindings, but in this case we need on the test executable,
164168
# because ed25519 keygen is hitting the same issue

tests/ed25519_test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ static int s_ed25519_key_pair_generate_test(struct aws_allocator *allocator, voi
3333
# endif
3434
# endif
3535
#endif
36-
3736
return AWS_OP_SKIP;
3837
}
3938

0 commit comments

Comments
 (0)