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