GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SSLClient CA cert configuration: separating `ca_cert_pem_` from system CA control, and supporting complementary CA combinations · Issue #2471 · yhirose/cpp-httplib

SSLClient CA cert configuration: separating ca_cert_pem_ from system CA control, and supporting complementary CA combinations #2471

New issue
Copy link
New issue
Copy link
Closed
Closed
SSLClient CA cert configuration: separating ca_cert_pem_ from system CA control, and supporting complementary CA combinations#2471
Copy link

Description

@Hyukya
Hyukya
opened on Jun 9, 2026
Issue body actions

Context

After reviewing the SSLClient source and its commit history, I can see that the three CA configuration interfaces are intentionally designed to be mutually exclusive:

  • set_ca_cert_path(file, dir) — file/directory path-based CA
  • set_ca_cert_store(store) — native OS cert store
  • load_ca_cert_store(pem, size) — in-memory PEM block

This is reflected in the load_certs() branching logic, which effectively enforces a file → dir → pem priority order:

if (!ca_cert_file_path_.empty()) {
    // file
} else if (!ca_cert_dir_path_.empty()) {
    // dir
} else if (ca_cert_pem_.empty()) {
    load_system_certs(ctx_);
}

I respect this design decision and would like to ask a few questions before proposing anything concrete.


Observation

ca_cert_pem_.empty() currently serves two roles at the same time:

  • Its intended role: "holds user-provided PEM data"
  • Its additional role: "signals that system CA should not be loaded"

These are conceptually independent concerns, and I would like to ask whether this coupling is an intentional design decision or whether there might be room to separate the two roles in the future.

As a result of this structure, setting a custom PEM simultaneously disables system CA loading, which means there is currently no supported way to configure the client to trust a custom CA (e.g., a corporate self-signed CA) while also trusting the system CA store. I would like to ask whether there is an intended approach for this use case.


Possible Direction

The following three approaches came to mind, all of which preserve the existing interfaces and their behavior entirely, with no breaking changes to existing users.

Option A — Dedicated functions per combination

Add a separate "with system CA" variant for each existing interface:

load_ca_cert_store_with_system(pem, size);
set_ca_cert_path_with_system(file, dir);
  • Pros: The function name alone fully communicates the intended behavior
  • Cons: The number of functions grows with the number of combinations

Option B — A single enable_system_ca() flag function (my preferred approach)

Add a single function that explicitly controls whether the system CA is loaded:

// Existing interface unchanged
client.load_ca_cert_store(pem, size);

// Explicitly opt in to system CA
client.enable_system_ca(true);
  • Pros: One additional function works with all existing interfaces; calling it multiple times is idempotent
  • Cons: Users need to be aware that both calls are required to achieve the combined behavior

Option C — Configuration struct

Introduce a new entry point that allows all CA sources to be configured explicitly at once:

struct CAConfig {
    std::string pem;
    std.string file_path;
    std::string dir_path;
    bool use_system_ca = false;
};
client.set_ca_config(config);
  • Pros: All combinations can be expressed in a single, explicit call; no repeated-call concerns; good extensibility
  • Cons: Introduces a new entry point that coexists alongside the existing interfaces

Questions

I would greatly appreciate your thoughts on the following:

  1. Is the use of ca_cert_pem_.empty() to implicitly control system CA loading an intentional design decision, or did it emerge naturally during implementation?

  2. Would you be open to the idea of separating system CA loading control into an independent flag (e.g., use_system_certs_)?

  3. Do you think making CA sources complementary (i.e., combinable) aligns with the design philosophy of this project?

  4. If supporting combinations is worth considering, which of the three options above do you think fits best with the direction of this project? Or if there is a better approach altogether, I would love to hear your thoughts.


Thank you very much for maintaining this library and for your time reading through this. I wanted to check alignment with your vision before moving forward with any concrete proposal.

Activity

  1. yhirose commented on Jun 10, 2026

    @yhirose
    yhirose
    on Jun 10, 2026
    Owner
    More actions

    Thank you for the thorough analysis. You read the situation correctly.

    The exclusive behavior is intentional and long-standing: before the TLS abstraction layer (#2342), load_certs() checked the X509_STORE * member, so a custom CA store has always suppressed system CA loading. The coupling with ca_cert_pem_ is not a deliberate decision, though. #2342 replaced that member with a PEM copy (kept for redirect transfer), and the old check was mechanically rewritten as ca_cert_pem_.empty().

    While investigating, I found that the same refactoring broke set_ca_cert_store(): it left no trace on the client, so system certs were silently merged into a user-provided store. Fixed in e7e7bf7.

    On your questions: yes to an independent flag, and yes to supporting "custom CA + system CA" as an explicit opt-in, keeping the current exclusive behavior as the safer default. I'll go with Option B (enable_system_ca(bool)). It keeps the API surface minimal and works with all existing interfaces.

    I'll also unify WebSocketClient, which currently loads system certs unconditionally (the opposite of SSLClient).

    I'll implement this myself and update this issue when it lands. Thanks again for the well-structured proposal.

    Reacted by Kim, Hyuk
  2. yhirose
    added a commit that references this issue on Jun 10, 2026

    Add enable_system_ca() and unify WebSocketClient CA handling (#2471)

    39ec7d0
  3. yhirose commented on Jun 10, 2026

    @yhirose
    yhirose
    on Jun 10, 2026
    Owner
    More actions

    Implemented in 39ec7d0.

    As discussed above, I went with Option B. enable_system_ca(bool) is now available on Client, SSLClient, and ws::WebSocketClient:

    httplib::Client cli("https://internal.example.com");
    cli.load_ca_cert_store(corp_ca_pem, corp_ca_pem_len);
    
    // Trust the system CA store in addition to the custom CA
    cli.enable_system_ca(true);

    The default behavior is unchanged: a custom CA remains exclusive, and system CA certs are loaded only when no custom CA is configured. The policy also carries over to clients created internally for cross-host HTTPS redirects.

    A few related changes that came out of this work:

    • WebSocketClient previously loaded system CA certs unconditionally, even with a custom CA configured. It now follows the same exclusive-by-default policy as SSLClient. If you relied on the old merging behavior, call enable_system_ca(true) explicitly.
    • WebSocketClient gained load_ca_cert_store(pem, size), and its TLS context now lives as long as the client, which fixes a use-after-free when reconnecting after set_ca_cert_store().

    Thanks again for the well-structured report. It led to one regression fix and several cleanups beyond the original question.

    Reacted by Kim, Hyuk
  4. yhirose
    closed this as completedon Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.