roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1 | /** |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 2 | * @file server_config_util.c |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3 | * @author Roman Janota <janota@cesnet.cz> |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 4 | * @brief libnetconf2 server configuration utilities |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 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 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 18 | #include "server_config_util.h" |
| 19 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 20 | #include <libyang/libyang.h> |
roman | 5d9fc73 | 2023-10-26 11:26:57 +0200 | [diff] [blame] | 21 | |
| 22 | #include <inttypes.h> |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 28 | #ifdef NC_ENABLED_SSH_TLS |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 29 | #include <libssh/libssh.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 30 | #include <openssl/err.h> |
| 31 | #include <openssl/evp.h> |
| 32 | #include <openssl/pem.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 33 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 34 | |
| 35 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 36 | #include "log_p.h" |
| 37 | #include "session.h" |
| 38 | #include "session_p.h" |
| 39 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 40 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 41 | nc_server_config_create(const struct ly_ctx *ctx, struct lyd_node **tree, const char *value, const char *path_fmt, ...) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 42 | { |
| 43 | int ret = 0; |
| 44 | va_list ap; |
| 45 | char *path = NULL; |
| 46 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 47 | NC_CHECK_ARG_RET(NULL, ctx, tree, path_fmt, 1); |
| 48 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 49 | va_start(ap, path_fmt); |
| 50 | |
| 51 | /* create the path from the format */ |
| 52 | ret = vasprintf(&path, path_fmt, ap); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 53 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; path = NULL, cleanup); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 54 | |
| 55 | /* create the nodes in the path */ |
roman | 5ef2a57 | 2023-08-18 15:45:44 +0200 | [diff] [blame] | 56 | if (!*tree) { |
| 57 | ret = lyd_new_path(*tree, ctx, path, value, LYD_NEW_PATH_UPDATE, tree); |
| 58 | } else { |
| 59 | /* this could output NULL if no new nodes, lyd_find_path would fail then */ |
| 60 | ret = lyd_new_path(*tree, ctx, path, value, LYD_NEW_PATH_UPDATE, NULL); |
| 61 | } |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 62 | if (ret) { |
| 63 | goto cleanup; |
| 64 | } |
| 65 | |
| 66 | /* set the node to the top level node */ |
| 67 | ret = lyd_find_path(*tree, "/ietf-netconf-server:netconf-server", 0, tree); |
| 68 | if (ret) { |
| 69 | goto cleanup; |
| 70 | } |
| 71 | |
| 72 | /* add all default nodes */ |
| 73 | ret = lyd_new_implicit_tree(*tree, LYD_IMPLICIT_NO_STATE, NULL); |
| 74 | if (ret) { |
| 75 | goto cleanup; |
| 76 | } |
| 77 | |
| 78 | cleanup: |
| 79 | free(path); |
| 80 | va_end(ap); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 85 | nc_server_config_append(const struct ly_ctx *ctx, const char *parent_path, const char *child_name, |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 86 | const char *value, struct lyd_node **tree) |
| 87 | { |
| 88 | int ret = 0; |
| 89 | char *path = NULL; |
| 90 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 91 | NC_CHECK_ARG_RET(NULL, ctx, parent_path, child_name, tree, 1); |
| 92 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 93 | /* create the path by appending child to the parent path */ |
| 94 | ret = asprintf(&path, "%s/%s", parent_path, child_name); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 95 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; path = NULL, cleanup); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 96 | |
| 97 | /* create the nodes in the path */ |
roman | 5ef2a57 | 2023-08-18 15:45:44 +0200 | [diff] [blame] | 98 | if (!*tree) { |
| 99 | ret = lyd_new_path(*tree, ctx, path, value, LYD_NEW_PATH_UPDATE, tree); |
| 100 | } else { |
| 101 | /* this could output NULL if no new nodes, lyd_find_path would fail then */ |
| 102 | ret = lyd_new_path(*tree, ctx, path, value, LYD_NEW_PATH_UPDATE, NULL); |
| 103 | } |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 104 | if (ret) { |
| 105 | goto cleanup; |
| 106 | } |
| 107 | |
| 108 | /* set the node to the top level node */ |
| 109 | ret = lyd_find_path(*tree, "/ietf-netconf-server:netconf-server", 0, tree); |
| 110 | if (ret) { |
| 111 | goto cleanup; |
| 112 | } |
| 113 | |
| 114 | /* add all default nodes */ |
| 115 | ret = lyd_new_implicit_tree(*tree, LYD_IMPLICIT_NO_STATE, NULL); |
| 116 | if (ret) { |
| 117 | goto cleanup; |
| 118 | } |
| 119 | |
| 120 | cleanup: |
| 121 | free(path); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 126 | nc_server_config_delete(struct lyd_node **tree, const char *path_fmt, ...) |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 127 | { |
| 128 | int ret = 0; |
| 129 | va_list ap; |
| 130 | char *path = NULL; |
| 131 | struct lyd_node *sub = NULL; |
| 132 | |
| 133 | NC_CHECK_ARG_RET(NULL, tree, path_fmt, 1); |
| 134 | |
| 135 | va_start(ap, path_fmt); |
| 136 | |
| 137 | /* create the path from the format */ |
| 138 | ret = vasprintf(&path, path_fmt, ap); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 139 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; path = NULL, cleanup); |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 140 | |
| 141 | /* find the node we want to delete */ |
| 142 | ret = lyd_find_path(*tree, path, 0, &sub); |
| 143 | if (ret) { |
| 144 | goto cleanup; |
| 145 | } |
| 146 | |
| 147 | lyd_free_tree(sub); |
| 148 | |
| 149 | /* set the node to top level container */ |
| 150 | ret = lyd_find_path(*tree, "/ietf-netconf-server:netconf-server", 0, tree); |
| 151 | if (ret) { |
| 152 | goto cleanup; |
| 153 | } |
| 154 | |
| 155 | /* add all default nodes */ |
| 156 | ret = lyd_new_implicit_tree(*tree, LYD_IMPLICIT_NO_STATE, NULL); |
| 157 | if (ret) { |
| 158 | goto cleanup; |
| 159 | } |
| 160 | |
| 161 | cleanup: |
| 162 | free(path); |
| 163 | va_end(ap); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 168 | nc_server_config_check_delete(struct lyd_node **tree, const char *path_fmt, ...) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 169 | { |
| 170 | int ret = 0; |
| 171 | va_list ap; |
| 172 | char *path = NULL; |
| 173 | struct lyd_node *sub = NULL; |
| 174 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 175 | NC_CHECK_ARG_RET(NULL, tree, path_fmt, 1); |
| 176 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 177 | va_start(ap, path_fmt); |
| 178 | |
| 179 | /* create the path from the format */ |
| 180 | ret = vasprintf(&path, path_fmt, ap); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 181 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; path = NULL, cleanup); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 182 | |
| 183 | /* find the node we want to delete */ |
| 184 | ret = lyd_find_path(*tree, path, 0, &sub); |
| 185 | if ((ret == LY_EINCOMPLETE) || (ret == LY_ENOTFOUND)) { |
| 186 | ret = 0; |
| 187 | goto cleanup; |
| 188 | } else if (ret) { |
| 189 | ERR(NULL, "Unable to delete node in the path \"%s\".", path); |
| 190 | goto cleanup; |
| 191 | } |
| 192 | |
| 193 | lyd_free_tree(sub); |
| 194 | |
| 195 | /* set the node to top level container */ |
| 196 | ret = lyd_find_path(*tree, "/ietf-netconf-server:netconf-server", 0, tree); |
| 197 | if (ret) { |
| 198 | goto cleanup; |
| 199 | } |
| 200 | |
| 201 | cleanup: |
| 202 | free(path); |
| 203 | va_end(ap); |
| 204 | return ret; |
| 205 | } |
| 206 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 207 | #ifdef NC_ENABLED_SSH_TLS |
| 208 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 209 | const char * |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 210 | nc_server_config_util_privkey_format_to_identityref(NC_PRIVKEY_FORMAT format) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 211 | { |
| 212 | switch (format) { |
| 213 | case NC_PRIVKEY_FORMAT_RSA: |
| 214 | return "ietf-crypto-types:rsa-private-key-format"; |
| 215 | case NC_PRIVKEY_FORMAT_EC: |
| 216 | return "ietf-crypto-types:ec-private-key-format"; |
| 217 | case NC_PRIVKEY_FORMAT_X509: |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 218 | return "libnetconf2-netconf-server:private-key-info-format"; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 219 | case NC_PRIVKEY_FORMAT_OPENSSH: |
| 220 | return "libnetconf2-netconf-server:openssh-private-key-format"; |
| 221 | default: |
| 222 | ERR(NULL, "Private key type not supported."); |
| 223 | return NULL; |
| 224 | } |
| 225 | } |
| 226 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 227 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 228 | nc_server_config_util_pubkey_bin_to_b64(const unsigned char *pub_bin, int bin_len, char **pubkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 229 | { |
| 230 | int ret = 0, b64_len; |
| 231 | char *pub_b64 = NULL; |
| 232 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 233 | NC_CHECK_ARG_RET(NULL, pub_bin, bin_len, pubkey, 1); |
| 234 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 235 | /* get b64 buffer len, for ever 3 bytes of bin 4 bytes of b64 + NULL terminator */ |
| 236 | if (bin_len % 3 == 0) { |
| 237 | pub_b64 = malloc((bin_len / 3) * 4 + 1); |
| 238 | } else { |
| 239 | /* bin len not divisible by 3, need to add 4 bytes for some padding so that the len is divisible by 4 */ |
| 240 | pub_b64 = malloc((bin_len / 3) * 4 + 4 + 1); |
| 241 | } |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 242 | NC_CHECK_ERRMEM_GOTO(!pub_b64, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 243 | |
| 244 | /* bin to b64 */ |
| 245 | b64_len = EVP_EncodeBlock((unsigned char *)pub_b64, pub_bin, bin_len); |
| 246 | *pubkey = strndup(pub_b64, b64_len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 247 | NC_CHECK_ERRMEM_GOTO(!*pubkey, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 248 | |
| 249 | cleanup: |
| 250 | free(pub_b64); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 255 | nc_server_config_util_bn_to_bin(const BIGNUM *bn, unsigned char **bin, int *bin_len) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 256 | { |
| 257 | int ret = 0; |
| 258 | unsigned char *bin_tmp = NULL; |
| 259 | |
| 260 | NC_CHECK_ARG_RET(NULL, bn, bin, bin_len, 1); |
| 261 | |
| 262 | *bin = NULL; |
| 263 | |
| 264 | /* prepare buffer for converting BN to binary */ |
| 265 | bin_tmp = calloc(BN_num_bytes(bn), sizeof *bin_tmp); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 266 | NC_CHECK_ERRMEM_RET(!bin_tmp, 1); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 267 | |
| 268 | /* convert to binary */ |
| 269 | *bin_len = BN_bn2bin(bn, bin_tmp); |
| 270 | |
| 271 | /* if the highest bit in the MSB is set a byte with the value 0 has to be prepended */ |
| 272 | if (bin_tmp[0] & 0x80) { |
| 273 | *bin = malloc(*bin_len + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 274 | NC_CHECK_ERRMEM_GOTO(!*bin, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 275 | (*bin)[0] = 0; |
| 276 | memcpy(*bin + 1, bin_tmp, *bin_len); |
| 277 | (*bin_len)++; |
| 278 | } else { |
| 279 | *bin = malloc(*bin_len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 280 | NC_CHECK_ERRMEM_GOTO(!*bin, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 281 | memcpy(*bin, bin_tmp, *bin_len); |
| 282 | } |
| 283 | |
| 284 | cleanup: |
| 285 | free(bin_tmp); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | /* ssh pubkey defined in RFC 4253 section 6.6 */ |
| 290 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 291 | nc_server_config_util_evp_pkey_to_ssh_pubkey(EVP_PKEY *pkey, char **pubkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 292 | { |
| 293 | int ret = 0, e_len, n_len, p_len, bin_len; |
| 294 | BIGNUM *e = NULL, *n = NULL, *p = NULL; |
| 295 | unsigned char *e_bin = NULL, *n_bin = NULL, *p_bin = NULL, *bin = NULL, *bin_tmp; |
| 296 | const char *algorithm_name, *curve_name; |
| 297 | char *ec_group = NULL; |
| 298 | uint32_t alg_name_len, curve_name_len, alg_name_len_be, curve_name_len_be, p_len_be, e_len_be, n_len_be; |
| 299 | size_t ec_group_len; |
| 300 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 301 | NC_CHECK_ARG_RET(NULL, pkey, pubkey, 1); |
| 302 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 303 | if (EVP_PKEY_is_a(pkey, "RSA")) { |
| 304 | /* RSA key */ |
| 305 | algorithm_name = "ssh-rsa"; |
| 306 | |
| 307 | /* get the public key params */ |
| 308 | if (!EVP_PKEY_get_bn_param(pkey, "e", &e) || !EVP_PKEY_get_bn_param(pkey, "n", &n)) { |
| 309 | ERR(NULL, "Getting public key parameters from RSA private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 310 | ret = 1; |
| 311 | goto cleanup; |
| 312 | } |
| 313 | |
| 314 | /* BIGNUM to bin */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 315 | if (nc_server_config_util_bn_to_bin(e, &e_bin, &e_len) || nc_server_config_util_bn_to_bin(n, &n_bin, &n_len)) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 316 | ret = 1; |
| 317 | goto cleanup; |
| 318 | } |
| 319 | |
| 320 | alg_name_len = strlen(algorithm_name); |
| 321 | /* buffer for public key in binary, which looks like this: |
| 322 | * alg_name len (4 bytes), alg_name, PK exponent len (4 bytes), PK exponent, modulus len (4 bytes), modulus |
| 323 | */ |
| 324 | bin_len = 4 + alg_name_len + 4 + e_len + 4 + n_len; |
| 325 | bin = malloc(bin_len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 326 | NC_CHECK_ERRMEM_GOTO(!bin, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 327 | |
| 328 | /* to network byte order (big endian) */ |
| 329 | alg_name_len_be = htonl(alg_name_len); |
| 330 | e_len_be = htonl(e_len); |
| 331 | n_len_be = htonl(n_len); |
| 332 | |
| 333 | /* create the public key in binary */ |
| 334 | bin_tmp = bin; |
| 335 | memcpy(bin_tmp, &alg_name_len_be, 4); |
| 336 | bin_tmp += 4; |
| 337 | memcpy(bin_tmp, algorithm_name, alg_name_len); |
| 338 | bin_tmp += alg_name_len; |
| 339 | memcpy(bin_tmp, &e_len_be, 4); |
| 340 | bin_tmp += 4; |
| 341 | memcpy(bin_tmp, e_bin, e_len); |
| 342 | bin_tmp += e_len; |
| 343 | memcpy(bin_tmp, &n_len_be, 4); |
| 344 | bin_tmp += 4; |
| 345 | memcpy(bin_tmp, n_bin, n_len); |
| 346 | } else if (EVP_PKEY_is_a(pkey, "EC")) { |
| 347 | /* EC Private key, get it's group first */ |
| 348 | /* get group len */ |
| 349 | ret = EVP_PKEY_get_utf8_string_param(pkey, "group", NULL, 0, &ec_group_len); |
| 350 | if (!ret) { |
| 351 | ret = 1; |
| 352 | goto cleanup; |
| 353 | } |
| 354 | /* alloc mem for group + 1 for \0 */ |
| 355 | ec_group = malloc(ec_group_len + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 356 | NC_CHECK_ERRMEM_GOTO(!ec_group, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 357 | /* get the group */ |
| 358 | ret = EVP_PKEY_get_utf8_string_param(pkey, "group", ec_group, ec_group_len + 1, NULL); |
| 359 | if (!ret) { |
| 360 | ERR(NULL, "Getting public key parameter from EC private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 361 | ret = 1; |
| 362 | goto cleanup; |
| 363 | } |
| 364 | |
| 365 | /* get alg and curve names */ |
| 366 | if (!strcmp(ec_group, "P-256") || !strcmp(ec_group, "secp256r1") || !strcmp(ec_group, "prime256v1")) { |
| 367 | algorithm_name = "ecdsa-sha2-nistp256"; |
| 368 | curve_name = "nistp256"; |
| 369 | } else if (!strcmp(ec_group, "P-384") || !strcmp(ec_group, "secp384r1")) { |
| 370 | algorithm_name = "ecdsa-sha2-nistp384"; |
| 371 | curve_name = "nistp384"; |
| 372 | } else if (!strcmp(ec_group, "P-521") || !strcmp(ec_group, "secp521r1")) { |
| 373 | algorithm_name = "ecdsa-sha2-nistp521"; |
| 374 | curve_name = "nistp521"; |
| 375 | } else { |
| 376 | ERR(NULL, "EC group \"%s\" not supported.", ec_group); |
| 377 | ret = 1; |
| 378 | goto cleanup; |
| 379 | } |
| 380 | |
| 381 | /* get the public key - p, which is a point on the elliptic curve */ |
| 382 | ret = EVP_PKEY_get_bn_param(pkey, "p", &p); |
| 383 | if (!ret) { |
| 384 | ERR(NULL, "Getting public key point from the EC private key failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 385 | ret = 1; |
| 386 | goto cleanup; |
| 387 | } |
| 388 | |
| 389 | /* prepare buffer for converting p to binary */ |
| 390 | p_bin = malloc(BN_num_bytes(p)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 391 | NC_CHECK_ERRMEM_GOTO(!p_bin, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 392 | /* convert to binary */ |
| 393 | p_len = BN_bn2bin(p, p_bin); |
| 394 | |
| 395 | alg_name_len = strlen(algorithm_name); |
| 396 | curve_name_len = strlen(curve_name); |
| 397 | /* buffer for public key in binary, which looks like so: |
| 398 | * alg_name len (4 bytes), alg_name, curve_name len (4 bytes), curve_name, PK point p len (4 bytes), PK point p |
| 399 | */ |
| 400 | bin_len = 4 + alg_name_len + 4 + curve_name_len + 4 + p_len; |
| 401 | bin = malloc(bin_len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 402 | NC_CHECK_ERRMEM_GOTO(!bin, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 403 | |
| 404 | /* to network byte order (big endian) */ |
| 405 | alg_name_len_be = htonl(alg_name_len); |
| 406 | curve_name_len_be = htonl(curve_name_len); |
| 407 | p_len_be = htonl(p_len); |
| 408 | |
| 409 | /* create the public key in binary */ |
| 410 | bin_tmp = bin; |
| 411 | memcpy(bin_tmp, &alg_name_len_be, 4); |
| 412 | bin_tmp += 4; |
| 413 | memcpy(bin_tmp, algorithm_name, alg_name_len); |
| 414 | bin_tmp += alg_name_len; |
| 415 | memcpy(bin_tmp, &curve_name_len_be, 4); |
| 416 | bin_tmp += 4; |
| 417 | memcpy(bin_tmp, curve_name, curve_name_len); |
| 418 | bin_tmp += curve_name_len; |
| 419 | memcpy(bin_tmp, &p_len_be, 4); |
| 420 | bin_tmp += 4; |
| 421 | memcpy(bin_tmp, p_bin, p_len); |
| 422 | } else if (EVP_PKEY_is_a(pkey, "ED25519")) { |
| 423 | ERR(NULL, "Generating PEM ED25519 key from OpenSSH is not supported by libssh yet."); |
| 424 | ret = 1; |
| 425 | goto cleanup; |
| 426 | } else { |
| 427 | ERR(NULL, "Unable to generate public key from private key (Private key type not supported)."); |
| 428 | ret = 1; |
| 429 | goto cleanup; |
| 430 | } |
| 431 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 432 | /* convert created bin to b64 */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 433 | ret = nc_server_config_util_pubkey_bin_to_b64(bin, bin_len, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 434 | if (ret) { |
| 435 | ERR(NULL, "Converting public key from binary to base64 failed."); |
| 436 | goto cleanup; |
| 437 | } |
| 438 | |
| 439 | cleanup: |
| 440 | free(bin); |
| 441 | free(e_bin); |
| 442 | free(n_bin); |
| 443 | free(ec_group); |
| 444 | free(p_bin); |
| 445 | BN_free(e); |
| 446 | BN_free(n); |
| 447 | BN_free(p); |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* spki = subject public key info */ |
| 452 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 453 | nc_server_config_util_evp_pkey_to_spki_pubkey(EVP_PKEY *pkey, char **pubkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 454 | { |
| 455 | int ret = 0, len; |
| 456 | BIO *bio = NULL; |
| 457 | char *pub_b64 = NULL; |
| 458 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 459 | NC_CHECK_ARG_RET(NULL, pkey, pubkey, 1); |
| 460 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 461 | bio = BIO_new(BIO_s_mem()); |
| 462 | if (!bio) { |
| 463 | ERR(NULL, "Creating new BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 464 | ret = 1; |
| 465 | goto cleanup; |
| 466 | } |
| 467 | |
| 468 | /* write the evp_pkey contents to bio */ |
| 469 | if (!PEM_write_bio_PUBKEY(bio, pkey)) { |
| 470 | ERR(NULL, "Writing public key to BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 471 | ret = 1; |
| 472 | goto cleanup; |
| 473 | } |
| 474 | |
| 475 | /* read the pubkey from bio */ |
| 476 | len = BIO_get_mem_data(bio, &pub_b64); |
| 477 | if (len <= 0) { |
| 478 | ERR(NULL, "Reading base64 private key from BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 479 | ret = 1; |
| 480 | goto cleanup; |
| 481 | } |
| 482 | |
| 483 | /* copy the public key without the header and footer */ |
| 484 | *pubkey = strndup(pub_b64 + strlen(NC_SUBJECT_PUBKEY_INFO_HEADER), |
| 485 | len - strlen(NC_SUBJECT_PUBKEY_INFO_HEADER) - strlen(NC_SUBJECT_PUBKEY_INFO_FOOTER)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 486 | NC_CHECK_ERRMEM_GOTO(!*pubkey, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 487 | |
| 488 | cleanup: |
| 489 | BIO_free(bio); |
| 490 | return ret; |
| 491 | } |
| 492 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 493 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 494 | nc_server_config_util_read_certificate(const char *cert_path, char **cert) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 495 | { |
| 496 | int ret = 0, cert_len; |
| 497 | X509 *x509 = NULL; |
| 498 | FILE *f = NULL; |
| 499 | BIO *bio = NULL; |
| 500 | char *c = NULL; |
| 501 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 502 | NC_CHECK_ARG_RET(NULL, cert_path, cert, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 503 | |
| 504 | f = fopen(cert_path, "r"); |
| 505 | if (!f) { |
| 506 | ERR(NULL, "Unable to open certificate file \"%s\".", cert_path); |
| 507 | ret = 1; |
| 508 | goto cleanup; |
| 509 | } |
| 510 | |
| 511 | /* load the cert into memory */ |
| 512 | x509 = PEM_read_X509(f, NULL, NULL, NULL); |
| 513 | if (!x509) { |
| 514 | ret = -1; |
| 515 | goto cleanup; |
| 516 | } |
| 517 | |
| 518 | bio = BIO_new(BIO_s_mem()); |
| 519 | if (!bio) { |
| 520 | ret = -1; |
| 521 | goto cleanup; |
| 522 | } |
| 523 | |
| 524 | ret = PEM_write_bio_X509(bio, x509); |
| 525 | if (!ret) { |
| 526 | ret = -1; |
| 527 | goto cleanup; |
| 528 | } |
| 529 | |
| 530 | cert_len = BIO_pending(bio); |
| 531 | if (cert_len <= 0) { |
| 532 | ret = -1; |
| 533 | goto cleanup; |
| 534 | } |
| 535 | |
| 536 | c = malloc(cert_len + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 537 | NC_CHECK_ERRMEM_GOTO(!c, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 538 | |
| 539 | /* read the cert from bio */ |
| 540 | ret = BIO_read(bio, c, cert_len); |
| 541 | if (ret <= 0) { |
| 542 | ret = -1; |
| 543 | goto cleanup; |
| 544 | } |
| 545 | c[cert_len] = '\0'; |
| 546 | |
| 547 | /* strip the cert of the header and footer */ |
| 548 | *cert = strdup(c + strlen(NC_PEM_CERTIFICATE_HEADER)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 549 | NC_CHECK_ERRMEM_GOTO(!*cert, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 550 | |
| 551 | (*cert)[strlen(*cert) - strlen(NC_PEM_CERTIFICATE_FOOTER)] = '\0'; |
| 552 | |
| 553 | ret = 0; |
| 554 | |
| 555 | cleanup: |
| 556 | if (ret == -1) { |
| 557 | ERR(NULL, "Error getting certificate from file \"%s\" (OpenSSL Error): \"%s\".", cert_path, ERR_reason_error_string(ERR_get_error())); |
| 558 | ret = 1; |
| 559 | } |
| 560 | if (f) { |
| 561 | fclose(f); |
| 562 | } |
| 563 | |
| 564 | BIO_free(bio); |
| 565 | X509_free(x509); |
| 566 | free(c); |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 571 | nc_server_config_util_read_pubkey_ssh2(FILE *f, char **pubkey) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 572 | { |
| 573 | char *buffer = NULL; |
| 574 | size_t size = 0, pubkey_len = 0; |
| 575 | void *tmp; |
| 576 | ssize_t read; |
| 577 | int ret = 0; |
| 578 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 579 | NC_CHECK_ARG_RET(NULL, f, pubkey, 1); |
| 580 | |
| 581 | /* read lines from the file and create the public key without NL from it */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 582 | while ((read = getline(&buffer, &size, f)) > 0) { |
| 583 | if (!strncmp(buffer, "----", 4)) { |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 584 | /* skip header and footer */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 585 | continue; |
| 586 | } |
| 587 | |
| 588 | if (!strncmp(buffer, "Comment:", 8)) { |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 589 | /* skip a comment */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 590 | continue; |
| 591 | } |
| 592 | |
| 593 | if (buffer[read - 1] == '\n') { |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 594 | /* avoid NL */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 595 | read--; |
| 596 | } |
| 597 | |
| 598 | tmp = realloc(*pubkey, pubkey_len + read + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 599 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 600 | |
| 601 | *pubkey = tmp; |
| 602 | memcpy(*pubkey + pubkey_len, buffer, read); |
| 603 | pubkey_len += read; |
| 604 | } |
| 605 | |
| 606 | if (!pubkey_len) { |
| 607 | ERR(NULL, "Unexpected public key format."); |
| 608 | ret = 1; |
| 609 | goto cleanup; |
| 610 | } |
| 611 | |
| 612 | (*pubkey)[pubkey_len] = '\0'; |
| 613 | |
| 614 | cleanup: |
| 615 | free(buffer); |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 620 | nc_server_config_util_read_pubkey_openssl(FILE *f, char **pubkey) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 621 | { |
| 622 | int ret = 0; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 623 | EVP_PKEY *pub_pkey = NULL; |
| 624 | |
| 625 | NC_CHECK_ARG_RET(NULL, f, pubkey, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 626 | |
| 627 | /* read the pubkey from file */ |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 628 | pub_pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); |
| 629 | if (!pub_pkey) { |
| 630 | ERR(NULL, "Reading public key from file failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 631 | return 1; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 634 | ret = nc_server_config_util_evp_pkey_to_ssh_pubkey(pub_pkey, pubkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 635 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 636 | EVP_PKEY_free(pub_pkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 637 | return ret; |
| 638 | } |
| 639 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 640 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 641 | nc_server_config_util_read_pubkey_libssh(const char *pubkey_path, char **pubkey) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 642 | { |
| 643 | int ret = 0; |
| 644 | ssh_key pub_sshkey = NULL; |
| 645 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 646 | NC_CHECK_ARG_RET(NULL, pubkey_path, pubkey, 1); |
| 647 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 648 | ret = ssh_pki_import_pubkey_file(pubkey_path, &pub_sshkey); |
| 649 | if (ret) { |
| 650 | ERR(NULL, "Importing public key from file \"%s\" failed.", pubkey_path); |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | ret = ssh_pki_export_pubkey_base64(pub_sshkey, pubkey); |
| 655 | if (ret) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 656 | ERR(NULL, "Importing pubkey failed."); |
| 657 | goto cleanup; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 658 | } |
| 659 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 660 | cleanup: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 661 | ssh_key_free(pub_sshkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 662 | return 0; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 663 | } |
| 664 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 665 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 666 | nc_server_config_util_get_ssh_pubkey_file(const char *pubkey_path, char **pubkey) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 667 | { |
| 668 | int ret = 0; |
| 669 | FILE *f = NULL; |
| 670 | char *header = NULL; |
| 671 | size_t len = 0; |
| 672 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 673 | NC_CHECK_ARG_RET(NULL, pubkey_path, pubkey, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 674 | |
| 675 | *pubkey = NULL; |
| 676 | |
| 677 | f = fopen(pubkey_path, "r"); |
| 678 | if (!f) { |
| 679 | ERR(NULL, "Unable to open file \"%s\".", pubkey_path); |
| 680 | ret = 1; |
| 681 | goto cleanup; |
| 682 | } |
| 683 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 684 | /* read the header */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 685 | if (getline(&header, &len, f) < 0) { |
| 686 | ERR(NULL, "Error reading header from file \"%s\".", pubkey_path); |
| 687 | ret = 1; |
| 688 | goto cleanup; |
| 689 | } |
| 690 | rewind(f); |
| 691 | |
| 692 | if (!strncmp(header, NC_SUBJECT_PUBKEY_INFO_HEADER, strlen(NC_SUBJECT_PUBKEY_INFO_HEADER))) { |
| 693 | /* it's subject public key info public key */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 694 | ret = nc_server_config_util_read_pubkey_openssl(f, pubkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 695 | } else if (!strncmp(header, NC_SSH2_PUBKEY_HEADER, strlen(NC_SSH2_PUBKEY_HEADER))) { |
| 696 | /* it's ssh2 public key */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 697 | ret = nc_server_config_util_read_pubkey_ssh2(f, pubkey); |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 698 | } else { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 699 | /* it's probably OpenSSH public key */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 700 | ret = nc_server_config_util_read_pubkey_libssh(pubkey_path, pubkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 701 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 702 | if (ret) { |
| 703 | ERR(NULL, "Error getting public key from file \"%s\".", pubkey_path); |
| 704 | goto cleanup; |
| 705 | } |
| 706 | |
| 707 | cleanup: |
| 708 | if (f) { |
| 709 | fclose(f); |
| 710 | } |
| 711 | |
| 712 | free(header); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 713 | return ret; |
| 714 | } |
| 715 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 716 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 717 | nc_server_config_util_get_spki_pubkey_file(const char *pubkey_path, char **pubkey) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 718 | { |
| 719 | int ret = 0; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 720 | FILE *f = NULL; |
| 721 | EVP_PKEY *pub_pkey = NULL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 722 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 723 | NC_CHECK_ARG_RET(NULL, pubkey_path, pubkey, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 724 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 725 | *pubkey = NULL; |
| 726 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 727 | f = fopen(pubkey_path, "r"); |
| 728 | if (!f) { |
| 729 | ERR(NULL, "Unable to open file \"%s\".", pubkey_path); |
| 730 | ret = 1; |
| 731 | goto cleanup; |
| 732 | } |
| 733 | |
| 734 | /* read the pubkey from file */ |
| 735 | pub_pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); |
| 736 | if (!pub_pkey) { |
| 737 | ERR(NULL, "Reading public key from file failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 738 | return 1; |
| 739 | } |
| 740 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 741 | ret = nc_server_config_util_evp_pkey_to_spki_pubkey(pub_pkey, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 742 | if (ret) { |
| 743 | goto cleanup; |
| 744 | } |
| 745 | |
| 746 | cleanup: |
| 747 | if (f) { |
| 748 | fclose(f); |
| 749 | } |
| 750 | |
| 751 | EVP_PKEY_free(pub_pkey); |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 756 | nc_server_config_util_privkey_header_to_format(FILE *f_privkey, const char *privkey_path, NC_PRIVKEY_FORMAT *privkey_format) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 757 | { |
| 758 | char *privkey_header = NULL; |
| 759 | size_t len = 0; |
| 760 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 761 | NC_CHECK_ARG_RET(NULL, f_privkey, privkey_path, privkey_format, 1); |
| 762 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 763 | /* read header */ |
| 764 | if (getline(&privkey_header, &len, f_privkey) < 0) { |
| 765 | ERR(NULL, "Error reading header from file \"%s\".", privkey_path); |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | if (!strncmp(privkey_header, NC_PKCS8_PRIVKEY_HEADER, strlen(NC_PKCS8_PRIVKEY_HEADER))) { |
| 770 | /* it's PKCS8 (X.509) private key */ |
| 771 | *privkey_format = NC_PRIVKEY_FORMAT_X509; |
| 772 | } else if (!strncmp(privkey_header, NC_OPENSSH_PRIVKEY_HEADER, strlen(NC_OPENSSH_PRIVKEY_HEADER))) { |
| 773 | /* it's OpenSSH private key */ |
| 774 | *privkey_format = NC_PRIVKEY_FORMAT_OPENSSH; |
| 775 | } else if (!strncmp(privkey_header, NC_PKCS1_RSA_PRIVKEY_HEADER, strlen(NC_PKCS1_RSA_PRIVKEY_HEADER))) { |
| 776 | /* it's RSA privkey in PKCS1 format */ |
| 777 | *privkey_format = NC_PRIVKEY_FORMAT_RSA; |
| 778 | } else if (!strncmp(privkey_header, NC_SEC1_EC_PRIVKEY_HEADER, strlen(NC_SEC1_EC_PRIVKEY_HEADER))) { |
| 779 | /* it's EC privkey in SEC1 format */ |
| 780 | *privkey_format = NC_PRIVKEY_FORMAT_EC; |
| 781 | } else { |
| 782 | ERR(NULL, "Private key format (%s) not supported.", privkey_header); |
| 783 | free(privkey_header); |
| 784 | return 1; |
| 785 | } |
| 786 | |
| 787 | /* reset the reading head */ |
| 788 | rewind(f_privkey); |
| 789 | free(privkey_header); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 794 | nc_server_config_util_get_privkey_openssl(const char *privkey_path, FILE *f_privkey, char **privkey, EVP_PKEY **pkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 795 | { |
| 796 | int ret = 0, len; |
| 797 | BIO *bio = NULL; |
| 798 | char *priv_b64 = NULL; |
| 799 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 800 | NC_CHECK_ARG_RET(NULL, privkey_path, f_privkey, privkey, pkey, 1); |
| 801 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 802 | bio = BIO_new(BIO_s_mem()); |
| 803 | if (!bio) { |
| 804 | ERR(NULL, "Creating new BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 805 | ret = 1; |
| 806 | goto cleanup; |
| 807 | } |
| 808 | |
| 809 | /* read the privkey file, create EVP_PKEY */ |
| 810 | *pkey = PEM_read_PrivateKey(f_privkey, NULL, NULL, NULL); |
| 811 | if (!*pkey) { |
| 812 | ERR(NULL, "Getting private key from file \"%s\" failed (%s).", privkey_path, ERR_reason_error_string(ERR_get_error())); |
| 813 | ret = 1; |
| 814 | goto cleanup; |
| 815 | } |
| 816 | |
| 817 | /* write the privkey to bio */ |
| 818 | if (!PEM_write_bio_PrivateKey(bio, *pkey, NULL, NULL, 0, NULL, NULL)) { |
| 819 | ERR(NULL, "Writing private key to BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 820 | ret = 1; |
| 821 | goto cleanup; |
| 822 | } |
| 823 | |
| 824 | /* read the privkey from bio */ |
| 825 | len = BIO_get_mem_data(bio, &priv_b64); |
| 826 | if (len <= 0) { |
| 827 | ERR(NULL, "Reading base64 private key from BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 828 | ret = 1; |
| 829 | goto cleanup; |
| 830 | } |
| 831 | |
| 832 | *privkey = strndup(priv_b64, len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 833 | NC_CHECK_ERRMEM_GOTO(!*privkey, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 834 | |
| 835 | cleanup: |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 836 | /* priv_b64 is freed with BIO */ |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 837 | BIO_free(bio); |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 842 | nc_server_config_util_get_privkey_libssh(const char *privkey_path, char **privkey, EVP_PKEY **pkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 843 | { |
| 844 | int ret = 0; |
| 845 | BIO *bio = NULL; |
| 846 | char *priv_b64 = NULL; |
| 847 | ssh_key key = NULL; |
| 848 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 849 | NC_CHECK_ARG_RET(NULL, privkey_path, privkey, pkey, 1); |
| 850 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 851 | ret = ssh_pki_import_privkey_file(privkey_path, NULL, NULL, NULL, &key); |
| 852 | if (ret) { |
| 853 | ERR(NULL, "Importing privkey from file \"%s\" failed.", privkey_path); |
| 854 | goto cleanup; |
| 855 | } |
| 856 | |
| 857 | /* exports the key in a format in which OpenSSL can read it */ |
| 858 | ret = ssh_pki_export_privkey_base64(key, NULL, NULL, NULL, &priv_b64); |
| 859 | if (ret) { |
| 860 | ERR(NULL, "Exporting privkey to base64 failed."); |
| 861 | goto cleanup; |
| 862 | } |
| 863 | |
| 864 | bio = BIO_new(BIO_s_mem()); |
| 865 | if (!bio) { |
| 866 | ERR(NULL, "Creating new BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 867 | ret = 1; |
| 868 | goto cleanup; |
| 869 | } |
| 870 | |
| 871 | ret = BIO_write(bio, priv_b64, strlen(priv_b64)); |
| 872 | if (ret <= 0) { |
| 873 | ERR(NULL, "Writing private key to BIO failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 874 | ret = 1; |
| 875 | goto cleanup; |
| 876 | } |
| 877 | |
| 878 | /* create EVP_PKEY from the b64 */ |
| 879 | *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 880 | if (!*pkey) { |
| 881 | ERR(NULL, "Getting private key from file \"%s\" failed (%s).", privkey_path, ERR_reason_error_string(ERR_get_error())); |
| 882 | ret = 1; |
| 883 | goto cleanup; |
| 884 | } |
| 885 | |
| 886 | *privkey = strndup(priv_b64, ret); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 887 | NC_CHECK_ERRMEM_GOTO(!*privkey, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 888 | |
| 889 | /* ok */ |
| 890 | ret = 0; |
| 891 | |
| 892 | cleanup: |
| 893 | free(priv_b64); |
| 894 | BIO_free(bio); |
| 895 | ssh_key_free(key); |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | static int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 900 | nc_server_config_util_get_privkey(const char *privkey_path, NC_PRIVKEY_FORMAT *privkey_format, char **privkey, EVP_PKEY **pkey) |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 901 | { |
| 902 | int ret = 0; |
| 903 | FILE *f_privkey = NULL; |
| 904 | char *priv = NULL; |
| 905 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 906 | NC_CHECK_ARG_RET(NULL, privkey_path, privkey_format, privkey, pkey, 1); |
| 907 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 908 | f_privkey = fopen(privkey_path, "r"); |
| 909 | if (!f_privkey) { |
| 910 | ERR(NULL, "Unable to open file \"%s\".", privkey_path); |
| 911 | ret = 1; |
| 912 | goto cleanup; |
| 913 | } |
| 914 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 915 | /* read the first line from the privkey to determine it's type */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 916 | ret = nc_server_config_util_privkey_header_to_format(f_privkey, privkey_path, privkey_format); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 917 | if (ret) { |
| 918 | ERR(NULL, "Getting private key format from file \"%s\" failed.", privkey_path); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 919 | goto cleanup; |
| 920 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 921 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 922 | switch (*privkey_format) { |
| 923 | /* fall-through */ |
| 924 | case NC_PRIVKEY_FORMAT_RSA: |
| 925 | case NC_PRIVKEY_FORMAT_EC: |
| 926 | case NC_PRIVKEY_FORMAT_X509: |
| 927 | /* OpenSSL solely can do this */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 928 | ret = nc_server_config_util_get_privkey_openssl(privkey_path, f_privkey, &priv, pkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 929 | break; |
| 930 | case NC_PRIVKEY_FORMAT_OPENSSH: |
| 931 | /* need the help of libssh */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 932 | ret = nc_server_config_util_get_privkey_libssh(privkey_path, &priv, pkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 933 | /* if the function returned successfully, the key is no longer OpenSSH, it was converted to x509 */ |
| 934 | *privkey_format = NC_PRIVKEY_FORMAT_X509; |
| 935 | break; |
| 936 | default: |
| 937 | ERR(NULL, "Private key format not recognized."); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 938 | ret = 1; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 939 | break; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 940 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 941 | if (ret) { |
| 942 | goto cleanup; |
| 943 | } |
| 944 | |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 945 | /* strip private key's header and footer */ |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 946 | *privkey = strdup(priv + strlen(NC_PKCS8_PRIVKEY_HEADER)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 947 | NC_CHECK_ERRMEM_GOTO(!*privkey, ret = 1, cleanup); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 948 | (*privkey)[strlen(*privkey) - strlen(NC_PKCS8_PRIVKEY_FOOTER)] = '\0'; |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 949 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 950 | cleanup: |
| 951 | if (f_privkey) { |
| 952 | fclose(f_privkey); |
| 953 | } |
| 954 | |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 955 | free(priv); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 956 | return ret; |
| 957 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 958 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 959 | int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 960 | nc_server_config_util_get_asym_key_pair(const char *privkey_path, const char *pubkey_path, NC_PUBKEY_FORMAT wanted_pubkey_format, |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 961 | char **privkey, NC_PRIVKEY_FORMAT *privkey_type, char **pubkey) |
| 962 | { |
| 963 | int ret = 0; |
| 964 | EVP_PKEY *priv_pkey = NULL; |
| 965 | |
| 966 | NC_CHECK_ARG_RET(NULL, privkey_path, privkey, privkey_type, pubkey, 1); |
| 967 | |
| 968 | *privkey = NULL; |
| 969 | *pubkey = NULL; |
| 970 | |
| 971 | /* get private key base64 and EVP_PKEY */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 972 | ret = nc_server_config_util_get_privkey(privkey_path, privkey_type, privkey, &priv_pkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 973 | if (ret) { |
| 974 | ERR(NULL, "Getting private key from file \"%s\" failed.", privkey_path); |
| 975 | goto cleanup; |
| 976 | } |
| 977 | |
| 978 | /* get public key, either from file or generate it from the EVP_PKEY */ |
| 979 | if (!pubkey_path) { |
| 980 | if (wanted_pubkey_format == NC_PUBKEY_FORMAT_SSH) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 981 | ret = nc_server_config_util_evp_pkey_to_ssh_pubkey(priv_pkey, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 982 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 983 | ret = nc_server_config_util_evp_pkey_to_spki_pubkey(priv_pkey, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 984 | } |
| 985 | } else { |
| 986 | if (wanted_pubkey_format == NC_PUBKEY_FORMAT_SSH) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 987 | ret = nc_server_config_util_get_ssh_pubkey_file(pubkey_path, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 988 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 989 | ret = nc_server_config_util_get_spki_pubkey_file(pubkey_path, pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | if (ret) { |
| 993 | if (pubkey_path) { |
| 994 | ERR(NULL, "Getting public key from file \"%s\" failed.", pubkey_path); |
| 995 | } else { |
| 996 | ERR(NULL, "Generating public key from private key failed."); |
| 997 | } |
| 998 | goto cleanup; |
| 999 | } |
| 1000 | |
| 1001 | cleanup: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1002 | EVP_PKEY_free(priv_pkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1007 | nc_server_config_add_address_port(const struct ly_ctx *ctx, const char *endpt_name, NC_TRANSPORT_IMPL transport, |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1008 | const char *address, uint16_t port, struct lyd_node **config) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1009 | { |
| 1010 | int ret = 0; |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 1011 | const char *address_fmt, *port_fmt; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1012 | char port_buf[6] = {0}; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1013 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1014 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, address, config, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1015 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1016 | if (transport == NC_TI_LIBSSH) { |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 1017 | /* SSH path */ |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1018 | address_fmt = "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='%s']/ssh/tcp-server-parameters/local-address"; |
| 1019 | port_fmt = "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='%s']/ssh/tcp-server-parameters/local-port"; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1020 | } else if (transport == NC_TI_OPENSSL) { |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 1021 | /* TLS path */ |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1022 | address_fmt = "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='%s']/tls/tcp-server-parameters/local-address"; |
| 1023 | port_fmt = "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='%s']/tls/tcp-server-parameters/local-port"; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1024 | } else { |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1025 | ERR(NULL, "Can not set address and port of a non SSH/TLS endpoint."); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1026 | ret = 1; |
| 1027 | goto cleanup; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1028 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1029 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1030 | ret = nc_server_config_create(ctx, config, address, address_fmt, endpt_name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1031 | if (ret) { |
| 1032 | goto cleanup; |
| 1033 | } |
| 1034 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1035 | sprintf(port_buf, "%d", port); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1036 | ret = nc_server_config_create(ctx, config, port_buf, port_fmt, endpt_name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1037 | if (ret) { |
| 1038 | goto cleanup; |
| 1039 | } |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 1040 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1041 | cleanup: |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 1042 | return ret; |
| 1043 | } |
| 1044 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1045 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1046 | nc_server_config_add_ch_address_port(const struct ly_ctx *ctx, const char *client_name, const char *endpt_name, |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1047 | NC_TRANSPORT_IMPL transport, const char *address, const char *port, struct lyd_node **config) |
| 1048 | { |
| 1049 | int ret = 0; |
| 1050 | const char *address_fmt, *port_fmt; |
| 1051 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1052 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, address, port, config, 1); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1053 | |
| 1054 | if (transport == NC_TI_LIBSSH) { |
| 1055 | /* SSH path */ |
| 1056 | address_fmt = "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/endpoints/endpoint[name='%s']/ssh/tcp-client-parameters/remote-address"; |
| 1057 | port_fmt = "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/endpoints/endpoint[name='%s']/ssh/tcp-client-parameters/remote-port"; |
| 1058 | } else if (transport == NC_TI_OPENSSL) { |
| 1059 | /* TLS path */ |
| 1060 | address_fmt = "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tcp-client-parameters/remote-address"; |
| 1061 | port_fmt = "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tcp-client-parameters/remote-port"; |
| 1062 | } else { |
| 1063 | ERR(NULL, "Transport not supported."); |
| 1064 | ret = 1; |
| 1065 | goto cleanup; |
| 1066 | } |
| 1067 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1068 | ret = nc_server_config_create(ctx, config, address, address_fmt, client_name, endpt_name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1069 | if (ret) { |
| 1070 | goto cleanup; |
| 1071 | } |
| 1072 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1073 | ret = nc_server_config_create(ctx, config, port, port_fmt, client_name, endpt_name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1074 | if (ret) { |
| 1075 | goto cleanup; |
| 1076 | } |
| 1077 | |
| 1078 | cleanup: |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1083 | nc_server_config_del_endpt(const char *endpt_name, struct lyd_node **config) |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1084 | { |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1085 | NC_CHECK_ARG_RET(NULL, config, 1); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1086 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1087 | if (endpt_name) { |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1088 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='%s']", endpt_name); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1089 | } else { |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1090 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint"); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1095 | nc_server_config_del_ch_client(const char *ch_client_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1096 | { |
| 1097 | NC_CHECK_ARG_RET(NULL, config, 1); |
| 1098 | |
| 1099 | if (ch_client_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1100 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']", ch_client_name); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1101 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1102 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client"); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1107 | nc_server_config_del_ch_endpt(const char *client_name, const char *endpt_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1108 | { |
| 1109 | NC_CHECK_ARG_RET(NULL, client_name, config, 1); |
| 1110 | |
| 1111 | if (endpt_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1112 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1113 | "endpoints/endpoint[name='%s']", client_name, endpt_name); |
| 1114 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1115 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1116 | "endpoints/endpoint", client_name); |
| 1117 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1120 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1121 | nc_server_config_add_keystore_asym_key(const struct ly_ctx *ctx, NC_TRANSPORT_IMPL ti, const char *asym_key_name, |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1122 | const char *privkey_path, const char *pubkey_path, struct lyd_node **config) |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1123 | { |
| 1124 | int ret = 0; |
| 1125 | char *privkey = NULL, *pubkey = NULL; |
| 1126 | NC_PRIVKEY_FORMAT privkey_type; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1127 | const char *privkey_format, *pubkey_format; |
| 1128 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1129 | NC_CHECK_ARG_RET(NULL, ctx, asym_key_name, privkey_path, config, 1); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1130 | |
| 1131 | /* get the keys as a string from the given files */ |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1132 | if (ti == NC_TI_LIBSSH) { |
Michal Vasko | 7ac7c56 | 2024-03-05 09:51:21 +0100 | [diff] [blame] | 1133 | ret = nc_server_config_util_get_asym_key_pair(privkey_path, pubkey_path, NC_PUBKEY_FORMAT_SSH, &privkey, |
| 1134 | &privkey_type, &pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1135 | } else if (ti == NC_TI_OPENSSL) { |
Michal Vasko | 7ac7c56 | 2024-03-05 09:51:21 +0100 | [diff] [blame] | 1136 | ret = nc_server_config_util_get_asym_key_pair(privkey_path, pubkey_path, NC_PUBKEY_FORMAT_X509, &privkey, |
| 1137 | &privkey_type, &pubkey); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1138 | } else { |
| 1139 | ERR(NULL, "Only SSH and TLS transports can be used to create an asymmetric key pair in the keystore."); |
| 1140 | ret = 1; |
| 1141 | goto cleanup; |
| 1142 | } |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1143 | if (ret) { |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1144 | goto cleanup; |
| 1145 | } |
| 1146 | |
| 1147 | /* get pubkey format str */ |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1148 | if (ti == NC_TI_LIBSSH) { |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1149 | pubkey_format = "ietf-crypto-types:ssh-public-key-format"; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1150 | } else { |
| 1151 | pubkey_format = "ietf-crypto-types:subject-public-key-info-format"; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /* get privkey identityref value */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1155 | privkey_format = nc_server_config_util_privkey_format_to_identityref(privkey_type); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1156 | if (!privkey_format) { |
| 1157 | ret = 1; |
| 1158 | goto cleanup; |
| 1159 | } |
| 1160 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1161 | ret = nc_server_config_create(ctx, config, pubkey_format, "/ietf-keystore:keystore/asymmetric-keys/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1162 | "asymmetric-key[name='%s']/public-key-format", asym_key_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1163 | if (ret) { |
| 1164 | goto cleanup; |
| 1165 | } |
| 1166 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1167 | ret = nc_server_config_create(ctx, config, pubkey, "/ietf-keystore:keystore/asymmetric-keys/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1168 | "asymmetric-key[name='%s']/public-key", asym_key_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1169 | if (ret) { |
| 1170 | goto cleanup; |
| 1171 | } |
| 1172 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1173 | ret = nc_server_config_create(ctx, config, privkey_format, "/ietf-keystore:keystore/asymmetric-keys/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1174 | "asymmetric-key[name='%s']/private-key-format", asym_key_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1175 | if (ret) { |
| 1176 | goto cleanup; |
| 1177 | } |
| 1178 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1179 | ret = nc_server_config_create(ctx, config, privkey, "/ietf-keystore:keystore/asymmetric-keys/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1180 | "asymmetric-key[name='%s']/cleartext-private-key", asym_key_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1181 | if (ret) { |
| 1182 | goto cleanup; |
| 1183 | } |
| 1184 | |
| 1185 | cleanup: |
| 1186 | free(privkey); |
| 1187 | free(pubkey); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1192 | nc_server_config_del_keystore_asym_key(const char *asym_key_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1193 | { |
| 1194 | NC_CHECK_ARG_RET(NULL, config, 1); |
| 1195 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1196 | if (asym_key_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1197 | return nc_server_config_delete(config, "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key[name='%s']", asym_key_name); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1198 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1199 | return nc_server_config_delete(config, "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key"); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1204 | nc_server_config_add_keystore_cert(const struct ly_ctx *ctx, const char *asym_key_name, const char *cert_name, |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1205 | const char *cert_path, struct lyd_node **config) |
| 1206 | { |
| 1207 | int ret = 0; |
| 1208 | char *cert = NULL; |
| 1209 | |
| 1210 | NC_CHECK_ARG_RET(NULL, ctx, asym_key_name, cert_name, cert_path, config, 1); |
| 1211 | |
| 1212 | /* get cert data */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1213 | ret = nc_server_config_util_read_certificate(cert_path, &cert); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1214 | if (ret) { |
| 1215 | goto cleanup; |
| 1216 | } |
| 1217 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1218 | ret = nc_server_config_create(ctx, config, cert, "/ietf-keystore:keystore/asymmetric-keys/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1219 | "asymmetric-key[name='%s']/certificates/certificate[name='%s']/cert-data", asym_key_name, cert_name); |
| 1220 | |
| 1221 | cleanup: |
| 1222 | free(cert); |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1227 | nc_server_config_del_keystore_cert(const char *asym_key_name, const char *cert_name, struct lyd_node **config) |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1228 | { |
| 1229 | NC_CHECK_ARG_RET(NULL, asym_key_name, config, 1); |
| 1230 | |
| 1231 | if (cert_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1232 | return nc_server_config_delete(config, "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key[name='%s']/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1233 | "certificates/certificate[name='%s']", asym_key_name, cert_name); |
| 1234 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1235 | return nc_server_config_delete(config, "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key[name='%s']/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1236 | "certificates/certificate", asym_key_name); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | API int |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 1241 | nc_server_config_add_truststore_pubkey(const struct ly_ctx *ctx, const char *pub_bag_name, const char *pubkey_name, |
| 1242 | const char *pubkey_path, struct lyd_node **config) |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1243 | { |
| 1244 | int ret = 0; |
| 1245 | char *pubkey = NULL; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1246 | const char *pubkey_format = "ietf-crypto-types:ssh-public-key-format"; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1247 | |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1248 | NC_CHECK_ARG_RET(NULL, ctx, pub_bag_name, pubkey_name, pubkey_path, config, 1); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1249 | |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 1250 | ret = nc_server_config_util_get_ssh_pubkey_file(pubkey_path, &pubkey); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1251 | if (ret) { |
| 1252 | goto cleanup; |
| 1253 | } |
| 1254 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1255 | ret = nc_server_config_create(ctx, config, pubkey_format, "/ietf-truststore:truststore/public-key-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1256 | "public-key-bag[name='%s']/public-key[name='%s']/public-key-format", pub_bag_name, pubkey_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1257 | if (ret) { |
| 1258 | goto cleanup; |
| 1259 | } |
| 1260 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1261 | ret = nc_server_config_create(ctx, config, pubkey, "/ietf-truststore:truststore/public-key-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1262 | "public-key-bag[name='%s']/public-key[name='%s']/public-key", pub_bag_name, pubkey_name); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 1263 | if (ret) { |
| 1264 | goto cleanup; |
| 1265 | } |
| 1266 | |
| 1267 | cleanup: |
| 1268 | free(pubkey); |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1272 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1273 | nc_server_config_del_truststore_pubkey(const char *pub_bag_name, |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1274 | const char *pubkey_name, struct lyd_node **config) |
| 1275 | { |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1276 | NC_CHECK_ARG_RET(NULL, pub_bag_name, config, 1); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1277 | |
| 1278 | if (pubkey_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1279 | return nc_server_config_delete(config, "/ietf-truststore:truststore/public-key-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1280 | "public-key-bag[name='%s']/public-key[name='%s']", pub_bag_name, pubkey_name); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1281 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1282 | return nc_server_config_delete(config, "/ietf-truststore:truststore/public-key-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1283 | "public-key-bag[name='%s']/public-key", pub_bag_name); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1288 | nc_server_config_add_truststore_cert(const struct ly_ctx *ctx, const char *cert_bag_name, const char *cert_name, |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1289 | const char *cert_path, struct lyd_node **config) |
| 1290 | { |
| 1291 | int ret = 0; |
| 1292 | char *cert = NULL; |
| 1293 | |
| 1294 | NC_CHECK_ARG_RET(NULL, ctx, cert_bag_name, cert_name, cert_path, config, 1); |
| 1295 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1296 | ret = nc_server_config_util_read_certificate(cert_path, &cert); |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1297 | if (ret) { |
| 1298 | goto cleanup; |
| 1299 | } |
| 1300 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1301 | ret = nc_server_config_create(ctx, config, cert, "/ietf-truststore:truststore/certificate-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1302 | "certificate-bag[name='%s']/certificate[name='%s']/cert-data", cert_bag_name, cert_name); |
| 1303 | if (ret) { |
| 1304 | goto cleanup; |
| 1305 | } |
| 1306 | |
| 1307 | cleanup: |
| 1308 | free(cert); |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
| 1312 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1313 | nc_server_config_del_truststore_cert(const char *cert_bag_name, |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1314 | const char *cert_name, struct lyd_node **config) |
| 1315 | { |
| 1316 | NC_CHECK_ARG_RET(NULL, cert_bag_name, config, 1); |
| 1317 | |
| 1318 | if (cert_name) { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1319 | return nc_server_config_delete(config, "/ietf-truststore:truststore/certificate-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1320 | "certificate-bag[name='%s']/certificate[name='%s']", cert_bag_name, cert_name); |
| 1321 | } else { |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1322 | return nc_server_config_delete(config, "/ietf-truststore:truststore/certificate-bags/" |
roman | 12c3d52 | 2023-07-26 13:39:30 +0200 | [diff] [blame] | 1323 | "certificate-bag[name='%s']/certificate", cert_bag_name); |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1327 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1328 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1329 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1330 | nc_server_config_add_ch_persistent(const struct ly_ctx *ctx, const char *ch_client_name, struct lyd_node **config) |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1331 | { |
| 1332 | NC_CHECK_ARG_RET(NULL, ctx, ch_client_name, config, 1); |
| 1333 | |
| 1334 | /* delete periodic tree if exists */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1335 | if (nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1336 | "netconf-client[name='%s']/connection-type/periodic", ch_client_name)) { |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1340 | return nc_server_config_create(ctx, config, NULL, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1341 | "netconf-client[name='%s']/connection-type/persistent", ch_client_name); |
| 1342 | } |
| 1343 | |
| 1344 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1345 | nc_server_config_add_ch_period(const struct ly_ctx *ctx, const char *ch_client_name, uint16_t period, |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1346 | struct lyd_node **config) |
| 1347 | { |
| 1348 | char buf[6] = {0}; |
| 1349 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1350 | NC_CHECK_ARG_RET(NULL, ctx, ch_client_name, config, 1); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1351 | |
| 1352 | /* delete persistent tree if exists */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1353 | if (nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1354 | "netconf-client[name='%s']/connection-type/persistent", ch_client_name)) { |
| 1355 | return 1; |
| 1356 | } |
| 1357 | |
roman | 5d9fc73 | 2023-10-26 11:26:57 +0200 | [diff] [blame] | 1358 | sprintf(buf, "%" PRIu16, period); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1359 | return nc_server_config_create(ctx, config, buf, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1360 | "netconf-client[name='%s']/connection-type/periodic/period", ch_client_name); |
| 1361 | } |
| 1362 | |
| 1363 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1364 | nc_server_config_del_ch_period(const char *ch_client_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1365 | { |
| 1366 | NC_CHECK_ARG_RET(NULL, ch_client_name, config, 1); |
| 1367 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1368 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1369 | "netconf-client[name='%s']/connection-type/periodic/period", ch_client_name); |
| 1370 | } |
| 1371 | |
| 1372 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1373 | nc_server_config_add_ch_anchor_time(const struct ly_ctx *ctx, const char *ch_client_name, |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1374 | const char *anchor_time, struct lyd_node **config) |
| 1375 | { |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1376 | NC_CHECK_ARG_RET(NULL, ctx, ch_client_name, anchor_time, config, 1); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1377 | |
| 1378 | /* delete persistent tree if exists */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1379 | if (nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1380 | "netconf-client[name='%s']/connection-type/persistent", ch_client_name)) { |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1384 | return nc_server_config_create(ctx, config, anchor_time, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1385 | "netconf-client[name='%s']/connection-type/periodic/anchor-time", ch_client_name); |
| 1386 | } |
| 1387 | |
| 1388 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1389 | nc_server_config_del_ch_anchor_time(const char *ch_client_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1390 | { |
| 1391 | NC_CHECK_ARG_RET(NULL, ch_client_name, config, 1); |
| 1392 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1393 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1394 | "netconf-client[name='%s']/connection-type/periodic/anchor-time", ch_client_name); |
| 1395 | } |
| 1396 | |
| 1397 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1398 | nc_server_config_add_ch_idle_timeout(const struct ly_ctx *ctx, const char *ch_client_name, |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1399 | uint16_t idle_timeout, struct lyd_node **config) |
| 1400 | { |
| 1401 | char buf[6] = {0}; |
| 1402 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 1403 | NC_CHECK_ARG_RET(NULL, ctx, ch_client_name, config, 1); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1404 | |
| 1405 | /* delete persistent tree if exists */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1406 | if (nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1407 | "netconf-client[name='%s']/connection-type/persistent", ch_client_name)) { |
| 1408 | return 1; |
| 1409 | } |
| 1410 | |
roman | 5d9fc73 | 2023-10-26 11:26:57 +0200 | [diff] [blame] | 1411 | sprintf(buf, "%" PRIu16, idle_timeout); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1412 | return nc_server_config_create(ctx, config, buf, "/ietf-netconf-server:netconf-server/call-home/" |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1413 | "netconf-client[name='%s']/connection-type/periodic/idle-timeout", ch_client_name); |
| 1414 | } |
| 1415 | |
| 1416 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1417 | nc_server_config_del_ch_idle_timeout(const char *ch_client_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1418 | { |
| 1419 | NC_CHECK_ARG_RET(NULL, ch_client_name, config, 1); |
| 1420 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1421 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1422 | "netconf-client[name='%s']/connection-type/periodic/idle-timeout", ch_client_name); |
| 1423 | } |
| 1424 | |
| 1425 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1426 | nc_server_config_add_ch_reconnect_strategy(const struct ly_ctx *ctx, const char *ch_client_name, |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1427 | NC_CH_START_WITH start_with, uint16_t max_wait, uint8_t max_attempts, struct lyd_node **config) |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1428 | { |
| 1429 | int ret = 0; |
| 1430 | char *path = NULL; |
| 1431 | char buf[6] = {0}; |
| 1432 | const char *start_with_val; |
| 1433 | |
| 1434 | NC_CHECK_ARG_RET(NULL, ctx, ch_client_name, config, 1); |
| 1435 | |
| 1436 | /* prepared the path */ |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1437 | ret = asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/reconnect-strategy", ch_client_name); |
| 1438 | NC_CHECK_ERRMEM_GOTO(ret == -1, path = NULL; ret = 1, cleanup); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1439 | |
roman | ded5258 | 2023-11-08 15:21:30 +0100 | [diff] [blame] | 1440 | /* get string value from enum */ |
| 1441 | if (start_with == NC_CH_FIRST_LISTED) { |
| 1442 | start_with_val = "first-listed"; |
| 1443 | } else if (start_with == NC_CH_LAST_CONNECTED) { |
| 1444 | start_with_val = "last-connected"; |
| 1445 | } else if (start_with == NC_CH_RANDOM) { |
| 1446 | start_with_val = "random-selection"; |
| 1447 | } else { |
| 1448 | ERR(NULL, "Unknown reconnect strategy."); |
| 1449 | goto cleanup; |
| 1450 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1451 | |
roman | ded5258 | 2023-11-08 15:21:30 +0100 | [diff] [blame] | 1452 | ret = nc_server_config_append(ctx, path, "start-with", start_with_val, config); |
| 1453 | if (ret) { |
| 1454 | goto cleanup; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | if (max_attempts) { |
roman | 5d9fc73 | 2023-10-26 11:26:57 +0200 | [diff] [blame] | 1458 | sprintf(buf, "%" PRIu8, max_attempts); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1459 | ret = nc_server_config_append(ctx, path, "max-attempts", buf, config); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1460 | if (ret) { |
| 1461 | goto cleanup; |
| 1462 | } |
| 1463 | memset(buf, 0, 6); |
| 1464 | } |
| 1465 | |
| 1466 | if (max_wait) { |
roman | 5d9fc73 | 2023-10-26 11:26:57 +0200 | [diff] [blame] | 1467 | sprintf(buf, "%" PRIu16, max_wait); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1468 | ret = nc_server_config_append(ctx, path, "max-wait", buf, config); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1469 | if (ret) { |
| 1470 | goto cleanup; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | cleanup: |
| 1475 | free(path); |
| 1476 | return ret; |
| 1477 | } |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1478 | |
| 1479 | API int |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1480 | nc_server_config_del_ch_reconnect_strategy(const char *ch_client_name, struct lyd_node **config) |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1481 | { |
| 1482 | NC_CHECK_ARG_RET(NULL, ch_client_name, config, 1); |
| 1483 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1484 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 1485 | "netconf-client[name='%s']/reconnect-strategy", ch_client_name); |
| 1486 | } |