Coverage Report

Created: 2026-06-07 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/rdata/generic/hip_55.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/* RFC 5205 */
15
16
#pragma once
17
18
41.3k
#define RRTYPE_HIP_ATTRIBUTES (0)
19
20
static isc_result_t
21
1.43k
fromtext_hip(ARGS_FROMTEXT) {
22
1.43k
  isc_token_t token;
23
1.43k
  isc_buffer_t buffer;
24
1.43k
  isc_buffer_t hit_len;
25
1.43k
  isc_buffer_t key_len;
26
1.43k
  unsigned char *start;
27
1.43k
  size_t len;
28
29
1.43k
  REQUIRE(type == dns_rdatatype_hip);
30
31
1.43k
  UNUSED(type);
32
1.43k
  UNUSED(rdclass);
33
1.43k
  UNUSED(callbacks);
34
35
  /*
36
   * Dummy HIT len.
37
   */
38
1.43k
  hit_len = *target;
39
1.43k
  RETERR(uint8_tobuffer(0, target));
40
41
  /*
42
   * Algorithm.
43
   */
44
1.43k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
45
1.43k
              false));
46
1.43k
  if (token.value.as_ulong > 0xffU) {
47
48
    RETTOK(ISC_R_RANGE);
48
48
  }
49
1.38k
  RETERR(uint8_tobuffer(token.value.as_ulong, target));
50
51
  /*
52
   * Dummy KEY len.
53
   */
54
1.38k
  key_len = *target;
55
1.38k
  RETERR(uint16_tobuffer(0, target));
56
57
  /*
58
   * HIT (base16).
59
   */
60
1.38k
  start = isc_buffer_used(target);
61
1.38k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62
1.38k
              false));
63
1.35k
  RETTOK(isc_hex_decodestring(DNS_AS_STR(token), target));
64
65
  /*
66
   * Fill in HIT len.
67
   */
68
1.32k
  len = (unsigned char *)isc_buffer_used(target) - start;
69
1.32k
  if (len > 0xffU) {
70
2
    RETTOK(ISC_R_RANGE);
71
2
  }
72
1.32k
  RETERR(uint8_tobuffer((uint32_t)len, &hit_len));
73
74
  /*
75
   * Public key (base64).
76
   */
77
1.32k
  start = isc_buffer_used(target);
78
1.32k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
79
1.32k
              false));
80
1.30k
  RETTOK(isc_base64_decodestring(DNS_AS_STR(token), target));
81
82
  /*
83
   * Fill in KEY len.
84
   */
85
1.27k
  len = (unsigned char *)isc_buffer_used(target) - start;
86
1.27k
  if (len > 0xffffU) {
87
1
    RETTOK(ISC_R_RANGE);
88
1
  }
89
1.27k
  RETERR(uint16_tobuffer((uint32_t)len, &key_len));
90
91
1.27k
  if (origin == NULL) {
92
73
    origin = dns_rootname;
93
73
  }
94
95
  /*
96
   * Rendezvous Servers.
97
   */
98
180k
  do {
99
180k
    RETERR(isc_lex_getmastertoken(lexer, &token,
100
180k
                isc_tokentype_string, true));
101
180k
    if (token.type != isc_tokentype_string) {
102
1.22k
      break;
103
1.22k
    }
104
178k
    buffer_fromregion(&buffer, &token.value.as_region);
105
178k
    RETTOK(dns_name_wirefromtext(&buffer, origin, options, target));
106
178k
  } while (1);
107
108
  /*
109
   * Let upper layer handle eol/eof.
110
   */
111
1.22k
  isc_lex_ungettoken(lexer, &token);
112
113
1.22k
  return ISC_R_SUCCESS;
