Skip to content

Commit f9f343f

Browse files
authored
src: handle DER decoding errors from system certificates
When decoding certificates from the system store, it's not actually guaranteed to succeed. In case the system returns a certificate that cannot be decoded (might be related to SSL implementation issues), skip them. PR-URL: #60787 Refs: microsoft/vscode#277064 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aditi Singh <aditisingh1400@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent b1e941e commit f9f343f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/crypto/crypto_context.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,11 @@ void ReadMacOSKeychainCertificates(
507507
CFRelease(search);
508508

509509
if (ortn) {
510-
fprintf(stderr, "ERROR: SecItemCopyMatching failed %d\n", ortn);
510+
per_process::Debug(DebugCategory::CRYPTO,
511+
"Cannot read certificates from system because "
512+
"SecItemCopyMatching failed %d\n",
513+
ortn);
514+
return;
511515
}
512516

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

519523
CFDataRef der_data = SecCertificateCopyData(cert_ref);
520524
if (!der_data) {
521-
fprintf(stderr, "ERROR: SecCertificateCopyData failed\n");
525+
per_process::Debug(DebugCategory::CRYPTO,
526+
"Skipping read of a system certificate "
527+
"because SecCertificateCopyData failed\n");
522528
continue;
523529
}
524530
auto data_buffer_pointer = CFDataGetBytePtr(der_data);
525531

526532
X509* cert =
527533
d2i_X509(nullptr, &data_buffer_pointer, CFDataGetLength(der_data));
528534
CFRelease(der_data);
535+
536+
if (cert == nullptr) {
537+
per_process::Debug(DebugCategory::CRYPTO,
538+
"Skipping read of a system certificate "
539+
"because decoding failed\n");
540+
continue;
541+
}
542+
529543
bool is_valid = IsCertificateTrustedForPolicy(cert, cert_ref);
530544
if (is_valid) {
531545
system_root_certificates_X509->emplace_back(cert);
546+
} else {
547+
X509_free(cert);
532548
}
533549
}
534550
CFRelease(curr_anchors);
@@ -638,7 +654,14 @@ void GatherCertsForLocation(std::vector<X509*>* vector,
638654
reinterpret_cast<const unsigned char*>(cert_from_store->pbCertEncoded);
639655
const size_t cert_size = cert_from_store->cbCertEncoded;
640656

641-
vector->emplace_back(d2i_X509(nullptr, &cert_data, cert_size));
657+
X509* x509 = d2i_X509(nullptr, &cert_data, cert_size);
658+
if (x509 == nullptr) {
659+
per_process::Debug(DebugCategory::CRYPTO,
660+
"Skipping read of a system certificate "
661+
"because decoding failed\n");
662+
} else {
663+
vector->emplace_back(x509);
664+
}
642665
}
643666
}
644667

0 commit comments

Comments
 (0)