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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef NC_SESSION_SERVER_CH_H_ |
| 24 | #define NC_SESSION_SERVER_CH_H_ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <libyang/libyang.h> |
| 28 | |
| 29 | #include "session.h" |
| 30 | #include "netconf.h" |
| 31 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame^] | 32 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @brief Establish an SSH Call Home connection with a listening NETCONF client. |
| 36 | * |
| 37 | * @param[in] host Host the client is listening on. |
| 38 | * @param[in] port Port the client is listening on. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 39 | * @param[in] timeout Timeout for transport-related operations in milliseconds. |
| 40 | * 0 for non-blocking call, -1 for infinite waiting. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 41 | * @param[out] session New Call Home session. |
| 42 | * @return 1 on success, 0 on timeout, -1 on error. |
| 43 | */ |
| 44 | int nc_connect_callhome_ssh(const char *host, uint16_t port, int timeout, struct nc_session **session); |
| 45 | |
| 46 | /** |
| 47 | * @brief Set Call Home SSH host keys the server will identify itself with. Each of RSA, DSA, and |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 48 | * ECDSA keys can be set. If the particular type was already set, it is replaced. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 49 | * |
| 50 | * @param[in] privkey_path Path to a private key. |
| 51 | * @return 0 on success, -1 on error. |
| 52 | */ |
| 53 | int nc_server_ssh_ch_set_hostkey(const char *privkey_path); |
| 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 | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 122 | * @param[in] timeout Timeout for transport-related operations in milliseconds. |
| 123 | * 0 for non-blocking call, -1 for infinite waiting. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 124 | * @param[out] session New Call Home session. |
| 125 | * @return 1 on success, 0 on timeout, -1 on error. |
| 126 | */ |
| 127 | int nc_connect_callhome_tls(const char *host, uint16_t port, int timeout, struct nc_session **session); |
| 128 | |
| 129 | /** |
| 130 | * @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] | 131 | * 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] | 132 | * |
| 133 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 134 | * @return 0 on success, -1 on error. |
| 135 | */ |
| 136 | int nc_server_tls_ch_set_cert(const char *cert); |
| 137 | |
| 138 | /** |
| 139 | * @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] | 140 | * 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] | 141 | * |
| 142 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 143 | * @return 0 on success, -1 on error. |
| 144 | */ |
| 145 | int nc_server_tls_ch_set_cert_path(const char *cert_path); |
| 146 | |
| 147 | /** |
| 148 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 149 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 150 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 151 | * |
| 152 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 153 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 154 | * @return 0 on success, -1 on error. |
| 155 | */ |
| 156 | int nc_server_tls_ch_set_key(const char *privkey, int is_rsa); |
| 157 | |
| 158 | /** |
| 159 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 160 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 161 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 162 | * |
| 163 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 164 | * @return 0 on success, -1 on error. |
| 165 | */ |
| 166 | int nc_server_tls_ch_set_key_path(const char *privkey_path); |
| 167 | |
| 168 | /** |
| 169 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 170 | * |
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 | */ |
| 174 | int nc_server_tls_ch_add_trusted_cert(const char *cert); |
| 175 | |
| 176 | /** |
| 177 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 178 | * |
| 179 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 180 | * @return 0 on success, -1 on error. |
| 181 | */ |
| 182 | int nc_server_tls_ch_add_trusted_cert_path(const char *cert_path); |
| 183 | |
| 184 | /** |
| 185 | * @brief Set trusted Call Home Certificate Authority certificate locations. There |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 186 | * 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] | 187 | * |
| 188 | * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 189 | * Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 190 | * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 191 | * (c_rehash utility can be used to create hashes) |
| 192 | * with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 193 | * @return 0 on success, -1 on error. |
| 194 | */ |
| 195 | int nc_server_tls_ch_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path); |
| 196 | |
| 197 | /** |
| 198 | * @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] | 199 | * CRLs and CTN entries are not affected. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 200 | */ |
| 201 | void nc_server_tls_ch_clear_certs(void); |
| 202 | |
| 203 | /** |
| 204 | * @brief Set Call Home Certificate Revocation List locations. There can only be |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 205 | * one file and one directory, they are replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 206 | * |
| 207 | * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL. |
| 208 | * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 209 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 210 | * @return 0 on success, -1 on error. |
| 211 | */ |
| 212 | int nc_server_tls_ch_set_crl_locations(const char *crl_file_path, const char *crl_dir_path); |
| 213 | |
| 214 | /** |
| 215 | * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys, |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 216 | * and CTN entries are not affected. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 217 | */ |
| 218 | void nc_server_tls_ch_clear_crls(void); |
| 219 | |
| 220 | /** |
| 221 | * @brief Add a Call Home Cert-to-name entry. |
| 222 | * |
| 223 | * @param[in] id Priority of the entry. |
| 224 | * @param[in] fingerprint Matching certificate fingerprint. |
| 225 | * @param[in] map_type Type of username-certificate mapping. |
| 226 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 227 | * @return 0 on success, -1 on error. |
| 228 | */ |
| 229 | int nc_server_tls_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 230 | |
| 231 | /** |
| 232 | * @brief Remove a Call Home Cert-to-name entry. |
| 233 | * |
| 234 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 235 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 236 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 237 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 238 | * @return 0 on success, -1 on not finding any match. |
| 239 | */ |
| 240 | int nc_server_tls_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 241 | |
| 242 | /** |
| 243 | * @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] | 244 | * can be set for the next client to connect. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 245 | */ |
| 246 | void nc_server_tls_ch_clear_opts(void); |
| 247 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame^] | 248 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 249 | |
| 250 | #endif /* NC_SESSION_SERVER_CH_H_ */ |