Skip to content

Commit ff26c87

Browse files
joyeecheungaduh95
authored andcommitted
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 d9d97b3 commit ff26c87

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
@@ -505,7 +505,11 @@ void ReadMacOSKeychainCertificates(
505505
CFRelease(search);
506506

507507
if (ortn) {
508-
fprintf(stderr, "ERROR: SecItemCopyMatching failed %d\n", ortn);
508+
per_process::Debug(DebugCategory::CRYPTO,
509+
"Cannot read certificates from system because "
510+
"SecItemCopyMatching failed %d\n",
511+
ortn);
512+
return;
509513
}
510514

511515
CFIndex count = CFArrayGetCount(curr_anchors);
@@ -516,17 +520,29 @@ void ReadMacOSKeychainCertificates(
516520

517521
CFDataRef der_data = SecCertificateCopyData(cert_ref);
518522
if (!der_data) {
519-
fprintf(stderr, "ERROR: SecCertificateCopyData failed\n");
523+
per_process::Debug(DebugCategory::CRYPTO,
524+
"Skipping read of a system certificate "
525+
"because SecCertificateCopyData failed\n");
520526
continue;
521527
}
522528
auto data_buffer_pointer = CFDataGetBytePtr(der_data);
523529

524530
X509* cert =
525531
d2i_X509(nullptr, &data_buffer_pointer, CFDataGetLength(der_data));
526532
CFRelease(der_data);
533+
534+
if (cert == nullptr) {
535+
per_process::Debug(DebugCategory::CRYPTO,
536+
"Skipping read of a system certificate "
537+
"because decoding failed\n");
538+
continue;
539+
}
540+
527541
bool is_valid = IsCertificateTrustedForPolicy(cert, cert_ref);
528542
if (is_valid) {
529543
system_root_certificates_X509->emplace_back(cert);
544+
} else {
545+
X509_free(cert);
530546
}
531547
}
532548
CFRelease(curr_anchors);
@@ -636,7 +652,14 @@ void GatherCertsForLocation(std::vector<X509*>* vector,
636652
reinterpret_cast<const unsigned char*>(cert_from_store->pbCertEncoded);
637653
const size_t cert_size = cert_from_store->cbCertEncoded;
638654

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

0 commit comments

Comments
 (0)