xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/generic/ipseckey_45.c (revision 1fb015a8af3a7e9b85db2510147a155826ef04d9)
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: ipseckey_45.c,v 1.14 2020/09/14 08:40:43 florian Exp $ */
18 
19 #ifndef RDATA_GENERIC_IPSECKEY_45_C
20 #define RDATA_GENERIC_IPSECKEY_45_C
21 
22 #include <string.h>
23 
24 static inline isc_result_t
totext_ipseckey(ARGS_TOTEXT)25 totext_ipseckey(ARGS_TOTEXT) {
26 	isc_region_t region;
27 	dns_name_t name;
28 	char buf[sizeof("255 ")];
29 	unsigned short num;
30 	unsigned short gateway;
31 
32 	REQUIRE(rdata->type == dns_rdatatype_ipseckey);
33 	REQUIRE(rdata->length >= 3);
34 
35 	dns_name_init(&name, NULL);
36 
37 	if (rdata->data[1] > 3U)
38 		return (ISC_R_NOTIMPLEMENTED);
39 
40 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
41 		RETERR(isc_str_tobuffer("( ", target));
42 
43 	/*
44 	 * Precedence.
45 	 */
46 	dns_rdata_toregion(rdata, &region);
47 	num = uint8_fromregion(&region);
48 	isc_region_consume(&region, 1);
49 	snprintf(buf, sizeof(buf), "%u ", num);
50 	RETERR(isc_str_tobuffer(buf, target));
51 
52 	/*
53 	 * Gateway type.
54 	 */
55 	gateway = uint8_fromregion(&region);
56 	isc_region_consume(&region, 1);
57 	snprintf(buf, sizeof(buf), "%u ", gateway);
58 	RETERR(isc_str_tobuffer(buf, target));
59 
60 	/*
61 	 * Algorithm.
62 	 */
63 	num = uint8_fromregion(&region);
64 	isc_region_consume(&region, 1);
65 	snprintf(buf, sizeof(buf), "%u ", num);
66 	RETERR(isc_str_tobuffer(buf, target));
67 
68 	/*
69 	 * Gateway.
70 	 */
71 	switch (gateway) {
72 	case 0:
73 		RETERR(isc_str_tobuffer(".", target));
74 		break;
75 
76 	case 1:
77 		RETERR(inet_totext(AF_INET, &region, target));
78 		isc_region_consume(&region, 4);
79 		break;
80 
81 	case 2:
82 		RETERR(inet_totext(AF_INET6, &region, target));
83 		isc_region_consume(&region, 16);
84 		break;
85 
86 	case 3:
87 		dns_name_fromregion(&name, &region);
88 		RETERR(dns_name_totext(&name, 0, target));
89 		isc_region_consume(&region, name_length(&name));
90 		break;
91 	}
92 
93 	/*
94 	 * Key.
95 	 */
96 	if (region.length > 0U) {
97 		RETERR(isc_str_tobuffer(tctx->linebreak, target));
98 		if (tctx->width == 0)   /* No splitting */
99 			RETERR(isc_base64_totext(&region, 60, "", target));
100 		else
101 			RETERR(isc_base64_totext(&region, tctx->width - 2,
102 						 tctx->linebreak, target));
103 	}
104 
105 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
106 		RETERR(isc_str_tobuffer(" )", target));
107 	return (ISC_R_SUCCESS);
108 }
109 
110 static inline isc_result_t
fromwire_ipseckey(ARGS_FROMWIRE)111 fromwire_ipseckey(ARGS_FROMWIRE) {
112 	dns_name_t name;
113 	isc_region_t region;
114 
115 	REQUIRE(type == dns_rdatatype_ipseckey);
116 
117 	UNUSED(type);
118 	UNUSED(rdclass);
119 
120 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
121 
122 	dns_name_init(&name, NULL);
123 
124 	isc_buffer_activeregion(source, &region);
125 	if (region.length < 3)
126 		return (ISC_R_UNEXPECTEDEND);
127 
128 	switch (region.base[1]) {
129 	case 0:
130 		isc_buffer_forward(source, region.length);
131 		return (isc_mem_tobuffer(target, region.base, region.length));
132 
133 	case 1:
134 		if (region.length < 7)
135 			return (ISC_R_UNEXPECTEDEND);
136 		isc_buffer_forward(source, region.length);
137 		return (isc_mem_tobuffer(target, region.base, region.length));
138 
139 	case 2:
140 		if (region.length < 19)
141 			return (ISC_R_UNEXPECTEDEND);
142 		isc_buffer_forward(source, region.length);
143 		return (isc_mem_tobuffer(target, region.base, region.length));
144 
145 	case 3:
146 		RETERR(isc_mem_tobuffer(target, region.base, 3));
147 		isc_buffer_forward(source, 3);
148 		RETERR(dns_name_fromwire(&name, source, dctx, options, target));
149 		isc_buffer_activeregion(source, &region);
150 		isc_buffer_forward(source, region.length);
151 		return(isc_mem_tobuffer(target, region.base, region.length));
152 
153 	default:
154 		return (ISC_R_NOTIMPLEMENTED);
155 	}
156 }
157 
158 static inline isc_result_t
towire_ipseckey(ARGS_TOWIRE)159 towire_ipseckey(ARGS_TOWIRE) {
160 	isc_region_t region;
161 
162 	REQUIRE(rdata->type == dns_rdatatype_ipseckey);
163 	REQUIRE(rdata->length != 0);
164 
165 	UNUSED(cctx);
166 
167 	dns_rdata_toregion(rdata, &region);
168 	return (isc_mem_tobuffer(target, region.base, region.length));
169 }
170 
171 #endif	/* RDATA_GENERIC_IPSECKEY_45_C */
172