Debian Codegraph @

parse-root-anchors

raw
1 #!/usr/bin/perl
2 # vim: shiftwidth=4 tabstop=4
3 #
4 # This program implements the procedure defined in RFC 7958bis to update the
5 # root zone DNSSEC trust anchors.
6 #
7 # https://datatracker.ietf.org/doc/draft-ietf-dnsop-rfc7958bis/
8
9 use v5.32;
10 use warnings;
11
12 use XML::LibXML;
13 use DateTime;
14 use DateTime::Format::RFC3339;
15 use Net::DNS;
16 use Path::Tiny;
17
18 ##############################################################################
19 sub parse_root_anchors {
20 my ($file) = @_;
21
22 my $now = DateTime->now;
23 my $format = DateTime::Format::RFC3339->new;
24
25 my $dom = XML::LibXML->load_xml(location => $file);
26
27 # check the basic XML structure of the file
28 my ($zone) = $dom->findnodes('/TrustAnchor/Zone')
29 or die "<TrustAnchor><Zone> node not found!\n";
30 my $zone_value = $zone->to_literal or die;
31 die "These hints are not for the root zone!\n" if not $zone_value eq '.';
32
33 # parse each anchor
34 my (@dnskey, @ds);
35 foreach my $key ($dom->findnodes('/TrustAnchor/KeyDigest')) {
36 my $id = $key->{id} or die 'No key id';
37 my $tag = $key->findvalue('./KeyTag') or die 'No key tag';
38
39 my $valid_until = $key->{validUntil};
40 if (defined $valid_until) {
41 $valid_until = $format->parse_datetime($valid_until);
42
43 if (DateTime->compare($valid_until, $now) <= 0) {
44 say "Key $tag ignored: it expired on $valid_until.";
45 next;
46 }
47 }
48 my $valid_from = $key->{validFrom};
49 if (defined $valid_from) {
50 $valid_from = $format->parse_datetime($valid_from);
51 say "Key $tag is or will be valid from $valid_from.";
52 } else {
53 say "Key $tag has no initial validity date defined.";
54 }
55
56 my $new_ds = Net::DNS::RR->new(
57 owner => '.',
58 type => 'DS',
59 keytag => $tag,
60 algorithm => $key->findvalue('./Algorithm'),
61 digtype => $key->findvalue('./DigestType'),
62 digest => $key->findvalue('./Digest')
63 );
64 push(@ds, $new_ds);
65
66 my $publickey = $key->findvalue('./PublicKey') or next;
67 my $new_dnskey = Net::DNS::RR->new(
68 owner => '.',
69 type => 'DNSKEY',
70 keytag => $tag,
71 algorithm => $key->findvalue('./Algorithm'),
72 flags => $key->findvalue('./Flags'),
73 key => $publickey,
74 );
75 compare_key_ds($new_dnskey, $new_ds);
76 push(@dnskey, $new_dnskey);
77 }
78
79 return {
80 dnskey => \@dnskey,
81 ds => \@ds,
82 };
83 }
84
85 ##############################################################################
86 # Make sure that the DS record matches the DNSKEY record, as required by
87 # RFC 7958bis section 4.1.2.
88 sub compare_key_ds {
89 my ($key, $ds) = @_;
90
91 # create a DS record computed from the key in the DNSKEY record
92 my $dsk = Net::DNS::RR::DS->create(
93 $key,
94 digtype => $ds->digtype,
95 );
96
97 # and check they it matches the anchor DS record
98 if ($ds->algorithm ne $dsk->algorithm or $ds->digest ne $dsk->digest) {
99 say 'The DS record in the root anchors file:';
100 $ds->print;
101 say "\ndoes not match the DS record computed from the key in the"
102 . " root anchors file:";
103 $dsk->print;
104 die;
105 }
106
107 return 1;
108 }
109
110 ##############################################################################
111 sub write_ds {
112 my ($file, $data) = @_;
113
114 my $out = path($file);
115 my @lines = map {
116 join(' ', $_->{owner}->string, $_->class, $_->type,
117 $_->keytag, $_->algorithm, $_->digtype, uc $_->digest)
118 . "\n"
119 } @$data;
120 $out->spew(@lines);
121 return;
122 }
123
124 sub write_dnskey {
125 my ($file, $data) = @_;
126
127 my $out = path($file);
128 my @lines = map {
129 join(' ', $_->{owner}->string, $_->class, $_->type,
130 $_->flags, $_->protocol, $_->algorithm, $_->key)
131 . " ; keytag " . $_->keytag . "\n"
132 } @$data;
133 $out->spew(@lines);
134 return;
135 }
136
137 ##############################################################################
138 my $data = parse_root_anchors('root-anchors.xml');
139
140 die 'No DNSKEY records found' if not @{ $data->{dnskey} };
141 die 'No DS records found' if not @{ $data->{ds} };
142
143 write_ds('root.ds', $data->{ds});
144 write_dnskey('root.key', $data->{dnskey});
145