blob: 17b3fffd7baf2a24163614f0b36268ff31be1234 [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
roman44af5052024-04-05 12:31:24 +020040/* private key's pkcs1 rsa footer */
41#define NC_PKCS1_RSA_PRIVKEY_FOOTER "\n-----END RSA PRIVATE KEY-----\n"
42
roman466719d2023-05-05 16:14:37 +020043/* private key's sec1 ec header */
44#define NC_SEC1_EC_PRIVKEY_HEADER "-----BEGIN EC PRIVATE KEY-----\n"
45
roman44af5052024-04-05 12:31:24 +020046/* private key's sec1 ec footer */
47#define NC_SEC1_EC_PRIVKEY_FOOTER "\n-----END EC PRIVATE KEY-----\n"
48
roman466719d2023-05-05 16:14:37 +020049/* 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
roman3f9b65c2023-06-05 14:26:58 +020064/* 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
roman466719d2023-05-05 16:14:37 +020070typedef enum {
71 NC_ALG_HOSTKEY,
72 NC_ALG_KEY_EXCHANGE,
73 NC_ALG_ENCRYPTION,
74 NC_ALG_MAC
75} NC_ALG_TYPE;
76
Roytakb2794852023-10-18 14:30:22 +020077/**
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 */
88int 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 +020089 char **privkey, NC_PRIVKEY_FORMAT *privkey_type, char **pubkey);
roman3f9b65c2023-06-05 14:26:58 +020090
Roytakb2794852023-10-18 14:30:22 +020091/**
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 */
99int nc_server_config_util_get_ssh_pubkey_file(const char *pubkey_path, char **pubkey);
roman3f9b65c2023-06-05 14:26:58 +0200100
Roytakb2794852023-10-18 14:30:22 +0200101/**
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 */
109int nc_server_config_util_read_certificate(const char *cert_path, char **cert);
roman3f9b65c2023-06-05 14:26:58 +0200110
Roytakb2794852023-10-18 14:30:22 +0200111/**
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 */
118const char *nc_server_config_util_privkey_format_to_identityref(NC_PRIVKEY_FORMAT format);
roman3f9b65c2023-06-05 14:26:58 +0200119
roman2eab4742023-06-06 10:00:26 +0200120#endif /* NC_ENABLED_SSH_TLS */
121
romand30af552023-06-16 15:18:27 +0200122/**
123 * @brief Creates YANG data nodes in a path and gives the final node a value.
124 *
romanb6f44032023-06-30 15:07:56 +0200125 * @param[in] ctx libyang context.
romand30af552023-06-16 15:18:27 +0200126 * @param[in, out] tree The YANG data tree where the insertion will happen. On success
Roytak7b9bf292023-10-04 14:06:38 +0200127 * this is set to the top level container.
romand30af552023-06-16 15:18:27 +0200128 * @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 */
Roytakb2794852023-10-18 14:30:22 +0200133int 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 +0200134
roman142718b2023-06-29 09:15:29 +0200135/**
Roytak7b9bf292023-10-04 14:06:38 +0200136 * @brief Creates a YANG data node by appending it to a specified parent node.
romanb6f44032023-06-30 15:07:56 +0200137 *
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.
Roytak7b9bf292023-10-04 14:06:38 +0200141 * @param[in] value Value given to the child node.
romanb6f44032023-06-30 15:07:56 +0200142 * @param[out] tree YANG data tree where the insertion will happen. On success
Roytak7b9bf292023-10-04 14:06:38 +0200143 * this is set to the top level container.
romanb6f44032023-06-30 15:07:56 +0200144 * @return 0 on success, 1 otherwise.
145 */
Roytakb2794852023-10-18 14:30:22 +0200146int nc_server_config_append(const struct ly_ctx *ctx, const char *parent_path, const char *child_name,
romanb6f44032023-06-30 15:07:56 +0200147 const char *value, struct lyd_node **tree);
148
149/**
roman142718b2023-06-29 09:15:29 +0200150 * @brief Deletes a subtree from the YANG data.
151 *
152 * @param tree YANG data from which the subtree will be deleted.
Roytak7b9bf292023-10-04 14:06:38 +0200153 * @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 +0200154 * @param[in] ... Parameters for the path format, essentially representing the lists' keys.
155 * @return 0 on success, non-zero otherwise.
156 */
Roytakb2794852023-10-18 14:30:22 +0200157int nc_server_config_delete(struct lyd_node **tree, const char *path_fmt, ...);
roman142718b2023-06-29 09:15:29 +0200158
Roytak7b9bf292023-10-04 14:06:38 +0200159/**
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 */
Roytakb2794852023-10-18 14:30:22 +0200167int nc_server_config_check_delete(struct lyd_node **tree, const char *path_fmt, ...);
roman466719d2023-05-05 16:14:37 +0200168
roman3f9b65c2023-06-05 14:26:58 +0200169#endif /* NC_CONFIG_NEW_H_ */