"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/dns/rdata/generic/hip_55.c" (4 Sep 2020, 11802 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hip_55.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.17.3_vs_9.17.4.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /* RFC 5205 */
   13 
   14 #ifndef RDATA_GENERIC_HIP_5_C
   15 #define RDATA_GENERIC_HIP_5_C
   16 
   17 #define RRTYPE_HIP_ATTRIBUTES (0)
   18 
   19 static inline isc_result_t
   20 fromtext_hip(ARGS_FROMTEXT) {
   21     isc_token_t token;
   22     dns_name_t name;
   23     isc_buffer_t buffer;
   24     isc_buffer_t hit_len;
   25     isc_buffer_t key_len;
   26     unsigned char *start;
   27     size_t len;
   28 
   29     REQUIRE(type == dns_rdatatype_hip);
   30 
   31     UNUSED(type);
   32     UNUSED(rdclass);
   33     UNUSED(callbacks);
   34 
   35     /*
   36      * Dummy HIT len.
   37      */
   38     hit_len = *target;
   39     RETERR(uint8_tobuffer(0, target));
   40 
   41     /*
   42      * Algorithm.
   43      */
   44     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   45                       false));
   46     if (token.value.as_ulong > 0xffU) {
   47         RETTOK(ISC_R_RANGE);
   48     }
   49     RETERR(uint8_tobuffer(token.value.as_ulong, target));
   50 
   51     /*
   52      * Dummy KEY len.
   53      */
   54     key_len = *target;
   55     RETERR(uint16_tobuffer(0, target));
   56 
   57     /*
   58      * HIT (base16).
   59      */
   60     start = isc_buffer_used(target);
   61     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   62                       false));
   63     RETTOK(isc_hex_decodestring(DNS_AS_STR(token), target));
   64 
   65     /*
   66      * Fill in HIT len.
   67      */
   68     len = (unsigned char *)isc_buffer_used(target) - start;
   69     if (len > 0xffU) {
   70         RETTOK(ISC_R_RANGE);
   71     }
   72     RETERR(uint8_tobuffer((uint32_t)len, &hit_len));
   73 
   74     /*
   75      * Public key (base64).
   76      */
   77     start = isc_buffer_used(target);
   78     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   79                       false));
   80     RETTOK(isc_base64_decodestring(DNS_AS_STR(token), target));
   81 
   82     /*
   83      * Fill in KEY len.
   84      */
   85     len = (unsigned char *)isc_buffer_used(target) - start;
   86     if (len > 0xffffU) {
   87         RETTOK(ISC_R_RANGE);
   88     }
   89     RETERR(uint16_tobuffer((uint32_t)len, &key_len));
   90 
   91     if (origin == NULL) {
   92         origin = dns_rootname;
   93     }
   94 
   95     /*
   96      * Rendezvous Servers.
   97      */
   98     dns_name_init(&name, NULL);
   99     do {
  100         RETERR(isc_lex_getmastertoken(lexer, &token,
  101                           isc_tokentype_string, true));
  102         if (token.type != isc_tokentype_string) {
  103             break;
  104         }
  105         buffer_fromregion(&buffer, &token.value.as_region);
  106         RETTOK(dns_name_fromtext(&name, &buffer, origin, options,
  107                      target));
  108     } while (1);
  109 
  110     /*
  111      * Let upper layer handle eol/eof.
  112      */
  113     isc_lex_ungettoken(lexer, &token);
  114 
  115     return (ISC_R_SUCCESS);
  116 }
  117 
  118 static inline isc_result_t
  119 totext_hip(ARGS_TOTEXT) {
  120     isc_region_t region;
  121     dns_name_t name;
  122     unsigned int length, key_len, hit_len;
  123     unsigned char algorithm;
  124     char buf[sizeof("225 ")];
  125 
  126     REQUIRE(rdata->type == dns_rdatatype_hip);
  127     REQUIRE(rdata->length != 0);
  128 
  129     dns_rdata_toregion(rdata, &region);
  130 
  131     hit_len = uint8_fromregion(&region);
  132     isc_region_consume(&region, 1);
  133 
  134     algorithm = uint8_fromregion(&region);
  135     isc_region_consume(&region, 1);
  136 
  137     key_len = uint16_fromregion(&region);
  138     isc_region_consume(&region, 2);
  139 
  140     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  141         RETERR(str_totext("( ", target));
  142     }
  143 
  144     /*
  145      * Algorithm
  146      */
  147     snprintf(buf, sizeof(buf), "%u ", algorithm);
  148     RETERR(str_totext(buf, target));
  149 
  150     /*
  151      * HIT.
  152      */
  153     INSIST(hit_len < region.length);
  154     length = region.length;
  155     region.length = hit_len;
  156     RETERR(isc_hex_totext(&region, 1, "", target));
  157     region.length = length - hit_len;
  158     RETERR(str_totext(tctx->linebreak, target));
  159 
  160     /*
  161      * Public KEY.
  162      */
  163     INSIST(key_len <= region.length);
  164     length = region.length;
  165     region.length = key_len;
  166     RETERR(isc_base64_totext(&region, 1, "", target));
  167     region.length = length - key_len;
  168     if (region.length > 0) {
  169         RETERR(str_totext(tctx->linebreak, target));
  170     }
  171 
  172     /*
  173      * Rendezvous Servers.
  174      */
  175     dns_name_init(&name, NULL);
  176     while (region.length > 0) {
  177         dns_name_fromregion(&name, &region);
  178 
  179         RETERR(dns_name_totext(&name, false, target));
  180         isc_region_consume(&region, name.length);
  181         if (region.length > 0) {
  182             RETERR(str_totext(tctx->linebreak, target));
  183         }
  184     }
  185     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  186         RETERR(str_totext(" )", target));
  187     }
  188     return (ISC_R_SUCCESS);
  189 }
  190 
  191 static inline isc_result_t
  192 fromwire_hip(ARGS_FROMWIRE) {
  193     isc_region_t region, rr;
  194     dns_name_t name;
  195     uint8_t hit_len;
  196     uint16_t key_len;
  197 
  198     REQUIRE(type == dns_rdatatype_hip);
  199 
  200     UNUSED(type);
  201     UNUSED(rdclass);
  202 
  203     isc_buffer_activeregion(source, &region);
  204     if (region.length < 4U) {
  205         RETERR(DNS_R_FORMERR);
  206     }
  207 
  208     rr = region;
  209     hit_len = uint8_fromregion(&region);
  210     if (hit_len == 0) {
  211         RETERR(DNS_R_FORMERR);
  212     }
  213     isc_region_consume(&region, 2); /* hit length + algorithm */
  214     key_len = uint16_fromregion(&region);
  215     if (key_len == 0) {
  216         RETERR(DNS_R_FORMERR);
  217     }
  218     isc_region_consume(&region, 2);
  219     if (region.length < (unsigned)(hit_len + key_len)) {
  220         RETERR(DNS_R_FORMERR);
  221     }
  222 
  223     RETERR(mem_tobuffer(target, rr.base, 4 + hit_len + key_len));
  224     isc_buffer_forward(source, 4 + hit_len + key_len);
  225 
  226     dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  227     while (isc_buffer_activelength(source) > 0) {
  228         dns_name_init(&name, NULL);
  229         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  230     }
  231     return (ISC_R_SUCCESS);
  232 }
  233 
  234 static inline isc_result_t
  235 towire_hip(ARGS_TOWIRE) {
  236     isc_region_t region;
  237 
  238     REQUIRE(rdata->type == dns_rdatatype_hip);
  239     REQUIRE(rdata->length != 0);
  240 
  241     UNUSED(cctx);
  242 
  243     dns_rdata_toregion(rdata, &region);
  244     return (mem_tobuffer(target, region.base, region.length));
  245 }
  246 
  247 static inline int
  248 compare_hip(ARGS_COMPARE) {
  249     isc_region_t region1;
  250     isc_region_t region2;
  251 
  252     REQUIRE(rdata1->type == rdata2->type);
  253     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  254     REQUIRE(rdata1->type == dns_rdatatype_hip);
  255     REQUIRE(rdata1->length != 0);
  256     REQUIRE(rdata2->length != 0);
  257 
  258     dns_rdata_toregion(rdata1, &region1);
  259     dns_rdata_toregion(rdata2, &region2);
  260     return (isc_region_compare(&region1, &region2));
  261 }
  262 
  263 static inline isc_result_t
  264 fromstruct_hip(ARGS_FROMSTRUCT) {
  265     dns_rdata_hip_t *hip = source;
  266     dns_rdata_hip_t myhip;
  267     isc_result_t result;
  268 
  269     REQUIRE(type == dns_rdatatype_hip);
  270     REQUIRE(hip != NULL);
  271     REQUIRE(hip->common.rdtype == type);
  272     REQUIRE(hip->common.rdclass == rdclass);
  273     REQUIRE(hip->hit_len > 0 && hip->hit != NULL);
  274     REQUIRE(hip->key_len > 0 && hip->key != NULL);
  275     REQUIRE((hip->servers == NULL && hip->servers_len == 0) ||
  276         (hip->servers != NULL && hip->servers_len != 0));
  277 
  278     UNUSED(type);
  279     UNUSED(rdclass);
  280 
  281     RETERR(uint8_tobuffer(hip->hit_len, target));
  282     RETERR(uint8_tobuffer(hip->algorithm, target));
  283     RETERR(uint16_tobuffer(hip->key_len, target));
  284     RETERR(mem_tobuffer(target, hip->hit, hip->hit_len));
  285     RETERR(mem_tobuffer(target, hip->key, hip->key_len));
  286 
  287     myhip = *hip;
  288     for (result = dns_rdata_hip_first(&myhip); result == ISC_R_SUCCESS;
  289          result = dns_rdata_hip_next(&myhip))
  290         /* empty */;
  291 
  292     return (mem_tobuffer(target, hip->servers, hip->servers_len));
  293 }
  294 
  295 static inline isc_result_t
  296 tostruct_hip(ARGS_TOSTRUCT) {
  297     isc_region_t region;
  298     dns_rdata_hip_t *hip = target;
  299 
  300     REQUIRE(rdata->type == dns_rdatatype_hip);
  301     REQUIRE(hip != NULL);
  302     REQUIRE(rdata->length != 0);
  303 
  304     hip->common.rdclass = rdata->rdclass;
  305     hip->common.rdtype = rdata->type;
  306     ISC_LINK_INIT(&hip->common, link);
  307 
  308     dns_rdata_toregion(rdata, &region);
  309 
  310     hip->hit_len = uint8_fromregion(&region);
  311     isc_region_consume(&region, 1);
  312 
  313     hip->algorithm = uint8_fromregion(&region);
  314     isc_region_consume(&region, 1);
  315 
  316     hip->key_len = uint16_fromregion(&region);
  317     isc_region_consume(&region, 2);
  318 
  319     hip->hit = hip->key = hip->servers = NULL;
  320 
  321     hip->hit = mem_maybedup(mctx, region.base, hip->hit_len);
  322     if (hip->hit == NULL) {
  323         goto cleanup;
  324     }
  325     isc_region_consume(&region, hip->hit_len);
  326 
  327     INSIST(hip->key_len <= region.length);
  328 
  329     hip->key = mem_maybedup(mctx, region.base, hip->key_len);
  330     if (hip->key == NULL) {
  331         goto cleanup;
  332     }
  333     isc_region_consume(&region, hip->key_len);
  334 
  335     hip->servers_len = region.length;
  336     if (hip->servers_len != 0) {
  337         hip->servers = mem_maybedup(mctx, region.base, region.length);
  338         if (hip->servers == NULL) {
  339             goto cleanup;
  340         }
  341     }
  342 
  343     hip->offset = hip->servers_len;
  344     hip->mctx = mctx;
  345     return (ISC_R_SUCCESS);
  346 
  347 cleanup:
  348     if (hip->hit != NULL) {
  349         isc_mem_free(mctx, hip->hit);
  350     }
  351     if (hip->key != NULL) {
  352         isc_mem_free(mctx, hip->key);
  353     }
  354     if (hip->servers != NULL) {
  355         isc_mem_free(mctx, hip->servers);
  356     }
  357     return (ISC_R_NOMEMORY);
  358 }
  359 
  360 static inline void
  361 freestruct_hip(ARGS_FREESTRUCT) {
  362     dns_rdata_hip_t *hip = source;
  363 
  364     REQUIRE(hip != NULL);
  365 
  366     if (hip->mctx == NULL) {
  367         return;
  368     }
  369 
  370     isc_mem_free(hip->mctx, hip->hit);
  371     isc_mem_free(hip->mctx, hip->key);
  372     if (hip->servers != NULL) {
  373         isc_mem_free(hip->mctx, hip->servers);
  374     }
  375     hip->mctx = NULL;
  376 }
  377 
  378 static inline isc_result_t
  379 additionaldata_hip(ARGS_ADDLDATA) {
  380     UNUSED(rdata);
  381     UNUSED(add);
  382     UNUSED(arg);
  383 
  384     REQUIRE(rdata->type == dns_rdatatype_hip);
  385 
  386     return (ISC_R_SUCCESS);
  387 }
  388 
  389 static inline isc_result_t
  390 digest_hip(ARGS_DIGEST) {
  391     isc_region_t r;
  392 
  393     REQUIRE(rdata->type == dns_rdatatype_hip);
  394 
  395     dns_rdata_toregion(rdata, &r);
  396     return ((digest)(arg, &r));
  397 }
  398 
  399 static inline bool
  400 checkowner_hip(ARGS_CHECKOWNER) {
  401     REQUIRE(type == dns_rdatatype_hip);
  402 
  403     UNUSED(name);
  404     UNUSED(type);
  405     UNUSED(rdclass);
  406     UNUSED(wildcard);
  407 
  408     return (true);
  409 }
  410 
  411 static inline bool
  412 checknames_hip(ARGS_CHECKNAMES) {
  413     REQUIRE(rdata->type == dns_rdatatype_hip);
  414 
  415     UNUSED(rdata);
  416     UNUSED(owner);
  417     UNUSED(bad);
  418 
  419     return (true);
  420 }
  421 
  422 isc_result_t
  423 dns_rdata_hip_first(dns_rdata_hip_t *hip) {
  424     if (hip->servers_len == 0) {
  425         return (ISC_R_NOMORE);
  426     }
  427     hip->offset = 0;
  428     return (ISC_R_SUCCESS);
  429 }
  430 
  431 isc_result_t
  432 dns_rdata_hip_next(dns_rdata_hip_t *hip) {
  433     isc_region_t region;
  434     dns_name_t name;
  435 
  436     if (hip->offset >= hip->servers_len) {
  437         return (ISC_R_NOMORE);
  438     }
  439 
  440     region.base = hip->servers + hip->offset;
  441     region.length = hip->servers_len - hip->offset;
  442     dns_name_init(&name, NULL);
  443     dns_name_fromregion(&name, &region);
  444     hip->offset += name.length;
  445     INSIST(hip->offset <= hip->servers_len);
  446     return (hip->offset < hip->servers_len ? ISC_R_SUCCESS : ISC_R_NOMORE);
  447 }
  448 
  449 void
  450 dns_rdata_hip_current(dns_rdata_hip_t *hip, dns_name_t *name) {
  451     isc_region_t region;
  452 
  453     REQUIRE(hip->offset < hip->servers_len);
  454 
  455     region.base = hip->servers + hip->offset;
  456     region.length = hip->servers_len - hip->offset;
  457     dns_name_fromregion(name, &region);
  458 
  459     INSIST(name->length + hip->offset <= hip->servers_len);
  460 }
  461 
  462 static inline int
  463 casecompare_hip(ARGS_COMPARE) {
  464     isc_region_t r1;
  465     isc_region_t r2;
  466     dns_name_t name1;
  467     dns_name_t name2;
  468     int order;
  469     uint8_t hit_len;
  470     uint16_t key_len;
  471 
  472     REQUIRE(rdata1->type == rdata2->type);
  473     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  474     REQUIRE(rdata1->type == dns_rdatatype_hip);
  475     REQUIRE(rdata1->length != 0);
  476     REQUIRE(rdata2->length != 0);
  477 
  478     dns_rdata_toregion(rdata1, &r1);
  479     dns_rdata_toregion(rdata2, &r2);
  480 
  481     INSIST(r1.length > 4);
  482     INSIST(r2.length > 4);
  483     order = memcmp(r1.base, r2.base, 4);
  484     if (order != 0) {
  485         return (order);
  486     }
  487 
  488     hit_len = uint8_fromregion(&r1);
  489     isc_region_consume(&r1, 2); /* hit length + algorithm */
  490     key_len = uint16_fromregion(&r1);
  491     isc_region_consume(&r1, 2); /* key length */
  492     isc_region_consume(&r2, 4);
  493 
  494     INSIST(r1.length >= (unsigned)(hit_len + key_len));
  495     INSIST(r2.length >= (unsigned)(hit_len + key_len));
  496     order = memcmp(r1.base, r2.base, hit_len + key_len);
  497     if (order != 0) {
  498         return (order);
  499     }
  500     isc_region_consume(&r1, hit_len + key_len);
  501     isc_region_consume(&r2, hit_len + key_len);
  502 
  503     dns_name_init(&name1, NULL);
  504     dns_name_init(&name2, NULL);
  505     while (r1.length != 0 && r2.length != 0) {
  506         dns_name_fromregion(&name1, &r1);
  507         dns_name_fromregion(&name2, &r2);
  508         order = dns_name_rdatacompare(&name1, &name2);
  509         if (order != 0) {
  510             return (order);
  511         }
  512 
  513         isc_region_consume(&r1, name_length(&name1));
  514         isc_region_consume(&r2, name_length(&name2));
  515     }
  516     return (isc_region_compare(&r1, &r2));
  517 }
  518 
  519 #endif /* RDATA_GENERIC_HIP_5_C */