libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-auth-dns.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#if defined(LWS_WITH_AUTHORITATIVE_DNS)
26
27struct auth_dns_rr {
28 lws_dll2_t list;
29
30 char *rdata;
31 size_t rdata_len;
32
33 uint8_t *wire_rdata;
34 size_t wire_rdata_len;
35};
36
37struct auth_dns_rrset {
38 lws_dll2_t list;
39 lws_dll2_owner_t rr_list;
40
41 char *name;
42 uint32_t ttl;
43 uint16_t class_;
44 uint16_t type;
45};
46
47struct auth_dns_zone {
48 lws_dll2_owner_t rrset_list;
49 char default_ttl[16];
50 char origin[256];
51 unsigned int origin_lines; /* $ORIGIN directives the text had */
52};
53
55lws_auth_dns_parse_zone_buf(const char *buf, size_t len, struct auth_dns_zone *zone);
56
76lws_auth_dns_zone_check_origin(const struct auth_dns_zone *z,
77 const char *domain);
78
79/*
80 * The clock skew allowed either side of an RRSIG's validity period when
81 * deciding whether a zone is valid now. lws_auth_dns_sign_zone() starts the
82 * period an hour before signing, so this is only for clocks that are wrong.
83 */
84#define LWS_AUTH_DNS_RRSIG_SKEW_SECS 300
85
86/*
87 * How long lws_auth_dns_sign_zone() makes the RRSIGs valid for when it is not
88 * told. Every version of a zone a publisher ever signed verifies for as long
89 * as its RRSIGs are in date, and a node that never held the zone can be
90 * handed any of them, so this is kept short: a publisher re-signs well before
91 * it runs out, as the dnssec-monitor does at half of it.
92 */
93#define LWS_AUTH_DNS_SIGN_VALIDITY_DEFAULT_SECS (6 * 24 * 3600)
94
95enum lws_auth_dns_zone_validity_result {
96 LWS_AUTH_DNS_ZV_OK,
97 LWS_AUTH_DNS_ZV_NO_SOA,
98 LWS_AUTH_DNS_ZV_UNSIGNED,
99 LWS_AUTH_DNS_ZV_NOT_YET_VALID,
100 LWS_AUTH_DNS_ZV_EXPIRED,
101};
102
109struct lws_auth_dns_zone_validity {
110 uint32_t serial;
111 time_t soa_inception;
114 time_t inception;
115 time_t expiration;
116 unsigned int rrsigs;
117 uint8_t soa_signed;
118};
119
143lws_auth_dns_zone_validity(const struct auth_dns_zone *z, time_t now,
144 unsigned int skew,
145 struct lws_auth_dns_zone_validity *v);
146
148lws_auth_dns_free_zone(struct auth_dns_zone *z);
149
165LWS_VISIBLE LWS_EXTERN struct auth_dns_rrset *
166lws_auth_dns_zone_cut(struct auth_dns_zone *z, const char *name);
167
168#define LWS_AUTH_DNS_NSEC3_HASH_LEN 20 /* SHA-1, the only NSEC3 hash */
169
170/*
171 * Anything hashing a zone's NSEC3 owner names spends the zone's iteration
172 * count of CPU per hash, and the zone can be untrusted (eg, DHT-published).
173 * RFC 5155 A.1 caps useful iterations at 150 and RFC 9276 deprecates any
174 * value above 0, so zones asking for more are not hashed.
175 */
176#define LWS_AUTH_DNS_MAX_NSEC3_ITERATIONS 150
177
194lws_auth_dns_nsec3_hash(const uint8_t *wire, size_t wire_len,
195 const uint8_t *salt, size_t salt_len,
196 unsigned int iterations, uint8_t *hash);
197
198/* the largest DNSKEY RDATA we express: 4 + 3 + e + 8192-bit RSA n */
199#define LWS_AUTH_DNS_DNSKEY_WIRE_MAX 1040
200
201/*
202 * A key's DNSKEY record, and the DS record for it the parent zone publishes,
203 * as the registrar asks for them
204 */
205
206struct lws_auth_dns_key_records {
207 char dnskey[((LWS_AUTH_DNS_DNSKEY_WIRE_MAX - 4 + 2) / 3) * 4 + 16];
209 char ds[128];
211 char digest[97];
213 uint16_t keytag;
214 uint8_t alg;
215 uint8_t digest_type;
216};
217
234lws_auth_dns_key_records(struct lws_jwk *jwk, const char *origin, int flags,
235 struct lws_auth_dns_key_records *r);
236
237struct lws_auth_dns_sign_info {
238 const char *input_filepath;
239 const char *output_filepath;
240 const char *jws_filepath; /* Path to output signed JWS of the zone */
241 const char *zsk_jwk_filepath; /* Path to ZSK JWK config */
242 const char *ksk_jwk_filepath; /* Path to KSK JWK config */
243 const char * (*subst_cb)(struct lws_auth_dns_sign_info *info, const char *name);
244 void *subst_priv;
245 const char **subst_names; /* For lws_strexp fallback */
246 const char **subst_values;
247 time_t sign_validity_start_time; /* 0 = now */
248 uint32_t sign_validity_duration; /* 0 = LWS_AUTH_DNS_SIGN_VALIDITY_DEFAULT_SECS */
249 int num_substs;
250 const char *ipv4; /* detected external addresses, */
251 const char *ipv6; /* for subst_cb's ${EXTIP4} / ${EXTIP6} */
252 struct lws_context *cx; /* For logging/alloc */
253 const char *domain; /* NULL, or the only zone the input may be, see lws_auth_dns_zone_check_origin() */
254 time_t verify_time; /* verify only: when the RRSIGs must be valid, 0 = now */
255
256 const char *curr_line;
257 size_t curr_line_len;
258 uint8_t skip_line;
259};
260
279lws_auth_dns_sign_zone(struct lws_auth_dns_sign_info *info);
280
331lws_auth_dns_verify_zone(struct lws_auth_dns_sign_info *info);
332
333#endif
struct lws_dll2 lws_dll2_t
struct lws_dll2_owner lws_dll2_owner_t
unsigned short uint16_t
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_VISIBLE