tkey_249.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /*
00021  * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
00022  */
00023 
00024 /* draft-ietf-dnsext-tkey-01.txt */
00025 
00026 #ifndef RDATA_GENERIC_TKEY_249_C
00027 #define RDATA_GENERIC_TKEY_249_C
00028 
00029 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
00030 
00031 static inline isc_result_t
00032 fromtext_tkey(ARGS_FROMTEXT) {
00033         isc_token_t token;
00034         dns_rcode_t rcode;
00035         dns_name_t name;
00036         isc_buffer_t buffer;
00037         long i;
00038         char *e;
00039 
00040         REQUIRE(type == 249);
00041 
00042         UNUSED(type);
00043         UNUSED(rdclass);
00044         UNUSED(callbacks);
00045 
00046         /*
00047          * Algorithm.
00048          */
00049         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00050                                       ISC_FALSE));
00051         dns_name_init(&name, NULL);
00052         buffer_fromregion(&buffer, &token.value.as_region);
00053         origin = (origin != NULL) ? origin : dns_rootname;
00054         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
00055 
00056 
00057         /*
00058          * Inception.
00059          */
00060         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00061                                       ISC_FALSE));
00062         RETERR(uint32_tobuffer(token.value.as_ulong, target));
00063 
00064         /*
00065          * Expiration.
00066          */
00067         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00068                                       ISC_FALSE));
00069         RETERR(uint32_tobuffer(token.value.as_ulong, target));
00070 
00071         /*
00072          * Mode.
00073          */
00074         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00075                                       ISC_FALSE));
00076         if (token.value.as_ulong > 0xffffU)
00077                 RETTOK(ISC_R_RANGE);
00078         RETERR(uint16_tobuffer(token.value.as_ulong, target));
00079 
00080         /*
00081          * Error.
00082          */
00083         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00084                                       ISC_FALSE));
00085         if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
00086                                 != ISC_R_SUCCESS)
00087         {
00088                 i = strtol(DNS_AS_STR(token), &e, 10);
00089                 if (*e != 0)
00090                         RETTOK(DNS_R_UNKNOWN);
00091                 if (i < 0 || i > 0xffff)
00092                         RETTOK(ISC_R_RANGE);
00093                 rcode = (dns_rcode_t)i;
00094         }
00095         RETERR(uint16_tobuffer(rcode, target));
00096 
00097         /*
00098          * Key Size.
00099          */
00100         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00101                                       ISC_FALSE));
00102         if (token.value.as_ulong > 0xffffU)
00103                 RETTOK(ISC_R_RANGE);
00104         RETERR(uint16_tobuffer(token.value.as_ulong, target));
00105 
00106         /*
00107          * Key Data.
00108          */
00109         RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
00110 
00111         /*
00112          * Other Size.
00113          */
00114         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
00115                                       ISC_FALSE));
00116         if (token.value.as_ulong > 0xffffU)
00117                 RETTOK(ISC_R_RANGE);
00118         RETERR(uint16_tobuffer(token.value.as_ulong, target));
00119 
00120         /*
00121          * Other Data.
00122          */
00123         return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
00124 }
00125 
00126 static inline isc_result_t
00127 totext_tkey(ARGS_TOTEXT) {
00128         isc_region_t sr, dr;
00129         char buf[sizeof("4294967295 ")];
00130         unsigned long n;
00131         dns_name_t name;
00132         dns_name_t prefix;
00133         isc_boolean_t sub;
00134 
00135         REQUIRE(rdata->type == 249);
00136         REQUIRE(rdata->length != 0);
00137 
00138         dns_rdata_toregion(rdata, &sr);
00139 
00140         /*
00141          * Algorithm.
00142          */
00143         dns_name_init(&name, NULL);
00144         dns_name_init(&prefix, NULL);
00145         dns_name_fromregion(&name, &sr);
00146         sub = name_prefix(&name, tctx->origin, &prefix);
00147         RETERR(dns_name_totext(&prefix, sub, target));
00148         RETERR(str_totext(" ", target));
00149         isc_region_consume(&sr, name_length(&name));
00150 
00151         /*
00152          * Inception.
00153          */
00154         n = uint32_fromregion(&sr);
00155         isc_region_consume(&sr, 4);
00156         sprintf(buf, "%lu ", n);
00157         RETERR(str_totext(buf, target));
00158 
00159         /*
00160          * Expiration.
00161          */
00162         n = uint32_fromregion(&sr);
00163         isc_region_consume(&sr, 4);
00164         sprintf(buf, "%lu ", n);
00165         RETERR(str_totext(buf, target));
00166 
00167         /*
00168          * Mode.
00169          */
00170         n = uint16_fromregion(&sr);
00171         isc_region_consume(&sr, 2);
00172         sprintf(buf, "%lu ", n);
00173         RETERR(str_totext(buf, target));
00174 
00175         /*
00176          * Error.
00177          */
00178         n = uint16_fromregion(&sr);
00179         isc_region_consume(&sr, 2);
00180         if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
00181                 RETERR(str_totext(" ", target));
00182         else {
00183                 sprintf(buf, "%lu ", n);
00184                 RETERR(str_totext(buf, target));
00185         }
00186 
00187         /*
00188          * Key Size.
00189          */
00190         n = uint16_fromregion(&sr);
00191         isc_region_consume(&sr, 2);
00192         sprintf(buf, "%lu", n);
00193         RETERR(str_totext(buf, target));
00194 
00195         /*
00196          * Key Data.
00197          */
00198         REQUIRE(n <= sr.length);
00199         dr = sr;
00200         dr.length = n;
00201         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00202                 RETERR(str_totext(" (", target));
00203         RETERR(str_totext(tctx->linebreak, target));
00204         if (tctx->width == 0)   /* No splitting */
00205                 RETERR(isc_base64_totext(&dr, 60, "", target));
00206         else
00207                 RETERR(isc_base64_totext(&dr, tctx->width - 2,
00208                                          tctx->linebreak, target));
00209         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00210                 RETERR(str_totext(" ) ", target));
00211         else
00212                 RETERR(str_totext(" ", target));
00213         isc_region_consume(&sr, n);
00214 
00215         /*
00216          * Other Size.
00217          */
00218         n = uint16_fromregion(&sr);
00219         isc_region_consume(&sr, 2);
00220         sprintf(buf, "%lu", n);
00221         RETERR(str_totext(buf, target));
00222 
00223         /*
00224          * Other Data.
00225          */
00226         REQUIRE(n <= sr.length);
00227         if (n != 0U) {
00228             dr = sr;
00229             dr.length = n;
00230             if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00231                     RETERR(str_totext(" (", target));
00232             RETERR(str_totext(tctx->linebreak, target));
00233                 if (tctx->width == 0)   /* No splitting */
00234                         RETERR(isc_base64_totext(&dr, 60, "", target));
00235                 else
00236                         RETERR(isc_base64_totext(&dr, tctx->width - 2,
00237                                                  tctx->linebreak, target));
00238             if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00239                     RETERR(str_totext(" )", target));
00240         }
00241         return (ISC_R_SUCCESS);
00242 }
00243 
00244 static inline isc_result_t
00245 fromwire_tkey(ARGS_FROMWIRE) {
00246         isc_region_t sr;
00247         unsigned long n;
00248         dns_name_t name;
00249 
00250         REQUIRE(type == 249);
00251 
00252         UNUSED(type);
00253         UNUSED(rdclass);
00254 
00255         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
00256 
00257         /*
00258          * Algorithm.
00259          */
00260         dns_name_init(&name, NULL);
00261         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
00262 
00263         /*
00264          * Inception: 4
00265          * Expiration: 4
00266          * Mode: 2
00267          * Error: 2
00268          */
00269         isc_buffer_activeregion(source, &sr);
00270         if (sr.length < 12)
00271                 return (ISC_R_UNEXPECTEDEND);
00272         RETERR(mem_tobuffer(target, sr.base, 12));
00273         isc_region_consume(&sr, 12);
00274         isc_buffer_forward(source, 12);
00275 
00276         /*
00277          * Key Length + Key Data.
00278          */
00279         if (sr.length < 2)
00280                 return (ISC_R_UNEXPECTEDEND);
00281         n = uint16_fromregion(&sr);
00282         if (sr.length < n + 2)
00283                 return (ISC_R_UNEXPECTEDEND);
00284         RETERR(mem_tobuffer(target, sr.base, n + 2));
00285         isc_region_consume(&sr, n + 2);
00286         isc_buffer_forward(source, n + 2);
00287 
00288         /*
00289          * Other Length + Other Data.
00290          */
00291         if (sr.length < 2)
00292                 return (ISC_R_UNEXPECTEDEND);
00293         n = uint16_fromregion(&sr);
00294         if (sr.length < n + 2)
00295                 return (ISC_R_UNEXPECTEDEND);
00296         isc_buffer_forward(source, n + 2);
00297         return (mem_tobuffer(target, sr.base, n + 2));
00298 }
00299 
00300 static inline isc_result_t
00301 towire_tkey(ARGS_TOWIRE) {
00302         isc_region_t sr;
00303         dns_name_t name;
00304         dns_offsets_t offsets;
00305 
00306         REQUIRE(rdata->type == 249);
00307         REQUIRE(rdata->length != 0);
00308 
00309         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
00310         /*
00311          * Algorithm.
00312          */
00313         dns_rdata_toregion(rdata, &sr);
00314         dns_name_init(&name, offsets);
00315         dns_name_fromregion(&name, &sr);
00316         RETERR(dns_name_towire(&name, cctx, target));
00317         isc_region_consume(&sr, name_length(&name));
00318 
00319         return (mem_tobuffer(target, sr.base, sr.length));
00320 }
00321 
00322 static inline int
00323 compare_tkey(ARGS_COMPARE) {
00324         isc_region_t r1;
00325         isc_region_t r2;
00326         dns_name_t name1;
00327         dns_name_t name2;
00328         int order;
00329 
00330         REQUIRE(rdata1->type == rdata2->type);
00331         REQUIRE(rdata1->rdclass == rdata2->rdclass);
00332         REQUIRE(rdata1->type == 249);
00333         REQUIRE(rdata1->length != 0);
00334         REQUIRE(rdata2->length != 0);
00335 
00336         /*
00337          * Algorithm.
00338          */
00339         dns_rdata_toregion(rdata1, &r1);
00340         dns_rdata_toregion(rdata2, &r2);
00341         dns_name_init(&name1, NULL);
00342         dns_name_init(&name2, NULL);
00343         dns_name_fromregion(&name1, &r1);
00344         dns_name_fromregion(&name2, &r2);
00345         if ((order = dns_name_rdatacompare(&name1, &name2)) != 0)
00346                 return (order);
00347         isc_region_consume(&r1, name_length(&name1));
00348         isc_region_consume(&r2, name_length(&name2));
00349         return (isc_region_compare(&r1, &r2));
00350 }
00351 
00352 static inline isc_result_t
00353 fromstruct_tkey(ARGS_FROMSTRUCT) {
00354         dns_rdata_tkey_t *tkey = source;
00355 
00356         REQUIRE(type == 249);
00357         REQUIRE(source != NULL);
00358         REQUIRE(tkey->common.rdtype == type);
00359         REQUIRE(tkey->common.rdclass == rdclass);
00360 
00361         UNUSED(type);
00362         UNUSED(rdclass);
00363 
00364         /*
00365          * Algorithm Name.
00366          */
00367         RETERR(name_tobuffer(&tkey->algorithm, target));
00368 
00369         /*
00370          * Inception: 32 bits.
00371          */
00372         RETERR(uint32_tobuffer(tkey->inception, target));
00373 
00374         /*
00375          * Expire: 32 bits.
00376          */
00377         RETERR(uint32_tobuffer(tkey->expire, target));
00378 
00379         /*
00380          * Mode: 16 bits.
00381          */
00382         RETERR(uint16_tobuffer(tkey->mode, target));
00383 
00384         /*
00385          * Error: 16 bits.
00386          */
00387         RETERR(uint16_tobuffer(tkey->error, target));
00388 
00389         /*
00390          * Key size: 16 bits.
00391          */
00392         RETERR(uint16_tobuffer(tkey->keylen, target));
00393 
00394         /*
00395          * Key.
00396          */
00397         RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
00398 
00399         /*
00400          * Other size: 16 bits.
00401          */
00402         RETERR(uint16_tobuffer(tkey->otherlen, target));
00403 
00404         /*
00405          * Other data.
00406          */
00407         return (mem_tobuffer(target, tkey->other, tkey->otherlen));
00408 }
00409 
00410 static inline isc_result_t
00411 tostruct_tkey(ARGS_TOSTRUCT) {
00412         dns_rdata_tkey_t *tkey = target;
00413         dns_name_t alg;
00414         isc_region_t sr;
00415 
00416         REQUIRE(rdata->type == 249);
00417         REQUIRE(target != NULL);
00418         REQUIRE(rdata->length != 0);
00419 
00420         tkey->common.rdclass = rdata->rdclass;
00421         tkey->common.rdtype = rdata->type;
00422         ISC_LINK_INIT(&tkey->common, link);
00423 
00424         dns_rdata_toregion(rdata, &sr);
00425 
00426         /*
00427          * Algorithm Name.
00428          */
00429         dns_name_init(&alg, NULL);
00430         dns_name_fromregion(&alg, &sr);
00431         dns_name_init(&tkey->algorithm, NULL);
00432         RETERR(name_duporclone(&alg, mctx, &tkey->algorithm));
00433         isc_region_consume(&sr, name_length(&tkey->algorithm));
00434 
00435         /*
00436          * Inception.
00437          */
00438         tkey->inception = uint32_fromregion(&sr);
00439         isc_region_consume(&sr, 4);
00440 
00441         /*
00442          * Expire.
00443          */
00444         tkey->expire = uint32_fromregion(&sr);
00445         isc_region_consume(&sr, 4);
00446 
00447         /*
00448          * Mode.
00449          */
00450         tkey->mode = uint16_fromregion(&sr);
00451         isc_region_consume(&sr, 2);
00452 
00453         /*
00454          * Error.
00455          */
00456         tkey->error = uint16_fromregion(&sr);
00457         isc_region_consume(&sr, 2);
00458 
00459         /*
00460          * Key size.
00461          */
00462         tkey->keylen = uint16_fromregion(&sr);
00463         isc_region_consume(&sr, 2);
00464 
00465         /*
00466          * Key.
00467          */
00468         INSIST(tkey->keylen + 2U <= sr.length);
00469         tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
00470         if (tkey->key == NULL)
00471                 goto cleanup;
00472         isc_region_consume(&sr, tkey->keylen);
00473 
00474         /*
00475          * Other size.
00476          */
00477         tkey->otherlen = uint16_fromregion(&sr);
00478         isc_region_consume(&sr, 2);
00479 
00480         /*
00481          * Other.
00482          */
00483         INSIST(tkey->otherlen <= sr.length);
00484         tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
00485         if (tkey->other == NULL)
00486                 goto cleanup;
00487 
00488         tkey->mctx = mctx;
00489         return (ISC_R_SUCCESS);
00490 
00491  cleanup:
00492         if (mctx != NULL)
00493                 dns_name_free(&tkey->algorithm, mctx);
00494         if (mctx != NULL && tkey->key != NULL)
00495                 isc_mem_free(mctx, tkey->key);
00496         return (ISC_R_NOMEMORY);
00497 }
00498 
00499 static inline void
00500 freestruct_tkey(ARGS_FREESTRUCT) {
00501         dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
00502 
00503         REQUIRE(source != NULL);
00504 
00505         if (tkey->mctx == NULL)
00506                 return;
00507 
00508         dns_name_free(&tkey->algorithm, tkey->mctx);
00509         if (tkey->key != NULL)
00510                 isc_mem_free(tkey->mctx, tkey->key);
00511         if (tkey->other != NULL)
00512                 isc_mem_free(tkey->mctx, tkey->other);
00513         tkey->mctx = NULL;
00514 }
00515 
00516 static inline isc_result_t
00517 additionaldata_tkey(ARGS_ADDLDATA) {
00518         UNUSED(rdata);
00519         UNUSED(add);
00520         UNUSED(arg);
00521 
00522         REQUIRE(rdata->type == 249);
00523 
00524         return (ISC_R_SUCCESS);
00525 }
00526 
00527 static inline isc_result_t
00528 digest_tkey(ARGS_DIGEST) {
00529         UNUSED(rdata);
00530         UNUSED(digest);
00531         UNUSED(arg);
00532 
00533         REQUIRE(rdata->type == 249);
00534 
00535         return (ISC_R_NOTIMPLEMENTED);
00536 }
00537 
00538 static inline isc_boolean_t
00539 checkowner_tkey(ARGS_CHECKOWNER) {
00540 
00541         REQUIRE(type == 249);
00542 
00543         UNUSED(name);
00544         UNUSED(type);
00545         UNUSED(rdclass);
00546         UNUSED(wildcard);
00547 
00548         return (ISC_TRUE);
00549 }
00550 
00551 static inline isc_boolean_t
00552 checknames_tkey(ARGS_CHECKNAMES) {
00553 
00554         REQUIRE(rdata->type == 249);
00555 
00556         UNUSED(rdata);
00557         UNUSED(owner);
00558         UNUSED(bad);
00559 
00560         return (ISC_TRUE);
00561 }
00562 
00563 static inline isc_result_t
00564 casecompare_tkey(ARGS_COMPARE) {
00565         return (compare_tkey(rdata1, rdata2));
00566 }
00567 #endif  /* RDATA_GENERIC_TKEY_249_C */

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