| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "bitmap.h" |
| 5 |
#include "crypto-util.h" |
| 6 |
#include "dns-answer.h" |
| 7 |
#include "dns-domain.h" |
| 8 |
#include "dns-rr.h" |
| 9 |
#include "dns-type.h" |
| 10 |
#include "hexdecoct.h" |
| 11 |
#include "log.h" |
| 12 |
#include "memory-util.h" |
| 13 |
#include "memstream-util.h" |
| 14 |
#include "resolved-dns-dnssec.h" |
| 15 |
#include "resolved-util.h" |
| 16 |
#include "sort-util.h" |
| 17 |
#include "string-table.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "time-util.h" |
| 20 |
|
| 21 |
|
| 22 |
#define VERIFY_RRS_MAX 256 |
| 23 |
#define MAX_KEY_SIZE (32*1024) |
| 24 |
|
| 25 |
/* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */ |
| 26 |
#define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE) |
| 27 |
|
| 28 |
/* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value, but |
| 29 |
* RFC9276 § 3.2 says that we should reduce the acceptable iteration count */ |
| 30 |
#define NSEC3_ITERATIONS_MAX 100 |
| 31 |
|
| 32 |
/* |
| 33 |
* The DNSSEC Chain of trust: |
| 34 |
* |
| 35 |
* Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone |
| 36 |
* DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree |
| 37 |
* DS RRs are protected like normal RRs |
| 38 |
* |
| 39 |
* Example chain: |
| 40 |
* Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS |
| 41 |
*/ |
| 42 |
|
| 43 |
#if HAVE_OPENSSL |
| 44 |
|
| 45 |
static int rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) { |
| 46 |
const DnsResourceRecord *x = *ASSERT_PTR(a), *y = *ASSERT_PTR(b); |
| 47 |
size_t m; |
| 48 |
int r; |
| 49 |
|
| 50 |
/* Let's order the RRs according to RFC 4034, Section 6.3 */ |
| 51 |
|
| 52 |
assert(x); |
| 53 |
assert(x->wire_format); |
| 54 |
assert(y); |
| 55 |
assert(y->wire_format); |
| 56 |
|
| 57 |
m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y)); |
| 58 |
|
| 59 |
r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m); |
| 60 |
if (r != 0) |
| 61 |
return r; |
| 62 |
|
| 63 |
return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y)); |
| 64 |
} |
| 65 |
|
| 66 |
static int dnssec_rsa_verify_raw( |
| 67 |
const EVP_MD *hash_algorithm, |
| 68 |
const void *signature, size_t signature_size, |
| 69 |
const void *data, size_t data_size, |
| 70 |
const void *exponent, size_t exponent_size, |
| 71 |
const void *modulus, size_t modulus_size) { |
| 72 |
int r; |
| 73 |
|
| 74 |
DISABLE_WARNING_DEPRECATED_DECLARATIONS; |
| 75 |
_cleanup_(RSA_freep) RSA *rpubkey = NULL; |
| 76 |
_cleanup_(EVP_PKEY_freep) EVP_PKEY *epubkey = NULL; |
| 77 |
_cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *ctx = NULL; |
| 78 |
_cleanup_(BN_freep) BIGNUM *e = NULL, *m = NULL; |
| 79 |
|
| 80 |
assert(hash_algorithm); |
| 81 |
|
| 82 |
e = sym_BN_bin2bn(exponent, exponent_size, NULL); |
| 83 |
if (!e) |
| 84 |
return -EIO; |
| 85 |
|
| 86 |
m = sym_BN_bin2bn(modulus, modulus_size, NULL); |
| 87 |
if (!m) |
| 88 |
return -EIO; |
| 89 |
|
| 90 |
rpubkey = sym_RSA_new(); |
| 91 |
if (!rpubkey) |
| 92 |
return -ENOMEM; |
| 93 |
|
| 94 |
if (sym_RSA_set0_key(rpubkey, m, e, NULL) <= 0) |
| 95 |
return -EIO; |
| 96 |
e = m = NULL; |
| 97 |
|
| 98 |
if ((size_t) sym_RSA_size(rpubkey) != signature_size) |
| 99 |
return -EINVAL; |
| 100 |
|
| 101 |
epubkey = sym_EVP_PKEY_new(); |
| 102 |
if (!epubkey) |
| 103 |
return -ENOMEM; |
| 104 |
|
| 105 |
if (sym_EVP_PKEY_assign_RSA(epubkey, sym_RSAPublicKey_dup(rpubkey)) <= 0) |
| 106 |
return -EIO; |
| 107 |
|
| 108 |
ctx = sym_EVP_PKEY_CTX_new(epubkey, NULL); |
| 109 |
if (!ctx) |
| 110 |
return -ENOMEM; |
| 111 |
|
| 112 |
if (sym_EVP_PKEY_verify_init(ctx) <= 0) |
| 113 |
return -EIO; |
| 114 |
|
| 115 |
if (sym_EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) |
| 116 |
return -EIO; |
| 117 |
|
| 118 |
if (sym_EVP_PKEY_CTX_set_signature_md(ctx, hash_algorithm) <= 0) |
| 119 |
return -EIO; |
| 120 |
|
| 121 |
r = sym_EVP_PKEY_verify(ctx, signature, signature_size, data, data_size); |
| 122 |
if (r < 0) |
| 123 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 124 |
"Signature verification failed: 0x%lx", sym_ERR_get_error()); |
| 125 |
|
| 126 |
REENABLE_WARNING; |
| 127 |
return r; |
| 128 |
} |
| 129 |
|
| 130 |
static int dnssec_rsa_verify( |
| 131 |
const EVP_MD *hash_algorithm, |
| 132 |
const void *hash, size_t hash_size, |
| 133 |
DnsResourceRecord *rrsig, |
| 134 |
DnsResourceRecord *dnskey) { |
| 135 |
|
| 136 |
size_t exponent_size, modulus_size; |
| 137 |
void *exponent, *modulus; |
| 138 |
|
| 139 |
assert(hash_algorithm); |
| 140 |
assert(hash); |
| 141 |
assert(hash_size > 0); |
| 142 |
assert(rrsig); |
| 143 |
assert(dnskey); |
| 144 |
|
| 145 |
if (dnskey->dnskey.key_size < 1) |
| 146 |
return -EINVAL; |
| 147 |
|
| 148 |
if (*(uint8_t*) dnskey->dnskey.key == 0) { |
| 149 |
/* exponent is > 255 bytes long */ |
| 150 |
|
| 151 |
if (dnskey->dnskey.key_size < 3) |
| 152 |
return -EINVAL; |
| 153 |
|
| 154 |
exponent_size = |
| 155 |
((size_t) (((uint8_t*) dnskey->dnskey.key)[1]) << 8) | |
| 156 |
((size_t) ((uint8_t*) dnskey->dnskey.key)[2]); |
| 157 |
|
| 158 |
if (exponent_size < 256) |
| 159 |
return -EINVAL; |
| 160 |
|
| 161 |
if (3 + exponent_size >= dnskey->dnskey.key_size) |
| 162 |
return -EINVAL; |
| 163 |
|
| 164 |
exponent = (uint8_t*) dnskey->dnskey.key + 3; |
| 165 |
modulus = (uint8_t*) dnskey->dnskey.key + 3 + exponent_size; |
| 166 |
modulus_size = dnskey->dnskey.key_size - 3 - exponent_size; |
| 167 |
|
| 168 |
} else { |
| 169 |
/* exponent is <= 255 bytes long */ |
| 170 |
exponent_size = (size_t) ((uint8_t*) dnskey->dnskey.key)[0]; |
| 171 |
|
| 172 |
if (exponent_size <= 0) |
| 173 |
return -EINVAL; |
| 174 |
|
| 175 |
if (1 + exponent_size >= dnskey->dnskey.key_size) |
| 176 |
return -EINVAL; |
| 177 |
|
| 178 |
exponent = (uint8_t*) dnskey->dnskey.key + 1; |
| 179 |
modulus = (uint8_t*) dnskey->dnskey.key + 1 + exponent_size; |
| 180 |
modulus_size = dnskey->dnskey.key_size - 1 - exponent_size; |
| 181 |
} |
| 182 |
|
| 183 |
return dnssec_rsa_verify_raw( |
| 184 |
hash_algorithm, |
| 185 |
rrsig->rrsig.signature, rrsig->rrsig.signature_size, |
| 186 |
hash, hash_size, |
| 187 |
exponent, exponent_size, |
| 188 |
modulus, modulus_size); |
| 189 |
} |
| 190 |
|
| 191 |
static int dnssec_ecdsa_verify_raw( |
| 192 |
const EVP_MD *hash_algorithm, |
| 193 |
int curve, |
| 194 |
const void *signature_r, size_t signature_r_size, |
| 195 |
const void *signature_s, size_t signature_s_size, |
| 196 |
const void *data, size_t data_size, |
| 197 |
const void *key, size_t key_size) { |
| 198 |
int k; |
| 199 |
|
| 200 |
DISABLE_WARNING_DEPRECATED_DECLARATIONS; |
| 201 |
_cleanup_(EC_GROUP_freep) EC_GROUP *ec_group = NULL; |
| 202 |
_cleanup_(EC_POINT_freep) EC_POINT *p = NULL; |
| 203 |
_cleanup_(EC_KEY_freep) EC_KEY *eckey = NULL; |
| 204 |
_cleanup_(BN_CTX_freep) BN_CTX *bctx = NULL; |
| 205 |
_cleanup_(BN_freep) BIGNUM *r = NULL, *s = NULL; |
| 206 |
_cleanup_(ECDSA_SIG_freep) ECDSA_SIG *sig = NULL; |
| 207 |
|
| 208 |
assert(hash_algorithm); |
| 209 |
|
| 210 |
ec_group = sym_EC_GROUP_new_by_curve_name(curve); |
| 211 |
if (!ec_group) |
| 212 |
return -ENOMEM; |
| 213 |
|
| 214 |
p = sym_EC_POINT_new(ec_group); |
| 215 |
if (!p) |
| 216 |
return -ENOMEM; |
| 217 |
|
| 218 |
bctx = sym_BN_CTX_new(); |
| 219 |
if (!bctx) |
| 220 |
return -ENOMEM; |
| 221 |
|
| 222 |
if (sym_EC_POINT_oct2point(ec_group, p, key, key_size, bctx) <= 0) |
| 223 |
return -EIO; |
| 224 |
|
| 225 |
eckey = sym_EC_KEY_new(); |
| 226 |
if (!eckey) |
| 227 |
return -ENOMEM; |
| 228 |
|
| 229 |
if (sym_EC_KEY_set_group(eckey, ec_group) <= 0) |
| 230 |
return -EIO; |
| 231 |
|
| 232 |
if (sym_EC_KEY_set_public_key(eckey, p) <= 0) |
| 233 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 234 |
"EC_KEY_set_public_key failed: 0x%lx", sym_ERR_get_error()); |
| 235 |
|
| 236 |
if (sym_EC_KEY_check_key(eckey) != 1) |
| 237 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 238 |
"EC_KEY_check_key failed: 0x%lx", sym_ERR_get_error()); |
| 239 |
|
| 240 |
r = sym_BN_bin2bn(signature_r, signature_r_size, NULL); |
| 241 |
if (!r) |
| 242 |
return -EIO; |
| 243 |
|
| 244 |
s = sym_BN_bin2bn(signature_s, signature_s_size, NULL); |
| 245 |
if (!s) |
| 246 |
return -EIO; |
| 247 |
|
| 248 |
/* TODO: We should eventually use the EVP API once it supports ECDSA signature verification */ |
| 249 |
|
| 250 |
sig = sym_ECDSA_SIG_new(); |
| 251 |
if (!sig) |
| 252 |
return -ENOMEM; |
| 253 |
|
| 254 |
if (sym_ECDSA_SIG_set0(sig, r, s) <= 0) |
| 255 |
return -EIO; |
| 256 |
r = s = NULL; |
| 257 |
|
| 258 |
k = sym_ECDSA_do_verify(data, data_size, sig, eckey); |
| 259 |
if (k < 0) |
| 260 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 261 |
"Signature verification failed: 0x%lx", sym_ERR_get_error()); |
| 262 |
|
| 263 |
REENABLE_WARNING; |
| 264 |
return k; |
| 265 |
} |
| 266 |
|
| 267 |
static int dnssec_ecdsa_verify( |
| 268 |
const EVP_MD *hash_algorithm, |
| 269 |
int algorithm, |
| 270 |
const void *hash, size_t hash_size, |
| 271 |
DnsResourceRecord *rrsig, |
| 272 |
DnsResourceRecord *dnskey) { |
| 273 |
|
| 274 |
int curve; |
| 275 |
size_t key_size; |
| 276 |
uint8_t *q; |
| 277 |
|
| 278 |
assert(hash); |
| 279 |
assert(hash_size); |
| 280 |
assert(rrsig); |
| 281 |
assert(dnskey); |
| 282 |
|
| 283 |
if (algorithm == DNSSEC_ALGORITHM_ECDSAP256SHA256) { |
| 284 |
curve = NID_X9_62_prime256v1; /* NIST P-256 */ |
| 285 |
key_size = 32; |
| 286 |
} else if (algorithm == DNSSEC_ALGORITHM_ECDSAP384SHA384) { |
| 287 |
curve = NID_secp384r1; /* NIST P-384 */ |
| 288 |
key_size = 48; |
| 289 |
} else |
| 290 |
return -EOPNOTSUPP; |
| 291 |
|
| 292 |
if (dnskey->dnskey.key_size != key_size * 2) |
| 293 |
return -EINVAL; |
| 294 |
|
| 295 |
if (rrsig->rrsig.signature_size != key_size * 2) |
| 296 |
return -EINVAL; |
| 297 |
|
| 298 |
q = newa(uint8_t, key_size*2 + 1); |
| 299 |
q[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */ |
| 300 |
memcpy(q+1, dnskey->dnskey.key, key_size*2); |
| 301 |
|
| 302 |
return dnssec_ecdsa_verify_raw( |
| 303 |
hash_algorithm, |
| 304 |
curve, |
| 305 |
rrsig->rrsig.signature, key_size, |
| 306 |
(uint8_t*) rrsig->rrsig.signature + key_size, key_size, |
| 307 |
hash, hash_size, |
| 308 |
q, key_size*2+1); |
| 309 |
} |
| 310 |
|
| 311 |
static int dnssec_eddsa_verify_raw( |
| 312 |
int curve, |
| 313 |
const uint8_t *signature, size_t signature_size, |
| 314 |
const uint8_t *data, size_t data_size, |
| 315 |
const uint8_t *key, size_t key_size) { |
| 316 |
|
| 317 |
_cleanup_(EVP_PKEY_freep) EVP_PKEY *evkey = NULL; |
| 318 |
_cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *pctx = NULL; |
| 319 |
_cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = NULL; |
| 320 |
int r; |
| 321 |
|
| 322 |
assert(curve == NID_ED25519); |
| 323 |
assert(signature_size == key_size * 2); |
| 324 |
|
| 325 |
uint8_t *q = newa(uint8_t, signature_size + 1); |
| 326 |
q[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */ |
| 327 |
memcpy(q+1, signature, signature_size); |
| 328 |
|
| 329 |
evkey = sym_EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, key, key_size); |
| 330 |
if (!evkey) |
| 331 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 332 |
"EVP_PKEY_new_raw_public_key failed: 0x%lx", sym_ERR_get_error()); |
| 333 |
|
| 334 |
pctx = sym_EVP_PKEY_CTX_new(evkey, NULL); |
| 335 |
if (!pctx) |
| 336 |
return -ENOMEM; |
| 337 |
|
| 338 |
ctx = sym_EVP_MD_CTX_new(); |
| 339 |
if (!ctx) |
| 340 |
return -ENOMEM; |
| 341 |
|
| 342 |
/* This prevents EVP_DigestVerifyInit from managing pctx and complicating our free logic. */ |
| 343 |
sym_EVP_MD_CTX_set_pkey_ctx(ctx, pctx); |
| 344 |
|
| 345 |
/* One might be tempted to use EVP_PKEY_verify_init, but see Ed25519(7ssl). */ |
| 346 |
if (sym_EVP_DigestVerifyInit(ctx, &pctx, NULL, NULL, evkey) <= 0) |
| 347 |
return -EIO; |
| 348 |
|
| 349 |
r = sym_EVP_DigestVerify(ctx, signature, signature_size, data, data_size); |
| 350 |
if (r < 0) |
| 351 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 352 |
"Signature verification failed: 0x%lx", sym_ERR_get_error()); |
| 353 |
|
| 354 |
return r; |
| 355 |
} |
| 356 |
|
| 357 |
static int dnssec_eddsa_verify( |
| 358 |
int algorithm, |
| 359 |
const void *data, size_t data_size, |
| 360 |
DnsResourceRecord *rrsig, |
| 361 |
DnsResourceRecord *dnskey) { |
| 362 |
int curve; |
| 363 |
size_t key_size; |
| 364 |
|
| 365 |
if (algorithm == DNSSEC_ALGORITHM_ED25519) { |
| 366 |
curve = NID_ED25519; |
| 367 |
key_size = 32; |
| 368 |
} else |
| 369 |
return -EOPNOTSUPP; |
| 370 |
|
| 371 |
if (dnskey->dnskey.key_size != key_size) |
| 372 |
return -EINVAL; |
| 373 |
|
| 374 |
if (rrsig->rrsig.signature_size != key_size * 2) |
| 375 |
return -EINVAL; |
| 376 |
|
| 377 |
return dnssec_eddsa_verify_raw( |
| 378 |
curve, |
| 379 |
rrsig->rrsig.signature, rrsig->rrsig.signature_size, |
| 380 |
data, data_size, |
| 381 |
dnskey->dnskey.key, key_size); |
| 382 |
} |
| 383 |
|
| 384 |
static int md_add_uint8(EVP_MD_CTX *ctx, uint8_t v) { |
| 385 |
return sym_EVP_DigestUpdate(ctx, &v, sizeof(v)); |
| 386 |
} |
| 387 |
|
| 388 |
static int md_add_uint16(EVP_MD_CTX *ctx, uint16_t v) { |
| 389 |
v = htobe16(v); |
| 390 |
return sym_EVP_DigestUpdate(ctx, &v, sizeof(v)); |
| 391 |
} |
| 392 |
|
| 393 |
static void fwrite_uint8(FILE *fp, uint8_t v) { |
| 394 |
fwrite(&v, sizeof(v), 1, fp); |
| 395 |
} |
| 396 |
|
| 397 |
static void fwrite_uint16(FILE *fp, uint16_t v) { |
| 398 |
v = htobe16(v); |
| 399 |
fwrite(&v, sizeof(v), 1, fp); |
| 400 |
} |
| 401 |
|
| 402 |
static void fwrite_uint32(FILE *fp, uint32_t v) { |
| 403 |
v = htobe32(v); |
| 404 |
fwrite(&v, sizeof(v), 1, fp); |
| 405 |
} |
| 406 |
|
| 407 |
static int dnssec_rrsig_prepare(DnsResourceRecord *rrsig) { |
| 408 |
int n_key_labels, n_signer_labels; |
| 409 |
const char *name; |
| 410 |
int r; |
| 411 |
|
| 412 |
/* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source |
| 413 |
* and .n_skip_labels_signer fields so that we can use them later on. */ |
| 414 |
|
| 415 |
assert(rrsig); |
| 416 |
assert(rrsig->key->type == DNS_TYPE_RRSIG); |
| 417 |
|
| 418 |
/* Check if this RRSIG RR is already prepared */ |
| 419 |
if (rrsig->n_skip_labels_source != UINT8_MAX) |
| 420 |
return 0; |
| 421 |
|
| 422 |
if (rrsig->rrsig.inception > rrsig->rrsig.expiration) |
| 423 |
return -EINVAL; |
| 424 |
|
| 425 |
name = dns_resource_key_name(rrsig->key); |
| 426 |
|
| 427 |
n_key_labels = dns_name_count_labels(name); |
| 428 |
if (n_key_labels < 0) |
| 429 |
return n_key_labels; |
| 430 |
if (rrsig->rrsig.labels > n_key_labels) |
| 431 |
return -EINVAL; |
| 432 |
|
| 433 |
n_signer_labels = dns_name_count_labels(rrsig->rrsig.signer); |
| 434 |
if (n_signer_labels < 0) |
| 435 |
return n_signer_labels; |
| 436 |
if (n_signer_labels > rrsig->rrsig.labels) |
| 437 |
return -EINVAL; |
| 438 |
|
| 439 |
r = dns_name_skip(name, n_key_labels - n_signer_labels, &name); |
| 440 |
if (r < 0) |
| 441 |
return r; |
| 442 |
if (r == 0) |
| 443 |
return -EINVAL; |
| 444 |
|
| 445 |
/* Check if the signer is really a suffix of us */ |
| 446 |
r = dns_name_equal(name, rrsig->rrsig.signer); |
| 447 |
if (r < 0) |
| 448 |
return r; |
| 449 |
if (r == 0) |
| 450 |
return -EINVAL; |
| 451 |
|
| 452 |
assert(n_key_labels < UINT8_MAX); /* UINT8_MAX/-1 means unsigned. */ |
| 453 |
rrsig->n_skip_labels_source = n_key_labels - rrsig->rrsig.labels; |
| 454 |
rrsig->n_skip_labels_signer = n_key_labels - n_signer_labels; |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static int dnssec_rrsig_expired(DnsResourceRecord *rrsig, usec_t realtime) { |
| 460 |
usec_t expiration, inception, skew; |
| 461 |
|
| 462 |
assert(rrsig); |
| 463 |
assert(rrsig->key->type == DNS_TYPE_RRSIG); |
| 464 |
|
| 465 |
if (realtime == USEC_INFINITY) |
| 466 |
realtime = now(CLOCK_REALTIME); |
| 467 |
|
| 468 |
expiration = rrsig->rrsig.expiration * USEC_PER_SEC; |
| 469 |
inception = rrsig->rrsig.inception * USEC_PER_SEC; |
| 470 |
|
| 471 |
/* Consider inverted validity intervals as expired */ |
| 472 |
if (inception > expiration) |
| 473 |
return true; |
| 474 |
|
| 475 |
/* Permit a certain amount of clock skew of 10% of the valid |
| 476 |
* time range. This takes inspiration from unbound's |
| 477 |
* resolver. */ |
| 478 |
skew = (expiration - inception) / 10; |
| 479 |
if (skew > SKEW_MAX) |
| 480 |
skew = SKEW_MAX; |
| 481 |
|
| 482 |
if (inception < skew) |
| 483 |
inception = 0; |
| 484 |
else |
| 485 |
inception -= skew; |
| 486 |
|
| 487 |
if (expiration + skew < expiration) |
| 488 |
expiration = USEC_INFINITY; |
| 489 |
else |
| 490 |
expiration += skew; |
| 491 |
|
| 492 |
return realtime < inception || realtime > expiration; |
| 493 |
} |
| 494 |
|
| 495 |
static const EVP_MD* algorithm_to_implementation_id(uint8_t algorithm) { |
| 496 |
|
| 497 |
/* Translates a DNSSEC signature algorithm into an openssl digest identifier. |
| 498 |
* |
| 499 |
* Note that we implement all algorithms listed as "Must implement" and "Recommended to Implement" in |
| 500 |
* RFC6944. We don't implement any algorithms that are listed as "Optional" or "Must Not Implement". |
| 501 |
* Specifically, we do not implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and GOST-ECC. */ |
| 502 |
|
| 503 |
switch (algorithm) { |
| 504 |
|
| 505 |
case DNSSEC_ALGORITHM_RSASHA1: |
| 506 |
case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1: |
| 507 |
return sym_EVP_sha1(); |
| 508 |
|
| 509 |
case DNSSEC_ALGORITHM_RSASHA256: |
| 510 |
case DNSSEC_ALGORITHM_ECDSAP256SHA256: |
| 511 |
return sym_EVP_sha256(); |
| 512 |
|
| 513 |
case DNSSEC_ALGORITHM_ECDSAP384SHA384: |
| 514 |
return sym_EVP_sha384(); |
| 515 |
|
| 516 |
case DNSSEC_ALGORITHM_RSASHA512: |
| 517 |
return sym_EVP_sha512(); |
| 518 |
|
| 519 |
default: |
| 520 |
return NULL; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
static void dnssec_fix_rrset_ttl( |
| 525 |
DnsResourceRecord *list[], |
| 526 |
unsigned n, |
| 527 |
DnsResourceRecord *rrsig) { |
| 528 |
|
| 529 |
assert(list); |
| 530 |
assert(n > 0); |
| 531 |
assert(rrsig); |
| 532 |
|
| 533 |
for (unsigned k = 0; k < n; k++) { |
| 534 |
DnsResourceRecord *rr = list[k]; |
| 535 |
|
| 536 |
/* Pick the TTL as the minimum of the RR's TTL, the |
| 537 |
* RR's original TTL according to the RRSIG and the |
| 538 |
* RRSIG's own TTL, see RFC 4035, Section 5.3.3 */ |
| 539 |
rr->ttl = MIN3(rr->ttl, rrsig->rrsig.original_ttl, rrsig->ttl); |
| 540 |
rr->expiry = rrsig->rrsig.expiration * USEC_PER_SEC; |
| 541 |
|
| 542 |
/* Copy over information about the signer and wildcard source of synthesis */ |
| 543 |
rr->n_skip_labels_source = rrsig->n_skip_labels_source; |
| 544 |
rr->n_skip_labels_signer = rrsig->n_skip_labels_signer; |
| 545 |
} |
| 546 |
|
| 547 |
rrsig->expiry = rrsig->rrsig.expiration * USEC_PER_SEC; |
| 548 |
} |
| 549 |
|
| 550 |
static int dnssec_rrset_serialize_sig( |
| 551 |
DnsResourceRecord *rrsig, |
| 552 |
const char *source, |
| 553 |
DnsResourceRecord **list, |
| 554 |
size_t list_len, |
| 555 |
bool wildcard, |
| 556 |
char **ret_sig_data, |
| 557 |
size_t *ret_sig_size) { |
| 558 |
|
| 559 |
_cleanup_(memstream_done) MemStream m = {}; |
| 560 |
uint8_t wire_format_name[DNS_WIRE_FORMAT_HOSTNAME_MAX]; |
| 561 |
DnsResourceRecord *rr; |
| 562 |
FILE *f; |
| 563 |
int r; |
| 564 |
|
| 565 |
assert(rrsig); |
| 566 |
assert(source); |
| 567 |
assert(list || list_len == 0); |
| 568 |
assert(ret_sig_data); |
| 569 |
assert(ret_sig_size); |
| 570 |
|
| 571 |
f = memstream_init(&m); |
| 572 |
if (!f) |
| 573 |
return -ENOMEM; |
| 574 |
|
| 575 |
fwrite_uint16(f, rrsig->rrsig.type_covered); |
| 576 |
fwrite_uint8(f, rrsig->rrsig.algorithm); |
| 577 |
fwrite_uint8(f, rrsig->rrsig.labels); |
| 578 |
fwrite_uint32(f, rrsig->rrsig.original_ttl); |
| 579 |
fwrite_uint32(f, rrsig->rrsig.expiration); |
| 580 |
fwrite_uint32(f, rrsig->rrsig.inception); |
| 581 |
fwrite_uint16(f, rrsig->rrsig.key_tag); |
| 582 |
|
| 583 |
r = dns_name_to_wire_format(rrsig->rrsig.signer, wire_format_name, sizeof(wire_format_name), true); |
| 584 |
if (r < 0) |
| 585 |
return r; |
| 586 |
fwrite(wire_format_name, 1, r, f); |
| 587 |
|
| 588 |
/* Convert the source of synthesis into wire format */ |
| 589 |
r = dns_name_to_wire_format(source, wire_format_name, sizeof(wire_format_name), true); |
| 590 |
if (r < 0) |
| 591 |
return r; |
| 592 |
|
| 593 |
for (size_t k = 0; k < list_len; k++) { |
| 594 |
size_t l; |
| 595 |
|
| 596 |
rr = list[k]; |
| 597 |
|
| 598 |
/* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */ |
| 599 |
if (wildcard) |
| 600 |
fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f); |
| 601 |
fwrite(wire_format_name, 1, r, f); |
| 602 |
|
| 603 |
fwrite_uint16(f, rr->key->type); |
| 604 |
fwrite_uint16(f, rr->key->class); |
| 605 |
fwrite_uint32(f, rrsig->rrsig.original_ttl); |
| 606 |
|
| 607 |
l = DNS_RESOURCE_RECORD_RDATA_SIZE(rr); |
| 608 |
assert(l <= 0xFFFF); |
| 609 |
|
| 610 |
fwrite_uint16(f, (uint16_t) l); |
| 611 |
fwrite(DNS_RESOURCE_RECORD_RDATA(rr), 1, l, f); |
| 612 |
} |
| 613 |
|
| 614 |
return memstream_finalize(&m, ret_sig_data, ret_sig_size); |
| 615 |
} |
| 616 |
|
| 617 |
static int dnssec_rrset_verify_sig( |
| 618 |
DnsResourceRecord *rrsig, |
| 619 |
DnsResourceRecord *dnskey, |
| 620 |
const char *sig_data, |
| 621 |
size_t sig_size) { |
| 622 |
|
| 623 |
assert(rrsig); |
| 624 |
assert(dnskey); |
| 625 |
assert(sig_data); |
| 626 |
assert(sig_size > 0); |
| 627 |
|
| 628 |
const EVP_MD *md_algorithm; |
| 629 |
|
| 630 |
uint8_t hash[EVP_MAX_MD_SIZE]; |
| 631 |
unsigned hash_size; |
| 632 |
|
| 633 |
switch (rrsig->rrsig.algorithm) { |
| 634 |
case DNSSEC_ALGORITHM_ED25519: |
| 635 |
return dnssec_eddsa_verify( |
| 636 |
rrsig->rrsig.algorithm, |
| 637 |
sig_data, sig_size, |
| 638 |
rrsig, |
| 639 |
dnskey); |
| 640 |
case DNSSEC_ALGORITHM_ED448: |
| 641 |
return -EOPNOTSUPP; |
| 642 |
default: |
| 643 |
/* OK, the RRs are now in canonical order. Let's calculate the digest */ |
| 644 |
md_algorithm = algorithm_to_implementation_id(rrsig->rrsig.algorithm); |
| 645 |
if (!md_algorithm) |
| 646 |
return -EOPNOTSUPP; |
| 647 |
|
| 648 |
_cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = sym_EVP_MD_CTX_new(); |
| 649 |
if (!ctx) |
| 650 |
return -ENOMEM; |
| 651 |
|
| 652 |
/* If the signature algorithm is supported by systemd-resolved but disabled by host policy, |
| 653 |
* also return -EOPNOTSUPP. */ |
| 654 |
if (sym_EVP_DigestInit_ex(ctx, md_algorithm, NULL) <= 0) |
| 655 |
return -EOPNOTSUPP; |
| 656 |
|
| 657 |
if (sym_EVP_DigestUpdate(ctx, sig_data, sig_size) <= 0) |
| 658 |
return -EIO; |
| 659 |
|
| 660 |
if (sym_EVP_DigestFinal_ex(ctx, hash, &hash_size) <= 0) |
| 661 |
return -EIO; |
| 662 |
|
| 663 |
assert(hash_size > 0); |
| 664 |
} |
| 665 |
|
| 666 |
switch (rrsig->rrsig.algorithm) { |
| 667 |
|
| 668 |
case DNSSEC_ALGORITHM_RSASHA1: |
| 669 |
case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1: |
| 670 |
case DNSSEC_ALGORITHM_RSASHA256: |
| 671 |
case DNSSEC_ALGORITHM_RSASHA512: |
| 672 |
return dnssec_rsa_verify( |
| 673 |
md_algorithm, |
| 674 |
hash, hash_size, |
| 675 |
rrsig, |
| 676 |
dnskey); |
| 677 |
|
| 678 |
case DNSSEC_ALGORITHM_ECDSAP256SHA256: |
| 679 |
case DNSSEC_ALGORITHM_ECDSAP384SHA384: |
| 680 |
return dnssec_ecdsa_verify( |
| 681 |
md_algorithm, |
| 682 |
rrsig->rrsig.algorithm, |
| 683 |
hash, hash_size, |
| 684 |
rrsig, |
| 685 |
dnskey); |
| 686 |
|
| 687 |
default: |
| 688 |
assert_not_reached(); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
int dnssec_verify_rrset( |
| 693 |
DnsAnswer *a, |
| 694 |
const DnsResourceKey *key, |
| 695 |
DnsResourceRecord *rrsig, |
| 696 |
DnsResourceRecord *dnskey, |
| 697 |
usec_t realtime, |
| 698 |
DnssecResult *result) { |
| 699 |
|
| 700 |
DnsResourceRecord **list, *rr; |
| 701 |
const char *source, *name; |
| 702 |
_cleanup_free_ char *sig_data = NULL; |
| 703 |
size_t sig_size = 0; /* avoid false maybe-uninitialized warning */ |
| 704 |
size_t n = 0; |
| 705 |
bool wildcard; |
| 706 |
int r; |
| 707 |
|
| 708 |
assert(key); |
| 709 |
assert(rrsig); |
| 710 |
assert(dnskey); |
| 711 |
assert(result); |
| 712 |
|
| 713 |
r = DLOPEN_LIBCRYPTO(LOG_DEBUG, DLOPEN_LIBCRYPTO_PRIORITY); |
| 714 |
if (r < 0) |
| 715 |
return r; |
| 716 |
|
| 717 |
assert(rrsig->key->type == DNS_TYPE_RRSIG); |
| 718 |
assert(dnskey->key->type == DNS_TYPE_DNSKEY); |
| 719 |
|
| 720 |
/* Verifies that the RRSet matches the specified "key" in "a", |
| 721 |
* using the signature "rrsig" and the key "dnskey". It's |
| 722 |
* assumed that RRSIG and DNSKEY match. */ |
| 723 |
|
| 724 |
r = dnssec_rrsig_prepare(rrsig); |
| 725 |
if (r == -EINVAL) { |
| 726 |
*result = DNSSEC_INVALID; |
| 727 |
return r; |
| 728 |
} |
| 729 |
if (r < 0) |
| 730 |
return r; |
| 731 |
|
| 732 |
r = dnssec_rrsig_expired(rrsig, realtime); |
| 733 |
if (r < 0) |
| 734 |
return r; |
| 735 |
if (r > 0) { |
| 736 |
*result = DNSSEC_SIGNATURE_EXPIRED; |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
name = dns_resource_key_name(key); |
| 741 |
|
| 742 |
/* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */ |
| 743 |
if (dns_type_apex_only(rrsig->rrsig.type_covered)) { |
| 744 |
r = dns_name_equal(rrsig->rrsig.signer, name); |
| 745 |
if (r < 0) |
| 746 |
return r; |
| 747 |
if (r == 0) { |
| 748 |
*result = DNSSEC_INVALID; |
| 749 |
return 0; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
/* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */ |
| 754 |
if (rrsig->rrsig.type_covered == DNS_TYPE_DS) { |
| 755 |
r = dns_name_equal(rrsig->rrsig.signer, name); |
| 756 |
if (r < 0) |
| 757 |
return r; |
| 758 |
if (r > 0) { |
| 759 |
*result = DNSSEC_INVALID; |
| 760 |
return 0; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
/* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */ |
| 765 |
r = dns_name_suffix(name, rrsig->rrsig.labels, &source); |
| 766 |
if (r < 0) |
| 767 |
return r; |
| 768 |
if (r > 0 && !dns_type_may_wildcard(rrsig->rrsig.type_covered)) { |
| 769 |
/* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */ |
| 770 |
*result = DNSSEC_INVALID; |
| 771 |
return 0; |
| 772 |
} |
| 773 |
if (r == 1) { |
| 774 |
/* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really |
| 775 |
* synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */ |
| 776 |
r = dns_name_startswith(name, "*"); |
| 777 |
if (r < 0) |
| 778 |
return r; |
| 779 |
if (r > 0) |
| 780 |
source = name; |
| 781 |
|
| 782 |
wildcard = r == 0; |
| 783 |
} else |
| 784 |
wildcard = r > 0; |
| 785 |
|
| 786 |
/* Collect all relevant RRs in a single array, so that we can look at the RRset */ |
| 787 |
list = newa(DnsResourceRecord *, dns_answer_size(a)); |
| 788 |
|
| 789 |
DNS_ANSWER_FOREACH(rr, a) { |
| 790 |
r = dns_resource_key_equal(key, rr->key); |
| 791 |
if (r < 0) |
| 792 |
return r; |
| 793 |
if (r == 0) |
| 794 |
continue; |
| 795 |
|
| 796 |
/* We need the wire format for ordering, and digest calculation */ |
| 797 |
r = dns_resource_record_to_wire_format(rr, true); |
| 798 |
if (r < 0) |
| 799 |
return r; |
| 800 |
|
| 801 |
list[n++] = rr; |
| 802 |
|
| 803 |
if (n > VERIFY_RRS_MAX) |
| 804 |
return -E2BIG; |
| 805 |
} |
| 806 |
|
| 807 |
if (n <= 0) |
| 808 |
return -ENODATA; |
| 809 |
|
| 810 |
/* Bring the RRs into canonical order */ |
| 811 |
typesafe_qsort(list, n, rr_compare); |
| 812 |
|
| 813 |
r = dnssec_rrset_serialize_sig(rrsig, source, list, n, wildcard, |
| 814 |
&sig_data, &sig_size); |
| 815 |
if (r < 0) |
| 816 |
return r; |
| 817 |
|
| 818 |
r = dnssec_rrset_verify_sig(rrsig, dnskey, sig_data, sig_size); |
| 819 |
if (r == -EOPNOTSUPP) { |
| 820 |
*result = DNSSEC_UNSUPPORTED_ALGORITHM; |
| 821 |
return 0; |
| 822 |
} |
| 823 |
if (r < 0) |
| 824 |
return r; |
| 825 |
|
| 826 |
/* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */ |
| 827 |
if (r > 0) |
| 828 |
dnssec_fix_rrset_ttl(list, n, rrsig); |
| 829 |
|
| 830 |
if (r == 0) |
| 831 |
*result = DNSSEC_INVALID; |
| 832 |
else if (wildcard) |
| 833 |
*result = DNSSEC_VALIDATED_WILDCARD; |
| 834 |
else |
| 835 |
*result = DNSSEC_VALIDATED; |
| 836 |
|
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
int dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) { |
| 841 |
|
| 842 |
assert(rrsig); |
| 843 |
assert(dnskey); |
| 844 |
|
| 845 |
/* Checks if the specified DNSKEY RR matches the key used for |
| 846 |
* the signature in the specified RRSIG RR */ |
| 847 |
|
| 848 |
if (rrsig->key->type != DNS_TYPE_RRSIG) |
| 849 |
return -EINVAL; |
| 850 |
|
| 851 |
if (dnskey->key->type != DNS_TYPE_DNSKEY) |
| 852 |
return 0; |
| 853 |
if (dnskey->key->class != rrsig->key->class) |
| 854 |
return 0; |
| 855 |
if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0) |
| 856 |
return 0; |
| 857 |
if (!revoked_ok && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE)) |
| 858 |
return 0; |
| 859 |
if (dnskey->dnskey.protocol != 3) |
| 860 |
return 0; |
| 861 |
if (dnskey->dnskey.algorithm != rrsig->rrsig.algorithm) |
| 862 |
return 0; |
| 863 |
|
| 864 |
if (dnssec_keytag(dnskey, false) != rrsig->rrsig.key_tag) |
| 865 |
return 0; |
| 866 |
|
| 867 |
return dns_name_equal(dns_resource_key_name(dnskey->key), rrsig->rrsig.signer); |
| 868 |
} |
| 869 |
|
| 870 |
int dnssec_key_match_rrsig(const DnsResourceKey *key, DnsResourceRecord *rrsig) { |
| 871 |
assert(key); |
| 872 |
assert(rrsig); |
| 873 |
|
| 874 |
/* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */ |
| 875 |
|
| 876 |
if (rrsig->key->type != DNS_TYPE_RRSIG) |
| 877 |
return 0; |
| 878 |
if (rrsig->key->class != key->class) |
| 879 |
return 0; |
| 880 |
if (rrsig->rrsig.type_covered != key->type) |
| 881 |
return 0; |
| 882 |
|
| 883 |
return dns_name_equal(dns_resource_key_name(rrsig->key), dns_resource_key_name(key)); |
| 884 |
} |
| 885 |
|
| 886 |
int dnssec_verify_rrset_search( |
| 887 |
DnsAnswer *a, |
| 888 |
const DnsResourceKey *key, |
| 889 |
DnsAnswer *validated_dnskeys, |
| 890 |
usec_t realtime, |
| 891 |
DnssecResult *result, |
| 892 |
DnsResourceRecord **ret_rrsig) { |
| 893 |
|
| 894 |
bool found_rrsig = false, found_invalid = false, found_expired_rrsig = false, found_unsupported_algorithm = false; |
| 895 |
unsigned nvalidations = 0; |
| 896 |
DnsResourceRecord *rrsig; |
| 897 |
int r; |
| 898 |
|
| 899 |
assert(key); |
| 900 |
assert(result); |
| 901 |
|
| 902 |
/* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */ |
| 903 |
|
| 904 |
if (dns_answer_isempty(a)) |
| 905 |
return -ENODATA; |
| 906 |
|
| 907 |
/* Iterate through each RRSIG RR. */ |
| 908 |
DNS_ANSWER_FOREACH(rrsig, a) { |
| 909 |
DnsResourceRecord *dnskey; |
| 910 |
DnsAnswerFlags flags; |
| 911 |
|
| 912 |
/* Is this an RRSIG RR that applies to RRs matching our key? */ |
| 913 |
r = dnssec_key_match_rrsig(key, rrsig); |
| 914 |
if (r < 0) |
| 915 |
return r; |
| 916 |
if (r == 0) |
| 917 |
continue; |
| 918 |
|
| 919 |
found_rrsig = true; |
| 920 |
|
| 921 |
/* Look for a matching key */ |
| 922 |
DNS_ANSWER_FOREACH_FLAGS(dnskey, flags, validated_dnskeys) { |
| 923 |
DnssecResult one_result; |
| 924 |
|
| 925 |
/* Is this a DNSKEY RR that matches they key of our RRSIG? */ |
| 926 |
r = dnssec_rrsig_match_dnskey(rrsig, dnskey, false); |
| 927 |
if (r < 0) |
| 928 |
return r; |
| 929 |
if (r == 0) |
| 930 |
continue; |
| 931 |
|
| 932 |
if ((flags & DNS_ANSWER_AUTHENTICATED) == 0) { |
| 933 |
/* An unauthenticated DNSKEY in validated_dnskeys is a key we are not able to |
| 934 |
* authenticate, but might still be valid. Record this as an unsupported |
| 935 |
* algorithm so we can still at least report an insecure answer. */ |
| 936 |
found_unsupported_algorithm = true; |
| 937 |
continue; |
| 938 |
} |
| 939 |
|
| 940 |
/* Take the time here, if it isn't set yet, so |
| 941 |
* that we do all validations with the same |
| 942 |
* time. */ |
| 943 |
if (realtime == USEC_INFINITY) |
| 944 |
realtime = now(CLOCK_REALTIME); |
| 945 |
|
| 946 |
/* Have we seen an unreasonable number of invalid signatures? */ |
| 947 |
if (nvalidations > DNSSEC_INVALID_MAX) { |
| 948 |
if (ret_rrsig) |
| 949 |
*ret_rrsig = NULL; |
| 950 |
*result = DNSSEC_TOO_MANY_VALIDATIONS; |
| 951 |
return (int) nvalidations; |
| 952 |
} |
| 953 |
|
| 954 |
/* Yay, we found a matching RRSIG with a matching |
| 955 |
* DNSKEY, awesome. Now let's verify all entries of |
| 956 |
* the RRSet against the RRSIG and DNSKEY |
| 957 |
* combination. */ |
| 958 |
|
| 959 |
r = dnssec_verify_rrset(a, key, rrsig, dnskey, realtime, &one_result); |
| 960 |
if (r < 0) |
| 961 |
return r; |
| 962 |
|
| 963 |
nvalidations++; |
| 964 |
|
| 965 |
switch (one_result) { |
| 966 |
|
| 967 |
case DNSSEC_VALIDATED: |
| 968 |
case DNSSEC_VALIDATED_WILDCARD: |
| 969 |
/* Yay, the RR has been validated, |
| 970 |
* return immediately, but fix up the expiry */ |
| 971 |
if (ret_rrsig) |
| 972 |
*ret_rrsig = rrsig; |
| 973 |
|
| 974 |
*result = one_result; |
| 975 |
return (int) nvalidations; |
| 976 |
|
| 977 |
case DNSSEC_INVALID: |
| 978 |
/* If the signature is invalid, let's try another |
| 979 |
key and/or signature. After all they |
| 980 |
key_tags and stuff are not unique, and |
| 981 |
might be shared by multiple keys. */ |
| 982 |
found_invalid = true; |
| 983 |
continue; |
| 984 |
|
| 985 |
case DNSSEC_UNSUPPORTED_ALGORITHM: |
| 986 |
/* If the key algorithm is |
| 987 |
unsupported, try another |
| 988 |
RRSIG/DNSKEY pair, but remember we |
| 989 |
encountered this, so that we can |
| 990 |
return a proper error when we |
| 991 |
encounter nothing better. */ |
| 992 |
found_unsupported_algorithm = true; |
| 993 |
continue; |
| 994 |
|
| 995 |
case DNSSEC_SIGNATURE_EXPIRED: |
| 996 |
/* If the signature is expired, try |
| 997 |
another one, but remember it, so |
| 998 |
that we can return this */ |
| 999 |
found_expired_rrsig = true; |
| 1000 |
continue; |
| 1001 |
|
| 1002 |
default: |
| 1003 |
assert_not_reached(); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (found_expired_rrsig) |
| 1009 |
*result = DNSSEC_SIGNATURE_EXPIRED; |
| 1010 |
else if (found_unsupported_algorithm) |
| 1011 |
*result = DNSSEC_UNSUPPORTED_ALGORITHM; |
| 1012 |
else if (found_invalid) |
| 1013 |
*result = DNSSEC_INVALID; |
| 1014 |
else if (found_rrsig) |
| 1015 |
*result = DNSSEC_MISSING_KEY; |
| 1016 |
else |
| 1017 |
*result = DNSSEC_NO_SIGNATURE; |
| 1018 |
|
| 1019 |
if (ret_rrsig) |
| 1020 |
*ret_rrsig = NULL; |
| 1021 |
|
| 1022 |
return (int) nvalidations; |
| 1023 |
} |
| 1024 |
|
| 1025 |
int dnssec_has_rrsig(DnsAnswer *a, const DnsResourceKey *key) { |
| 1026 |
DnsResourceRecord *rr; |
| 1027 |
int r; |
| 1028 |
|
| 1029 |
/* Checks whether there's at least one RRSIG in 'a' that protects RRs of the specified key */ |
| 1030 |
|
| 1031 |
DNS_ANSWER_FOREACH(rr, a) { |
| 1032 |
r = dnssec_key_match_rrsig(key, rr); |
| 1033 |
if (r < 0) |
| 1034 |
return r; |
| 1035 |
if (r > 0) |
| 1036 |
return 1; |
| 1037 |
} |
| 1038 |
|
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static const EVP_MD* digest_to_hash_md(uint8_t algorithm) { |
| 1043 |
|
| 1044 |
/* Translates a DNSSEC digest algorithm into an openssl digest identifier */ |
| 1045 |
|
| 1046 |
switch (algorithm) { |
| 1047 |
|
| 1048 |
case DNSSEC_DIGEST_SHA1: |
| 1049 |
return sym_EVP_sha1(); |
| 1050 |
|
| 1051 |
case DNSSEC_DIGEST_SHA256: |
| 1052 |
return sym_EVP_sha256(); |
| 1053 |
|
| 1054 |
case DNSSEC_DIGEST_SHA384: |
| 1055 |
return sym_EVP_sha384(); |
| 1056 |
|
| 1057 |
default: |
| 1058 |
return NULL; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
int dnssec_verify_dnskey_by_ds(DnsResourceRecord *dnskey, DnsResourceRecord *ds, bool mask_revoke) { |
| 1063 |
uint8_t wire_format[DNS_WIRE_FORMAT_HOSTNAME_MAX]; |
| 1064 |
size_t encoded_length; |
| 1065 |
int r; |
| 1066 |
|
| 1067 |
assert(dnskey); |
| 1068 |
assert(ds); |
| 1069 |
|
| 1070 |
r = DLOPEN_LIBCRYPTO(LOG_DEBUG, DLOPEN_LIBCRYPTO_PRIORITY); |
| 1071 |
if (r < 0) |
| 1072 |
return r; |
| 1073 |
|
| 1074 |
/* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */ |
| 1075 |
|
| 1076 |
if (dnskey->key->type != DNS_TYPE_DNSKEY) |
| 1077 |
return -EINVAL; |
| 1078 |
if (ds->key->type != DNS_TYPE_DS) |
| 1079 |
return -EINVAL; |
| 1080 |
if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0) |
| 1081 |
return -EKEYREJECTED; |
| 1082 |
if (!mask_revoke && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE)) |
| 1083 |
return -EKEYREJECTED; |
| 1084 |
if (dnskey->dnskey.protocol != 3) |
| 1085 |
return -EKEYREJECTED; |
| 1086 |
|
| 1087 |
if (dnskey->dnskey.algorithm != ds->ds.algorithm) |
| 1088 |
return 0; |
| 1089 |
if (dnssec_keytag(dnskey, mask_revoke) != ds->ds.key_tag) |
| 1090 |
return 0; |
| 1091 |
|
| 1092 |
r = dns_name_to_wire_format(dns_resource_key_name(dnskey->key), wire_format, sizeof wire_format, true); |
| 1093 |
if (r < 0) |
| 1094 |
return r; |
| 1095 |
encoded_length = r; |
| 1096 |
|
| 1097 |
const EVP_MD *md_algorithm = digest_to_hash_md(ds->ds.digest_type); |
| 1098 |
if (!md_algorithm) |
| 1099 |
return -EOPNOTSUPP; |
| 1100 |
|
| 1101 |
_cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = NULL; |
| 1102 |
uint8_t result[EVP_MAX_MD_SIZE]; |
| 1103 |
|
| 1104 |
unsigned hash_size = sym_EVP_MD_get_size(md_algorithm); |
| 1105 |
assert(hash_size > 0); |
| 1106 |
|
| 1107 |
if (ds->ds.digest_size != hash_size) |
| 1108 |
return 0; |
| 1109 |
|
| 1110 |
ctx = sym_EVP_MD_CTX_new(); |
| 1111 |
if (!ctx) |
| 1112 |
return -ENOMEM; |
| 1113 |
|
| 1114 |
/* If the digest is supported by systemd-resolved but disabled by host policy, also return -EOPNOTSUPP |
| 1115 |
*/ |
| 1116 |
if (sym_EVP_DigestInit_ex(ctx, md_algorithm, NULL) <= 0) |
| 1117 |
return -EOPNOTSUPP; |
| 1118 |
|
| 1119 |
if (sym_EVP_DigestUpdate(ctx, wire_format, encoded_length) <= 0) |
| 1120 |
return -EIO; |
| 1121 |
|
| 1122 |
if (mask_revoke) |
| 1123 |
md_add_uint16(ctx, dnskey->dnskey.flags & ~DNSKEY_FLAG_REVOKE); |
| 1124 |
else |
| 1125 |
md_add_uint16(ctx, dnskey->dnskey.flags); |
| 1126 |
|
| 1127 |
r = md_add_uint8(ctx, dnskey->dnskey.protocol); |
| 1128 |
if (r <= 0) |
| 1129 |
return r; |
| 1130 |
r = md_add_uint8(ctx, dnskey->dnskey.algorithm); |
| 1131 |
if (r <= 0) |
| 1132 |
return r; |
| 1133 |
if (sym_EVP_DigestUpdate(ctx, dnskey->dnskey.key, dnskey->dnskey.key_size) <= 0) |
| 1134 |
return -EIO; |
| 1135 |
|
| 1136 |
if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0) |
| 1137 |
return -EIO; |
| 1138 |
|
| 1139 |
return memcmp(result, ds->ds.digest, ds->ds.digest_size) == 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord *dnskey, DnsAnswer *validated_ds) { |
| 1143 |
DnsResourceRecord *ds; |
| 1144 |
DnsAnswerFlags flags; |
| 1145 |
bool found_unsupported_algorithm = false; |
| 1146 |
int r; |
| 1147 |
|
| 1148 |
assert(dnskey); |
| 1149 |
|
| 1150 |
if (dnskey->key->type != DNS_TYPE_DNSKEY) |
| 1151 |
return 0; |
| 1152 |
|
| 1153 |
DNS_ANSWER_FOREACH_FLAGS(ds, flags, validated_ds) { |
| 1154 |
|
| 1155 |
if ((flags & DNS_ANSWER_AUTHENTICATED) == 0) |
| 1156 |
continue; |
| 1157 |
|
| 1158 |
if (ds->key->type != DNS_TYPE_DS) |
| 1159 |
continue; |
| 1160 |
if (ds->key->class != dnskey->key->class) |
| 1161 |
continue; |
| 1162 |
|
| 1163 |
r = dns_name_equal(dns_resource_key_name(dnskey->key), dns_resource_key_name(ds->key)); |
| 1164 |
if (r < 0) |
| 1165 |
return r; |
| 1166 |
if (r == 0) |
| 1167 |
continue; |
| 1168 |
|
| 1169 |
r = dnssec_verify_dnskey_by_ds(dnskey, ds, false); |
| 1170 |
if (r == -EKEYREJECTED) |
| 1171 |
continue; /* The DNSKEY is revoked or otherwise invalid. */ |
| 1172 |
if (r == -EOPNOTSUPP) { |
| 1173 |
found_unsupported_algorithm = true; |
| 1174 |
continue; |
| 1175 |
} |
| 1176 |
if (r < 0) |
| 1177 |
return r; |
| 1178 |
if (r > 0) |
| 1179 |
return 1; |
| 1180 |
} |
| 1181 |
|
| 1182 |
if (found_unsupported_algorithm) |
| 1183 |
return -EOPNOTSUPP; |
| 1184 |
|
| 1185 |
return 0; |
| 1186 |
} |
| 1187 |
|
| 1188 |
static const EVP_MD* nsec3_hash_to_hash_md(uint8_t algorithm) { |
| 1189 |
|
| 1190 |
/* Translates a DNSSEC NSEC3 hash algorithm into an openssl digest identifier */ |
| 1191 |
|
| 1192 |
switch (algorithm) { |
| 1193 |
|
| 1194 |
case NSEC3_ALGORITHM_SHA1: |
| 1195 |
return sym_EVP_sha1(); |
| 1196 |
|
| 1197 |
default: |
| 1198 |
return NULL; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) { |
| 1203 |
uint8_t wire_format[DNS_WIRE_FORMAT_HOSTNAME_MAX]; |
| 1204 |
int r; |
| 1205 |
|
| 1206 |
assert(nsec3); |
| 1207 |
assert(name); |
| 1208 |
assert(ret); |
| 1209 |
|
| 1210 |
r = DLOPEN_LIBCRYPTO(LOG_DEBUG, DLOPEN_LIBCRYPTO_PRIORITY); |
| 1211 |
if (r < 0) |
| 1212 |
return r; |
| 1213 |
|
| 1214 |
if (nsec3->key->type != DNS_TYPE_NSEC3) |
| 1215 |
return -EINVAL; |
| 1216 |
|
| 1217 |
if (nsec3->nsec3.iterations > NSEC3_ITERATIONS_MAX) |
| 1218 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 1219 |
"Ignoring NSEC3 RR %s with excessive number of iterations.", |
| 1220 |
dns_resource_record_to_string(nsec3)); |
| 1221 |
|
| 1222 |
const EVP_MD *algorithm = nsec3_hash_to_hash_md(nsec3->nsec3.algorithm); |
| 1223 |
if (!algorithm) |
| 1224 |
return -EOPNOTSUPP; |
| 1225 |
|
| 1226 |
size_t hash_size = sym_EVP_MD_get_size(algorithm); |
| 1227 |
assert(hash_size > 0); |
| 1228 |
|
| 1229 |
if (nsec3->nsec3.next_hashed_name_size != hash_size) |
| 1230 |
return -EINVAL; |
| 1231 |
|
| 1232 |
_cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = sym_EVP_MD_CTX_new(); |
| 1233 |
if (!ctx) |
| 1234 |
return -ENOMEM; |
| 1235 |
|
| 1236 |
if (sym_EVP_DigestInit_ex(ctx, algorithm, NULL) <= 0) |
| 1237 |
return -EOPNOTSUPP; |
| 1238 |
|
| 1239 |
r = dns_name_to_wire_format(name, wire_format, sizeof(wire_format), true); |
| 1240 |
if (r < 0) |
| 1241 |
return r; |
| 1242 |
|
| 1243 |
if (sym_EVP_DigestUpdate(ctx, wire_format, r) <= 0) |
| 1244 |
return -EIO; |
| 1245 |
if (sym_EVP_DigestUpdate(ctx, nsec3->nsec3.salt, nsec3->nsec3.salt_size) <= 0) |
| 1246 |
return -EIO; |
| 1247 |
|
| 1248 |
uint8_t result[EVP_MAX_MD_SIZE]; |
| 1249 |
if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0) |
| 1250 |
return -EIO; |
| 1251 |
|
| 1252 |
for (unsigned k = 0; k < nsec3->nsec3.iterations; k++) { |
| 1253 |
if (sym_EVP_DigestInit_ex(ctx, algorithm, NULL) <= 0) |
| 1254 |
return -EOPNOTSUPP; |
| 1255 |
if (sym_EVP_DigestUpdate(ctx, result, hash_size) <= 0) |
| 1256 |
return -EIO; |
| 1257 |
if (sym_EVP_DigestUpdate(ctx, nsec3->nsec3.salt, nsec3->nsec3.salt_size) <= 0) |
| 1258 |
return -EIO; |
| 1259 |
|
| 1260 |
if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0) |
| 1261 |
return -EIO; |
| 1262 |
} |
| 1263 |
|
| 1264 |
memcpy(ret, result, hash_size); |
| 1265 |
return (int) hash_size; |
| 1266 |
} |
| 1267 |
|
| 1268 |
static int nsec3_is_good(DnsResourceRecord *rr, DnsResourceRecord *nsec3) { |
| 1269 |
const char *a, *b; |
| 1270 |
int r; |
| 1271 |
|
| 1272 |
assert(rr); |
| 1273 |
|
| 1274 |
if (rr->key->type != DNS_TYPE_NSEC3) |
| 1275 |
return 0; |
| 1276 |
|
| 1277 |
/* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */ |
| 1278 |
if (!IN_SET(rr->nsec3.flags, 0, 1)) |
| 1279 |
return 0; |
| 1280 |
|
| 1281 |
/* Ignore NSEC3 RRs whose algorithm we don't know */ |
| 1282 |
if (!nsec3_hash_to_hash_md(rr->nsec3.algorithm)) |
| 1283 |
return 0; |
| 1284 |
|
| 1285 |
/* Ignore NSEC3 RRs with an excessive number of required iterations */ |
| 1286 |
if (rr->nsec3.iterations > NSEC3_ITERATIONS_MAX) |
| 1287 |
return 0; |
| 1288 |
|
| 1289 |
/* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this |
| 1290 |
* check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */ |
| 1291 |
if (!IN_SET(rr->n_skip_labels_source, 0, UINT8_MAX)) |
| 1292 |
return 0; |
| 1293 |
/* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */ |
| 1294 |
if (!IN_SET(rr->n_skip_labels_signer, 1, UINT8_MAX)) |
| 1295 |
return 0; |
| 1296 |
|
| 1297 |
if (!nsec3) |
| 1298 |
return 1; |
| 1299 |
|
| 1300 |
/* If a second NSEC3 RR is specified, also check if they are from the same zone. */ |
| 1301 |
|
| 1302 |
if (nsec3 == rr) /* Shortcut */ |
| 1303 |
return 1; |
| 1304 |
|
| 1305 |
if (rr->key->class != nsec3->key->class) |
| 1306 |
return 0; |
| 1307 |
if (rr->nsec3.algorithm != nsec3->nsec3.algorithm) |
| 1308 |
return 0; |
| 1309 |
if (rr->nsec3.iterations != nsec3->nsec3.iterations) |
| 1310 |
return 0; |
| 1311 |
if (rr->nsec3.salt_size != nsec3->nsec3.salt_size) |
| 1312 |
return 0; |
| 1313 |
if (memcmp_safe(rr->nsec3.salt, nsec3->nsec3.salt, rr->nsec3.salt_size) != 0) |
| 1314 |
return 0; |
| 1315 |
|
| 1316 |
a = dns_resource_key_name(rr->key); |
| 1317 |
r = dns_name_parent(&a); /* strip off hash */ |
| 1318 |
if (r <= 0) |
| 1319 |
return r; |
| 1320 |
|
| 1321 |
b = dns_resource_key_name(nsec3->key); |
| 1322 |
r = dns_name_parent(&b); /* strip off hash */ |
| 1323 |
if (r <= 0) |
| 1324 |
return r; |
| 1325 |
|
| 1326 |
/* Make sure both have the same parent */ |
| 1327 |
return dns_name_equal(a, b); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static int nsec3_hashed_domain_format(const uint8_t *hashed, size_t hashed_size, const char *zone, char **ret) { |
| 1331 |
_cleanup_free_ char *l = NULL; |
| 1332 |
char *j; |
| 1333 |
|
| 1334 |
assert(hashed); |
| 1335 |
assert(hashed_size > 0); |
| 1336 |
assert(zone); |
| 1337 |
assert(ret); |
| 1338 |
|
| 1339 |
l = base32hexmem(hashed, hashed_size, false); |
| 1340 |
if (!l) |
| 1341 |
return -ENOMEM; |
| 1342 |
|
| 1343 |
j = strjoin(l, ".", zone); |
| 1344 |
if (!j) |
| 1345 |
return -ENOMEM; |
| 1346 |
|
| 1347 |
*ret = j; |
| 1348 |
return (int) hashed_size; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int nsec3_hashed_domain_make(DnsResourceRecord *nsec3, const char *domain, const char *zone, char **ret) { |
| 1352 |
uint8_t hashed[DNSSEC_HASH_SIZE_MAX]; |
| 1353 |
int hashed_size; |
| 1354 |
|
| 1355 |
assert(nsec3); |
| 1356 |
assert(domain); |
| 1357 |
assert(zone); |
| 1358 |
assert(ret); |
| 1359 |
|
| 1360 |
hashed_size = dnssec_nsec3_hash(nsec3, domain, hashed); |
| 1361 |
if (hashed_size < 0) |
| 1362 |
return hashed_size; |
| 1363 |
|
| 1364 |
return nsec3_hashed_domain_format(hashed, (size_t) hashed_size, zone, ret); |
| 1365 |
} |
| 1366 |
|
| 1367 |
/* See RFC 5155, Section 8 |
| 1368 |
* First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest |
| 1369 |
* enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there |
| 1370 |
* is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that |
| 1371 |
* matches the wildcard domain. |
| 1372 |
* |
| 1373 |
* Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or |
| 1374 |
* that there is no proof either way. The latter is the case if a proof of non-existence of a given |
| 1375 |
* name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records |
| 1376 |
* to conclude anything we indicate this by returning NO_RR. */ |
| 1377 |
static int dnssec_test_nsec3(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) { |
| 1378 |
_cleanup_free_ char *next_closer_domain = NULL, *wildcard_domain = NULL; |
| 1379 |
const char *zone, *p, *pp = NULL, *wildcard; |
| 1380 |
DnsResourceRecord *rr, *enclosure_rr, *zone_rr, *wildcard_rr = NULL; |
| 1381 |
DnsAnswerFlags flags; |
| 1382 |
int hashed_size, r; |
| 1383 |
bool a, no_closer = false, no_wildcard = false, optout = false; |
| 1384 |
|
| 1385 |
assert(key); |
| 1386 |
assert(result); |
| 1387 |
|
| 1388 |
/* First step, find the zone name and the NSEC3 parameters of the zone. |
| 1389 |
* it is sufficient to look for the longest common suffix we find with |
| 1390 |
* any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3 |
| 1391 |
* records from a given zone in a response must use the same |
| 1392 |
* parameters. */ |
| 1393 |
zone = dns_resource_key_name(key); |
| 1394 |
for (;;) { |
| 1395 |
DNS_ANSWER_FOREACH_FLAGS(zone_rr, flags, answer) { |
| 1396 |
r = nsec3_is_good(zone_rr, NULL); |
| 1397 |
if (r < 0) |
| 1398 |
return r; |
| 1399 |
if (r == 0) |
| 1400 |
continue; |
| 1401 |
|
| 1402 |
r = dns_name_equal_skip(dns_resource_key_name(zone_rr->key), 1, zone); |
| 1403 |
if (r < 0) |
| 1404 |
return r; |
| 1405 |
if (r > 0) |
| 1406 |
goto found_zone; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* Strip one label from the front */ |
| 1410 |
r = dns_name_parent(&zone); |
| 1411 |
if (r < 0) |
| 1412 |
return r; |
| 1413 |
if (r == 0) |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
|
| 1417 |
*result = DNSSEC_NSEC_NO_RR; |
| 1418 |
return 0; |
| 1419 |
|
| 1420 |
found_zone: |
| 1421 |
/* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */ |
| 1422 |
p = dns_resource_key_name(key); |
| 1423 |
for (;;) { |
| 1424 |
_cleanup_free_ char *hashed_domain = NULL; |
| 1425 |
|
| 1426 |
hashed_size = nsec3_hashed_domain_make(zone_rr, p, zone, &hashed_domain); |
| 1427 |
if (hashed_size == -EOPNOTSUPP) { |
| 1428 |
*result = DNSSEC_NSEC_UNSUPPORTED_ALGORITHM; |
| 1429 |
return 0; |
| 1430 |
} |
| 1431 |
if (hashed_size < 0) |
| 1432 |
return hashed_size; |
| 1433 |
|
| 1434 |
DNS_ANSWER_FOREACH_FLAGS(enclosure_rr, flags, answer) { |
| 1435 |
|
| 1436 |
r = nsec3_is_good(enclosure_rr, zone_rr); |
| 1437 |
if (r < 0) |
| 1438 |
return r; |
| 1439 |
if (r == 0) |
| 1440 |
continue; |
| 1441 |
|
| 1442 |
if (enclosure_rr->nsec3.next_hashed_name_size != (size_t) hashed_size) |
| 1443 |
continue; |
| 1444 |
|
| 1445 |
r = dns_name_equal(dns_resource_key_name(enclosure_rr->key), hashed_domain); |
| 1446 |
if (r < 0) |
| 1447 |
return r; |
| 1448 |
if (r > 0) { |
| 1449 |
a = flags & DNS_ANSWER_AUTHENTICATED; |
| 1450 |
goto found_closest_encloser; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
/* We didn't find the closest encloser with this name, |
| 1455 |
* but let's remember this domain name, it might be |
| 1456 |
* the next closer name */ |
| 1457 |
|
| 1458 |
pp = p; |
| 1459 |
|
| 1460 |
/* Strip one label from the front */ |
| 1461 |
r = dns_name_parent(&p); |
| 1462 |
if (r < 0) |
| 1463 |
return r; |
| 1464 |
if (r == 0) |
| 1465 |
break; |
| 1466 |
} |
| 1467 |
|
| 1468 |
*result = DNSSEC_NSEC_NO_RR; |
| 1469 |
return 0; |
| 1470 |
|
| 1471 |
found_closest_encloser: |
| 1472 |
/* We found a closest encloser in 'p'; next closer is 'pp' */ |
| 1473 |
|
| 1474 |
if (!pp) { |
| 1475 |
/* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR |
| 1476 |
* from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are |
| 1477 |
* appropriately set. */ |
| 1478 |
|
| 1479 |
if (key->type == DNS_TYPE_DS) { |
| 1480 |
if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA)) |
| 1481 |
return -EBADMSG; |
| 1482 |
} else { |
| 1483 |
if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_NS) && |
| 1484 |
!bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA)) |
| 1485 |
return -EBADMSG; |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */ |
| 1489 |
if (bitmap_isset(enclosure_rr->nsec3.types, key->type)) |
| 1490 |
*result = DNSSEC_NSEC_FOUND; |
| 1491 |
else if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_CNAME)) |
| 1492 |
*result = DNSSEC_NSEC_CNAME; |
| 1493 |
else |
| 1494 |
*result = DNSSEC_NSEC_NODATA; |
| 1495 |
|
| 1496 |
if (authenticated) |
| 1497 |
*authenticated = a; |
| 1498 |
if (ttl) |
| 1499 |
*ttl = enclosure_rr->ttl; |
| 1500 |
|
| 1501 |
return 0; |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */ |
| 1505 |
if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_DNAME)) |
| 1506 |
return -EBADMSG; |
| 1507 |
|
| 1508 |
/* Ensure that this data is from the delegated domain |
| 1509 |
* (i.e. originates from the "lower" DNS server), and isn't |
| 1510 |
* just glue records (i.e. doesn't originate from the "upper" |
| 1511 |
* DNS server). */ |
| 1512 |
if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_NS) && |
| 1513 |
!bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA)) |
| 1514 |
return -EBADMSG; |
| 1515 |
|
| 1516 |
/* Prove that there is no next closer and whether or not there is a wildcard domain. */ |
| 1517 |
|
| 1518 |
wildcard = strjoina("*.", p); |
| 1519 |
r = nsec3_hashed_domain_make(enclosure_rr, wildcard, zone, &wildcard_domain); |
| 1520 |
if (r < 0) |
| 1521 |
return r; |
| 1522 |
if (r != hashed_size) |
| 1523 |
return -EBADMSG; |
| 1524 |
|
| 1525 |
r = nsec3_hashed_domain_make(enclosure_rr, pp, zone, &next_closer_domain); |
| 1526 |
if (r < 0) |
| 1527 |
return r; |
| 1528 |
if (r != hashed_size) |
| 1529 |
return -EBADMSG; |
| 1530 |
|
| 1531 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { |
| 1532 |
_cleanup_free_ char *next_hashed_domain = NULL; |
| 1533 |
|
| 1534 |
r = nsec3_is_good(rr, zone_rr); |
| 1535 |
if (r < 0) |
| 1536 |
return r; |
| 1537 |
if (r == 0) |
| 1538 |
continue; |
| 1539 |
|
| 1540 |
r = nsec3_hashed_domain_format(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, zone, &next_hashed_domain); |
| 1541 |
if (r < 0) |
| 1542 |
return r; |
| 1543 |
|
| 1544 |
r = dns_name_between(dns_resource_key_name(rr->key), next_closer_domain, next_hashed_domain); |
| 1545 |
if (r < 0) |
| 1546 |
return r; |
| 1547 |
if (r > 0) { |
| 1548 |
if (rr->nsec3.flags & 1) |
| 1549 |
optout = true; |
| 1550 |
|
| 1551 |
a = a && (flags & DNS_ANSWER_AUTHENTICATED); |
| 1552 |
|
| 1553 |
no_closer = true; |
| 1554 |
} |
| 1555 |
|
| 1556 |
r = dns_name_equal(dns_resource_key_name(rr->key), wildcard_domain); |
| 1557 |
if (r < 0) |
| 1558 |
return r; |
| 1559 |
if (r > 0) { |
| 1560 |
a = a && (flags & DNS_ANSWER_AUTHENTICATED); |
| 1561 |
|
| 1562 |
wildcard_rr = rr; |
| 1563 |
} |
| 1564 |
|
| 1565 |
r = dns_name_between(dns_resource_key_name(rr->key), wildcard_domain, next_hashed_domain); |
| 1566 |
if (r < 0) |
| 1567 |
return r; |
| 1568 |
if (r > 0) { |
| 1569 |
if (rr->nsec3.flags & 1) |
| 1570 |
/* This only makes sense if we have a wildcard delegation, which is |
| 1571 |
* very unlikely, see RFC 4592, Section 4.2, but we cannot rely on |
| 1572 |
* this not happening, so hence cannot simply conclude NXDOMAIN as |
| 1573 |
* we would wish */ |
| 1574 |
optout = true; |
| 1575 |
|
| 1576 |
a = a && (flags & DNS_ANSWER_AUTHENTICATED); |
| 1577 |
|
| 1578 |
no_wildcard = true; |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
if (wildcard_rr && no_wildcard) |
| 1583 |
return -EBADMSG; |
| 1584 |
|
| 1585 |
if (!no_closer) { |
| 1586 |
*result = DNSSEC_NSEC_NO_RR; |
| 1587 |
return 0; |
| 1588 |
} |
| 1589 |
|
| 1590 |
if (wildcard_rr) { |
| 1591 |
/* A wildcard exists that matches our query. */ |
| 1592 |
if (optout) |
| 1593 |
/* This is not specified in any RFC to the best of my knowledge, but |
| 1594 |
* if the next closer enclosure is covered by an opt-out NSEC3 RR |
| 1595 |
* it means that we cannot prove that the source of synthesis is |
| 1596 |
* correct, as there may be a closer match. */ |
| 1597 |
*result = DNSSEC_NSEC_OPTOUT; |
| 1598 |
else if (bitmap_isset(wildcard_rr->nsec3.types, key->type)) |
| 1599 |
*result = DNSSEC_NSEC_FOUND; |
| 1600 |
else if (bitmap_isset(wildcard_rr->nsec3.types, DNS_TYPE_CNAME)) |
| 1601 |
*result = DNSSEC_NSEC_CNAME; |
| 1602 |
else |
| 1603 |
*result = DNSSEC_NSEC_NODATA; |
| 1604 |
} else { |
| 1605 |
if (optout) |
| 1606 |
/* The RFC only specifies that we have to care for optout for NODATA for |
| 1607 |
* DS records. However, children of an insecure opt-out delegation should |
| 1608 |
* also be considered opt-out, rather than verified NXDOMAIN. |
| 1609 |
* Note that we do not require a proof of wildcard non-existence if the |
| 1610 |
* next closer domain is covered by an opt-out, as that would not provide |
| 1611 |
* any additional information. */ |
| 1612 |
*result = DNSSEC_NSEC_OPTOUT; |
| 1613 |
else if (no_wildcard) |
| 1614 |
*result = DNSSEC_NSEC_NXDOMAIN; |
| 1615 |
else { |
| 1616 |
*result = DNSSEC_NSEC_NO_RR; |
| 1617 |
|
| 1618 |
return 0; |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
if (authenticated) |
| 1623 |
*authenticated = a; |
| 1624 |
|
| 1625 |
if (ttl) |
| 1626 |
*ttl = enclosure_rr->ttl; |
| 1627 |
|
| 1628 |
return 0; |
| 1629 |
} |
| 1630 |
|
| 1631 |
static int dnssec_nsec_wildcard_equal(DnsResourceRecord *rr, const char *name) { |
| 1632 |
char label[DNS_LABEL_MAX+1]; |
| 1633 |
const char *n; |
| 1634 |
int r; |
| 1635 |
|
| 1636 |
assert(rr); |
| 1637 |
assert(rr->key->type == DNS_TYPE_NSEC); |
| 1638 |
|
| 1639 |
/* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */ |
| 1640 |
|
| 1641 |
if (rr->n_skip_labels_source != 1) |
| 1642 |
return 0; |
| 1643 |
|
| 1644 |
n = dns_resource_key_name(rr->key); |
| 1645 |
r = dns_label_unescape(&n, label, sizeof label, 0); |
| 1646 |
if (r <= 0) |
| 1647 |
return r; |
| 1648 |
if (r != 1 || label[0] != '*') |
| 1649 |
return 0; |
| 1650 |
|
| 1651 |
return dns_name_endswith(name, n); |
| 1652 |
} |
| 1653 |
|
| 1654 |
static int dnssec_nsec_in_path(DnsResourceRecord *rr, const char *name) { |
| 1655 |
const char *nn, *common_suffix; |
| 1656 |
int r; |
| 1657 |
|
| 1658 |
assert(rr); |
| 1659 |
assert(rr->key->type == DNS_TYPE_NSEC); |
| 1660 |
|
| 1661 |
/* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT) |
| 1662 |
* |
| 1663 |
* A couple of examples: |
| 1664 |
* |
| 1665 |
* NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT |
| 1666 |
* NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs |
| 1667 |
* NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs |
| 1668 |
*/ |
| 1669 |
|
| 1670 |
/* First, determine parent of next domain. */ |
| 1671 |
nn = rr->nsec.next_domain_name; |
| 1672 |
r = dns_name_parent(&nn); |
| 1673 |
if (r <= 0) |
| 1674 |
return r; |
| 1675 |
|
| 1676 |
/* If the name we just determined is not equal or child of the name we are interested in, then we can't say |
| 1677 |
* anything at all. */ |
| 1678 |
r = dns_name_endswith(nn, name); |
| 1679 |
if (r <= 0) |
| 1680 |
return r; |
| 1681 |
|
| 1682 |
/* If the name we are interested in is not a prefix of the common suffix of the NSEC RR's owner and next domain names, then we can't say anything either. */ |
| 1683 |
r = dns_name_common_suffix(dns_resource_key_name(rr->key), rr->nsec.next_domain_name, &common_suffix); |
| 1684 |
if (r < 0) |
| 1685 |
return r; |
| 1686 |
|
| 1687 |
return dns_name_endswith(name, common_suffix); |
| 1688 |
} |
| 1689 |
|
| 1690 |
static int dnssec_nsec_from_parent_zone(DnsResourceRecord *rr, const char *name) { |
| 1691 |
int r; |
| 1692 |
|
| 1693 |
assert(rr); |
| 1694 |
assert(rr->key->type == DNS_TYPE_NSEC); |
| 1695 |
|
| 1696 |
/* Checks whether this NSEC originates to the parent zone or the child zone. */ |
| 1697 |
|
| 1698 |
r = dns_name_parent(&name); |
| 1699 |
if (r <= 0) |
| 1700 |
return r; |
| 1701 |
|
| 1702 |
r = dns_name_equal(name, dns_resource_key_name(rr->key)); |
| 1703 |
if (r <= 0) |
| 1704 |
return r; |
| 1705 |
|
| 1706 |
/* DNAME, and NS without SOA is an indication for a delegation. */ |
| 1707 |
if (bitmap_isset(rr->nsec.types, DNS_TYPE_DNAME)) |
| 1708 |
return 1; |
| 1709 |
|
| 1710 |
if (bitmap_isset(rr->nsec.types, DNS_TYPE_NS) && !bitmap_isset(rr->nsec.types, DNS_TYPE_SOA)) |
| 1711 |
return 1; |
| 1712 |
|
| 1713 |
return 0; |
| 1714 |
} |
| 1715 |
|
| 1716 |
static int dnssec_nsec_covers(DnsResourceRecord *rr, const char *name) { |
| 1717 |
const char *signer; |
| 1718 |
int r; |
| 1719 |
|
| 1720 |
assert(rr); |
| 1721 |
assert(rr->key->type == DNS_TYPE_NSEC); |
| 1722 |
|
| 1723 |
/* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's |
| 1724 |
* signer name, and between the NSEC's two names. */ |
| 1725 |
|
| 1726 |
r = dns_resource_record_signer(rr, &signer); |
| 1727 |
if (r < 0) |
| 1728 |
return r; |
| 1729 |
|
| 1730 |
r = dns_name_endswith(name, signer); /* this NSEC isn't suitable the name is not in the signer's domain */ |
| 1731 |
if (r <= 0) |
| 1732 |
return r; |
| 1733 |
|
| 1734 |
return dns_name_between(dns_resource_key_name(rr->key), name, rr->nsec.next_domain_name); |
| 1735 |
} |
| 1736 |
|
| 1737 |
static int dnssec_nsec_generate_wildcard(DnsResourceRecord *rr, const char *name, char **wc) { |
| 1738 |
const char *common_suffix1, *common_suffix2, *signer; |
| 1739 |
int r, labels1, labels2; |
| 1740 |
|
| 1741 |
assert(rr); |
| 1742 |
assert(rr->key->type == DNS_TYPE_NSEC); |
| 1743 |
|
| 1744 |
/* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */ |
| 1745 |
|
| 1746 |
r = dns_resource_record_signer(rr, &signer); |
| 1747 |
if (r < 0) |
| 1748 |
return r; |
| 1749 |
|
| 1750 |
r = dns_name_endswith(name, signer); /* this NSEC isn't suitable the name is not in the signer's domain */ |
| 1751 |
if (r <= 0) |
| 1752 |
return r; |
| 1753 |
|
| 1754 |
r = dns_name_common_suffix(name, dns_resource_key_name(rr->key), &common_suffix1); |
| 1755 |
if (r < 0) |
| 1756 |
return r; |
| 1757 |
|
| 1758 |
r = dns_name_common_suffix(name, rr->nsec.next_domain_name, &common_suffix2); |
| 1759 |
if (r < 0) |
| 1760 |
return r; |
| 1761 |
|
| 1762 |
labels1 = dns_name_count_labels(common_suffix1); |
| 1763 |
if (labels1 < 0) |
| 1764 |
return labels1; |
| 1765 |
|
| 1766 |
labels2 = dns_name_count_labels(common_suffix2); |
| 1767 |
if (labels2 < 0) |
| 1768 |
return labels2; |
| 1769 |
|
| 1770 |
if (labels1 > labels2) |
| 1771 |
r = dns_name_concat("*", common_suffix1, 0, wc); |
| 1772 |
else |
| 1773 |
r = dns_name_concat("*", common_suffix2, 0, wc); |
| 1774 |
|
| 1775 |
if (r < 0) |
| 1776 |
return r; |
| 1777 |
|
| 1778 |
return 0; |
| 1779 |
} |
| 1780 |
|
| 1781 |
int dnssec_nsec_test(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) { |
| 1782 |
bool have_nsec3 = false, covering_rr_authenticated = false, wildcard_rr_authenticated = false; |
| 1783 |
DnsResourceRecord *rr, *covering_rr = NULL, *wildcard_rr = NULL; |
| 1784 |
DnsAnswerFlags flags; |
| 1785 |
const char *name; |
| 1786 |
int r; |
| 1787 |
|
| 1788 |
assert(key); |
| 1789 |
assert(result); |
| 1790 |
|
| 1791 |
/* Look for any NSEC/NSEC3 RRs that say something about the specified key. */ |
| 1792 |
|
| 1793 |
name = dns_resource_key_name(key); |
| 1794 |
|
| 1795 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { |
| 1796 |
|
| 1797 |
if (rr->key->class != key->class) |
| 1798 |
continue; |
| 1799 |
|
| 1800 |
have_nsec3 = have_nsec3 || (rr->key->type == DNS_TYPE_NSEC3); |
| 1801 |
|
| 1802 |
if (rr->key->type != DNS_TYPE_NSEC) |
| 1803 |
continue; |
| 1804 |
|
| 1805 |
/* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */ |
| 1806 |
r = dns_resource_record_is_synthetic(rr); |
| 1807 |
if (r == -ENODATA) /* No signing RR known. */ |
| 1808 |
continue; |
| 1809 |
if (r < 0) |
| 1810 |
return r; |
| 1811 |
if (r > 0) |
| 1812 |
continue; |
| 1813 |
|
| 1814 |
/* Check if this is a direct match. If so, we have encountered a NODATA case */ |
| 1815 |
r = dns_name_equal(dns_resource_key_name(rr->key), name); |
| 1816 |
if (r < 0) |
| 1817 |
return r; |
| 1818 |
if (r == 0) { |
| 1819 |
/* If it's not a direct match, maybe it's a wild card match? */ |
| 1820 |
r = dnssec_nsec_wildcard_equal(rr, name); |
| 1821 |
if (r < 0) |
| 1822 |
return r; |
| 1823 |
} |
| 1824 |
if (r > 0) { |
| 1825 |
if (key->type == DNS_TYPE_DS) { |
| 1826 |
/* If we look for a DS RR and the server sent us the NSEC RR of the child zone |
| 1827 |
* we have a problem. For DS RRs we want the NSEC RR from the parent */ |
| 1828 |
if (bitmap_isset(rr->nsec.types, DNS_TYPE_SOA)) |
| 1829 |
continue; |
| 1830 |
} else { |
| 1831 |
/* For all RR types, ensure that if NS is set SOA is set too, so that we know |
| 1832 |
* we got the child's NSEC. */ |
| 1833 |
if (bitmap_isset(rr->nsec.types, DNS_TYPE_NS) && |
| 1834 |
!bitmap_isset(rr->nsec.types, DNS_TYPE_SOA)) |
| 1835 |
continue; |
| 1836 |
} |
| 1837 |
|
| 1838 |
if (bitmap_isset(rr->nsec.types, key->type)) |
| 1839 |
*result = DNSSEC_NSEC_FOUND; |
| 1840 |
else if (bitmap_isset(rr->nsec.types, DNS_TYPE_CNAME)) |
| 1841 |
*result = DNSSEC_NSEC_CNAME; |
| 1842 |
else |
| 1843 |
*result = DNSSEC_NSEC_NODATA; |
| 1844 |
|
| 1845 |
if (authenticated) |
| 1846 |
*authenticated = flags & DNS_ANSWER_AUTHENTICATED; |
| 1847 |
if (ttl) |
| 1848 |
*ttl = rr->ttl; |
| 1849 |
|
| 1850 |
return 0; |
| 1851 |
} |
| 1852 |
|
| 1853 |
/* Check if the name we are looking for is an empty non-terminal within the owner or next name |
| 1854 |
* of the NSEC RR. */ |
| 1855 |
r = dnssec_nsec_in_path(rr, name); |
| 1856 |
if (r < 0) |
| 1857 |
return r; |
| 1858 |
if (r > 0) { |
| 1859 |
*result = DNSSEC_NSEC_NODATA; |
| 1860 |
|
| 1861 |
if (authenticated) |
| 1862 |
*authenticated = flags & DNS_ANSWER_AUTHENTICATED; |
| 1863 |
if (ttl) |
| 1864 |
*ttl = rr->ttl; |
| 1865 |
|
| 1866 |
return 0; |
| 1867 |
} |
| 1868 |
|
| 1869 |
/* The following two "covering" checks, are not useful if the NSEC is from the parent */ |
| 1870 |
r = dnssec_nsec_from_parent_zone(rr, name); |
| 1871 |
if (r < 0) |
| 1872 |
return r; |
| 1873 |
if (r > 0) |
| 1874 |
continue; |
| 1875 |
|
| 1876 |
/* Check if this NSEC RR proves the absence of an explicit RR under this name */ |
| 1877 |
r = dnssec_nsec_covers(rr, name); |
| 1878 |
if (r < 0) |
| 1879 |
return r; |
| 1880 |
if (r > 0 && (!covering_rr || !covering_rr_authenticated)) { |
| 1881 |
covering_rr = rr; |
| 1882 |
covering_rr_authenticated = flags & DNS_ANSWER_AUTHENTICATED; |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
if (covering_rr) { |
| 1887 |
_cleanup_free_ char *wc = NULL; |
| 1888 |
r = dnssec_nsec_generate_wildcard(covering_rr, name, &wc); |
| 1889 |
if (r < 0) |
| 1890 |
return r; |
| 1891 |
|
| 1892 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { |
| 1893 |
|
| 1894 |
if (rr->key->class != key->class) |
| 1895 |
continue; |
| 1896 |
|
| 1897 |
if (rr->key->type != DNS_TYPE_NSEC) |
| 1898 |
continue; |
| 1899 |
|
| 1900 |
/* Check if this NSEC RR proves the nonexistence of the wildcard */ |
| 1901 |
r = dnssec_nsec_covers(rr, wc); |
| 1902 |
if (r < 0) |
| 1903 |
return r; |
| 1904 |
if (r > 0 && (!wildcard_rr || !wildcard_rr_authenticated)) { |
| 1905 |
wildcard_rr = rr; |
| 1906 |
wildcard_rr_authenticated = flags & DNS_ANSWER_AUTHENTICATED; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
} |
| 1910 |
|
| 1911 |
if (covering_rr && wildcard_rr) { |
| 1912 |
/* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we |
| 1913 |
* proved the NXDOMAIN case. */ |
| 1914 |
*result = DNSSEC_NSEC_NXDOMAIN; |
| 1915 |
|
| 1916 |
if (authenticated) |
| 1917 |
*authenticated = covering_rr_authenticated && wildcard_rr_authenticated; |
| 1918 |
if (ttl) |
| 1919 |
*ttl = MIN(covering_rr->ttl, wildcard_rr->ttl); |
| 1920 |
|
| 1921 |
return 0; |
| 1922 |
} |
| 1923 |
|
| 1924 |
/* OK, this was not sufficient. Let's see if NSEC3 can help. */ |
| 1925 |
if (have_nsec3) |
| 1926 |
return dnssec_test_nsec3(answer, key, result, authenticated, ttl); |
| 1927 |
|
| 1928 |
/* No appropriate NSEC RR found, report this. */ |
| 1929 |
*result = DNSSEC_NSEC_NO_RR; |
| 1930 |
return 0; |
| 1931 |
} |
| 1932 |
|
| 1933 |
static int dnssec_nsec_test_enclosed(DnsAnswer *answer, uint16_t type, const char *name, const char *zone, bool *authenticated) { |
| 1934 |
DnsResourceRecord *rr; |
| 1935 |
DnsAnswerFlags flags; |
| 1936 |
int r; |
| 1937 |
|
| 1938 |
assert(name); |
| 1939 |
assert(zone); |
| 1940 |
|
| 1941 |
/* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified |
| 1942 |
* 'zone'. The 'zone' must be a suffix of the 'name'. */ |
| 1943 |
|
| 1944 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { |
| 1945 |
bool found = false; |
| 1946 |
|
| 1947 |
if (rr->key->type != type && type != DNS_TYPE_ANY) |
| 1948 |
continue; |
| 1949 |
|
| 1950 |
switch (rr->key->type) { |
| 1951 |
|
| 1952 |
case DNS_TYPE_NSEC: |
| 1953 |
|
| 1954 |
/* We only care for NSEC RRs from the indicated zone */ |
| 1955 |
r = dns_resource_record_is_signer(rr, zone); |
| 1956 |
if (r < 0) |
| 1957 |
return r; |
| 1958 |
if (r == 0) |
| 1959 |
continue; |
| 1960 |
|
| 1961 |
r = dns_name_between(dns_resource_key_name(rr->key), name, rr->nsec.next_domain_name); |
| 1962 |
if (r < 0) |
| 1963 |
return r; |
| 1964 |
|
| 1965 |
found = r > 0; |
| 1966 |
break; |
| 1967 |
|
| 1968 |
case DNS_TYPE_NSEC3: { |
| 1969 |
_cleanup_free_ char *hashed_domain = NULL, *next_hashed_domain = NULL; |
| 1970 |
|
| 1971 |
/* We only care for NSEC3 RRs from the indicated zone */ |
| 1972 |
r = dns_resource_record_is_signer(rr, zone); |
| 1973 |
if (r < 0) |
| 1974 |
return r; |
| 1975 |
if (r == 0) |
| 1976 |
continue; |
| 1977 |
|
| 1978 |
r = nsec3_is_good(rr, NULL); |
| 1979 |
if (r < 0) |
| 1980 |
return r; |
| 1981 |
if (r == 0) |
| 1982 |
break; |
| 1983 |
|
| 1984 |
/* Format the domain we are testing with the NSEC3 RR's hash function */ |
| 1985 |
r = nsec3_hashed_domain_make( |
| 1986 |
rr, |
| 1987 |
name, |
| 1988 |
zone, |
| 1989 |
&hashed_domain); |
| 1990 |
if (r < 0) |
| 1991 |
return r; |
| 1992 |
if ((size_t) r != rr->nsec3.next_hashed_name_size) |
| 1993 |
break; |
| 1994 |
|
| 1995 |
/* Format the NSEC3's next hashed name as proper domain name */ |
| 1996 |
r = nsec3_hashed_domain_format( |
| 1997 |
rr->nsec3.next_hashed_name, |
| 1998 |
rr->nsec3.next_hashed_name_size, |
| 1999 |
zone, |
| 2000 |
&next_hashed_domain); |
| 2001 |
if (r < 0) |
| 2002 |
return r; |
| 2003 |
|
| 2004 |
r = dns_name_between(dns_resource_key_name(rr->key), hashed_domain, next_hashed_domain); |
| 2005 |
if (r < 0) |
| 2006 |
return r; |
| 2007 |
|
| 2008 |
found = r > 0; |
| 2009 |
break; |
| 2010 |
} |
| 2011 |
|
| 2012 |
default: |
| 2013 |
continue; |
| 2014 |
} |
| 2015 |
|
| 2016 |
if (found) { |
| 2017 |
if (authenticated) |
| 2018 |
*authenticated = flags & DNS_ANSWER_AUTHENTICATED; |
| 2019 |
return 1; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
return 0; |
| 2024 |
} |
| 2025 |
|
| 2026 |
static int dnssec_test_positive_wildcard_nsec3( |
| 2027 |
DnsAnswer *answer, |
| 2028 |
const char *name, |
| 2029 |
const char *source, |
| 2030 |
const char *zone, |
| 2031 |
bool *authenticated) { |
| 2032 |
|
| 2033 |
const char *next_closer = NULL; |
| 2034 |
int r; |
| 2035 |
|
| 2036 |
/* Run a positive NSEC3 wildcard proof. Specifically: |
| 2037 |
* |
| 2038 |
* A proof that the "next closer" of the generating wildcard does not exist. |
| 2039 |
* |
| 2040 |
* Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for |
| 2041 |
* empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name |
| 2042 |
* exists for the NSEC3 RR and we are done. |
| 2043 |
* |
| 2044 |
* To prove that a.b.c.d.e.f is rightfully synthesized from a wildcard *.d.e.f all we have to check is that |
| 2045 |
* c.d.e.f does not exist. */ |
| 2046 |
|
| 2047 |
for (;;) { |
| 2048 |
next_closer = name; |
| 2049 |
r = dns_name_parent(&name); |
| 2050 |
if (r <= 0) |
| 2051 |
return r; |
| 2052 |
|
| 2053 |
r = dns_name_equal(name, source); |
| 2054 |
if (r < 0) |
| 2055 |
return r; |
| 2056 |
if (r > 0) |
| 2057 |
break; |
| 2058 |
} |
| 2059 |
|
| 2060 |
return dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC3, next_closer, zone, authenticated); |
| 2061 |
} |
| 2062 |
|
| 2063 |
static int dnssec_test_positive_wildcard_nsec( |
| 2064 |
DnsAnswer *answer, |
| 2065 |
const char *name, |
| 2066 |
const char *source, |
| 2067 |
const char *zone, |
| 2068 |
bool *_authenticated) { |
| 2069 |
|
| 2070 |
bool authenticated = true; |
| 2071 |
int r; |
| 2072 |
|
| 2073 |
assert(_authenticated); |
| 2074 |
|
| 2075 |
/* Run a positive NSEC wildcard proof. Specifically: |
| 2076 |
* |
| 2077 |
* A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and |
| 2078 |
* a prefix of the synthesizing source "source" in the zone "zone". |
| 2079 |
* |
| 2080 |
* See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4 |
| 2081 |
* |
| 2082 |
* Note that if we want to prove that a.b.c.d.e.f is rightfully synthesized from a wildcard *.d.e.f, then we |
| 2083 |
* have to prove that none of the following exist: |
| 2084 |
* |
| 2085 |
* 1) a.b.c.d.e.f |
| 2086 |
* 2) *.b.c.d.e.f |
| 2087 |
* 3) b.c.d.e.f |
| 2088 |
* 4) *.c.d.e.f |
| 2089 |
* 5) c.d.e.f |
| 2090 |
*/ |
| 2091 |
|
| 2092 |
for (;;) { |
| 2093 |
_cleanup_free_ char *wc = NULL; |
| 2094 |
bool a = false; |
| 2095 |
|
| 2096 |
/* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing, |
| 2097 |
* i.e between the owner name and the next name of an NSEC RR. */ |
| 2098 |
r = dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC, name, zone, &a); |
| 2099 |
if (r <= 0) |
| 2100 |
return r; |
| 2101 |
|
| 2102 |
authenticated = authenticated && a; |
| 2103 |
|
| 2104 |
/* Strip one label off */ |
| 2105 |
r = dns_name_parent(&name); |
| 2106 |
if (r <= 0) |
| 2107 |
return r; |
| 2108 |
|
| 2109 |
/* Did we reach the source of synthesis? */ |
| 2110 |
r = dns_name_equal(name, source); |
| 2111 |
if (r < 0) |
| 2112 |
return r; |
| 2113 |
if (r > 0) { |
| 2114 |
/* Successful exit */ |
| 2115 |
*_authenticated = authenticated; |
| 2116 |
return 1; |
| 2117 |
} |
| 2118 |
|
| 2119 |
/* Safety check, that the source of synthesis is still our suffix */ |
| 2120 |
r = dns_name_endswith(name, source); |
| 2121 |
if (r < 0) |
| 2122 |
return r; |
| 2123 |
if (r == 0) |
| 2124 |
return -EBADMSG; |
| 2125 |
|
| 2126 |
/* Replace the label we stripped off with an asterisk */ |
| 2127 |
wc = strjoin("*.", name); |
| 2128 |
if (!wc) |
| 2129 |
return -ENOMEM; |
| 2130 |
|
| 2131 |
/* And check if the proof holds for the asterisk name, too */ |
| 2132 |
r = dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC, wc, zone, &a); |
| 2133 |
if (r <= 0) |
| 2134 |
return r; |
| 2135 |
|
| 2136 |
authenticated = authenticated && a; |
| 2137 |
/* In the next iteration we'll check the non-asterisk-prefixed version */ |
| 2138 |
} |
| 2139 |
} |
| 2140 |
|
| 2141 |
int dnssec_test_positive_wildcard( |
| 2142 |
DnsAnswer *answer, |
| 2143 |
const char *name, |
| 2144 |
const char *source, |
| 2145 |
const char *zone, |
| 2146 |
bool *authenticated) { |
| 2147 |
|
| 2148 |
int r; |
| 2149 |
|
| 2150 |
assert(name); |
| 2151 |
assert(source); |
| 2152 |
assert(zone); |
| 2153 |
assert(authenticated); |
| 2154 |
|
| 2155 |
r = dns_answer_contains_zone_nsec3(answer, zone); |
| 2156 |
if (r < 0) |
| 2157 |
return r; |
| 2158 |
if (r > 0) |
| 2159 |
return dnssec_test_positive_wildcard_nsec3(answer, name, source, zone, authenticated); |
| 2160 |
else |
| 2161 |
return dnssec_test_positive_wildcard_nsec(answer, name, source, zone, authenticated); |
| 2162 |
} |
| 2163 |
|
| 2164 |
#else |
| 2165 |
|
| 2166 |
int dnssec_verify_rrset( |
| 2167 |
DnsAnswer *a, |
| 2168 |
const DnsResourceKey *key, |
| 2169 |
DnsResourceRecord *rrsig, |
| 2170 |
DnsResourceRecord *dnskey, |
| 2171 |
usec_t realtime, |
| 2172 |
DnssecResult *result) { |
| 2173 |
|
| 2174 |
return -EOPNOTSUPP; |
| 2175 |
} |
| 2176 |
|
| 2177 |
int dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) { |
| 2178 |
|
| 2179 |
return -EOPNOTSUPP; |
| 2180 |
} |
| 2181 |
|
| 2182 |
int dnssec_key_match_rrsig(const DnsResourceKey *key, DnsResourceRecord *rrsig) { |
| 2183 |
|
| 2184 |
return -EOPNOTSUPP; |
| 2185 |
} |
| 2186 |
|
| 2187 |
int dnssec_verify_rrset_search( |
| 2188 |
DnsAnswer *a, |
| 2189 |
const DnsResourceKey *key, |
| 2190 |
DnsAnswer *validated_dnskeys, |
| 2191 |
usec_t realtime, |
| 2192 |
DnssecResult *result, |
| 2193 |
DnsResourceRecord **ret_rrsig) { |
| 2194 |
|
| 2195 |
return -EOPNOTSUPP; |
| 2196 |
} |
| 2197 |
|
| 2198 |
int dnssec_has_rrsig(DnsAnswer *a, const DnsResourceKey *key) { |
| 2199 |
|
| 2200 |
return -EOPNOTSUPP; |
| 2201 |
} |
| 2202 |
|
| 2203 |
int dnssec_verify_dnskey_by_ds(DnsResourceRecord *dnskey, DnsResourceRecord *ds, bool mask_revoke) { |
| 2204 |
|
| 2205 |
return -EOPNOTSUPP; |
| 2206 |
} |
| 2207 |
|
| 2208 |
int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord *dnskey, DnsAnswer *validated_ds) { |
| 2209 |
|
| 2210 |
return -EOPNOTSUPP; |
| 2211 |
} |
| 2212 |
|
| 2213 |
int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) { |
| 2214 |
|
| 2215 |
return -EOPNOTSUPP; |
| 2216 |
} |
| 2217 |
|
| 2218 |
int dnssec_nsec_test(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) { |
| 2219 |
|
| 2220 |
return -EOPNOTSUPP; |
| 2221 |
} |
| 2222 |
|
| 2223 |
int dnssec_test_positive_wildcard( |
| 2224 |
DnsAnswer *answer, |
| 2225 |
const char *name, |
| 2226 |
const char *source, |
| 2227 |
const char *zone, |
| 2228 |
bool *authenticated) { |
| 2229 |
|
| 2230 |
return -EOPNOTSUPP; |
| 2231 |
} |
| 2232 |
|
| 2233 |
#endif |
| 2234 |
|
| 2235 |
static const char* const dnssec_result_table[_DNSSEC_RESULT_MAX] = { |
| 2236 |
[DNSSEC_VALIDATED] = "validated", |
| 2237 |
[DNSSEC_VALIDATED_WILDCARD] = "validated-wildcard", |
| 2238 |
[DNSSEC_INVALID] = "invalid", |
| 2239 |
[DNSSEC_SIGNATURE_EXPIRED] = "signature-expired", |
| 2240 |
[DNSSEC_UNSUPPORTED_ALGORITHM] = "unsupported-algorithm", |
| 2241 |
[DNSSEC_NO_SIGNATURE] = "no-signature", |
| 2242 |
[DNSSEC_MISSING_KEY] = "missing-key", |
| 2243 |
[DNSSEC_UNSIGNED] = "unsigned", |
| 2244 |
[DNSSEC_FAILED_AUXILIARY] = "failed-auxiliary", |
| 2245 |
[DNSSEC_NSEC_MISMATCH] = "nsec-mismatch", |
| 2246 |
[DNSSEC_INCOMPATIBLE_SERVER] = "incompatible-server", |
| 2247 |
[DNSSEC_UPSTREAM_FAILURE] = "upstream-failure", |
| 2248 |
[DNSSEC_TOO_MANY_VALIDATIONS] = "too-many-validations", |
| 2249 |
}; |
| 2250 |
DEFINE_STRING_TABLE_LOOKUP(dnssec_result, DnssecResult); |
| 2251 |
|
| 2252 |
static const char* const dnssec_verdict_table[_DNSSEC_VERDICT_MAX] = { |
| 2253 |
[DNSSEC_SECURE] = "secure", |
| 2254 |
[DNSSEC_INSECURE] = "insecure", |
| 2255 |
[DNSSEC_BOGUS] = "bogus", |
| 2256 |
[DNSSEC_INDETERMINATE] = "indeterminate", |
| 2257 |
}; |
| 2258 |
DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict, DnssecVerdict); |