blob: 8c6aa7975fc775a169e5453b35f77ac4f1610dd1 [file] [log] [blame]
roman45cec4e2023-02-17 10:21:39 +01001/**
roman142718b2023-06-29 09:15:29 +02002 * @file test_ks_ts.c
roman2ca058f2024-08-14 10:35:36 +02003 * @author Roman Janota <janota@cesnet.cz>
roman142718b2023-06-29 09:15:29 +02004 * @brief libnetconf2 Keystore and trustore usage test.
roman45cec4e2023-02-17 10:21:39 +01005 *
6 * @copyright
roman2ca058f2024-08-14 10:35:36 +02007 * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
roman45cec4e2023-02-17 10:21:39 +01008 *
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
roman9b1379c2023-03-31 10:11:10 +020016#define _GNU_SOURCE
17
roman45cec4e2023-02-17 10:21:39 +010018#include <errno.h>
19#include <pthread.h>
20#include <setjmp.h>
roman9b1379c2023-03-31 10:11:10 +020021#include <stdarg.h>
roman45cec4e2023-02-17 10:21:39 +010022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cmocka.h>
27
romanabe69902024-08-13 14:43:49 +020028#include "ln2_test.h"
roman45cec4e2023-02-17 10:21:39 +010029
romanabe69902024-08-13 14:43:49 +020030int TEST_PORT = 10050;
31const char *TEST_PORT_STR = "10050";
32
roman45cec4e2023-02-17 10:21:39 +010033static void *
roman12c3d522023-07-26 13:39:30 +020034client_thread_ssh(void *arg)
roman45cec4e2023-02-17 10:21:39 +010035{
36 int ret;
37 struct nc_session *session = NULL;
roman2ca058f2024-08-14 10:35:36 +020038 struct ln2_test_ctx *test_ctx = arg;
roman45cec4e2023-02-17 10:21:39 +010039
roman472420c2023-04-24 16:28:09 +020040 /* skip all hostkey and known_hosts checks */
41 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
42
roman45cec4e2023-02-17 10:21:39 +010043 ret = nc_client_set_schema_searchpath(MODULES_DIR);
44 assert_int_equal(ret, 0);
45
roman142718b2023-06-29 09:15:29 +020046 ret = nc_client_ssh_set_username("client");
roman45cec4e2023-02-17 10:21:39 +010047 assert_int_equal(ret, 0);
48
roman142718b2023-06-29 09:15:29 +020049 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519");
roman45cec4e2023-02-17 10:21:39 +010050 assert_int_equal(ret, 0);
51
roman2ca058f2024-08-14 10:35:36 +020052 pthread_barrier_wait(&test_ctx->barrier);
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020053 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
roman45cec4e2023-02-17 10:21:39 +010054 assert_non_null(session);
55
56 nc_session_free(session, NULL);
roman45cec4e2023-02-17 10:21:39 +010057 return NULL;
58}
59
60static void
roman12c3d522023-07-26 13:39:30 +020061test_nc_ks_ts_ssh(void **state)
roman45cec4e2023-02-17 10:21:39 +010062{
63 int ret, i;
64 pthread_t tids[2];
65
66 assert_non_null(state);
67
roman12c3d522023-07-26 13:39:30 +020068 ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state);
roman45cec4e2023-02-17 10:21:39 +010069 assert_int_equal(ret, 0);
roman2ca058f2024-08-14 10:35:36 +020070 ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
roman45cec4e2023-02-17 10:21:39 +010071 assert_int_equal(ret, 0);
72
73 for (i = 0; i < 2; i++) {
74 pthread_join(tids[i], NULL);
75 }
76}
77
78static int
roman12c3d522023-07-26 13:39:30 +020079setup_ssh(void **state)
roman45cec4e2023-02-17 10:21:39 +010080{
81 int ret;
roman142718b2023-06-29 09:15:29 +020082 struct lyd_node *tree = NULL;
roman2ca058f2024-08-14 10:35:36 +020083 struct ln2_test_ctx *test_ctx;
roman45cec4e2023-02-17 10:21:39 +010084
roman2ca058f2024-08-14 10:35:36 +020085 ret = ln2_glob_test_setup(&test_ctx);
roman45cec4e2023-02-17 10:21:39 +010086 assert_int_equal(ret, 0);
87
roman2ca058f2024-08-14 10:35:36 +020088 *state = test_ctx;
roman45cec4e2023-02-17 10:21:39 +010089
roman2ca058f2024-08-14 10:35:36 +020090 ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
roman45cec4e2023-02-17 10:21:39 +010091 assert_int_equal(ret, 0);
92
roman2ca058f2024-08-14 10:35:36 +020093 ret = nc_server_config_add_ssh_keystore_ref(test_ctx->ctx, "endpt", "hostkey", "test_keystore", &tree);
roman45cec4e2023-02-17 10:21:39 +010094 assert_int_equal(ret, 0);
95
roman2ca058f2024-08-14 10:35:36 +020096 ret = nc_server_config_add_ssh_truststore_ref(test_ctx->ctx, "endpt", "client", "test_truststore", &tree);
roman45cec4e2023-02-17 10:21:39 +010097 assert_int_equal(ret, 0);
98
roman2ca058f2024-08-14 10:35:36 +020099 ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_SSH, "test_keystore", TESTS_DIR "/data/key_rsa", NULL, &tree);
roman142718b2023-06-29 09:15:29 +0200100 assert_int_equal(ret, 0);
101
roman2ca058f2024-08-14 10:35:36 +0200102 ret = nc_server_config_add_truststore_pubkey(test_ctx->ctx, "test_truststore", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
roman45cec4e2023-02-17 10:21:39 +0100103 assert_int_equal(ret, 0);
104
105 /* configure the server based on the data */
roman142718b2023-06-29 09:15:29 +0200106 ret = nc_server_config_setup_data(tree);
roman45cec4e2023-02-17 10:21:39 +0100107 assert_int_equal(ret, 0);
108
roman45cec4e2023-02-17 10:21:39 +0100109 lyd_free_all(tree);
110
111 return 0;
112}
113
roman12c3d522023-07-26 13:39:30 +0200114static void *
115client_thread_tls(void *arg)
116{
117 int ret;
118 struct nc_session *session = NULL;
roman2ca058f2024-08-14 10:35:36 +0200119 struct ln2_test_ctx *test_ctx = arg;
roman12c3d522023-07-26 13:39:30 +0200120
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
roman2ca058f2024-08-14 10:35:36 +0200132 pthread_barrier_wait(&test_ctx->barrier);
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +0200133 session = nc_connect_tls("127.0.0.1", TEST_PORT, NULL);
roman12c3d522023-07-26 13:39:30 +0200134 assert_non_null(session);
135
136 nc_session_free(session, NULL);
137 return NULL;
138}
139
140static void
141test_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);
roman2ca058f2024-08-14 10:35:36 +0200150 ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
roman12c3d522023-07-26 13:39:30 +0200151 assert_int_equal(ret, 0);
152
153 for (i = 0; i < 2; i++) {
154 pthread_join(tids[i], NULL);
155 }
156}
157
158static int
159setup_tls(void **state)
160{
161 int ret;
162 struct lyd_node *tree = NULL;
roman2ca058f2024-08-14 10:35:36 +0200163 struct ln2_test_ctx *test_ctx;
roman12c3d522023-07-26 13:39:30 +0200164
roman2ca058f2024-08-14 10:35:36 +0200165 ret = ln2_glob_test_setup(&test_ctx);
roman12c3d522023-07-26 13:39:30 +0200166 assert_int_equal(ret, 0);
167
roman2ca058f2024-08-14 10:35:36 +0200168 *state = test_ctx;
roman12c3d522023-07-26 13:39:30 +0200169
170 /* new tls bind */
roman2ca058f2024-08-14 10:35:36 +0200171 ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree);
roman12c3d522023-07-26 13:39:30 +0200172 assert_int_equal(ret, 0);
173
174 /* new keystore asym key pair */
roman2ca058f2024-08-14 10:35:36 +0200175 ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_TLS, "server_key", TESTS_DIR "/data/server.key", NULL, &tree);
roman12c3d522023-07-26 13:39:30 +0200176 assert_int_equal(ret, 0);
177
178 /* new keystore cert belonging to the key pair */
roman2ca058f2024-08-14 10:35:36 +0200179 ret = nc_server_config_add_keystore_cert(test_ctx->ctx, "server_key", "server_cert", TESTS_DIR "/data/server.crt", &tree);
roman12c3d522023-07-26 13:39:30 +0200180 assert_int_equal(ret, 0);
181
182 /* new truststore client cert */
roman2ca058f2024-08-14 10:35:36 +0200183 ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ee_cert_bag", "ee_cert", TESTS_DIR "/data/client.crt", &tree);
roman12c3d522023-07-26 13:39:30 +0200184 assert_int_equal(ret, 0);
185
186 /* new truststore client CA cert */
roman2ca058f2024-08-14 10:35:36 +0200187 ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ca_cert_bag", "ca_cert", TESTS_DIR "/data/serverca.pem", &tree);
roman12c3d522023-07-26 13:39:30 +0200188 assert_int_equal(ret, 0);
189
190 /* new keystore ref for the TLS server cert */
roman2ca058f2024-08-14 10:35:36 +0200191 ret = nc_server_config_add_tls_keystore_ref(test_ctx->ctx, "endpt", "server_key", "server_cert", &tree);
roman12c3d522023-07-26 13:39:30 +0200192 assert_int_equal(ret, 0);
193
194 /* new truststore ref for the client cert */
roman2ca058f2024-08-14 10:35:36 +0200195 ret = nc_server_config_add_tls_client_cert_truststore_ref(test_ctx->ctx, "endpt", "ee_cert_bag", &tree);
roman12c3d522023-07-26 13:39:30 +0200196 assert_int_equal(ret, 0);
197
198 /* new truststore ref for the client CA cert */
roman2ca058f2024-08-14 10:35:36 +0200199 ret = nc_server_config_add_tls_ca_cert_truststore_ref(test_ctx->ctx, "endpt", "ca_cert_bag", &tree);
roman12c3d522023-07-26 13:39:30 +0200200 assert_int_equal(ret, 0);
201
202 /* new cert-to-name */
roman2ca058f2024-08-14 10:35:36 +0200203 ret = nc_server_config_add_tls_ctn(test_ctx->ctx, "endpt", 1,
roman12c3d522023-07-26 13:39:30 +0200204 "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
roman12c3d522023-07-26 13:39:30 +0200212 lyd_free_all(tree);
213
214 return 0;
215}
216
roman45cec4e2023-02-17 10:21:39 +0100217int
218main(void)
219{
220 const struct CMUnitTest tests[] = {
roman2ca058f2024-08-14 10:35:36 +0200221 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),
roman45cec4e2023-02-17 10:21:39 +0100223 };
224
romanabe69902024-08-13 14:43:49 +0200225 /* 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
roman45cec4e2023-02-17 10:21:39 +0100230 setenv("CMOCKA_TEST_ABORT", "1", 1);
231 return cmocka_run_group_tests(tests, NULL, NULL);
232}