Coverage Report

Created: 2023-06-07 06:23

/src/bind9/lib/dns/rdata/generic/sig_24.c
Line
Count
Source (jump to first uncovered line)
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
/* RFC2535 */
15
16
#ifndef RDATA_GENERIC_SIG_24_C
17
#define RDATA_GENERIC_SIG_24_C
18
19
22.8k
#define RRTYPE_SIG_ATTRIBUTES (0)
20
21
static isc_result_t
22
2.37k
fromtext_sig(ARGS_FROMTEXT) {
23
2.37k
  isc_token_t token;
24
2.37k
  unsigned char alg, c;
25
2.37k
  long i;
26
2.37k
  dns_rdatatype_t covered;
27
2.37k
  char *e;
28
2.37k
  isc_result_t result;
29
2.37k
  dns_name_t name;
30
2.37k
  isc_buffer_t buffer;
31
2.37k
  uint32_t time_signed, time_expire;
32
2.37k
  unsigned int used;
33
34
2.37k
  REQUIRE(type == dns_rdatatype_sig);
35
36
2.37k
  UNUSED(type);
37
2.37k
  UNUSED(rdclass);
38
2.37k
  UNUSED(callbacks);
39
40
  /*
41
   * Type covered.
42
   */
43
2.37k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
44
2.37k
              false));
45
2.36k
  result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
46
2.36k
  if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
47
1.99k
    i = strtol(DNS_AS_STR(token), &e, 10);
48
1.99k
    if (i < 0 || i > 65535) {
49
252
      RETTOK(ISC_R_RANGE);
50
252
    }
51
1.74k
    if (*e != 0) {
52
43
      RETTOK(result);
53
43
    }
54
1.70k
    covered = (dns_rdatatype_t)i;
55
1.70k
  }
56
2.07k
  RETERR(uint16_tobuffer(covered, target));
57
58
  /*
59
   * Algorithm.
60
   */
61
2.07k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62
2.07k
              false));
63
2.01k
  RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
64
1.94k
  RETERR(mem_tobuffer(target, &alg, 1));
65
66
  /*
67
   * Labels.
68
   */
69
1.94k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
70
1.94k
              false));
71
1.93k
  if (token.value.as_ulong > 0xffU) {
72
34
    RETTOK(ISC_R_RANGE);
73
34
  }
74
1.90k
  c = (unsigned char)token.value.as_ulong;
75
1.90k
  RETERR(mem_tobuffer(target, &c, 1));
76
77
  /*
78
   * Original ttl.
79
   */
80
1.90k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
81
1.90k
              false));
82
1.88k
  RETERR(uint32_tobuffer(token.value.as_ulong, target));
83
84
  /*
85
   * Signature expiration.
86
   */
87
1.88k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
88
1.88k
              false));
89
1.88k
  RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
90
1.88k
  RETERR(uint32_tobuffer(time_expire, target));
91
92
  /*
93
   * Time signed.
94
   */
95
1.88k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
96
1.88k
              false));
97
1.87k
  RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
98
1.86k
  RETERR(uint32_tobuffer(time_signed, target));
99
100
  /*
101
   * Key footprint.
102
   */
103
1.86k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
104
1.86k
              false));
105
1.86k
  RETERR(uint16_tobuffer(token.value.as_ulong, target));
106
107
  /*
108
   * Signer.
109
   */
110
1.86k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
111
1.86k
              false));
112
1.85k
  dns_name_init(&name, NULL);
113
1.85k
  buffer_fromregion(&buffer, &token.value.as_region);
114
1.85k
  if (origin == NULL) {
115
5
    origin = dns_rootname;
116
5
  }
117
1.85k
  RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
118
119
  /*
120
   * Sig.
121
   */
122
1.85k
  used = isc_buffer_usedlength(target);
123
124
1.85k
  RETERR(isc_base64_tobuffer(lexer, target, -2));
125
126
1.84k
  if (alg == DNS_KEYALG_PRIVATEDNS || alg == DNS_KEYALG_PRIVATEOID) {
127
276
    isc_buffer_t b;
128
129
    /*
130
     * Set up 'b' so that the signature data can be parsed.
131
     */
132
276
    b = *target;
133
276
    b.active = b.used;
134
276
    b.current = used;
135
136
276
    RETERR(check_private(&b, alg));
137
276
  }
138
139
1.83k
  return (ISC_R_SUCCESS);
