roman | e60ef99 | 2024-05-13 12:53:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file session_wrapper.h |
| 3 | * @author Roman Janota <janota@cesnet.cz> |
| 4 | * @brief libnetconf2 - header for wrapped TLS library function calls (currently OpenSSL and MbedTLS) |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2024 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 | |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 16 | #ifndef _SESSION_WRAPPER_H_ |
| 17 | #define _SESSION_WRAPPER_H_ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #ifdef HAVE_LIBMBEDTLS |
| 24 | |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 25 | #include <mbedtls/ctr_drbg.h> |
roman | c787d28 | 2024-04-25 16:10:46 +0200 | [diff] [blame] | 26 | #include <mbedtls/entropy.h> |
| 27 | #include <mbedtls/pk.h> |
roman | 008cfe7 | 2024-04-05 12:36:18 +0200 | [diff] [blame] | 28 | #include <mbedtls/ssl.h> |
roman | c787d28 | 2024-04-25 16:10:46 +0200 | [diff] [blame] | 29 | #include <mbedtls/x509_crl.h> |
| 30 | #include <mbedtls/x509_crt.h> |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 31 | |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 32 | /** |
| 33 | * @brief Context from which a TLS session may be created. |
| 34 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 35 | struct nc_tls_ctx { |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 36 | int *sock; /**< Socket FD. */ |
| 37 | mbedtls_entropy_context *entropy; /**< Entropy. */ |
| 38 | mbedtls_ctr_drbg_context *ctr_drbg; /**< Random bit generator. */ |
| 39 | mbedtls_x509_crt *cert; /**< Certificate. */ |
| 40 | mbedtls_pk_context *pkey; /**< Private key. */ |
| 41 | mbedtls_x509_crt *cert_store; /**< CA certificates store. */ |
| 42 | mbedtls_x509_crl *crl_store; /**< CRL store. */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | #else |
| 46 | |
roman | c787d28 | 2024-04-25 16:10:46 +0200 | [diff] [blame] | 47 | #include <openssl/evp.h> |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 48 | #include <openssl/ssl.h> |
roman | c787d28 | 2024-04-25 16:10:46 +0200 | [diff] [blame] | 49 | #include <openssl/x509.h> |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 50 | |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 51 | /** |
| 52 | * @brief Context from which a TLS session may be created. |
| 53 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 54 | struct nc_tls_ctx { |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 55 | X509 *cert; /**< Certificate. */ |
| 56 | EVP_PKEY *pkey; /**< Private key. */ |
| 57 | X509_STORE *cert_store; /**< CA certificate store. */ |
| 58 | X509_STORE *crl_store; /**< CRL store. */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | #endif |
| 62 | |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 63 | /** |
| 64 | * @brief Server side TLS verify callback data. |
| 65 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 66 | struct nc_tls_verify_cb_data { |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 67 | struct nc_session *session; /**< NETCONF session. */ |
| 68 | struct nc_server_tls_opts *opts; /**< TLS server options. */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 69 | struct nc_ctn_data { |
roman | 6da4cdb | 2024-05-13 13:47:39 +0200 | [diff] [blame^] | 70 | char *username; /**< Username. */ |
| 71 | int matched_ctns; /**< OR'd values of currently matched CTN types. */ |
| 72 | int matched_ctn_type[6]; /**< Currently matched CTN types (order matters). */ |
| 73 | int matched_ctn_count; /**< Number of matched CTN types. */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 74 | } ctn_data; |
| 75 | }; |
| 76 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 77 | /** |
| 78 | * @brief Creates a new TLS session from the given configuration. |
| 79 | * |
| 80 | * @param[in] tls_cfg TLS configuration. |
| 81 | * @return New TLS session on success, NULL on fail. |
| 82 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 83 | void * nc_tls_session_new_wrap(void *tls_cfg); |
| 84 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 85 | /** |
| 86 | * @brief Destroys a TLS session. |
| 87 | * |
| 88 | * @param[in] tls_session TLS session to destroy. |
| 89 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 90 | void nc_tls_session_destroy_wrap(void *tls_session); |
| 91 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 92 | /** |
| 93 | * @brief Creates a new TLS configuration. |
| 94 | * |
| 95 | * @param[in] side Side of the TLS connection. |
| 96 | * @return New TLS configuration on success, NULL on fail. |
| 97 | */ |
| 98 | void * nc_tls_config_new_wrap(int side); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 99 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 100 | /** |
| 101 | * @brief Destroys a TLS configuration. |
| 102 | * |
| 103 | * @param[in] tls_cfg TLS configuration to destroy. |
| 104 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 105 | void nc_tls_config_destroy_wrap(void *tls_cfg); |
| 106 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 107 | /** |
| 108 | * @brief Creates a new TLS certificate. |
| 109 | * |
| 110 | * @return New TLS certificate on success, NULL on fail. |
| 111 | */ |
roman | b87aa45 | 2024-05-13 12:58:00 +0200 | [diff] [blame] | 112 | void * nc_tls_cert_new_wrap(void); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 113 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 114 | /** |
| 115 | * @brief Destroys a TLS certificate. |
| 116 | * |
| 117 | * @param[in] cert TLS certificate to destroy. |
| 118 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 119 | void nc_tls_cert_destroy_wrap(void *cert); |
| 120 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 121 | /** |
| 122 | * @brief Destroys a TLS private key. |
| 123 | * |
| 124 | * @param[in] pkey TLS private key to destroy. |
| 125 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 126 | void nc_tls_privkey_destroy_wrap(void *pkey); |
| 127 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 128 | /** |
| 129 | * @brief Creates a new TLS certificate store. |
| 130 | * |
| 131 | * @return New TLS certificate store on success, NULL on fail. |
| 132 | */ |
roman | b87aa45 | 2024-05-13 12:58:00 +0200 | [diff] [blame] | 133 | void * nc_tls_cert_store_new_wrap(void); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 134 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 135 | /** |
| 136 | * @brief Destroys a TLS certificate store. |
| 137 | * |
| 138 | * @param[in] cert_store TLS certificate store to destroy. |
| 139 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 140 | void nc_tls_cert_store_destroy_wrap(void *cert_store); |
| 141 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 142 | /** |
| 143 | * @brief Creates a new CRL store. |
| 144 | * |
| 145 | * @return New CRL store on success, NULL on fail. |
| 146 | */ |
roman | b87aa45 | 2024-05-13 12:58:00 +0200 | [diff] [blame] | 147 | void * nc_tls_crl_store_new_wrap(void); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 148 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 149 | /** |
| 150 | * @brief Destroys a CRL store. |
| 151 | * |
| 152 | * @param[in] crl_store CRL store to destroy. |
| 153 | */ |
| 154 | void nc_tls_crl_store_destroy_wrap(void *crl_store); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 155 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 156 | /** |
| 157 | * @brief Converts PEM certificate data to a certificate. |
| 158 | * |
| 159 | * @param[in] cert_data PEM certificate data. |
| 160 | * @return New certificate on success, NULL on fail. |
| 161 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 162 | void * nc_tls_pem_to_cert_wrap(const char *cert_data); |
| 163 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 164 | /** |
| 165 | * @brief Adds a certificate to a certificate store. |
| 166 | * |
| 167 | * @param[in] cert Certificate to add. |
| 168 | * @param[in] cert_store Certificate store to add the certificate to. |
| 169 | * @return 0 on success and the memory belongs to cert_store, non-zero on fail. |
| 170 | */ |
| 171 | int nc_tls_add_cert_to_store_wrap(void *cert, void *cert_store); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 172 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 173 | /** |
| 174 | * @brief Converts PEM private key data to a private key. |
| 175 | * |
| 176 | * @param[in] privkey_data PEM private key data. |
| 177 | * @return New private key on success, NULL on fail. |
| 178 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 179 | void * nc_tls_pem_to_privkey_wrap(const char *privkey_data); |
| 180 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 181 | /** |
| 182 | * @brief Imports CRL from a file. |
| 183 | * |
| 184 | * @param[in] path Path to the CRL file. |
| 185 | * @param[in] crl_store CRL store to import the CRL to. |
| 186 | * @return 0 on success, non-zero on fail. |
| 187 | */ |
| 188 | int nc_tls_import_crl_path_wrap(const char *path, void *crl_store); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 189 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 190 | /** |
| 191 | * @brief Parses and adds a CRL to a CRL store. |
| 192 | * |
| 193 | * @param[in] crl_data CRL data. |
| 194 | * @param[in] size Size of the CRL data. |
| 195 | * @param[in] crl_store CRL store to add the CRL to. |
| 196 | * @return 0 on success, non-zero on fail. |
| 197 | */ |
| 198 | int nc_server_tls_add_crl_to_store_wrap(const unsigned char *crl_data, size_t size, void *crl_store); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 199 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 200 | /** |
| 201 | * @brief Sets the TLS version. |
| 202 | * |
| 203 | * @param[in] tls_cfg TLS configuration. |
| 204 | * @param[in] tls_versions Bit-field of supported TLS versions. |
| 205 | * |
| 206 | * @return 0 on success, non-zero on fail. |
| 207 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 208 | int nc_server_tls_set_tls_versions_wrap(void *tls_cfg, unsigned int tls_versions); |
| 209 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 210 | /** |
| 211 | * @brief Set TLS server's verify flags, verify cb and its data. |
| 212 | * |
| 213 | * @param[in] tls_cfg TLS configuration. |
| 214 | * @param[in] cb_data Verify callback data. |
| 215 | */ |
| 216 | void nc_server_tls_set_verify_wrap(void *tls_cfg, struct nc_tls_verify_cb_data *cb_data); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 217 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 218 | /** |
| 219 | * @brief Set TLS client's verify flags. |
| 220 | * |
| 221 | * @param[in] tls_cfg TLS configuration. |
| 222 | */ |
| 223 | void nc_client_tls_set_verify_wrap(void *tls_cfg); |
| 224 | |
| 225 | /** |
| 226 | * @brief Verify the certificate. |
| 227 | * |
| 228 | * @param[in] cert Certificate to verify. |
| 229 | * @param[in] depth Certificate depth. |
| 230 | * @param[in] self_signed Boolean flag representing self-signedness of the certificate. |
| 231 | * @param[in] cb_data Data for the verify callback. |
| 232 | * @return 0 on success, 1 on verify fail, -1 on fatal error. |
| 233 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 234 | int nc_server_tls_verify_cert(void *cert, int depth, int self_signed, struct nc_tls_verify_cb_data *cb_data); |
| 235 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 236 | /** |
| 237 | * @brief Check if the peer certificate matches any configured ee certs. |
| 238 | * |
| 239 | * @param[in] peer_cert Peer certificate. |
| 240 | * @param[in] opts TLS options. |
| 241 | * @return 0 on success, non-zero on fail. |
| 242 | */ |
| 243 | int nc_server_tls_verify_peer_cert(void *peer_cert, struct nc_server_tls_opts *opts); |
| 244 | |
| 245 | /** |
| 246 | * @brief Get the subject of the certificate. |
| 247 | * |
| 248 | * @param[in] cert Certificate. |
| 249 | * @return Subject of the certificate on success, NULL on fail. |
| 250 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 251 | char * nc_server_tls_get_subject_wrap(void *cert); |
| 252 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 253 | /** |
| 254 | * @brief Get the issuer of the certificate. |
| 255 | * |
| 256 | * @param[in] cert Certificate. |
| 257 | * @return Issuer of the certificate on success, NULL on fail. |
| 258 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 259 | char * nc_server_tls_get_issuer_wrap(void *cert); |
| 260 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 261 | /** |
| 262 | * @brief Get the Subject Alternative Names of the certificate. |
| 263 | * |
| 264 | * @param[in] cert Certificate. |
| 265 | * @return SANs on success, NULL on fail. |
| 266 | */ |
| 267 | void * nc_tls_get_sans_wrap(void *cert); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 268 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 269 | /** |
| 270 | * @brief Destroy the SANs. |
| 271 | * |
| 272 | * @param[in] sans SANs to destroy. |
| 273 | */ |
| 274 | void nc_tls_sans_destroy_wrap(void *sans); |
| 275 | |
| 276 | /** |
| 277 | * @brief Get the number of SANs. |
| 278 | * |
| 279 | * @param[in] sans SANs. |
| 280 | * @return Number of SANs. |
| 281 | */ |
| 282 | int nc_tls_get_num_sans_wrap(void *sans); |
| 283 | |
| 284 | /** |
| 285 | * @brief Get the SAN value and type in the context of CTN. |
| 286 | * |
| 287 | * @param[in] sans SANs. |
| 288 | * @param[in] idx Index of the SAN. |
| 289 | * @param[out] san_value SAN value. |
| 290 | * @param[out] san_type SAN type. |
| 291 | * @return 0 on success, non-zero on fail. |
| 292 | */ |
| 293 | int nc_tls_get_san_value_type_wrap(void *sans, int idx, char **san_value, NC_TLS_CTN_MAPTYPE *san_type); |
| 294 | |
| 295 | /** |
| 296 | * @brief Compare two certificates. |
| 297 | * |
| 298 | * @param[in] cert1 Certificate 1. |
| 299 | * @param[in] cert2 Certificate 2. |
| 300 | * @return 1 if the certificates match, 0 otherwise. |
| 301 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 302 | int nc_server_tls_certs_match_wrap(void *cert1, void *cert2); |
| 303 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 304 | /** |
| 305 | * @brief Get the MD5 digest of the certificate. |
| 306 | * |
| 307 | * @param[in] cert Certificate. |
| 308 | * @param[out] buf Buffer for the digest. |
| 309 | * @return 0 on success, non-zero on fail. |
| 310 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 311 | int nc_server_tls_md5_wrap(void *cert, unsigned char *buf); |
| 312 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 313 | /** |
| 314 | * @brief Get the SHA1 digest of the certificate. |
| 315 | * |
| 316 | * @param[in] cert Certificate. |
| 317 | * @param[out] buf Buffer for the digest. |
| 318 | * @return 0 on success, non-zero on fail. |
| 319 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 320 | int nc_server_tls_sha1_wrap(void *cert, unsigned char *buf); |
| 321 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 322 | /** |
| 323 | * @brief Get the SHA224 digest of the certificate. |
| 324 | * |
| 325 | * @param[in] cert Certificate. |
| 326 | * @param[out] buf Buffer for the digest. |
| 327 | * @return 0 on success, non-zero on fail. |
| 328 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 329 | int nc_server_tls_sha224_wrap(void *cert, unsigned char *buf); |
| 330 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 331 | /** |
| 332 | * @brief Get the SHA256 digest of the certificate. |
| 333 | * |
| 334 | * @param[in] cert Certificate. |
| 335 | * @param[out] buf Buffer for the digest. |
| 336 | * @return 0 on success, non-zero on fail. |
| 337 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 338 | int nc_server_tls_sha256_wrap(void *cert, unsigned char *buf); |
| 339 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 340 | /** |
| 341 | * @brief Get the SHA384 digest of the certificate. |
| 342 | * |
| 343 | * @param[in] cert Certificate. |
| 344 | * @param[out] buf Buffer for the digest. |
| 345 | * @return 0 on success, non-zero on fail. |
| 346 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 347 | int nc_server_tls_sha384_wrap(void *cert, unsigned char *buf); |
| 348 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 349 | /** |
| 350 | * @brief Get the SHA512 digest of the certificate. |
| 351 | * |
| 352 | * @param[in] cert Certificate. |
| 353 | * @param[out] buf Buffer for the digest. |
| 354 | * @return 0 on success, non-zero on fail. |
| 355 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 356 | int nc_server_tls_sha512_wrap(void *cert, unsigned char *buf); |
| 357 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 358 | /** |
| 359 | * @brief Set the FD for a TLS session. |
| 360 | * |
| 361 | * @param[in] tls_session TLS session. |
| 362 | * @param[in] sock Socket FD. |
| 363 | * @param[in] tls_ctx TLS context. |
| 364 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 365 | void nc_server_tls_set_fd_wrap(void *tls_session, int sock, struct nc_tls_ctx *tls_ctx); |
| 366 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 367 | /** |
| 368 | * @brief Perform a server-side step of the TLS handshake. |
| 369 | * |
| 370 | * @param[in] tls_session TLS session. |
| 371 | * @return 1 on success, 0 if the handshake is not finished, negative number on error. |
| 372 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 373 | int nc_server_tls_handshake_step_wrap(void *tls_session); |
| 374 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 375 | /** |
| 376 | * @brief Perform a client-side step of the TLS handshake. |
| 377 | * |
| 378 | * @param[in] tls_session TLS session. |
| 379 | * @param[in] sock Socket FD. |
| 380 | * @return 1 on success, 0 if the handshake is not finished, negative number on error. |
| 381 | */ |
| 382 | int nc_client_tls_handshake_step_wrap(void *tls_session, int sock); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 383 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 384 | /** |
| 385 | * @brief Destroy a TLS context. |
| 386 | * |
| 387 | * @param[in] tls_ctx TLS context. |
| 388 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 389 | void nc_tls_ctx_destroy_wrap(struct nc_tls_ctx *tls_ctx); |
| 390 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 391 | /** |
| 392 | * @brief Load client's certificate and a private key. |
| 393 | * |
| 394 | * @param[in] cert_path Path to the certificate. |
| 395 | * @param[in] key_path Path to the private key. |
| 396 | * @param[out] cert Certificate. |
| 397 | * @param[out] pkey Private key. |
| 398 | * @return 0 on success, non-zero on fail. |
| 399 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 400 | int nc_client_tls_load_cert_key_wrap(const char *cert_path, const char *key_path, void **cert, void **pkey); |
| 401 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 402 | /** |
| 403 | * @brief Load client's trusted certificates. |
| 404 | * |
| 405 | * @param[in] cert_store Certificate store. |
| 406 | * @param[in] file_path Path to the file with trusted certificates. |
| 407 | * @param[in] dir_path Path to the directory with trusted certificates. |
| 408 | * @return 0 on success, non-zero on fail. |
| 409 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 410 | int nc_client_tls_load_trusted_certs_wrap(void *cert_store, const char *file_path, const char *dir_path); |
| 411 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 412 | /** |
| 413 | * @brief Load client's CRLs. |
| 414 | * |
| 415 | * @param[in] crl_store CRL store. |
| 416 | * @param[in] file_path Path to the file with CRLs. |
| 417 | * @param[in] dir_path Path to the directory with CRLs. |
| 418 | * @return 0 on success, non-zero on fail. |
| 419 | */ |
| 420 | int nc_client_tls_load_crl_wrap(void *crl_store, const char *file_path, const char *dir_path); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 421 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 422 | /** |
| 423 | * @brief Set the hostname for the TLS session. |
| 424 | * |
| 425 | * @param[in] tls_session TLS session. |
| 426 | * @param[in] hostname Hostname. |
| 427 | * @return 0 on success, non-zero on fail. |
| 428 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 429 | int nc_client_tls_set_hostname_wrap(void *tls_session, const char *hostname); |
| 430 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 431 | /** |
| 432 | * @brief Initialize a TLS context. |
| 433 | * |
| 434 | * @param[in] sock Socket FD. |
| 435 | * @param[in] cert Certificate. |
| 436 | * @param[in] pkey Private key. |
| 437 | * @param[in] cert_store Certificate store. |
| 438 | * @param[in] crl_store CRL store. |
| 439 | * @param[in,out] tls_ctx TLS context. |
| 440 | * @return 0 on success, non-zero on fail. |
| 441 | */ |
| 442 | int nc_tls_init_ctx_wrap(int sock, void *cert, void *pkey, void *cert_store, void *crl_store, struct nc_tls_ctx *tls_ctx); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 443 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 444 | /** |
| 445 | * @brief Setup a TLS configuration from a TLS context. |
| 446 | * |
| 447 | * @param[in] tls_ctx TLS context. |
| 448 | * @param[in] side Side of the TLS connection. |
| 449 | * @param[in,out] tls_cfg TLS configuration. |
| 450 | * @return 0 on success, non-zero on fail. |
| 451 | */ |
| 452 | int nc_tls_setup_config_from_ctx_wrap(struct nc_tls_ctx *tls_ctx, int side, void *tls_cfg); |
| 453 | |
| 454 | /** |
| 455 | * @brief Get the error code from a TLS session's verification. |
| 456 | * |
| 457 | * @param[in] tls_session TLS session. |
| 458 | * @return Error code, 0 indicates success. |
| 459 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 460 | uint32_t nc_tls_get_verify_result_wrap(void *tls_session); |
| 461 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 462 | /** |
| 463 | * @brief Get the error string from a TLS session's verification. |
| 464 | * |
| 465 | * @param[in] err_code Error code. |
| 466 | * @return Error string. |
| 467 | */ |
| 468 | char * nc_tls_verify_error_string_wrap(uint32_t err_code); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 469 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 470 | /** |
| 471 | * @brief Print the TLS session's connection error. |
| 472 | * |
| 473 | * @param[in] connect_ret Error code. |
| 474 | * @param[in] peername Peername. |
| 475 | * @param[in] tls_session TLS session. |
| 476 | */ |
| 477 | void nc_client_tls_print_connect_err_wrap(int connect_ret, const char *peername, void *tls_session); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 478 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 479 | /** |
| 480 | * @brief Print the TLS session's accept error. |
| 481 | * |
| 482 | * @param[in] accept_ret Error code. |
| 483 | * @param[in] tls_session TLS session. |
| 484 | */ |
| 485 | void nc_server_tls_print_accept_err_wrap(int accept_ret, void *tls_session); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 486 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 487 | /** |
| 488 | * @brief Checks if the DER data is a SubjectPublicKeyInfo public key. |
| 489 | * |
| 490 | * @param[in] der DER data. |
| 491 | * @param[in] len Length of the DER data. |
| 492 | * |
| 493 | * @return 1 if the data is a SubjectPublicKeyInfo public key, 0 if not, -1 on error. |
| 494 | */ |
| 495 | int nc_tls_is_der_subpubkey_wrap(unsigned char *der, long len); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * @brief Decodes base64 to binary. |
| 499 | * |
| 500 | * @param[in] base64 Base64 string. |
| 501 | * @param[out] bin Binary result, memory managed by the caller. |
| 502 | * @return Length of the binary data on success, -1 on error. |
| 503 | */ |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 504 | int nc_base64_decode_wrap(const char *base64, unsigned char **bin); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 505 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 506 | /** |
| 507 | * @brief Encodes binary to base64. |
| 508 | * |
| 509 | * @param[in] bin Binary data. |
| 510 | * @param[in] len Length of the binary data. |
| 511 | * @param[out] base64 NULL terminated Base64 result, memory managed by the caller. |
| 512 | * @return 0 on success, -1 on error. |
| 513 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 514 | int nc_base64_encode_wrap(const unsigned char *bin, size_t len, char **base64); |
| 515 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 516 | /** |
| 517 | * @brief Reads data from a TLS session. |
| 518 | * |
| 519 | * @param[in] session NETCONF session. |
| 520 | * @param[out] buf Buffer for the data. |
| 521 | * @param[in] size Size of the buffer. |
| 522 | * @return Number of bytes read on success, -1 on error. |
| 523 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 524 | int nc_tls_read_wrap(struct nc_session *session, unsigned char *buf, size_t size); |
| 525 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 526 | /** |
| 527 | * @brief Writes data to a TLS session. |
| 528 | * |
| 529 | * @param[in] session NETCONF session. |
| 530 | * @param[in] buf Data to write. |
| 531 | * @param[in] size Size of the data. |
| 532 | * @return Number of bytes written on success, -1 on error. |
| 533 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 534 | int nc_tls_write_wrap(struct nc_session *session, const unsigned char *buf, size_t size); |
| 535 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 536 | /** |
| 537 | * @brief Get the number of pending bytes in a TLS session. |
| 538 | * |
| 539 | * @param[in] tls_session TLS session. |
| 540 | * @return Number of pending bytes. |
| 541 | */ |
| 542 | int nc_tls_get_num_pending_bytes_wrap(void *tls_session); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 543 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 544 | /** |
| 545 | * @brief Get the file descriptor of a TLS session. |
| 546 | * |
| 547 | * @param[in] session NETCONF session. |
| 548 | * @return File descriptor, -1 on error. |
| 549 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 550 | int nc_tls_get_fd_wrap(const struct nc_session *session); |
| 551 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 552 | /** |
| 553 | * @brief Close a TLS session. |
| 554 | * |
| 555 | * @param[in] tls_session TLS session. |
| 556 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 557 | void nc_tls_close_notify_wrap(void *tls_session); |
| 558 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 559 | /** |
| 560 | * @brief Import a private key from a file. |
| 561 | * |
| 562 | * @param[in] privkey_path Path to the private key file. |
| 563 | * @return Imported private key on success, NULL on fail. |
| 564 | */ |
| 565 | void * nc_tls_import_privkey_file_wrap(const char *privkey_path); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 566 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 567 | /** |
| 568 | * @brief Import a certificate from a file. |
| 569 | * |
| 570 | * @param[in] cert_path Path to the certificate file. |
| 571 | * @return Imported certificate on success, NULL on fail. |
| 572 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 573 | void * nc_tls_import_cert_file_wrap(const char *cert_path); |
| 574 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 575 | /** |
| 576 | * @brief Export a private key to a PEM string. |
| 577 | * |
| 578 | * @param[in] pkey Private key. |
| 579 | * @return PEM string on success, NULL on fail. |
| 580 | */ |
| 581 | char * nc_tls_export_privkey_pem_wrap(void *pkey); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 582 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 583 | /** |
| 584 | * @brief Export a certificate to a PEM string. |
| 585 | * |
| 586 | * @param[in] cert Certificate. |
| 587 | * @return PEM string on success, NULL on fail. |
| 588 | */ |
| 589 | char * nc_tls_export_cert_pem_wrap(void *cert); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 590 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 591 | /** |
| 592 | * @brief Export a public key to a PEM string. |
| 593 | * |
| 594 | * @param[in] pkey Public key. |
| 595 | * @return PEM string on success, NULL on fail. |
| 596 | */ |
| 597 | char * nc_tls_export_pubkey_pem_wrap(void *pkey); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 598 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 599 | /** |
| 600 | * @brief Check if a private key is RSA. |
| 601 | * |
| 602 | * @param[in] pkey Private key. |
| 603 | * @return 1 if the private key is RSA, 0 if not. |
| 604 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 605 | int nc_tls_privkey_is_rsa_wrap(void *pkey); |
| 606 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 607 | /** |
| 608 | * @brief Get the RSA public key parameters from a private key. |
| 609 | * |
| 610 | * @param[in] pkey Private key. |
| 611 | * @param[out] e Exponent. |
| 612 | * @param[out] n Modulus. |
| 613 | * @return 0 on success, non-zero on fail. |
| 614 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 615 | int nc_tls_get_rsa_pubkey_params_wrap(void *pkey, void **e, void **n); |
| 616 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 617 | /** |
| 618 | * @brief Destroy an MPI. |
| 619 | * |
| 620 | * @param[in] mpi MPI. |
| 621 | */ |
| 622 | void nc_tls_destroy_mpi_wrap(void *mpi); |
| 623 | |
| 624 | /** |
| 625 | * @brief Check if a private key is EC. |
| 626 | * |
| 627 | * @param[in] pkey Private key. |
| 628 | * @return 1 if the private key is EC, 0 if not. |
| 629 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 630 | int nc_tls_privkey_is_ec_wrap(void *pkey); |
| 631 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 632 | /** |
| 633 | * @brief Get the group name of an EC private key. |
| 634 | * |
| 635 | * @param[in] pkey Private key. |
| 636 | * @return Group name on success, NULL on fail. |
| 637 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 638 | char * nc_tls_get_ec_group_wrap(void *pkey); |
| 639 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 640 | /** |
| 641 | * @brief Get the EC public key parameters from a private key. |
| 642 | * |
| 643 | * @param[in] pkey Private key. |
| 644 | * @param[out] q Public key point. |
| 645 | * @param[out] q_grp Public key group. |
| 646 | * @return 0 on success, non-zero on fail. |
| 647 | */ |
| 648 | int nc_tls_get_ec_pubkey_params_wrap(void *pkey, void **q, void **q_grp); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 649 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 650 | /** |
| 651 | * @brief Convert an EC point to binary. |
| 652 | * |
| 653 | * @param[in] q EC point. |
| 654 | * @param[in] q_grp EC group. |
| 655 | * @param[out] bin Binary point. |
| 656 | * @param[out] bin_len Length of the binary point. |
| 657 | * @return 0 on success, non-zero on fail. |
| 658 | */ |
| 659 | int nc_tls_ec_point_to_bin_wrap(void *q, void *q_grp, unsigned char **bin, int *bin_len); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 660 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 661 | /** |
| 662 | * @brief Destroy an EC point. |
| 663 | * |
| 664 | * @param[in] p EC point. |
| 665 | */ |
| 666 | void nc_tls_ec_point_destroy_wrap(void *p); |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 667 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 668 | /** |
| 669 | * @brief Destroy an EC group. |
| 670 | * |
| 671 | * @param[in] grp EC group. |
| 672 | */ |
| 673 | void nc_tls_ec_group_destroy_wrap(void *grp); |
| 674 | |
| 675 | /** |
| 676 | * @brief Convert an MPI to binary. |
| 677 | * |
| 678 | * @param[in] mpi MPI. |
| 679 | * @param[out] bin Binary buffer. |
| 680 | * @param[out] bin_len Length of the binary. |
| 681 | * @return 0 on success, 1 on error. |
| 682 | */ |
| 683 | int nc_tls_mpi2bin_wrap(void *mpi, unsigned char **bin, int *bin_len); |
| 684 | |
| 685 | /** |
| 686 | * @brief Import a public key from a file. |
| 687 | * |
| 688 | * @param[in] pubkey_path Path to the public key file. |
| 689 | * @return Imported public key on success, NULL on fail. |
| 690 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 691 | void * nc_tls_import_pubkey_file_wrap(const char *pubkey_path); |
| 692 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 693 | /** |
| 694 | * @brief Get all the URIs from a CRL distribution points. |
| 695 | * |
| 696 | * @param[in] cert_store Certificate store. |
| 697 | * @param[out] uris URIs to download the CRLs from. |
| 698 | * @param[out] uri_count Number of URIs found. |
| 699 | * @return 0 on success, non-zero on fail. |
| 700 | */ |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 701 | int nc_server_tls_get_crl_distpoint_uris_wrap(void *cert_store, char ***uris, int *uri_count); |
| 702 | |
roman | b2a73b8 | 2024-04-23 15:10:12 +0200 | [diff] [blame] | 703 | /** |
| 704 | * @brief Process a cipher suite so that it can be set by the underlying TLS lib. |
| 705 | * |
| 706 | * @param[in] cipher Cipher suite identity value. |
| 707 | * @param[out] out Processed cipher suite. |
| 708 | * @return 0 on success, 1 on fail. |
| 709 | */ |
| 710 | int nc_tls_process_cipher_suite_wrap(const char *cipher, char **out); |
| 711 | |
| 712 | /** |
| 713 | * @brief Append a cipher suite to the list of cipher suites. |
| 714 | * |
| 715 | * @param[in] opts TLS options. |
| 716 | * @param[in] cipher_suite Cipher suite to append. |
| 717 | * @return 0 on success, 1 on fail. |
| 718 | */ |
| 719 | int nc_tls_append_cipher_suite_wrap(struct nc_server_tls_opts *opts, const char *cipher_suite); |
| 720 | |
| 721 | /** |
| 722 | * @brief Set the list of cipher suites for the TLS configuration. |
| 723 | * |
| 724 | * @param[in] tls_cfg TLS configuration. |
| 725 | * @param[in] cipher_suites List of cipher suites. |
| 726 | */ |
| 727 | void nc_server_tls_set_cipher_suites_wrap(void *tls_cfg, void *cipher_suites); |
| 728 | |
roman | 4b64fed | 2024-04-05 12:28:35 +0200 | [diff] [blame] | 729 | #endif |