114
1.27k
}
115
116
static isc_result_t
117
6.19k
totext_hip(ARGS_TOTEXT) {
118
6.19k
  isc_region_t region;
119
6.19k
  dns_name_t name;
120
6.19k
  unsigned int length, key_len, hit_len;
121
6.19k
  unsigned char algorithm;
122
6.19k
  char buf[sizeof("225 ")];
123
124
6.19k
  REQUIRE(rdata->type == dns_rdatatype_hip);
125
6.19k
  REQUIRE(rdata->length != 0);
126
127
6.19k
  dns_rdata_toregion(rdata, &region);
128
129
6.19k
  hit_len = uint8_fromregion(&region);
130
6.19k
  isc_region_consume(&region, 1);
131
132
6.19k
  algorithm = uint8_fromregion(&region);
133
6.19k
  isc_region_consume(&region, 1);
134
135
6.19k
  key_len = uint16_fromregion(&region);
136
6.19k
  isc_region_consume(&region, 2);
137
138
6.19k
  if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
139
91
    RETERR(str_totext("( ", target));
140
91
  }
141
142
  /*
143
   * Algorithm
144
   */
145
6.19k
  snprintf(buf, sizeof(buf), "%u ", algorithm);
146
6.19k
  RETERR(str_totext(buf, target));
147
148
  /*
149
   * HIT.
150
   */
151
6.19k
  INSIST(hit_len < region.length);
152
6.19k
  length = region.length;
153
6.19k
  region.length = hit_len;
154
6.19k
  RETERR(isc_hex_totext(&region, 1, "", target));
155
6.19k
  region.length = length - hit_len;
156
6.19k
  RETERR(str_totext(tctx->linebreak, target));
157
158
  /*
159
   * Public KEY.
160
   */
161
6.19k
  INSIST(key_len <= region.length);
162
6.19k
  length = region.length;
163
6.19k
  region.length = key_len;
164
6.19k
  RETERR(isc_base64_totext(&region, 1, "", target));
165
6.19k
  region.length = length - key_len;
166
6.19k
  if (region.length > 0) {
167
5.77k
    RETERR(str_totext(tctx->linebreak, target));
168
5.77k
  }
169
170
  /*
171
   * Rendezvous Servers.
172
   */
173
6.19k
  dns_name_init(&name);
174
405k
  while (region.length > 0) {
175
399k
    dns_name_fromregion(&name, &region);
176
177
399k
    RETERR(dns_name_totext(&name, 0, target));
178
399k
    isc_region_consume(&region, name.length);
179
399k
    if (region.length > 0) {
180
393k
      RETERR(str_totext(tctx->linebreak, target));
181
393k
    }
182
399k
  }
183
6.18k
  if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
184
91
    RETERR(str_totext(" )", target));
185
91
  }
186
6.18k
  return ISC_R_SUCCESS;
