"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/dns/rdata/generic/sig_24.c" (4 Sep 2020, 12195 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 "sig_24.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 /* RFC2535 */
   13 
   14 #ifndef RDATA_GENERIC_SIG_24_C
   15 #define RDATA_GENERIC_SIG_24_C
   16 
   17 #define RRTYPE_SIG_ATTRIBUTES (0)
   18 
   19 static inline isc_result_t
   20 fromtext_sig(ARGS_FROMTEXT) {
   21     isc_token_t token;
   22     unsigned char c;
   23     long i;
   24     dns_rdatatype_t covered;
   25     char *e;
   26     isc_result_t result;
   27     dns_name_t name;
   28     isc_buffer_t buffer;
   29     uint32_t time_signed, time_expire;
   30 
   31     REQUIRE(type == dns_rdatatype_sig);
   32 
   33     UNUSED(type);
   34     UNUSED(rdclass);
   35     UNUSED(callbacks);
   36 
   37     /*
   38      * Type covered.
   39      */
   40     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   41                       false));
   42     result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
   43     if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
   44         i = strtol(DNS_AS_STR(token), &e, 10);
   45         if (i < 0 || i > 65535) {
   46             RETTOK(ISC_R_RANGE);
   47         }
   48         if (*e != 0) {
   49             RETTOK(result);
   50         }
   51         covered = (dns_rdatatype_t)i;
   52     }
   53     RETERR(uint16_tobuffer(covered, target));
   54 
   55     /*
   56      * Algorithm.
   57      */
   58     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   59                       false));
   60     RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
   61     RETERR(mem_tobuffer(target, &c, 1));
   62 
   63     /*
   64      * Labels.
   65      */
   66     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   67                       false));
   68     if (token.value.as_ulong > 0xffU) {
   69         RETTOK(ISC_R_RANGE);
   70     }
   71     c = (unsigned char)token.value.as_ulong;
   72     RETERR(mem_tobuffer(target, &c, 1));
   73 
   74     /*
   75      * Original ttl.
   76      */
   77     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   78                       false));
   79     RETERR(uint32_tobuffer(token.value.as_ulong, target));
   80 
   81     /*
   82      * Signature expiration.
   83      */
   84     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   85                       false));
   86     RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
   87     RETERR(uint32_tobuffer(time_expire, target));
   88 
   89     /*
   90      * Time signed.
   91      */
   92     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   93                       false));
   94     RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
   95     RETERR(uint32_tobuffer(time_signed, target));
   96 
   97     /*
   98      * Key footprint.
   99      */
  100     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  101                       false));
  102     RETERR(uint16_tobuffer(token.value.as_ulong, target));
  103 
  104     /*
  105      * Signer.
  106      */
  107     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  108                       false));
  109     dns_name_init(&name, NULL);
  110     buffer_fromregion(&buffer, &token.value.as_region);
  111     if (origin == NULL) {
  112         origin = dns_rootname;
  113     }
  114     RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
  115 
  116     /*
  117      * Sig.
  118      */
  119     return (isc_base64_tobuffer(lexer, target, -2));
  120 }
  121 
  122 static inline isc_result_t
  123 totext_sig(ARGS_TOTEXT) {
  124     isc_region_t sr;
  125     char buf[sizeof("4294967295")];
  126     dns_rdatatype_t covered;
  127     unsigned long ttl;
  128     unsigned long when;
  129     unsigned long exp;
  130     unsigned long foot;
  131     dns_name_t name;
  132     dns_name_t prefix;
  133     bool sub;
  134 
  135     REQUIRE(rdata->type == dns_rdatatype_sig);
  136     REQUIRE(rdata->length != 0);
  137 
  138     dns_rdata_toregion(rdata, &sr);
  139 
  140     /*
  141      * Type covered.
  142      */
  143     covered = uint16_fromregion(&sr);
  144     isc_region_consume(&sr, 2);
  145     /*
  146      * XXXAG We should have something like dns_rdatatype_isknown()
  147      * that does the right thing with type 0.
  148      */
  149     if (dns_rdatatype_isknown(covered) && covered != 0) {
  150         RETERR(dns_rdatatype_totext(covered, target));
  151     } else {
  152         snprintf(buf, sizeof(buf), "%u", covered);
  153         RETERR(str_totext(buf, target));
  154     }
  155     RETERR(str_totext(" ", target));
  156 
  157     /*
  158      * Algorithm.
  159      */
  160     snprintf(buf, sizeof(buf), "%u", sr.base[0]);
  161     isc_region_consume(&sr, 1);
  162     RETERR(str_totext(buf, target));
  163     RETERR(str_totext(" ", target));
  164 
  165     /*
  166      * Labels.
  167      */
  168     snprintf(buf, sizeof(buf), "%u", sr.base[0]);
  169     isc_region_consume(&sr, 1);
  170     RETERR(str_totext(buf, target));
  171     RETERR(str_totext(" ", target));
  172 
  173     /*
  174      * Ttl.
  175      */
  176     ttl = uint32_fromregion(&sr);
  177     isc_region_consume(&sr, 4);
  178     snprintf(buf, sizeof(buf), "%lu", ttl);
  179     RETERR(str_totext(buf, target));
  180     RETERR(str_totext(" ", target));
  181 
  182     /*
  183      * Sig exp.
  184      */
  185     exp = uint32_fromregion(&sr);
  186     isc_region_consume(&sr, 4);
  187     RETERR(dns_time32_totext(exp, target));
  188 
  189     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  190         RETERR(str_totext(" (", target));
  191     }
  192     RETERR(str_totext(tctx->linebreak, target));
  193 
  194     /*
  195      * Time signed.
  196      */
  197     when = uint32_fromregion(&sr);
  198     isc_region_consume(&sr, 4);
  199     RETERR(dns_time32_totext(when, target));
  200     RETERR(str_totext(" ", target));
  201 
  202     /*
  203      * Footprint.
  204      */
  205     foot = uint16_fromregion(&sr);
  206     isc_region_consume(&sr, 2);
  207     snprintf(buf, sizeof(buf), "%lu", foot);
  208     RETERR(str_totext(buf, target));
  209     RETERR(str_totext(" ", target));
  210 
  211     /*
  212      * Signer.
  213      */
  214     dns_name_init(&name, NULL);
  215     dns_name_init(&prefix, NULL);
  216     dns_name_fromregion(&name, &sr);
  217     isc_region_consume(&sr, name_length(&name));
  218     sub = name_prefix(&name, tctx->origin, &prefix);
  219     RETERR(dns_name_totext(&prefix, sub, target));
  220 
  221     /*
  222      * Sig.
  223      */
  224     RETERR(str_totext(tctx->linebreak, target));
  225     if (tctx->width == 0) { /* No splitting */
  226         RETERR(isc_base64_totext(&sr, 60, "", target));
  227     } else {
  228         RETERR(isc_base64_totext(&sr, tctx->width - 2, tctx->linebreak,
  229                      target));
  230     }
  231     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  232         RETERR(str_totext(" )", target));
  233     }
  234 
  235     return (ISC_R_SUCCESS);
  236 }
  237 
  238 static inline isc_result_t
  239 fromwire_sig(ARGS_FROMWIRE) {
  240     isc_region_t sr;
  241     dns_name_t name;
  242 
  243     REQUIRE(type == dns_rdatatype_sig);
  244 
  245     UNUSED(type);
  246     UNUSED(rdclass);
  247 
  248     dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  249 
  250     isc_buffer_activeregion(source, &sr);
  251     /*
  252      * type covered: 2
  253      * algorithm: 1
  254      * labels: 1
  255      * original ttl: 4
  256      * signature expiration: 4
  257      * time signed: 4
  258      * key footprint: 2
  259      */
  260     if (sr.length < 18) {
  261         return (ISC_R_UNEXPECTEDEND);
  262     }
  263 
  264     isc_buffer_forward(source, 18);
  265     RETERR(mem_tobuffer(target, sr.base, 18));
  266 
  267     /*
  268      * Signer.
  269      */
  270     dns_name_init(&name, NULL);
  271     RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  272 
  273     /*
  274      * Sig.
  275      */
  276     isc_buffer_activeregion(source, &sr);
  277     if (sr.length == 0) {
  278         return (ISC_R_UNEXPECTEDEND);
  279     }
  280     isc_buffer_forward(source, sr.length);
  281     return (mem_tobuffer(target, sr.base, sr.length));
  282 }
  283 
  284 static inline isc_result_t
  285 towire_sig(ARGS_TOWIRE) {
  286     isc_region_t sr;
  287     dns_name_t name;
  288     dns_offsets_t offsets;
  289 
  290     REQUIRE(rdata->type == dns_rdatatype_sig);
  291     REQUIRE(rdata->length != 0);
  292 
  293     dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
  294     dns_rdata_toregion(rdata, &sr);
  295     /*
  296      * type covered: 2
  297      * algorithm: 1
  298      * labels: 1
  299      * original ttl: 4
  300      * signature expiration: 4
  301      * time signed: 4
  302      * key footprint: 2
  303      */
  304     RETERR(mem_tobuffer(target, sr.base, 18));
  305     isc_region_consume(&sr, 18);
  306 
  307     /*
  308      * Signer.
  309      */
  310     dns_name_init(&name, offsets);
  311     dns_name_fromregion(&name, &sr);
  312     isc_region_consume(&sr, name_length(&name));
  313     RETERR(dns_name_towire(&name, cctx, target));
  314 
  315     /*
  316      * Signature.
  317      */
  318     return (mem_tobuffer(target, sr.base, sr.length));
  319 }
  320 
  321 static inline int
  322 compare_sig(ARGS_COMPARE) {
  323     isc_region_t r1;
  324     isc_region_t r2;
  325     dns_name_t name1;
  326     dns_name_t name2;
  327     int order;
  328 
  329     REQUIRE(rdata1->type == rdata2->type);
  330     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  331     REQUIRE(rdata1->type == dns_rdatatype_sig);
  332     REQUIRE(rdata1->length != 0);
  333     REQUIRE(rdata2->length != 0);
  334 
  335     dns_rdata_toregion(rdata1, &r1);
  336     dns_rdata_toregion(rdata2, &r2);
  337 
  338     INSIST(r1.length > 18);
  339     INSIST(r2.length > 18);
  340     r1.length = 18;
  341     r2.length = 18;
  342     order = isc_region_compare(&r1, &r2);
  343     if (order != 0) {
  344         return (order);
  345     }
  346 
  347     dns_name_init(&name1, NULL);
  348     dns_name_init(&name2, NULL);
  349     dns_rdata_toregion(rdata1, &r1);
  350     dns_rdata_toregion(rdata2, &r2);
  351     isc_region_consume(&r1, 18);
  352     isc_region_consume(&r2, 18);
  353     dns_name_fromregion(&name1, &r1);
  354     dns_name_fromregion(&name2, &r2);
  355     order = dns_name_rdatacompare(&name1, &name2);
  356     if (order != 0) {
  357         return (order);
  358     }
  359 
  360     isc_region_consume(&r1, name_length(&name1));
  361     isc_region_consume(&r2, name_length(&name2));
  362 
  363     return (isc_region_compare(&r1, &r2));
  364 }
  365 
  366 static inline isc_result_t
  367 fromstruct_sig(ARGS_FROMSTRUCT) {
  368     dns_rdata_sig_t *sig = source;
  369 
  370     REQUIRE(type == dns_rdatatype_sig);
  371     REQUIRE(sig != NULL);
  372     REQUIRE(sig->common.rdtype == type);
  373     REQUIRE(sig->common.rdclass == rdclass);
  374     REQUIRE(sig->signature != NULL || sig->siglen == 0);
  375 
  376     UNUSED(type);
  377     UNUSED(rdclass);
  378 
  379     /*
  380      * Type covered.
  381      */
  382     RETERR(uint16_tobuffer(sig->covered, target));
  383 
  384     /*
  385      * Algorithm.
  386      */
  387     RETERR(uint8_tobuffer(sig->algorithm, target));
  388 
  389     /*
  390      * Labels.
  391      */
  392     RETERR(uint8_tobuffer(sig->labels, target));
  393 
  394     /*
  395      * Original TTL.
  396      */
  397     RETERR(uint32_tobuffer(sig->originalttl, target));
  398 
  399     /*
  400      * Expire time.
  401      */
  402     RETERR(uint32_tobuffer(sig->timeexpire, target));
  403 
  404     /*
  405      * Time signed.
  406      */
  407     RETERR(uint32_tobuffer(sig->timesigned, target));
  408 
  409     /*
  410      * Key ID.
  411      */
  412     RETERR(uint16_tobuffer(sig->keyid, target));
  413 
  414     /*
  415      * Signer name.
  416      */
  417     RETERR(name_tobuffer(&sig->signer, target));
  418 
  419     /*
  420      * Signature.
  421      */
  422     return (mem_tobuffer(target, sig->signature, sig->siglen));
  423 }
  424 
  425 static inline isc_result_t
  426 tostruct_sig(ARGS_TOSTRUCT) {
  427     isc_region_t sr;
  428     dns_rdata_sig_t *sig = target;
  429     dns_name_t signer;
  430 
  431     REQUIRE(rdata->type == dns_rdatatype_sig);
  432     REQUIRE(sig != NULL);
  433     REQUIRE(rdata->length != 0);
  434 
  435     sig->common.rdclass = rdata->rdclass;
  436     sig->common.rdtype = rdata->type;
  437     ISC_LINK_INIT(&sig->common, link);
  438 
  439     dns_rdata_toregion(rdata, &sr);
  440 
  441     /*
  442      * Type covered.
  443      */
  444     sig->covered = uint16_fromregion(&sr);
  445     isc_region_consume(&sr, 2);
  446 
  447     /*
  448      * Algorithm.
  449      */
  450     sig->algorithm = uint8_fromregion(&sr);
  451     isc_region_consume(&sr, 1);
  452 
  453     /*
  454      * Labels.
  455      */
  456     sig->labels = uint8_fromregion(&sr);
  457     isc_region_consume(&sr, 1);
  458 
  459     /*
  460      * Original TTL.
  461      */
  462     sig->originalttl = uint32_fromregion(&sr);
  463     isc_region_consume(&sr, 4);
  464 
  465     /*
  466      * Expire time.
  467      */
  468     sig->timeexpire = uint32_fromregion(&sr);
  469     isc_region_consume(&sr, 4);
  470 
  471     /*
  472      * Time signed.
  473      */
  474     sig->timesigned = uint32_fromregion(&sr);
  475     isc_region_consume(&sr, 4);
  476 
  477     /*
  478      * Key ID.
  479      */
  480     sig->keyid = uint16_fromregion(&sr);
  481     isc_region_consume(&sr, 2);
  482 
  483     dns_name_init(&signer, NULL);
  484     dns_name_fromregion(&signer, &sr);
  485     dns_name_init(&sig->signer, NULL);
  486     RETERR(name_duporclone(&signer, mctx, &sig->signer));
  487     isc_region_consume(&sr, name_length(&sig->signer));
  488 
  489     /*
  490      * Signature.
  491      */
  492     sig->siglen = sr.length;
  493     sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
  494     if (sig->signature == NULL) {
  495         goto cleanup;
  496     }
  497 
  498     sig->mctx = mctx;
  499     return (ISC_R_SUCCESS);
  500 
  501 cleanup:
  502     if (mctx != NULL) {
  503         dns_name_free(&sig->signer, mctx);
  504     }
  505     return (ISC_R_NOMEMORY);
  506 }
  507 
  508 static inline void
  509 freestruct_sig(ARGS_FREESTRUCT) {
  510     dns_rdata_sig_t *sig = (dns_rdata_sig_t *)source;
  511 
  512     REQUIRE(sig != NULL);
  513     REQUIRE(sig->common.rdtype == dns_rdatatype_sig);
  514 
  515     if (sig->mctx == NULL) {
  516         return;
  517     }
  518 
  519     dns_name_free(&sig->signer, sig->mctx);
  520     if (sig->signature != NULL) {
  521         isc_mem_free(sig->mctx, sig->signature);
  522     }
  523     sig->mctx = NULL;
  524 }
  525 
  526 static inline isc_result_t
  527 additionaldata_sig(ARGS_ADDLDATA) {
  528     REQUIRE(rdata->type == dns_rdatatype_sig);
  529 
  530     UNUSED(rdata);
  531     UNUSED(add);
  532     UNUSED(arg);
  533 
  534     return (ISC_R_SUCCESS);
  535 }
  536 
  537 static inline isc_result_t
  538 digest_sig(ARGS_DIGEST) {
  539     REQUIRE(rdata->type == dns_rdatatype_sig);
  540 
  541     UNUSED(rdata);
  542     UNUSED(digest);
  543     UNUSED(arg);
  544 
  545     return (ISC_R_NOTIMPLEMENTED);
  546 }
  547 
  548 static inline dns_rdatatype_t
  549 covers_sig(dns_rdata_t *rdata) {
  550     dns_rdatatype_t type;
  551     isc_region_t r;
  552 
  553     REQUIRE(rdata->type == dns_rdatatype_sig);
  554 
  555     dns_rdata_toregion(rdata, &r);
  556     type = uint16_fromregion(&r);
  557 
  558     return (type);
  559 }
  560 
  561 static inline bool
  562 checkowner_sig(ARGS_CHECKOWNER) {
  563     REQUIRE(type == dns_rdatatype_sig);
  564 
  565     UNUSED(name);
  566     UNUSED(type);
  567     UNUSED(rdclass);
  568     UNUSED(wildcard);
  569 
  570     return (true);
  571 }
  572 
  573 static inline bool
  574 checknames_sig(ARGS_CHECKNAMES) {
  575     REQUIRE(rdata->type == dns_rdatatype_sig);
  576 
  577     UNUSED(rdata);
  578     UNUSED(owner);
  579     UNUSED(bad);
  580 
  581     return (true);
  582 }
  583 
  584 static inline int
  585 casecompare_sig(ARGS_COMPARE) {
  586     return (compare_sig(rdata1, rdata2));
  587 }
  588 #endif /* RDATA_GENERIC_SIG_24_C */