Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file server_config_util_tls.c |
| 3 | * @author Roman Janota <janota@cesnet.cz> |
| 4 | * @brief libnetconf2 server TLS configuration utilities |
| 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 | |
| 18 | #include "server_config_util.h" |
| 19 | |
| 20 | #include <stdarg.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <libyang/libyang.h> |
| 27 | |
| 28 | #include "compat.h" |
| 29 | #include "config.h" |
| 30 | #include "log_p.h" |
| 31 | #include "server_config.h" |
| 32 | #include "session.h" |
| 33 | #include "session_p.h" |
| 34 | |
| 35 | static int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 36 | _nc_server_config_add_tls_server_cert(const struct ly_ctx *ctx, const char *tree_path, const char *privkey_path, |
| 37 | const char *pubkey_path, const char *cert_path, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 38 | { |
| 39 | int ret = 0; |
| 40 | char *privkey = NULL, *pubkey = NULL, *cert = NULL; |
| 41 | NC_PRIVKEY_FORMAT privkey_type; |
| 42 | const char *privkey_format, *pubkey_format = "ietf-crypto-types:subject-public-key-info-format"; |
| 43 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 44 | NC_CHECK_ARG_RET(NULL, ctx, tree_path, privkey_path, cert_path, config, 1); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 45 | |
| 46 | /* get the keys as a string from the given files */ |
| 47 | ret = nc_server_config_util_get_asym_key_pair(privkey_path, pubkey_path, NC_PUBKEY_FORMAT_X509, &privkey, &privkey_type, &pubkey); |
| 48 | if (ret) { |
| 49 | ERR(NULL, "Getting keys from file(s) failed."); |
| 50 | goto cleanup; |
| 51 | } |
| 52 | |
| 53 | /* get cert data from file */ |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 54 | ret = nc_server_config_util_read_certificate(cert_path, &cert); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 55 | if (ret) { |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 56 | ERR(NULL, "Getting certificate from file \"%s\" failed.", cert_path); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 57 | goto cleanup; |
| 58 | } |
| 59 | |
| 60 | /* get privkey identityref value */ |
| 61 | privkey_format = nc_server_config_util_privkey_format_to_identityref(privkey_type); |
| 62 | if (!privkey_format) { |
| 63 | ret = 1; |
| 64 | goto cleanup; |
| 65 | } |
| 66 | |
| 67 | ret = nc_server_config_append(ctx, tree_path, "inline-definition/public-key-format", pubkey_format, config); |
| 68 | if (ret) { |
| 69 | goto cleanup; |
| 70 | } |
| 71 | |
| 72 | ret = nc_server_config_append(ctx, tree_path, "inline-definition/public-key", pubkey, config); |
| 73 | if (ret) { |
| 74 | goto cleanup; |
| 75 | } |
| 76 | |
| 77 | ret = nc_server_config_append(ctx, tree_path, "inline-definition/private-key-format", privkey_format, config); |
| 78 | if (ret) { |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 82 | ret = nc_server_config_append(ctx, tree_path, "inline-definition/cleartext-private-key", privkey, config); |
| 83 | if (ret) { |
| 84 | goto cleanup; |
| 85 | } |
| 86 | |
| 87 | ret = nc_server_config_append(ctx, tree_path, "inline-definition/cert-data", cert, config); |
| 88 | if (ret) { |
| 89 | goto cleanup; |
| 90 | } |
| 91 | |
| 92 | /* delete keystore if present */ |
| 93 | ret = nc_server_config_check_delete(config, "%s/keystore-reference", tree_path); |
| 94 | if (ret) { |
| 95 | goto cleanup; |
| 96 | } |
| 97 | |
| 98 | cleanup: |
| 99 | free(privkey); |
| 100 | free(pubkey); |
| 101 | free(cert); |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 106 | nc_server_config_add_tls_server_cert(const struct ly_ctx *ctx, const char *endpt_name, const char *privkey_path, |
| 107 | const char *pubkey_path, const char *cert_path, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 108 | { |
| 109 | int ret = 0; |
| 110 | char *path = NULL; |
| 111 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 112 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, privkey_path, cert_path, config, 1); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 113 | |
| 114 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/" |
| 115 | "tls/tls-server-parameters/server-identity/certificate", endpt_name) == -1) { |
| 116 | ERRMEM; |
| 117 | path = NULL; |
| 118 | ret = 1; |
| 119 | goto cleanup; |
| 120 | } |
| 121 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 122 | ret = _nc_server_config_add_tls_server_cert(ctx, path, privkey_path, pubkey_path, |
| 123 | cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 124 | if (ret) { |
| 125 | ERR(NULL, "Creating new TLS server certificate YANG data failed."); |
| 126 | goto cleanup; |
| 127 | } |
| 128 | |
| 129 | cleanup: |
| 130 | free(path); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 135 | nc_server_config_del_tls_server_cert(const char *endpt_name, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 136 | { |
| 137 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 138 | |
| 139 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/" |
| 140 | "tls/tls-server-parameters/server-identity/certificate/inline-definition", endpt_name); |
| 141 | } |
| 142 | |
| 143 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 144 | nc_server_config_add_ch_tls_server_cert(const struct ly_ctx *ctx, const char *client_name, const char *endpt_name, |
| 145 | const char *privkey_path, const char *pubkey_path, const char *cert_path, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 146 | { |
| 147 | int ret = 0; |
| 148 | char *path = NULL; |
| 149 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 150 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, privkey_path, cert_path, config, 1); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 151 | |
| 152 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/" |
| 153 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/server-identity/" |
| 154 | "certificate", client_name, endpt_name) == -1) { |
| 155 | ERRMEM; |
| 156 | path = NULL; |
| 157 | ret = 1; |
| 158 | goto cleanup; |
| 159 | } |
| 160 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 161 | ret = _nc_server_config_add_tls_server_cert(ctx, path, privkey_path, pubkey_path, |
| 162 | cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 163 | if (ret) { |
| 164 | ERR(NULL, "Creating new CH TLS server certificate YANG data failed."); |
| 165 | goto cleanup; |
| 166 | } |
| 167 | |
| 168 | cleanup: |
| 169 | free(path); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 174 | nc_server_config_del_ch_tls_server_cert(const char *client_name, const char *endpt_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 175 | struct lyd_node **config) |
| 176 | { |
| 177 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 178 | |
| 179 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
| 180 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/server-identity/" |
| 181 | "certificate/inline-definition", client_name, endpt_name); |
| 182 | } |
| 183 | |
| 184 | static int |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 185 | _nc_server_config_add_tls_keystore_ref(const struct ly_ctx *ctx, const char *tree_path, const char *asym_key_ref, |
| 186 | const char *cert_ref, struct lyd_node **config) |
| 187 | { |
| 188 | int ret = 0; |
| 189 | |
| 190 | /* create asymmetric key pair reference */ |
| 191 | ret = nc_server_config_append(ctx, tree_path, "keystore-reference/asymmetric-key", asym_key_ref, config); |
| 192 | if (ret) { |
| 193 | goto cleanup; |
| 194 | } |
| 195 | |
| 196 | /* create cert reference, this cert has to belong to the asym key */ |
| 197 | ret = nc_server_config_append(ctx, tree_path, "keystore-reference/certificate", cert_ref, config); |
| 198 | if (ret) { |
| 199 | goto cleanup; |
| 200 | } |
| 201 | |
| 202 | /* delete inline definition if present */ |
| 203 | ret = nc_server_config_check_delete(config, "%s/inline-definition", tree_path); |
| 204 | if (ret) { |
| 205 | goto cleanup; |
| 206 | } |
| 207 | |
| 208 | cleanup: |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | API int |
| 213 | nc_server_config_add_tls_keystore_ref(const struct ly_ctx *ctx, const char *endpt_name, const char *asym_key_ref, |
| 214 | const char *cert_ref, struct lyd_node **config) |
| 215 | { |
| 216 | int ret = 0; |
| 217 | char *path = NULL; |
| 218 | |
| 219 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, asym_key_ref, cert_ref, config, 1); |
| 220 | |
| 221 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/" |
| 222 | "tls/tls-server-parameters/server-identity/certificate", endpt_name) == -1) { |
| 223 | ERRMEM; |
| 224 | path = NULL; |
| 225 | ret = 1; |
| 226 | goto cleanup; |
| 227 | } |
| 228 | |
| 229 | ret = _nc_server_config_add_tls_keystore_ref(ctx, path, asym_key_ref, cert_ref, config); |
| 230 | if (ret) { |
| 231 | goto cleanup; |
| 232 | } |
| 233 | |
| 234 | cleanup: |
| 235 | free(path); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | API int |
| 240 | nc_server_config_del_tls_keystore_ref(const char *endpt_name, struct lyd_node **config) |
| 241 | { |
| 242 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 243 | |
| 244 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/" |
| 245 | "tls/tls-server-parameters/server-identity/certificate/keystore-reference", endpt_name); |
| 246 | } |
| 247 | |
| 248 | API int |
| 249 | nc_server_config_add_ch_tls_keystore_ref(const struct ly_ctx *ctx, const char *client_name, |
| 250 | const char *endpt_name, const char *asym_key_ref, const char *cert_ref, struct lyd_node **config) |
| 251 | { |
| 252 | int ret = 0; |
| 253 | char *path = NULL; |
| 254 | |
| 255 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, asym_key_ref, cert_ref, config, 1); |
| 256 | |
| 257 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/endpoints/" |
| 258 | "endpoint[name='%s']/tls/tls-server-parameters/server-identity/certificate", client_name, endpt_name) == -1) { |
| 259 | ERRMEM; |
| 260 | path = NULL; |
| 261 | ret = 1; |
| 262 | goto cleanup; |
| 263 | } |
| 264 | |
| 265 | ret = _nc_server_config_add_tls_keystore_ref(ctx, path, asym_key_ref, cert_ref, config); |
| 266 | if (ret) { |
| 267 | goto cleanup; |
| 268 | } |
| 269 | |
| 270 | cleanup: |
| 271 | free(path); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | API int |
| 276 | nc_server_config_del_ch_tls_keystore_ref(const char *client_name, const char *endpt_name, |
| 277 | struct lyd_node **config) |
| 278 | { |
| 279 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 280 | |
| 281 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 282 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/server-identity/certificate/" |
| 283 | "keystore-reference", client_name, endpt_name); |
| 284 | } |
| 285 | |
| 286 | static int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 287 | _nc_server_config_add_tls_client_cert(const struct ly_ctx *ctx, const char *tree_path, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 288 | const char *cert_path, struct lyd_node **config) |
| 289 | { |
| 290 | int ret = 0; |
| 291 | char *cert = NULL; |
| 292 | |
| 293 | NC_CHECK_ARG_RET(NULL, ctx, tree_path, cert_path, config, 1); |
| 294 | |
| 295 | ret = nc_server_config_util_read_certificate(cert_path, &cert); |
| 296 | if (ret) { |
| 297 | ERR(NULL, "Getting certificate from file \"%s\" failed.", cert_path); |
| 298 | goto cleanup; |
| 299 | } |
| 300 | |
| 301 | ret = nc_server_config_append(ctx, tree_path, "cert-data", cert, config); |
| 302 | if (ret) { |
| 303 | goto cleanup; |
| 304 | } |
| 305 | |
| 306 | cleanup: |
| 307 | free(cert); |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 312 | nc_server_config_add_tls_client_cert(const struct ly_ctx *ctx, const char *endpt_name, const char *cert_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 313 | const char *cert_path, struct lyd_node **config) |
| 314 | { |
| 315 | int ret = 0; |
| 316 | char *path = NULL; |
| 317 | |
| 318 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, cert_name, cert_path, config, 1); |
| 319 | |
| 320 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 321 | "client-authentication/ee-certs/inline-definition/certificate[name='%s']", endpt_name, cert_name) == -1) { |
| 322 | ERRMEM; |
| 323 | path = NULL; |
| 324 | ret = 1; |
| 325 | goto cleanup; |
| 326 | } |
| 327 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 328 | ret = _nc_server_config_add_tls_client_cert(ctx, path, cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 329 | if (ret) { |
| 330 | ERR(NULL, "Creating new TLS client certificate YANG data failed."); |
| 331 | goto cleanup; |
| 332 | } |
| 333 | |
| 334 | /* delete truststore if present */ |
| 335 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 336 | "client-authentication/ee-certs/truststore-reference", endpt_name); |
| 337 | if (ret) { |
| 338 | goto cleanup; |
| 339 | } |
| 340 | |
| 341 | cleanup: |
| 342 | free(path); |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 347 | nc_server_config_del_tls_client_cert(const char *endpt_name, const char *cert_name, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 348 | { |
| 349 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 350 | |
| 351 | if (cert_name) { |
| 352 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 353 | "tls-server-parameters/client-authentication/ee-certs/inline-definition/" |
| 354 | "certificate[name='%s']", endpt_name, cert_name); |
| 355 | } else { |
| 356 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 357 | "tls-server-parameters/client-authentication/ee-certs/inline-definition/" |
| 358 | "certificate", endpt_name); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 363 | nc_server_config_add_ch_tls_client_cert(const struct ly_ctx *ctx, const char *client_name, const char *endpt_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 364 | const char *cert_name, const char *cert_path, struct lyd_node **config) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | char *path = NULL; |
| 368 | |
| 369 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, cert_name, cert_path, config, 1); |
| 370 | |
| 371 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 372 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ee-certs/" |
| 373 | "inline-definition/certificate[name='%s']", client_name, endpt_name, cert_name) == -1) { |
| 374 | ERRMEM; |
| 375 | path = NULL; |
| 376 | ret = 1; |
| 377 | goto cleanup; |
| 378 | } |
| 379 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 380 | ret = _nc_server_config_add_tls_client_cert(ctx, path, cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 381 | if (ret) { |
| 382 | ERR(NULL, "Creating new CH TLS client certificate YANG data failed."); |
| 383 | goto cleanup; |
| 384 | } |
| 385 | |
| 386 | /* delete truststore if present */ |
| 387 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 388 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 389 | "client-authentication/ee-certs/truststore-reference", client_name, endpt_name); |
| 390 | if (ret) { |
| 391 | goto cleanup; |
| 392 | } |
| 393 | |
| 394 | cleanup: |
| 395 | free(path); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 400 | nc_server_config_del_ch_tls_client_cert(const char *client_name, const char *endpt_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 401 | const char *cert_name, struct lyd_node **config) |
| 402 | { |
| 403 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 404 | |
| 405 | if (cert_name) { |
| 406 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 407 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ee-certs/" |
| 408 | "inline-definition/certificate[name='%s']", client_name, endpt_name, cert_name); |
| 409 | } else { |
| 410 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 411 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ee-certs/" |
| 412 | "inline-definition/certificate", client_name, endpt_name); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | API int |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 417 | nc_server_config_add_tls_client_cert_truststore_ref(const struct ly_ctx *ctx, const char *endpt_name, |
| 418 | const char *cert_bag_ref, struct lyd_node **config) |
| 419 | { |
| 420 | int ret = 0; |
| 421 | |
| 422 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, cert_bag_ref, config, 1); |
| 423 | |
| 424 | ret = nc_server_config_create(ctx, config, cert_bag_ref, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 425 | "tls-server-parameters/client-authentication/ee-certs/truststore-reference", endpt_name); |
| 426 | if (ret) { |
| 427 | goto cleanup; |
| 428 | } |
| 429 | |
| 430 | /* delete inline definition if present */ |
| 431 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 432 | "tls-server-parameters/client-authentication/ee-certs/inline-definition", endpt_name); |
| 433 | if (ret) { |
| 434 | goto cleanup; |
| 435 | } |
| 436 | |
| 437 | cleanup: |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | API int |
| 442 | nc_server_config_del_tls_client_cert_truststore_ref(const char *endpt_name, struct lyd_node **config) |
| 443 | { |
| 444 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 445 | |
| 446 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 447 | "tls-server-parameters/client-authentication/ee-certs/truststore-reference", endpt_name); |
| 448 | } |
| 449 | |
| 450 | API int |
| 451 | nc_server_config_add_ch_tls_client_cert_truststore_ref(const struct ly_ctx *ctx, const char *client_name, |
| 452 | const char *endpt_name, const char *cert_bag_ref, struct lyd_node **config) |
| 453 | { |
| 454 | int ret = 0; |
| 455 | |
| 456 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, cert_bag_ref, config, 1); |
| 457 | |
| 458 | ret = nc_server_config_create(ctx, config, cert_bag_ref, "/ietf-netconf-server:netconf-server/call-home/" |
| 459 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 460 | "client-authentication/ee-certs/truststore-reference", client_name, endpt_name); |
| 461 | if (ret) { |
| 462 | goto cleanup; |
| 463 | } |
| 464 | |
| 465 | /* delete inline definition if present */ |
| 466 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
| 467 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/" |
| 468 | "tls-server-parameters/client-authentication/ee-certs/inline-definition", client_name, endpt_name); |
| 469 | if (ret) { |
| 470 | goto cleanup; |
| 471 | } |
| 472 | |
| 473 | cleanup: |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | API int |
| 478 | nc_server_config_del_ch_tls_client_cert_truststore_ref(const char *client_name, const char *endpt_name, |
| 479 | struct lyd_node **config) |
| 480 | { |
| 481 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 482 | |
| 483 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
| 484 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 485 | "client-authentication/ee-certs/truststore-reference", client_name, endpt_name); |
| 486 | } |
| 487 | |
| 488 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 489 | nc_server_config_add_tls_ca_cert(const struct ly_ctx *ctx, const char *endpt_name, const char *cert_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 490 | const char *cert_path, struct lyd_node **config) |
| 491 | { |
| 492 | int ret = 0; |
| 493 | char *path = NULL; |
| 494 | |
| 495 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, cert_name, cert_path, config, 1); |
| 496 | |
| 497 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 498 | "client-authentication/ca-certs/inline-definition/certificate[name='%s']", endpt_name, cert_name) == -1) { |
| 499 | ERRMEM; |
| 500 | path = NULL; |
| 501 | ret = 1; |
| 502 | goto cleanup; |
| 503 | } |
| 504 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 505 | ret = _nc_server_config_add_tls_client_cert(ctx, path, cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 506 | if (ret) { |
| 507 | ERR(NULL, "Creating new TLS client certificate authority YANG data failed."); |
| 508 | goto cleanup; |
| 509 | } |
| 510 | |
| 511 | /* delete truststore if present */ |
| 512 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 513 | "client-authentication/ca-certs/truststore-reference", endpt_name); |
| 514 | if (ret) { |
| 515 | goto cleanup; |
| 516 | } |
| 517 | |
| 518 | cleanup: |
| 519 | free(path); |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 524 | nc_server_config_del_tls_ca_cert(const char *endpt_name, const char *cert_name, struct lyd_node **config) |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 525 | { |
| 526 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 527 | |
| 528 | if (cert_name) { |
| 529 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 530 | "tls-server-parameters/client-authentication/ca-certs/inline-definition/" |
| 531 | "certificate[name='%s']", endpt_name, cert_name); |
| 532 | } else { |
| 533 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 534 | "tls-server-parameters/client-authentication/ca-certs/inline-definition/" |
| 535 | "certificate", endpt_name); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 540 | nc_server_config_add_ch_tls_ca_cert(const struct ly_ctx *ctx, const char *client_name, const char *endpt_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 541 | const char *cert_name, const char *cert_path, struct lyd_node **config) |
| 542 | { |
| 543 | int ret = 0; |
| 544 | char *path = NULL; |
| 545 | |
| 546 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, cert_name, cert_path, config, 1); |
| 547 | |
| 548 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 549 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ca-certs/" |
| 550 | "inline-definition/certificate[name='%s']", client_name, endpt_name, cert_name) == -1) { |
| 551 | ERRMEM; |
| 552 | path = NULL; |
| 553 | ret = 1; |
| 554 | goto cleanup; |
| 555 | } |
| 556 | |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 557 | ret = _nc_server_config_add_tls_client_cert(ctx, path, cert_path, config); |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 558 | if (ret) { |
| 559 | ERR(NULL, "Creating new CH TLS client certificate authority YANG data failed."); |
| 560 | goto cleanup; |
| 561 | } |
| 562 | |
| 563 | /* delete truststore if present */ |
| 564 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 565 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 566 | "client-authentication/ca-certs/truststore-reference", client_name, endpt_name); |
| 567 | if (ret) { |
| 568 | goto cleanup; |
| 569 | } |
| 570 | |
| 571 | cleanup: |
| 572 | free(path); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 577 | nc_server_config_del_ch_tls_ca_cert(const char *client_name, const char *endpt_name, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 578 | const char *cert_name, struct lyd_node **config) |
| 579 | { |
| 580 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 581 | |
| 582 | if (cert_name) { |
| 583 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 584 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ca-certs/" |
| 585 | "inline-definition/certificate[name='%s']", client_name, endpt_name, cert_name); |
| 586 | } else { |
| 587 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 588 | "endpoints/endpoint[name='%s']/tls/tls-server-parameters/client-authentication/ca-certs/" |
| 589 | "inline-definition/certificate", client_name, endpt_name); |
| 590 | } |
| 591 | } |
| 592 | |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 593 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 594 | nc_server_config_add_tls_ca_cert_truststore_ref(const struct ly_ctx *ctx, const char *endpt_name, |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 595 | const char *cert_bag_ref, struct lyd_node **config) |
| 596 | { |
| 597 | int ret = 0; |
| 598 | |
| 599 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, cert_bag_ref, config, 1); |
| 600 | |
| 601 | ret = nc_server_config_create(ctx, config, cert_bag_ref, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 602 | "tls-server-parameters/client-authentication/ca-certs/truststore-reference", endpt_name); |
| 603 | if (ret) { |
| 604 | goto cleanup; |
| 605 | } |
| 606 | |
| 607 | /* delete inline definition if present */ |
| 608 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 609 | "tls-server-parameters/client-authentication/ca-certs/inline-definition", endpt_name); |
| 610 | if (ret) { |
| 611 | goto cleanup; |
| 612 | } |
| 613 | |
| 614 | cleanup: |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 619 | nc_server_config_del_tls_ca_cert_truststore_ref(const char *endpt_name, struct lyd_node **config) |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 620 | { |
| 621 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 622 | |
| 623 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 624 | "tls-server-parameters/client-authentication/ca-certs/truststore-reference", endpt_name); |
| 625 | } |
| 626 | |
| 627 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 628 | nc_server_config_add_ch_tls_ca_cert_truststore_ref(const struct ly_ctx *ctx, const char *client_name, |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 629 | const char *endpt_name, const char *cert_bag_ref, struct lyd_node **config) |
| 630 | { |
| 631 | int ret = 0; |
| 632 | |
| 633 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, cert_bag_ref, config, 1); |
| 634 | |
| 635 | ret = nc_server_config_create(ctx, config, cert_bag_ref, "/ietf-netconf-server:netconf-server/call-home/" |
| 636 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 637 | "client-authentication/ca-certs/truststore-reference", client_name, endpt_name); |
| 638 | if (ret) { |
| 639 | goto cleanup; |
| 640 | } |
| 641 | |
| 642 | /* delete inline definition if present */ |
| 643 | ret = nc_server_config_check_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
| 644 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 645 | "client-authentication/ca-certs/inline-definition", client_name, endpt_name); |
| 646 | if (ret) { |
| 647 | goto cleanup; |
| 648 | } |
| 649 | |
| 650 | cleanup: |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | API int |
roman | e6ec60e | 2023-10-19 15:21:52 +0200 | [diff] [blame^] | 655 | nc_server_config_del_ch_tls_ca_cert_truststore_ref(const char *client_name, const char *endpt_name, |
roman | d348b94 | 2023-10-13 14:32:19 +0200 | [diff] [blame] | 656 | struct lyd_node **config) |
| 657 | { |
| 658 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 659 | |
| 660 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/" |
| 661 | "netconf-client[name='%s']/endpoints/endpoint[name='%s']/tls/tls-server-parameters/" |
| 662 | "client-authentication/ca-certs/truststore-reference", client_name, endpt_name); |
| 663 | } |
| 664 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 665 | static const char * |
| 666 | nc_server_config_tls_maptype2str(NC_TLS_CTN_MAPTYPE map_type) |
| 667 | { |
| 668 | switch (map_type) { |
| 669 | case NC_TLS_CTN_SPECIFIED: |
| 670 | return "ietf-x509-cert-to-name:specified"; |
| 671 | case NC_TLS_CTN_SAN_RFC822_NAME: |
| 672 | return "ietf-x509-cert-to-name:san-rfc822-name"; |
| 673 | case NC_TLS_CTN_SAN_DNS_NAME: |
| 674 | return "ietf-x509-cert-to-name:san-dns-name"; |
| 675 | case NC_TLS_CTN_SAN_IP_ADDRESS: |
| 676 | return "ietf-x509-cert-to-name:san-ip-address"; |
| 677 | case NC_TLS_CTN_SAN_ANY: |
| 678 | return "ietf-x509-cert-to-name:san-any"; |
| 679 | case NC_TLS_CTN_COMMON_NAME: |
| 680 | return "ietf-x509-cert-to-name:common-name"; |
| 681 | case NC_TLS_CTN_UNKNOWN: |
| 682 | default: |
| 683 | ERR(NULL, "Unknown CTN mapping type."); |
| 684 | return NULL; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static int |
| 689 | _nc_server_config_add_tls_ctn(const struct ly_ctx *ctx, const char *tree_path, const char *fingerprint, |
| 690 | NC_TLS_CTN_MAPTYPE map_type, const char *name, struct lyd_node **config) |
| 691 | { |
| 692 | int ret = 0; |
| 693 | const char *map; |
| 694 | |
| 695 | NC_CHECK_ARG_RET(NULL, ctx, tree_path, name, config, 1); |
| 696 | |
| 697 | if (fingerprint) { |
| 698 | /* optional */ |
| 699 | ret = nc_server_config_append(ctx, tree_path, "fingerprint", fingerprint, config); |
| 700 | if (ret) { |
| 701 | goto cleanup; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* get map str */ |
| 706 | map = nc_server_config_tls_maptype2str(map_type); |
| 707 | if (!map) { |
| 708 | ret = 1; |
| 709 | goto cleanup; |
| 710 | } |
| 711 | |
| 712 | ret = nc_server_config_append(ctx, tree_path, "map-type", map, config); |
| 713 | if (ret) { |
| 714 | goto cleanup; |
| 715 | } |
| 716 | |
| 717 | ret = nc_server_config_append(ctx, tree_path, "name", name, config); |
| 718 | if (ret) { |
| 719 | goto cleanup; |
| 720 | } |
| 721 | |
| 722 | cleanup: |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | API int |
| 727 | nc_server_config_add_tls_ctn(const struct ly_ctx *ctx, const char *endpt_name, uint32_t id, const char *fingerprint, |
| 728 | NC_TLS_CTN_MAPTYPE map_type, const char *name, struct lyd_node **config) |
| 729 | { |
| 730 | int ret = 0; |
| 731 | char *path = NULL; |
| 732 | |
| 733 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, id, name, config, 1); |
| 734 | |
| 735 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/netconf-server-parameters/" |
| 736 | "client-identity-mappings/cert-to-name[id='%u']", endpt_name, id) == -1) { |
| 737 | ERRMEM; |
| 738 | path = NULL; |
| 739 | ret = 1; |
| 740 | goto cleanup; |
| 741 | } |
| 742 | |
| 743 | ret = _nc_server_config_add_tls_ctn(ctx, path, fingerprint, map_type, name, config); |
| 744 | if (ret) { |
| 745 | ERR(NULL, "Creating new TLS cert-to-name YANG data failed."); |
| 746 | goto cleanup; |
| 747 | } |
| 748 | |
| 749 | cleanup: |
| 750 | free(path); |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | API int |
| 755 | nc_server_config_del_tls_ctn(const char *endpt_name, uint32_t id, struct lyd_node **config) |
| 756 | { |
| 757 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 758 | |
| 759 | if (id) { |
| 760 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 761 | "netconf-server-parameters/client-identity-mappings/cert-to-name[id='%u']", endpt_name, id); |
| 762 | } else { |
| 763 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/" |
| 764 | "netconf-server-parameters/client-identity-mappings/cert-to-name", endpt_name); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | API int |
| 769 | nc_server_config_add_ch_tls_ctn(const struct ly_ctx *ctx, const char *client_name, const char *endpt_name, |
| 770 | uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name, struct lyd_node **config) |
| 771 | { |
| 772 | int ret = 0; |
| 773 | char *path = NULL; |
| 774 | |
| 775 | NC_CHECK_ARG_RET(NULL, ctx, client_name, endpt_name, id, name, config, 1); |
| 776 | |
| 777 | if (asprintf(&path, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 778 | "endpoints/endpoint[name='%s']/tls/netconf-server-parameters/client-identity-mappings/" |
| 779 | "cert-to-name[id='%u']", client_name, endpt_name, id) == -1) { |
| 780 | ERRMEM; |
| 781 | path = NULL; |
| 782 | ret = 1; |
| 783 | goto cleanup; |
| 784 | } |
| 785 | |
| 786 | ret = _nc_server_config_add_tls_ctn(ctx, path, fingerprint, map_type, name, config); |
| 787 | if (ret) { |
| 788 | ERR(NULL, "Creating new CH TLS cert-to-name YANG data failed."); |
| 789 | goto cleanup; |
| 790 | } |
| 791 | |
| 792 | cleanup: |
| 793 | free(path); |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | API int |
| 798 | nc_server_config_del_ch_tls_ctn(const char *client_name, const char *endpt_name, |
| 799 | uint32_t id, struct lyd_node **config) |
| 800 | { |
| 801 | NC_CHECK_ARG_RET(NULL, client_name, endpt_name, config, 1); |
| 802 | |
| 803 | if (id) { |
| 804 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 805 | "endpoints/endpoint[name='%s']/tls/netconf-server-parameters/client-identity-mappings/" |
| 806 | "cert-to-name[id='%u']", client_name, endpt_name, id); |
| 807 | } else { |
| 808 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='%s']/" |
| 809 | "endpoints/endpoint[name='%s']/tls/netconf-server-parameters/client-identity-mappings/" |
| 810 | "cert-to-name", client_name, endpt_name); |
| 811 | } |
| 812 | } |
| 813 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 814 | API int |
| 815 | nc_server_config_add_tls_endpoint_client_ref(const struct ly_ctx *ctx, const char *endpt_name, const char *referenced_endpt, struct lyd_node **config) |
| 816 | { |
| 817 | NC_CHECK_ARG_RET(NULL, ctx, endpt_name, referenced_endpt, config, 1); |
| 818 | |
| 819 | return nc_server_config_create(ctx, config, referenced_endpt, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 820 | "client-authentication/libnetconf2-netconf-server:endpoint-client-auth", endpt_name); |
| 821 | } |
| 822 | |
| 823 | API int |
| 824 | nc_server_config_del_tls_endpoint_client_ref(const char *endpt_name, struct lyd_node **config) |
| 825 | { |
| 826 | NC_CHECK_ARG_RET(NULL, endpt_name, config, 1); |
| 827 | |
| 828 | return nc_server_config_delete(config, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/tls/tls-server-parameters/" |
| 829 | "client-authentication/libnetconf2-netconf-server:endpoint-client-auth", endpt_name); |
| 830 | } |