Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_server_ch.h |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 Call Home session server manipulation |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_SESSION_SERVER_CH_H_ |
| 16 | #define NC_SESSION_SERVER_CH_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <libyang/libyang.h> |
| 20 | |
| 21 | #include "session.h" |
| 22 | #include "netconf.h" |
| 23 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 24 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @brief Establish an SSH Call Home connection with a listening NETCONF client. |
| 28 | * |
| 29 | * @param[in] host Host the client is listening on. |
| 30 | * @param[in] port Port the client is listening on. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 31 | * @param[out] session New Call Home session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 32 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 33 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 34 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 35 | NC_MSG_TYPE nc_connect_callhome_ssh(const char *host, uint16_t port, struct nc_session **session); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 38 | * @brief Add Call Home SSH host keys the server will identify itself with. Any RSA, DSA, and |
| 39 | * ECDSA keys can be added. However, a maximum of one key of each type will be used |
| 40 | * during SSH authentication, later keys replacing earlier ones. |
| 41 | * |
Michal Vasko | d6829ce | 2016-09-22 09:45:13 +0200 | [diff] [blame] | 42 | * @param[in] privkey_path Path to a private key. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 43 | * @return 0 on success, -1 on error. |
| 44 | */ |
| 45 | int nc_server_ssh_ch_add_hostkey(const char *privkey_path); |
| 46 | |
| 47 | /** |
| 48 | * @brief Delete Call Home SSH host keys. Their order is preserved. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 49 | * |
Michal Vasko | d6829ce | 2016-09-22 09:45:13 +0200 | [diff] [blame] | 50 | * @param[in] privkey_path Path to a private key. NULL matches all the keys. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 51 | * @return 0 on success, -1 on error. |
| 52 | */ |
Michal Vasko | d6829ce | 2016-09-22 09:45:13 +0200 | [diff] [blame] | 53 | int nc_server_ssh_ch_del_hostkey(const char *privkey_path); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * @brief Set Call Home SSH banner the server will send to every client. |
| 57 | * |
| 58 | * @param[in] banner SSH banner. |
| 59 | * @return 0 on success, -1 on error. |
| 60 | */ |
| 61 | int nc_server_ssh_ch_set_banner(const char *banner); |
| 62 | |
| 63 | /** |
| 64 | * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 65 | * are supported by default. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 66 | * |
| 67 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 68 | * @return 0 on success, -1 on error. |
| 69 | */ |
| 70 | int nc_server_ssh_ch_set_auth_methods(int auth_methods); |
| 71 | |
| 72 | /** |
| 73 | * @brief Set Call Home SSH authentication attempts of every client. 3 by default. |
| 74 | * |
| 75 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
| 76 | * @return 0 on success, -1 on error. |
| 77 | */ |
| 78 | int nc_server_ssh_ch_set_auth_attempts(uint16_t auth_attempts); |
| 79 | |
| 80 | /** |
| 81 | * @brief Set Call Home SSH authentication timeout. 10 seconds by default. |
| 82 | * |
| 83 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 84 | * @return 0 on success, -1 on error. |
| 85 | */ |
| 86 | int nc_server_ssh_ch_set_auth_timeout(uint16_t auth_timeout); |
| 87 | |
| 88 | /** |
| 89 | * @brief Add an authorized Call Home client SSH public key. This public key can be used for |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 90 | * publickey authentication afterwards. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 91 | * |
| 92 | * @param[in] pubkey_path Path to the public key. |
| 93 | * @param[in] username Username that the client with the public key must use. |
| 94 | * @return 0 on success, -1 on error. |
| 95 | */ |
| 96 | int nc_server_ssh_ch_add_authkey(const char *pubkey_path, const char *username); |
| 97 | |
| 98 | /** |
| 99 | * @brief Remove an authorized Call Home client SSH public key. |
| 100 | * |
| 101 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 102 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 103 | * @return 0 on success, -1 on not finding any match. |
| 104 | */ |
| 105 | int nc_server_ssh_ch_del_authkey(const char *pubkey_path, const char *username); |
| 106 | |
| 107 | /** |
| 108 | * @brief Clear all the SSH Call Home options. Afterwards a new set of options |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 109 | * can be set for the next client to connect to. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 110 | */ |
| 111 | void nc_server_ssh_ch_clear_opts(void); |
| 112 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 113 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 114 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 115 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * @brief Establish a TLS Call Home connection with a listening NETCONF client. |
| 119 | * |
| 120 | * @param[in] host Host the client is listening on. |
| 121 | * @param[in] port Port the client is listening on. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 122 | * @param[out] session New Call Home session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 123 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 124 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 125 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 126 | NC_MSG_TYPE nc_connect_callhome_tls(const char *host, uint16_t port, struct nc_session **session); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 130 | * There can only be one certificate for each key type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 131 | * |
| 132 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 133 | * @return 0 on success, -1 on error. |
| 134 | */ |
| 135 | int nc_server_tls_ch_set_cert(const char *cert); |
| 136 | |
| 137 | /** |
| 138 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 139 | * There can only be one certificate for each key type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 140 | * |
| 141 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 142 | * @return 0 on success, -1 on error. |
| 143 | */ |
| 144 | int nc_server_tls_ch_set_cert_path(const char *cert_path); |
| 145 | |
| 146 | /** |
| 147 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 148 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 149 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 150 | * |
| 151 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 152 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 153 | * @return 0 on success, -1 on error. |
| 154 | */ |
| 155 | int nc_server_tls_ch_set_key(const char *privkey, int is_rsa); |
| 156 | |
| 157 | /** |
| 158 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 159 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 160 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 161 | * |
| 162 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 163 | * @return 0 on success, -1 on error. |
| 164 | */ |
| 165 | int nc_server_tls_ch_set_key_path(const char *privkey_path); |
| 166 | |
| 167 | /** |
| 168 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 169 | * |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 170 | * @param[in] cert_name Arbitary name identifying this certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 171 | * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 172 | * @return 0 on success, -1 on error. |
| 173 | */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 174 | int nc_server_tls_ch_add_trusted_cert(const char *cert_name, const char *cert); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 178 | * |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 179 | * @param[in] cert_name Arbitary name identifying this certificate. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 180 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 181 | * @return 0 on success, -1 on error. |
| 182 | */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 183 | int nc_server_tls_ch_add_trusted_cert_path(const char *cert_name, const char *cert_path); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * @brief Set trusted Call Home Certificate Authority certificate locations. There |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 187 | * can only be one file and one directory, they are replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 188 | * |
Michal Vasko | b34b3e8 | 2016-02-26 15:04:58 +0100 | [diff] [blame] | 189 | * @param[in] ca_file Path to a trusted CA cert store file in PEM format. |
| 190 | * Can be NULL. |
| 191 | * @param[in] ca_dir Path to a trusted CA cert store hashed directory |
| 192 | * (c_rehash utility can be used to create hashes) |
| 193 | * with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 194 | * @return 0 on success, -1 on error. |
| 195 | */ |
Michal Vasko | b34b3e8 | 2016-02-26 15:04:58 +0100 | [diff] [blame] | 196 | int nc_server_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * @brief Destroy and clean all the set Call Home certificates and private keys. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 200 | * CRLs and CTN entries are not affected. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 201 | * |
| 202 | * @param[in] cert_name Name of the certificate to delete. NULL deletes all the certificates. |
| 203 | * @return 0 on success, -1 on not found. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 204 | */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 205 | int nc_server_tls_ch_del_trusted_cert(const char *cert_name); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * @brief Set Call Home Certificate Revocation List locations. There can only be |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 209 | * one file and one directory, they are replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 210 | * |
Michal Vasko | b34b3e8 | 2016-02-26 15:04:58 +0100 | [diff] [blame] | 211 | * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL. |
| 212 | * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility |
| 213 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 214 | * @return 0 on success, -1 on error. |
| 215 | */ |
Michal Vasko | b34b3e8 | 2016-02-26 15:04:58 +0100 | [diff] [blame] | 216 | int nc_server_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys, |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 220 | * and CTN entries are not affected. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 221 | */ |
| 222 | void nc_server_tls_ch_clear_crls(void); |
| 223 | |
| 224 | /** |
| 225 | * @brief Add a Call Home Cert-to-name entry. |
| 226 | * |
| 227 | * @param[in] id Priority of the entry. |
| 228 | * @param[in] fingerprint Matching certificate fingerprint. |
| 229 | * @param[in] map_type Type of username-certificate mapping. |
| 230 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 231 | * @return 0 on success, -1 on error. |
| 232 | */ |
| 233 | int nc_server_tls_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 234 | |
| 235 | /** |
| 236 | * @brief Remove a Call Home Cert-to-name entry. |
| 237 | * |
| 238 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 239 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 240 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 241 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 242 | * @return 0 on success, -1 on not finding any match. |
| 243 | */ |
| 244 | int nc_server_tls_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 245 | |
| 246 | /** |
| 247 | * @brief Clear all the TLS Call Home options. Afterwards a new set of options |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 248 | * can be set for the next client to connect. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 249 | */ |
| 250 | void nc_server_tls_ch_clear_opts(void); |
| 251 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 252 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 253 | |
| 254 | #endif /* NC_SESSION_SERVER_CH_H_ */ |