1 /* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #pragma once
18
19 #include <assert.h>
20 #include <dirent.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <gnutls/abstract.h>
27 #include <gnutls/crypto.h>
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
30
31 #include "libdnssec/binary.h"
32 #include "libknot/attribute.h"
33
34 /*!
35 * Macro to clear a structure of known size.
36 *
37 * \param pointer Pointer to the structure.
38 */
39 #define clear_struct(pointer) memset((pointer), '\0', sizeof(*(pointer)))
40
41 /* -- cleanup macros ------------------------------------------------------- */
42
free_ptr(void * ptr)43 static inline void free_ptr(void *ptr)
44 {
45 free(*(void **)ptr);
46 }
47
close_ptr(int * ptr)48 static inline void close_ptr(int *ptr)
49 {
50 if (*ptr != -1) {
51 close(*ptr);
52 }
53 }
54
fclose_ptr(FILE ** ptr)55 static inline void fclose_ptr(FILE **ptr)
56 {
57 if (*ptr) {
58 fclose(*ptr);
59 }
60 }
61
closedir_ptr(DIR ** ptr)62 static inline void closedir_ptr(DIR **ptr)
63 {
64 if (*ptr) {
65 closedir(*ptr);
66 }
67 }
68
free_gnutls_datum_ptr(gnutls_datum_t * ptr)69 static inline void free_gnutls_datum_ptr(gnutls_datum_t *ptr)
70 {
71 gnutls_free(ptr->data);
72 }
73
free_x509_privkey_ptr(gnutls_x509_privkey_t * ptr)74 static inline void free_x509_privkey_ptr(gnutls_x509_privkey_t *ptr)
75 {
76 if (*ptr) {
77 gnutls_x509_privkey_deinit(*ptr);
78 }
79 }
80
free_pubkey_ptr(gnutls_pubkey_t * ptr)81 static inline void free_pubkey_ptr(gnutls_pubkey_t *ptr)
82 {
83 if (*ptr) {
84 gnutls_pubkey_deinit(*ptr);
85 }
86 }
87
free_gnutls_hash_ptr(gnutls_hash_hd_t * ptr)88 static inline void free_gnutls_hash_ptr(gnutls_hash_hd_t *ptr)
89 {
90 if (*ptr) {
91 gnutls_hash_deinit(*ptr, NULL);
92 }
93 }
94
95 #define _cleanup_free_ _cleanup_(free_ptr)
96 #define _cleanup_close_ _cleanup_(close_ptr)
97 #define _cleanup_fclose_ _cleanup_(fclose_ptr)
98 #define _cleanup_closedir_ _cleanup_(closedir_ptr)
99 #define _cleanup_binary_ _cleanup_(dnssec_binary_free)
100 #define _cleanup_datum_ _cleanup_(free_gnutls_datum_ptr)
101 #define _cleanup_x509_privkey_ _cleanup_(free_x509_privkey_ptr)
102 #define _cleanup_pubkey_ _cleanup_(free_pubkey_ptr)
103 #define _cleanup_hash_ _cleanup_(free_gnutls_hash_ptr)
104
105 /* -- assertions ----------------------------------------------------------- */
106
107 #define assert_unreachable() assert(0)
108
109 /* -- crypto helpers ------------------------------------------------------- */
110
binary_to_datum(const dnssec_binary_t * from)111 static inline gnutls_datum_t binary_to_datum(const dnssec_binary_t *from)
112 {
113 gnutls_datum_t to = { .size = from->size, .data = from->data };
114 return to;
115 }
116
binary_from_datum(const gnutls_datum_t * from)117 static inline dnssec_binary_t binary_from_datum(const gnutls_datum_t *from)
118 {
119 dnssec_binary_t to = { .size = from->size, .data = from->data };
120 return to;
121 }
122