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