roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 1 | /** |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 2 | * @file test_ks_ts.c |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 4 | * @brief libnetconf2 Keystore and trustore usage test. |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 5 | * |
| 6 | * @copyright |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 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 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <pthread.h> |
| 20 | #include <setjmp.h> |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 28 | #include "tests/config.h" |
| 29 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 30 | #define NC_ACCEPT_TIMEOUT 2000 |
| 31 | #define NC_PS_POLL_TIMEOUT 2000 |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 32 | |
| 33 | struct ly_ctx *ctx; |
| 34 | |
| 35 | struct test_state { |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 36 | pthread_barrier_t barrier; |
| 37 | }; |
| 38 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 39 | static void * |
| 40 | server_thread(void *arg) |
| 41 | { |
| 42 | int ret; |
| 43 | NC_MSG_TYPE msgtype; |
| 44 | struct nc_session *session; |
| 45 | struct nc_pollsession *ps; |
| 46 | struct test_state *state = arg; |
| 47 | |
| 48 | ps = nc_ps_new(); |
| 49 | assert_non_null(ps); |
| 50 | |
| 51 | /* accept a session and add it to the poll session structure */ |
| 52 | pthread_barrier_wait(&state->barrier); |
| 53 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 54 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 55 | |
| 56 | ret = nc_ps_add_session(ps, session); |
| 57 | assert_int_equal(ret, 0); |
| 58 | |
| 59 | do { |
| 60 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 61 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 62 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 63 | |
| 64 | nc_ps_clear(ps, 1, NULL); |
| 65 | nc_ps_free(ps); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 69 | static void * |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 70 | client_thread_ssh(void *arg) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 71 | { |
| 72 | int ret; |
| 73 | struct nc_session *session = NULL; |
| 74 | struct test_state *state = arg; |
| 75 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 76 | /* skip all hostkey and known_hosts checks */ |
| 77 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 78 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 79 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 80 | assert_int_equal(ret, 0); |
| 81 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 82 | ret = nc_client_ssh_set_username("client"); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 83 | assert_int_equal(ret, 0); |
| 84 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 85 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519"); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 86 | assert_int_equal(ret, 0); |
| 87 | |
| 88 | pthread_barrier_wait(&state->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 89 | session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 90 | assert_non_null(session); |
| 91 | |
| 92 | nc_session_free(session, NULL); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static void |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 97 | test_nc_ks_ts_ssh(void **state) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 98 | { |
| 99 | int ret, i; |
| 100 | pthread_t tids[2]; |
| 101 | |
| 102 | assert_non_null(state); |
| 103 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 104 | ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 105 | assert_int_equal(ret, 0); |
| 106 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 107 | assert_int_equal(ret, 0); |
| 108 | |
| 109 | for (i = 0; i < 2; i++) { |
| 110 | pthread_join(tids[i], NULL); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static int |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 115 | setup_ssh(void **state) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 116 | { |
| 117 | int ret; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 118 | struct lyd_node *tree = NULL; |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 119 | struct test_state *test_state; |
| 120 | |
| 121 | nc_verbosity(NC_VERB_VERBOSE); |
| 122 | |
| 123 | /* init barrier */ |
| 124 | test_state = malloc(sizeof *test_state); |
| 125 | assert_non_null(test_state); |
| 126 | |
| 127 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 128 | assert_int_equal(ret, 0); |
| 129 | |
| 130 | *state = test_state; |
| 131 | |
| 132 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 133 | assert_int_equal(ret, 0); |
| 134 | |
| 135 | ret = nc_server_init_ctx(&ctx); |
| 136 | assert_int_equal(ret, 0); |
| 137 | |
| 138 | ret = nc_server_config_load_modules(&ctx); |
| 139 | assert_int_equal(ret, 0); |
| 140 | |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 141 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 142 | assert_int_equal(ret, 0); |
| 143 | |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 144 | ret = nc_server_config_add_ssh_keystore_ref(ctx, "endpt", "hostkey", "test_keystore", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 145 | assert_int_equal(ret, 0); |
| 146 | |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 147 | ret = nc_server_config_add_ssh_truststore_ref(ctx, "endpt", "client", "test_truststore", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 148 | assert_int_equal(ret, 0); |
| 149 | |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 150 | ret = nc_server_config_add_keystore_asym_key(ctx, NC_TI_SSH, "test_keystore", TESTS_DIR "/data/key_rsa", NULL, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 151 | assert_int_equal(ret, 0); |
| 152 | |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 153 | ret = nc_server_config_add_truststore_pubkey(ctx, "test_truststore", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 154 | assert_int_equal(ret, 0); |
| 155 | |
| 156 | /* configure the server based on the data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 157 | ret = nc_server_config_setup_data(tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 158 | assert_int_equal(ret, 0); |
| 159 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 160 | ret = nc_server_init(); |
| 161 | assert_int_equal(ret, 0); |
| 162 | |
roman | eadc478 | 2023-09-14 10:10:08 +0200 | [diff] [blame] | 163 | /* initialize client */ |
| 164 | ret = nc_client_init(); |
| 165 | assert_int_equal(ret, 0); |
| 166 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 167 | lyd_free_all(tree); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 172 | static void * |
| 173 | client_thread_tls(void *arg) |
| 174 | { |
| 175 | int ret; |
| 176 | struct nc_session *session = NULL; |
| 177 | struct test_state *state = arg; |
| 178 | |
| 179 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 180 | assert_int_equal(ret, 0); |
| 181 | |
| 182 | /* set client cert */ |
| 183 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
| 184 | assert_int_equal(ret, 0); |
| 185 | |
| 186 | /* set client ca */ |
| 187 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
| 188 | assert_int_equal(ret, 0); |
| 189 | |
| 190 | pthread_barrier_wait(&state->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 191 | session = nc_connect_tls("127.0.0.1", TEST_PORT, NULL); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 192 | assert_non_null(session); |
| 193 | |
| 194 | nc_session_free(session, NULL); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | test_nc_ks_ts_tls(void **state) |
| 200 | { |
| 201 | int ret, i; |
| 202 | pthread_t tids[2]; |
| 203 | |
| 204 | assert_non_null(state); |
| 205 | |
| 206 | ret = pthread_create(&tids[0], NULL, client_thread_tls, *state); |
| 207 | assert_int_equal(ret, 0); |
| 208 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 209 | assert_int_equal(ret, 0); |
| 210 | |
| 211 | for (i = 0; i < 2; i++) { |
| 212 | pthread_join(tids[i], NULL); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static int |
| 217 | setup_tls(void **state) |
| 218 | { |
| 219 | int ret; |
| 220 | struct lyd_node *tree = NULL; |
| 221 | struct test_state *test_state; |
| 222 | |
| 223 | nc_verbosity(NC_VERB_VERBOSE); |
| 224 | |
| 225 | /* init barrier */ |
| 226 | test_state = malloc(sizeof *test_state); |
| 227 | assert_non_null(test_state); |
| 228 | |
| 229 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 230 | assert_int_equal(ret, 0); |
| 231 | |
| 232 | *state = test_state; |
| 233 | |
| 234 | /* new ctx */ |
| 235 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 236 | assert_int_equal(ret, 0); |
| 237 | |
| 238 | /* init ctx */ |
| 239 | ret = nc_server_init_ctx(&ctx); |
| 240 | assert_int_equal(ret, 0); |
| 241 | |
| 242 | /* load ietf netconf server module and its requisities */ |
| 243 | ret = nc_server_config_load_modules(&ctx); |
| 244 | assert_int_equal(ret, 0); |
| 245 | |
| 246 | /* new tls bind */ |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 247 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 248 | assert_int_equal(ret, 0); |
| 249 | |
| 250 | /* new keystore asym key pair */ |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 251 | ret = nc_server_config_add_keystore_asym_key(ctx, NC_TI_TLS, "server_key", TESTS_DIR "/data/server.key", NULL, &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 252 | assert_int_equal(ret, 0); |
| 253 | |
| 254 | /* new keystore cert belonging to the key pair */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 255 | ret = nc_server_config_add_keystore_cert(ctx, "server_key", "server_cert", TESTS_DIR "/data/server.crt", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 256 | assert_int_equal(ret, 0); |
| 257 | |
| 258 | /* new truststore client cert */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 259 | ret = nc_server_config_add_truststore_cert(ctx, "ee_cert_bag", "ee_cert", TESTS_DIR "/data/client.crt", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 260 | assert_int_equal(ret, 0); |
| 261 | |
| 262 | /* new truststore client CA cert */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 263 | ret = nc_server_config_add_truststore_cert(ctx, "ca_cert_bag", "ca_cert", TESTS_DIR "/data/serverca.pem", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 264 | assert_int_equal(ret, 0); |
| 265 | |
| 266 | /* new keystore ref for the TLS server cert */ |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 267 | ret = nc_server_config_add_tls_keystore_ref(ctx, "endpt", "server_key", "server_cert", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 268 | assert_int_equal(ret, 0); |
| 269 | |
| 270 | /* new truststore ref for the client cert */ |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 271 | ret = nc_server_config_add_tls_client_cert_truststore_ref(ctx, "endpt", "ee_cert_bag", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 272 | assert_int_equal(ret, 0); |
| 273 | |
| 274 | /* new truststore ref for the client CA cert */ |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame] | 275 | ret = nc_server_config_add_tls_ca_cert_truststore_ref(ctx, "endpt", "ca_cert_bag", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 276 | assert_int_equal(ret, 0); |
| 277 | |
| 278 | /* new cert-to-name */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 279 | ret = nc_server_config_add_tls_ctn(ctx, "endpt", 1, |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 280 | "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", |
| 281 | NC_TLS_CTN_SPECIFIED, "client", &tree); |
| 282 | assert_int_equal(ret, 0); |
| 283 | |
| 284 | /* configure the server based on the data */ |
| 285 | ret = nc_server_config_setup_data(tree); |
| 286 | assert_int_equal(ret, 0); |
| 287 | |
| 288 | ret = nc_server_init(); |
| 289 | assert_int_equal(ret, 0); |
| 290 | |
roman | eadc478 | 2023-09-14 10:10:08 +0200 | [diff] [blame] | 291 | /* initialize client */ |
| 292 | ret = nc_client_init(); |
| 293 | assert_int_equal(ret, 0); |
| 294 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 295 | lyd_free_all(tree); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 300 | static int |
| 301 | teardown_f(void **state) |
| 302 | { |
| 303 | int ret = 0; |
| 304 | struct test_state *test_state; |
| 305 | |
| 306 | assert_non_null(state); |
| 307 | test_state = *state; |
| 308 | |
| 309 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 310 | assert_int_equal(ret, 0); |
| 311 | |
| 312 | free(*state); |
| 313 | nc_client_destroy(); |
| 314 | nc_server_destroy(); |
| 315 | ly_ctx_destroy(ctx); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | int |
| 321 | main(void) |
| 322 | { |
| 323 | const struct CMUnitTest tests[] = { |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 324 | cmocka_unit_test_setup_teardown(test_nc_ks_ts_ssh, setup_ssh, teardown_f), |
| 325 | cmocka_unit_test_setup_teardown(test_nc_ks_ts_tls, setup_tls, teardown_f), |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 329 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 330 | } |