Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_client_tls.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libnetconf2 - TLS specific session client transport functions |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 7 | * This source is compiled only with libssl. |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 8 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 9 | * @copyright |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 10 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 13 | * You may not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 15 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 16 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 19 | #define _GNU_SOURCE /* pthread_rwlock_t, strdup */ |
| 20 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <errno.h> |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 23 | #include <signal.h> |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <libyang/libyang.h> |
| 28 | |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 29 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | #include "log_p.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 32 | #include "session_client.h" |
| 33 | #include "session_client_ch.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 34 | #include "session_p.h" |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 35 | #include "session_wrapper.h" |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 36 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 37 | struct nc_client_context *nc_client_context_location(void); |
| 38 | |
| 39 | #define client_opts nc_client_context_location()->opts |
| 40 | #define tls_opts nc_client_context_location()->tls_opts |
| 41 | #define tls_ch_opts nc_client_context_location()->tls_ch_opts |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 42 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 43 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 44 | _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts) |
| 45 | { |
| 46 | free(opts->cert_path); |
| 47 | free(opts->key_path); |
| 48 | free(opts->ca_file); |
| 49 | free(opts->ca_dir); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 50 | free(opts->crl_file); |
| 51 | free(opts->crl_dir); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 52 | memset(opts, 0, sizeof *opts); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 55 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 56 | nc_client_tls_destroy_opts(void) |
| 57 | { |
| 58 | _nc_client_tls_destroy_opts(&tls_opts); |
| 59 | _nc_client_tls_destroy_opts(&tls_ch_opts); |
| 60 | } |
| 61 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 62 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 63 | _nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key, struct nc_client_tls_opts *opts) |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 64 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 65 | NC_CHECK_ARG_RET(NULL, client_cert, -1); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 66 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 67 | free(opts->cert_path); |
| 68 | free(opts->key_path); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 69 | |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 70 | opts->cert_path = strdup(client_cert); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 71 | NC_CHECK_ERRMEM_RET(!opts->cert_path, -1); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 72 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 73 | if (client_key) { |
| 74 | opts->key_path = strdup(client_key); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 75 | NC_CHECK_ERRMEM_RET(!opts->key_path, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 76 | } else { |
| 77 | opts->key_path = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 80 | return 0; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 83 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 84 | nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key) |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 85 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 86 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 90 | nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 91 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 92 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | _nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts) |
| 97 | { |
| 98 | if (!client_cert && !client_key) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 99 | ERRARG(NULL, "client_cert and client_key"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (client_cert) { |
| 104 | *client_cert = opts->cert_path; |
| 105 | } |
| 106 | if (client_key) { |
| 107 | *client_key = opts->key_path; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | API void |
| 112 | nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 113 | { |
| 114 | _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts); |
| 115 | } |
| 116 | |
| 117 | API void |
| 118 | nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 119 | { |
| 120 | _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 124 | _nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_client_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 125 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 126 | if (!ca_file && !ca_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 127 | ERRARG(NULL, "ca_file and ca_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 128 | return -1; |
| 129 | } |
| 130 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 131 | free(opts->ca_file); |
| 132 | free(opts->ca_dir); |
| 133 | |
| 134 | if (ca_file) { |
| 135 | opts->ca_file = strdup(ca_file); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 136 | NC_CHECK_ERRMEM_RET(!opts->ca_file, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 137 | } else { |
| 138 | opts->ca_file = NULL; |
| 139 | } |
| 140 | |
| 141 | if (ca_dir) { |
| 142 | opts->ca_dir = strdup(ca_dir); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 143 | NC_CHECK_ERRMEM_RET(!opts->ca_dir, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 144 | } else { |
| 145 | opts->ca_dir = NULL; |
| 146 | } |
| 147 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 152 | nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 153 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 154 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 158 | nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 159 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 160 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | _nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts) |
| 165 | { |
| 166 | if (!ca_file && !ca_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 167 | ERRARG(NULL, "ca_file and ca_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (ca_file) { |
| 172 | *ca_file = opts->ca_file; |
| 173 | } |
| 174 | if (ca_dir) { |
| 175 | *ca_dir = opts->ca_dir; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | API void |
| 180 | nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 181 | { |
| 182 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
| 183 | } |
| 184 | |
| 185 | API void |
| 186 | nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 187 | { |
| 188 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 192 | _nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir, struct nc_client_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 193 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 194 | if (!crl_file && !crl_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 195 | ERRARG(NULL, "crl_file and crl_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 199 | free(opts->crl_file); |
| 200 | free(opts->crl_dir); |
| 201 | |
| 202 | if (crl_file) { |
| 203 | opts->crl_file = strdup(crl_file); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 204 | NC_CHECK_ERRMEM_RET(!opts->crl_file, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 205 | } else { |
| 206 | opts->crl_file = NULL; |
| 207 | } |
| 208 | |
| 209 | if (crl_dir) { |
| 210 | opts->crl_dir = strdup(crl_dir); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 211 | NC_CHECK_ERRMEM_RET(!opts->crl_dir, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 212 | } else { |
| 213 | opts->crl_dir = NULL; |
| 214 | } |
| 215 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 220 | nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 221 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 222 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 226 | nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 227 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 228 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | _nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts) |
| 233 | { |
| 234 | if (!crl_file && !crl_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 235 | ERRARG(NULL, "crl_file and crl_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | if (crl_file) { |
| 240 | *crl_file = opts->crl_file; |
| 241 | } |
| 242 | if (crl_dir) { |
| 243 | *crl_dir = opts->crl_dir; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | API void |
| 248 | nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 249 | { |
| 250 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts); |
| 251 | } |
| 252 | |
| 253 | API void |
| 254 | nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 255 | { |
| 256 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | API int |
| 260 | nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port) |
| 261 | { |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 262 | return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_TLS); |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | API int |
| 266 | nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname) |
| 267 | { |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 268 | return nc_client_ch_add_bind_listen(address, port, hostname, NC_TI_TLS); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | API int |
| 272 | nc_client_tls_ch_del_bind(const char *address, uint16_t port) |
| 273 | { |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 274 | return nc_client_ch_del_bind(address, port, NC_TI_TLS); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 277 | static int |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 278 | nc_client_tls_connect_check(int connect_ret, void *tls_session, const char *peername) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 279 | { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 280 | uint32_t verify; |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 281 | char *err; |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 282 | |
| 283 | /* check certificate verification result */ |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 284 | verify = nc_tls_get_verify_result_wrap(tls_session); |
roman | 008cfe7 | 2024-04-05 12:36:18 +0200 | [diff] [blame] | 285 | if (!verify && (connect_ret == 1)) { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 286 | VRB(NULL, "Server certificate verified (domain \"%s\").", peername); |
| 287 | } else if (verify) { |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 288 | err = nc_tls_verify_error_string_wrap(verify); |
| 289 | ERR(NULL, "Server certificate error (%s).", err); |
| 290 | free(err); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* check TLS connection result */ |
| 294 | if (connect_ret != 1) { |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 295 | nc_client_tls_print_connect_err_wrap(connect_ret, peername, tls_session); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return connect_ret; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 299 | } |
| 300 | |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 301 | static void * |
| 302 | nc_client_tls_session_new(int sock, const char *host, int timeout, struct nc_client_tls_opts *opts, void **out_tls_cfg, struct nc_tls_ctx *tls_ctx) |
| 303 | { |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 304 | int ret = 0, sock_tmp = sock; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 305 | struct timespec ts_timeout; |
| 306 | void *tls_session, *tls_cfg, *cli_cert, *cli_pkey, *cert_store, *crl_store; |
| 307 | |
| 308 | tls_session = tls_cfg = cli_cert = cli_pkey = cert_store = crl_store = NULL; |
| 309 | |
| 310 | /* prepare TLS context from which a session will be created */ |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 311 | tls_cfg = nc_tls_config_new_wrap(NC_CLIENT); |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 312 | if (!tls_cfg) { |
| 313 | goto fail; |
| 314 | } |
| 315 | |
| 316 | /* opaque CA/CRL certificate store */ |
| 317 | cert_store = nc_tls_cert_store_new_wrap(); |
| 318 | if (!cert_store) { |
| 319 | goto fail; |
| 320 | } |
| 321 | |
| 322 | /* load client's key and certificate */ |
| 323 | if (nc_client_tls_load_cert_key_wrap(opts->cert_path, opts->key_path, &cli_cert, &cli_pkey)) { |
| 324 | goto fail; |
| 325 | } |
| 326 | |
| 327 | /* load trusted CA certificates */ |
| 328 | if (nc_client_tls_load_trusted_certs_wrap(cert_store, opts->ca_file, opts->ca_dir)) { |
| 329 | goto fail; |
| 330 | } |
| 331 | |
| 332 | if (opts->crl_file || opts->crl_dir) { |
| 333 | /* opaque CRL store */ |
| 334 | crl_store = nc_tls_crl_store_new_wrap(); |
| 335 | if (!crl_store) { |
| 336 | goto fail; |
| 337 | } |
| 338 | |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 339 | /* load CRLs into the crl store */ |
| 340 | if (nc_client_tls_load_crl_wrap(crl_store, opts->crl_file, opts->crl_dir)) { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 341 | goto fail; |
| 342 | } |
| 343 | } |
| 344 | |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 345 | /* set client's verify mode flags */ |
| 346 | nc_client_tls_set_verify_wrap(tls_cfg); |
| 347 | |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 348 | /* init TLS context and store data which may be needed later in it */ |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 349 | if (nc_tls_init_ctx_wrap(sock, cli_cert, cli_pkey, cert_store, crl_store, tls_ctx)) { |
| 350 | goto fail; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* memory is managed by context now */ |
| 354 | cli_cert = cli_pkey = cert_store = crl_store = NULL; |
| 355 | |
| 356 | /* setup config from ctx */ |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 357 | if (nc_tls_setup_config_from_ctx_wrap(tls_ctx, NC_CLIENT, tls_cfg)) { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 358 | goto fail; |
| 359 | } |
| 360 | |
| 361 | /* session from config */ |
| 362 | tls_session = nc_tls_session_new_wrap(tls_cfg); |
| 363 | if (!tls_session) { |
| 364 | goto fail; |
| 365 | } |
| 366 | |
| 367 | /* set session fd */ |
| 368 | nc_server_tls_set_fd_wrap(tls_session, sock, tls_ctx); |
| 369 | |
| 370 | sock = -1; |
| 371 | |
| 372 | /* set session hostname to check against in the server cert */ |
| 373 | if (nc_client_tls_set_hostname_wrap(tls_session, host)) { |
| 374 | goto fail; |
| 375 | } |
| 376 | |
| 377 | /* handshake */ |
| 378 | if (timeout > -1) { |
| 379 | nc_timeouttime_get(&ts_timeout, timeout); |
| 380 | } |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 381 | while ((ret = nc_client_tls_handshake_step_wrap(tls_session, sock_tmp)) == 0) { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 382 | usleep(NC_TIMEOUT_STEP); |
| 383 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
| 384 | ERR(NULL, "SSL connect timeout."); |
| 385 | goto fail; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* check if handshake was ok */ |
| 390 | if (nc_client_tls_connect_check(ret, tls_session, host) != 1) { |
| 391 | goto fail; |
| 392 | } |
| 393 | |
| 394 | *out_tls_cfg = tls_cfg; |
| 395 | return tls_session; |
| 396 | |
| 397 | fail: |
| 398 | if (sock > -1) { |
| 399 | close(sock); |
| 400 | } |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 401 | |
| 402 | nc_tls_session_destroy_wrap(tls_session); |
| 403 | nc_tls_cert_destroy_wrap(cli_cert); |
| 404 | nc_tls_privkey_destroy_wrap(cli_pkey); |
| 405 | nc_tls_cert_store_destroy_wrap(cert_store); |
| 406 | nc_tls_crl_store_destroy_wrap(crl_store); |
| 407 | nc_tls_config_destroy_wrap(tls_cfg); |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 408 | return NULL; |
| 409 | } |
| 410 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 411 | API struct nc_session * |
Michal Vasko | 9bee18d | 2015-12-08 14:41:42 +0100 | [diff] [blame] | 412 | nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx) |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 413 | { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 414 | struct nc_session *session = NULL; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 415 | int sock; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 416 | char *ip_host = NULL; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 417 | void *tls_cfg = NULL; |
| 418 | struct nc_tls_ctx tls_ctx = {0}; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 419 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 420 | if (!tls_opts.cert_path) { |
| 421 | ERR(NULL, "Client certificate not set."); |
| 422 | return NULL; |
| 423 | } else if (!tls_opts.ca_file && !tls_opts.ca_dir) { |
| 424 | ERR(NULL, "Certificate authority certificates not set."); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 427 | |
| 428 | /* process parameters */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 429 | if (!host || (host[0] == '\0')) { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 430 | host = "localhost"; |
| 431 | } |
| 432 | |
| 433 | if (!port) { |
| 434 | port = NC_PORT_TLS; |
| 435 | } |
| 436 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 437 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 438 | session = nc_new_session(NC_CLIENT, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 439 | NC_CHECK_ERRMEM_RET(!session, NULL); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 440 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 441 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 442 | /* create and assign socket */ |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 443 | sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 444 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 445 | ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno)); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 446 | goto fail; |
| 447 | } |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 448 | |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 449 | /* fill the session */ |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 450 | session->ti_type = NC_TI_TLS; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 451 | if (!(session->ti.tls.session = nc_client_tls_session_new(sock, host, NC_TRANSPORT_TIMEOUT, &tls_opts, &tls_cfg, &tls_ctx))) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 452 | goto fail; |
| 453 | } |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 454 | session->ti.tls.config = tls_cfg; |
| 455 | |
| 456 | /* memory belongs to session */ |
| 457 | memcpy(&session->ti.tls.ctx, &tls_ctx, sizeof tls_ctx); |
| 458 | memset(&tls_ctx, 0, sizeof tls_ctx); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 459 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 460 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 461 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 462 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 463 | ctx = session->ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 464 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 465 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 466 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 467 | goto fail; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 468 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 469 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 470 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 471 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 472 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 473 | } |
| 474 | |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 475 | /* store information into session */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 476 | session->host = ip_host; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 477 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 478 | session->username = strdup("certificate-based"); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 479 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 480 | return session; |
| 481 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 482 | fail: |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 483 | free(ip_host); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 484 | nc_session_free(session, NULL); |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 485 | nc_tls_ctx_destroy_wrap(&tls_ctx); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | API struct nc_session * |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 490 | nc_connect_libssl(void *UNUSED(tls), struct ly_ctx *UNUSED(ctx)) |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 491 | { |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 492 | ERR(NULL, "nc_connect_libssl() is deprecated, do not use it."); |
| 493 | return NULL; |
| 494 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 495 | |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 496 | struct nc_session * |
| 497 | nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout, const char *peername) |
| 498 | { |
| 499 | struct nc_session *session = NULL; |
| 500 | void *tls_cfg = NULL; |
| 501 | struct nc_tls_ctx tls_ctx = {0}; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 502 | |
| 503 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 504 | session = nc_new_session(NC_CLIENT, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 505 | NC_CHECK_ERRMEM_RET(!session, NULL); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 506 | session->status = NC_STATUS_STARTING; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 507 | |
| 508 | /* fill the session */ |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 509 | session->ti_type = NC_TI_TLS; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 510 | if (!(session->ti.tls.session = nc_client_tls_session_new(sock, peername, timeout, &tls_ch_opts, &tls_cfg, &tls_ctx))) { |
| 511 | goto fail; |
| 512 | } |
| 513 | session->ti.tls.config = tls_cfg; |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 514 | |
| 515 | /* memory belongs to session */ |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 516 | memcpy(&session->ti.tls.ctx, &tls_ctx, sizeof tls_ctx); |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 517 | memset(&tls_ctx, 0, sizeof tls_ctx); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 518 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 519 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 520 | goto fail; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 521 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 522 | ctx = session->ctx; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 523 | |
| 524 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 525 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 526 | goto fail; |
| 527 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 528 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 529 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 530 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 531 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 534 | session->flags |= NC_SESSION_CALLHOME; |
| 535 | |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 536 | /* store information into session */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 537 | session->host = strdup(host); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 538 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 539 | session->username = strdup("certificate-based"); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 540 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 541 | return session; |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 542 | |
| 543 | fail: |
roman | 7731b3a | 2024-04-23 15:12:32 +0200 | [diff] [blame] | 544 | nc_session_free(session, NULL); |
| 545 | nc_tls_ctx_destroy_wrap(&tls_ctx); |
roman | 68ea7d6 | 2024-04-05 12:32:49 +0200 | [diff] [blame] | 546 | return NULL; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 547 | } |