Skip to content

Commit be51e59

Browse files
committed
crypto: fix X509* leak in --use-system-ca
The X509 structures are never freed. Use ncrypto::X509Pointer to manage it automatically and move the X509* to PEM conversion into a helper to be reused by integration in other systems.
1 parent 304bb9c commit be51e59

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/crypto/crypto_context.cc

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using ncrypto::MarkPopErrorOnReturn;
3535
using ncrypto::SSLPointer;
3636
using ncrypto::StackOfX509;
3737
using ncrypto::X509Pointer;
38+
using ncrypto::X509View;
3839
using v8::Array;
3940
using v8::ArrayBufferView;
4041
using v8::Boolean;
@@ -255,6 +256,31 @@ bool isSelfIssued(X509* cert) {
255256
return X509_NAME_cmp(subject, issuer) == 0;
256257
}
257258

259+
void X509VectorToPEMVector(const std::vector<X509Pointer>& src,
260+
std::vector<std::string>* dest) {
261+
for (size_t i = 0; i < src.size(); i++) {
262+
X509View x509_view(src[i].get());
263+
264+
auto pem_bio = x509_view.toPEM();
265+
if (!pem_bio) {
266+
fprintf(stderr,
267+
"Warning: converting system certificate to PEM format failed\n");
268+
continue;
269+
}
270+
271+
char* pem_data = nullptr;
272+
auto pem_size = BIO_get_mem_data(pem_bio.get(), &pem_data);
273+
if (pem_size <= 0 || !pem_data) {
274+
fprintf(
275+
stderr,
276+
"Warning: cannot read PEM-encoded data from system certificate\n");
277+
continue;
278+
}
279+
280+
dest->emplace_back(pem_data, pem_size);
281+
}
282+
}
283+
258284
#ifdef __APPLE__
259285
// This code is loosely based on
260286
// https://github.com/chromium/chromium/blob/54bd8e3/net/cert/internal/trust_store_mac.cc
@@ -467,7 +493,7 @@ void ReadMacOSKeychainCertificates(
467493

468494
CFIndex count = CFArrayGetCount(curr_anchors);
469495

470-
std::vector<X509*> system_root_certificates_X509;
496+
std::vector<X509Pointer> system_root_certificates_X509;
471497
for (int i = 0; i < count; ++i) {
472498
SecCertificateRef cert_ref = reinterpret_cast<SecCertificateRef>(
473499
const_cast<void*>(CFArrayGetValueAtIndex(curr_anchors, i)));
@@ -489,28 +515,8 @@ void ReadMacOSKeychainCertificates(
489515
}
490516
CFRelease(curr_anchors);
491517

492-
for (size_t i = 0; i < system_root_certificates_X509.size(); i++) {
493-
ncrypto::X509View x509_view(system_root_certificates_X509[i]);
494-
495-
auto pem_bio = x509_view.toPEM();
496-
if (!pem_bio) {
497-
fprintf(stderr,
498-
"Warning: converting system certificate to PEM format failed\n");
499-
continue;
500-
}
501-
502-
char* pem_data = nullptr;
503-
auto pem_size = BIO_get_mem_data(pem_bio.get(), &pem_data);
504-
if (pem_size <= 0 || !pem_data) {
505-
fprintf(
506-
stderr,
507-
"Warning: cannot read PEM-encoded data from system certificate\n");
508-
continue;
509-
}
510-
std::string certificate_string_pem(pem_data, pem_size);
511-
512-
system_root_certificates->emplace_back(certificate_string_pem);
513-
}
518+
X509VectorToPEMVector(system_root_certificates_X509,
519+
system_root_certificates);
514520
}
515521
#endif // __APPLE__
516522

0 commit comments

Comments
 (0)