forked from NLnetLabs/nsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdata.c
More file actions
4241 lines (3850 loc) · 111 KB
/
rdata.c
File metadata and controls
4241 lines (3850 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rdata.c -- RDATA conversion functions.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "rdata.h"
#include "zonec.h"
#include "query.h"
#include "dname.h"
/* Taken from RFC 4398, section 2.1. */
lookup_table_type dns_certificate_types[] = {
/* 0 Reserved */
{ 1, "PKIX" }, /* X.509 as per PKIX */
{ 2, "SPKI" }, /* SPKI cert */
{ 3, "PGP" }, /* OpenPGP packet */
{ 4, "IPKIX" }, /* The URL of an X.509 data object */
{ 5, "ISPKI" }, /* The URL of an SPKI certificate */
{ 6, "IPGP" }, /* The fingerprint and URL of an OpenPGP packet */
{ 7, "ACPKIX" }, /* Attribute Certificate */
{ 8, "IACPKIX" }, /* The URL of an Attribute Certificate */
{ 253, "URI" }, /* URI private */
{ 254, "OID" }, /* OID private */
/* 255 Reserved */
/* 256-65279 Available for IANA assignment */
/* 65280-65534 Experimental */
/* 65535 Reserved */
{ 0, NULL }
};
/* Taken from RFC 2535, section 7. */
lookup_table_type dns_algorithms[] = {
{ 1, "RSAMD5" }, /* RFC 2537 */
{ 2, "DH" }, /* RFC 2539 */
{ 3, "DSA" }, /* RFC 2536 */
{ 4, "ECC" },
{ 5, "RSASHA1" }, /* RFC 3110 */
{ 6, "DSA-NSEC3-SHA1" }, /* RFC 5155 */
{ 7, "RSASHA1-NSEC3-SHA1" }, /* RFC 5155 */
{ 8, "RSASHA256" }, /* RFC 5702 */
{ 10, "RSASHA512" }, /* RFC 5702 */
{ 12, "ECC-GOST" }, /* RFC 5933 */
{ 13, "ECDSAP256SHA256" }, /* RFC 6605 */
{ 14, "ECDSAP384SHA384" }, /* RFC 6605 */
{ 15, "ED25519" }, /* RFC 8080 */
{ 16, "ED448" }, /* RFC 8080 */
{ 252, "INDIRECT" },
{ 253, "PRIVATEDNS" },
{ 254, "PRIVATEOID" },
{ 0, NULL }
};
/* Print svcparam key without a value */
static int print_svcparam_no_value(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen);
/* Print svcparam mandatory */
static int print_svcparam_mandatory(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam alpn */
static int print_svcparam_alpn(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam port */
static int print_svcparam_port(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam ipv4hint */
static int print_svcparam_ipv4hint(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam ech */
static int print_svcparam_ech(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam ipv6hint */
static int print_svcparam_ipv6hint(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam dohpath */
static int print_svcparam_dohpath(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
/* Print svcparam tls-supported-groups */
static int print_svcparam_tls_supported_groups(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen);
static const nsd_svcparam_descriptor_type svcparams[] = {
{ SVCB_KEY_MANDATORY, "mandatory", print_svcparam_mandatory },
{ SVCB_KEY_ALPN, "alpn", print_svcparam_alpn },
{ SVCB_KEY_NO_DEFAULT_ALPN, "no-default-alpn",
print_svcparam_no_value },
{ SVCB_KEY_PORT, "port", print_svcparam_port },
{ SVCB_KEY_IPV4HINT, "ipv4hint", print_svcparam_ipv4hint },
{ SVCB_KEY_ECH, "ech", print_svcparam_ech },
{ SVCB_KEY_IPV6HINT, "ipv6hint", print_svcparam_ipv6hint },
{ SVCB_KEY_DOHPATH, "dohpath", print_svcparam_dohpath },
{ SVCB_KEY_OHTTP, "ohttp", print_svcparam_no_value },
{ SVCB_KEY_TLS_SUPPORTED_GROUPS, "tls-supported-groups",
print_svcparam_tls_supported_groups },
};
/*
* Print domain name, as example.com. with escapes.
* The domain name is wireformat in the rdata. That is a literal dname
* in the rdata.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_name_literal(struct buffer *output, uint16_t rdlength,
const uint8_t *rdata, uint16_t *offset)
{
const uint8_t *name, *label, *limit;
assert(rdlength >= *offset);
if (rdlength - *offset == 0)
return 0;
name = rdata + *offset;
label = name;
limit = rdata + rdlength;
if(*label) {
do {
/* space for labellen, label and a next root label. */
if (label - name > MAXDOMAINLEN-1
|| *label > MAXLABELLEN
|| limit - label < 2 + *label)
return 0;
label += 1 + *label;
} while (*label);
} else {
/* root domain. */
/* The label is within the rdlength by the checks at start of
* the function. */
}
buffer_printf(output, "%s", wiredname2str(name));
*offset += (label - name) + 1 /* root label */;
return 1;
}
/*
* Print domain name, as example.com. with escapes.
* The domain must be a reference in the rdata. That is a stored pointer
* to struct domain.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_domain(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
const struct dname *dname;
struct domain *domain;
if(rdlength - *offset < (uint16_t)sizeof(void*))
return 0;
memcpy(&domain, rdata+*offset, sizeof(void*));
dname = domain_dname(domain);
buffer_printf(output, "%s", dname_to_string(dname, NULL));
*offset += sizeof(void*);
return 1;
}
/* Return length of string or -1 on wireformat error. offset is moved +len. */
static inline int32_t
skip_string(struct buffer* output, uint16_t rdlength, uint16_t* offset)
{
int32_t length;
if (rdlength - *offset < 1)
return -1;
length = buffer_read_u8(output);
if (length + 1 > rdlength - *offset)
return -1;
buffer_skip(output, length);
*offset += length + 1;
return length + 1;
}
/* Return length of strings or -1 on wireformat error. offset is moved +len. */
static inline int32_t
skip_strings(struct buffer* output, uint16_t rdlength, uint16_t* offset)
{
int32_t olen = 0;
while(*offset < rdlength) {
int32_t slen = skip_string(output, rdlength, offset);
if(slen < 0)
return slen;
olen += slen;
}
return olen;
}
/*
* Print string, as "string" with escapes.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_string(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
size_t n;
if(rdlength - *offset < 1)
return 0;
n = rdata[*offset];
if((size_t)rdlength - *offset < 1 + n)
return 0;
buffer_printf(output, "\"");
for (size_t i = 1; i <= n; i++) {
char ch = (char) rdata[*offset+i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u",
(unsigned) rdata[*offset+i]);
}
}
buffer_printf(output, "\"");
*offset += 1;
*offset += n;
return 1;
}
static int32_t
print_text(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
buffer_printf(output, "\"");
for (size_t i = *offset; i < rdlength; ++i) {
char ch = (char) rdata[i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u", (unsigned) rdata[i]);
}
}
buffer_printf(output, "\"");
*offset = rdlength;
return 1;
}
static int
print_unquoted(buffer_type *output, uint16_t rdlength,
const uint8_t* rdata, uint16_t* offset)
{
uint8_t len;
size_t i;
if(rdlength - *offset < 1)
return 0;
len = rdata[*offset];
if(((size_t)len) + 1 > (size_t)rdlength - *offset)
return 0;
for (i = 1; i <= (size_t)len; ++i) {
char ch = (char) rdata[*offset + i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\' || ch == '(' || ch == ')'
|| ch == '\'' || isspace((unsigned char)ch)) {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u",
(unsigned) rdata[*offset + i]);
}
}
*offset += 1;
*offset += len;
return 1;
}
static int
print_unquoteds(buffer_type *output, uint16_t rdlength,
const uint8_t* rdata, uint16_t* offset)
{
while (*offset < rdlength) {
if(!print_unquoted(output, rdlength, rdata, offset))
return 0;
if(*offset < rdlength)
buffer_printf(output, " ");
}
return 1;
}
/*
* Print IP4 address.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_ip4(struct buffer *output, size_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
char str[INET_ADDRSTRLEN + 1];
assert(rdlength >= *offset);
if(((size_t)*offset) + 4 > rdlength)
return 0;
if(!inet_ntop(AF_INET, rdata + *offset, str, sizeof(str)))
return 0;
buffer_printf(output, "%s", str);
*offset += 4;
return 1;
}
/*
* Print IP6 address.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_ip6(struct buffer *output, size_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
char str[INET6_ADDRSTRLEN + 1];
assert(rdlength >= *offset);
if (rdlength - *offset < 16)
return 0;
if (!inet_ntop(AF_INET6, rdata + *offset, str, sizeof(str)))
return 0;
buffer_printf(output, "%s", str);
*offset += 16;
return 1;
}
/*
* Print ilnp64 field.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_ilnp64(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
uint16_t a1, a2, a3, a4;
assert(rdlength >= *offset);
if (rdlength - *offset < 8)
return 0;
a1 = read_uint16(rdata + *offset);
a2 = read_uint16(rdata + *offset + 2);
a3 = read_uint16(rdata + *offset + 4);
a4 = read_uint16(rdata + *offset + 6);
buffer_printf(output, "%.4x:%.4x:%.4x:%.4x", a1, a2, a3, a4);
*offset += 8;
return 1;
}
/*
* Print certificate type.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_certificate_type(struct buffer *output, size_t rdlength,
const uint8_t *rdata, uint16_t *offset)
{
uint16_t id;
lookup_table_type* type;
if (rdlength < *offset || rdlength - *offset < 2)
return 0;
id = read_uint16(rdata + *offset);
type = lookup_by_id(dns_certificate_types, id);
if (type)
buffer_printf(output, "%s", type->name);
else
buffer_printf(output, "%u", (unsigned) id);
*offset += 2;
return 1;
}
/*
* Print time field.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_time(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
time_t time;
struct tm tmbuf;
struct tm* tm;
char buf[15];
assert(rdlength >= *offset);
if (rdlength - *offset < 4)
return 0;
time = (time_t)read_uint32(rdata + *offset);
tm = gmtime_r(&time, &tmbuf);
if (!strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm))
return 0;
buffer_printf(output, "%s", buf);
*offset += 4;
return 1;
}
/*
* Print base32 output for a b32 field length uint8_t, like for NSEC3.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_base32(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
size_t size;
int length;
if(rdlength - *offset == 0)
return 0;
size = rdata[*offset];
if (rdlength - ((size_t)*offset) < 1 + size)
return 0;
if (size == 0) {
buffer_write(output, "-", 1);
*offset += 1;
return 1;
}
buffer_reserve(output, size * 2 + 1);
length = b32_ntop(rdata + *offset + 1, size,
(char *)buffer_current(output), size * 2);
if (length == -1)
return 0;
buffer_skip(output, length);
*offset += 1 + size;
return 1;
}
/*
* Print base64 output for the remainder of rdata.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_base64(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
int length;
size_t size = rdlength - *offset;
if(size == 0) {
/* single zero represents empty buffer */
buffer_write(output, "0", 1);
return 1;
}
buffer_reserve(output, size * 2 + 1);
length = b64_ntop(rdata + *offset, size,
(char *) buffer_current(output), size * 2);
if (length == -1)
return 0;
buffer_skip(output, length);
*offset += size;
return 1;
}
static void
buffer_print_hex(buffer_type *output, const uint8_t *data, size_t size)
{
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
size_t i;
buffer_reserve(output, size * 2);
for (i = 0; i < size; ++i) {
uint8_t octet = *data++;
buffer_write_u8(output, hexdigits[octet >> 4]);
buffer_write_u8(output, hexdigits[octet & 0x0f]);
}
}
/*
* Print base16 output for the remainder of rdata, in hex lowercase.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_base16(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
size_t size = rdlength - *offset;
if(size == 0) {
/* single zero represents empty buffer, such as CDS deletes */
buffer_write(output, "0", 1);
return 1;
} else {
buffer_print_hex(output, rdata+*offset, size);
}
*offset += size;
return 1;
}
/*
* Print salt, for NSEC3, in hex lowercase.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_salt(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
uint8_t length;
assert(rdlength >= *offset);
if (rdlength - *offset == 0)
return 0;
length = rdata[*offset];
if (rdlength - *offset < 1 + length)
return 0;
if (!length)
/* NSEC3 salt hex can be empty */
buffer_printf(output, "-");
else
buffer_print_hex(output, rdata + *offset + 1, length);
*offset += 1 + (uint16_t)length;
return 1;
}
/* Return length of nsec type bitmap or -1 on wireformat error. offset is
* moved +len. rdlength is the length of the rdata, offset is offset in it. */
static inline int32_t
skip_nsec(struct buffer* packet, uint16_t rdlength, uint16_t *offset)
{
uint16_t length = 0;
uint8_t last_window = 0;
while (rdlength - *offset - length > 2) {
uint8_t window = buffer_read_u8(packet);
uint8_t blocks = buffer_read_u8(packet);
if (length > 0 && window <= last_window)
return -1;
if (!blocks || blocks > 32)
return -1;
if (rdlength - *offset - length < 2 + blocks)
return -1;
buffer_skip(packet, blocks);
length += 2 + blocks;
last_window = window;
}
*offset += length;
if (rdlength != *offset)
return -1;
return length;
}
/*
* Print nsec type bitmap, for the remainder of rdata.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_nsec_bitmap(struct buffer *output, uint16_t rdlength,
const uint8_t *rdata, uint16_t *offset)
{
int insert_space = 0;
rdata += *offset;
while(rdlength - *offset > 0) {
uint8_t window, bitmap_size;
const uint8_t* bitmap;
int i;
if(rdlength - *offset < 2)
return 0;
window = rdata[0];
bitmap_size = rdata[1];
*offset += 2;
if(rdlength - *offset < bitmap_size)
return 0;
bitmap = rdata + 2;
rdata += 2;
for(i=0; i<((int)bitmap_size)*8; i++) {
if (get_bit(bitmap, i)) {
buffer_printf(output,
"%s%s",
insert_space ? " " : "",
rrtype_to_string(
window * 256 + i));
insert_space = 1;
}
}
rdata += bitmap_size;
*offset += bitmap_size;
}
return 1;
}
/* If an svcparam must have a value */
static int
svcparam_must_have_value(uint16_t svcparamkey)
{
switch (svcparamkey) {
case SVCB_KEY_ALPN:
case SVCB_KEY_PORT:
case SVCB_KEY_IPV4HINT:
case SVCB_KEY_IPV6HINT:
case SVCB_KEY_MANDATORY:
case SVCB_KEY_DOHPATH:
case SVCB_KEY_TLS_SUPPORTED_GROUPS:
return 1;
default:
break;
}
return 0;
}
/* If an svcparam must not have a value */
static int
svcparam_must_not_have_value(uint16_t svcparamkey)
{
switch (svcparamkey) {
case SVCB_KEY_NO_DEFAULT_ALPN:
case SVCB_KEY_OHTTP:
return 1;
default:
break;
}
return 0;
};
/*
* Skip over the svcparams in the packet. Moves position.
* @param packet: wire packet, position at rdata fields of svcparams.
* @param rdlength: remaining rdata length in the packet.
* @return 0 on wireformat error.
*/
static inline int
skip_svcparams(struct buffer *packet, uint16_t rdlength)
{
unsigned pos = 0;
uint16_t key, count;
while(pos < rdlength) {
if(pos+4 > (unsigned)rdlength)
return 0;
if(!buffer_available(packet, 4))
return 0;
key = buffer_read_u16(packet);
count = buffer_read_u16(packet);
if(count == 0 && svcparam_must_have_value(key))
return 0;
if(count != 0 && svcparam_must_not_have_value(key))
return 0;
pos += 4;
if(pos+count > (unsigned)rdlength)
return 0;
if(!buffer_available(packet, count))
return 0;
buffer_skip(packet, count);
pos += count;
}
return 1;
}
/*
* Print svcparamkey name to the buffer, or unknown as key<NUM>.
* @param output: printed to string.
* @param svcparamkey: the key to print.
*/
static void
buffer_print_svcparamkey(buffer_type *output, uint16_t svcparamkey)
{
if (svcparamkey < sizeof(svcparams)/sizeof(svcparams[0]))
buffer_printf(output, "%s", svcparams[svcparamkey].name);
else
buffer_printf(output, "key%" PRIu16, svcparamkey);
}
static int
print_svcparam_no_value(struct buffer *output, uint16_t svcparamkey,
const uint8_t* ATTR_UNUSED(data), uint16_t ATTR_UNUSED(datalen))
{
buffer_print_svcparamkey(output, svcparamkey);
return 1;
}
static int
print_svcparam_mandatory(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
assert(datalen > 0); /* Guaranteed by svcparam_print */
if (datalen % sizeof(uint16_t))
return 0; /* wireformat error, val_len must be multiple of shorts */
buffer_print_svcparamkey(output, svcparamkey);
buffer_write_u8(output, '=');
buffer_print_svcparamkey(output, read_uint16(data));
data += 2;
while ((datalen -= sizeof(uint16_t))) {
buffer_write_u8(output, ',');
buffer_print_svcparamkey(output, read_uint16(data));
data += 2;
}
return 1;
}
static int
print_svcparam_alpn(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
uint8_t *dp = (void *)data;
assert(datalen > 0); /* Guaranteed by svcparam_print */
buffer_print_svcparamkey(output, svcparamkey);
buffer_write_u8(output, '=');
buffer_write_u8(output, '"');
while (datalen) {
uint8_t i, str_len = *dp++;
if (str_len > --datalen)
return 0;
for (i = 0; i < str_len; i++) {
if (dp[i] == '"' || dp[i] == '\\')
buffer_printf(output, "\\\\\\%c", dp[i]);
else if (dp[i] == ',')
buffer_printf(output, "\\\\%c", dp[i]);
else if (!isprint(dp[i]))
buffer_printf(output, "\\%03u", (unsigned) dp[i]);
else
buffer_write_u8(output, dp[i]);
}
dp += str_len;
if ((datalen -= str_len))
buffer_write_u8(output, ',');
}
buffer_write_u8(output, '"');
return 1;
}
static int
print_svcparam_port(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
if (datalen != 2)
return 0; /* wireformat error, a short is 2 bytes */
buffer_print_svcparamkey(output, svcparamkey);
buffer_printf(output, "=%d", (int)read_uint16(data));
return 1;
}
static int
print_svcparam_ipv4hint(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
char ip_str[INET_ADDRSTRLEN + 1];
assert(datalen > 0); /* Guaranteed by svcparam_print */
buffer_print_svcparamkey(output, svcparamkey);
if ((datalen % IP4ADDRLEN) == 0) {
if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, "=%s", ip_str);
data += IP4ADDRLEN;
while ((datalen -= IP4ADDRLEN) > 0) {
if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str))
== NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, ",%s", ip_str);
data += IP4ADDRLEN;
}
return 1;
}
return 0;
}
static int
print_svcparam_ech(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
int length;
buffer_print_svcparamkey(output, svcparamkey);
if(datalen == 0)
return 1;
buffer_write_u8(output, '=');
buffer_reserve(output, datalen * 2 + 1);
length = b64_ntop(data, datalen, (char*)buffer_current(output),
datalen * 2);
if (length > 0) {
buffer_skip(output, length);
}
return length != -1;
}
static int
print_svcparam_ipv6hint(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
char ip_str[INET6_ADDRSTRLEN + 1];
assert(datalen > 0); /* Guaranteed by svcparam_print */
buffer_print_svcparamkey(output, svcparamkey);
if ((datalen % IP6ADDRLEN) == 0) {
if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, "=%s", ip_str);
data += IP6ADDRLEN;
while ((datalen -= IP6ADDRLEN) > 0) {
if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str))
== NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, ",%s", ip_str);
data += IP6ADDRLEN;
}
return 1;
}
return 0;
}
static int
print_svcparam_dohpath(struct buffer *output, uint16_t svcparamkey,
const uint8_t* data, uint16_t datalen)
{
const uint8_t* dp = data;
unsigned i;
buffer_print_svcparamkey(output, svcparamkey);
buffer_write(output, "=\"", 2);
for (i = 0; i < datalen; i++) {
if (dp[i] == '"' || dp[i] == '\\')
buffer_printf(output, "\\%c", dp[i]);
else if (!isprint(dp[i]))
buffer_printf(output, "\\%03u", (unsigned) dp[i]);
else
buffer_write_u8(output, dp[i]);
}
buffer_write_u8(output, '"');
return 1;
}
static int
print_svcparam_tls_supported_groups(struct buffer *output,
uint16_t svcparamkey, const uint8_t* data, uint16_t datalen)
{
assert(datalen > 0); /* Guaranteed by svcparam_print */
if ((datalen % sizeof(uint16_t)) == 1)
return 0; /* A series of uint16_t is an even number of bytes */
buffer_print_svcparamkey(output, svcparamkey);
buffer_printf(output, "=%d", (int)read_uint16(data));
data += 2;
while ((datalen -= sizeof(uint16_t)) > 0) {
buffer_printf(output, ",%d", (int)read_uint16(data));
data += 2;
}
return 1;
}
/*
* Print svcparam.
* @param output: the string is output here.
* @param rdlength: length of rdata.
* @param rdata: the rdata. The rdata+*offset is where the field is.
* @param offset: the current position on input. The position is updated to
* be incremented with the length of rdata that was used.
* @return false on failure.
*/
static int
print_svcparam(struct buffer *output, uint16_t rdlength, const uint8_t *rdata,
uint16_t *offset)
{
uint16_t key, length;
const uint8_t* dp;
unsigned i;
assert(rdlength >= *offset);
if (rdlength - *offset < 4)
return 0;
key = read_uint16(rdata + *offset);
length = read_uint16(rdata + *offset + 2);
if (rdlength - *offset < length + 4)
return 0; /* wireformat error */
if(length == 0 && svcparam_must_have_value(key))
return 0;
if(length != 0 && svcparam_must_not_have_value(key))
return 0;
if (key < sizeof(svcparams)/sizeof(svcparams[0])) {
if(!svcparams[key].print_rdata(output, key, rdata+*offset+4, length))
return 0;
*offset += length+4;
return 1;
}
buffer_printf(output, "key%" PRIu16, key);
if (!length) {
*offset += 4;
return 1;
}
buffer_write(output, "=\"", 2);
dp = rdata + *offset + 4;
for (i = 0; i < length; i++) {