187
6.18k
}
188
189
static isc_result_t
190
48.7k
fromwire_hip(ARGS_FROMWIRE) {
191
48.7k
  isc_region_t region, rr;
192
48.7k
  dns_name_t name;
193
48.7k
  uint8_t hit_len;
194
48.7k
  uint16_t key_len;
195
48.7k
  size_t len;
196
197
48.7k
  REQUIRE(type == dns_rdatatype_hip);
198
199
48.7k
  UNUSED(type);
200
48.7k
  UNUSED(rdclass);
201
202
48.7k
  isc_buffer_activeregion(source, &region);
203
48.7k
  if (region.length < 4U) {
204
14
    RETERR(DNS_R_FORMERR);
205
0
  }
206
207
48.7k
  rr = region;
208
48.7k
  hit_len = uint8_fromregion(&region);
209
48.7k
  if (hit_len == 0) {
210
6
    RETERR(DNS_R_FORMERR);
211
0
  }
212
48.7k
  isc_region_consume(&region, 2); /* hit length + algorithm */
213
48.7k
  key_len = uint16_fromregion(&region);
214
48.7k
  if (key_len == 0) {
215
4
    RETERR(DNS_R_FORMERR);
216
0
  }
217
48.7k
  isc_region_consume(&region, 2);
218
48.7k
  len = hit_len + key_len;
219
48.7k
  if (len > region.length) {
220
94
    RETERR(DNS_R_FORMERR);
221
0
  }
222
223
48.6k
  RETERR(mem_tobuffer(target, rr.base, 4 + len));
224
48.3k
  isc_buffer_forward(source, 4 + len);
225
226
48.3k
  dctx = dns_decompress_setpermitted(dctx, false);
227
1.31M
  while (isc_buffer_activelength(source) > 0) {
228
1.29M
    dns_name_init(&name);
229
1.29M
    RETERR(dns_name_fromwire(&name, source, dctx, target));
230
1.26M
  }
231
21.0k
  return ISC_R_SUCCESS;
232
48.3k
}
233
234
static isc_result_t
235
2.72k
towire_hip(ARGS_TOWIRE) {
236
2.72k
  isc_region_t region;
237
238
2.72k
  REQUIRE(rdata->type == dns_rdatatype_hip);
239
2.72k
  REQUIRE(rdata->length != 0);
240
241
2.72k
  UNUSED(cctx);
242
243
2.72k
  dns_rdata_toregion(rdata, &region);
244
2.72k
  return mem_tobuffer(target, region.base, region.length);
245
2.72k
}
246
247
static int
248
3.02k
compare_hip(ARGS_COMPARE) {
249
3.02k
  isc_region_t region1;
250
3.02k
  isc_region_t region2;
251
252
3.02k
  REQUIRE(rdata1->type == rdata2->type);
253
3.02k
  REQUIRE(rdata1->rdclass == rdata2->rdclass);
254
3.02k
  REQUIRE(rdata1->type == dns_rdatatype_hip);
255
3.02k
  REQUIRE(rdata1->length != 0);
256
3.02k
  REQUIRE(rdata2->length != 0);
257
258
3.02k
  dns_rdata_toregion(rdata1, &region1);
259
3.02k
  dns_rdata_toregion(rdata2, &region2);
260
3.02k
  return isc_region_compare(&region1, &region2);
261
3.02k
}
262
263
static isc_result_t
264
0
fromstruct_hip(ARGS_FROMSTRUCT) {
265
0
  dns_rdata_hip_t *hip = source;
266
0
  dns_rdata_hip_t myhip;
267
0
  isc_result_t result;
268
269
0
  REQUIRE(type == dns_rdatatype_hip);
270
0
  REQUIRE(hip != NULL);
271
0
  REQUIRE(hip->common.rdtype == type);
272
0
  REQUIRE(hip->common.rdclass == rdclass);
273
0
  REQUIRE(hip->hit_len > 0 && hip->hit != NULL);
274
0
  REQUIRE(hip->key_len > 0 && hip->key != NULL);
275
0
  REQUIRE((hip->servers == NULL && hip->servers_len == 0) ||
276
0
    (hip->servers != NULL && hip->servers_len != 0));
277
278
0
  UNUSED(type);
279
0
  UNUSED(rdclass);
280
281
0
  RETERR(uint8_tobuffer(hip->hit_len, target));
282
0
  RETERR(uint8_tobuffer(hip->algorithm, target));
283
0
  RETERR(uint16_tobuffer(hip->key_len, target));
284
0
  RETERR(mem_tobuffer(target, hip->hit, hip->hit_len));
285
0
  RETERR(mem_tobuffer(target, hip->key, hip->key_len));
286
287
0
  myhip = *hip;
288
0
  for (result = dns_rdata_hip_first(&myhip); result == ISC_R_SUCCESS;
289
0
       result = dns_rdata_hip_next(&myhip))
290
0
  {
291
    /* initialize the names */
292
0
  }
293
294
0
  return mem_tobuffer(target, hip->servers, hip->servers_len);
295
0
}
296
297
static isc_result_t
298
0
tostruct_hip(ARGS_TOSTRUCT) {
299
0
  isc_region_t region;
300
0
  dns_rdata_hip_t *hip = target;
301
302
0
  REQUIRE(rdata->type == dns_rdatatype_hip);
303
0
  REQUIRE(hip != NULL);
304
0
  REQUIRE(rdata->length != 0);
305
306
0
  DNS_RDATACOMMON_INIT(hip, rdata->type, rdata->rdclass);
307
308
0
  dns_rdata_toregion(rdata, &region);
309
310
0
  hip->hit_len = uint8_fromregion(&region);
311
0
  isc_region_consume(&region, 1);
312
313
0
  hip->algorithm = uint8_fromregion(&region);
314
0
  isc_region_consume(&region, 1);
315
316
0
  hip->key_len = uint16_fromregion(&region);
317
0
  isc_region_consume(&region, 2);
318
319
0
  hip->hit = hip->key = hip->servers = NULL;
320
321
0
  hip->hit = mem_maybedup(mctx, region.base, hip->hit_len);
322
0
  isc_region_consume(&region, hip->hit_len);
323
324
0
  INSIST(hip->key_len <= region.length);
325
326
0
  hip->key = mem_maybedup(mctx, region.base, hip->key_len);
327
0
  isc_region_consume(&region, hip->key_len);
328
329
0
  hip->servers_len = region.length;
330
0
  if (hip->servers_len != 0) {
331
0
    hip->servers = mem_maybedup(mctx, region.base, region.length);
332
0
  }
333
334
0
  hip->offset = hip->servers_len;
335
0
  hip->mctx = mctx;
336
0
  return ISC_R_SUCCESS;
337
0
}
338
339
static void
340
0
freestruct_hip(ARGS_FREESTRUCT) {
341
0
  dns_rdata_hip_t *hip = source;
342
343
0
  REQUIRE(hip != NULL);
344
345
0
  if (hip->mctx == NULL) {
346
0
    return;
347
0
  }
348
349
0
  isc_mem_free(hip->mctx, hip->hit);
350
0
  isc_mem_free(hip->mctx, hip->key);
351
0
  if (hip->servers != NULL) {
352
0
    isc_mem_free(hip->mctx, hip->servers);
353
0
  }
354
0
  hip->mctx = NULL;
355
0
}
356
357
static isc_result_t
358
0
additionaldata_hip(ARGS_ADDLDATA) {
359
0
  REQUIRE(rdata->type == dns_rdatatype_hip);
360
361
0
  UNUSED(rdata);
362
0
  UNUSED(owner);
363
0
  UNUSED(add);
364
0
  UNUSED(arg);
365
366
0
  return ISC_R_SUCCESS;
367
0
}
368
369
static isc_result_t
370
0
digest_hip(ARGS_DIGEST) {
371
0
  isc_region_t r;
372
373
0
  REQUIRE(rdata->type == dns_rdatatype_hip);
374
375
0
  dns_rdata_toregion(rdata, &r);
376
0
  return (digest)(arg, &r);
377
0
}
378
379
static bool
380
0
checkowner_hip(ARGS_CHECKOWNER) {
381
0
  REQUIRE(type == dns_rdatatype_hip);
382
383
0
  UNUSED(name);
384
0
  UNUSED(type);
385
0
  UNUSED(rdclass);
386
0
  UNUSED(wildcard);
387
388
0
  return true;
389
0
}
390
391
static bool
392
0
checknames_hip(ARGS_CHECKNAMES) {
393
0
  REQUIRE(rdata->type == dns_rdatatype_hip);
394
395
0
  UNUSED(rdata);
396
0
  UNUSED(owner);
397
0
  UNUSED(bad);
398
399
0
  return true;
400
0
}
401
402
isc_result_t
403
0
dns_rdata_hip_first(dns_rdata_hip_t *hip) {
404
0
  if (hip->servers_len == 0) {
405
0
    return ISC_R_NOMORE;
406
0
  }
407
0
  hip->offset = 0;
408
0
  return ISC_R_SUCCESS;
409
0
}
410
411
isc_result_t
412
0
dns_rdata_hip_next(dns_rdata_hip_t *hip) {
413
0
  isc_region_t region;
414
0
  dns_name_t name;
415
416
0
  if (hip->offset >= hip->servers_len) {
417
0
    return ISC_R_NOMORE;
418
0
  }
419
420
0
  region.base = hip->servers + hip->offset;
421
0
  region.length = hip->servers_len - hip->offset;
422
0
  dns_name_init(&name);
423
0
  dns_name_fromregion(&name, &region);
424
0
  hip->offset += name.length;
425
0
  INSIST(hip->offset <= hip->servers_len);
426
0
  return hip->offset < hip->servers_len ? ISC_R_SUCCESS : ISC_R_NOMORE;
427
0
}
428
429
void
430
0
dns_rdata_hip_current(dns_rdata_hip_t *hip, dns_name_t *name) {
431
0
  isc_region_t region;
432
433
0
  REQUIRE(hip->offset < hip->servers_len);
434
435
0
  region.base = hip->servers + hip->offset;
436
0
  region.length = hip->servers_len - hip->offset;
437
0
  dns_name_fromregion(name, &region);
438
439
0
  INSIST(name->length + hip->offset <= hip->servers_len);
440
0
}
441
442
static int
443
0
casecompare_hip(ARGS_COMPARE) {
444
0
  isc_region_t r1;
445
0
  isc_region_t r2;
446
0
  dns_name_t name1;
447
0
  dns_name_t name2;
448
0
  int order;
449
0
  uint8_t hit_len;
450
0
  uint16_t key_len;
451
452
0
  REQUIRE(rdata1->type == rdata2->type);
453
0
  REQUIRE(rdata1->rdclass == rdata2->rdclass);
454
0
  REQUIRE(rdata1->type == dns_rdatatype_hip);
455
0
  REQUIRE(rdata1->length != 0);
456
0
  REQUIRE(rdata2->length != 0);
457
458
0
  dns_rdata_toregion(rdata1, &r1);
459
0
  dns_rdata_toregion(rdata2, &r2);
460
461
0
  INSIST(r1.length > 4);
462
0
  INSIST(r2.length > 4);
463
0
  order = memcmp(r1.base, r2.base, 4);
464
0
  if (order != 0) {
465
0
    return order;
466
0
  }
467
468
0
  hit_len = uint8_fromregion(&r1);
469
0
  isc_region_consume(&r1, 2); /* hit length + algorithm */
470
0
  key_len = uint16_fromregion(&r1);
471
0
  isc_region_consume(&r1, 2); /* key length */
472
0
  isc_region_consume(&r2, 4);
473
474
0
  INSIST(r1.length >= (unsigned int)(hit_len + key_len));
475
0
  INSIST(r2.length >= (unsigned int)(hit_len + key_len));
476
0
  order = memcmp(r1.base, r2.base, hit_len + key_len);
477
0
  if (order != 0) {
478
0
    return order;
479
0
  }
480
0
  isc_region_consume(&r1, hit_len + key_len);
481
0
  isc_region_consume(&r2, hit_len + key_len);
482
483
0
  dns_name_init(&name1);
484
0
  dns_name_init(&name2);
485
0
  while (r1.length != 0 && r2.length != 0) {
486
0
    dns_name_fromregion(&name1, &r1);
487
0
    dns_name_fromregion(&name2, &r2);
488
0
    order = dns_name_rdatacompare(&name1, &name2);
489
0
    if (order != 0) {
490
0
      return order;
491
0
    }
492
493
0
    isc_region_consume(&r1, name_length(&name1));
494
0
    isc_region_consume(&r2, name_length(&name2));
495
0
  }
496
0
  return isc_region_compare(&r1, &r2);
497
0
}