140
1.84k
}
141
142
static isc_result_t
143
7.20k
totext_sig(ARGS_TOTEXT) {
144
7.20k
  isc_region_t sr;
145
7.20k
  char buf[sizeof("4294967295")];
146
7.20k
  dns_rdatatype_t covered;
147
7.20k
  unsigned long ttl;
148
7.20k
  unsigned long when;
149
7.20k
  unsigned long exp;
150
7.20k
  unsigned long foot;
151
7.20k
  dns_name_t name;
152
7.20k
  dns_name_t prefix;
153
7.20k
  bool sub;
154
155
7.20k
  REQUIRE(rdata->type == dns_rdatatype_sig);
156
7.20k
  REQUIRE(rdata->length != 0);
157
158
7.20k
  dns_rdata_toregion(rdata, &sr);
159
160
  /*
161
   * Type covered.
162
   */
163
7.20k
  covered = uint16_fromregion(&sr);
164
7.20k
  isc_region_consume(&sr, 2);
165
  /*
166
   * XXXAG We should have something like dns_rdatatype_isknown()
167
   * that does the right thing with type 0.
168
   */
169
7.20k
  if (dns_rdatatype_isknown(covered) && covered != 0) {
170
3.55k
    RETERR(dns_rdatatype_totext(covered, target));
171
3.65k
  } else {
172
3.65k
    snprintf(buf, sizeof(buf), "%u", covered);
173
3.65k
    RETERR(str_totext(buf, target));
174
3.65k
  }
175
7.20k
  RETERR(str_totext(" ", target));
176
177
  /*
178
   * Algorithm.
179
   */
180
7.20k
  snprintf(buf, sizeof(buf), "%u", sr.base[0]);
181
7.20k
  isc_region_consume(&sr, 1);
182
7.20k
  RETERR(str_totext(buf, target));
183
7.20k
  RETERR(str_totext(" ", target));
184
185
  /*
186
   * Labels.
187
   */
188
7.20k
  snprintf(buf, sizeof(buf), "%u", sr.base[0]);
189
7.20k
  isc_region_consume(&sr, 1);
190
7.20k
  RETERR(str_totext(buf, target));
191
7.20k
  RETERR(str_totext(" ", target));
192
193
  /*
194
   * Ttl.
195
   */
196
7.20k
  ttl = uint32_fromregion(&sr);
197
7.20k
  isc_region_consume(&sr, 4);
198
7.20k
  snprintf(buf, sizeof(buf), "%lu", ttl);
199
7.20k
  RETERR(str_totext(buf, target));
200
7.20k
  RETERR(str_totext(" ", target));
201
202
  /*
203
   * Sig exp.
204
   */
205
7.20k
  exp = uint32_fromregion(&sr);
206
7.20k
  isc_region_consume(&sr, 4);
207
7.20k
  RETERR(dns_time32_totext(exp, target));
208
209
7.20k
  if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
210
169
    RETERR(str_totext(" (", target));
211
169
  }
212
7.20k
  RETERR(str_totext(tctx->linebreak, target));
213
214
  /*
215
   * Time signed.
216
   */
217
7.20k
  when = uint32_fromregion(&sr);
218
7.20k
  isc_region_consume(&sr, 4);
219
7.20k
  RETERR(dns_time32_totext(when, target));
220
7.20k
  RETERR(str_totext(" ", target));
221
222
  /*
223
   * Footprint.
224
   */
225
7.20k
  foot = uint16_fromregion(&sr);
226
7.20k
  isc_region_consume(&sr, 2);
227
7.20k
  snprintf(buf, sizeof(buf), "%lu", foot);
228
7.20k
  RETERR(str_totext(buf, target));
229
7.20k
  RETERR(str_totext(" ", target));
230
231
  /*
232
   * Signer.
233
   */
234
7.20k
  dns_name_init(&name, NULL);
235
7.20k
  dns_name_init(&prefix, NULL);
236
7.20k
  dns_name_fromregion(&name, &sr);
237
7.20k
  isc_region_consume(&sr, name_length(&name));
238
7.20k
  sub = name_prefix(&name, tctx->origin, &prefix);
239
7.20k
  RETERR(dns_name_totext(&prefix, sub, target));
240
241
  /*
242
   * Sig.
243
   */
244
7.20k
  RETERR(str_totext(tctx->linebreak, target));
