xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/hip_55.c (revision 4331a4ca)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: hip_55.c,v 1.10 2020/02/24 17:47:50 florian Exp $ */
18 
19 /* reviewed: TBC */
20 
21 /* RFC 5205 */
22 
23 #ifndef RDATA_GENERIC_HIP_5_C
24 #define RDATA_GENERIC_HIP_5_C
25 
26 #define RRTYPE_HIP_ATTRIBUTES (0)
27 
28 static inline isc_result_t
29 totext_hip(ARGS_TOTEXT) {
30 	isc_region_t region;
31 	dns_name_t name;
32 	unsigned int length, key_len, hit_len;
33 	unsigned char algorithm;
34 	char buf[sizeof("225 ")];
35 
36 	REQUIRE(rdata->type == dns_rdatatype_hip);
37 	REQUIRE(rdata->length != 0);
38 
39 	dns_rdata_toregion(rdata, &region);
40 
41 	hit_len = uint8_fromregion(&region);
42 	isc_region_consume(&region, 1);
43 
44 	algorithm = uint8_fromregion(&region);
45 	isc_region_consume(&region, 1);
46 
47 	key_len = uint16_fromregion(&region);
48 	isc_region_consume(&region, 2);
49 
50 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
51 		RETERR(str_totext("( ", target));
52 
53 	/*
54 	 * Algorithm
55 	 */
56 	snprintf(buf, sizeof(buf), "%u ", algorithm);
57 	RETERR(str_totext(buf, target));
58 
59 	/*
60 	 * HIT.
61 	 */
62 	INSIST(hit_len < region.length);
63 	length = region.length;
64 	region.length = hit_len;
65 	RETERR(isc_hex_totext(&region, 1, "", target));
66 	region.length = length - hit_len;
67 	RETERR(str_totext(tctx->linebreak, target));
68 
69 	/*
70 	 * Public KEY.
71 	 */
72 	INSIST(key_len <= region.length);
73 	length = region.length;
74 	region.length = key_len;
75 	RETERR(isc_base64_totext(&region, 1, "", target));
76 	region.length = length - key_len;
77 	RETERR(str_totext(tctx->linebreak, target));
78 
79 	/*
80 	 * Rendezvous Servers.
81 	 */
82 	dns_name_init(&name, NULL);
83 	while (region.length > 0) {
84 		dns_name_fromregion(&name, &region);
85 
86 		RETERR(dns_name_totext(&name, ISC_FALSE, target));
87 		isc_region_consume(&region, name.length);
88 		if (region.length > 0)
89 			RETERR(str_totext(tctx->linebreak, target));
90 	}
91 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
92 		RETERR(str_totext(" )", target));
93 	return (ISC_R_SUCCESS);
94 }
95 
96 static inline isc_result_t
97 fromwire_hip(ARGS_FROMWIRE) {
98 	isc_region_t region, rr;
99 	dns_name_t name;
100 	uint8_t hit_len;
101 	uint16_t key_len;
102 
103 	REQUIRE(type == dns_rdatatype_hip);
104 
105 	UNUSED(type);
106 	UNUSED(rdclass);
107 
108 	isc_buffer_activeregion(source, &region);
109 	if (region.length < 4U)
110 		RETERR(DNS_R_FORMERR);
111 
112 	rr = region;
113 	hit_len = uint8_fromregion(&region);
114 	if (hit_len == 0)
115 		RETERR(DNS_R_FORMERR);
116 	isc_region_consume(&region, 2);  	/* hit length + algorithm */
117 	key_len = uint16_fromregion(&region);
118 	if (key_len == 0)
119 		RETERR(DNS_R_FORMERR);
120 	isc_region_consume(&region, 2);
121 	if (region.length < (unsigned) (hit_len + key_len))
122 		RETERR(DNS_R_FORMERR);
123 
124 	RETERR(mem_tobuffer(target, rr.base, 4 + hit_len + key_len));
125 	isc_buffer_forward(source, 4 + hit_len + key_len);
126 
127 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
128 	while (isc_buffer_activelength(source) > 0) {
129 		dns_name_init(&name, NULL);
130 		RETERR(dns_name_fromwire(&name, source, dctx, options, target));
131 	}
132 	return (ISC_R_SUCCESS);
133 }
134 
135 static inline isc_result_t
136 towire_hip(ARGS_TOWIRE) {
137 	isc_region_t region;
138 
139 	REQUIRE(rdata->type == dns_rdatatype_hip);
140 	REQUIRE(rdata->length != 0);
141 
142 	UNUSED(cctx);
143 
144 	dns_rdata_toregion(rdata, &region);
145 	return (mem_tobuffer(target, region.base, region.length));
146 }
147 #endif	/* RDATA_GENERIC_HIP_5_C */
148