Debian Codegraph @

src/resolve/test-dnssec.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <netinet/in.h>
5
6 #include "alloc-util.h"
7 #include "bitmap.h"
8 #include "dns-answer.h"
9 #include "dns-rr.h"
10 #include "hexdecoct.h"
11 #include "resolved-dns-dnssec.h"
12 #include "string-util.h"
13 #include "tests.h"
14 #include "time-util.h"
15
16 TEST(dnssec_verify_dns_key) {
17 static const uint8_t ds1_fprint[] = {
18 0x46, 0x8B, 0xC8, 0xDD, 0xC7, 0xE8, 0x27, 0x03, 0x40, 0xBB, 0x8A, 0x1F, 0x3B, 0x2E, 0x45, 0x9D,
19 0x80, 0x67, 0x14, 0x01,
20 };
21 static const uint8_t ds2_fprint[] = {
22 0x8A, 0xEE, 0x80, 0x47, 0x05, 0x5F, 0x83, 0xD1, 0x48, 0xBA, 0x8F, 0xF6, 0xDD, 0xA7, 0x60, 0xCE,
23 0x94, 0xF7, 0xC7, 0x5E, 0x52, 0x4C, 0xF2, 0xE9, 0x50, 0xB9, 0x2E, 0xCB, 0xEF, 0x96, 0xB9, 0x98,
24 };
25 static const uint8_t dnskey_blob[] = {
26 0x03, 0x01, 0x00, 0x01, 0xa8, 0x12, 0xda, 0x4f, 0xd2, 0x7d, 0x54, 0x14, 0x0e, 0xcc, 0x5b, 0x5e,
27 0x45, 0x9c, 0x96, 0x98, 0xc0, 0xc0, 0x85, 0x81, 0xb1, 0x47, 0x8c, 0x7d, 0xe8, 0x39, 0x50, 0xcc,
28 0xc5, 0xd0, 0xf2, 0x00, 0x81, 0x67, 0x79, 0xf6, 0xcc, 0x9d, 0xad, 0x6c, 0xbb, 0x7b, 0x6f, 0x48,
29 0x97, 0x15, 0x1c, 0xfd, 0x0b, 0xfe, 0xd3, 0xd7, 0x7d, 0x9f, 0x81, 0x26, 0xd3, 0xc5, 0x65, 0x49,
30 0xcf, 0x46, 0x62, 0xb0, 0x55, 0x6e, 0x47, 0xc7, 0x30, 0xef, 0x51, 0xfb, 0x3e, 0xc6, 0xef, 0xde,
31 0x27, 0x3f, 0xfa, 0x57, 0x2d, 0xa7, 0x1d, 0x80, 0x46, 0x9a, 0x5f, 0x14, 0xb3, 0xb0, 0x2c, 0xbe,
32 0x72, 0xca, 0xdf, 0xb2, 0xff, 0x36, 0x5b, 0x4f, 0xec, 0x58, 0x8e, 0x8d, 0x01, 0xe9, 0xa9, 0xdf,
33 0xb5, 0x60, 0xad, 0x52, 0x4d, 0xfc, 0xa9, 0x3e, 0x8d, 0x35, 0x95, 0xb3, 0x4e, 0x0f, 0xca, 0x45,
34 0x1b, 0xf7, 0xef, 0x3a, 0x88, 0x25, 0x08, 0xc7, 0x4e, 0x06, 0xc1, 0x62, 0x1a, 0xce, 0xd8, 0x77,
35 0xbd, 0x02, 0x65, 0xf8, 0x49, 0xfb, 0xce, 0xf6, 0xa8, 0x09, 0xfc, 0xde, 0xb2, 0x09, 0x9d, 0x39,
36 0xf8, 0x63, 0x9c, 0x32, 0x42, 0x7c, 0xa0, 0x30, 0x86, 0x72, 0x7a, 0x4a, 0xc6, 0xd4, 0xb3, 0x2d,
37 0x24, 0xef, 0x96, 0x3f, 0xc2, 0xda, 0xd3, 0xf2, 0x15, 0x6f, 0xda, 0x65, 0x4b, 0x81, 0x28, 0x68,
38 0xf4, 0xfe, 0x3e, 0x71, 0x4f, 0x50, 0x96, 0x72, 0x58, 0xa1, 0x89, 0xdd, 0x01, 0x61, 0x39, 0x39,
39 0xc6, 0x76, 0xa4, 0xda, 0x02, 0x70, 0x3d, 0xc0, 0xdc, 0x8d, 0x70, 0x72, 0x04, 0x90, 0x79, 0xd4,
40 0xec, 0x65, 0xcf, 0x49, 0x35, 0x25, 0x3a, 0x14, 0x1a, 0x45, 0x20, 0xeb, 0x31, 0xaf, 0x92, 0xba,
41 0x20, 0xd3, 0xcd, 0xa7, 0x13, 0x44, 0xdc, 0xcf, 0xf0, 0x27, 0x34, 0xb9, 0xe7, 0x24, 0x6f, 0x73,
42 0xe7, 0xea, 0x77, 0x03,
43 };
44
45 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dnskey = NULL, *ds1 = NULL, *ds2 = NULL;
46
47 /* The two DS RRs in effect for nasa.gov on 2015-12-01. */
48 ds1 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "nasa.gov");
49 assert_se(ds1);
50
51 ds1->ds.key_tag = 47857;
52 ds1->ds.algorithm = DNSSEC_ALGORITHM_RSASHA256;
53 ds1->ds.digest_type = DNSSEC_DIGEST_SHA1;
54 ds1->ds.digest_size = sizeof(ds1_fprint);
55 ds1->ds.digest = memdup(ds1_fprint, ds1->ds.digest_size);
56 assert_se(ds1->ds.digest);
57
58 log_info("DS1: %s", strna(dns_resource_record_to_string(ds1)));
59
60 ds2 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "NASA.GOV");
61 assert_se(ds2);
62
63 ds2->ds.key_tag = 47857;
64 ds2->ds.algorithm = DNSSEC_ALGORITHM_RSASHA256;
65 ds2->ds.digest_type = DNSSEC_DIGEST_SHA256;
66 ds2->ds.digest_size = sizeof(ds2_fprint);
67 ds2->ds.digest = memdup(ds2_fprint, ds2->ds.digest_size);
68 assert_se(ds2->ds.digest);
69
70 log_info("DS2: %s", strna(dns_resource_record_to_string(ds2)));
71
72 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "nasa.GOV");
73 assert_se(dnskey);
74
75 dnskey->dnskey.flags = 257;
76 dnskey->dnskey.protocol = 3;
77 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_RSASHA256;
78 dnskey->dnskey.key_size = sizeof(dnskey_blob);
79 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
80 assert_se(dnskey->dnskey.key);
81
82 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
83 log_info("DNSKEY keytag: %u", dnssec_keytag(dnskey, false));
84
85 assert_se(dnssec_verify_dnskey_by_ds(dnskey, ds1, false) > 0);
86 assert_se(dnssec_verify_dnskey_by_ds(dnskey, ds2, false) > 0);
87 }
88
89 TEST(dnssec_verify_rfc8080_ed25519_example1) {
90 static const uint8_t dnskey_blob[] = {
91 0x97, 0x4d, 0x96, 0xa2, 0x2d, 0x22, 0x4b, 0xc0, 0x1a, 0xdb, 0x91, 0x50, 0x91, 0x47, 0x7d,
92 0x44, 0xcc, 0xd9, 0x1c, 0x9a, 0x41, 0xa1, 0x14, 0x30, 0x01, 0x01, 0x17, 0xd5, 0x2c, 0x59,
93 0x24, 0xe
94 };
95 static const uint8_t ds_fprint[] = {
96 0xdd, 0xa6, 0xb9, 0x69, 0xbd, 0xfb, 0x79, 0xf7, 0x1e, 0xe7, 0xb7, 0xfb, 0xdf, 0xb7, 0xdc,
97 0xd7, 0xad, 0xbb, 0xd3, 0x5d, 0xdf, 0x79, 0xed, 0x3b, 0x6d, 0xd7, 0xf6, 0xe3, 0x56, 0xdd,
98 0xd7, 0x47, 0xf7, 0x6f, 0x5f, 0x7a, 0xe1, 0xa6, 0xf9, 0xe5, 0xce, 0xfc, 0x7b, 0xbf, 0x5a,
99 0xdf, 0x4e, 0x1b
100 };
101 static const uint8_t signature_blob[] = {
102 0xa0, 0xbf, 0x64, 0xac, 0x9b, 0xa7, 0xef, 0x17, 0xc1, 0x38, 0x85, 0x9c, 0x18, 0x78, 0xbb,
103 0x99, 0xa8, 0x39, 0xfe, 0x17, 0x59, 0xac, 0xa5, 0xb0, 0xd7, 0x98, 0xcf, 0x1a, 0xb1, 0xe9,
104 0x8d, 0x07, 0x91, 0x02, 0xf4, 0xdd, 0xb3, 0x36, 0x8f, 0x0f, 0xe4, 0x0b, 0xb3, 0x77, 0xf1,
105 0xf0, 0x0e, 0x0c, 0xdd, 0xed, 0xb7, 0x99, 0x16, 0x7d, 0x56, 0xb6, 0xe9, 0x32, 0x78, 0x30,
106 0x72, 0xba, 0x8d, 0x02
107 };
108
109 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dnskey = NULL, *ds = NULL, *mx = NULL,
110 *rrsig = NULL;
111 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
112 DnssecResult result;
113
114 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "example.com.");
115 assert_se(dnskey);
116
117 dnskey->dnskey.flags = 257;
118 dnskey->dnskey.protocol = 3;
119 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_ED25519;
120 dnskey->dnskey.key_size = sizeof(dnskey_blob);
121 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
122 assert_se(dnskey->dnskey.key);
123
124 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
125
126 ds = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "example.com.");
127 assert_se(ds);
128
129 ds->ds.key_tag = 3613;
130 ds->ds.algorithm = DNSSEC_ALGORITHM_ED25519;
131 ds->ds.digest_type = DNSSEC_DIGEST_SHA256;
132 ds->ds.digest_size = sizeof(ds_fprint);
133 ds->ds.digest = memdup(ds_fprint, ds->ds.digest_size);
134 assert_se(ds->ds.digest);
135
136 log_info("DS: %s", strna(dns_resource_record_to_string(ds)));
137
138 mx = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "example.com.");
139 assert_se(mx);
140
141 mx->mx.priority = 10;
142 mx->mx.exchange = strdup("mail.example.com.");
143 assert_se(mx->mx.exchange);
144
145 log_info("MX: %s", strna(dns_resource_record_to_string(mx)));
146
147 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "example.com.");
148 assert_se(rrsig);
149
150 rrsig->rrsig.type_covered = DNS_TYPE_MX;
151 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_ED25519;
152 rrsig->rrsig.labels = 2;
153 rrsig->rrsig.original_ttl = 3600;
154 rrsig->rrsig.expiration = 1440021600;
155 rrsig->rrsig.inception = 1438207200;
156 rrsig->rrsig.key_tag = 3613;
157 rrsig->rrsig.signer = strdup("example.com.");
158 assert_se(rrsig->rrsig.signer);
159 rrsig->rrsig.signature_size = sizeof(signature_blob);
160 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
161 assert_se(rrsig->rrsig.signature);
162
163 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
164
165 assert_se(dnssec_key_match_rrsig(mx->key, rrsig) > 0);
166 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
167
168 answer = dns_answer_new(1);
169 assert_se(answer);
170 assert_se(dns_answer_add(answer, mx, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
171
172 assert_se(dnssec_verify_rrset(answer, mx->key, rrsig, dnskey,
173 rrsig->rrsig.inception * USEC_PER_SEC, &result) >= 0);
174 assert_se(result == DNSSEC_VALIDATED);
175 }
176
177 TEST(dnssec_verify_rfc8080_ed25519_example2) {
178 static const uint8_t dnskey_blob[] = {
179 0xcc, 0xf9, 0xd9, 0xfd, 0x0c, 0x04, 0x7b, 0xb4, 0xbc, 0x0b, 0x94, 0x8f, 0xcf, 0x63, 0x9f,
180 0x4b, 0x94, 0x51, 0xe3, 0x40, 0x13, 0x93, 0x6f, 0xeb, 0x62, 0x71, 0x3d, 0xc4, 0x72, 0x4,
181 0x8a, 0x3b
182 };
183 static const uint8_t ds_fprint[] = {
184 0xe3, 0x4d, 0x7b, 0xf3, 0x56, 0xfd, 0xdf, 0x87, 0xb7, 0xf7, 0x67, 0x5e, 0xe3, 0xdd, 0x9e,
185 0x73, 0xbe, 0xda, 0x7b, 0x67, 0xb5, 0xe5, 0xde, 0xf4, 0x7f, 0xae, 0x7b, 0xe5, 0xad, 0x5c,
186 0xd1, 0xb7, 0x39, 0xf5, 0xce, 0x76, 0xef, 0x97, 0x34, 0xe1, 0xe6, 0xde, 0xf3, 0x47, 0x3a,
187 0xeb, 0x5e, 0x1c
188 };
189 static const uint8_t signature_blob[] = {
190 0xcd, 0x74, 0x34, 0x6e, 0x46, 0x20, 0x41, 0x31, 0x05, 0xc9, 0xf2, 0xf2, 0x8b, 0xd4, 0x28,
191 0x89, 0x8e, 0x83, 0xf1, 0x97, 0x58, 0xa3, 0x8c, 0x32, 0x52, 0x15, 0x62, 0xa1, 0x86, 0x57,
192 0x15, 0xd4, 0xf8, 0xd7, 0x44, 0x0f, 0x44, 0x84, 0xd0, 0x4a, 0xa2, 0x52, 0x9f, 0x34, 0x28,
193 0x4a, 0x6e, 0x69, 0xa0, 0x9e, 0xe0, 0x0f, 0xb0, 0x10, 0x47, 0x43, 0xbb, 0x2a, 0xe2, 0x39,
194 0x93, 0x6a, 0x5c, 0x06
195 };
196
197 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dnskey = NULL, *ds = NULL, *mx = NULL,
198 *rrsig = NULL;
199 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
200 DnssecResult result;
201
202 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "example.com.");
203 assert_se(dnskey);
204
205 dnskey->dnskey.flags = 257;
206 dnskey->dnskey.protocol = 3;
207 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_ED25519;
208 dnskey->dnskey.key_size = sizeof(dnskey_blob);
209 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
210 assert_se(dnskey->dnskey.key);
211
212 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
213
214 ds = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "example.com.");
215 assert_se(ds);
216
217 ds->ds.key_tag = 35217;
218 ds->ds.algorithm = DNSSEC_ALGORITHM_ED25519;
219 ds->ds.digest_type = DNSSEC_DIGEST_SHA256;
220 ds->ds.digest_size = sizeof(ds_fprint);
221 ds->ds.digest = memdup(ds_fprint, ds->ds.digest_size);
222 assert_se(ds->ds.digest);
223
224 log_info("DS: %s", strna(dns_resource_record_to_string(ds)));
225
226 mx = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "example.com.");
227 assert_se(mx);
228
229 mx->mx.priority = 10;
230 mx->mx.exchange = strdup("mail.example.com.");
231 assert_se(mx->mx.exchange);
232
233 log_info("MX: %s", strna(dns_resource_record_to_string(mx)));
234
235 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "example.com.");
236 assert_se(rrsig);
237
238 rrsig->rrsig.type_covered = DNS_TYPE_MX;
239 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_ED25519;
240 rrsig->rrsig.labels = 2;
241 rrsig->rrsig.original_ttl = 3600;
242 rrsig->rrsig.expiration = 1440021600;
243 rrsig->rrsig.inception = 1438207200;
244 rrsig->rrsig.key_tag = 35217;
245 rrsig->rrsig.signer = strdup("example.com.");
246 assert_se(rrsig->rrsig.signer);
247 rrsig->rrsig.signature_size = sizeof(signature_blob);
248 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
249 assert_se(rrsig->rrsig.signature);
250
251 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
252
253 assert_se(dnssec_key_match_rrsig(mx->key, rrsig) > 0);
254 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
255
256 answer = dns_answer_new(1);
257 assert_se(answer);
258 assert_se(dns_answer_add(answer, mx, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
259
260 assert_se(dnssec_verify_rrset(answer, mx->key, rrsig, dnskey,
261 rrsig->rrsig.inception * USEC_PER_SEC, &result) >= 0);
262 assert_se(result == DNSSEC_VALIDATED);
263 }
264
265 TEST(dnssec_verify_rfc6605_example1) {
266 static const uint8_t signature_blob[] = {
267 0xab, 0x1e, 0xb0, 0x2d, 0x8a, 0xa6, 0x87, 0xe9, 0x7d, 0xa0, 0x22, 0x93, 0x37, 0xaa, 0x88, 0x73,
268 0xe6, 0xf0, 0xeb, 0x26, 0xbe, 0x28, 0x9f, 0x28, 0x33, 0x3d, 0x18, 0x3f, 0x5d, 0x3b, 0x7a, 0x95,
269 0xc0, 0xc8, 0x69, 0xad, 0xfb, 0x74, 0x8d, 0xae, 0xe3, 0xc5, 0x28, 0x6e, 0xed, 0x66, 0x82, 0xc1,
270 0x2e, 0x55, 0x33, 0x18, 0x6b, 0xac, 0xed, 0x9c, 0x26, 0xc1, 0x67, 0xa9, 0xeb, 0xae, 0x95, 0x0b,
271 };
272
273 static const uint8_t ds_fprint[] = {
274 0x6f, 0x87, 0x3c, 0x73, 0x57, 0xde, 0xd9, 0xee, 0xf8, 0xef, 0xbd, 0x76, 0xed, 0xbd, 0xbb, 0xd7,
275 0x5e, 0x7a, 0xe7, 0xa6, 0x9d, 0xeb, 0x6e, 0x7a, 0x7f, 0x8d, 0xb8, 0xeb, 0x6e, 0x5b, 0x7f, 0x97,
276 0x35, 0x7b, 0x6e, 0xfb, 0xd1, 0xc7, 0xba, 0x77, 0xa7, 0xb7, 0xed, 0xd7, 0xfa, 0xd5, 0xdd, 0x7b,
277 };
278
279 static const uint8_t dnskey_blob[] = {
280 0x1a, 0x88, 0xc8, 0x86, 0x15, 0xd4, 0x37, 0xfb, 0xb8, 0xbf, 0x9e, 0x19, 0x42, 0xa1, 0x92, 0x9f,
281 0x28, 0x56, 0x27, 0x06, 0xae, 0x6c, 0x2b, 0xd3, 0x99, 0xe7, 0xb1, 0xbf, 0xb6, 0xd1, 0xe9, 0xe7,
282 0x5b, 0x92, 0xb4, 0xaa, 0x42, 0x91, 0x7a, 0xe1, 0xc6, 0x1b, 0x70, 0x1e, 0xf0, 0x35, 0xc3, 0xfe,
283 0x7b, 0xe3, 0x00, 0x9c, 0xba, 0xfe, 0x5a, 0x2f, 0x71, 0x31, 0x6c, 0x90, 0x2d, 0xcf, 0x0d, 0x00,
284 };
285
286 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dnskey = NULL, *ds = NULL, *a = NULL,
287 *rrsig = NULL;
288 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
289 DnssecResult result;
290
291 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "example.net.");
292 assert_se(dnskey);
293
294 dnskey->dnskey.flags = 257;
295 dnskey->dnskey.protocol = 3;
296 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_ECDSAP256SHA256;
297 dnskey->dnskey.key_size = sizeof(dnskey_blob);
298 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
299 assert_se(dnskey->dnskey.key);
300
301 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
302
303 ds = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "example.net.");
304 assert_se(ds);
305
306 ds->ds.key_tag = 55648;
307 ds->ds.algorithm = DNSSEC_ALGORITHM_ECDSAP256SHA256;
308 ds->ds.digest_type = DNSSEC_DIGEST_SHA256;
309 ds->ds.digest_size = sizeof(ds_fprint);
310 ds->ds.digest = memdup(ds_fprint, ds->ds.digest_size);
311 assert_se(ds->ds.digest);
312
313 log_info("DS: %s", strna(dns_resource_record_to_string(ds)));
314
315 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.net");
316 assert_se(a);
317
318 a->a.in_addr.s_addr = inet_addr("192.0.2.1");
319
320 log_info("A: %s", strna(dns_resource_record_to_string(a)));
321
322 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.net.");
323 assert_se(rrsig);
324
325 rrsig->rrsig.type_covered = DNS_TYPE_A;
326 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_ECDSAP256SHA256;
327 rrsig->rrsig.labels = 3;
328 rrsig->rrsig.expiration = 1284026679;
329 rrsig->rrsig.inception = 1281607479;
330 rrsig->rrsig.key_tag = 55648;
331 rrsig->rrsig.original_ttl = 3600;
332 rrsig->rrsig.signer = strdup("example.net.");
333 assert_se(rrsig->rrsig.signer);
334 rrsig->rrsig.signature_size = sizeof(signature_blob);
335 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
336 assert_se(rrsig->rrsig.signature);
337
338 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
339
340 assert_se(dnssec_key_match_rrsig(a->key, rrsig) > 0);
341 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
342
343 answer = dns_answer_new(1);
344 assert_se(answer);
345 assert_se(dns_answer_add(answer, a, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
346
347 assert_se(dnssec_verify_rrset(answer, a->key, rrsig, dnskey,
348 rrsig->rrsig.inception * USEC_PER_SEC, &result) >= 0);
349 assert_se(result == DNSSEC_VALIDATED);
350 }
351
352 TEST(dnssec_verify_rfc6605_example2) {
353 static const uint8_t signature_blob[] = {
354 0xfc, 0xbe, 0x61, 0x0c, 0xa2, 0x2f, 0x18, 0x3c, 0x88, 0xd5, 0xf7, 0x00, 0x45, 0x7d, 0xf3, 0xeb,
355 0x9a, 0xab, 0x98, 0xfb, 0x15, 0xcf, 0xbd, 0xd0, 0x0f, 0x53, 0x2b, 0xe4, 0x21, 0x2a, 0x3a, 0x22,
356 0xcf, 0xf7, 0x98, 0x71, 0x42, 0x8b, 0xae, 0xae, 0x81, 0x82, 0x79, 0x93, 0xaf, 0xcc, 0x56, 0xb1,
357 0xb1, 0x3f, 0x06, 0x96, 0xbe, 0xf8, 0x85, 0xb6, 0xaf, 0x44, 0xa6, 0xb2, 0x24, 0xdb, 0xb2, 0x74,
358 0x2b, 0xb3, 0x59, 0x34, 0x92, 0x3d, 0xdc, 0xfb, 0xc2, 0x7a, 0x97, 0x2f, 0x96, 0xdd, 0x70, 0x9c,
359 0xee, 0xb1, 0xd9, 0xc8, 0xd1, 0x14, 0x8c, 0x44, 0xec, 0x71, 0xc0, 0x68, 0xa9, 0x59, 0xc2, 0x66,
360
361 };
362
363 static const uint8_t ds_fprint[] = {
364 0xef, 0x67, 0x7b, 0x6f, 0xad, 0xbd, 0xef, 0xa7, 0x1e, 0xd3, 0xae, 0x37, 0xf1, 0xef, 0x5c, 0xd1,
365 0xb7, 0xf7, 0xd7, 0xdd, 0x35, 0xdd, 0xc7, 0xfc, 0xd3, 0x57, 0xf4, 0xf5, 0xe7, 0x1c, 0xf3, 0x86,
366 0xfc, 0x77, 0xb7, 0xbd, 0xe3, 0xde, 0x5f, 0xdb, 0xb7, 0xb7, 0xd3, 0x97, 0x3a, 0x6b, 0xd6, 0xf4,
367 0xe7, 0xad, 0xda, 0xf5, 0xbe, 0x5f, 0xe1, 0xdd, 0xbc, 0xf3, 0x8d, 0x39, 0x73, 0x7d, 0x34, 0xf1,
368 0xaf, 0x78, 0xe9, 0xd7, 0xfd, 0xf3, 0x77, 0x7a,
369 };
370
371 static const uint8_t dnskey_blob[] = {
372 0xc4, 0xa6, 0x1a, 0x36, 0x15, 0x9d, 0x18, 0xe7, 0xc9, 0xfa, 0x73, 0xeb, 0x2f, 0xcf, 0xda, 0xae,
373 0x4c, 0x1f, 0xd8, 0x46, 0x37, 0x30, 0x32, 0x7e, 0x48, 0x4a, 0xca, 0x8a, 0xf0, 0x55, 0x4a, 0xe9,
374 0xb5, 0xc3, 0xf7, 0xa0, 0xb1, 0x7b, 0xd2, 0x00, 0x3b, 0x4d, 0x26, 0x1c, 0x9e, 0x9b, 0x94, 0x42,
375 0x3a, 0x98, 0x10, 0xe8, 0xaf, 0x17, 0xd4, 0x34, 0x52, 0x12, 0x4a, 0xdb, 0x61, 0x0f, 0x8e, 0x07,
376 0xeb, 0xfc, 0xfe, 0xe5, 0xf8, 0xe4, 0xd0, 0x70, 0x63, 0xca, 0xe9, 0xeb, 0x91, 0x7a, 0x1a, 0x5b,
377 0xab, 0xf0, 0x8f, 0xe6, 0x95, 0x53, 0x60, 0x17, 0xa5, 0xbf, 0xa9, 0x32, 0x37, 0xee, 0x6e, 0x34,
378 };
379
380 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dnskey = NULL, *ds = NULL, *a = NULL,
381 *rrsig = NULL;
382 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
383 DnssecResult result;
384
385 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "example.net.");
386 assert_se(dnskey);
387
388 dnskey->dnskey.flags = 257;
389 dnskey->dnskey.protocol = 3;
390 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_ECDSAP384SHA384;
391 dnskey->dnskey.key_size = sizeof(dnskey_blob);
392 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
393 assert_se(dnskey->dnskey.key);
394
395 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
396
397 ds = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "example.net.");
398 assert_se(ds);
399
400 ds->ds.key_tag = 10771;
401 ds->ds.algorithm = DNSSEC_ALGORITHM_ECDSAP384SHA384;
402 ds->ds.digest_type = DNSSEC_DIGEST_SHA384;
403 ds->ds.digest_size = sizeof(ds_fprint);
404 ds->ds.digest = memdup(ds_fprint, ds->ds.digest_size);
405 assert_se(ds->ds.digest);
406
407 log_info("DS: %s", strna(dns_resource_record_to_string(ds)));
408
409 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.net");
410 assert_se(a);
411
412 a->a.in_addr.s_addr = inet_addr("192.0.2.1");
413
414 log_info("A: %s", strna(dns_resource_record_to_string(a)));
415
416 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.net.");
417 assert_se(rrsig);
418
419 rrsig->rrsig.type_covered = DNS_TYPE_A;
420 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_ECDSAP384SHA384;
421 rrsig->rrsig.labels = 3;
422 rrsig->rrsig.expiration = 1284027625;
423 rrsig->rrsig.inception = 1281608425;
424 rrsig->rrsig.key_tag = 10771;
425 rrsig->rrsig.original_ttl = 3600;
426 rrsig->rrsig.signer = strdup("example.net.");
427 assert_se(rrsig->rrsig.signer);
428 rrsig->rrsig.signature_size = sizeof(signature_blob);
429 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
430 assert_se(rrsig->rrsig.signature);
431
432 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
433
434 assert_se(dnssec_key_match_rrsig(a->key, rrsig) > 0);
435 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
436
437 answer = dns_answer_new(1);
438 assert_se(answer);
439 assert_se(dns_answer_add(answer, a, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
440
441 assert_se(dnssec_verify_rrset(answer, a->key, rrsig, dnskey,
442 rrsig->rrsig.inception * USEC_PER_SEC, &result) >= 0);
443 assert_se(result == DNSSEC_VALIDATED);
444 }
445
446 TEST(dnssec_verify_rrset) {
447 static const uint8_t signature_blob[] = {
448 0x7f, 0x79, 0xdd, 0x5e, 0x89, 0x79, 0x18, 0xd0, 0x34, 0x86, 0x8c, 0x72, 0x77, 0x75, 0x48, 0x4d,
449 0xc3, 0x7d, 0x38, 0x04, 0xab, 0xcd, 0x9e, 0x4c, 0x82, 0xb0, 0x92, 0xca, 0xe9, 0x66, 0xe9, 0x6e,
450 0x47, 0xc7, 0x68, 0x8c, 0x94, 0xf6, 0x69, 0xcb, 0x75, 0x94, 0xe6, 0x30, 0xa6, 0xfb, 0x68, 0x64,
451 0x96, 0x1a, 0x84, 0xe1, 0xdc, 0x16, 0x4c, 0x83, 0x6c, 0x44, 0xf2, 0x74, 0x4d, 0x74, 0x79, 0x8f,
452 0xf3, 0xf4, 0x63, 0x0d, 0xef, 0x5a, 0xe7, 0xe2, 0xfd, 0xf2, 0x2b, 0x38, 0x7c, 0x28, 0x96, 0x9d,
453 0xb6, 0xcd, 0x5c, 0x3b, 0x57, 0xe2, 0x24, 0x78, 0x65, 0xd0, 0x9e, 0x77, 0x83, 0x09, 0x6c, 0xff,
454 0x3d, 0x52, 0x3f, 0x6e, 0xd1, 0xed, 0x2e, 0xf9, 0xee, 0x8e, 0xa6, 0xbe, 0x9a, 0xa8, 0x87, 0x76,
455 0xd8, 0x77, 0xcc, 0x96, 0xa0, 0x98, 0xa1, 0xd1, 0x68, 0x09, 0x43, 0xcf, 0x56, 0xd9, 0xd1, 0x66,
456 };
457
458 static const uint8_t dnskey_blob[] = {
459 0x03, 0x01, 0x00, 0x01, 0x9b, 0x49, 0x9b, 0xc1, 0xf9, 0x9a, 0xe0, 0x4e, 0xcf, 0xcb, 0x14, 0x45,
460 0x2e, 0xc9, 0xf9, 0x74, 0xa7, 0x18, 0xb5, 0xf3, 0xde, 0x39, 0x49, 0xdf, 0x63, 0x33, 0x97, 0x52,
461 0xe0, 0x8e, 0xac, 0x50, 0x30, 0x8e, 0x09, 0xd5, 0x24, 0x3d, 0x26, 0xa4, 0x49, 0x37, 0x2b, 0xb0,
462 0x6b, 0x1b, 0xdf, 0xde, 0x85, 0x83, 0xcb, 0x22, 0x4e, 0x60, 0x0a, 0x91, 0x1a, 0x1f, 0xc5, 0x40,
463 0xb1, 0xc3, 0x15, 0xc1, 0x54, 0x77, 0x86, 0x65, 0x53, 0xec, 0x10, 0x90, 0x0c, 0x91, 0x00, 0x5e,
464 0x15, 0xdc, 0x08, 0x02, 0x4c, 0x8c, 0x0d, 0xc0, 0xac, 0x6e, 0xc4, 0x3e, 0x1b, 0x80, 0x19, 0xe4,
465 0xf7, 0x5f, 0x77, 0x51, 0x06, 0x87, 0x61, 0xde, 0xa2, 0x18, 0x0f, 0x40, 0x8b, 0x79, 0x72, 0xfa,
466 0x8d, 0x1a, 0x44, 0x47, 0x0d, 0x8e, 0x3a, 0x2d, 0xc7, 0x39, 0xbf, 0x56, 0x28, 0x97, 0xd9, 0x20,
467 0x4f, 0x00, 0x51, 0x3b,
468 };
469
470 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *rrsig = NULL, *dnskey = NULL;
471 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
472 DnssecResult result;
473
474 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "nAsA.gov");
475 assert_se(a);
476
477 a->a.in_addr.s_addr = inet_addr("52.0.14.116");
478
479 log_info("A: %s", strna(dns_resource_record_to_string(a)));
480
481 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "NaSa.GOV.");
482 assert_se(rrsig);
483
484 rrsig->rrsig.type_covered = DNS_TYPE_A;
485 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_RSASHA256;
486 rrsig->rrsig.labels = 2;
487 rrsig->rrsig.original_ttl = 600;
488 rrsig->rrsig.expiration = 0x5683135c;
489 rrsig->rrsig.inception = 0x565b7da8;
490 rrsig->rrsig.key_tag = 63876;
491 rrsig->rrsig.signer = strdup("Nasa.Gov.");
492 assert_se(rrsig->rrsig.signer);
493 rrsig->rrsig.signature_size = sizeof(signature_blob);
494 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
495 assert_se(rrsig->rrsig.signature);
496
497 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
498
499 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "nASA.gOV");
500 assert_se(dnskey);
501
502 dnskey->dnskey.flags = 256;
503 dnskey->dnskey.protocol = 3;
504 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_RSASHA256;
505 dnskey->dnskey.key_size = sizeof(dnskey_blob);
506 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
507 assert_se(dnskey->dnskey.key);
508
509 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
510 log_info("DNSKEY keytag: %u", dnssec_keytag(dnskey, false));
511
512 assert_se(dnssec_key_match_rrsig(a->key, rrsig) > 0);
513 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
514
515 answer = dns_answer_new(1);
516 assert_se(answer);
517 assert_se(dns_answer_add(answer, a, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
518
519 /* Validate the RR as it if was 2015-12-2 today */
520 assert_se(dnssec_verify_rrset(answer, a->key, rrsig, dnskey, 1449092754*USEC_PER_SEC, &result) >= 0);
521 assert_se(result == DNSSEC_VALIDATED);
522 }
523
524 TEST(dnssec_verify_rrset_invalid_rsa_dnskey) {
525 static const uint8_t signature_blob[] = { 0x00 };
526 static const uint8_t dnskey_blob[] = { 0x00 };
527
528 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *rrsig = NULL, *dnskey = NULL;
529 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
530 DnssecResult result;
531
532 ASSERT_NOT_NULL(a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com."));
533
534 a->a.in_addr.s_addr = inet_addr("192.0.2.1");
535
536 ASSERT_NOT_NULL(dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "example.com."));
537
538 dnskey->dnskey.flags = DNSKEY_FLAG_ZONE_KEY;
539 dnskey->dnskey.protocol = 3;
540 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_RSASHA256;
541 dnskey->dnskey.key_size = sizeof(dnskey_blob);
542 ASSERT_NOT_NULL(dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob)));
543
544 ASSERT_NOT_NULL(rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "example.com."));
545
546 rrsig->rrsig.type_covered = DNS_TYPE_A;
547 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_RSASHA256;
548 rrsig->rrsig.labels = 2;
549 rrsig->rrsig.original_ttl = 3600;
550 rrsig->rrsig.expiration = 2000000000;
551 rrsig->rrsig.inception = 1000000000;
552 rrsig->rrsig.key_tag = dnssec_keytag(dnskey, false);
553 ASSERT_NOT_NULL(rrsig->rrsig.signer = strdup("example.com."));
554 rrsig->rrsig.signature_size = sizeof(signature_blob);
555 ASSERT_NOT_NULL(rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size));
556
557 ASSERT_GT(dnssec_key_match_rrsig(a->key, rrsig), 0);
558 ASSERT_GT(dnssec_rrsig_match_dnskey(rrsig, dnskey, false), 0);
559
560 ASSERT_NOT_NULL(answer = dns_answer_new(1));
561 ASSERT_OK(dns_answer_add(answer, a, 0, DNS_ANSWER_AUTHENTICATED, NULL));
562
563 ASSERT_ERROR(dnssec_verify_rrset(answer, a->key, rrsig, dnskey,
564 rrsig->rrsig.inception * USEC_PER_SEC, &result),
565 EINVAL);
566
567 dnskey->dnskey.key = mfree(dnskey->dnskey.key);
568 dnskey->dnskey.key_size = 0;
569 rrsig->rrsig.key_tag = dnssec_keytag(dnskey, false);
570
571 ASSERT_GT(dnssec_rrsig_match_dnskey(rrsig, dnskey, false), 0);
572 ASSERT_ERROR(dnssec_verify_rrset(answer, a->key, rrsig, dnskey,
573 rrsig->rrsig.inception * USEC_PER_SEC, &result),
574 EINVAL);
575 }
576
577 TEST(dnssec_verify_rrset2) {
578 static const uint8_t signature_blob[] = {
579 0x48, 0x45, 0xc8, 0x8b, 0xc0, 0x14, 0x92, 0xf5, 0x15, 0xc6, 0x84, 0x9d, 0x2f, 0xe3, 0x32, 0x11,
580 0x7d, 0xf1, 0xe6, 0x87, 0xb9, 0x42, 0xd3, 0x8b, 0x9e, 0xaf, 0x92, 0x31, 0x0a, 0x53, 0xad, 0x8b,
581 0xa7, 0x5c, 0x83, 0x39, 0x8c, 0x28, 0xac, 0xce, 0x6e, 0x9c, 0x18, 0xe3, 0x31, 0x16, 0x6e, 0xca,
582 0x38, 0x31, 0xaf, 0xd9, 0x94, 0xf1, 0x84, 0xb1, 0xdf, 0x5a, 0xc2, 0x73, 0x22, 0xf6, 0xcb, 0xa2,
583 0xe7, 0x8c, 0x77, 0x0c, 0x74, 0x2f, 0xc2, 0x13, 0xb0, 0x93, 0x51, 0xa9, 0x4f, 0xae, 0x0a, 0xda,
584 0x45, 0xcc, 0xfd, 0x43, 0x99, 0x36, 0x9a, 0x0d, 0x21, 0xe0, 0xeb, 0x30, 0x65, 0xd4, 0xa0, 0x27,
585 0x37, 0x3b, 0xe4, 0xc1, 0xc5, 0xa1, 0x2a, 0xd1, 0x76, 0xc4, 0x7e, 0x64, 0x0e, 0x5a, 0xa6, 0x50,
586 0x24, 0xd5, 0x2c, 0xcc, 0x6d, 0xe5, 0x37, 0xea, 0xbd, 0x09, 0x34, 0xed, 0x24, 0x06, 0xa1, 0x22,
587 };
588
589 static const uint8_t dnskey_blob[] = {
590 0x03, 0x01, 0x00, 0x01, 0xc3, 0x7f, 0x1d, 0xd1, 0x1c, 0x97, 0xb1, 0x13, 0x34, 0x3a, 0x9a, 0xea,
591 0xee, 0xd9, 0x5a, 0x11, 0x1b, 0x17, 0xc7, 0xe3, 0xd4, 0xda, 0x20, 0xbc, 0x5d, 0xba, 0x74, 0xe3,
592 0x37, 0x99, 0xec, 0x25, 0xce, 0x93, 0x7f, 0xbd, 0x22, 0x73, 0x7e, 0x14, 0x71, 0xe0, 0x60, 0x07,
593 0xd4, 0x39, 0x8b, 0x5e, 0xe9, 0xba, 0x25, 0xe8, 0x49, 0xe9, 0x34, 0xef, 0xfe, 0x04, 0x5c, 0xa5,
594 0x27, 0xcd, 0xa9, 0xda, 0x70, 0x05, 0x21, 0xab, 0x15, 0x82, 0x24, 0xc3, 0x94, 0xf5, 0xd7, 0xb7,
595 0xc4, 0x66, 0xcb, 0x32, 0x6e, 0x60, 0x2b, 0x55, 0x59, 0x28, 0x89, 0x8a, 0x72, 0xde, 0x88, 0x56,
596 0x27, 0x95, 0xd9, 0xac, 0x88, 0x4f, 0x65, 0x2b, 0x68, 0xfc, 0xe6, 0x41, 0xc1, 0x1b, 0xef, 0x4e,
597 0xd6, 0xc2, 0x0f, 0x64, 0x88, 0x95, 0x5e, 0xdd, 0x3a, 0x02, 0x07, 0x50, 0xa9, 0xda, 0xa4, 0x49,
598 0x74, 0x62, 0xfe, 0xd7,
599 };
600
601 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *nsec = NULL, *rrsig = NULL, *dnskey = NULL;
602 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
603 DnssecResult result;
604
605 nsec = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NSEC, "nasa.gov");
606 assert_se(nsec);
607
608 nsec->nsec.next_domain_name = strdup("3D-Printing.nasa.gov");
609 assert_se(nsec->nsec.next_domain_name);
610
611 nsec->nsec.types = bitmap_new();
612 assert_se(nsec->nsec.types);
613 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_A) >= 0);
614 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_NS) >= 0);
615 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_SOA) >= 0);
616 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_MX) >= 0);
617 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_TXT) >= 0);
618 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_RRSIG) >= 0);
619 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_NSEC) >= 0);
620 assert_se(bitmap_set(nsec->nsec.types, DNS_TYPE_DNSKEY) >= 0);
621 assert_se(bitmap_set(nsec->nsec.types, 65534) >= 0);
622
623 log_info("NSEC: %s", strna(dns_resource_record_to_string(nsec)));
624
625 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "NaSa.GOV.");
626 assert_se(rrsig);
627
628 rrsig->rrsig.type_covered = DNS_TYPE_NSEC;
629 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_RSASHA256;
630 rrsig->rrsig.labels = 2;
631 rrsig->rrsig.original_ttl = 300;
632 rrsig->rrsig.expiration = 0x5689002f;
633 rrsig->rrsig.inception = 0x56617230;
634 rrsig->rrsig.key_tag = 30390;
635 rrsig->rrsig.signer = strdup("Nasa.Gov.");
636 assert_se(rrsig->rrsig.signer);
637 rrsig->rrsig.signature_size = sizeof(signature_blob);
638 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
639 assert_se(rrsig->rrsig.signature);
640
641 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
642
643 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "nASA.gOV");
644 assert_se(dnskey);
645
646 dnskey->dnskey.flags = 256;
647 dnskey->dnskey.protocol = 3;
648 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_RSASHA256;
649 dnskey->dnskey.key_size = sizeof(dnskey_blob);
650 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
651 assert_se(dnskey->dnskey.key);
652
653 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
654 log_info("DNSKEY keytag: %u", dnssec_keytag(dnskey, false));
655
656 assert_se(dnssec_key_match_rrsig(nsec->key, rrsig) > 0);
657 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
658
659 answer = dns_answer_new(1);
660 assert_se(answer);
661 assert_se(dns_answer_add(answer, nsec, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
662
663 /* Validate the RR as it if was 2015-12-11 today */
664 assert_se(dnssec_verify_rrset(answer, nsec->key, rrsig, dnskey, 1449849318*USEC_PER_SEC, &result) >= 0);
665 assert_se(result == DNSSEC_VALIDATED);
666 }
667
668 TEST(dnssec_verify_rrset3) {
669 static const uint8_t signature_blob[] = {
670 0x41, 0x09, 0x08, 0x67, 0x51, 0x6d, 0x02, 0xf2, 0x17, 0x1e, 0x61, 0x03, 0xc6, 0x80, 0x7a, 0x82,
671 0x8f, 0x6c, 0x8c, 0x4c, 0x68, 0x6f, 0x1c, 0xaa, 0x4a, 0xe0, 0x9b, 0x72, 0xdf, 0x7f, 0x15, 0xfa,
672 0x2b, 0xc5, 0x63, 0x6f, 0x52, 0xa2, 0x60, 0x59, 0x24, 0xb6, 0xc3, 0x43, 0x3d, 0x47, 0x38, 0xd8,
673 0x0c, 0xcc, 0x6c, 0x10, 0x49, 0x92, 0x97, 0x6c, 0x7d, 0x32, 0xc2, 0x62, 0x83, 0x34, 0x96, 0xdf,
674 0xbd, 0xf9, 0xcc, 0xcf, 0xd9, 0x4d, 0x8b, 0x8a, 0xa9, 0x3c, 0x1f, 0x89, 0xc4, 0xad, 0xd5, 0xbb,
675 0x74, 0xf8, 0xee, 0x60, 0x54, 0x7a, 0xec, 0x36, 0x45, 0xf2, 0xec, 0xb9, 0x73, 0x66, 0xae, 0x57,
676 0x2d, 0xd4, 0x91, 0x02, 0x99, 0xcd, 0xba, 0xbd, 0x6e, 0xfb, 0xa6, 0xf6, 0x34, 0xce, 0x4c, 0x44,
677 0x0b, 0xd2, 0x66, 0xdb, 0x4e, 0x5e, 0x00, 0x72, 0x1b, 0xe5, 0x2f, 0x24, 0xd2, 0xc8, 0x72, 0x37,
678 0x97, 0x2b, 0xd0, 0xcd, 0xa9, 0x6b, 0x84, 0x32, 0x56, 0x7a, 0x89, 0x6e, 0x3d, 0x8f, 0x03, 0x9a,
679 0x9d, 0x6d, 0xf7, 0xe5, 0x13, 0xd7, 0x4b, 0xbc, 0xe2, 0x6c, 0xd1, 0x18, 0x60, 0x0e, 0x1a, 0xe3,
680 0xf9, 0xc0, 0x34, 0x4b, 0x1c, 0x82, 0x17, 0x5e, 0xdf, 0x81, 0x32, 0xd7, 0x5b, 0x30, 0x1d, 0xe0,
681 0x29, 0x80, 0x6b, 0xb1, 0x69, 0xbf, 0x3f, 0x12, 0x56, 0xb0, 0x80, 0x91, 0x22, 0x1a, 0x31, 0xd5,
682 0x5d, 0x3d, 0xdd, 0x70, 0x5e, 0xcb, 0xc7, 0x2d, 0xb8, 0x3e, 0x54, 0x34, 0xd3, 0x50, 0x89, 0x77,
683 0x08, 0xc1, 0xf7, 0x11, 0x6e, 0x57, 0xd7, 0x09, 0x94, 0x20, 0x03, 0x38, 0xc3, 0x3a, 0xd3, 0x93,
684 0x8f, 0xd0, 0x65, 0xc5, 0xa1, 0xe0, 0x69, 0x2c, 0xf6, 0x0a, 0xce, 0x01, 0xb6, 0x0d, 0x95, 0xa0,
685 0x5d, 0x97, 0x94, 0xc3, 0xf1, 0xcd, 0x49, 0xea, 0x20, 0xd3, 0xa9, 0xa6, 0x67, 0x94, 0x64, 0x17
686 };
687
688 static const uint8_t dnskey_blob[] = {
689 0x03, 0x01, 0x00, 0x01, 0xbf, 0xdd, 0x24, 0x95, 0x21, 0x70, 0xa8, 0x5b, 0x19, 0xa6, 0x76, 0xd3,
690 0x5b, 0x37, 0xcf, 0x59, 0x0d, 0x3c, 0xdb, 0x0c, 0xcf, 0xd6, 0x19, 0x02, 0xc7, 0x8e, 0x56, 0x4d,
691 0x14, 0xb7, 0x9d, 0x71, 0xf4, 0xdd, 0x24, 0x36, 0xc8, 0x32, 0x1c, 0x63, 0xf7, 0xc0, 0xfc, 0xe3,
692 0x83, 0xa6, 0x22, 0x8b, 0x6a, 0x34, 0x41, 0x72, 0xaa, 0x95, 0x98, 0x06, 0xac, 0x03, 0xec, 0xc3,
693 0xa1, 0x6d, 0x8b, 0x1b, 0xfd, 0xa4, 0x05, 0x72, 0xe6, 0xe0, 0xb9, 0x98, 0x07, 0x54, 0x7a, 0xb2,
694 0x55, 0x30, 0x96, 0xa3, 0x22, 0x3b, 0xe0, 0x9d, 0x61, 0xf6, 0xdc, 0x31, 0x2b, 0xc9, 0x2c, 0x12,
695 0x06, 0x7f, 0x3c, 0x5d, 0x29, 0x76, 0x01, 0x62, 0xe3, 0x41, 0x41, 0x4f, 0xa6, 0x07, 0xfa, 0x2d,
696 0x0c, 0x64, 0x88, 0xd1, 0x56, 0x18, 0x4b, 0x2b, 0xc2, 0x19, 0x7e, 0xd0, 0x1a, 0x8c, 0x2d, 0x8d,
697 0x06, 0xdf, 0x4d, 0xaf, 0xd9, 0xe3, 0x31, 0x59, 0xbc, 0xc3, 0x36, 0x22, 0xe7, 0x15, 0xf9, 0xb2,
698 0x44, 0x8a, 0x33, 0xd7, 0x6c, 0xf1, 0xcc, 0x37, 0x05, 0x69, 0x32, 0x71, 0x76, 0xd8, 0x50, 0x06,
699 0xae, 0x27, 0xed, 0x3b, 0xdb, 0x1a, 0x97, 0x9b, 0xa3, 0x3e, 0x40, 0x42, 0x29, 0xaf, 0x75, 0x1c,
700 0xff, 0x1d, 0xaf, 0x85, 0x02, 0xb3, 0x2e, 0x99, 0x67, 0x08, 0x13, 0xd5, 0xda, 0x6d, 0x65, 0xb2,
701 0x36, 0x6f, 0x2f, 0x64, 0xe0, 0xfa, 0xd3, 0x81, 0x86, 0x6b, 0x41, 0x3e, 0x91, 0xaa, 0x0a, 0xd3,
702 0xb2, 0x92, 0xd9, 0x42, 0x36, 0x8a, 0x11, 0x0b, 0x5b, 0xb0, 0xea, 0xad, 0x76, 0xd5, 0xb4, 0x81,
703 0x30, 0xca, 0x5c, 0x4f, 0xd9, 0xea, 0xe7, 0x4b, 0x10, 0x0a, 0x09, 0x4b, 0x73, 0x66, 0xed, 0x8e,
704 0x84, 0xa2, 0x4f, 0x93, 0x7e, 0x29, 0xdc, 0x6a, 0xbd, 0x12, 0xa1, 0x3d, 0xd2, 0xd6, 0x2a, 0x67,
705 0x99, 0x4d, 0xf3, 0x43
706 };
707
708 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *mx1 = NULL, *mx2 = NULL, *mx3 = NULL, *mx4 = NULL, *rrsig = NULL, *dnskey = NULL;
709 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
710 DnssecResult result;
711
712 mx1 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "kodapan.se");
713 assert_se(mx1);
714
715 mx1->mx.priority = 1;
716 mx1->mx.exchange = strdup("ASPMX.L.GOOGLE.COM");
717 assert_se(mx1->mx.exchange);
718
719 log_info("MX: %s", strna(dns_resource_record_to_string(mx1)));
720
721 mx2 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "kodapan.se");
722 assert_se(mx2);
723
724 mx2->mx.priority = 5;
725 mx2->mx.exchange = strdup("ALT2.ASPMX.L.GOOGLE.COM");
726 assert_se(mx2->mx.exchange);
727
728 log_info("MX: %s", strna(dns_resource_record_to_string(mx2)));
729
730 mx3 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "kodapan.se");
731 assert_se(mx3);
732
733 mx3->mx.priority = 10;
734 mx3->mx.exchange = strdup("ASPMX2.GOOGLEMAIL.COM");
735 assert_se(mx3->mx.exchange);
736
737 log_info("MX: %s", strna(dns_resource_record_to_string(mx3)));
738
739 mx4 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "kodapan.se");
740 assert_se(mx4);
741
742 mx4->mx.priority = 10;
743 mx4->mx.exchange = strdup("ASPMX3.GOOGLEMAIL.COM");
744 assert_se(mx4->mx.exchange);
745
746 log_info("MX: %s", strna(dns_resource_record_to_string(mx4)));
747
748 rrsig = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "kodapan.se");
749 assert_se(rrsig);
750
751 rrsig->rrsig.type_covered = DNS_TYPE_MX;
752 rrsig->rrsig.algorithm = DNSSEC_ALGORITHM_RSASHA256;
753 rrsig->rrsig.labels = 2;
754 rrsig->rrsig.original_ttl = 900;
755 rrsig->rrsig.expiration = 0x5e608a84;
756 rrsig->rrsig.inception = 0x5e4e1584;
757 rrsig->rrsig.key_tag = 44028;
758 rrsig->rrsig.signer = strdup("kodapan.se.");
759 assert_se(rrsig->rrsig.signer);
760 rrsig->rrsig.signature_size = sizeof(signature_blob);
761 rrsig->rrsig.signature = memdup(signature_blob, rrsig->rrsig.signature_size);
762 assert_se(rrsig->rrsig.signature);
763
764 log_info("RRSIG: %s", strna(dns_resource_record_to_string(rrsig)));
765
766 dnskey = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, "kodapan.se");
767 assert_se(dnskey);
768
769 dnskey->dnskey.flags = 256;
770 dnskey->dnskey.protocol = 3;
771 dnskey->dnskey.algorithm = DNSSEC_ALGORITHM_RSASHA256;
772 dnskey->dnskey.key_size = sizeof(dnskey_blob);
773 dnskey->dnskey.key = memdup(dnskey_blob, sizeof(dnskey_blob));
774 assert_se(dnskey->dnskey.key);
775
776 log_info("DNSKEY: %s", strna(dns_resource_record_to_string(dnskey)));
777 log_info("DNSKEY keytag: %u", dnssec_keytag(dnskey, false));
778
779 assert_se(dnssec_key_match_rrsig(mx1->key, rrsig) > 0);
780 assert_se(dnssec_key_match_rrsig(mx2->key, rrsig) > 0);
781 assert_se(dnssec_key_match_rrsig(mx3->key, rrsig) > 0);
782 assert_se(dnssec_key_match_rrsig(mx4->key, rrsig) > 0);
783 assert_se(dnssec_rrsig_match_dnskey(rrsig, dnskey, false) > 0);
784
785 answer = dns_answer_new(4);
786 assert_se(answer);
787 assert_se(dns_answer_add(answer, mx1, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
788 assert_se(dns_answer_add(answer, mx2, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
789 assert_se(dns_answer_add(answer, mx3, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
790 assert_se(dns_answer_add(answer, mx4, 0, DNS_ANSWER_AUTHENTICATED, NULL) >= 0);
791
792 /* Validate the RR as it if was 2020-02-24 today */
793 assert_se(dnssec_verify_rrset(answer, mx1->key, rrsig, dnskey, 1582534685*USEC_PER_SEC, &result) >= 0);
794 assert_se(result == DNSSEC_VALIDATED);
795 }
796
797 TEST(dnssec_nsec3_hash) {
798 static const uint8_t salt[] = { 0xB0, 0x1D, 0xFA, 0xCE };
799 static const uint8_t next_hashed_name[] = { 0x84, 0x10, 0x26, 0x53, 0xc9, 0xfa, 0x4d, 0x85, 0x6c, 0x97, 0x82, 0xe2, 0x8f, 0xdf, 0x2d, 0x5e, 0x87, 0x69, 0xc4, 0x52 };
800 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
801 uint8_t h[DNSSEC_HASH_SIZE_MAX];
802 _cleanup_free_ char *b = NULL;
803 int k;
804
805 /* The NSEC3 RR for eurid.eu on 2015-12-14. */
806 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NSEC3, "PJ8S08RR45VIQDAQGE7EN3VHKNROTBMM.eurid.eu.");
807 assert_se(rr);
808
809 rr->nsec3.algorithm = DNSSEC_DIGEST_SHA1;
810 rr->nsec3.flags = 1;
811 rr->nsec3.iterations = 1;
812 rr->nsec3.salt = memdup(salt, sizeof(salt));
813 assert_se(rr->nsec3.salt);
814 rr->nsec3.salt_size = sizeof(salt);
815 rr->nsec3.next_hashed_name = memdup(next_hashed_name, sizeof(next_hashed_name));
816 assert_se(rr->nsec3.next_hashed_name);
817 rr->nsec3.next_hashed_name_size = sizeof(next_hashed_name);
818
819 log_info("NSEC3: %s", strna(dns_resource_record_to_string(rr)));
820
821 k = dnssec_nsec3_hash(rr, "eurid.eu", &h);
822 assert_se(k >= 0);
823
824 b = base32hexmem(h, k, false);
825 assert_se(b);
826 assert_se(strcasecmp(b, "PJ8S08RR45VIQDAQGE7EN3VHKNROTBMM") == 0);
827 }
828
829 DEFINE_TEST_MAIN(LOG_INFO);