15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*1fb015a8Sflorian /* $Id: hip_55.c,v 1.15 2020/09/14 08:40:43 florian Exp $ */
185185a700Sflorian
195185a700Sflorian /* reviewed: TBC */
205185a700Sflorian
215185a700Sflorian /* RFC 5205 */
225185a700Sflorian
235185a700Sflorian #ifndef RDATA_GENERIC_HIP_5_C
245185a700Sflorian #define RDATA_GENERIC_HIP_5_C
255185a700Sflorian
265185a700Sflorian static inline isc_result_t
totext_hip(ARGS_TOTEXT)275185a700Sflorian totext_hip(ARGS_TOTEXT) {
285185a700Sflorian isc_region_t region;
295185a700Sflorian dns_name_t name;
305185a700Sflorian unsigned int length, key_len, hit_len;
315185a700Sflorian unsigned char algorithm;
325185a700Sflorian char buf[sizeof("225 ")];
335185a700Sflorian
345185a700Sflorian REQUIRE(rdata->type == dns_rdatatype_hip);
355185a700Sflorian REQUIRE(rdata->length != 0);
365185a700Sflorian
375185a700Sflorian dns_rdata_toregion(rdata, ®ion);
385185a700Sflorian
395185a700Sflorian hit_len = uint8_fromregion(®ion);
405185a700Sflorian isc_region_consume(®ion, 1);
415185a700Sflorian
425185a700Sflorian algorithm = uint8_fromregion(®ion);
435185a700Sflorian isc_region_consume(®ion, 1);
445185a700Sflorian
455185a700Sflorian key_len = uint16_fromregion(®ion);
465185a700Sflorian isc_region_consume(®ion, 2);
475185a700Sflorian
485185a700Sflorian if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
49873f12b9Sflorian RETERR(isc_str_tobuffer("( ", target));
505185a700Sflorian
515185a700Sflorian /*
525185a700Sflorian * Algorithm
535185a700Sflorian */
545185a700Sflorian snprintf(buf, sizeof(buf), "%u ", algorithm);
55873f12b9Sflorian RETERR(isc_str_tobuffer(buf, target));
565185a700Sflorian
575185a700Sflorian /*
585185a700Sflorian * HIT.
595185a700Sflorian */
605185a700Sflorian INSIST(hit_len < region.length);
615185a700Sflorian length = region.length;
625185a700Sflorian region.length = hit_len;
635185a700Sflorian RETERR(isc_hex_totext(®ion, 1, "", target));
645185a700Sflorian region.length = length - hit_len;
65873f12b9Sflorian RETERR(isc_str_tobuffer(tctx->linebreak, target));
665185a700Sflorian
675185a700Sflorian /*
685185a700Sflorian * Public KEY.
695185a700Sflorian */
705185a700Sflorian INSIST(key_len <= region.length);
715185a700Sflorian length = region.length;
725185a700Sflorian region.length = key_len;
735185a700Sflorian RETERR(isc_base64_totext(®ion, 1, "", target));
745185a700Sflorian region.length = length - key_len;
75873f12b9Sflorian RETERR(isc_str_tobuffer(tctx->linebreak, target));
765185a700Sflorian
775185a700Sflorian /*
785185a700Sflorian * Rendezvous Servers.
795185a700Sflorian */
805185a700Sflorian dns_name_init(&name, NULL);
815185a700Sflorian while (region.length > 0) {
825185a700Sflorian dns_name_fromregion(&name, ®ion);
835185a700Sflorian
84*1fb015a8Sflorian RETERR(dns_name_totext(&name, 0, target));
855185a700Sflorian isc_region_consume(®ion, name.length);
865185a700Sflorian if (region.length > 0)
87873f12b9Sflorian RETERR(isc_str_tobuffer(tctx->linebreak, target));
885185a700Sflorian }
895185a700Sflorian if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
90873f12b9Sflorian RETERR(isc_str_tobuffer(" )", target));
915185a700Sflorian return (ISC_R_SUCCESS);
925185a700Sflorian }
935185a700Sflorian
945185a700Sflorian static inline isc_result_t
fromwire_hip(ARGS_FROMWIRE)955185a700Sflorian fromwire_hip(ARGS_FROMWIRE) {
965185a700Sflorian isc_region_t region, rr;
975185a700Sflorian dns_name_t name;
985185a700Sflorian uint8_t hit_len;
995185a700Sflorian uint16_t key_len;
1005185a700Sflorian
1015185a700Sflorian REQUIRE(type == dns_rdatatype_hip);
1025185a700Sflorian
1035185a700Sflorian UNUSED(type);
1045185a700Sflorian UNUSED(rdclass);
1055185a700Sflorian
1065185a700Sflorian isc_buffer_activeregion(source, ®ion);
1075185a700Sflorian if (region.length < 4U)
108fbc1dec8Sflorian return (DNS_R_FORMERR);
1095185a700Sflorian
1105185a700Sflorian rr = region;
1115185a700Sflorian hit_len = uint8_fromregion(®ion);
1125185a700Sflorian if (hit_len == 0)
113fbc1dec8Sflorian return (DNS_R_FORMERR);
1145185a700Sflorian isc_region_consume(®ion, 2); /* hit length + algorithm */
1155185a700Sflorian key_len = uint16_fromregion(®ion);
1165185a700Sflorian if (key_len == 0)
117fbc1dec8Sflorian return (DNS_R_FORMERR);
1185185a700Sflorian isc_region_consume(®ion, 2);
1195185a700Sflorian if (region.length < (unsigned) (hit_len + key_len))
120fbc1dec8Sflorian return (DNS_R_FORMERR);
1215185a700Sflorian
122637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, rr.base, 4 + hit_len + key_len));
1235185a700Sflorian isc_buffer_forward(source, 4 + hit_len + key_len);
1245185a700Sflorian
1255185a700Sflorian dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
1265185a700Sflorian while (isc_buffer_activelength(source) > 0) {
1275185a700Sflorian dns_name_init(&name, NULL);
1285185a700Sflorian RETERR(dns_name_fromwire(&name, source, dctx, options, target));
1295185a700Sflorian }
1305185a700Sflorian return (ISC_R_SUCCESS);
1315185a700Sflorian }
1325185a700Sflorian
1335185a700Sflorian static inline isc_result_t
towire_hip(ARGS_TOWIRE)1345185a700Sflorian towire_hip(ARGS_TOWIRE) {
1355185a700Sflorian isc_region_t region;
1365185a700Sflorian
1375185a700Sflorian REQUIRE(rdata->type == dns_rdatatype_hip);
1385185a700Sflorian REQUIRE(rdata->length != 0);
1395185a700Sflorian
1405185a700Sflorian UNUSED(cctx);
1415185a700Sflorian
1425185a700Sflorian dns_rdata_toregion(rdata, ®ion);
143637d8eb6Sflorian return (isc_mem_tobuffer(target, region.base, region.length));
1445185a700Sflorian }
1455185a700Sflorian #endif /* RDATA_GENERIC_HIP_5_C */
146