Skip to content

Commit 11a1ab7

Browse files
committed
tests/server: add optional required client cert auth.
This commit updates the `tests/server.c` program so that if an `AUTH_CERT` env var is provided the server will be configured to require clients provide a client certificate issued that chains to the `AUTH_CERT` certificate authority. If no `AUTH_CERT` env var is set the server works as it did before, ignoring client certificate authentication.
1 parent f7ca733 commit 11a1ab7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/server.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ main(int argc, const char **argv)
255255
struct rustls_connection *rconn = NULL;
256256
const struct rustls_certified_key *certified_key = NULL;
257257
struct rustls_slice_bytes alpn_http11;
258+
const struct rustls_client_cert_verifier *client_cert_verifier = NULL;
259+
struct rustls_root_cert_store *client_cert_root_store = NULL;
258260

259261
alpn_http11.data = (unsigned char*)"http/1.1";
260262
alpn_http11.len = 8;
@@ -285,6 +287,22 @@ main(int argc, const char **argv)
285287
config_builder, &certified_key, 1);
286288
rustls_server_config_builder_set_alpn_protocols(config_builder, &alpn_http11, 1);
287289

290+
char* auth_cert = getenv("AUTH_CERT");
291+
if(auth_cert) {
292+
char certbuf[10000];
293+
size_t certbuf_len;
294+
int result = read_file(argv[0], auth_cert, certbuf, sizeof(certbuf), &certbuf_len);
295+
if(result != DEMO_OK) {
296+
goto cleanup;
297+
}
298+
299+
client_cert_root_store = rustls_root_cert_store_new();
300+
rustls_root_cert_store_add_pem(client_cert_root_store, (uint8_t *)certbuf, certbuf_len, true);
301+
302+
client_cert_verifier = rustls_client_cert_verifier_new(client_cert_root_store);
303+
rustls_server_config_builder_set_client_verifier(config_builder, client_cert_verifier);
304+
}
305+
288306
server_config = rustls_server_config_builder_build(config_builder);
289307

290308
#ifdef _WIN32
@@ -360,6 +378,8 @@ main(int argc, const char **argv)
360378

361379
cleanup:
362380
rustls_certified_key_free(certified_key);
381+
rustls_root_cert_store_free(client_cert_root_store);
382+
rustls_client_cert_verifier_free(client_cert_verifier);
363383
rustls_server_config_free(server_config);
364384
rustls_connection_free(rconn);
365385
if(sockfd>0) {

0 commit comments

Comments
 (0)