245
7.20k
  if (tctx->width == 0) { /* No splitting */
246
0
    RETERR(isc_base64_totext(&sr, 60, "", target));
247
7.20k
  } else {
248
7.20k
    RETERR(isc_base64_totext(&sr, tctx->width - 2, tctx->linebreak,
249
7.20k
           target));
250
7.20k
  }
251
7.20k
  if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
252
169
    RETERR(str_totext(" )", target));
253
169
  }
254
255
7.20k
  return (ISC_R_SUCCESS);
256
7.20k
}
257
258
static isc_result_t
259
8.92k
fromwire_sig(ARGS_FROMWIRE) {
260
8.92k
  isc_region_t sr;
261
8.92k
  dns_name_t name;
262
8.92k
  unsigned char algorithm;
263
264
8.92k
  REQUIRE(type == dns_rdatatype_sig);
265
266
8.92k
  UNUSED(type);
267
8.92k
  UNUSED(rdclass);
268
269
8.92k
  dctx = dns_decompress_setpermitted(dctx, false);
270
271
8.92k
  isc_buffer_activeregion(source, &sr);
272
  /*
273
   * type covered: 2
274
   * algorithm: 1
275
   * labels: 1
276
   * original ttl: 4
277
   * signature expiration: 4
278
   * time signed: 4
279
   * key footprint: 2
280
   */
281
8.92k
  if (sr.length < 18) {
282
43
    return (ISC_R_UNEXPECTEDEND);
283
43
  }
284
285
8.87k
  algorithm = sr.base[2];
286
287
8.87k
  isc_buffer_forward(source, 18);
288
8.87k
  RETERR(mem_tobuffer(target, sr.base, 18));
289
290
  /*
291
   * Signer.
292
   */
293
8.64k
  dns_name_init(&name, NULL);
294
8.64k
  RETERR(dns_name_fromwire(&name, source, dctx, target));
295
296
  /*
297
   * Sig.
298
   */
299
8.41k
  isc_buffer_activeregion(source, &sr);
300
8.41k
  if (sr.length == 0) {
301
8
    return (ISC_R_UNEXPECTEDEND);
302
8
  }
303
304
8.41k
  if (algorithm == DNS_KEYALG_PRIVATEDNS ||
305
8.41k
      algorithm == DNS_KEYALG_PRIVATEOID)
306
1.38k
  {
307
1.38k
    isc_buffer_t b = *source;
308
1.38k
    RETERR(check_private(&b, algorithm));
309
1.38k
  }
310
311
8.31k
  isc_buffer_forward(source, sr.length);
312
8.31k
  return (mem_tobuffer(target, sr.base, sr.length));
313
8.41k
}
314
315
static isc_result_t
316
4.06k
towire_sig(ARGS_TOWIRE) {
317
4.06k
  isc_region_t sr;
318
4.06k
  dns_name_t name;
319
4.06k
  dns_offsets_t offsets;
320
321
4.06k
  REQUIRE(rdata->type == dns_rdatatype_sig);
322
4.06k
  REQUIRE(rdata->length != 0);
323
324
4.06k
  dns_compress_setpermitted(cctx, false);
325
4.06k
  dns_rdata_toregion(rdata, &sr);
326
  /*
327
   * type covered: 2
328
   * algorithm: 1
329
   * labels: 1
330
   * original ttl: 4
331
   * signature expiration: 4
332
   * time signed: 4
333
   * key footprint: 2
334
   */
335
4.06k
  RETERR(mem_tobuffer(target, sr.base, 18));
336
4.06k
  isc_region_consume(&sr, 18);
337
338
  /*
339
   * Signer.
340
   */
341
4.06k
  dns_name_init(&name, offsets);
342
4.06k
  dns_name_fromregion(&name, &sr);
343
4.06k
  isc_region_consume(&sr, name_length(&name));
344
4.06k
  RETERR(dns_name_towire(&name, cctx, target));
345
346
  /*
347
   * Signature.
348
   */
349
4.06k
  return (mem_tobuffer(target, sr.base, sr.length));
350
4.06k
}
351
352
static int
353
6.75k
compare_sig(ARGS_COMPARE) {
354
6.75k
  isc_region_t r1;
355
6.75k
  isc_region_t r2;
356
6.75k
  dns_name_t name1;
357
6.75k
  dns_name_t name2;
358
6.75k
  int order;
359
360
6.75k
  REQUIRE(rdata1->type == rdata2->type);
361
6.75k
  REQUIRE(rdata1->rdclass == rdata2->rdclass);
362
6.75k
  REQUIRE(rdata1->type == dns_rdatatype_sig);
363
6.75k
  REQUIRE(rdata1->length != 0);
364
6.75k
  REQUIRE(rdata2->length != 0);
365
366
6.75k
  dns_rdata_toregion(rdata1, &r1);
367
6.75k
  dns_rdata_toregion(rdata2, &r2);
368
369
6.75k
  INSIST(r1.length > 18);
370
6.75k
  INSIST(r2.length > 18);
371
6.75k
  r1.length = 18;
372
6.75k
  r2.length = 18;
373
6.75k
  order = isc_region_compare(&r1, &r2);
374
6.75k
  if (order != 0) {
375
3.94k
    return (order);
376
3.94k
  }
377
378
2.81k
  dns_name_init(&name1, NULL);
379
2.81k
  dns_name_init(&name2, NULL);
380
2.81k
  dns_rdata_toregion(rdata1, &r1);
381
2.81k
  dns_rdata_toregion(rdata2, &r2);
382
2.81k
  isc_region_consume(&r1, 18);
383
2.81k
  isc_region_consume(&r2, 18);
384
2.81k
  dns_name_fromregion(&name1, &r1);
385
2.81k
  dns_name_fromregion(&name2, &r2);
386
2.81k
  order = dns_name_rdatacompare(&name1, &name2);
387
2.81k
  if (order != 0) {
388
1.52k
    return (order);
389
1.52k
  }
390
391
1.28k
  isc_region_consume(&r1, name_length(&name1));
392
1.28k
  isc_region_consume(&r2, name_length(&name2));
393
394
1.28k
  return (isc_region_compare(&r1, &r2));
395
2.81k
}
396
397
static isc_result_t
398
0
fromstruct_sig(ARGS_FROMSTRUCT) {
399
0
  dns_rdata_sig_t *sig = source;
400
401
0
  REQUIRE(type == dns_rdatatype_sig);
402
0
  REQUIRE(sig != NULL);
403
0
  REQUIRE(sig->common.rdtype == type);
404
0
  REQUIRE(sig->common.rdclass == rdclass);
405
0
  REQUIRE(sig->signature != NULL || sig->siglen == 0);
406
407
0
  UNUSED(type);
408
0
  UNUSED(rdclass);
409
410
  /*
411
   * Type covered.
412
   */
413
0
  RETERR(uint16_tobuffer(sig->covered, target));
414
415
  /*
416
   * Algorithm.
417
   */
418
0
  RETERR(uint8_tobuffer(sig->algorithm, target));
419
420
  /*
421
   * Labels.
422
   */
423
0
  RETERR(uint8_tobuffer(sig->labels, target));
424
425
  /*
426
   * Original TTL.
427
   */
428
0
  RETERR(uint32_tobuffer(sig->originalttl, target));
429
430
  /*
431
   * Expire time.
432
   */
433
0
  RETERR(uint32_tobuffer(sig->timeexpire, target));
434
435
  /*
436
   * Time signed.
437
   */
438
0
  RETERR(uint32_tobuffer(sig->timesigned, target));
439
440
  /*
441
   * Key ID.
442
   */
443
0
  RETERR(uint16_tobuffer(sig->keyid, target));
444
445
  /*
446
   * Signer name.
447
   */
448
0
  RETERR(name_tobuffer(&sig->signer, target));
449
450
  /*
451
   * Signature.
452
   */
453
0
  return (mem_tobuffer(target, sig->signature, sig->siglen));
454
0
}
455
456
static isc_result_t
457
507
tostruct_sig(ARGS_TOSTRUCT) {
458
507
  isc_region_t sr;
459
507
  dns_rdata_sig_t *sig = target;
460
507
  dns_name_t signer;
461
462
507
  REQUIRE(rdata->type == dns_rdatatype_sig);
463
507
  REQUIRE(sig != NULL);
464
507
  REQUIRE(rdata->length != 0);
465
466
507
  sig->common.rdclass = rdata->rdclass;
467
507
  sig->common.rdtype = rdata->type;
468
507
  ISC_LINK_INIT(&sig->common, link);
469
470
507
  dns_rdata_toregion(rdata, &sr);
471
472
  /*
473
   * Type covered.
474
   */
475
507
  sig->covered = uint16_fromregion(&sr);
476
507
  isc_region_consume(&sr, 2);
477
478
  /*
479
   * Algorithm.
480
   */
481
507
  sig->algorithm = uint8_fromregion(&sr);
482
507
  isc_region_consume(&sr, 1);
483
484
  /*
485
   * Labels.
486
   */
487
507
  sig->labels = uint8_fromregion(&sr);
488
507
  isc_region_consume(&sr, 1);
489
490
  /*
491
   * Original TTL.
492
   */
493
507
  sig->originalttl = uint32_fromregion(&sr);
494
507
  isc_region_consume(&sr, 4);
495
496
  /*
497
   * Expire time.
498
   */
499
507
  sig->timeexpire = uint32_fromregion(&sr);
500
507
  isc_region_consume(&sr, 4);
501
502
  /*
503
   * Time signed.
504
   */
505
507
  sig->timesigned = uint32_fromregion(&sr);
506
507
  isc_region_consume(&sr, 4);
507
508
  /*
509
   * Key ID.
510
   */
511
507
  sig->keyid = uint16_fromregion(&sr);
512
507
  isc_region_consume(&sr, 2);
513
514
507
  dns_name_init(&signer, NULL);
515
507
  dns_name_fromregion(&signer, &sr);
516
507
  dns_name_init(&sig->signer, NULL);
517
507
  name_duporclone(&signer, mctx, &sig->signer);
518
507
  isc_region_consume(&sr, name_length(&sig->signer));
519
520
  /*
521
   * Signature.
522
   */
523
507
  sig->siglen = sr.length;
524
507
  sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
525
507
  sig->mctx = mctx;
526
507
  return (ISC_R_SUCCESS);
527
507
}
528
529
static void
530
507
freestruct_sig(ARGS_FREESTRUCT) {
531
507
  dns_rdata_sig_t *sig = (dns_rdata_sig_t *)source;
532
533
507
  REQUIRE(sig != NULL);
534
507
  REQUIRE(sig->common.rdtype == dns_rdatatype_sig);
535
536
507
  if (sig->mctx == NULL) {
537
507
    return;
538
507
  }
539
540
0
  dns_name_free(&sig->signer, sig->mctx);
541
0
  if (sig->signature != NULL) {
542
0
    isc_mem_free(sig->mctx, sig->signature);
543
0
  }
544
0
  sig->mctx = NULL;
545
0
}
546
547
static isc_result_t
548
0
additionaldata_sig(ARGS_ADDLDATA) {
549
0
  REQUIRE(rdata->type == dns_rdatatype_sig);
550
551
0
  UNUSED(rdata);
552
0
  UNUSED(owner);
553
0
  UNUSED(add);
554
0
  UNUSED(arg);
555
556
0
  return (ISC_R_SUCCESS);
557
0
}
558
559
static isc_result_t
560
0
digest_sig(ARGS_DIGEST) {
561
0
  REQUIRE(rdata->type == dns_rdatatype_sig);
562
563
0
  UNUSED(rdata);
564
0
  UNUSED(digest);
565
0
  UNUSED(arg);
566
567
0
  return (ISC_R_NOTIMPLEMENTED);
568
0
}
569
570
static dns_rdatatype_t
571
9.32k
covers_sig(dns_rdata_t *rdata) {
572
9.32k
  dns_rdatatype_t type;
573
9.32k
  isc_region_t r;
574
575
9.32k
  REQUIRE(rdata->type == dns_rdatatype_sig);
576
577
9.32k
  dns_rdata_toregion(rdata, &r);
578
9.32k
  type = uint16_fromregion(&r);
579
580
9.32k
  return (type);
581
9.32k
}
582
583
static bool
584
0
checkowner_sig(ARGS_CHECKOWNER) {
585
0
  REQUIRE(type == dns_rdatatype_sig);
586
587
0
  UNUSED(name);
588
0
  UNUSED(type);
589
0
  UNUSED(rdclass);
590
0
  UNUSED(wildcard);
591
592
0
  return (true);
593
0
}
594
595
static bool
596
0
checknames_sig(ARGS_CHECKNAMES) {
597
0
  REQUIRE(rdata->type == dns_rdatatype_sig);
598
599
0
  UNUSED(rdata);
600
0
  UNUSED(owner);
601
0
  UNUSED(bad);
602
603
0
  return (true);
604
0
}
605
606
static int
607
0
casecompare_sig(ARGS_COMPARE) {
608
0
  return (compare_sig(rdata1, rdata2));
609
0
}
610
#endif /* RDATA_GENERIC_SIG_24_C */