blob: 6048fc4b31acd36c44f785b8796b36bb3eb90115 [file] [log] [blame]
roman466719d2023-05-05 16:14:37 +02001/**
Roytakb2794852023-10-18 14:30:22 +02002 * @file server_config_util.h
roman466719d2023-05-05 16:14:37 +02003 * @author Roman Janota <janota@cesnet.cz>
Roytakb2794852023-10-18 14:30:22 +02004 * @brief libnetconf2 server configuration utlities header
roman466719d2023-05-05 16:14:37 +02005 *
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
Roytakb2794852023-10-18 14:30:22 +020016#ifndef NC_SERVER_CONFIG_UTIL_H_
17#define NC_SERVER_CONFIG_UTIL_H_
roman466719d2023-05-05 16:14:37 +020018
19#include <libyang/libyang.h>
20
21#include "session_p.h"
22
roman2eab4742023-06-06 10:00:26 +020023#ifdef NC_ENABLED_SSH_TLS
24
roman466719d2023-05-05 16:14:37 +020025/* 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
40/* private key's sec1 ec header */
41#define NC_SEC1_EC_PRIVKEY_HEADER "-----BEGIN EC PRIVATE KEY-----\n"
42
43/* private key's header when getting an EC/RSA privkey from file using libssh */
44#define NC_LIBSSH_PRIVKEY_HEADER "-----BEGIN PRIVATE KEY-----\n"
45
46/* private key's footer when getting an EC/RSA privkey from file using libssh */
47#define NC_LIBSSH_PRIVKEY_FOOTER "\n-----END PRIVATE KEY-----\n"
48
49/* public key's ssh2 header */
50#define NC_SSH2_PUBKEY_HEADER "---- BEGIN SSH2 PUBLIC KEY ----\n"
51
52/* public key's SubjectPublicKeyInfo format header */
53#define NC_SUBJECT_PUBKEY_INFO_HEADER "-----BEGIN PUBLIC KEY-----\n"
54
55/* public key's SubjectPublicKeyInfo format footer */
56#define NC_SUBJECT_PUBKEY_INFO_FOOTER "\n-----END PUBLIC KEY-----\n"
57
roman3f9b65c2023-06-05 14:26:58 +020058/* certificate's PEM format header */
59#define NC_PEM_CERTIFICATE_HEADER "-----BEGIN CERTIFICATE-----\n"
60
61/* certificate's PEM format footer */
62#define NC_PEM_CERTIFICATE_FOOTER "\n-----END CERTIFICATE-----\n"
63
roman466719d2023-05-05 16:14:37 +020064typedef enum {
65 NC_ALG_HOSTKEY,
66 NC_ALG_KEY_EXCHANGE,
67 NC_ALG_ENCRYPTION,
68 NC_ALG_MAC
69} NC_ALG_TYPE;
70
Roytakb2794852023-10-18 14:30:22 +020071/**
72 * @brief Gets asymmetric key pair from private key (and optionally public key) file(s).
73 *
74 * @param[in] privkey_path Path to private key.
75 * @param[in] pubkey_path Optional path to public key. If not set, PK will be generated from private key.
76 * @param[in] wanted_pubkey_type Wanted public key format to be generated (SPKI/SSH)
77 * @param[out] privkey Base64 encoded private key.
78 * @param[out] privkey_type Type of the private key. (RSA, EC, etc)
79 * @param[out] pubkey Base64 encoded public key.
80 * @return 0 on success, non-zero otherwise.
81 */
82int nc_server_config_util_get_asym_key_pair(const char *privkey_path, const char *pubkey_path, NC_PUBKEY_FORMAT wanted_pubkey_type,
roman13145912023-08-17 15:36:54 +020083 char **privkey, NC_PRIVKEY_FORMAT *privkey_type, char **pubkey);
roman3f9b65c2023-06-05 14:26:58 +020084
Roytakb2794852023-10-18 14:30:22 +020085/**
86 * @brief Gets public key from a file and converts it to the SSH format if need be.
87 *
88 * @param[in] pubkey_path Path to the public key.
89 * @param[out] pubkey Base64 encoded public key.
90 *
91 * @return 0 on success, non-zero otherwise.
92 */
93int nc_server_config_util_get_ssh_pubkey_file(const char *pubkey_path, char **pubkey);
roman3f9b65c2023-06-05 14:26:58 +020094
Roytakb2794852023-10-18 14:30:22 +020095/**
96 * @brief Gets a certificate from a file.
97 *
98 * @param[in] cert_path Path to the certificate.
99 * @param[out] cert Base64 PEM encoded certificate data.
100 *
101 * @return 0 on success, non-zero otherwise.
102 */
103int nc_server_config_util_read_certificate(const char *cert_path, char **cert);
roman3f9b65c2023-06-05 14:26:58 +0200104
Roytakb2794852023-10-18 14:30:22 +0200105/**
106 * @brief Converts private key format to its associated identityref value.
107 *
108 * @param[in] format Private key format.
109 *
110 * @return Identityref on success, NULL on failure.
111 */
112const char *nc_server_config_util_privkey_format_to_identityref(NC_PRIVKEY_FORMAT format);
roman3f9b65c2023-06-05 14:26:58 +0200113
roman2eab4742023-06-06 10:00:26 +0200114#endif /* NC_ENABLED_SSH_TLS */
115
romand30af552023-06-16 15:18:27 +0200116/**
117 * @brief Creates YANG data nodes in a path and gives the final node a value.
118 *
romanb6f44032023-06-30 15:07:56 +0200119 * @param[in] ctx libyang context.
romand30af552023-06-16 15:18:27 +0200120 * @param[in, out] tree The YANG data tree where the insertion will happen. On success
Roytak7b9bf292023-10-04 14:06:38 +0200121 * this is set to the top level container.
romand30af552023-06-16 15:18:27 +0200122 * @param[in] value Value assigned to the final node in the path.
123 * @param[in] path_fmt Format of the path.
124 * @param[in] ... Parameters for the path format, essentially representing the lists' keys.
125 * @return 0 on success, 1 otherwise.
126 */
Roytakb2794852023-10-18 14:30:22 +0200127int nc_server_config_create(const struct ly_ctx *ctx, struct lyd_node **tree, const char *value, const char *path_fmt, ...);
romand30af552023-06-16 15:18:27 +0200128
roman142718b2023-06-29 09:15:29 +0200129/**
Roytak7b9bf292023-10-04 14:06:38 +0200130 * @brief Creates a YANG data node by appending it to a specified parent node.
romanb6f44032023-06-30 15:07:56 +0200131 *
132 * @param[in] ctx libyang context.
133 * @param[in] parent_path Path to the parent node.
134 * @param[in] child_name Name of the parent's child node to be created.
Roytak7b9bf292023-10-04 14:06:38 +0200135 * @param[in] value Value given to the child node.
romanb6f44032023-06-30 15:07:56 +0200136 * @param[out] tree YANG data tree where the insertion will happen. On success
Roytak7b9bf292023-10-04 14:06:38 +0200137 * this is set to the top level container.
romanb6f44032023-06-30 15:07:56 +0200138 * @return 0 on success, 1 otherwise.
139 */
Roytakb2794852023-10-18 14:30:22 +0200140int nc_server_config_append(const struct ly_ctx *ctx, const char *parent_path, const char *child_name,
romanb6f44032023-06-30 15:07:56 +0200141 const char *value, struct lyd_node **tree);
142
143/**
roman142718b2023-06-29 09:15:29 +0200144 * @brief Deletes a subtree from the YANG data.
145 *
146 * @param tree YANG data from which the subtree will be deleted.
Roytak7b9bf292023-10-04 14:06:38 +0200147 * @param[in] path_fmt Format of the path. The last node will be the top level node of the deleted tree.
roman142718b2023-06-29 09:15:29 +0200148 * @param[in] ... Parameters for the path format, essentially representing the lists' keys.
149 * @return 0 on success, non-zero otherwise.
150 */
Roytakb2794852023-10-18 14:30:22 +0200151int nc_server_config_delete(struct lyd_node **tree, const char *path_fmt, ...);
roman142718b2023-06-29 09:15:29 +0200152
Roytak7b9bf292023-10-04 14:06:38 +0200153/**
154 * @brief Deletes a subtree from the YANG data, but doesn't return an error if the node doesn't exist.
155 *
156 * @param tree YANG data from which the subtree will be deleted.
157 * @param[in] path_fmt Format of the path. The last node will be the top level node of the deleted tree.
158 * @param[in] ... Parameters for the path format, essentially representing the lists' keys.
159 * @return 0 on success, non-zero otherwise.
160 */
Roytakb2794852023-10-18 14:30:22 +0200161int nc_server_config_check_delete(struct lyd_node **tree, const char *path_fmt, ...);
roman466719d2023-05-05 16:14:37 +0200162
roman3f9b65c2023-06-05 14:26:58 +0200163#endif /* NC_CONFIG_NEW_H_ */