xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/generic/key_25.c (revision 873f12b9e6aaf39ba104db6af3d0dc687cd95f7e)
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*873f12b9Sflorian /* $Id: key_25.c,v 1.13 2020/02/26 18:47:59 florian Exp $ */
185185a700Sflorian 
195185a700Sflorian /*
205185a700Sflorian  * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
215185a700Sflorian  */
225185a700Sflorian 
235185a700Sflorian /* RFC2535 */
245185a700Sflorian 
255185a700Sflorian #ifndef RDATA_GENERIC_KEY_25_C
265185a700Sflorian #define RDATA_GENERIC_KEY_25_C
275185a700Sflorian 
285185a700Sflorian #include <dst/dst.h>
295185a700Sflorian 
305185a700Sflorian static inline isc_result_t
generic_totext_key(ARGS_TOTEXT)315185a700Sflorian generic_totext_key(ARGS_TOTEXT) {
325185a700Sflorian 	isc_region_t sr;
335185a700Sflorian 	char buf[sizeof("[key id = 64000]")];
345185a700Sflorian 	unsigned int flags;
355185a700Sflorian 	unsigned char algorithm;
365185a700Sflorian 	char algbuf[DNS_NAME_FORMATSIZE];
375185a700Sflorian 	const char *keyinfo;
385185a700Sflorian 	isc_region_t tmpr;
395185a700Sflorian 
405185a700Sflorian 	REQUIRE(rdata->length != 0);
415185a700Sflorian 
425185a700Sflorian 	dns_rdata_toregion(rdata, &sr);
435185a700Sflorian 
445185a700Sflorian 	/* flags */
455185a700Sflorian 	flags = uint16_fromregion(&sr);
465185a700Sflorian 	isc_region_consume(&sr, 2);
475185a700Sflorian 	snprintf(buf, sizeof(buf), "%u", flags);
48*873f12b9Sflorian 	RETERR(isc_str_tobuffer(buf, target));
49*873f12b9Sflorian 	RETERR(isc_str_tobuffer(" ", target));
505185a700Sflorian 	if ((flags & DNS_KEYFLAG_KSK) != 0) {
515185a700Sflorian 		if (flags & DNS_KEYFLAG_REVOKE)
525185a700Sflorian 			keyinfo = "revoked KSK";
535185a700Sflorian 		else
545185a700Sflorian 			keyinfo = "KSK";
555185a700Sflorian 	} else
565185a700Sflorian 		keyinfo = "ZSK";
575185a700Sflorian 
585185a700Sflorian 	/* protocol */
595185a700Sflorian 	snprintf(buf, sizeof(buf), "%u", sr.base[0]);
605185a700Sflorian 	isc_region_consume(&sr, 1);
61*873f12b9Sflorian 	RETERR(isc_str_tobuffer(buf, target));
62*873f12b9Sflorian 	RETERR(isc_str_tobuffer(" ", target));
635185a700Sflorian 
645185a700Sflorian 	/* algorithm */
655185a700Sflorian 	algorithm = sr.base[0];
665185a700Sflorian 	snprintf(buf, sizeof(buf), "%u", algorithm);
675185a700Sflorian 	isc_region_consume(&sr, 1);
68*873f12b9Sflorian 	RETERR(isc_str_tobuffer(buf, target));
695185a700Sflorian 
705185a700Sflorian 	/* No Key? */
715185a700Sflorian 	if ((flags & 0xc000) == 0xc000)
725185a700Sflorian 		return (ISC_R_SUCCESS);
735185a700Sflorian 
745185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0 &&
755185a700Sflorian 	     algorithm == DNS_KEYALG_PRIVATEDNS) {
765185a700Sflorian 		dns_name_t name;
775185a700Sflorian 		dns_name_init(&name, NULL);
785185a700Sflorian 		dns_name_fromregion(&name, &sr);
795185a700Sflorian 		dns_name_format(&name, algbuf, sizeof(algbuf));
805185a700Sflorian 	} else {
815185a700Sflorian 		dns_secalg_format((dns_secalg_t) algorithm, algbuf,
825185a700Sflorian 				  sizeof(algbuf));
835185a700Sflorian 	}
845185a700Sflorian 
855185a700Sflorian 	/* key */
865185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
87*873f12b9Sflorian 		RETERR(isc_str_tobuffer(" (", target));
88*873f12b9Sflorian 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
895185a700Sflorian 
905185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
915185a700Sflorian 		if (tctx->width == 0)   /* No splitting */
925185a700Sflorian 			RETERR(isc_base64_totext(&sr, 60, "", target));
935185a700Sflorian 		else
945185a700Sflorian 			RETERR(isc_base64_totext(&sr, tctx->width - 2,
955185a700Sflorian 						 tctx->linebreak, target));
965185a700Sflorian 	} else {
975185a700Sflorian 		dns_rdata_toregion(rdata, &tmpr);
985185a700Sflorian 		snprintf(buf, sizeof(buf), "[key id = %u]",
995185a700Sflorian 			 dst_region_computeid(&tmpr, algorithm));
100*873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
1015185a700Sflorian 	}
1025185a700Sflorian 
1035185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0)
104*873f12b9Sflorian 		RETERR(isc_str_tobuffer(tctx->linebreak, target));
1055185a700Sflorian 	else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
106*873f12b9Sflorian 		RETERR(isc_str_tobuffer(" ", target));
1075185a700Sflorian 
1085185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
109*873f12b9Sflorian 		RETERR(isc_str_tobuffer(")", target));
1105185a700Sflorian 
1115185a700Sflorian 	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0) {
1125185a700Sflorian 
1135185a700Sflorian 		if (rdata->type == dns_rdatatype_dnskey ||
1145185a700Sflorian 		    rdata->type == dns_rdatatype_cdnskey) {
115*873f12b9Sflorian 			RETERR(isc_str_tobuffer(" ; ", target));
116*873f12b9Sflorian 			RETERR(isc_str_tobuffer(keyinfo, target));
1175185a700Sflorian 		}
118*873f12b9Sflorian 		RETERR(isc_str_tobuffer("; alg = ", target));
119*873f12b9Sflorian 		RETERR(isc_str_tobuffer(algbuf, target));
120*873f12b9Sflorian 		RETERR(isc_str_tobuffer(" ; key id = ", target));
1215185a700Sflorian 		dns_rdata_toregion(rdata, &tmpr);
1225185a700Sflorian 		snprintf(buf, sizeof(buf), "%u",
1235185a700Sflorian 			 dst_region_computeid(&tmpr, algorithm));
124*873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
1255185a700Sflorian 	}
1265185a700Sflorian 	return (ISC_R_SUCCESS);
1275185a700Sflorian }
1285185a700Sflorian 
1295185a700Sflorian static inline isc_result_t
generic_fromwire_key(ARGS_FROMWIRE)1305185a700Sflorian generic_fromwire_key(ARGS_FROMWIRE) {
1315185a700Sflorian 	unsigned char algorithm;
1325185a700Sflorian 	isc_region_t sr;
1335185a700Sflorian 
1345185a700Sflorian 	UNUSED(type);
1355185a700Sflorian 	UNUSED(rdclass);
1365185a700Sflorian 	UNUSED(dctx);
1375185a700Sflorian 	UNUSED(options);
1385185a700Sflorian 
1395185a700Sflorian 	isc_buffer_activeregion(source, &sr);
1405185a700Sflorian 	if (sr.length < 4)
1415185a700Sflorian 		return (ISC_R_UNEXPECTEDEND);
1425185a700Sflorian 
1435185a700Sflorian 	algorithm = sr.base[3];
144637d8eb6Sflorian 	RETERR(isc_mem_tobuffer(target, sr.base, 4));
1455185a700Sflorian 	isc_region_consume(&sr, 4);
1465185a700Sflorian 	isc_buffer_forward(source, 4);
1475185a700Sflorian 
1485185a700Sflorian 	if (algorithm == DNS_KEYALG_PRIVATEDNS) {
1495185a700Sflorian 		dns_name_t name;
1505185a700Sflorian 		dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
1515185a700Sflorian 		dns_name_init(&name, NULL);
1525185a700Sflorian 		RETERR(dns_name_fromwire(&name, source, dctx, options, target));
1535185a700Sflorian 	}
1545185a700Sflorian 
1555185a700Sflorian 	/*
1565185a700Sflorian 	 * RSAMD5 computes key ID differently from other
1575185a700Sflorian 	 * algorithms: we need to ensure there's enough data
1585185a700Sflorian 	 * present for the computation
1595185a700Sflorian 	 */
1605185a700Sflorian 	if (algorithm == DST_ALG_RSAMD5 && sr.length < 3)
1615185a700Sflorian 		return (ISC_R_UNEXPECTEDEND);
1625185a700Sflorian 
1635185a700Sflorian 	isc_buffer_activeregion(source, &sr);
1645185a700Sflorian 	isc_buffer_forward(source, sr.length);
165637d8eb6Sflorian 	return (isc_mem_tobuffer(target, sr.base, sr.length));
1665185a700Sflorian }
1675185a700Sflorian 
1685185a700Sflorian static inline isc_result_t
totext_key(ARGS_TOTEXT)1695185a700Sflorian totext_key(ARGS_TOTEXT) {
1705185a700Sflorian 
1715185a700Sflorian 	REQUIRE(rdata != NULL);
1725185a700Sflorian 	REQUIRE(rdata->type == dns_rdatatype_key);
1735185a700Sflorian 
1745185a700Sflorian 	return (generic_totext_key(rdata, tctx, target));
1755185a700Sflorian }
1765185a700Sflorian 
1775185a700Sflorian static inline isc_result_t
fromwire_key(ARGS_FROMWIRE)1785185a700Sflorian fromwire_key(ARGS_FROMWIRE) {
1795185a700Sflorian 
1805185a700Sflorian 	REQUIRE(type == dns_rdatatype_key);
1815185a700Sflorian 
1825185a700Sflorian 	return (generic_fromwire_key(rdclass, type, source, dctx,
1835185a700Sflorian 				     options, target));
1845185a700Sflorian }
1855185a700Sflorian 
1865185a700Sflorian static inline isc_result_t
towire_key(ARGS_TOWIRE)1875185a700Sflorian towire_key(ARGS_TOWIRE) {
1885185a700Sflorian 	isc_region_t sr;
1895185a700Sflorian 
1905185a700Sflorian 	REQUIRE(rdata != NULL);
1915185a700Sflorian 	REQUIRE(rdata->type == dns_rdatatype_key);
1925185a700Sflorian 	REQUIRE(rdata->length != 0);
1935185a700Sflorian 
1945185a700Sflorian 	UNUSED(cctx);
1955185a700Sflorian 
1965185a700Sflorian 	dns_rdata_toregion(rdata, &sr);
197637d8eb6Sflorian 	return (isc_mem_tobuffer(target, sr.base, sr.length));
1985185a700Sflorian }
1995185a700Sflorian 
2005185a700Sflorian #endif	/* RDATA_GENERIC_KEY_25_C */
201