]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/dnscrypt/dnscrypt.h
Upgrade Unbound to 1.6.6. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / dnscrypt / dnscrypt.h
1 #ifndef UNBOUND_DNSCRYPT_H
2 #define UNBOUND_DNSCRYPT_H
3
4 /**
5  * \file
6  * dnscrypt functions for encrypting DNS packets.
7  */
8
9 #include "dnscrypt/dnscrypt_config.h"
10 #ifdef USE_DNSCRYPT
11
12 #define DNSCRYPT_MAGIC_HEADER_LEN 8U
13 #define DNSCRYPT_MAGIC_RESPONSE  "r6fnvWj8"
14
15 #ifndef DNSCRYPT_MAX_PADDING
16 # define DNSCRYPT_MAX_PADDING 256U
17 #endif
18 #ifndef DNSCRYPT_BLOCK_SIZE
19 # define DNSCRYPT_BLOCK_SIZE 64U
20 #endif
21 #ifndef DNSCRYPT_MIN_PAD_LEN
22 # define DNSCRYPT_MIN_PAD_LEN 8U
23 #endif
24
25 #define crypto_box_HALF_NONCEBYTES (crypto_box_NONCEBYTES / 2U)
26
27 #include "config.h"
28 #include "dnscrypt/cert.h"
29 #include "util/locks.h"
30
31 #define DNSCRYPT_QUERY_HEADER_SIZE \
32     (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_PUBLICKEYBYTES + crypto_box_HALF_NONCEBYTES + crypto_box_MACBYTES)
33 #define DNSCRYPT_RESPONSE_HEADER_SIZE \
34     (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_NONCEBYTES + crypto_box_MACBYTES)
35
36 #define DNSCRYPT_REPLY_HEADER_SIZE \
37     (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_HALF_NONCEBYTES * 2 + crypto_box_MACBYTES)
38
39 struct sldns_buffer;
40 struct config_file;
41 struct comm_reply;
42 struct slabhash;
43
44 typedef struct KeyPair_ {
45     uint8_t crypt_publickey[crypto_box_PUBLICKEYBYTES];
46     uint8_t crypt_secretkey[crypto_box_SECRETKEYBYTES];
47 } KeyPair;
48
49 typedef struct cert_ {
50     uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
51     uint8_t es_version[2];
52     KeyPair *keypair;
53 } dnsccert;
54
55 struct dnsc_env {
56         struct SignedCert *signed_certs;
57         dnsccert *certs;
58         size_t signed_certs_count;
59         uint8_t provider_publickey[crypto_sign_ed25519_PUBLICKEYBYTES];
60         uint8_t provider_secretkey[crypto_sign_ed25519_SECRETKEYBYTES];
61         KeyPair *keypairs;
62         size_t keypairs_count;
63         uint64_t nonce_ts_last;
64         unsigned char hash_key[crypto_shorthash_KEYBYTES];
65         char * provider_name;
66         struct slabhash *shared_secrets_cache;
67         /** lock on shared secret cache counters */
68         lock_basic_type shared_secrets_cache_lock;
69         /** number of misses from shared_secrets_cache */
70         size_t num_query_dnscrypt_secret_missed_cache;
71 };
72
73 struct dnscrypt_query_header {
74     uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
75     uint8_t publickey[crypto_box_PUBLICKEYBYTES];
76     uint8_t nonce[crypto_box_HALF_NONCEBYTES];
77     uint8_t mac[crypto_box_MACBYTES];
78 };
79
80 /**
81  * Initialize DNSCrypt environment.
82  * Initialize sodium library and allocate the dnsc_env structure.
83  * \return an uninitialized struct dnsc_env.
84  */
85 struct dnsc_env * dnsc_create(void);
86
87 /**
88  * Apply configuration.
89  * Read certificates and secret keys from configuration. Initialize hashkey and
90  * provider name as well as loading cert TXT records.
91  * In case of issue applying configuration, this function fatals.
92  * \param[in] env the struct dnsc_env to populate.
93  * \param[in] cfg the config_file struct with dnscrypt options.
94  * \return 0 on success.
95  */
96 int dnsc_apply_cfg(struct dnsc_env *env, struct config_file *cfg);
97
98 /**
99  * Delete DNSCrypt environment
100  *
101  */
102 void dnsc_delete(struct dnsc_env *env);
103
104 /**
105  * handle a crypted dnscrypt request.
106  * Determine wether or not a query is coming over the dnscrypt listener and
107  * attempt to uncurve it or detect if it is a certificate query.
108  * return 0 in case of failure.
109  */
110 int dnsc_handle_curved_request(struct dnsc_env* dnscenv,
111                                struct comm_reply* repinfo);
112 /**
113  * handle an unencrypted dnscrypt request.
114  * Determine wether or not a query is going over the dnscrypt channel and
115  * attempt to curve it unless it was not crypted like when  it is a
116  * certificate query.
117  * \return 0 in case of failure.
118  */
119
120 int dnsc_handle_uncurved_request(struct comm_reply *repinfo);
121
122 /**
123  * Computes the size of the shared secret cache entry.
124  */
125 size_t dnsc_shared_secrets_sizefunc(void *k, void *d);
126
127 /**
128  * Compares two shared secret cache keys.
129  */
130 int dnsc_shared_secrets_compfunc(void *m1, void *m2);
131
132 /**
133  * Function to delete a shared secret cache key.
134  */
135 void dnsc_shared_secrets_delkeyfunc(void *k, void* arg);
136
137 /**
138  * Function to delete a share secret cache value.
139  */
140 void dnsc_shared_secrets_deldatafunc(void* d, void* arg);
141
142 #endif /* USE_DNSCRYPT */
143 #endif