xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/generic/zonemd_63.c (revision b73bdc823b63c0911907a771adc13e97db3d9a8b)
1a944512fSflorian /*
2a944512fSflorian  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3a944512fSflorian  *
4a944512fSflorian  * Permission to use, copy, modify, and/or distribute this software for any
5a944512fSflorian  * purpose with or without fee is hereby granted, provided that the above
6a944512fSflorian  * copyright notice and this permission notice appear in all copies.
7a944512fSflorian  *
8a944512fSflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9a944512fSflorian  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10a944512fSflorian  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11a944512fSflorian  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12a944512fSflorian  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13a944512fSflorian  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14a944512fSflorian  * PERFORMANCE OF THIS SOFTWARE.
15a944512fSflorian  */
16a944512fSflorian 
17*b73bdc82Sjmc /* $Id: zonemd_63.c,v 1.2 2022/12/26 19:24:11 jmc Exp $ */
18a944512fSflorian 
19a944512fSflorian /* RFC8976 */
20a944512fSflorian 
21a944512fSflorian #ifndef RDATA_GENERIC_ZONEMD_63_C
22a944512fSflorian #define RDATA_GENERIC_ZONEMD_63_C
23a944512fSflorian 
24a944512fSflorian #define	DNS_ZONEMD_DIGEST_SHA384	(1)
25a944512fSflorian 
26a944512fSflorian static inline isc_result_t
totext_zonemd(ARGS_TOTEXT)27a944512fSflorian totext_zonemd(ARGS_TOTEXT) {
28a944512fSflorian 	isc_region_t sr;
29a944512fSflorian 	char buf[sizeof("4294967295 ")];
30a944512fSflorian 	uint32_t n;
31a944512fSflorian 
32a944512fSflorian 	REQUIRE(rdata->type == dns_rdatatype_zonemd);
33a944512fSflorian 	REQUIRE(rdata->length > 6);
34a944512fSflorian 
35a944512fSflorian 	dns_rdata_toregion(rdata, &sr);
36a944512fSflorian 
37a944512fSflorian 	/* serial */
38a944512fSflorian 	n = uint32_fromregion(&sr);
39a944512fSflorian 	isc_region_consume(&sr, 4);
40a944512fSflorian 	snprintf(buf, sizeof(buf), "%u ", n);
41a944512fSflorian 	RETERR(isc_str_tobuffer(buf, target));
42a944512fSflorian 
43a944512fSflorian 	/* scheme */
44a944512fSflorian 	n = uint8_fromregion(&sr);
45a944512fSflorian 	isc_region_consume(&sr, 1);
46a944512fSflorian 	snprintf(buf, sizeof(buf), "%u ", n);
47a944512fSflorian 	RETERR(isc_str_tobuffer(buf, target));
48a944512fSflorian 
49a944512fSflorian 	/* hash algo */
50a944512fSflorian 	n = uint8_fromregion(&sr);
51a944512fSflorian 	isc_region_consume(&sr, 1);
52a944512fSflorian 	snprintf(buf, sizeof(buf), "%u", n);
53a944512fSflorian 	RETERR(isc_str_tobuffer(buf, target));
54a944512fSflorian 
55a944512fSflorian 	/* Digest */
56a944512fSflorian 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
57a944512fSflorian 		RETERR(isc_str_tobuffer(" (", target));
58a944512fSflorian 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
59a944512fSflorian 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
60a944512fSflorian 		if (tctx->width == 0) /* No splitting */
61a944512fSflorian 			RETERR(isc_hex_totext(&sr, 0, "", target));
62a944512fSflorian 		else
63a944512fSflorian 			RETERR(isc_hex_totext(&sr, tctx->width - 2,
64a944512fSflorian 					      tctx->linebreak, target));
65a944512fSflorian 	} else
66a944512fSflorian 		RETERR(isc_str_tobuffer("[omitted]", target));
67a944512fSflorian 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
68a944512fSflorian 		RETERR(isc_str_tobuffer(" )", target));
69a944512fSflorian 	return (ISC_R_SUCCESS);
70a944512fSflorian }
71a944512fSflorian 
72a944512fSflorian static inline isc_result_t
fromwire_zonemd(ARGS_FROMWIRE)73a944512fSflorian fromwire_zonemd(ARGS_FROMWIRE) {
74a944512fSflorian 	isc_region_t sr;
75a944512fSflorian 
76a944512fSflorian 	REQUIRE(type == dns_rdatatype_zonemd);
77a944512fSflorian 
78a944512fSflorian 	UNUSED(type);
79a944512fSflorian 	UNUSED(rdclass);
80a944512fSflorian 	UNUSED(dctx);
81a944512fSflorian 	UNUSED(options);
82a944512fSflorian 
83a944512fSflorian 	isc_buffer_activeregion(source, &sr);
84a944512fSflorian 	/*
85a944512fSflorian 	 * serial: 4
86a944512fSflorian 	 * scheme: 1
87*b73bdc82Sjmc 	 * hash algorithm: 1
88a944512fSflorian 	 * digest: at least 1
89a944512fSflorian 	 */
90a944512fSflorian 	if (sr.length < 7)
91a944512fSflorian 		return (ISC_R_UNEXPECTEDEND);
92a944512fSflorian 
93a944512fSflorian 	if (sr.base[5] == DNS_ZONEMD_DIGEST_SHA384) {
94a944512fSflorian 		if (sr.length < 6 + ISC_SHA384_DIGESTLENGTH)
95a944512fSflorian 			return (ISC_R_UNEXPECTEDEND);
96a944512fSflorian 		else
97a944512fSflorian 			/* truncate in case there is additional junk */
98a944512fSflorian 			sr.length = 6 + ISC_SHA384_DIGESTLENGTH;
99a944512fSflorian 	}
100a944512fSflorian 
101a944512fSflorian 	isc_buffer_forward(source, sr.length);
102a944512fSflorian 	return (isc_mem_tobuffer(target, sr.base, sr.length));
103a944512fSflorian }
104a944512fSflorian 
105a944512fSflorian static inline isc_result_t
towire_zonemd(ARGS_TOWIRE)106a944512fSflorian towire_zonemd(ARGS_TOWIRE) {
107a944512fSflorian 	isc_region_t sr;
108a944512fSflorian 
109a944512fSflorian 	REQUIRE(rdata->type == dns_rdatatype_zonemd);
110a944512fSflorian 	REQUIRE(rdata->length != 0);
111a944512fSflorian 
112a944512fSflorian 	UNUSED(cctx);
113a944512fSflorian 
114a944512fSflorian 	dns_rdata_toregion(rdata, &sr);
115a944512fSflorian 	return (isc_mem_tobuffer(target, sr.base, sr.length));
116a944512fSflorian }
117a944512fSflorian 
118a944512fSflorian #endif	/* RDATA_GENERIC_ZONEMD_63_C */
119