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 | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 3 | * @author Roman Janota <janota@cesnet.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 | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 7 | * Copyright (c) 2023 - 2024 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 | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 28 | #include "ln2_test.h" |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 29 | |
roman | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 30 | int TEST_PORT = 10050; |
| 31 | const char *TEST_PORT_STR = "10050"; |
| 32 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 33 | static void * |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 34 | client_thread_ssh(void *arg) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 35 | { |
| 36 | int ret; |
| 37 | struct nc_session *session = NULL; |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 38 | struct ln2_test_ctx *test_ctx = arg; |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 39 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 40 | /* skip all hostkey and known_hosts checks */ |
| 41 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 42 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 43 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 44 | assert_int_equal(ret, 0); |
| 45 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 46 | ret = nc_client_ssh_set_username("client"); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 47 | assert_int_equal(ret, 0); |
| 48 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 49 | 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] | 50 | assert_int_equal(ret, 0); |
| 51 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 52 | pthread_barrier_wait(&test_ctx->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 53 | session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 54 | assert_non_null(session); |
| 55 | |
| 56 | nc_session_free(session, NULL); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | static void |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 61 | test_nc_ks_ts_ssh(void **state) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 62 | { |
| 63 | int ret, i; |
| 64 | pthread_t tids[2]; |
| 65 | |
| 66 | assert_non_null(state); |
| 67 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 68 | ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 69 | assert_int_equal(ret, 0); |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 70 | ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 71 | assert_int_equal(ret, 0); |
| 72 | |
| 73 | for (i = 0; i < 2; i++) { |
| 74 | pthread_join(tids[i], NULL); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 79 | setup_ssh(void **state) |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 80 | { |
| 81 | int ret; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 82 | struct lyd_node *tree = NULL; |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 83 | struct ln2_test_ctx *test_ctx; |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 84 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 85 | ret = ln2_glob_test_setup(&test_ctx); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 86 | assert_int_equal(ret, 0); |
| 87 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 88 | *state = test_ctx; |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 89 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 90 | ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 91 | assert_int_equal(ret, 0); |
| 92 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 93 | ret = nc_server_config_add_ssh_keystore_ref(test_ctx->ctx, "endpt", "hostkey", "test_keystore", &tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 94 | assert_int_equal(ret, 0); |
| 95 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 96 | ret = nc_server_config_add_ssh_truststore_ref(test_ctx->ctx, "endpt", "client", "test_truststore", &tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 97 | assert_int_equal(ret, 0); |
| 98 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 99 | ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_SSH, "test_keystore", TESTS_DIR "/data/key_rsa", NULL, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 100 | assert_int_equal(ret, 0); |
| 101 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 102 | ret = nc_server_config_add_truststore_pubkey(test_ctx->ctx, "test_truststore", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 103 | assert_int_equal(ret, 0); |
| 104 | |
| 105 | /* configure the server based on the data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 106 | ret = nc_server_config_setup_data(tree); |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 107 | assert_int_equal(ret, 0); |
| 108 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 109 | lyd_free_all(tree); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 114 | static void * |
| 115 | client_thread_tls(void *arg) |
| 116 | { |
| 117 | int ret; |
| 118 | struct nc_session *session = NULL; |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 119 | struct ln2_test_ctx *test_ctx = arg; |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 120 | |
| 121 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 122 | assert_int_equal(ret, 0); |
| 123 | |
| 124 | /* set client cert */ |
| 125 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
| 126 | assert_int_equal(ret, 0); |
| 127 | |
| 128 | /* set client ca */ |
| 129 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
| 130 | assert_int_equal(ret, 0); |
| 131 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 132 | pthread_barrier_wait(&test_ctx->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 133 | session = nc_connect_tls("127.0.0.1", TEST_PORT, NULL); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 134 | assert_non_null(session); |
| 135 | |
| 136 | nc_session_free(session, NULL); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | test_nc_ks_ts_tls(void **state) |
| 142 | { |
| 143 | int ret, i; |
| 144 | pthread_t tids[2]; |
| 145 | |
| 146 | assert_non_null(state); |
| 147 | |
| 148 | ret = pthread_create(&tids[0], NULL, client_thread_tls, *state); |
| 149 | assert_int_equal(ret, 0); |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 150 | ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 151 | assert_int_equal(ret, 0); |
| 152 | |
| 153 | for (i = 0; i < 2; i++) { |
| 154 | pthread_join(tids[i], NULL); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static int |
| 159 | setup_tls(void **state) |
| 160 | { |
| 161 | int ret; |
| 162 | struct lyd_node *tree = NULL; |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 163 | struct ln2_test_ctx *test_ctx; |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 164 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 165 | ret = ln2_glob_test_setup(&test_ctx); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 166 | assert_int_equal(ret, 0); |
| 167 | |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 168 | *state = test_ctx; |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 169 | |
| 170 | /* new tls bind */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 171 | ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 172 | assert_int_equal(ret, 0); |
| 173 | |
| 174 | /* new keystore asym key pair */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 175 | ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_TLS, "server_key", TESTS_DIR "/data/server.key", NULL, &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 176 | assert_int_equal(ret, 0); |
| 177 | |
| 178 | /* new keystore cert belonging to the key pair */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 179 | ret = nc_server_config_add_keystore_cert(test_ctx->ctx, "server_key", "server_cert", TESTS_DIR "/data/server.crt", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 180 | assert_int_equal(ret, 0); |
| 181 | |
| 182 | /* new truststore client cert */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 183 | ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ee_cert_bag", "ee_cert", TESTS_DIR "/data/client.crt", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 184 | assert_int_equal(ret, 0); |
| 185 | |
| 186 | /* new truststore client CA cert */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 187 | ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ca_cert_bag", "ca_cert", TESTS_DIR "/data/serverca.pem", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 188 | assert_int_equal(ret, 0); |
| 189 | |
| 190 | /* new keystore ref for the TLS server cert */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 191 | ret = nc_server_config_add_tls_keystore_ref(test_ctx->ctx, "endpt", "server_key", "server_cert", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 192 | assert_int_equal(ret, 0); |
| 193 | |
| 194 | /* new truststore ref for the client cert */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 195 | ret = nc_server_config_add_tls_client_cert_truststore_ref(test_ctx->ctx, "endpt", "ee_cert_bag", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 196 | assert_int_equal(ret, 0); |
| 197 | |
| 198 | /* new truststore ref for the client CA cert */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 199 | ret = nc_server_config_add_tls_ca_cert_truststore_ref(test_ctx->ctx, "endpt", "ca_cert_bag", &tree); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 200 | assert_int_equal(ret, 0); |
| 201 | |
| 202 | /* new cert-to-name */ |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 203 | ret = nc_server_config_add_tls_ctn(test_ctx->ctx, "endpt", 1, |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 204 | "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", |
| 205 | NC_TLS_CTN_SPECIFIED, "client", &tree); |
| 206 | assert_int_equal(ret, 0); |
| 207 | |
| 208 | /* configure the server based on the data */ |
| 209 | ret = nc_server_config_setup_data(tree); |
| 210 | assert_int_equal(ret, 0); |
| 211 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 212 | lyd_free_all(tree); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 217 | int |
| 218 | main(void) |
| 219 | { |
| 220 | const struct CMUnitTest tests[] = { |
roman | 2ca058f | 2024-08-14 10:35:36 +0200 | [diff] [blame] | 221 | cmocka_unit_test_setup_teardown(test_nc_ks_ts_ssh, setup_ssh, ln2_glob_test_teardown), |
| 222 | cmocka_unit_test_setup_teardown(test_nc_ks_ts_tls, setup_tls, ln2_glob_test_teardown), |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
roman | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 225 | /* try to get ports from the environment, otherwise use the default */ |
| 226 | if (ln2_glob_test_get_ports(1, &TEST_PORT, &TEST_PORT_STR)) { |
| 227 | return 1; |
| 228 | } |
| 229 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 230 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 231 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 232 | } |