|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +/** |
| 22 | + * Example demonstrating client certificate (mTLS) authentication. |
| 23 | + * |
| 24 | + * This example shows how to: |
| 25 | + * 1. Configure the server to request client certificates |
| 26 | + * 2. Extract client certificate information in request handlers |
| 27 | + * 3. Implement certificate-based access control |
| 28 | + * |
| 29 | + * To test this example: |
| 30 | + * |
| 31 | + * 1. Generate server certificate and key: |
| 32 | + * openssl req -x509 -newkey rsa:2048 -keyout server_key.pem -out server_cert.pem \ |
| 33 | + * -days 365 -nodes -subj "/CN=localhost" |
| 34 | + * |
| 35 | + * 2. Generate a CA certificate for client certs: |
| 36 | + * openssl req -x509 -newkey rsa:2048 -keyout ca_key.pem -out ca_cert.pem \ |
| 37 | + * -days 365 -nodes -subj "/CN=Test CA" |
| 38 | + * |
| 39 | + * 3. Generate client certificate signed by the CA: |
| 40 | + * openssl req -newkey rsa:2048 -keyout client_key.pem -out client_csr.pem \ |
| 41 | + * -nodes -subj "/CN=Alice/O=Engineering" |
| 42 | + * openssl x509 -req -in client_csr.pem -CA ca_cert.pem -CAkey ca_key.pem \ |
| 43 | + * -CAcreateserial -out client_cert.pem -days 365 |
| 44 | + * |
| 45 | + * 4. Run the server: |
| 46 | + * ./client_cert_auth |
| 47 | + * |
| 48 | + * 5. Test with curl using client certificate: |
| 49 | + * curl -k --cert client_cert.pem --key client_key.pem https://localhost:8443/secure |
| 50 | + * |
| 51 | + * Or without a certificate (will be denied): |
| 52 | + * curl -k https://localhost:8443/secure |
| 53 | + */ |
| 54 | + |
| 55 | +#include <iostream> |
| 56 | +#include <memory> |
| 57 | +#include <set> |
| 58 | +#include <string> |
| 59 | + |
| 60 | +#include <httpserver.hpp> |
| 61 | + |
| 62 | +// Set of allowed certificate fingerprints (SHA-256, hex-encoded) |
| 63 | +// In a real application, this would be loaded from a database or config file |
| 64 | +std::set<std::string> allowed_fingerprints; |
| 65 | + |
| 66 | +// Resource that requires client certificate authentication |
| 67 | +class secure_resource : public httpserver::http_resource { |
| 68 | + public: |
| 69 | + std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) { |
| 70 | + // Check if client provided a certificate |
| 71 | + if (!req.has_client_certificate()) { |
| 72 | + return std::make_shared<httpserver::string_response>( |
| 73 | + "Client certificate required", |
| 74 | + httpserver::http::http_utils::http_unauthorized, "text/plain"); |
| 75 | + } |
| 76 | + |
| 77 | + // Get certificate information |
| 78 | + std::string cn = req.get_client_cert_cn(); |
| 79 | + std::string dn = req.get_client_cert_dn(); |
| 80 | + std::string issuer = req.get_client_cert_issuer_dn(); |
| 81 | + std::string fingerprint = req.get_client_cert_fingerprint_sha256(); |
| 82 | + bool verified = req.is_client_cert_verified(); |
| 83 | + |
| 84 | + // Check if certificate is verified by our CA |
| 85 | + if (!verified) { |
| 86 | + return std::make_shared<httpserver::string_response>( |
| 87 | + "Certificate not verified by trusted CA", |
| 88 | + httpserver::http::http_utils::http_forbidden, "text/plain"); |
| 89 | + } |
| 90 | + |
| 91 | + // Optional: Check fingerprint against allowlist |
| 92 | + if (!allowed_fingerprints.empty() && |
| 93 | + allowed_fingerprints.find(fingerprint) == allowed_fingerprints.end()) { |
| 94 | + return std::make_shared<httpserver::string_response>( |
| 95 | + "Certificate not in allowlist", |
| 96 | + httpserver::http::http_utils::http_forbidden, "text/plain"); |
| 97 | + } |
| 98 | + |
| 99 | + // Check certificate validity times |
| 100 | + time_t now = time(nullptr); |
| 101 | + time_t not_before = req.get_client_cert_not_before(); |
| 102 | + time_t not_after = req.get_client_cert_not_after(); |
| 103 | + |
| 104 | + if (now < not_before) { |
| 105 | + return std::make_shared<httpserver::string_response>( |
| 106 | + "Certificate not yet valid", |
| 107 | + httpserver::http::http_utils::http_forbidden, "text/plain"); |
| 108 | + } |
| 109 | + |
| 110 | + if (now > not_after) { |
| 111 | + return std::make_shared<httpserver::string_response>( |
| 112 | + "Certificate has expired", |
| 113 | + httpserver::http::http_utils::http_forbidden, "text/plain"); |
| 114 | + } |
| 115 | + |
| 116 | + // Build response with certificate info |
| 117 | + std::string response = "Welcome, " + cn + "!\n\n"; |
| 118 | + response += "Certificate Details:\n"; |
| 119 | + response += " Subject DN: " + dn + "\n"; |
| 120 | + response += " Issuer DN: " + issuer + "\n"; |
| 121 | + response += " Fingerprint (SHA-256): " + fingerprint + "\n"; |
| 122 | + response += " Verified: " + std::string(verified ? "Yes" : "No") + "\n"; |
| 123 | + |
| 124 | + return std::make_shared<httpserver::string_response>(response, 200, "text/plain"); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +// Public resource that shows certificate info but doesn't require it |
| 129 | +class info_resource : public httpserver::http_resource { |
| 130 | + public: |
| 131 | + std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request& req) { |
| 132 | + std::string response; |
| 133 | + |
| 134 | + if (req.has_client_certificate()) { |
| 135 | + response = "Client certificate detected:\n"; |
| 136 | + response += " Common Name: " + req.get_client_cert_cn() + "\n"; |
| 137 | + response += " Verified: " + std::string(req.is_client_cert_verified() ? "Yes" : "No") + "\n"; |
| 138 | + } else { |
| 139 | + response = "No client certificate provided.\n"; |
| 140 | + response += "Use --cert and --key with curl to provide one.\n"; |
| 141 | + } |
| 142 | + |
| 143 | + return std::make_shared<httpserver::string_response>(response, 200, "text/plain"); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +int main() { |
| 148 | + std::cout << "Starting HTTPS server with client certificate authentication on port 8443...\n"; |
| 149 | + std::cout << "\nEndpoints:\n"; |
| 150 | + std::cout << " /info - Shows certificate info (optional cert)\n"; |
| 151 | + std::cout << " /secure - Requires valid client certificate\n\n"; |
| 152 | + |
| 153 | + // Create webserver with SSL and client certificate trust store |
| 154 | + httpserver::webserver ws = httpserver::create_webserver(8443) |
| 155 | + .use_ssl() |
| 156 | + .https_mem_key("server_key.pem") // Server private key |
| 157 | + .https_mem_cert("server_cert.pem") // Server certificate |
| 158 | + .https_mem_trust("ca_cert.pem"); // CA certificate for verifying client certs |
| 159 | + |
| 160 | + secure_resource secure; |
| 161 | + info_resource info; |
| 162 | + |
| 163 | + ws.register_resource("/secure", &secure); |
| 164 | + ws.register_resource("/info", &info); |
| 165 | + |
| 166 | + std::cout << "Server started. Press Ctrl+C to stop.\n\n"; |
| 167 | + std::cout << "Test commands:\n"; |
| 168 | + std::cout << " curl -k https://localhost:8443/info\n"; |
| 169 | + std::cout << " curl -k --cert client_cert.pem --key client_key.pem https://localhost:8443/info\n"; |
| 170 | + std::cout << " curl -k --cert client_cert.pem --key client_key.pem https://localhost:8443/secure\n"; |
| 171 | + |
| 172 | + ws.start(true); |
| 173 | + |
| 174 | + return 0; |
| 175 | +} |
0 commit comments