blob: 43c5d74f8bcfc67bca945599d2da1fc190c689f7 [file] [log] [blame]
roman4b64fed2024-04-05 12:28:35 +02001#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
roman4b64fed2024-04-05 12:28:35 +020010#include <mbedtls/ctr_drbg.h>
roman008cfe72024-04-05 12:36:18 +020011#include <mbedtls/ssl.h>
roman4b64fed2024-04-05 12:28:35 +020012
13struct nc_tls_ctx {
roman008cfe72024-04-05 12:36:18 +020014 int *sock;
roman4b64fed2024-04-05 12:28:35 +020015 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
romanb2a73b82024-04-23 15:10:12 +020025#include <openssl/ssl.h>
roman4b64fed2024-04-05 12:28:35 +020026
27struct nc_tls_ctx {
romanb2a73b82024-04-23 15:10:12 +020028 X509 *cert;
29 EVP_PKEY *pkey;
30 X509_STORE *cert_store;
31 X509_STORE *crl_store;
roman4b64fed2024-04-05 12:28:35 +020032};
33
34#endif
35
roman4b64fed2024-04-05 12:28:35 +020036struct nc_tls_verify_cb_data {
37 struct nc_session *session;
roman4b64fed2024-04-05 12:28:35 +020038 struct nc_server_tls_opts *opts;
39 struct nc_ctn_data {
roman008cfe72024-04-05 12:36:18 +020040 char *username;
41 int matched_ctns;
42 int matched_ctn_type[6];
43 int matched_ctn_count;
roman4b64fed2024-04-05 12:28:35 +020044 } ctn_data;
45};
46
romanb2a73b82024-04-23 15:10:12 +020047/**
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 */
roman4b64fed2024-04-05 12:28:35 +020053void * nc_tls_session_new_wrap(void *tls_cfg);
54
romanb2a73b82024-04-23 15:10:12 +020055/**
56 * @brief Destroys a TLS session.
57 *
58 * @param[in] tls_session TLS session to destroy.
59 */
roman4b64fed2024-04-05 12:28:35 +020060void nc_tls_session_destroy_wrap(void *tls_session);
61
romanb2a73b82024-04-23 15:10:12 +020062/**
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 */
68void * nc_tls_config_new_wrap(int side);
roman4b64fed2024-04-05 12:28:35 +020069
romanb2a73b82024-04-23 15:10:12 +020070/**
71 * @brief Destroys a TLS configuration.
72 *
73 * @param[in] tls_cfg TLS configuration to destroy.
74 */
roman4b64fed2024-04-05 12:28:35 +020075void nc_tls_config_destroy_wrap(void *tls_cfg);
76
romanb2a73b82024-04-23 15:10:12 +020077/**
78 * @brief Creates a new TLS certificate.
79 *
80 * @return New TLS certificate on success, NULL on fail.
81 */
roman4b64fed2024-04-05 12:28:35 +020082void * nc_tls_cert_new_wrap();
83
romanb2a73b82024-04-23 15:10:12 +020084/**
85 * @brief Destroys a TLS certificate.
86 *
87 * @param[in] cert TLS certificate to destroy.
88 */
roman4b64fed2024-04-05 12:28:35 +020089void nc_tls_cert_destroy_wrap(void *cert);
90
romanb2a73b82024-04-23 15:10:12 +020091/**
92 * @brief Destroys a TLS private key.
93 *
94 * @param[in] pkey TLS private key to destroy.
95 */
roman4b64fed2024-04-05 12:28:35 +020096void nc_tls_privkey_destroy_wrap(void *pkey);
97
romanb2a73b82024-04-23 15:10:12 +020098/**
99 * @brief Creates a new TLS certificate store.
100 *
101 * @return New TLS certificate store on success, NULL on fail.
102 */
roman4b64fed2024-04-05 12:28:35 +0200103void * nc_tls_cert_store_new_wrap();
104
romanb2a73b82024-04-23 15:10:12 +0200105/**
106 * @brief Destroys a TLS certificate store.
107 *
108 * @param[in] cert_store TLS certificate store to destroy.
109 */
roman4b64fed2024-04-05 12:28:35 +0200110void nc_tls_cert_store_destroy_wrap(void *cert_store);
111
romanb2a73b82024-04-23 15:10:12 +0200112/**
113 * @brief Creates a new CRL store.
114 *
115 * @return New CRL store on success, NULL on fail.
116 */
roman4b64fed2024-04-05 12:28:35 +0200117void * nc_tls_crl_store_new_wrap();
118
romanb2a73b82024-04-23 15:10:12 +0200119/**
120 * @brief Destroys a CRL store.
121 *
122 * @param[in] crl_store CRL store to destroy.
123 */
124void nc_tls_crl_store_destroy_wrap(void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200125
romanb2a73b82024-04-23 15:10:12 +0200126/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200132void * nc_tls_pem_to_cert_wrap(const char *cert_data);
133
romanb2a73b82024-04-23 15:10:12 +0200134/**
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 */
141int nc_tls_add_cert_to_store_wrap(void *cert, void *cert_store);
roman4b64fed2024-04-05 12:28:35 +0200142
romanb2a73b82024-04-23 15:10:12 +0200143/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200149void * nc_tls_pem_to_privkey_wrap(const char *privkey_data);
150
romanb2a73b82024-04-23 15:10:12 +0200151/**
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 */
158int nc_tls_import_crl_path_wrap(const char *path, void *crl_store);
roman4b64fed2024-04-05 12:28:35 +0200159
romanb2a73b82024-04-23 15:10:12 +0200160/**
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 */
168int 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 +0200169
romanb2a73b82024-04-23 15:10:12 +0200170/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200178int nc_server_tls_set_tls_versions_wrap(void *tls_cfg, unsigned int tls_versions);
179
romanb2a73b82024-04-23 15:10:12 +0200180/**
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 */
186void nc_server_tls_set_verify_wrap(void *tls_cfg, struct nc_tls_verify_cb_data *cb_data);
roman4b64fed2024-04-05 12:28:35 +0200187
romanb2a73b82024-04-23 15:10:12 +0200188/**
189 * @brief Set TLS client's verify flags.
190 *
191 * @param[in] tls_cfg TLS configuration.
192 */
193void 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 */
roman4b64fed2024-04-05 12:28:35 +0200204int nc_server_tls_verify_cert(void *cert, int depth, int self_signed, struct nc_tls_verify_cb_data *cb_data);
205
romanb2a73b82024-04-23 15:10:12 +0200206/**
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 */
213int 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 */
roman4b64fed2024-04-05 12:28:35 +0200221char * nc_server_tls_get_subject_wrap(void *cert);
222
romanb2a73b82024-04-23 15:10:12 +0200223/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200229char * nc_server_tls_get_issuer_wrap(void *cert);
230
romanb2a73b82024-04-23 15:10:12 +0200231/**
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 */
237void * nc_tls_get_sans_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200238
romanb2a73b82024-04-23 15:10:12 +0200239/**
240 * @brief Destroy the SANs.
241 *
242 * @param[in] sans SANs to destroy.
243 */
244void 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 */
252int 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 */
263int 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 */
roman4b64fed2024-04-05 12:28:35 +0200272int nc_server_tls_certs_match_wrap(void *cert1, void *cert2);
273
romanb2a73b82024-04-23 15:10:12 +0200274/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200281int nc_server_tls_md5_wrap(void *cert, unsigned char *buf);
282
romanb2a73b82024-04-23 15:10:12 +0200283/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200290int nc_server_tls_sha1_wrap(void *cert, unsigned char *buf);
291
romanb2a73b82024-04-23 15:10:12 +0200292/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200299int nc_server_tls_sha224_wrap(void *cert, unsigned char *buf);
300
romanb2a73b82024-04-23 15:10:12 +0200301/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200308int nc_server_tls_sha256_wrap(void *cert, unsigned char *buf);
309
romanb2a73b82024-04-23 15:10:12 +0200310/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200317int nc_server_tls_sha384_wrap(void *cert, unsigned char *buf);
318
romanb2a73b82024-04-23 15:10:12 +0200319/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200326int nc_server_tls_sha512_wrap(void *cert, unsigned char *buf);
327
romanb2a73b82024-04-23 15:10:12 +0200328/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200335void nc_server_tls_set_fd_wrap(void *tls_session, int sock, struct nc_tls_ctx *tls_ctx);
336
romanb2a73b82024-04-23 15:10:12 +0200337/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200343int nc_server_tls_handshake_step_wrap(void *tls_session);
344
romanb2a73b82024-04-23 15:10:12 +0200345/**
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 */
352int nc_client_tls_handshake_step_wrap(void *tls_session, int sock);
roman4b64fed2024-04-05 12:28:35 +0200353
romanb2a73b82024-04-23 15:10:12 +0200354/**
355 * @brief Destroy a TLS context.
356 *
357 * @param[in] tls_ctx TLS context.
358 */
roman4b64fed2024-04-05 12:28:35 +0200359void nc_tls_ctx_destroy_wrap(struct nc_tls_ctx *tls_ctx);
360
romanb2a73b82024-04-23 15:10:12 +0200361/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200370int nc_client_tls_load_cert_key_wrap(const char *cert_path, const char *key_path, void **cert, void **pkey);
371
romanb2a73b82024-04-23 15:10:12 +0200372/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200380int nc_client_tls_load_trusted_certs_wrap(void *cert_store, const char *file_path, const char *dir_path);
381
romanb2a73b82024-04-23 15:10:12 +0200382/**
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 */
390int nc_client_tls_load_crl_wrap(void *crl_store, const char *file_path, const char *dir_path);
roman4b64fed2024-04-05 12:28:35 +0200391
romanb2a73b82024-04-23 15:10:12 +0200392/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200399int nc_client_tls_set_hostname_wrap(void *tls_session, const char *hostname);
400
romanb2a73b82024-04-23 15:10:12 +0200401/**
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 */
412int 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 +0200413
romanb2a73b82024-04-23 15:10:12 +0200414/**
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 */
422int 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 */
roman4b64fed2024-04-05 12:28:35 +0200430uint32_t nc_tls_get_verify_result_wrap(void *tls_session);
431
romanb2a73b82024-04-23 15:10:12 +0200432/**
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 */
438char * nc_tls_verify_error_string_wrap(uint32_t err_code);
roman4b64fed2024-04-05 12:28:35 +0200439
romanb2a73b82024-04-23 15:10:12 +0200440/**
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 */
447void nc_client_tls_print_connect_err_wrap(int connect_ret, const char *peername, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200448
romanb2a73b82024-04-23 15:10:12 +0200449/**
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 */
455void nc_server_tls_print_accept_err_wrap(int accept_ret, void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200456
romanb2a73b82024-04-23 15:10:12 +0200457/**
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 */
465int nc_tls_is_der_subpubkey_wrap(unsigned char *der, long len);
roman4b64fed2024-04-05 12:28:35 +0200466
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 */
romanb2a73b82024-04-23 15:10:12 +0200474int nc_base64_decode_wrap(const char *base64, unsigned char **bin);
roman4b64fed2024-04-05 12:28:35 +0200475
romanb2a73b82024-04-23 15:10:12 +0200476/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200484int nc_base64_encode_wrap(const unsigned char *bin, size_t len, char **base64);
485
romanb2a73b82024-04-23 15:10:12 +0200486/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200494int nc_tls_read_wrap(struct nc_session *session, unsigned char *buf, size_t size);
495
romanb2a73b82024-04-23 15:10:12 +0200496/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200504int nc_tls_write_wrap(struct nc_session *session, const unsigned char *buf, size_t size);
505
romanb2a73b82024-04-23 15:10:12 +0200506/**
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 */
512int nc_tls_get_num_pending_bytes_wrap(void *tls_session);
roman4b64fed2024-04-05 12:28:35 +0200513
romanb2a73b82024-04-23 15:10:12 +0200514/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200520int nc_tls_get_fd_wrap(const struct nc_session *session);
521
romanb2a73b82024-04-23 15:10:12 +0200522/**
523 * @brief Close a TLS session.
524 *
525 * @param[in] tls_session TLS session.
526 */
roman4b64fed2024-04-05 12:28:35 +0200527void nc_tls_close_notify_wrap(void *tls_session);
528
romanb2a73b82024-04-23 15:10:12 +0200529/**
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 */
535void * nc_tls_import_privkey_file_wrap(const char *privkey_path);
roman4b64fed2024-04-05 12:28:35 +0200536
romanb2a73b82024-04-23 15:10:12 +0200537/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200543void * nc_tls_import_cert_file_wrap(const char *cert_path);
544
romanb2a73b82024-04-23 15:10:12 +0200545/**
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 */
551char * nc_tls_export_privkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200552
romanb2a73b82024-04-23 15:10:12 +0200553/**
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 */
559char * nc_tls_export_cert_pem_wrap(void *cert);
roman4b64fed2024-04-05 12:28:35 +0200560
romanb2a73b82024-04-23 15:10:12 +0200561/**
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 */
567char * nc_tls_export_pubkey_pem_wrap(void *pkey);
roman4b64fed2024-04-05 12:28:35 +0200568
romanb2a73b82024-04-23 15:10:12 +0200569/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200575int nc_tls_privkey_is_rsa_wrap(void *pkey);
576
romanb2a73b82024-04-23 15:10:12 +0200577/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200585int nc_tls_get_rsa_pubkey_params_wrap(void *pkey, void **e, void **n);
586
romanb2a73b82024-04-23 15:10:12 +0200587/**
588 * @brief Destroy an MPI.
589 *
590 * @param[in] mpi MPI.
591 */
592void 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 */
roman4b64fed2024-04-05 12:28:35 +0200600int nc_tls_privkey_is_ec_wrap(void *pkey);
601
romanb2a73b82024-04-23 15:10:12 +0200602/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200608char * nc_tls_get_ec_group_wrap(void *pkey);
609
romanb2a73b82024-04-23 15:10:12 +0200610/**
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 */
618int nc_tls_get_ec_pubkey_params_wrap(void *pkey, void **q, void **q_grp);
roman4b64fed2024-04-05 12:28:35 +0200619
romanb2a73b82024-04-23 15:10:12 +0200620/**
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 */
629int nc_tls_ec_point_to_bin_wrap(void *q, void *q_grp, unsigned char **bin, int *bin_len);
roman4b64fed2024-04-05 12:28:35 +0200630
romanb2a73b82024-04-23 15:10:12 +0200631/**
632 * @brief Destroy an EC point.
633 *
634 * @param[in] p EC point.
635 */
636void nc_tls_ec_point_destroy_wrap(void *p);
roman4b64fed2024-04-05 12:28:35 +0200637
romanb2a73b82024-04-23 15:10:12 +0200638/**
639 * @brief Destroy an EC group.
640 *
641 * @param[in] grp EC group.
642 */
643void 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 */
653int 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 */
roman4b64fed2024-04-05 12:28:35 +0200661void * nc_tls_import_pubkey_file_wrap(const char *pubkey_path);
662
romanb2a73b82024-04-23 15:10:12 +0200663/**
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 */
roman4b64fed2024-04-05 12:28:35 +0200671int nc_server_tls_get_crl_distpoint_uris_wrap(void *cert_store, char ***uris, int *uri_count);
672
romanb2a73b82024-04-23 15:10:12 +0200673/**
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 */
680int 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 */
689int 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 */
697void nc_server_tls_set_cipher_suites_wrap(void *tls_cfg, void *cipher_suites);
698
roman4b64fed2024-04-05 12:28:35 +0200699#endif