LCOV - code coverage report
Current view: top level - resolve - resolved-dns-trust-anchor.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 0 399 0.0 %
Date: 2019-08-23 13:36:53 Functions: 0 18 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 413 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include "sd-messages.h"
       4                 :            : 
       5                 :            : #include "alloc-util.h"
       6                 :            : #include "conf-files.h"
       7                 :            : #include "def.h"
       8                 :            : #include "dns-domain.h"
       9                 :            : #include "fd-util.h"
      10                 :            : #include "fileio.h"
      11                 :            : #include "hexdecoct.h"
      12                 :            : #include "nulstr-util.h"
      13                 :            : #include "parse-util.h"
      14                 :            : #include "resolved-dns-dnssec.h"
      15                 :            : #include "resolved-dns-trust-anchor.h"
      16                 :            : #include "set.h"
      17                 :            : #include "sort-util.h"
      18                 :            : #include "string-util.h"
      19                 :            : #include "strv.h"
      20                 :            : 
      21                 :            : static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
      22                 :            : 
      23                 :            : /* The first DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */
      24                 :            : static const uint8_t root_digest1[] =
      25                 :            :         { 0x49, 0xAA, 0xC1, 0x1D, 0x7B, 0x6F, 0x64, 0x46, 0x70, 0x2E, 0x54, 0xA1, 0x60, 0x73, 0x71, 0x60,
      26                 :            :           0x7A, 0x1A, 0x41, 0x85, 0x52, 0x00, 0xFD, 0x2C, 0xE1, 0xCD, 0xDE, 0x32, 0xF2, 0x4E, 0x8F, 0xB5 };
      27                 :            : 
      28                 :            : /* The second DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved February 2017 */
      29                 :            : static const uint8_t root_digest2[] =
      30                 :            :         { 0xE0, 0x6D, 0x44, 0xB8, 0x0B, 0x8F, 0x1D, 0x39, 0xA9, 0x5C, 0x0B, 0x0D, 0x7C, 0x65, 0xD0, 0x84,
      31                 :            :           0x58, 0xE8, 0x80, 0x40, 0x9B, 0xBC, 0x68, 0x34, 0x57, 0x10, 0x42, 0x37, 0xC7, 0xF8, 0xEC, 0x8D };
      32                 :            : 
      33                 :          0 : static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
      34         [ #  # ]:          0 :         assert(d);
      35                 :            : 
      36                 :            :         /* Returns true if there's an entry for the specified domain
      37                 :            :          * name in our trust anchor */
      38                 :            : 
      39                 :            :         return
      40   [ #  #  #  # ]:          0 :                 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, name)) ||
      41                 :          0 :                 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name));
      42                 :            : }
      43                 :            : 
      44                 :          0 : static int add_root_ksk(
      45                 :            :                 DnsAnswer *answer,
      46                 :            :                 DnsResourceKey *key,
      47                 :            :                 uint16_t key_tag,
      48                 :            :                 uint8_t algorithm,
      49                 :            :                 uint8_t digest_type,
      50                 :            :                 const void *digest,
      51                 :            :                 size_t digest_size) {
      52                 :            : 
      53                 :          0 :         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
      54                 :            :         int r;
      55                 :            : 
      56                 :          0 :         rr = dns_resource_record_new(key);
      57         [ #  # ]:          0 :         if (!rr)
      58                 :          0 :                 return -ENOMEM;
      59                 :            : 
      60                 :          0 :         rr->ds.key_tag = key_tag;
      61                 :          0 :         rr->ds.algorithm = algorithm;
      62                 :          0 :         rr->ds.digest_type = digest_type;
      63                 :          0 :         rr->ds.digest_size = digest_size;
      64                 :          0 :         rr->ds.digest = memdup(digest, rr->ds.digest_size);
      65         [ #  # ]:          0 :         if (!rr->ds.digest)
      66                 :          0 :                 return  -ENOMEM;
      67                 :            : 
      68                 :          0 :         r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
      69         [ #  # ]:          0 :         if (r < 0)
      70                 :          0 :                 return r;
      71                 :            : 
      72                 :          0 :         return 0;
      73                 :            : }
      74                 :            : 
      75                 :          0 : static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor *d) {
      76                 :          0 :         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
      77                 :          0 :         _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
      78                 :            :         int r;
      79                 :            : 
      80         [ #  # ]:          0 :         assert(d);
      81                 :            : 
      82                 :          0 :         r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
      83         [ #  # ]:          0 :         if (r < 0)
      84                 :          0 :                 return r;
      85                 :            : 
      86                 :            :         /* Only add the built-in trust anchor if there's neither a DS nor a DNSKEY defined for the root domain. That
      87                 :            :          * way users have an easy way to override the root domain DS/DNSKEY data. */
      88         [ #  # ]:          0 :         if (dns_trust_anchor_knows_domain_positive(d, "."))
      89                 :          0 :                 return 0;
      90                 :            : 
      91                 :          0 :         key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DS, "");
      92         [ #  # ]:          0 :         if (!key)
      93                 :          0 :                 return -ENOMEM;
      94                 :            : 
      95                 :          0 :         answer = dns_answer_new(2);
      96         [ #  # ]:          0 :         if (!answer)
      97                 :          0 :                 return -ENOMEM;
      98                 :            : 
      99                 :            :         /* Add the two RRs from https://data.iana.org/root-anchors/root-anchors.xml */
     100                 :          0 :         r = add_root_ksk(answer, key, 19036, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest1, sizeof(root_digest1));
     101         [ #  # ]:          0 :         if (r < 0)
     102                 :          0 :                 return r;
     103                 :            : 
     104                 :          0 :         r = add_root_ksk(answer, key, 20326, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest2, sizeof(root_digest2));
     105         [ #  # ]:          0 :         if (r < 0)
     106                 :          0 :                 return r;
     107                 :            : 
     108                 :          0 :         r = hashmap_put(d->positive_by_key, key, answer);
     109         [ #  # ]:          0 :         if (r < 0)
     110                 :          0 :                 return r;
     111                 :            : 
     112                 :          0 :         answer = NULL;
     113                 :          0 :         return 0;
     114                 :            : }
     115                 :            : 
     116                 :          0 : static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
     117                 :            : 
     118                 :            :         static const char private_domains[] =
     119                 :            :                 /* RFC 6761 says that .test is a special domain for
     120                 :            :                  * testing and not to be installed in the root zone */
     121                 :            :                 "test\0"
     122                 :            : 
     123                 :            :                 /* RFC 6761 says that these reverse IP lookup ranges
     124                 :            :                  * are for private addresses, and hence should not
     125                 :            :                  * show up in the root zone */
     126                 :            :                 "10.in-addr.arpa\0"
     127                 :            :                 "16.172.in-addr.arpa\0"
     128                 :            :                 "17.172.in-addr.arpa\0"
     129                 :            :                 "18.172.in-addr.arpa\0"
     130                 :            :                 "19.172.in-addr.arpa\0"
     131                 :            :                 "20.172.in-addr.arpa\0"
     132                 :            :                 "21.172.in-addr.arpa\0"
     133                 :            :                 "22.172.in-addr.arpa\0"
     134                 :            :                 "23.172.in-addr.arpa\0"
     135                 :            :                 "24.172.in-addr.arpa\0"
     136                 :            :                 "25.172.in-addr.arpa\0"
     137                 :            :                 "26.172.in-addr.arpa\0"
     138                 :            :                 "27.172.in-addr.arpa\0"
     139                 :            :                 "28.172.in-addr.arpa\0"
     140                 :            :                 "29.172.in-addr.arpa\0"
     141                 :            :                 "30.172.in-addr.arpa\0"
     142                 :            :                 "31.172.in-addr.arpa\0"
     143                 :            :                 "168.192.in-addr.arpa\0"
     144                 :            : 
     145                 :            :                 /* The same, but for IPv6. */
     146                 :            :                 "d.f.ip6.arpa\0"
     147                 :            : 
     148                 :            :                 /* RFC 6762 reserves the .local domain for Multicast
     149                 :            :                  * DNS, it hence cannot appear in the root zone. (Note
     150                 :            :                  * that we by default do not route .local traffic to
     151                 :            :                  * DNS anyway, except when a configured search domain
     152                 :            :                  * suggests so.) */
     153                 :            :                 "local\0"
     154                 :            : 
     155                 :            :                 /* These two are well known, popular private zone
     156                 :            :                  * TLDs, that are blocked from delegation, according
     157                 :            :                  * to:
     158                 :            :                  * http://icannwiki.com/Name_Collision#NGPC_Resolution
     159                 :            :                  *
     160                 :            :                  * There's also ongoing work on making this official
     161                 :            :                  * in an RRC:
     162                 :            :                  * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
     163                 :            :                 "home\0"
     164                 :            :                 "corp\0"
     165                 :            : 
     166                 :            :                 /* The following four TLDs are suggested for private
     167                 :            :                  * zones in RFC 6762, Appendix G, and are hence very
     168                 :            :                  * unlikely to be made official TLDs any day soon */
     169                 :            :                 "lan\0"
     170                 :            :                 "intranet\0"
     171                 :            :                 "internal\0"
     172                 :            :                 "private\0";
     173                 :            : 
     174                 :            :         const char *name;
     175                 :            :         int r;
     176                 :            : 
     177         [ #  # ]:          0 :         assert(d);
     178                 :            : 
     179                 :            :         /* Only add the built-in trust anchor if there's no negative
     180                 :            :          * trust anchor defined at all. This enables easy overriding
     181                 :            :          * of negative trust anchors. */
     182                 :            : 
     183         [ #  # ]:          0 :         if (set_size(d->negative_by_name) > 0)
     184                 :          0 :                 return 0;
     185                 :            : 
     186                 :          0 :         r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
     187         [ #  # ]:          0 :         if (r < 0)
     188                 :          0 :                 return r;
     189                 :            : 
     190                 :            :         /* We add a couple of domains as default negative trust
     191                 :            :          * anchors, where it's very unlikely they will be installed in
     192                 :            :          * the root zone. If they exist they must be private, and thus
     193                 :            :          * unsigned. */
     194                 :            : 
     195   [ #  #  #  # ]:          0 :         NULSTR_FOREACH(name, private_domains) {
     196                 :            : 
     197         [ #  # ]:          0 :                 if (dns_trust_anchor_knows_domain_positive(d, name))
     198                 :          0 :                         continue;
     199                 :            : 
     200                 :          0 :                 r = set_put_strdup(d->negative_by_name, name);
     201         [ #  # ]:          0 :                 if (r < 0)
     202                 :          0 :                         return r;
     203                 :            :         }
     204                 :            : 
     205                 :          0 :         return 0;
     206                 :            : }
     207                 :            : 
     208                 :          0 : static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
     209                 :          0 :         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
     210                 :          0 :         _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
     211                 :          0 :         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
     212                 :          0 :         DnsAnswer *old_answer = NULL;
     213                 :          0 :         const char *p = s;
     214                 :            :         int r;
     215                 :            : 
     216         [ #  # ]:          0 :         assert(d);
     217         [ #  # ]:          0 :         assert(line);
     218                 :            : 
     219                 :          0 :         r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
     220         [ #  # ]:          0 :         if (r < 0)
     221         [ #  # ]:          0 :                 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
     222                 :            : 
     223                 :          0 :         r = dns_name_is_valid(domain);
     224         [ #  # ]:          0 :         if (r < 0)
     225         [ #  # ]:          0 :                 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
     226         [ #  # ]:          0 :         if (r == 0) {
     227         [ #  # ]:          0 :                 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
     228                 :          0 :                 return -EINVAL;
     229                 :            :         }
     230                 :            : 
     231                 :          0 :         r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
     232         [ #  # ]:          0 :         if (r < 0)
     233         [ #  # ]:          0 :                 return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
     234         [ #  # ]:          0 :         if (r != 2) {
     235         [ #  # ]:          0 :                 log_warning("Missing class or type in line %s:%u", path, line);
     236                 :          0 :                 return -EINVAL;
     237                 :            :         }
     238                 :            : 
     239         [ #  # ]:          0 :         if (!strcaseeq(class, "IN")) {
     240         [ #  # ]:          0 :                 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
     241                 :          0 :                 return -EINVAL;
     242                 :            :         }
     243                 :            : 
     244         [ #  # ]:          0 :         if (strcaseeq(type, "DS")) {
     245   [ #  #  #  #  :          0 :                 _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL;
                   #  # ]
     246         [ #  # ]:          0 :                 _cleanup_free_ void *dd = NULL;
     247                 :            :                 uint16_t kt;
     248                 :            :                 int a, dt;
     249                 :            :                 size_t l;
     250                 :            : 
     251                 :          0 :                 r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, NULL);
     252         [ #  # ]:          0 :                 if (r < 0) {
     253         [ #  # ]:          0 :                         log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
     254                 :          0 :                         return -EINVAL;
     255                 :            :                 }
     256         [ #  # ]:          0 :                 if (r != 3) {
     257         [ #  # ]:          0 :                         log_warning("Missing DS parameters on line %s:%u", path, line);
     258                 :          0 :                         return -EINVAL;
     259                 :            :                 }
     260                 :            : 
     261                 :          0 :                 r = safe_atou16(key_tag, &kt);
     262         [ #  # ]:          0 :                 if (r < 0)
     263         [ #  # ]:          0 :                         return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
     264                 :            : 
     265                 :          0 :                 a = dnssec_algorithm_from_string(algorithm);
     266         [ #  # ]:          0 :                 if (a < 0) {
     267         [ #  # ]:          0 :                         log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
     268                 :          0 :                         return -EINVAL;
     269                 :            :                 }
     270                 :            : 
     271                 :          0 :                 dt = dnssec_digest_from_string(digest_type);
     272         [ #  # ]:          0 :                 if (dt < 0) {
     273         [ #  # ]:          0 :                         log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
     274                 :          0 :                         return -EINVAL;
     275                 :            :                 }
     276                 :            : 
     277         [ #  # ]:          0 :                 if (isempty(p)) {
     278         [ #  # ]:          0 :                         log_warning("Missing DS digest on line %s:%u", path, line);
     279                 :          0 :                         return -EINVAL;
     280                 :            :                 }
     281                 :            : 
     282                 :          0 :                 r = unhexmem(p, strlen(p), &dd, &l);
     283         [ #  # ]:          0 :                 if (r < 0) {
     284         [ #  # ]:          0 :                         log_warning("Failed to parse DS digest %s on line %s:%u", p, path, line);
     285                 :          0 :                         return -EINVAL;
     286                 :            :                 }
     287                 :            : 
     288                 :          0 :                 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
     289         [ #  # ]:          0 :                 if (!rr)
     290                 :          0 :                         return log_oom();
     291                 :            : 
     292                 :          0 :                 rr->ds.key_tag = kt;
     293                 :          0 :                 rr->ds.algorithm = a;
     294                 :          0 :                 rr->ds.digest_type = dt;
     295                 :          0 :                 rr->ds.digest_size = l;
     296                 :          0 :                 rr->ds.digest = TAKE_PTR(dd);
     297                 :            : 
     298         [ #  # ]:          0 :         } else if (strcaseeq(type, "DNSKEY")) {
     299   [ #  #  #  #  :          0 :                 _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL;
                   #  # ]
     300         [ #  # ]:          0 :                 _cleanup_free_ void *k = NULL;
     301                 :            :                 uint16_t f;
     302                 :            :                 size_t l;
     303                 :            :                 int a;
     304                 :            : 
     305                 :          0 :                 r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, NULL);
     306         [ #  # ]:          0 :                 if (r < 0)
     307         [ #  # ]:          0 :                         return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
     308         [ #  # ]:          0 :                 if (r != 3) {
     309         [ #  # ]:          0 :                         log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
     310                 :          0 :                         return -EINVAL;
     311                 :            :                 }
     312                 :            : 
     313         [ #  # ]:          0 :                 if (!streq(protocol, "3")) {
     314         [ #  # ]:          0 :                         log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
     315                 :          0 :                         return -EINVAL;
     316                 :            :                 }
     317                 :            : 
     318                 :          0 :                 r = safe_atou16(flags, &f);
     319         [ #  # ]:          0 :                 if (r < 0)
     320         [ #  # ]:          0 :                         return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line);
     321         [ #  # ]:          0 :                 if ((f & DNSKEY_FLAG_ZONE_KEY) == 0) {
     322         [ #  # ]:          0 :                         log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line);
     323                 :          0 :                         return -EINVAL;
     324                 :            :                 }
     325         [ #  # ]:          0 :                 if ((f & DNSKEY_FLAG_REVOKE)) {
     326         [ #  # ]:          0 :                         log_warning("DNSKEY is already revoked on line %s:%u", path, line);
     327                 :          0 :                         return -EINVAL;
     328                 :            :                 }
     329                 :            : 
     330                 :          0 :                 a = dnssec_algorithm_from_string(algorithm);
     331         [ #  # ]:          0 :                 if (a < 0) {
     332         [ #  # ]:          0 :                         log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
     333                 :          0 :                         return -EINVAL;
     334                 :            :                 }
     335                 :            : 
     336         [ #  # ]:          0 :                 if (isempty(p)) {
     337         [ #  # ]:          0 :                         log_warning("Missing DNSKEY key on line %s:%u", path, line);
     338                 :          0 :                         return -EINVAL;
     339                 :            :                 }
     340                 :            : 
     341                 :          0 :                 r = unbase64mem(p, strlen(p), &k, &l);
     342         [ #  # ]:          0 :                 if (r < 0)
     343         [ #  # ]:          0 :                         return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", p, path, line);
     344                 :            : 
     345                 :          0 :                 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
     346         [ #  # ]:          0 :                 if (!rr)
     347                 :          0 :                         return log_oom();
     348                 :            : 
     349                 :          0 :                 rr->dnskey.flags = f;
     350                 :          0 :                 rr->dnskey.protocol = 3;
     351                 :          0 :                 rr->dnskey.algorithm = a;
     352                 :          0 :                 rr->dnskey.key_size = l;
     353                 :          0 :                 rr->dnskey.key = TAKE_PTR(k);
     354                 :            : 
     355                 :            :         } else {
     356         [ #  # ]:          0 :                 log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
     357                 :          0 :                 return -EINVAL;
     358                 :            :         }
     359                 :            : 
     360                 :          0 :         r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
     361         [ #  # ]:          0 :         if (r < 0)
     362                 :          0 :                 return log_oom();
     363                 :            : 
     364                 :          0 :         old_answer = hashmap_get(d->positive_by_key, rr->key);
     365                 :          0 :         answer = dns_answer_ref(old_answer);
     366                 :            : 
     367                 :          0 :         r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
     368         [ #  # ]:          0 :         if (r < 0)
     369         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to add trust anchor RR: %m");
     370                 :            : 
     371                 :          0 :         r = hashmap_replace(d->positive_by_key, rr->key, answer);
     372         [ #  # ]:          0 :         if (r < 0)
     373         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to add answer to trust anchor: %m");
     374                 :            : 
     375                 :          0 :         old_answer = dns_answer_unref(old_answer);
     376                 :          0 :         answer = NULL;
     377                 :            : 
     378                 :          0 :         return 0;
     379                 :            : }
     380                 :            : 
     381                 :          0 : static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
     382                 :          0 :         _cleanup_free_ char *domain = NULL;
     383                 :          0 :         const char *p = s;
     384                 :            :         int r;
     385                 :            : 
     386         [ #  # ]:          0 :         assert(d);
     387         [ #  # ]:          0 :         assert(line);
     388                 :            : 
     389                 :          0 :         r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
     390         [ #  # ]:          0 :         if (r < 0)
     391         [ #  # ]:          0 :                 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
     392                 :            : 
     393                 :          0 :         r = dns_name_is_valid(domain);
     394         [ #  # ]:          0 :         if (r < 0)
     395         [ #  # ]:          0 :                 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
     396         [ #  # ]:          0 :         if (r == 0) {
     397         [ #  # ]:          0 :                 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
     398                 :          0 :                 return -EINVAL;
     399                 :            :         }
     400                 :            : 
     401         [ #  # ]:          0 :         if (!isempty(p)) {
     402         [ #  # ]:          0 :                 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
     403                 :          0 :                 return -EINVAL;
     404                 :            :         }
     405                 :            : 
     406                 :          0 :         r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
     407         [ #  # ]:          0 :         if (r < 0)
     408                 :          0 :                 return log_oom();
     409                 :            : 
     410                 :          0 :         r = set_put(d->negative_by_name, domain);
     411         [ #  # ]:          0 :         if (r < 0)
     412                 :          0 :                 return log_oom();
     413         [ #  # ]:          0 :         if (r > 0)
     414                 :          0 :                 domain = NULL;
     415                 :            : 
     416                 :          0 :         return 0;
     417                 :            : }
     418                 :            : 
     419                 :          0 : static int dns_trust_anchor_load_files(
     420                 :            :                 DnsTrustAnchor *d,
     421                 :            :                 const char *suffix,
     422                 :            :                 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
     423                 :            : 
     424                 :          0 :         _cleanup_strv_free_ char **files = NULL;
     425                 :            :         char **f;
     426                 :            :         int r;
     427                 :            : 
     428         [ #  # ]:          0 :         assert(d);
     429         [ #  # ]:          0 :         assert(suffix);
     430         [ #  # ]:          0 :         assert(loader);
     431                 :            : 
     432                 :          0 :         r = conf_files_list_nulstr(&files, suffix, NULL, 0, trust_anchor_dirs);
     433         [ #  # ]:          0 :         if (r < 0)
     434         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
     435                 :            : 
     436   [ #  #  #  # ]:          0 :         STRV_FOREACH(f, files) {
     437         [ #  # ]:          0 :                 _cleanup_fclose_ FILE *g = NULL;
     438                 :          0 :                 unsigned n = 0;
     439                 :            : 
     440                 :          0 :                 g = fopen(*f, "r");
     441         [ #  # ]:          0 :                 if (!g) {
     442         [ #  # ]:          0 :                         if (errno == ENOENT)
     443                 :          0 :                                 continue;
     444                 :            : 
     445         [ #  # ]:          0 :                         log_warning_errno(errno, "Failed to open '%s', ignoring: %m", *f);
     446                 :          0 :                         continue;
     447                 :            :                 }
     448                 :            : 
     449                 :          0 :                 for (;;) {
     450      [ #  #  # ]:          0 :                         _cleanup_free_ char *line = NULL;
     451                 :            :                         char *l;
     452                 :            : 
     453                 :          0 :                         r = read_line(g, LONG_LINE_MAX, &line);
     454         [ #  # ]:          0 :                         if (r < 0) {
     455         [ #  # ]:          0 :                                 log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f);
     456                 :          0 :                                 break;
     457                 :            :                         }
     458         [ #  # ]:          0 :                         if (r == 0)
     459                 :          0 :                                 break;
     460                 :            : 
     461                 :          0 :                         n++;
     462                 :            : 
     463                 :          0 :                         l = strstrip(line);
     464         [ #  # ]:          0 :                         if (isempty(l))
     465                 :          0 :                                 continue;
     466                 :            : 
     467         [ #  # ]:          0 :                         if (*l == ';')
     468                 :          0 :                                 continue;
     469                 :            : 
     470                 :          0 :                         (void) loader(d, *f, n, l);
     471                 :            :                 }
     472                 :            :         }
     473                 :            : 
     474                 :          0 :         return 0;
     475                 :            : }
     476                 :            : 
     477                 :          0 : static int domain_name_cmp(char * const *a, char * const *b) {
     478                 :          0 :         return dns_name_compare_func(*a, *b);
     479                 :            : }
     480                 :            : 
     481                 :          0 : static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
     482                 :            :         DnsAnswer *a;
     483                 :            :         Iterator i;
     484                 :            : 
     485         [ #  # ]:          0 :         assert(d);
     486                 :            : 
     487         [ #  # ]:          0 :         if (hashmap_isempty(d->positive_by_key))
     488         [ #  # ]:          0 :                 log_info("No positive trust anchors defined.");
     489                 :            :         else {
     490         [ #  # ]:          0 :                 log_info("Positive Trust Anchors:");
     491         [ #  # ]:          0 :                 HASHMAP_FOREACH(a, d->positive_by_key, i) {
     492                 :            :                         DnsResourceRecord *rr;
     493                 :            : 
     494   [ #  #  #  #  :          0 :                         DNS_ANSWER_FOREACH(rr, a)
             #  #  #  # ]
     495   [ #  #  #  # ]:          0 :                                 log_info("%s", dns_resource_record_to_string(rr));
     496                 :            :                 }
     497                 :            :         }
     498                 :            : 
     499         [ #  # ]:          0 :         if (set_isempty(d->negative_by_name))
     500         [ #  # ]:          0 :                 log_info("No negative trust anchors defined.");
     501                 :            :         else {
     502   [ #  #  #  # ]:          0 :                 _cleanup_free_ char **l = NULL, *j = NULL;
     503                 :            : 
     504                 :          0 :                 l = set_get_strv(d->negative_by_name);
     505         [ #  # ]:          0 :                 if (!l)
     506                 :          0 :                         return log_oom();
     507                 :            : 
     508                 :          0 :                 typesafe_qsort(l, set_size(d->negative_by_name), domain_name_cmp);
     509                 :            : 
     510                 :          0 :                 j = strv_join(l, " ");
     511         [ #  # ]:          0 :                 if (!j)
     512                 :          0 :                         return log_oom();
     513                 :            : 
     514         [ #  # ]:          0 :                 log_info("Negative trust anchors: %s", j);
     515                 :            :         }
     516                 :            : 
     517                 :          0 :         return 0;
     518                 :            : }
     519                 :            : 
     520                 :          0 : int dns_trust_anchor_load(DnsTrustAnchor *d) {
     521                 :            :         int r;
     522                 :            : 
     523         [ #  # ]:          0 :         assert(d);
     524                 :            : 
     525                 :            :         /* If loading things from disk fails, we don't consider this fatal */
     526                 :          0 :         (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
     527                 :          0 :         (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
     528                 :            : 
     529                 :            :         /* However, if the built-in DS fails, then we have a problem. */
     530                 :          0 :         r = dns_trust_anchor_add_builtin_positive(d);
     531         [ #  # ]:          0 :         if (r < 0)
     532         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
     533                 :            : 
     534                 :          0 :         r = dns_trust_anchor_add_builtin_negative(d);
     535         [ #  # ]:          0 :         if (r < 0)
     536         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
     537                 :            : 
     538                 :          0 :         dns_trust_anchor_dump(d);
     539                 :            : 
     540                 :          0 :         return 0;
     541                 :            : }
     542                 :            : 
     543                 :          0 : void dns_trust_anchor_flush(DnsTrustAnchor *d) {
     544         [ #  # ]:          0 :         assert(d);
     545                 :            : 
     546         [ #  # ]:          0 :         d->positive_by_key = hashmap_free_with_destructor(d->positive_by_key, dns_answer_unref);
     547         [ #  # ]:          0 :         d->revoked_by_rr = set_free_with_destructor(d->revoked_by_rr, dns_resource_record_unref);
     548                 :          0 :         d->negative_by_name = set_free_free(d->negative_by_name);
     549                 :          0 : }
     550                 :            : 
     551                 :          0 : int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
     552                 :            :         DnsAnswer *a;
     553                 :            : 
     554         [ #  # ]:          0 :         assert(d);
     555         [ #  # ]:          0 :         assert(key);
     556         [ #  # ]:          0 :         assert(ret);
     557                 :            : 
     558                 :            :         /* We only serve DS and DNSKEY RRs. */
     559   [ #  #  #  # ]:          0 :         if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
     560                 :          0 :                 return 0;
     561                 :            : 
     562                 :          0 :         a = hashmap_get(d->positive_by_key, key);
     563         [ #  # ]:          0 :         if (!a)
     564                 :          0 :                 return 0;
     565                 :            : 
     566                 :          0 :         *ret = dns_answer_ref(a);
     567                 :          0 :         return 1;
     568                 :            : }
     569                 :            : 
     570                 :          0 : int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
     571                 :            :         int r;
     572                 :            : 
     573         [ #  # ]:          0 :         assert(d);
     574         [ #  # ]:          0 :         assert(name);
     575                 :            : 
     576                 :            :         for (;;) {
     577                 :            :                 /* If the domain is listed as-is in the NTA database, then that counts */
     578         [ #  # ]:          0 :                 if (set_contains(d->negative_by_name, name))
     579                 :          0 :                         return true;
     580                 :            : 
     581                 :            :                 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
     582                 :            :                  * 7646, section 1.1 */
     583         [ #  # ]:          0 :                 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
     584                 :          0 :                         return false;
     585                 :            : 
     586         [ #  # ]:          0 :                 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
     587                 :          0 :                         return false;
     588                 :            : 
     589                 :            :                 /* And now, let's look at the parent, and check that too */
     590                 :          0 :                 r = dns_name_parent(&name);
     591         [ #  # ]:          0 :                 if (r < 0)
     592                 :          0 :                         return r;
     593         [ #  # ]:          0 :                 if (r == 0)
     594                 :          0 :                         break;
     595                 :            :         }
     596                 :            : 
     597                 :          0 :         return false;
     598                 :            : }
     599                 :            : 
     600                 :          0 : static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
     601                 :            :         int r;
     602                 :            : 
     603         [ #  # ]:          0 :         assert(d);
     604                 :            : 
     605                 :          0 :         r = set_ensure_allocated(&d->revoked_by_rr, &dns_resource_record_hash_ops);
     606         [ #  # ]:          0 :         if (r < 0)
     607                 :          0 :                 return r;
     608                 :            : 
     609                 :          0 :         r = set_put(d->revoked_by_rr, rr);
     610         [ #  # ]:          0 :         if (r < 0)
     611                 :          0 :                 return r;
     612         [ #  # ]:          0 :         if (r > 0)
     613                 :          0 :                 dns_resource_record_ref(rr);
     614                 :            : 
     615                 :          0 :         return r;
     616                 :            : }
     617                 :            : 
     618                 :          0 : static int dns_trust_anchor_remove_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
     619                 :          0 :         _cleanup_(dns_answer_unrefp) DnsAnswer *new_answer = NULL;
     620                 :            :         DnsAnswer *old_answer;
     621                 :            :         int r;
     622                 :            : 
     623                 :            :         /* Remember that this is a revoked trust anchor RR */
     624                 :          0 :         r = dns_trust_anchor_revoked_put(d, rr);
     625         [ #  # ]:          0 :         if (r < 0)
     626                 :          0 :                 return r;
     627                 :            : 
     628                 :            :         /* Remove this from the positive trust anchor */
     629                 :          0 :         old_answer = hashmap_get(d->positive_by_key, rr->key);
     630         [ #  # ]:          0 :         if (!old_answer)
     631                 :          0 :                 return 0;
     632                 :            : 
     633                 :          0 :         new_answer = dns_answer_ref(old_answer);
     634                 :            : 
     635                 :          0 :         r = dns_answer_remove_by_rr(&new_answer, rr);
     636         [ #  # ]:          0 :         if (r <= 0)
     637                 :          0 :                 return r;
     638                 :            : 
     639                 :            :         /* We found the key! Warn the user */
     640                 :          0 :         log_struct(LOG_WARNING,
     641                 :            :                    "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR,
     642                 :            :                    LOG_MESSAGE("DNSSEC trust anchor %s has been revoked.\n"
     643                 :            :                                "Please update the trust anchor, or upgrade your operating system.",
     644                 :            :                                strna(dns_resource_record_to_string(rr))),
     645                 :            :                    "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr));
     646                 :            : 
     647         [ #  # ]:          0 :         if (dns_answer_size(new_answer) <= 0) {
     648         [ #  # ]:          0 :                 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
     649                 :          0 :                 dns_answer_unref(old_answer);
     650                 :          0 :                 return 1;
     651                 :            :         }
     652                 :            : 
     653                 :          0 :         r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer);
     654         [ #  # ]:          0 :         if (r < 0)
     655                 :          0 :                 return r;
     656                 :            : 
     657                 :          0 :         new_answer = NULL;
     658                 :          0 :         dns_answer_unref(old_answer);
     659                 :          0 :         return 1;
     660                 :            : }
     661                 :            : 
     662                 :          0 : static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
     663                 :            :         DnsAnswer *a;
     664                 :            :         int r;
     665                 :            : 
     666         [ #  # ]:          0 :         assert(d);
     667         [ #  # ]:          0 :         assert(revoked_dnskey);
     668         [ #  # ]:          0 :         assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
     669         [ #  # ]:          0 :         assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
     670                 :            : 
     671                 :          0 :         a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
     672         [ #  # ]:          0 :         if (a) {
     673                 :            :                 DnsResourceRecord *anchor;
     674                 :            : 
     675                 :            :                 /* First, look for the precise DNSKEY in our trust anchor database */
     676                 :            : 
     677   [ #  #  #  #  :          0 :                 DNS_ANSWER_FOREACH(anchor, a) {
          #  #  #  #  #  
                      # ]
     678                 :            : 
     679         [ #  # ]:          0 :                         if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
     680                 :          0 :                                 continue;
     681                 :            : 
     682         [ #  # ]:          0 :                         if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
     683                 :          0 :                                 continue;
     684                 :            : 
     685         [ #  # ]:          0 :                         if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
     686                 :          0 :                                 continue;
     687                 :            : 
     688                 :            :                         /* Note that we allow the REVOKE bit to be
     689                 :            :                          * different! It will be set in the revoked
     690                 :            :                          * key, but unset in our version of it */
     691         [ #  # ]:          0 :                         if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
     692                 :          0 :                                 continue;
     693                 :            : 
     694         [ #  # ]:          0 :                         if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
     695                 :          0 :                                 continue;
     696                 :            : 
     697                 :          0 :                         dns_trust_anchor_remove_revoked(d, anchor);
     698                 :          0 :                         break;
     699                 :            :                 }
     700                 :            :         }
     701                 :            : 
     702                 :          0 :         a = hashmap_get(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(revoked_dnskey->key->class, DNS_TYPE_DS, dns_resource_key_name(revoked_dnskey->key)));
     703         [ #  # ]:          0 :         if (a) {
     704                 :            :                 DnsResourceRecord *anchor;
     705                 :            : 
     706                 :            :                 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
     707                 :            : 
     708   [ #  #  #  #  :          0 :                 DNS_ANSWER_FOREACH(anchor, a) {
             #  #  #  # ]
     709                 :            : 
     710                 :            :                         /* We set mask_revoke to true here, since our
     711                 :            :                          * DS fingerprint will be the one of the
     712                 :            :                          * unrevoked DNSKEY, but the one we got passed
     713                 :            :                          * here has the bit set. */
     714                 :          0 :                         r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
     715         [ #  # ]:          0 :                         if (r < 0)
     716                 :          0 :                                 return r;
     717         [ #  # ]:          0 :                         if (r == 0)
     718         [ #  # ]:          0 :                                 continue;
     719                 :            : 
     720                 :          0 :                         dns_trust_anchor_remove_revoked(d, anchor);
     721                 :          0 :                         break;
     722                 :            :                 }
     723                 :            :         }
     724                 :            : 
     725                 :          0 :         return 0;
     726                 :            : }
     727                 :            : 
     728                 :          0 : int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
     729                 :            :         DnsResourceRecord *rrsig;
     730                 :            :         int r;
     731                 :            : 
     732         [ #  # ]:          0 :         assert(d);
     733         [ #  # ]:          0 :         assert(dnskey);
     734                 :            : 
     735                 :            :         /* Looks if "dnskey" is a self-signed RR that has been revoked
     736                 :            :          * and matches one of our trust anchor entries. If so, removes
     737                 :            :          * it from the trust anchor and returns > 0. */
     738                 :            : 
     739         [ #  # ]:          0 :         if (dnskey->key->type != DNS_TYPE_DNSKEY)
     740                 :          0 :                 return 0;
     741                 :            : 
     742                 :            :         /* Is this DNSKEY revoked? */
     743         [ #  # ]:          0 :         if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
     744                 :          0 :                 return 0;
     745                 :            : 
     746                 :            :         /* Could this be interesting to us at all? If not,
     747                 :            :          * there's no point in looking for and verifying a
     748                 :            :          * self-signed RRSIG. */
     749         [ #  # ]:          0 :         if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
     750                 :          0 :                 return 0;
     751                 :            : 
     752                 :            :         /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
     753   [ #  #  #  #  :          0 :         DNS_ANSWER_FOREACH(rrsig, rrs) {
             #  #  #  # ]
     754                 :            :                 DnssecResult result;
     755                 :            : 
     756         [ #  # ]:          0 :                 if (rrsig->key->type != DNS_TYPE_RRSIG)
     757         [ #  # ]:          0 :                         continue;
     758                 :            : 
     759                 :          0 :                 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
     760         [ #  # ]:          0 :                 if (r < 0)
     761                 :          0 :                         return r;
     762         [ #  # ]:          0 :                 if (r == 0)
     763                 :          0 :                         continue;
     764                 :            : 
     765                 :          0 :                 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
     766         [ #  # ]:          0 :                 if (r < 0)
     767                 :          0 :                         return r;
     768         [ #  # ]:          0 :                 if (result != DNSSEC_VALIDATED)
     769                 :          0 :                         continue;
     770                 :            : 
     771                 :            :                 /* Bingo! This is a revoked self-signed DNSKEY. Let's
     772                 :            :                  * see if this precise one exists in our trust anchor
     773                 :            :                  * database, too. */
     774                 :          0 :                 r = dns_trust_anchor_check_revoked_one(d, dnskey);
     775         [ #  # ]:          0 :                 if (r < 0)
     776                 :          0 :                         return r;
     777                 :            : 
     778                 :          0 :                 return 1;
     779                 :            :         }
     780                 :            : 
     781                 :          0 :         return 0;
     782                 :            : }
     783                 :            : 
     784                 :          0 : int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
     785         [ #  # ]:          0 :         assert(d);
     786                 :            : 
     787   [ #  #  #  # ]:          0 :         if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
     788                 :          0 :                 return 0;
     789                 :            : 
     790                 :          0 :         return set_contains(d->revoked_by_rr, rr);
     791                 :            : }

Generated by: LCOV version 1.14