roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 1 | /** |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 2 | * @file server_config_util.h |
roman | 466719d | 2023-05-05 16:14:37 +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 utlities header |
roman | 466719d | 2023-05-05 16:14:37 +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 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 16 | #ifndef NC_SERVER_CONFIG_UTIL_H_ |
| 17 | #define NC_SERVER_CONFIG_UTIL_H_ |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 18 | |
| 19 | #include <libyang/libyang.h> |
| 20 | |
| 21 | #include "session_p.h" |
| 22 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 23 | #ifdef NC_ENABLED_SSH_TLS |
| 24 | |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 25 | /* private key's pkcs8 header */ |
| 26 | #define NC_PKCS8_PRIVKEY_HEADER "-----BEGIN PRIVATE KEY-----\n" |
| 27 | |
| 28 | /* private key's pkcs8 footer */ |
| 29 | #define NC_PKCS8_PRIVKEY_FOOTER "\n-----END PRIVATE KEY-----\n" |
| 30 | |
| 31 | /* private key's openssh header */ |
| 32 | #define NC_OPENSSH_PRIVKEY_HEADER "-----BEGIN OPENSSH PRIVATE KEY-----\n" |
| 33 | |
| 34 | /* private key's openssh footer */ |
| 35 | #define NC_OPENSSH_PRIVKEY_FOOTER "\n-----END OPENSSH PRIVATE KEY-----\n" |
| 36 | |
| 37 | /* private key's pkcs1 rsa header */ |
| 38 | #define NC_PKCS1_RSA_PRIVKEY_HEADER "-----BEGIN RSA PRIVATE KEY-----\n" |
| 39 | |
roman | 44af505 | 2024-04-05 12:31:24 +0200 | [diff] [blame] | 40 | /* private key's pkcs1 rsa footer */ |
| 41 | #define NC_PKCS1_RSA_PRIVKEY_FOOTER "\n-----END RSA PRIVATE KEY-----\n" |
| 42 | |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 43 | /* private key's sec1 ec header */ |
| 44 | #define NC_SEC1_EC_PRIVKEY_HEADER "-----BEGIN EC PRIVATE KEY-----\n" |
| 45 | |
roman | 44af505 | 2024-04-05 12:31:24 +0200 | [diff] [blame] | 46 | /* private key's sec1 ec footer */ |
| 47 | #define NC_SEC1_EC_PRIVKEY_FOOTER "\n-----END EC PRIVATE KEY-----\n" |
| 48 | |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 49 | /* private key's header when getting an EC/RSA privkey from file using libssh */ |
| 50 | #define NC_LIBSSH_PRIVKEY_HEADER "-----BEGIN PRIVATE KEY-----\n" |
| 51 | |
| 52 | /* private key's footer when getting an EC/RSA privkey from file using libssh */ |
| 53 | #define NC_LIBSSH_PRIVKEY_FOOTER "\n-----END PRIVATE KEY-----\n" |
| 54 | |
| 55 | /* public key's ssh2 header */ |
| 56 | #define NC_SSH2_PUBKEY_HEADER "---- BEGIN SSH2 PUBLIC KEY ----\n" |
| 57 | |
| 58 | /* public key's SubjectPublicKeyInfo format header */ |
| 59 | #define NC_SUBJECT_PUBKEY_INFO_HEADER "-----BEGIN PUBLIC KEY-----\n" |
| 60 | |
| 61 | /* public key's SubjectPublicKeyInfo format footer */ |
| 62 | #define NC_SUBJECT_PUBKEY_INFO_FOOTER "\n-----END PUBLIC KEY-----\n" |
| 63 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 64 | /* certificate's PEM format header */ |
| 65 | #define NC_PEM_CERTIFICATE_HEADER "-----BEGIN CERTIFICATE-----\n" |
| 66 | |
| 67 | /* certificate's PEM format footer */ |
| 68 | #define NC_PEM_CERTIFICATE_FOOTER "\n-----END CERTIFICATE-----\n" |
| 69 | |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 70 | typedef enum { |
| 71 | NC_ALG_HOSTKEY, |
| 72 | NC_ALG_KEY_EXCHANGE, |
| 73 | NC_ALG_ENCRYPTION, |
| 74 | NC_ALG_MAC |
| 75 | } NC_ALG_TYPE; |
| 76 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 77 | /** |
| 78 | * @brief Gets asymmetric key pair from private key (and optionally public key) file(s). |
| 79 | * |
| 80 | * @param[in] privkey_path Path to private key. |
| 81 | * @param[in] pubkey_path Optional path to public key. If not set, PK will be generated from private key. |
| 82 | * @param[in] wanted_pubkey_type Wanted public key format to be generated (SPKI/SSH) |
| 83 | * @param[out] privkey Base64 encoded private key. |
| 84 | * @param[out] privkey_type Type of the private key. (RSA, EC, etc) |
| 85 | * @param[out] pubkey Base64 encoded public key. |
| 86 | * @return 0 on success, non-zero otherwise. |
| 87 | */ |
| 88 | int nc_server_config_util_get_asym_key_pair(const char *privkey_path, const char *pubkey_path, NC_PUBKEY_FORMAT wanted_pubkey_type, |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 89 | char **privkey, NC_PRIVKEY_FORMAT *privkey_type, char **pubkey); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 90 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 91 | /** |
| 92 | * @brief Gets public key from a file and converts it to the SSH format if need be. |
| 93 | * |
| 94 | * @param[in] pubkey_path Path to the public key. |
| 95 | * @param[out] pubkey Base64 encoded public key. |
| 96 | * |
| 97 | * @return 0 on success, non-zero otherwise. |
| 98 | */ |
| 99 | int 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] | 100 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 101 | /** |
| 102 | * @brief Gets a certificate from a file. |
| 103 | * |
| 104 | * @param[in] cert_path Path to the certificate. |
| 105 | * @param[out] cert Base64 PEM encoded certificate data. |
| 106 | * |
| 107 | * @return 0 on success, non-zero otherwise. |
| 108 | */ |
| 109 | int nc_server_config_util_read_certificate(const char *cert_path, char **cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 110 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 111 | /** |
| 112 | * @brief Converts private key format to its associated identityref value. |
| 113 | * |
| 114 | * @param[in] format Private key format. |
| 115 | * |
| 116 | * @return Identityref on success, NULL on failure. |
| 117 | */ |
| 118 | const char *nc_server_config_util_privkey_format_to_identityref(NC_PRIVKEY_FORMAT format); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 119 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 120 | #endif /* NC_ENABLED_SSH_TLS */ |
| 121 | |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 122 | /** |
| 123 | * @brief Creates YANG data nodes in a path and gives the final node a value. |
| 124 | * |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 125 | * @param[in] ctx libyang context. |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 126 | * @param[in, out] tree The YANG data tree where the insertion will happen. On success |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 127 | * this is set to the top level container. |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 128 | * @param[in] value Value assigned to the final node in the path. |
| 129 | * @param[in] path_fmt Format of the path. |
| 130 | * @param[in] ... Parameters for the path format, essentially representing the lists' keys. |
| 131 | * @return 0 on success, 1 otherwise. |
| 132 | */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 133 | int nc_server_config_create(const struct ly_ctx *ctx, struct lyd_node **tree, const char *value, const char *path_fmt, ...); |
roman | d30af55 | 2023-06-16 15:18:27 +0200 | [diff] [blame] | 134 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 135 | /** |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 136 | * @brief Creates a YANG data node by appending it to a specified parent node. |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 137 | * |
| 138 | * @param[in] ctx libyang context. |
| 139 | * @param[in] parent_path Path to the parent node. |
| 140 | * @param[in] child_name Name of the parent's child node to be created. |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 141 | * @param[in] value Value given to the child node. |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 142 | * @param[out] tree YANG data tree where the insertion will happen. On success |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 143 | * this is set to the top level container. |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 144 | * @return 0 on success, 1 otherwise. |
| 145 | */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 146 | int nc_server_config_append(const struct ly_ctx *ctx, const char *parent_path, const char *child_name, |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 147 | const char *value, struct lyd_node **tree); |
| 148 | |
| 149 | /** |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 150 | * @brief Deletes a subtree from the YANG data. |
| 151 | * |
| 152 | * @param tree YANG data from which the subtree will be deleted. |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 153 | * @param[in] path_fmt Format of the path. The last node will be the top level node of the deleted tree. |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 154 | * @param[in] ... Parameters for the path format, essentially representing the lists' keys. |
| 155 | * @return 0 on success, non-zero otherwise. |
| 156 | */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 157 | int nc_server_config_delete(struct lyd_node **tree, const char *path_fmt, ...); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 158 | |
Roytak | 7b9bf29 | 2023-10-04 14:06:38 +0200 | [diff] [blame] | 159 | /** |
| 160 | * @brief Deletes a subtree from the YANG data, but doesn't return an error if the node doesn't exist. |
| 161 | * |
| 162 | * @param tree YANG data from which the subtree will be deleted. |
| 163 | * @param[in] path_fmt Format of the path. The last node will be the top level node of the deleted tree. |
| 164 | * @param[in] ... Parameters for the path format, essentially representing the lists' keys. |
| 165 | * @return 0 on success, non-zero otherwise. |
| 166 | */ |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 167 | int nc_server_config_check_delete(struct lyd_node **tree, const char *path_fmt, ...); |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 168 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 169 | #endif /* NC_CONFIG_NEW_H_ */ |