Files
eugene e8f8218f33 Treat an empty CA as the system store in load_tls
ziti_context_init() stores "" for a config without a CA, which the openssl backend
rejects. Also refresh the shared test loop's clock so test order cannot fire timers at once.
2026-10-14 13:57:13 -04:00

230 lines
7.8 KiB
C++

// Copyright (c) 2026. NetFoundry Inc
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// model_support.h #defines `map`: tls_test_util.h pulls in the standard headers, so it goes first
#include "tls_test_util.h"
#include "catch2_includes.hpp"
#include <uv.h>
#include <ziti/ziti.h>
#include <ziti/errors.h>
#include "auth_method.h"
#include "credentials.h"
namespace {
// one loop for every case: creating and closing a loop per case trips libuv's fd assertion when
// the whole suite runs in one process
uv_loop_t *test_loop() {
static uv_loop_t *loop = uv_loop_new();
// the cached time is stale if other cases ran since the loop last did: timers started on
// it would fire at once
uv_update_time(loop);
return loop;
}
struct shutdown_test {
bool from_timer = false;
int init_err = ZITI_OK;
bool disabled = false;
uv_timer_t shutdown_timer{};
ziti_context ztx = nullptr;
};
void on_event(ziti_context ztx, const ziti_event_t *ev) {
auto t = (shutdown_test *) ziti_app_ctx(ztx);
if (ev->type != ZitiContextEvent) return;
if (ev->ctx.ctrl_status == ZITI_DISABLED) {
t->disabled = true;
return;
}
if (ev->ctx.ctrl_status == ZITI_OK || t->init_err != ZITI_OK) return;
t->init_err = ev->ctx.ctrl_status;
if (t->from_timer) {
t->ztx = ztx;
uv_timer_init(test_loop(), &t->shutdown_timer);
t->shutdown_timer.data = t;
uv_timer_start(&t->shutdown_timer, [](uv_timer_t *timer) {
auto t = (shutdown_test *) timer->data;
ziti_shutdown(t->ztx);
uv_close((uv_handle_t *) timer, nullptr);
}, 10, 0);
} else {
ziti_shutdown(ztx);
}
}
// stands in for a process without a loaded user profile, where every persisted key
// operation fails: the thread impersonates its own token with the user SID deny-only,
// so the user key store is denied
struct key_store_unreachable {
#if _WIN32
HANDLE self = nullptr, restricted = nullptr, imp = nullptr;
key_store_unreachable() {
REQUIRE(OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &self));
BYTE user_buf[256];
DWORD user_len = 0;
REQUIRE(GetTokenInformation(self, TokenUser, user_buf, sizeof(user_buf), &user_len));
// SYSTEM's key store stays reachable through a token that denies the SYSTEM SID
if (IsWellKnownSid(reinterpret_cast<TOKEN_USER *>(user_buf)->User.Sid, WinLocalSystemSid)) {
CloseHandle(self);
self = nullptr;
SKIP("the user key store cannot be denied to SYSTEM");
}
SID_AND_ATTRIBUTES deny = {reinterpret_cast<TOKEN_USER *>(user_buf)->User.Sid, 0};
REQUIRE(CreateRestrictedToken(self, 0, 1, &deny, 0, nullptr, 0, nullptr, &restricted));
REQUIRE(DuplicateToken(restricted, SecurityImpersonation, &imp));
REQUIRE(SetThreadToken(nullptr, imp));
}
~key_store_unreachable() {
RevertToSelf();
if (imp) CloseHandle(imp);
if (restricted) CloseHandle(restricted);
if (self) CloseHandle(self);
}
#else
key_store_unreachable() { FAIL("win32 only"); }
#endif
};
}
TEST_CASE("ziti_shutdown completes after a TLS init failure", "[ztx]") {
shutdown_test test;
SECTION("shutdown from the event callback") {
test.from_timer = false;
}
SECTION("shutdown from a later loop callback") {
test.from_timer = true;
}
// 192.0.2.0/24 is the RFC 5737 documentation range: the controller is never contacted
ziti_config cfg = {};
cfg.controller_url = (char *) "https://192.0.2.1:1280";
cfg.id.key = (char *) "pem:not a private key";
ziti_context ztx = nullptr;
REQUIRE(ziti_context_init(&ztx, &cfg) == ZITI_OK);
ziti_options opts = {};
opts.app_ctx = &test;
opts.events = ZitiContextEvent;
opts.event_cb = on_event;
REQUIRE(ziti_context_set_options(ztx, &opts) == ZITI_OK);
uv_loop_t *loop = test_loop();
REQUIRE(ziti_context_run(ztx, loop) == ZITI_OK);
// unref'd: it ends a hung shutdown but does not keep a finished loop alive
uv_timer_t guard;
uv_timer_init(loop, &guard);
uv_unref((uv_handle_t *) &guard);
uv_timer_start(&guard, [](uv_timer_t *timer) { uv_stop(timer->loop); }, 5000, 0);
uv_run(loop, UV_RUN_DEFAULT);
bool timed_out = uv_is_active((uv_handle_t *) &guard) == 0;
uv_close((uv_handle_t *) &guard, nullptr);
uv_run(loop, UV_RUN_DEFAULT);
CHECK(test.init_err == ZITI_INVALID_CONFIG);
CHECK_FALSE(timed_out);
CHECK(test.disabled);
}
TEST_CASE("load_tls reports a key the win32crypto key store cannot persist", "[crypto]") {
persisted_key_cleanup cleanup;
// other backends keep the key in memory, so the key store can only fail on win32crypto
tls_context *probe = default_tls_context();
std::string version = probe->version();
probe->free_ctx(probe);
if (version.find("win32crypto") == std::string::npos) {
SKIP("not a win32crypto build: " << version);
}
std::string key_ref = std::string("pem:") + rsa_key;
std::string cert_ref = std::string("pem:") + rsa_cert;
ziti_config cfg{};
cfg.id.key = const_cast<char *>(key_ref.c_str());
cfg.id.cert = const_cast<char *>(cert_ref.c_str());
tls_context *tls = nullptr;
zt_x509 creds{};
int rc;
{
key_store_unreachable guard;
rc = load_tls(&cfg, &tls, &creds);
}
CHECK(rc == ZITI_INVALID_CERT_KEY_PAIR);
CHECK(tls == nullptr);
CHECK(creds.key == nullptr);
CHECK(creds.cert == nullptr);
if (tls) tls->free_ctx(tls);
zt_x509_drop(&creds);
}
TEST_CASE("load_tls rejects a CA bundle the TLS backend cannot load", "[ztx]") {
ziti_config cfg{};
SECTION("inline PEM without a certificate") {
cfg.id.ca = (char *) "pem:not a certificate";
}
SECTION("file that does not exist") {
cfg.id.ca = (char *) "file:///nonexistent/ziti-ca.pem";
}
// a rejected bundle must not fall back to the system CA store
tls_context *tls = nullptr;
CHECK(load_tls(&cfg, &tls, nullptr) == ZITI_INVALID_CONFIG);
CHECK(tls == nullptr);
if (tls) tls->free_ctx(tls);
}
TEST_CASE("load_tls uses the system CA store when the identity has no CA", "[ztx]") {
ziti_config cfg{};
SECTION("no CA") {
cfg.id.ca = nullptr;
}
SECTION("empty CA") {
// what ziti_context_init() stores for a config without a CA
cfg.id.ca = (char *) "";
}
tls_context *tls = nullptr;
CHECK(load_tls(&cfg, &tls, nullptr) == ZITI_OK);
CHECK(tls != nullptr);
if (tls) tls->free_ctx(tls);
}
TEST_CASE("new_oidc_auth fails on a CA bundle the TLS backend cannot load", "[ztx]") {
const char *json = R"({"path": "/oidc", "apiBaseUrls": ["https://192.0.2.1:1280"]})";
api_path oidc{};
REQUIRE(parse_api_path(&oidc, json, strlen(json)) >= 0);
zt_x509 creds{};
uv_loop_t *loop = test_loop();
ziti_auth_method_t *auth = new_oidc_auth(loop, &oidc, "not a certificate", &creds);
CHECK(auth == nullptr);
if (auth) {
auth->free(auth);
uv_run(loop, UV_RUN_DEFAULT);
}
free_api_path(&oidc);
}