roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_crl.c |
| 3 | * @author Roman Janota <janota@cesnet.cz> |
| 4 | * @brief libnetconf2 TLS CRL test |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include <pthread.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <cmocka.h> |
| 25 | |
| 26 | #include "tests/config.h" |
| 27 | |
| 28 | #define NC_ACCEPT_TIMEOUT 2000 |
| 29 | #define NC_PS_POLL_TIMEOUT 2000 |
| 30 | |
| 31 | struct ly_ctx *ctx; |
| 32 | |
| 33 | struct test_state { |
| 34 | pthread_barrier_t barrier; |
| 35 | }; |
| 36 | |
| 37 | char buffer[512]; |
| 38 | char expected[512]; |
| 39 | |
| 40 | static void |
| 41 | test_msg_callback(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg) |
| 42 | { |
| 43 | (void) level; |
| 44 | (void) session; |
| 45 | |
| 46 | if (strstr(msg, expected)) { |
| 47 | strcpy(buffer, msg); |
| 48 | } |
| 49 | |
| 50 | printf("%s\n", msg); |
| 51 | } |
| 52 | |
| 53 | static void * |
| 54 | server_thread(void *arg) |
| 55 | { |
| 56 | NC_MSG_TYPE msgtype; |
| 57 | struct nc_session *session; |
| 58 | struct test_state *state = arg; |
| 59 | |
| 60 | /* set print clb so we get access to messages */ |
| 61 | nc_set_print_clb_session(test_msg_callback); |
| 62 | buffer[0] = '\0'; |
| 63 | strcpy(expected, "revoked per CRL"); |
| 64 | |
| 65 | /* accept a session and add it to the poll session structure */ |
| 66 | pthread_barrier_wait(&state->barrier); |
| 67 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 68 | assert_int_equal(msgtype, NC_MSG_ERROR); |
| 69 | |
| 70 | assert_int_not_equal(strlen(buffer), 0); |
| 71 | |
| 72 | nc_session_free(session, NULL); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | static void * |
| 77 | client_thread(void *arg) |
| 78 | { |
| 79 | int ret; |
| 80 | struct nc_session *session = NULL; |
| 81 | struct test_state *state = arg; |
| 82 | |
| 83 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 84 | assert_int_equal(ret, 0); |
| 85 | |
| 86 | /* set client cert */ |
| 87 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
| 88 | assert_int_equal(ret, 0); |
| 89 | |
| 90 | /* set client ca */ |
| 91 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
| 92 | assert_int_equal(ret, 0); |
| 93 | |
| 94 | pthread_barrier_wait(&state->barrier); |
| 95 | session = nc_connect_tls("127.0.0.1", 10005, NULL); |
| 96 | |
| 97 | nc_session_free(session, NULL); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | test_nc_tls(void **state) |
| 103 | { |
| 104 | int ret, i; |
| 105 | pthread_t tids[2]; |
| 106 | |
| 107 | assert_non_null(state); |
| 108 | |
| 109 | ret = pthread_create(&tids[0], NULL, client_thread, *state); |
| 110 | assert_int_equal(ret, 0); |
| 111 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 112 | assert_int_equal(ret, 0); |
| 113 | |
| 114 | for (i = 0; i < 2; i++) { |
| 115 | pthread_join(tids[i], NULL); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | setup_f(void **state) |
| 121 | { |
| 122 | int ret; |
| 123 | struct lyd_node *tree = NULL; |
| 124 | struct test_state *test_state; |
| 125 | |
| 126 | nc_verbosity(NC_VERB_VERBOSE); |
| 127 | |
| 128 | /* init barrier */ |
| 129 | test_state = malloc(sizeof *test_state); |
| 130 | assert_non_null(test_state); |
| 131 | |
| 132 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 133 | assert_int_equal(ret, 0); |
| 134 | |
| 135 | *state = test_state; |
| 136 | |
| 137 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 138 | assert_int_equal(ret, 0); |
| 139 | |
| 140 | ret = nc_server_init_ctx(&ctx); |
| 141 | assert_int_equal(ret, 0); |
| 142 | |
| 143 | ret = nc_server_config_load_modules(&ctx); |
| 144 | assert_int_equal(ret, 0); |
| 145 | |
| 146 | /* create new address and port data */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 147 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_OPENSSL, "127.0.0.1", 10005, &tree); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 148 | assert_int_equal(ret, 0); |
| 149 | |
| 150 | /* create new server certificate data */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 151 | ret = nc_server_config_add_tls_server_cert(ctx, "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 152 | assert_int_equal(ret, 0); |
| 153 | |
| 154 | /* create new end entity client cert data */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 155 | ret = nc_server_config_add_tls_client_cert(ctx, "endpt", "client_cert", TESTS_DIR "/data/client.crt", &tree); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 156 | assert_int_equal(ret, 0); |
| 157 | |
| 158 | /* create new client ca data */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 159 | ret = nc_server_config_add_tls_ca_cert(ctx, "endpt", "client_ca", TESTS_DIR "/data/serverca.pem", &tree); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 160 | assert_int_equal(ret, 0); |
| 161 | |
| 162 | /* create new cert-to-name */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 163 | ret = nc_server_config_add_tls_ctn(ctx, "endpt", 1, |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 164 | "04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D", |
| 165 | NC_TLS_CTN_SPECIFIED, "client", &tree); |
| 166 | assert_int_equal(ret, 0); |
| 167 | |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 168 | /* set path to a CRL file */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame^] | 169 | ret = lyd_new_path(tree, ctx, "/ietf-netconf-server:netconf-server/listen/endpoint[name='endpt']/tls/tls-server-parameters/" |
| 170 | "client-authentication/libnetconf2-netconf-server:crl-path", TESTS_DIR "/data/crl.pem", 0, NULL); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 171 | assert_int_equal(ret, 0); |
| 172 | |
| 173 | /* configure the server based on the data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 174 | ret = nc_server_config_setup_data(tree); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 175 | assert_int_equal(ret, 0); |
| 176 | |
| 177 | ret = nc_server_init(); |
| 178 | assert_int_equal(ret, 0); |
| 179 | |
roman | eadc478 | 2023-09-14 10:10:08 +0200 | [diff] [blame] | 180 | /* initialize client */ |
| 181 | ret = nc_client_init(); |
| 182 | assert_int_equal(ret, 0); |
| 183 | |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 184 | lyd_free_all(tree); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | teardown_f(void **state) |
| 191 | { |
| 192 | int ret = 0; |
| 193 | struct test_state *test_state; |
| 194 | |
| 195 | assert_non_null(state); |
| 196 | test_state = *state; |
| 197 | |
| 198 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 199 | assert_int_equal(ret, 0); |
| 200 | |
| 201 | free(*state); |
| 202 | nc_client_destroy(); |
| 203 | nc_server_destroy(); |
| 204 | ly_ctx_destroy(ctx); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | main(void) |
| 211 | { |
| 212 | const struct CMUnitTest tests[] = { |
| 213 | cmocka_unit_test_setup_teardown(test_nc_tls, setup_f, teardown_f), |
| 214 | }; |
| 215 | |
| 216 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 217 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 218 | } |