blob: 785b33f791dc3e4efc566b604937f1550cdf8b2c [file] [log] [blame]
romane60ef992024-05-13 12:53:02 +02001/**
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
roman4b64fed2024-04-05 12:28:35 +020016#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
roman4b64fed2024-04-05 12:28:35 +020025#include <mbedtls/ctr_drbg.h>
romanc787d282024-04-25 16:10:46 +020026#include <mbedtls/entropy.h>
27#include <mbedtls/pk.h>
roman008cfe72024-04-05 12:36:18 +020028#include <mbedtls/ssl.h>
romanc787d282024-04-25 16:10:46 +020029#include <mbedtls/x509_crl.h>
30#include <mbedtls/x509_crt.h>
roman4b64fed2024-04-05 12:28:35 +020031
roman6da4cdb2024-05-13 13:47:39 +020032/**
33 * @brief Context from which a TLS session may be created.
34 */
roman4b64fed2024-04-05 12:28:35 +020035struct nc_tls_ctx {
roman6da4cdb2024-05-13 13:47:39 +020036 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. */
roman4b64fed2024-04-05 12:28:35 +020043};
44
45#else
46
romanc787d282024-04-25 16:10:46 +020047#include <openssl/evp.h>
romanb2a73b82024-04-23 15:10:12 +020048#include <openssl/ssl.h>
romanc787d282024-04-25 16:10:46 +020049#include <openssl/x509.h>
roman4b64fed2024-04-05 12:28:35 +020050
roman6da4cdb2024-05-13 13:47:39 +020051/**
52 * @brief Context from which a TLS session may be created.
53 */
roman4b64fed2024-04-05 12:28:35 +020054struct nc_tls_ctx {
roman6da4cdb2024-05-13 13:47:39 +020055 X509 *cert; /**< Certificate. */
56 EVP_PKEY *pkey; /**< Private key. */
57 X509_STORE *cert_store; /**< CA certificate store. */
58 X509_STORE *crl_store; /**< CRL store. */
roman4b64fed2024-04-05 12:28:35 +020059};
60
61#endif
62
roman6da4cdb2024-05-13 13:47:39 +020063/**
64 * @brief Server side TLS verify callback data.
65 */
roman4b64fed2024-04-05 12:28:35 +020066struct nc_tls_verify_cb_data {
roman6da4cdb2024-05-13 13:47:39 +020067 struct nc_session *session; /**< NETCONF session. */
68 struct nc_server_tls_opts *opts; /**< TLS server options. */
roman4b64fed2024-04-05 12:28:35 +020069 struct nc_ctn_data {
roman6da4cdb2024-05-13 13:47:39 +020070 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. */
roman4b64fed2024-04-05 12:28:35 +020074 } ctn_data;
75};
76
romanb2a73b82024-04-23 15:10:12 +020077/**
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 */
roman4b64fed2024-04-05 12:28:35 +020083void * nc_tls_session_new_wrap(void *tls_cfg);
84
romanb2a73b82024-04-23 15:10:12 +020085/**
86 * @brief Destroys a TLS session.
87 *
88 * @param[in] tls_session TLS session to destroy.
89 */
roman4b64fed2024-04-05 12:28:35 +020090void nc_tls_session_destroy_wrap(void *tls_session);
91
romanb2a73b82024-04-23 15:10:12 +020092/**
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 */
98void * nc_tls_config_new_wrap(int side);
roman4b64fed2024-04-05 12:28:35 +020099
romanb2a73b82024-04-23 15:10:12 +0200100/**
101 * @brief Destroys a TLS configuration.
102 *
103 * @param[in] tls_cfg TLS configuration to destroy.
104 */
roman4b64fed2024-04-05 12:28:35 +0200105void nc_tls_config_destroy_wrap(void *tls_cfg);
106
romanb2a73b82024-04-23 15:10:12 +0200107/**
108 * @brief Creates a new TLS certificate.
109 *
110 * @return New TLS certificate on success, NULL on fail.
111 */
romanb87aa452024-05-13 12:58:00 +0200112void * nc_tls_cert_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200113
romanb2a73b82024-04-23 15:10:12 +0200114/**
115 * @brief Destroys a TLS certificate.
116 *
117 * @param[in] cert TLS certificate to destroy.
118 */
roman4b64fed2024-04-05 12:28:35 +0200119void nc_tls_cert_destroy_wrap(void *cert);
120
romanb2a73b82024-04-23 15:10:12 +0200121/**
122 * @brief Destroys a TLS private key.
123 *
124 * @param[in] pkey TLS private key to destroy.
125 */
roman4b64fed2024-04-05 12:28:35 +0200126void nc_tls_privkey_destroy_wrap(void *pkey);
127
romanb2a73b82024-04-23 15:10:12 +0200128/**
129 * @brief Creates a new TLS certificate store.
130 *
131 * @return New TLS certificate store on success, NULL on fail.
132 */
romanb87aa452024-05-13 12:58:00 +0200133void * nc_tls_cert_store_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200134
romanb2a73b82024-04-23 15:10:12 +0200135/**
136 * @brief Destroys a TLS certificate store.
137 *
138 * @param[in] cert_store TLS certificate store to destroy.
139 */
roman4b64fed2024-04-05 12:28:35 +0200140void nc_tls_cert_store_destroy_wrap(void *cert_store);
141
romanb2a73b82024-04-23 15:10:12 +0200142/**
143 * @brief Creates a new CRL store.
144 *
145 * @return New CRL store on success, NULL on fail.
146 */
romanb87aa452024-05-13 12:58:00 +0200147void * nc_tls_crl_store_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200148
romanb2a73b82024-04-23 15:10:12 +0200149/**
150 * @brief Destroys a CRL store.
151 *
152 * @param[in] crl_store CRL store to destroy.
153 */
154void nc_tls_crl_store_destroy_wrap(void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200155
romanb2a73b82024-04-23 15:10:12 +0200156/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200162void * nc_tls_pem_to_cert_wrap(const char *cert_data);
163
romanb2a73b82024-04-23 15:10:12 +0200164/**
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 */
171int nc_tls_add_cert_to_store_wrap(void *cert, void *cert_store);
roman4b64fed2024-04-05 12:28:35 +0200172
romanb2a73b82024-04-23 15:10:12 +0200173/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200179void * nc_tls_pem_to_privkey_wrap(const char *privkey_data);
180
romanb2a73b82024-04-23 15:10:12 +0200181/**
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 */
188int nc_tls_import_crl_path_wrap(const char *path, void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200189
romanb2a73b82024-04-23 15:10:12 +0200190/**
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 */
198int nc_server_tls_add_crl_to_store_wrap(const unsigned char *crl_data, size_t size, void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200199
romanb2a73b82024-04-23 15:10:12 +0200200/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200208int nc_server_tls_set_tls_versions_wrap(void *tls_cfg, unsigned int tls_versions);
209
romanb2a73b82024-04-23 15:10:12 +0200210/**
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 */
216void nc_server_tls_set_verify_wrap(void *tls_cfg, struct nc_tls_verify_cb_data *cb_data);
roman4b64fed2024-04-05 12:28:35 +0200217
romanb2a73b82024-04-23 15:10:12 +0200218/**
219 * @brief Set TLS client's verify flags.
220 *
221 * @param[in] tls_cfg TLS configuration.
222 */
223void 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 */
roman4b64fed2024-04-05 12:28:35 +0200234int nc_server_tls_verify_cert(void *cert, int depth, int self_signed, struct nc_tls_verify_cb_data *cb_data);
235
romanb2a73b82024-04-23 15:10:12 +0200236/**
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 */
243int 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 */
roman4b64fed2024-04-05 12:28:35 +0200251char * nc_server_tls_get_subject_wrap(void *cert);
252
romanb2a73b82024-04-23 15:10:12 +0200253/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200259char * nc_server_tls_get_issuer_wrap(void *cert);
260
romanb2a73b82024-04-23 15:10:12 +0200261/**
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 */
267void * nc_tls_get_sans_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200268
romanb2a73b82024-04-23 15:10:12 +0200269/**
270 * @brief Destroy the SANs.
271 *
272 * @param[in] sans SANs to destroy.
273 */
274void 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 */
282int 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 */
293int 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 */
roman4b64fed2024-04-05 12:28:35 +0200302int nc_server_tls_certs_match_wrap(void *cert1, void *cert2);
303
romanb2a73b82024-04-23 15:10:12 +0200304/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200311int nc_server_tls_md5_wrap(void *cert, unsigned char *buf);
312
romanb2a73b82024-04-23 15:10:12 +0200313/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200320int nc_server_tls_sha1_wrap(void *cert, unsigned char *buf);
321
romanb2a73b82024-04-23 15:10:12 +0200322/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200329int nc_server_tls_sha224_wrap(void *cert, unsigned char *buf);
330
romanb2a73b82024-04-23 15:10:12 +0200331/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200338int nc_server_tls_sha256_wrap(void *cert, unsigned char *buf);
339
romanb2a73b82024-04-23 15:10:12 +0200340/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200347int nc_server_tls_sha384_wrap(void *cert, unsigned char *buf);
348
romanb2a73b82024-04-23 15:10:12 +0200349/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200356int nc_server_tls_sha512_wrap(void *cert, unsigned char *buf);
357
romanb2a73b82024-04-23 15:10:12 +0200358/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200365void nc_server_tls_set_fd_wrap(void *tls_session, int sock, struct nc_tls_ctx *tls_ctx);
366
romanb2a73b82024-04-23 15:10:12 +0200367/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200373int nc_server_tls_handshake_step_wrap(void *tls_session);
374
romanb2a73b82024-04-23 15:10:12 +0200375/**
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 */
382int nc_client_tls_handshake_step_wrap(void *tls_session, int sock);
roman4b64fed2024-04-05 12:28:35 +0200383
romanb2a73b82024-04-23 15:10:12 +0200384/**
385 * @brief Destroy a TLS context.
386 *
387 * @param[in] tls_ctx TLS context.
388 */
roman4b64fed2024-04-05 12:28:35 +0200389void nc_tls_ctx_destroy_wrap(struct nc_tls_ctx *tls_ctx);
390
romanb2a73b82024-04-23 15:10:12 +0200391/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200400int nc_client_tls_load_cert_key_wrap(const char *cert_path, const char *key_path, void **cert, void **pkey);
401
romanb2a73b82024-04-23 15:10:12 +0200402/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200410int nc_client_tls_load_trusted_certs_wrap(void *cert_store, const char *file_path, const char *dir_path);
411
romanb2a73b82024-04-23 15:10:12 +0200412/**
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 */
420int nc_client_tls_load_crl_wrap(void *crl_store, const char *file_path, const char *dir_path);
roman4b64fed2024-04-05 12:28:35 +0200421
romanb2a73b82024-04-23 15:10:12 +0200422/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200429int nc_client_tls_set_hostname_wrap(void *tls_session, const char *hostname);
430
romanb2a73b82024-04-23 15:10:12 +0200431/**
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 */
442int nc_tls_init_ctx_wrap(int sock, void *cert, void *pkey, void *cert_store, void *crl_store, struct nc_tls_ctx *tls_ctx);
roman4b64fed2024-04-05 12:28:35 +0200443
romanb2a73b82024-04-23 15:10:12 +0200444/**
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 */
452int 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 */
roman4b64fed2024-04-05 12:28:35 +0200460uint32_t nc_tls_get_verify_result_wrap(void *tls_session);
461
romanb2a73b82024-04-23 15:10:12 +0200462/**
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 */
468char * nc_tls_verify_error_string_wrap(uint32_t err_code);
roman4b64fed2024-04-05 12:28:35 +0200469
romanb2a73b82024-04-23 15:10:12 +0200470/**
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 */
477void nc_client_tls_print_connect_err_wrap(int connect_ret, const char *peername, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200478
romanb2a73b82024-04-23 15:10:12 +0200479/**
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 */
485void nc_server_tls_print_accept_err_wrap(int accept_ret, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200486
romanb2a73b82024-04-23 15:10:12 +0200487/**
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 */
495int nc_tls_is_der_subpubkey_wrap(unsigned char *der, long len);
roman4b64fed2024-04-05 12:28:35 +0200496
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 */
romanb2a73b82024-04-23 15:10:12 +0200504int nc_base64_decode_wrap(const char *base64, unsigned char **bin);
roman4b64fed2024-04-05 12:28:35 +0200505
romanb2a73b82024-04-23 15:10:12 +0200506/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200514int nc_base64_encode_wrap(const unsigned char *bin, size_t len, char **base64);
515
romanb2a73b82024-04-23 15:10:12 +0200516/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200524int nc_tls_read_wrap(struct nc_session *session, unsigned char *buf, size_t size);
525
romanb2a73b82024-04-23 15:10:12 +0200526/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200534int nc_tls_write_wrap(struct nc_session *session, const unsigned char *buf, size_t size);
535
romanb2a73b82024-04-23 15:10:12 +0200536/**
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 */
542int nc_tls_get_num_pending_bytes_wrap(void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200543
romanb2a73b82024-04-23 15:10:12 +0200544/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200550int nc_tls_get_fd_wrap(const struct nc_session *session);
551
romanb2a73b82024-04-23 15:10:12 +0200552/**
553 * @brief Close a TLS session.
554 *
555 * @param[in] tls_session TLS session.
556 */
roman4b64fed2024-04-05 12:28:35 +0200557void nc_tls_close_notify_wrap(void *tls_session);
558
romanb2a73b82024-04-23 15:10:12 +0200559/**
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 */
565void * nc_tls_import_privkey_file_wrap(const char *privkey_path);
roman4b64fed2024-04-05 12:28:35 +0200566
romanb2a73b82024-04-23 15:10:12 +0200567/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200573void * nc_tls_import_cert_file_wrap(const char *cert_path);
574
romanb2a73b82024-04-23 15:10:12 +0200575/**
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 */
581char * nc_tls_export_privkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200582
romanb2a73b82024-04-23 15:10:12 +0200583/**
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 */
589char * nc_tls_export_cert_pem_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200590
romanb2a73b82024-04-23 15:10:12 +0200591/**
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 */
597char * nc_tls_export_pubkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200598
romanb2a73b82024-04-23 15:10:12 +0200599/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200605int nc_tls_privkey_is_rsa_wrap(void *pkey);
606
romanb2a73b82024-04-23 15:10:12 +0200607/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200615int nc_tls_get_rsa_pubkey_params_wrap(void *pkey, void **e, void **n);
616
romanb2a73b82024-04-23 15:10:12 +0200617/**
618 * @brief Destroy an MPI.
619 *
620 * @param[in] mpi MPI.
621 */
622void 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 */
roman4b64fed2024-04-05 12:28:35 +0200630int nc_tls_privkey_is_ec_wrap(void *pkey);
631
romanb2a73b82024-04-23 15:10:12 +0200632/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200638char * nc_tls_get_ec_group_wrap(void *pkey);
639
romanb2a73b82024-04-23 15:10:12 +0200640/**
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 */
648int nc_tls_get_ec_pubkey_params_wrap(void *pkey, void **q, void **q_grp);
roman4b64fed2024-04-05 12:28:35 +0200649
romanb2a73b82024-04-23 15:10:12 +0200650/**
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 */
659int nc_tls_ec_point_to_bin_wrap(void *q, void *q_grp, unsigned char **bin, int *bin_len);
roman4b64fed2024-04-05 12:28:35 +0200660
romanb2a73b82024-04-23 15:10:12 +0200661/**
662 * @brief Destroy an EC point.
663 *
664 * @param[in] p EC point.
665 */
666void nc_tls_ec_point_destroy_wrap(void *p);
roman4b64fed2024-04-05 12:28:35 +0200667
romanb2a73b82024-04-23 15:10:12 +0200668/**
669 * @brief Destroy an EC group.
670 *
671 * @param[in] grp EC group.
672 */
673void 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 */
683int 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 */
roman4b64fed2024-04-05 12:28:35 +0200691void * nc_tls_import_pubkey_file_wrap(const char *pubkey_path);
692
romanb2a73b82024-04-23 15:10:12 +0200693/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200701int nc_server_tls_get_crl_distpoint_uris_wrap(void *cert_store, char ***uris, int *uri_count);
702
romanb2a73b82024-04-23 15:10:12 +0200703/**
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 */
710int 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 */
719int 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 */
727void nc_server_tls_set_cipher_suites_wrap(void *tls_cfg, void *cipher_suites);
728
roman4b64fed2024-04-05 12:28:35 +0200729#endif