xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/cert_37.c (revision 76d0caae)
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: cert_37.c,v 1.12 2020/02/26 18:47:58 florian Exp $ */
18 
19 /* Reviewed: Wed Mar 15 21:14:32 EST 2000 by tale */
20 
21 /* RFC2538 */
22 
23 #ifndef RDATA_GENERIC_CERT_37_C
24 #define RDATA_GENERIC_CERT_37_C
25 
26 static inline isc_result_t
27 totext_cert(ARGS_TOTEXT) {
28 	isc_region_t sr;
29 	char buf[sizeof("64000 ")];
30 	unsigned int n;
31 
32 	REQUIRE(rdata->type == dns_rdatatype_cert);
33 	REQUIRE(rdata->length != 0);
34 
35 	UNUSED(tctx);
36 
37 	dns_rdata_toregion(rdata, &sr);
38 
39 	/*
40 	 * Type.
41 	 */
42 	n = uint16_fromregion(&sr);
43 	isc_region_consume(&sr, 2);
44 	RETERR(dns_cert_totext((dns_cert_t)n, target));
45 	RETERR(isc_str_tobuffer(" ", target));
46 
47 	/*
48 	 * Key tag.
49 	 */
50 	n = uint16_fromregion(&sr);
51 	isc_region_consume(&sr, 2);
52 	snprintf(buf, sizeof(buf), "%u ", n);
53 	RETERR(isc_str_tobuffer(buf, target));
54 
55 	/*
56 	 * Algorithm.
57 	 */
58 	RETERR(dns_secalg_totext(sr.base[0], target));
59 	isc_region_consume(&sr, 1);
60 
61 	/*
62 	 * Cert.
63 	 */
64 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
65 		RETERR(isc_str_tobuffer(" (", target));
66 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
67 	if (tctx->width == 0)   /* No splitting */
68 		RETERR(isc_base64_totext(&sr, 60, "", target));
69 	else
70 		RETERR(isc_base64_totext(&sr, tctx->width - 2,
71 					 tctx->linebreak, target));
72 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
73 		RETERR(isc_str_tobuffer(" )", target));
74 	return (ISC_R_SUCCESS);
75 }
76 
77 static inline isc_result_t
78 fromwire_cert(ARGS_FROMWIRE) {
79 	isc_region_t sr;
80 
81 	REQUIRE(type == dns_rdatatype_cert);
82 
83 	UNUSED(type);
84 	UNUSED(rdclass);
85 	UNUSED(dctx);
86 	UNUSED(options);
87 
88 	isc_buffer_activeregion(source, &sr);
89 	if (sr.length < 5)
90 		return (ISC_R_UNEXPECTEDEND);
91 
92 	isc_buffer_forward(source, sr.length);
93 	return (isc_mem_tobuffer(target, sr.base, sr.length));
94 }
95 
96 static inline isc_result_t
97 towire_cert(ARGS_TOWIRE) {
98 	isc_region_t sr;
99 
100 	REQUIRE(rdata->type == dns_rdatatype_cert);
101 	REQUIRE(rdata->length != 0);
102 
103 	UNUSED(cctx);
104 
105 	dns_rdata_toregion(rdata, &sr);
106 	return (isc_mem_tobuffer(target, sr.base, sr.length));
107 }
108 
109 #endif	/* RDATA_GENERIC_CERT_37_C */
110