blob: 170f4f1ddecb5c1c681334289e827b73c0e84005 [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
32struct nc_tls_ctx {
roman008cfe72024-04-05 12:36:18 +020033 int *sock;
roman4b64fed2024-04-05 12:28:35 +020034 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
romanc787d282024-04-25 16:10:46 +020044#include <openssl/evp.h>
romanb2a73b82024-04-23 15:10:12 +020045#include <openssl/ssl.h>
romanc787d282024-04-25 16:10:46 +020046#include <openssl/x509.h>
roman4b64fed2024-04-05 12:28:35 +020047
48struct nc_tls_ctx {
romanb2a73b82024-04-23 15:10:12 +020049 X509 *cert;
50 EVP_PKEY *pkey;
51 X509_STORE *cert_store;
52 X509_STORE *crl_store;
roman4b64fed2024-04-05 12:28:35 +020053};
54
55#endif
56
roman4b64fed2024-04-05 12:28:35 +020057struct nc_tls_verify_cb_data {
58 struct nc_session *session;
roman4b64fed2024-04-05 12:28:35 +020059 struct nc_server_tls_opts *opts;
60 struct nc_ctn_data {
roman008cfe72024-04-05 12:36:18 +020061 char *username;
62 int matched_ctns;
63 int matched_ctn_type[6];
64 int matched_ctn_count;
roman4b64fed2024-04-05 12:28:35 +020065 } ctn_data;
66};
67
romanb2a73b82024-04-23 15:10:12 +020068/**
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 */
roman4b64fed2024-04-05 12:28:35 +020074void * nc_tls_session_new_wrap(void *tls_cfg);
75
romanb2a73b82024-04-23 15:10:12 +020076/**
77 * @brief Destroys a TLS session.
78 *
79 * @param[in] tls_session TLS session to destroy.
80 */
roman4b64fed2024-04-05 12:28:35 +020081void nc_tls_session_destroy_wrap(void *tls_session);
82
romanb2a73b82024-04-23 15:10:12 +020083/**
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 */
89void * nc_tls_config_new_wrap(int side);
roman4b64fed2024-04-05 12:28:35 +020090
romanb2a73b82024-04-23 15:10:12 +020091/**
92 * @brief Destroys a TLS configuration.
93 *
94 * @param[in] tls_cfg TLS configuration to destroy.
95 */
roman4b64fed2024-04-05 12:28:35 +020096void nc_tls_config_destroy_wrap(void *tls_cfg);
97
romanb2a73b82024-04-23 15:10:12 +020098/**
99 * @brief Creates a new TLS certificate.
100 *
101 * @return New TLS certificate on success, NULL on fail.
102 */
romanb87aa452024-05-13 12:58:00 +0200103void * nc_tls_cert_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200104
romanb2a73b82024-04-23 15:10:12 +0200105/**
106 * @brief Destroys a TLS certificate.
107 *
108 * @param[in] cert TLS certificate to destroy.
109 */
roman4b64fed2024-04-05 12:28:35 +0200110void nc_tls_cert_destroy_wrap(void *cert);
111
romanb2a73b82024-04-23 15:10:12 +0200112/**
113 * @brief Destroys a TLS private key.
114 *
115 * @param[in] pkey TLS private key to destroy.
116 */
roman4b64fed2024-04-05 12:28:35 +0200117void nc_tls_privkey_destroy_wrap(void *pkey);
118
romanb2a73b82024-04-23 15:10:12 +0200119/**
120 * @brief Creates a new TLS certificate store.
121 *
122 * @return New TLS certificate store on success, NULL on fail.
123 */
romanb87aa452024-05-13 12:58:00 +0200124void * nc_tls_cert_store_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200125
romanb2a73b82024-04-23 15:10:12 +0200126/**
127 * @brief Destroys a TLS certificate store.
128 *
129 * @param[in] cert_store TLS certificate store to destroy.
130 */
roman4b64fed2024-04-05 12:28:35 +0200131void nc_tls_cert_store_destroy_wrap(void *cert_store);
132
romanb2a73b82024-04-23 15:10:12 +0200133/**
134 * @brief Creates a new CRL store.
135 *
136 * @return New CRL store on success, NULL on fail.
137 */
romanb87aa452024-05-13 12:58:00 +0200138void * nc_tls_crl_store_new_wrap(void);
roman4b64fed2024-04-05 12:28:35 +0200139
romanb2a73b82024-04-23 15:10:12 +0200140/**
141 * @brief Destroys a CRL store.
142 *
143 * @param[in] crl_store CRL store to destroy.
144 */
145void nc_tls_crl_store_destroy_wrap(void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200146
romanb2a73b82024-04-23 15:10:12 +0200147/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200153void * nc_tls_pem_to_cert_wrap(const char *cert_data);
154
romanb2a73b82024-04-23 15:10:12 +0200155/**
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 */
162int nc_tls_add_cert_to_store_wrap(void *cert, void *cert_store);
roman4b64fed2024-04-05 12:28:35 +0200163
romanb2a73b82024-04-23 15:10:12 +0200164/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200170void * nc_tls_pem_to_privkey_wrap(const char *privkey_data);
171
romanb2a73b82024-04-23 15:10:12 +0200172/**
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 */
179int nc_tls_import_crl_path_wrap(const char *path, void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200180
romanb2a73b82024-04-23 15:10:12 +0200181/**
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 */
189int 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 +0200190
romanb2a73b82024-04-23 15:10:12 +0200191/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200199int nc_server_tls_set_tls_versions_wrap(void *tls_cfg, unsigned int tls_versions);
200
romanb2a73b82024-04-23 15:10:12 +0200201/**
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 */
207void nc_server_tls_set_verify_wrap(void *tls_cfg, struct nc_tls_verify_cb_data *cb_data);
roman4b64fed2024-04-05 12:28:35 +0200208
romanb2a73b82024-04-23 15:10:12 +0200209/**
210 * @brief Set TLS client's verify flags.
211 *
212 * @param[in] tls_cfg TLS configuration.
213 */
214void 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 */
roman4b64fed2024-04-05 12:28:35 +0200225int nc_server_tls_verify_cert(void *cert, int depth, int self_signed, struct nc_tls_verify_cb_data *cb_data);
226
romanb2a73b82024-04-23 15:10:12 +0200227/**
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 */
234int 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 */
roman4b64fed2024-04-05 12:28:35 +0200242char * nc_server_tls_get_subject_wrap(void *cert);
243
romanb2a73b82024-04-23 15:10:12 +0200244/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200250char * nc_server_tls_get_issuer_wrap(void *cert);
251
romanb2a73b82024-04-23 15:10:12 +0200252/**
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 */
258void * nc_tls_get_sans_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200259
romanb2a73b82024-04-23 15:10:12 +0200260/**
261 * @brief Destroy the SANs.
262 *
263 * @param[in] sans SANs to destroy.
264 */
265void 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 */
273int 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 */
284int 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 */
roman4b64fed2024-04-05 12:28:35 +0200293int nc_server_tls_certs_match_wrap(void *cert1, void *cert2);
294
romanb2a73b82024-04-23 15:10:12 +0200295/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200302int nc_server_tls_md5_wrap(void *cert, unsigned char *buf);
303
romanb2a73b82024-04-23 15:10:12 +0200304/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200311int nc_server_tls_sha1_wrap(void *cert, unsigned char *buf);
312
romanb2a73b82024-04-23 15:10:12 +0200313/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200320int nc_server_tls_sha224_wrap(void *cert, unsigned char *buf);
321
romanb2a73b82024-04-23 15:10:12 +0200322/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200329int nc_server_tls_sha256_wrap(void *cert, unsigned char *buf);
330
romanb2a73b82024-04-23 15:10:12 +0200331/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200338int nc_server_tls_sha384_wrap(void *cert, unsigned char *buf);
339
romanb2a73b82024-04-23 15:10:12 +0200340/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200347int nc_server_tls_sha512_wrap(void *cert, unsigned char *buf);
348
romanb2a73b82024-04-23 15:10:12 +0200349/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200356void nc_server_tls_set_fd_wrap(void *tls_session, int sock, struct nc_tls_ctx *tls_ctx);
357
romanb2a73b82024-04-23 15:10:12 +0200358/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200364int nc_server_tls_handshake_step_wrap(void *tls_session);
365
romanb2a73b82024-04-23 15:10:12 +0200366/**
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 */
373int nc_client_tls_handshake_step_wrap(void *tls_session, int sock);
roman4b64fed2024-04-05 12:28:35 +0200374
romanb2a73b82024-04-23 15:10:12 +0200375/**
376 * @brief Destroy a TLS context.
377 *
378 * @param[in] tls_ctx TLS context.
379 */
roman4b64fed2024-04-05 12:28:35 +0200380void nc_tls_ctx_destroy_wrap(struct nc_tls_ctx *tls_ctx);
381
romanb2a73b82024-04-23 15:10:12 +0200382/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200391int nc_client_tls_load_cert_key_wrap(const char *cert_path, const char *key_path, void **cert, void **pkey);
392
romanb2a73b82024-04-23 15:10:12 +0200393/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200401int nc_client_tls_load_trusted_certs_wrap(void *cert_store, const char *file_path, const char *dir_path);
402
romanb2a73b82024-04-23 15:10:12 +0200403/**
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 */
411int nc_client_tls_load_crl_wrap(void *crl_store, const char *file_path, const char *dir_path);
roman4b64fed2024-04-05 12:28:35 +0200412
romanb2a73b82024-04-23 15:10:12 +0200413/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200420int nc_client_tls_set_hostname_wrap(void *tls_session, const char *hostname);
421
romanb2a73b82024-04-23 15:10:12 +0200422/**
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 */
433int 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 +0200434
romanb2a73b82024-04-23 15:10:12 +0200435/**
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 */
443int 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 */
roman4b64fed2024-04-05 12:28:35 +0200451uint32_t nc_tls_get_verify_result_wrap(void *tls_session);
452
romanb2a73b82024-04-23 15:10:12 +0200453/**
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 */
459char * nc_tls_verify_error_string_wrap(uint32_t err_code);
roman4b64fed2024-04-05 12:28:35 +0200460
romanb2a73b82024-04-23 15:10:12 +0200461/**
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 */
468void nc_client_tls_print_connect_err_wrap(int connect_ret, const char *peername, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200469
romanb2a73b82024-04-23 15:10:12 +0200470/**
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 */
476void nc_server_tls_print_accept_err_wrap(int accept_ret, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200477
romanb2a73b82024-04-23 15:10:12 +0200478/**
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 */
486int nc_tls_is_der_subpubkey_wrap(unsigned char *der, long len);
roman4b64fed2024-04-05 12:28:35 +0200487
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 */
romanb2a73b82024-04-23 15:10:12 +0200495int nc_base64_decode_wrap(const char *base64, unsigned char **bin);
roman4b64fed2024-04-05 12:28:35 +0200496
romanb2a73b82024-04-23 15:10:12 +0200497/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200505int nc_base64_encode_wrap(const unsigned char *bin, size_t len, char **base64);
506
romanb2a73b82024-04-23 15:10:12 +0200507/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200515int nc_tls_read_wrap(struct nc_session *session, unsigned char *buf, size_t size);
516
romanb2a73b82024-04-23 15:10:12 +0200517/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200525int nc_tls_write_wrap(struct nc_session *session, const unsigned char *buf, size_t size);
526
romanb2a73b82024-04-23 15:10:12 +0200527/**
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 */
533int nc_tls_get_num_pending_bytes_wrap(void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200534
romanb2a73b82024-04-23 15:10:12 +0200535/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200541int nc_tls_get_fd_wrap(const struct nc_session *session);
542
romanb2a73b82024-04-23 15:10:12 +0200543/**
544 * @brief Close a TLS session.
545 *
546 * @param[in] tls_session TLS session.
547 */
roman4b64fed2024-04-05 12:28:35 +0200548void nc_tls_close_notify_wrap(void *tls_session);
549
romanb2a73b82024-04-23 15:10:12 +0200550/**
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 */
556void * nc_tls_import_privkey_file_wrap(const char *privkey_path);
roman4b64fed2024-04-05 12:28:35 +0200557
romanb2a73b82024-04-23 15:10:12 +0200558/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200564void * nc_tls_import_cert_file_wrap(const char *cert_path);
565
romanb2a73b82024-04-23 15:10:12 +0200566/**
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 */
572char * nc_tls_export_privkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200573
romanb2a73b82024-04-23 15:10:12 +0200574/**
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 */
580char * nc_tls_export_cert_pem_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200581
romanb2a73b82024-04-23 15:10:12 +0200582/**
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 */
588char * nc_tls_export_pubkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200589
romanb2a73b82024-04-23 15:10:12 +0200590/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200596int nc_tls_privkey_is_rsa_wrap(void *pkey);
597
romanb2a73b82024-04-23 15:10:12 +0200598/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200606int nc_tls_get_rsa_pubkey_params_wrap(void *pkey, void **e, void **n);
607
romanb2a73b82024-04-23 15:10:12 +0200608/**
609 * @brief Destroy an MPI.
610 *
611 * @param[in] mpi MPI.
612 */
613void 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 */
roman4b64fed2024-04-05 12:28:35 +0200621int nc_tls_privkey_is_ec_wrap(void *pkey);
622
romanb2a73b82024-04-23 15:10:12 +0200623/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200629char * nc_tls_get_ec_group_wrap(void *pkey);
630
romanb2a73b82024-04-23 15:10:12 +0200631/**
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 */
639int nc_tls_get_ec_pubkey_params_wrap(void *pkey, void **q, void **q_grp);
roman4b64fed2024-04-05 12:28:35 +0200640
romanb2a73b82024-04-23 15:10:12 +0200641/**
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 */
650int nc_tls_ec_point_to_bin_wrap(void *q, void *q_grp, unsigned char **bin, int *bin_len);
roman4b64fed2024-04-05 12:28:35 +0200651
romanb2a73b82024-04-23 15:10:12 +0200652/**
653 * @brief Destroy an EC point.
654 *
655 * @param[in] p EC point.
656 */
657void nc_tls_ec_point_destroy_wrap(void *p);
roman4b64fed2024-04-05 12:28:35 +0200658
romanb2a73b82024-04-23 15:10:12 +0200659/**
660 * @brief Destroy an EC group.
661 *
662 * @param[in] grp EC group.
663 */
664void 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 */
674int 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 */
roman4b64fed2024-04-05 12:28:35 +0200682void * nc_tls_import_pubkey_file_wrap(const char *pubkey_path);
683
romanb2a73b82024-04-23 15:10:12 +0200684/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200692int nc_server_tls_get_crl_distpoint_uris_wrap(void *cert_store, char ***uris, int *uri_count);
693
romanb2a73b82024-04-23 15:10:12 +0200694/**
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 */
701int 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 */
710int 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 */
718void nc_server_tls_set_cipher_suites_wrap(void *tls_cfg, void *cipher_suites);
719
roman4b64fed2024-04-05 12:28:35 +0200720#endif