shared.h 3.17 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

17 18
#pragma once

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

33 34
#define _public_ __attribute__((visibility("default")))
#define _hidden_ __attribute__((visibility("hidden")))
35

36
#define _unused_ __attribute__((unused))
37 38 39 40 41 42 43 44

/**
 * Macro to clear a structure of known size.
 *
 * \param pointer Pointer to the structure.
 */
#define clear_struct(pointer) memset((pointer), '\0', sizeof(*(pointer)))

45 46
#define streq(one, two) (strcmp((one), (two)) == 0)

47 48
/* -- cleanup macros ------------------------------------------------------- */

49 50
#define _cleanup_(var) __attribute__((cleanup(var)))

51 52 53 54 55
static inline void free_ptr(void *ptr)
{
	free(*(void **)ptr);
}

56 57 58 59 60 61
static inline void close_ptr(int *ptr)
{
	if (*ptr != -1) {
		close(*ptr);
	}
}
62

63 64 65 66 67 68
static inline void fclose_ptr(FILE **ptr)
{
	if (*ptr) {
		fclose(*ptr);
	}
}
69

70
static inline void closedir_ptr(DIR **ptr)
71
{
72 73 74
	if (*ptr) {
		closedir(*ptr);
	}
75 76
}

77 78 79 80 81
static inline void free_gnutls_datum_ptr(gnutls_datum_t *ptr)
{
	gnutls_free(ptr->data);
}

82 83 84 85 86 87 88
static inline void free_x509_privkey_ptr(gnutls_x509_privkey_t *ptr)
{
	if (*ptr) {
		gnutls_x509_privkey_deinit(*ptr);
	}
}

89 90 91 92 93 94 95
static inline void free_pubkey_ptr(gnutls_pubkey_t *ptr)
{
	if (*ptr) {
		gnutls_pubkey_deinit(*ptr);
	}
}

96 97 98 99 100 101 102
static inline void free_gnutls_hash_ptr(gnutls_hash_hd_t *ptr)
{
	if (*ptr) {
		gnutls_hash_deinit(*ptr, NULL);
	}
}

103
#define _cleanup_free_ _cleanup_(free_ptr)
104
#define _cleanup_close_ _cleanup_(close_ptr)
105
#define _cleanup_fclose_ _cleanup_(fclose_ptr)
106
#define _cleanup_closedir_ _cleanup_(closedir_ptr)
107
#define _cleanup_binary_ _cleanup_(dnssec_binary_free)
108
#define _cleanup_datum_ _cleanup_(free_gnutls_datum_ptr)
109
#define _cleanup_x509_privkey_ _cleanup_(free_x509_privkey_ptr)
110
#define _cleanup_pubkey_ _cleanup_(free_pubkey_ptr)
111
#define _cleanup_hash_ _cleanup_(free_gnutls_hash_ptr)
112

113 114 115 116
/* -- assertions ----------------------------------------------------------- */

#define assert_unreachable() assert(0)

117 118
/* -- crypto helpers ------------------------------------------------------- */

119
static inline gnutls_datum_t binary_to_datum(const dnssec_binary_t *from)
120
{
121 122
	gnutls_datum_t to = { .size = from->size, .data = from->data };
	return to;
123
}
124

125
static inline dnssec_binary_t binary_from_datum(const gnutls_datum_t *from)
126
{
127 128
	dnssec_binary_t to = { .size = from->size, .data = from->data };
	return to;
129
}