"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/dns/rdata/generic/keydata_65533.c" (4 Sep 2020, 10847 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 "keydata_65533.c" see the Fossies "Dox" file reference documentation.

    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 #ifndef GENERIC_KEYDATA_65533_C
   13 #define GENERIC_KEYDATA_65533_C 1
   14 
   15 #include <isc/stdtime.h>
   16 #include <isc/time.h>
   17 
   18 #include <dst/dst.h>
   19 
   20 #define RRTYPE_KEYDATA_ATTRIBUTES (0)
   21 
   22 static inline isc_result_t
   23 fromtext_keydata(ARGS_FROMTEXT) {
   24     isc_token_t token;
   25     dns_secalg_t alg;
   26     dns_secproto_t proto;
   27     dns_keyflags_t flags;
   28     uint32_t refresh, addhd, removehd;
   29 
   30     REQUIRE(type == dns_rdatatype_keydata);
   31 
   32     UNUSED(type);
   33     UNUSED(rdclass);
   34     UNUSED(origin);
   35     UNUSED(options);
   36     UNUSED(callbacks);
   37 
   38     /* refresh timer */
   39     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   40                       false));
   41     RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &refresh));
   42     RETERR(uint32_tobuffer(refresh, target));
   43 
   44     /* add hold-down */
   45     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   46                       false));
   47     RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &addhd));
   48     RETERR(uint32_tobuffer(addhd, target));
   49 
   50     /* remove hold-down */
   51     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   52                       false));
   53     RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &removehd));
   54     RETERR(uint32_tobuffer(removehd, target));
   55 
   56     /* flags */
   57     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   58                       false));
   59     RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
   60     RETERR(uint16_tobuffer(flags, target));
   61 
   62     /* protocol */
   63     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   64                       false));
   65     RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
   66     RETERR(mem_tobuffer(target, &proto, 1));
   67 
   68     /* algorithm */
   69     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   70                       false));
   71     RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
   72     RETERR(mem_tobuffer(target, &alg, 1));
   73 
   74     /* No Key? */
   75     if ((flags & 0xc000) == 0xc000) {
   76         return (ISC_R_SUCCESS);
   77     }
   78 
   79     return (isc_base64_tobuffer(lexer, target, -2));
   80 }
   81 
   82 static inline isc_result_t
   83 totext_keydata(ARGS_TOTEXT) {
   84     isc_region_t sr;
   85     char buf[sizeof("64000")];
   86     unsigned int flags;
   87     unsigned char algorithm;
   88     unsigned long refresh, add, deltime;
   89     char algbuf[DNS_NAME_FORMATSIZE];
   90     const char *keyinfo;
   91 
   92     REQUIRE(rdata->type == dns_rdatatype_keydata);
   93 
   94     if ((tctx->flags & DNS_STYLEFLAG_KEYDATA) == 0 || rdata->length < 16) {
   95         return (unknown_totext(rdata, tctx, target));
   96     }
   97 
   98     dns_rdata_toregion(rdata, &sr);
   99 
  100     /* refresh timer */
  101     refresh = uint32_fromregion(&sr);
  102     isc_region_consume(&sr, 4);
  103     RETERR(dns_time32_totext(refresh, target));
  104     RETERR(str_totext(" ", target));
  105 
  106     /* add hold-down */
  107     add = uint32_fromregion(&sr);
  108     isc_region_consume(&sr, 4);
  109     RETERR(dns_time32_totext(add, target));
  110     RETERR(str_totext(" ", target));
  111 
  112     /* remove hold-down */
  113     deltime = uint32_fromregion(&sr);
  114     isc_region_consume(&sr, 4);
  115     RETERR(dns_time32_totext(deltime, target));
  116     RETERR(str_totext(" ", target));
  117 
  118     /* flags */
  119     flags = uint16_fromregion(&sr);
  120     isc_region_consume(&sr, 2);
  121     snprintf(buf, sizeof(buf), "%u", flags);
  122     RETERR(str_totext(buf, target));
  123     RETERR(str_totext(" ", target));
  124     if ((flags & DNS_KEYFLAG_KSK) != 0) {
  125         if ((flags & DNS_KEYFLAG_REVOKE) != 0) {
  126             keyinfo = "revoked KSK";
  127         } else {
  128             keyinfo = "KSK";
  129         }
  130     } else {
  131         keyinfo = "ZSK";
  132     }
  133 
  134     /* protocol */
  135     snprintf(buf, sizeof(buf), "%u", sr.base[0]);
  136     isc_region_consume(&sr, 1);
  137     RETERR(str_totext(buf, target));
  138     RETERR(str_totext(" ", target));
  139 
  140     /* algorithm */
  141     algorithm = sr.base[0];
  142     snprintf(buf, sizeof(buf), "%u", algorithm);
  143     isc_region_consume(&sr, 1);
  144     RETERR(str_totext(buf, target));
  145 
  146     /* No Key? */
  147     if ((flags & 0xc000) == 0xc000) {
  148         return (ISC_R_SUCCESS);
  149     }
  150 
  151     /* key */
  152     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  153         RETERR(str_totext(" (", target));
  154     }
  155     RETERR(str_totext(tctx->linebreak, target));
  156     if (tctx->width == 0) { /* No splitting */
  157         RETERR(isc_base64_totext(&sr, 60, "", target));
  158     } else {
  159         RETERR(isc_base64_totext(&sr, tctx->width - 2, tctx->linebreak,
  160                      target));
  161     }
  162 
  163     if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0) {
  164         RETERR(str_totext(tctx->linebreak, target));
  165     } else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  166         RETERR(str_totext(" ", target));
  167     }
  168 
  169     if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  170         RETERR(str_totext(")", target));
  171     }
  172 
  173     if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0) {
  174         isc_region_t tmpr;
  175         char rbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
  176         char abuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
  177         char dbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
  178         isc_time_t t;
  179 
  180         RETERR(str_totext(" ; ", target));
  181         RETERR(str_totext(keyinfo, target));
  182         dns_secalg_format((dns_secalg_t)algorithm, algbuf,
  183                   sizeof(algbuf));
  184         RETERR(str_totext("; alg = ", target));
  185         RETERR(str_totext(algbuf, target));
  186         RETERR(str_totext("; key id = ", target));
  187         dns_rdata_toregion(rdata, &tmpr);
  188         /* Skip over refresh, addhd, and removehd */
  189         isc_region_consume(&tmpr, 12);
  190         snprintf(buf, sizeof(buf), "%u", dst_region_computeid(&tmpr));
  191         RETERR(str_totext(buf, target));
  192 
  193         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
  194             isc_stdtime_t now;
  195 
  196             isc_stdtime_get(&now);
  197 
  198             RETERR(str_totext(tctx->linebreak, target));
  199             RETERR(str_totext("; next refresh: ", target));
  200             isc_time_set(&t, refresh, 0);
  201             isc_time_formathttptimestamp(&t, rbuf, sizeof(rbuf));
  202             RETERR(str_totext(rbuf, target));
  203 
  204             if (add == 0U) {
  205                 RETERR(str_totext(tctx->linebreak, target));
  206                 RETERR(str_totext("; no trust", target));
  207             } else {
  208                 RETERR(str_totext(tctx->linebreak, target));
  209                 if (add < now) {
  210                     RETERR(str_totext("; trusted since: ",
  211                               target));
  212                 } else {
  213                     RETERR(str_totext("; trust pending: ",
  214                               target));
  215                 }
  216                 isc_time_set(&t, add, 0);
  217                 isc_time_formathttptimestamp(&t, abuf,
  218                                  sizeof(abuf));
  219                 RETERR(str_totext(abuf, target));
  220             }
  221 
  222             if (deltime != 0U) {
  223                 RETERR(str_totext(tctx->linebreak, target));
  224                 RETERR(str_totext("; removal pending: ",
  225                           target));
  226                 isc_time_set(&t, deltime, 0);
  227                 isc_time_formathttptimestamp(&t, dbuf,
  228                                  sizeof(dbuf));
  229                 RETERR(str_totext(dbuf, target));
  230             }
  231         }
  232     }
  233     return (ISC_R_SUCCESS);
  234 }
  235 
  236 static inline isc_result_t
  237 fromwire_keydata(ARGS_FROMWIRE) {
  238     isc_region_t sr;
  239 
  240     REQUIRE(type == dns_rdatatype_keydata);
  241 
  242     UNUSED(type);
  243     UNUSED(rdclass);
  244     UNUSED(dctx);
  245     UNUSED(options);
  246 
  247     isc_buffer_activeregion(source, &sr);
  248     isc_buffer_forward(source, sr.length);
  249     return (mem_tobuffer(target, sr.base, sr.length));
  250 }
  251 
  252 static inline isc_result_t
  253 towire_keydata(ARGS_TOWIRE) {
  254     isc_region_t sr;
  255 
  256     REQUIRE(rdata->type == dns_rdatatype_keydata);
  257 
  258     UNUSED(cctx);
  259 
  260     dns_rdata_toregion(rdata, &sr);
  261     return (mem_tobuffer(target, sr.base, sr.length));
  262 }
  263 
  264 static inline int
  265 compare_keydata(ARGS_COMPARE) {
  266     isc_region_t r1;
  267     isc_region_t r2;
  268 
  269     REQUIRE(rdata1->type == rdata2->type);
  270     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  271     REQUIRE(rdata1->type == dns_rdatatype_keydata);
  272 
  273     dns_rdata_toregion(rdata1, &r1);
  274     dns_rdata_toregion(rdata2, &r2);
  275     return (isc_region_compare(&r1, &r2));
  276 }
  277 
  278 static inline isc_result_t
  279 fromstruct_keydata(ARGS_FROMSTRUCT) {
  280     dns_rdata_keydata_t *keydata = source;
  281 
  282     REQUIRE(type == dns_rdatatype_keydata);
  283     REQUIRE(keydata != NULL);
  284     REQUIRE(keydata->common.rdtype == type);
  285     REQUIRE(keydata->common.rdclass == rdclass);
  286 
  287     UNUSED(type);
  288     UNUSED(rdclass);
  289 
  290     /* Refresh timer */
  291     RETERR(uint32_tobuffer(keydata->refresh, target));
  292 
  293     /* Add hold-down */
  294     RETERR(uint32_tobuffer(keydata->addhd, target));
  295 
  296     /* Remove hold-down */
  297     RETERR(uint32_tobuffer(keydata->removehd, target));
  298 
  299     /* Flags */
  300     RETERR(uint16_tobuffer(keydata->flags, target));
  301 
  302     /* Protocol */
  303     RETERR(uint8_tobuffer(keydata->protocol, target));
  304 
  305     /* Algorithm */
  306     RETERR(uint8_tobuffer(keydata->algorithm, target));
  307 
  308     /* Data */
  309     return (mem_tobuffer(target, keydata->data, keydata->datalen));
  310 }
  311 
  312 static inline isc_result_t
  313 tostruct_keydata(ARGS_TOSTRUCT) {
  314     dns_rdata_keydata_t *keydata = target;
  315     isc_region_t sr;
  316 
  317     REQUIRE(rdata->type == dns_rdatatype_keydata);
  318     REQUIRE(keydata != NULL);
  319 
  320     keydata->common.rdclass = rdata->rdclass;
  321     keydata->common.rdtype = rdata->type;
  322     ISC_LINK_INIT(&keydata->common, link);
  323 
  324     dns_rdata_toregion(rdata, &sr);
  325 
  326     /* Refresh timer */
  327     if (sr.length < 4) {
  328         return (ISC_R_UNEXPECTEDEND);
  329     }
  330     keydata->refresh = uint32_fromregion(&sr);
  331     isc_region_consume(&sr, 4);
  332 
  333     /* Add hold-down */
  334     if (sr.length < 4) {
  335         return (ISC_R_UNEXPECTEDEND);
  336     }
  337     keydata->addhd = uint32_fromregion(&sr);
  338     isc_region_consume(&sr, 4);
  339 
  340     /* Remove hold-down */
  341     if (sr.length < 4) {
  342         return (ISC_R_UNEXPECTEDEND);
  343     }
  344     keydata->removehd = uint32_fromregion(&sr);
  345     isc_region_consume(&sr, 4);
  346 
  347     /* Flags */
  348     if (sr.length < 2) {
  349         return (ISC_R_UNEXPECTEDEND);
  350     }
  351     keydata->flags = uint16_fromregion(&sr);
  352     isc_region_consume(&sr, 2);
  353 
  354     /* Protocol */
  355     if (sr.length < 1) {
  356         return (ISC_R_UNEXPECTEDEND);
  357     }
  358     keydata->protocol = uint8_fromregion(&sr);
  359     isc_region_consume(&sr, 1);
  360 
  361     /* Algorithm */
  362     if (sr.length < 1) {
  363         return (ISC_R_UNEXPECTEDEND);
  364     }
  365     keydata->algorithm = uint8_fromregion(&sr);
  366     isc_region_consume(&sr, 1);
  367 
  368     /* Data */
  369     keydata->datalen = sr.length;
  370     keydata->data = mem_maybedup(mctx, sr.base, keydata->datalen);
  371     if (keydata->data == NULL) {
  372         return (ISC_R_NOMEMORY);
  373     }
  374 
  375     keydata->mctx = mctx;
  376     return (ISC_R_SUCCESS);
  377 }
  378 
  379 static inline void
  380 freestruct_keydata(ARGS_FREESTRUCT) {
  381     dns_rdata_keydata_t *keydata = (dns_rdata_keydata_t *)source;
  382 
  383     REQUIRE(keydata != NULL);
  384     REQUIRE(keydata->common.rdtype == dns_rdatatype_keydata);
  385 
  386     if (keydata->mctx == NULL) {
  387         return;
  388     }
  389 
  390     if (keydata->data != NULL) {
  391         isc_mem_free(keydata->mctx, keydata->data);
  392     }
  393     keydata->mctx = NULL;
  394 }
  395 
  396 static inline isc_result_t
  397 additionaldata_keydata(ARGS_ADDLDATA) {
  398     REQUIRE(rdata->type == dns_rdatatype_keydata);
  399 
  400     UNUSED(rdata);
  401     UNUSED(add);
  402     UNUSED(arg);
  403 
  404     return (ISC_R_SUCCESS);
  405 }
  406 
  407 static inline isc_result_t
  408 digest_keydata(ARGS_DIGEST) {
  409     isc_region_t r;
  410 
  411     REQUIRE(rdata->type == dns_rdatatype_keydata);
  412 
  413     dns_rdata_toregion(rdata, &r);
  414 
  415     return ((digest)(arg, &r));
  416 }
  417 
  418 static inline bool
  419 checkowner_keydata(ARGS_CHECKOWNER) {
  420     REQUIRE(type == dns_rdatatype_keydata);
  421 
  422     UNUSED(name);
  423     UNUSED(type);
  424     UNUSED(rdclass);
  425     UNUSED(wildcard);
  426 
  427     return (true);
  428 }
  429 
  430 static inline bool
  431 checknames_keydata(ARGS_CHECKNAMES) {
  432     REQUIRE(rdata->type == dns_rdatatype_keydata);
  433 
  434     UNUSED(rdata);
  435     UNUSED(owner);
  436     UNUSED(bad);
  437 
  438     return (true);
  439 }
  440 
  441 static inline int
  442 casecompare_keydata(ARGS_COMPARE) {
  443     return (compare_keydata(rdata1, rdata2));
  444 }
  445 
  446 #endif /* GENERIC_KEYDATA_65533_C */