Skip to content

Commit

Permalink
[mbedtls] use SecTrustCopyAnchorCertificates to get root certs on macOS.
Browse files Browse the repository at this point in the history
SecKeychainOpen is deprecated.
  • Loading branch information
Apprentice-Alchemist committed Apr 26, 2024
1 parent 7c3a65a commit 867a2ef
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions libs/mbedtls/mbedtls_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,36 +524,23 @@ CAMLprim value hx_cert_load_defaults(value certificate) {
#endif

#ifdef __APPLE__
CFMutableDictionaryRef search;
CFArrayRef result;
SecKeychainRef keychain;
SecCertificateRef item;
CFDataRef dat;
// Load keychain
if (SecKeychainOpen("/System/Library/Keychains/SystemRootCertificates.keychain", &keychain) == errSecSuccess) {
// Search for certificates
search = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
CFDictionarySetValue(search, kSecClass, kSecClassCertificate);
CFDictionarySetValue(search, kSecMatchLimit, kSecMatchLimitAll);
CFDictionarySetValue(search, kSecReturnRef, kCFBooleanTrue);
CFDictionarySetValue(search, kSecMatchSearchList, CFArrayCreate(NULL, (const void **)&keychain, 1, NULL));
if (SecItemCopyMatching(search, (CFTypeRef *)&result) == errSecSuccess) {
CFIndex n = CFArrayGetCount(result);
for (CFIndex i = 0; i < n; i++) {
item = (SecCertificateRef)CFArrayGetValueAtIndex(result, i);

// Get certificate in DER format
dat = SecCertificateCopyData(item);
if (dat) {
r = mbedtls_x509_crt_parse_der(chain, (unsigned char *)CFDataGetBytePtr(dat), CFDataGetLength(dat));
CFRelease(dat);
if (r != 0) {
CAMLreturn(Val_int(r));
}
CFArrayRef certs;
if (SecTrustCopyAnchorCertificates(&certs) == errSecSuccess) {
CFIndex count = CFArrayGetCount(certs);
for(CFIndex i = 0; i < count; i++) {
SecCertificateRef item = (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);

// Get certificate in DER format
CFDataRef data = SecCertificateCopyData(item);
if(data) {
r = mbedtls_x509_crt_parse_der(chain, (unsigned char *)CFDataGetBytePtr(data), CFDataGetLength(data));
CFRelease(data);
if (r != 0) {
CAMLreturn(Val_int(r));
}
}
}
CFRelease(keychain);
CFRelease(certs);
}
#endif

Expand Down

0 comments on commit 867a2ef

Please sign in to comment.