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: zonemd_63.c,v 1.1 2021/04/02 06:37:40 florian Exp $ */
18 
19 /* RFC8976 */
20 
21 #ifndef RDATA_GENERIC_ZONEMD_63_C
22 #define RDATA_GENERIC_ZONEMD_63_C
23 
24 #define	DNS_ZONEMD_DIGEST_SHA384	(1)
25 
26 static inline isc_result_t
27 totext_zonemd(ARGS_TOTEXT) {
28 	isc_region_t sr;
29 	char buf[sizeof("4294967295 ")];
30 	uint32_t n;
31 
32 	REQUIRE(rdata->type == dns_rdatatype_zonemd);
33 	REQUIRE(rdata->length > 6);
34 
35 	dns_rdata_toregion(rdata, &sr);
36 
37 	/* serial */
38 	n = uint32_fromregion(&sr);
39 	isc_region_consume(&sr, 4);
40 	snprintf(buf, sizeof(buf), "%u ", n);
41 	RETERR(isc_str_tobuffer(buf, target));
42 
43 	/* scheme */
44 	n = uint8_fromregion(&sr);
45 	isc_region_consume(&sr, 1);
46 	snprintf(buf, sizeof(buf), "%u ", n);
47 	RETERR(isc_str_tobuffer(buf, target));
48 
49 	/* hash algo */
50 	n = uint8_fromregion(&sr);
51 	isc_region_consume(&sr, 1);
52 	snprintf(buf, sizeof(buf), "%u", n);
53 	RETERR(isc_str_tobuffer(buf, target));
54 
55 	/* Digest */
56 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
57 		RETERR(isc_str_tobuffer(" (", target));
58 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
59 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
60 		if (tctx->width == 0) /* No splitting */
61 			RETERR(isc_hex_totext(&sr, 0, "", target));
62 		else
63 			RETERR(isc_hex_totext(&sr, tctx->width - 2,
64 					      tctx->linebreak, target));
65 	} else
66 		RETERR(isc_str_tobuffer("[omitted]", target));
67 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
68 		RETERR(isc_str_tobuffer(" )", target));
69 	return (ISC_R_SUCCESS);
70 }
71 
72 static inline isc_result_t
73 fromwire_zonemd(ARGS_FROMWIRE) {
74 	isc_region_t sr;
75 
76 	REQUIRE(type == dns_rdatatype_zonemd);
77 
78 	UNUSED(type);
79 	UNUSED(rdclass);
80 	UNUSED(dctx);
81 	UNUSED(options);
82 
83 	isc_buffer_activeregion(source, &sr);
84 	/*
85 	 * serial: 4
86 	 * scheme: 1
87 	 * hash algorith: 1
88 	 * digest: at least 1
89 	 */
90 	if (sr.length < 7)
91 		return (ISC_R_UNEXPECTEDEND);
92 
93 	if (sr.base[5] == DNS_ZONEMD_DIGEST_SHA384) {
94 		if (sr.length < 6 + ISC_SHA384_DIGESTLENGTH)
95 			return (ISC_R_UNEXPECTEDEND);
96 		else
97 			/* truncate in case there is additional junk */
98 			sr.length = 6 + ISC_SHA384_DIGESTLENGTH;
99 	}
100 
101 	isc_buffer_forward(source, sr.length);
102 	return (isc_mem_tobuffer(target, sr.base, sr.length));
103 }
104 
105 static inline isc_result_t
106 towire_zonemd(ARGS_TOWIRE) {
107 	isc_region_t sr;
108 
109 	REQUIRE(rdata->type == dns_rdatatype_zonemd);
110 	REQUIRE(rdata->length != 0);
111 
112 	UNUSED(cctx);
113 
114 	dns_rdata_toregion(rdata, &sr);
115 	return (isc_mem_tobuffer(target, sr.base, sr.length));
116 }
117 
118 #endif	/* RDATA_GENERIC_ZONEMD_63_C */
119