1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/*
* SPDX-FileCopyrightText: 2019 Louis Solofrizzo
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <array>
#include <cstring>
#include <pistache/winornix.h>
#include <pistache/ps_strl.h> // for PS_STRNCPY_S
#include <pistache/client.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <gtest/gtest.h>
#include <curl/curl.h>
using namespace Pistache;
/* Should these tests fail, please re-run "./new-certs.sh" from the "./certs"
* directory.
*/
/* Sept/2024: In Windows, if basic_tls_request_with_auth and
* basic_tls_request_with_auth_with_cb fail, you may need to uninstall the
* default (schannel) libcurl, and install the openssl one instead:
* vcpkg remove curl
* vcpkg install curl[openssl]
*
* See https://github.com/openssl/openssl/issues/25520 for more details
*/
static size_t write_cb(void* contents, size_t size, size_t nmemb, void* userp)
{
(static_cast<std::string*>(userp))->append(static_cast<char*>(contents), size * nmemb);
return size * nmemb;
}
static std::string getServerUrl(const Http::Endpoint& server)
{
return std::string("https://localhost:") + server.getPort().toString();
}
struct HelloHandler : public Http::Handler
{
HTTP_PROTOTYPE(HelloHandler)
void onRequest(const Http::Request&, Http::ResponseWriter writer) override
{
PS_TIMEDBG_START_THIS;
writer.send(Http::Code::Ok, "Hello, World!");
}
};
struct ServeFileHandler : public Http::Handler
{
HTTP_PROTOTYPE(ServeFileHandler)
void onRequest(const Http::Request&, Http::ResponseWriter writer) override
{
Http::serveFile(writer, "./certs/rootCA.crt")
.then(
[](PST_SSIZE_T bytes) {
std::cout << "Sent " << bytes << " bytes" << std::endl;
},
Async::NoExcept);
}
};
// @March/2024
//
// In macOS, calling curl_global_init, and then curl_global_cleanup, for every
// single test does not work. On the second test to be run, it generates the
// following error in the Pistache code:
/* listener.cc:691 in handleNewConnection(): SSL connection error: 0070E86F01000000:error:0A00041B:SSL routines:ssl3_read_bytes:tlsv1 alert decrypt error:ssl/record/rec_layer_s3.c:861:SSL alert number 51
*/
// In the openssl documentation, 51 decrypt_error is described as:
// Failed handshake cryptographic operation, including being unable to
// correctly verify a signature, decrypt a key exchange, or validate a
// finished message.
// BTW, updating Pistache certs (new-certs.sh) makes no difference
//
// The same code on Linux works no problem.
//
// The curl documentation is (IMHO) unclear as to whether it is OK to call
// curl_global_init+curl_global_cleanup repeatedly.
// https://everything.curl.dev/libcurl/globalinit states:
//
// curl_global_init initializes global state so you should only call it once,
// and once your program is completely done using libcurl you can call
// curl_global_cleanup()...
//
// Motivated by that statement, we have changed the code so curl_global_init is
// called only once for the whole program, not once per test. That works on
// BOTH macOS and Linux.
//
// In macOS, we have the following version of curl:
/*
curl 8.4.0 (x86_64-apple-darwin23.0) libcurl/8.4.0 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.58.0
Release-Date: 2023-10-11
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL threadsafe UnixSockets
*/
// and the following version of openssl:
// OpenSSL 3.2.0 23 Nov 2023 (Library: OpenSSL 3.2.0 23 Nov 2023)
//
// In Linux (Unbuntu), we have the following version of curl:
/*
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.17
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd
*/
// and the following version of openssl:
// OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
// MUST be FIRST test
TEST(https_server_test, first_curl_global_init)
{
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
ASSERT_EQ(res, CURLE_OK);
}
#ifdef _WIN32
// CURLSSLOPT_REVOKE_BEST_EFFORT tells libcurl to ignore certificate revocation
// checks in case of missing or offline distribution points for those SSL
// backends where such behavior is present. This option is only supported for
// Schannel (the native Windows SSL library). Setting this option eliminates
// the stderr "schannel: CertGetCertificateChain trust error
// CERT_TRUST_REVOCATION_STATUS_UNKNOWN" message, and makes the following tests
// work in Windows which otherwise failed:
// https_server_test.basic_tls_request
// https_server_test.basic_tls_request_with_chained_server_cert
// https_server_test.basic_tls_request_with_servefile
// https_server_test.basic_tls_request_with_password_cert
#define CSO_WIN_REVOKE_BEST_EFFORT \
{ \
CURLcode set_ssl_opts_res = curl_easy_setopt( \
curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_REVOKE_BEST_EFFORT); \
ASSERT_EQ(set_ssl_opts_res, CURLE_OK); \
}
#else
#define CSO_WIN_REVOKE_BEST_EFFORT
#endif
TEST(https_server_test, basic_tls_request)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
// Skip hostname check
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
PS_LOG_DEBUG("curl_easy_perform");
res = curl_easy_perform(curl);
PS_LOG_DEBUG("curl_easy_perform done");
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer, "Hello, World!");
}
TEST(https_server_test, basic_tls_request_with_chained_server_cert)
{
PS_TIMEDBG_START_THIS;
PS_LOG_DEBUG("basic_tls_request_with_chained_server_cert");
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server_from_intermediate_with_chain.crt",
"./certs/server_from_intermediate.key");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer, "Hello, World!");
}
TEST(https_server_test, basic_tls_request_with_auth)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.useSSLAuth("./certs/rootCA.crt");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer, "Hello, World!");
}
TEST(https_server_test, basic_tls_request_with_auth_no_client_cert)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.useSSLAuth("./certs/rootCA.crt");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_NE(res, CURLE_OK);
}
TEST(https_server_test, basic_tls_request_with_auth_client_cert_not_signed)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.useSSLAuth("./certs/rootCA.crt");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client_not_signed.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client_not_signed.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_NE(res, CURLE_OK);
}
static bool callback_called = false;
static int verify_callback(int verify, void* ctx)
{
(void)verify;
(void)ctx;
callback_called = true;
return 1;
}
TEST(https_server_test, basic_tls_request_with_auth_with_cb)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.useSSLAuth("./certs/rootCA.crt", "./certs", &verify_callback);
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSLCERT, "./certs/client.crt");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "./certs/client.key");
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer, "Hello, World!");
ASSERT_EQ(callback_called, true);
callback_called = false;
}
TEST(https_server_test, basic_tls_request_with_servefile)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts);
server.setHandler(Http::make_handler<ServeFileHandler>());
server.useSSL("./certs/server.crt", "./certs/server.key");
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
std::array<char, CURL_ERROR_SIZE> errorstring;
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorstring.data());
// curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
std::cerr << errorstring.data() << std::endl;
}
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer.rfind("-----BEGIN CERTIFICATE-----", 0), 0u);
}
TEST(https_server_test, basic_tls_request_with_password_cert)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));
const auto passwordCallback = [](char* buf, int size, int /*rwflag*/, void* /*u*/) -> int {
static constexpr const char* const password = "test";
PS_STRNCPY_S(buf, size, password, size);
return static_cast<int>(std::strlen(password));
};
server.init(Http::Endpoint::options().flags(Tcp::Options::ReuseAddr));
server.setHandler(Http::make_handler<HelloHandler>());
server.useSSL("./certs/server_protected.crt", "./certs/server_protected.key", false, passwordCallback);
server.serveThreaded();
CURL* curl;
CURLcode res;
std::string buffer;
curl = curl_easy_init();
ASSERT_NE(curl, nullptr);
const auto url = getServerUrl(server);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CAINFO, "./certs/rootCA.crt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CSO_WIN_REVOKE_BEST_EFFORT;
/* Skip hostname check */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
server.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(buffer, "Hello, World!");
}
// MUST be LAST test
TEST(https_server_test, last_curl_global_cleanup)
{
curl_global_cleanup();
// Note: curl_global_cleanup has no return code (void)
}
|