"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/dns/rdata/generic/nsec3param_51.c" (4 Sep 2020, 7907 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.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 "nsec3param_51.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.16.6_vs_9.16.7.

    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 /*
   13  * Copyright (C) 2004  Nominet, Ltd.
   14  *
   15  * Permission to use, copy, modify, and distribute this software for any
   16  * purpose with or without fee is hereby granted, provided that the above
   17  * copyright notice and this permission notice appear in all copies.
   18  *
   19  * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINET DISCLAIMS ALL WARRANTIES WITH
   20  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
   21  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
   22  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
   23  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
   24  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
   25  * PERFORMANCE OF THIS SOFTWARE.
   26  */
   27 
   28 /* RFC 5155 */
   29 
   30 #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
   31 #define RDATA_GENERIC_NSEC3PARAM_51_C
   32 
   33 #include <isc/base32.h>
   34 #include <isc/iterated_hash.h>
   35 
   36 #define RRTYPE_NSEC3PARAM_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
   37 
   38 static inline isc_result_t
   39 fromtext_nsec3param(ARGS_FROMTEXT) {
   40     isc_token_t token;
   41     unsigned int flags = 0;
   42     unsigned char hashalg;
   43 
   44     REQUIRE(type == dns_rdatatype_nsec3param);
   45 
   46     UNUSED(type);
   47     UNUSED(rdclass);
   48     UNUSED(callbacks);
   49     UNUSED(origin);
   50     UNUSED(options);
   51 
   52     /* Hash. */
   53     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   54                       false));
   55     RETTOK(dns_hashalg_fromtext(&hashalg, &token.value.as_textregion));
   56     RETERR(uint8_tobuffer(hashalg, target));
   57 
   58     /* Flags. */
   59     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   60                       false));
   61     flags = token.value.as_ulong;
   62     if (flags > 255U) {
   63         RETTOK(ISC_R_RANGE);
   64     }
   65     RETERR(uint8_tobuffer(flags, target));
   66 
   67     /* Iterations. */
   68     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   69                       false));
   70     if (token.value.as_ulong > 0xffffU) {
   71         RETTOK(ISC_R_RANGE);
   72     }
   73     RETERR(uint16_tobuffer(token.value.as_ulong, target));
   74 
   75     /* Salt. */
   76     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   77                       false));
   78     if (token.value.as_textregion.length > (255 * 2)) {
   79         RETTOK(DNS_R_TEXTTOOLONG);
   80     }
   81     if (strcmp(DNS_AS_STR(token), "-") == 0) {
   82         RETERR(uint8_tobuffer(0, target));
   83     } else {
   84         RETERR(uint8_tobuffer(strlen(DNS_AS_STR(token)) / 2, target));
   85         RETERR(isc_hex_decodestring(DNS_AS_STR(token), target));
   86     }
   87 
   88     return (ISC_R_SUCCESS);
   89 }
   90 
   91 static inline isc_result_t
   92 totext_nsec3param(ARGS_TOTEXT) {
   93     isc_region_t sr;
   94     unsigned int i, j;
   95     unsigned char hash;
   96     unsigned char flags;
   97     char buf[sizeof("65535 ")];
   98     uint32_t iterations;
   99 
  100     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  101     REQUIRE(rdata->length != 0);
  102 
  103     UNUSED(tctx);
  104 
  105     dns_rdata_toregion(rdata, &sr);
  106 
  107     hash = uint8_fromregion(&sr);
  108     isc_region_consume(&sr, 1);
  109 
  110     flags = uint8_fromregion(&sr);
  111     isc_region_consume(&sr, 1);
  112 
  113     iterations = uint16_fromregion(&sr);
  114     isc_region_consume(&sr, 2);
  115 
  116     snprintf(buf, sizeof(buf), "%u ", hash);
  117     RETERR(str_totext(buf, target));
  118 
  119     snprintf(buf, sizeof(buf), "%u ", flags);
  120     RETERR(str_totext(buf, target));
  121 
  122     snprintf(buf, sizeof(buf), "%u ", iterations);
  123     RETERR(str_totext(buf, target));
  124 
  125     j = uint8_fromregion(&sr);
  126     isc_region_consume(&sr, 1);
  127     INSIST(j <= sr.length);
  128 
  129     if (j != 0) {
  130         i = sr.length;
  131         sr.length = j;
  132         RETERR(isc_hex_totext(&sr, 1, "", target));
  133         sr.length = i - j;
  134     } else {
  135         RETERR(str_totext("-", target));
  136     }
  137 
  138     return (ISC_R_SUCCESS);
  139 }
  140 
  141 static inline isc_result_t
  142 fromwire_nsec3param(ARGS_FROMWIRE) {
  143     isc_region_t sr, rr;
  144     unsigned int saltlen;
  145 
  146     REQUIRE(type == dns_rdatatype_nsec3param);
  147 
  148     UNUSED(type);
  149     UNUSED(rdclass);
  150     UNUSED(options);
  151     UNUSED(dctx);
  152 
  153     isc_buffer_activeregion(source, &sr);
  154     rr = sr;
  155 
  156     /* hash(1), flags(1), iterations(2), saltlen(1) */
  157     if (sr.length < 5U) {
  158         RETERR(DNS_R_FORMERR);
  159     }
  160     saltlen = sr.base[4];
  161     isc_region_consume(&sr, 5);
  162 
  163     if (sr.length != saltlen) {
  164         RETERR(DNS_R_FORMERR);
  165     }
  166     isc_region_consume(&sr, saltlen);
  167     RETERR(mem_tobuffer(target, rr.base, rr.length));
  168     isc_buffer_forward(source, rr.length);
  169     return (ISC_R_SUCCESS);
  170 }
  171 
  172 static inline isc_result_t
  173 towire_nsec3param(ARGS_TOWIRE) {
  174     isc_region_t sr;
  175 
  176     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  177     REQUIRE(rdata->length != 0);
  178 
  179     UNUSED(cctx);
  180 
  181     dns_rdata_toregion(rdata, &sr);
  182     return (mem_tobuffer(target, sr.base, sr.length));
  183 }
  184 
  185 static inline int
  186 compare_nsec3param(ARGS_COMPARE) {
  187     isc_region_t r1;
  188     isc_region_t r2;
  189 
  190     REQUIRE(rdata1->type == rdata2->type);
  191     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  192     REQUIRE(rdata1->type == dns_rdatatype_nsec3param);
  193     REQUIRE(rdata1->length != 0);
  194     REQUIRE(rdata2->length != 0);
  195 
  196     dns_rdata_toregion(rdata1, &r1);
  197     dns_rdata_toregion(rdata2, &r2);
  198     return (isc_region_compare(&r1, &r2));
  199 }
  200 
  201 static inline isc_result_t
  202 fromstruct_nsec3param(ARGS_FROMSTRUCT) {
  203     dns_rdata_nsec3param_t *nsec3param = source;
  204 
  205     REQUIRE(type == dns_rdatatype_nsec3param);
  206     REQUIRE(nsec3param != NULL);
  207     REQUIRE(nsec3param->common.rdtype == type);
  208     REQUIRE(nsec3param->common.rdclass == rdclass);
  209 
  210     UNUSED(type);
  211     UNUSED(rdclass);
  212 
  213     RETERR(uint8_tobuffer(nsec3param->hash, target));
  214     RETERR(uint8_tobuffer(nsec3param->flags, target));
  215     RETERR(uint16_tobuffer(nsec3param->iterations, target));
  216     RETERR(uint8_tobuffer(nsec3param->salt_length, target));
  217     RETERR(mem_tobuffer(target, nsec3param->salt, nsec3param->salt_length));
  218     return (ISC_R_SUCCESS);
  219 }
  220 
  221 static inline isc_result_t
  222 tostruct_nsec3param(ARGS_TOSTRUCT) {
  223     isc_region_t region;
  224     dns_rdata_nsec3param_t *nsec3param = target;
  225 
  226     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  227     REQUIRE(nsec3param != NULL);
  228     REQUIRE(rdata->length != 0);
  229 
  230     nsec3param->common.rdclass = rdata->rdclass;
  231     nsec3param->common.rdtype = rdata->type;
  232     ISC_LINK_INIT(&nsec3param->common, link);
  233 
  234     region.base = rdata->data;
  235     region.length = rdata->length;
  236     nsec3param->hash = uint8_consume_fromregion(&region);
  237     nsec3param->flags = uint8_consume_fromregion(&region);
  238     nsec3param->iterations = uint16_consume_fromregion(&region);
  239 
  240     nsec3param->salt_length = uint8_consume_fromregion(&region);
  241     nsec3param->salt = mem_maybedup(mctx, region.base,
  242                     nsec3param->salt_length);
  243     if (nsec3param->salt == NULL) {
  244         return (ISC_R_NOMEMORY);
  245     }
  246     isc_region_consume(&region, nsec3param->salt_length);
  247 
  248     nsec3param->mctx = mctx;
  249     return (ISC_R_SUCCESS);
  250 }
  251 
  252 static inline void
  253 freestruct_nsec3param(ARGS_FREESTRUCT) {
  254     dns_rdata_nsec3param_t *nsec3param = source;
  255 
  256     REQUIRE(nsec3param != NULL);
  257     REQUIRE(nsec3param->common.rdtype == dns_rdatatype_nsec3param);
  258 
  259     if (nsec3param->mctx == NULL) {
  260         return;
  261     }
  262 
  263     if (nsec3param->salt != NULL) {
  264         isc_mem_free(nsec3param->mctx, nsec3param->salt);
  265     }
  266     nsec3param->mctx = NULL;
  267 }
  268 
  269 static inline isc_result_t
  270 additionaldata_nsec3param(ARGS_ADDLDATA) {
  271     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  272 
  273     UNUSED(rdata);
  274     UNUSED(add);
  275     UNUSED(arg);
  276 
  277     return (ISC_R_SUCCESS);
  278 }
  279 
  280 static inline isc_result_t
  281 digest_nsec3param(ARGS_DIGEST) {
  282     isc_region_t r;
  283 
  284     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  285 
  286     dns_rdata_toregion(rdata, &r);
  287     return ((digest)(arg, &r));
  288 }
  289 
  290 static inline bool
  291 checkowner_nsec3param(ARGS_CHECKOWNER) {
  292     REQUIRE(type == dns_rdatatype_nsec3param);
  293 
  294     UNUSED(name);
  295     UNUSED(type);
  296     UNUSED(rdclass);
  297     UNUSED(wildcard);
  298 
  299     return (true);
  300 }
  301 
  302 static inline bool
  303 checknames_nsec3param(ARGS_CHECKNAMES) {
  304     REQUIRE(rdata->type == dns_rdatatype_nsec3param);
  305 
  306     UNUSED(rdata);
  307     UNUSED(owner);
  308     UNUSED(bad);
  309 
  310     return (true);
  311 }
  312 
  313 static inline int
  314 casecompare_nsec3param(ARGS_COMPARE) {
  315     return (compare_nsec3param(rdata1, rdata2));
  316 }
  317 
  318 #endif /* RDATA_GENERIC_NSEC3PARAM_51_C */