Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,11 @@ void ReadMacOSKeychainCertificates(
CFRelease(search);

if (ortn) {
fprintf(stderr, "ERROR: SecItemCopyMatching failed %d\n", ortn);
per_process::Debug(DebugCategory::CRYPTO,
"Cannot read certificates from system because "
"SecItemCopyMatching failed %d\n",
ortn);
return;
Copy link
Member Author

@joyeecheung joyeecheung Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by: I added a skip here too, otherwise the code operating on that array below can crash (I think it's possible on a super duper fresh machine with zero certificates, for example - though most consumer macOS machines seem to have at least some preinstalled, or when there's some permission issue going on)

}

CFIndex count = CFArrayGetCount(curr_anchors);
Expand All @@ -518,17 +522,29 @@ void ReadMacOSKeychainCertificates(

CFDataRef der_data = SecCertificateCopyData(cert_ref);
if (!der_data) {
fprintf(stderr, "ERROR: SecCertificateCopyData failed\n");
per_process::Debug(DebugCategory::CRYPTO,
"Skipping read of a system certificate "
"because SecCertificateCopyData failed\n");
continue;
}
auto data_buffer_pointer = CFDataGetBytePtr(der_data);

X509* cert =
d2i_X509(nullptr, &data_buffer_pointer, CFDataGetLength(der_data));
CFRelease(der_data);

if (cert == nullptr) {
per_process::Debug(DebugCategory::CRYPTO,
"Skipping read of a system certificate "
"because decoding failed\n");
continue;
}

bool is_valid = IsCertificateTrustedForPolicy(cert, cert_ref);
if (is_valid) {
system_root_certificates_X509->emplace_back(cert);
} else {
X509_free(cert);
}
}
CFRelease(curr_anchors);
Expand Down Expand Up @@ -638,7 +654,14 @@ void GatherCertsForLocation(std::vector<X509*>* vector,
reinterpret_cast<const unsigned char*>(cert_from_store->pbCertEncoded);
const size_t cert_size = cert_from_store->cbCertEncoded;

vector->emplace_back(d2i_X509(nullptr, &cert_data, cert_size));
X509* x509 = d2i_X509(nullptr, &cert_data, cert_size);
if (x509 == nullptr) {
per_process::Debug(DebugCategory::CRYPTO,
"Skipping read of a system certificate "
"because decoding failed\n");
} else {
vector->emplace_back(x509);
}
}
}

Expand Down
Loading