cds_59.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* draft-ietf-dnsop-delegation-trust-maintainance-14 */
00018 
00019 #ifndef RDATA_GENERIC_CDS_59_C
00020 #define RDATA_GENERIC_CDS_59_C
00021 
00022 #define RRTYPE_CDS_ATTRIBUTES 0
00023 
00024 #include <isc/sha1.h>
00025 #include <isc/sha2.h>
00026 
00027 #include <dns/ds.h>
00028 
00029 #include "dst_gost.h"
00030 
00031 static inline isc_result_t
00032 fromtext_cds(ARGS_FROMTEXT) {
00033         isc_token_t token;
00034         unsigned char c;
00035         int length;
00036 
00037         REQUIRE(type == 59);
00038 
00039         UNUSED(type);
00040         UNUSED(rdclass);
00041         UNUSED(origin);
00042         UNUSED(options);
00043         UNUSED(callbacks);
00044 
00045         /*
00046          * Key tag.
00047          */
00048         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00049                                       ISC_FALSE));
00050         if (token.value.as_ulong > 0xffffU)
00051                 RETTOK(ISC_R_RANGE);
00052         RETERR(uint16_tobuffer(token.value.as_ulong, target));
00053 
00054         /*
00055          * Algorithm.
00056          */
00057         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00058                                       ISC_FALSE));
00059         RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
00060         RETERR(mem_tobuffer(target, &c, 1));
00061 
00062         /*
00063          * Digest type.
00064          */
00065         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00066                                       ISC_FALSE));
00067         RETTOK(dns_dsdigest_fromtext(&c, &token.value.as_textregion));
00068         RETERR(mem_tobuffer(target, &c, 1));
00069 
00070         /*
00071          * Digest.
00072          */
00073         switch (c) {
00074         case DNS_DSDIGEST_SHA1:
00075                 length = ISC_SHA1_DIGESTLENGTH;
00076                 break;
00077         case DNS_DSDIGEST_SHA256:
00078                 length = ISC_SHA256_DIGESTLENGTH;
00079                 break;
00080 #ifdef ISC_GOST_DIGESTLENGTH
00081         case DNS_DSDIGEST_GOST:
00082                 length = ISC_GOST_DIGESTLENGTH;
00083                 break;
00084 #endif
00085         case DNS_DSDIGEST_SHA384:
00086                 length = ISC_SHA384_DIGESTLENGTH;
00087                 break;
00088         default:
00089                 length = -1;
00090                 break;
00091         }
00092         return (isc_hex_tobuffer(lexer, target, length));
00093 }
00094 
00095 static inline isc_result_t
00096 totext_cds(ARGS_TOTEXT) {
00097         isc_region_t sr;
00098         char buf[sizeof("64000 ")];
00099         unsigned int n;
00100 
00101         REQUIRE(rdata->type == 59);
00102         REQUIRE(rdata->length != 0);
00103 
00104         UNUSED(tctx);
00105 
00106         dns_rdata_toregion(rdata, &sr);
00107 
00108         /*
00109          * Key tag.
00110          */
00111         n = uint16_fromregion(&sr);
00112         isc_region_consume(&sr, 2);
00113         sprintf(buf, "%u ", n);
00114         RETERR(str_totext(buf, target));
00115 
00116         /*
00117          * Algorithm.
00118          */
00119         n = uint8_fromregion(&sr);
00120         isc_region_consume(&sr, 1);
00121         sprintf(buf, "%u ", n);
00122         RETERR(str_totext(buf, target));
00123 
00124         /*
00125          * Digest type.
00126          */
00127         n = uint8_fromregion(&sr);
00128         isc_region_consume(&sr, 1);
00129         sprintf(buf, "%u", n);
00130         RETERR(str_totext(buf, target));
00131 
00132         /*
00133          * Digest.
00134          */
00135         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00136                 RETERR(str_totext(" (", target));
00137         RETERR(str_totext(tctx->linebreak, target));
00138         if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
00139                 if (tctx->width == 0) /* No splitting */
00140                         RETERR(isc_hex_totext(&sr, 0, "", target));
00141                 else
00142                         RETERR(isc_hex_totext(&sr, tctx->width - 2,
00143                                               tctx->linebreak, target));
00144         } else
00145                 RETERR(str_totext("[omitted]", target));
00146         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00147                 RETERR(str_totext(" )", target));
00148         return (ISC_R_SUCCESS);
00149 }
00150 
00151 static inline isc_result_t
00152 fromwire_cds(ARGS_FROMWIRE) {
00153         isc_region_t sr;
00154 
00155         REQUIRE(type == 59);
00156 
00157         UNUSED(type);
00158         UNUSED(rdclass);
00159         UNUSED(dctx);
00160         UNUSED(options);
00161 
00162         isc_buffer_activeregion(source, &sr);
00163 
00164         /*
00165          * Check digest lengths if we know them.
00166          */
00167         if (sr.length < 4 ||
00168             (sr.base[3] == DNS_DSDIGEST_SHA1 &&
00169              sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
00170             (sr.base[3] == DNS_DSDIGEST_SHA256 &&
00171              sr.length < 4 + ISC_SHA256_DIGESTLENGTH) ||
00172 #ifdef ISC_GOST_DIGESTLENGTH
00173             (sr.base[3] == DNS_DSDIGEST_GOST &&
00174              sr.length < 4 + ISC_GOST_DIGESTLENGTH) ||
00175 #endif
00176             (sr.base[3] == DNS_DSDIGEST_SHA384 &&
00177              sr.length < 4 + ISC_SHA384_DIGESTLENGTH))
00178                 return (ISC_R_UNEXPECTEDEND);
00179 
00180         /*
00181          * Only copy digest lengths if we know them.
00182          * If there is extra data dns_rdata_fromwire() will
00183          * detect that.
00184          */
00185         if (sr.base[3] == DNS_DSDIGEST_SHA1)
00186                 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
00187         else if (sr.base[3] == DNS_DSDIGEST_SHA256)
00188                 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
00189 #ifdef ISC_GOST_DIGESTLENGTH
00190         else if (sr.base[3] == DNS_DSDIGEST_GOST)
00191                 sr.length = 4 + ISC_GOST_DIGESTLENGTH;
00192 #endif
00193         else if (sr.base[3] == DNS_DSDIGEST_SHA384)
00194                 sr.length = 4 + ISC_SHA384_DIGESTLENGTH;
00195 
00196         isc_buffer_forward(source, sr.length);
00197         return (mem_tobuffer(target, sr.base, sr.length));
00198 }
00199 
00200 static inline isc_result_t
00201 towire_cds(ARGS_TOWIRE) {
00202         isc_region_t sr;
00203 
00204         REQUIRE(rdata->type == 59);
00205         REQUIRE(rdata->length != 0);
00206 
00207         UNUSED(cctx);
00208 
00209         dns_rdata_toregion(rdata, &sr);
00210         return (mem_tobuffer(target, sr.base, sr.length));
00211 }
00212 
00213 static inline int
00214 compare_cds(ARGS_COMPARE) {
00215         isc_region_t r1;
00216         isc_region_t r2;
00217 
00218         REQUIRE(rdata1->type == rdata2->type);
00219         REQUIRE(rdata1->rdclass == rdata2->rdclass);
00220         REQUIRE(rdata1->type == 59);
00221         REQUIRE(rdata1->length != 0);
00222         REQUIRE(rdata2->length != 0);
00223 
00224         dns_rdata_toregion(rdata1, &r1);
00225         dns_rdata_toregion(rdata2, &r2);
00226         return (isc_region_compare(&r1, &r2));
00227 }
00228 
00229 static inline isc_result_t
00230 fromstruct_cds(ARGS_FROMSTRUCT) {
00231         dns_rdata_cds_t *ds = source;
00232 
00233         REQUIRE(type == 59);
00234         REQUIRE(source != NULL);
00235         REQUIRE(ds->common.rdtype == type);
00236         REQUIRE(ds->common.rdclass == rdclass);
00237         switch (ds->digest_type) {
00238         case DNS_DSDIGEST_SHA1:
00239                 REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
00240                 break;
00241         case DNS_DSDIGEST_SHA256:
00242                 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
00243                 break;
00244 #ifdef ISC_GOST_DIGESTLENGTH
00245         case DNS_DSDIGEST_GOST:
00246                 REQUIRE(ds->length == ISC_GOST_DIGESTLENGTH);
00247                 break;
00248 #endif
00249         case DNS_DSDIGEST_SHA384:
00250                 REQUIRE(ds->length == ISC_SHA384_DIGESTLENGTH);
00251                 break;
00252         }
00253 
00254         UNUSED(type);
00255         UNUSED(rdclass);
00256 
00257         RETERR(uint16_tobuffer(ds->key_tag, target));
00258         RETERR(uint8_tobuffer(ds->algorithm, target));
00259         RETERR(uint8_tobuffer(ds->digest_type, target));
00260 
00261         return (mem_tobuffer(target, ds->digest, ds->length));
00262 }
00263 
00264 static inline isc_result_t
00265 tostruct_cds(ARGS_TOSTRUCT) {
00266         dns_rdata_cds_t *ds = target;
00267         isc_region_t region;
00268 
00269         REQUIRE(rdata->type == 59);
00270         REQUIRE(target != NULL);
00271         REQUIRE(rdata->length != 0);
00272 
00273         ds->common.rdclass = rdata->rdclass;
00274         ds->common.rdtype = rdata->type;
00275         ISC_LINK_INIT(&ds->common, link);
00276 
00277         dns_rdata_toregion(rdata, &region);
00278 
00279         ds->key_tag = uint16_fromregion(&region);
00280         isc_region_consume(&region, 2);
00281         ds->algorithm = uint8_fromregion(&region);
00282         isc_region_consume(&region, 1);
00283         ds->digest_type = uint8_fromregion(&region);
00284         isc_region_consume(&region, 1);
00285         ds->length = region.length;
00286 
00287         ds->digest = mem_maybedup(mctx, region.base, region.length);
00288         if (ds->digest == NULL)
00289                 return (ISC_R_NOMEMORY);
00290 
00291         ds->mctx = mctx;
00292         return (ISC_R_SUCCESS);
00293 }
00294 
00295 static inline void
00296 freestruct_cds(ARGS_FREESTRUCT) {
00297         dns_rdata_cds_t *ds = source;
00298 
00299         REQUIRE(ds != NULL);
00300         REQUIRE(ds->common.rdtype == 59);
00301 
00302         if (ds->mctx == NULL)
00303                 return;
00304 
00305         if (ds->digest != NULL)
00306                 isc_mem_free(ds->mctx, ds->digest);
00307         ds->mctx = NULL;
00308 }
00309 
00310 static inline isc_result_t
00311 additionaldata_cds(ARGS_ADDLDATA) {
00312         REQUIRE(rdata->type == 59);
00313 
00314         UNUSED(rdata);
00315         UNUSED(add);
00316         UNUSED(arg);
00317 
00318         return (ISC_R_SUCCESS);
00319 }
00320 
00321 static inline isc_result_t
00322 digest_cds(ARGS_DIGEST) {
00323         isc_region_t r;
00324 
00325         REQUIRE(rdata->type == 59);
00326 
00327         dns_rdata_toregion(rdata, &r);
00328 
00329         return ((digest)(arg, &r));
00330 }
00331 
00332 static inline isc_boolean_t
00333 checkowner_cds(ARGS_CHECKOWNER) {
00334 
00335         REQUIRE(type == 59);
00336 
00337         UNUSED(name);
00338         UNUSED(type);
00339         UNUSED(rdclass);
00340         UNUSED(wildcard);
00341 
00342         return (ISC_TRUE);
00343 }
00344 
00345 static inline isc_boolean_t
00346 checknames_cds(ARGS_CHECKNAMES) {
00347 
00348         REQUIRE(rdata->type == 59);
00349 
00350         UNUSED(rdata);
00351         UNUSED(owner);
00352         UNUSED(bad);
00353 
00354         return (ISC_TRUE);
00355 }
00356 
00357 static inline int
00358 casecompare_cds(ARGS_COMPARE) {
00359         return (compare_cds(rdata1, rdata2));
00360 }
00361 
00362 #endif  /* RDATA_GENERIC_CDS_59_C */

Generated on Tue Apr 28 17:40:59 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha