/contrib/bind9/lib/dns/rdata/generic/dnskey_48.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 349 lines · 230 code · 78 blank · 41 comment · 56 complexity · 9c892028c2332de970b8c9813b4819e3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*
  19. * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
  20. */
  21. /* RFC2535 */
  22. #ifndef RDATA_GENERIC_DNSKEY_48_C
  23. #define RDATA_GENERIC_DNSKEY_48_C
  24. #include <dst/dst.h>
  25. #define RRTYPE_DNSKEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
  26. static inline isc_result_t
  27. fromtext_dnskey(ARGS_FROMTEXT) {
  28. isc_token_t token;
  29. dns_secalg_t alg;
  30. dns_secproto_t proto;
  31. dns_keyflags_t flags;
  32. REQUIRE(type == 48);
  33. UNUSED(type);
  34. UNUSED(rdclass);
  35. UNUSED(origin);
  36. UNUSED(options);
  37. UNUSED(callbacks);
  38. /* flags */
  39. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  40. ISC_FALSE));
  41. RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
  42. RETERR(uint16_tobuffer(flags, target));
  43. /* protocol */
  44. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  45. ISC_FALSE));
  46. RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
  47. RETERR(mem_tobuffer(target, &proto, 1));
  48. /* algorithm */
  49. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  50. ISC_FALSE));
  51. RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
  52. RETERR(mem_tobuffer(target, &alg, 1));
  53. /* No Key? */
  54. if ((flags & 0xc000) == 0xc000)
  55. return (ISC_R_SUCCESS);
  56. return (isc_base64_tobuffer(lexer, target, -1));
  57. }
  58. static inline isc_result_t
  59. totext_dnskey(ARGS_TOTEXT) {
  60. isc_region_t sr;
  61. char buf[sizeof("64000")];
  62. unsigned int flags;
  63. unsigned char algorithm;
  64. char namebuf[DNS_NAME_FORMATSIZE];
  65. REQUIRE(rdata->type == 48);
  66. REQUIRE(rdata->length != 0);
  67. dns_rdata_toregion(rdata, &sr);
  68. /* flags */
  69. flags = uint16_fromregion(&sr);
  70. isc_region_consume(&sr, 2);
  71. sprintf(buf, "%u", flags);
  72. RETERR(str_totext(buf, target));
  73. RETERR(str_totext(" ", target));
  74. /* protocol */
  75. sprintf(buf, "%u", sr.base[0]);
  76. isc_region_consume(&sr, 1);
  77. RETERR(str_totext(buf, target));
  78. RETERR(str_totext(" ", target));
  79. /* algorithm */
  80. algorithm = sr.base[0];
  81. sprintf(buf, "%u", algorithm);
  82. isc_region_consume(&sr, 1);
  83. RETERR(str_totext(buf, target));
  84. /* No Key? */
  85. if ((flags & 0xc000) == 0xc000)
  86. return (ISC_R_SUCCESS);
  87. if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0 &&
  88. algorithm == DNS_KEYALG_PRIVATEDNS) {
  89. dns_name_t name;
  90. dns_name_init(&name, NULL);
  91. dns_name_fromregion(&name, &sr);
  92. dns_name_format(&name, namebuf, sizeof(namebuf));
  93. } else
  94. namebuf[0] = 0;
  95. /* key */
  96. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  97. RETERR(str_totext(" (", target));
  98. RETERR(str_totext(tctx->linebreak, target));
  99. RETERR(isc_base64_totext(&sr, tctx->width - 2,
  100. tctx->linebreak, target));
  101. if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
  102. RETERR(str_totext(tctx->linebreak, target));
  103. else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  104. RETERR(str_totext(" ", target));
  105. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  106. RETERR(str_totext(")", target));
  107. if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
  108. isc_region_t tmpr;
  109. RETERR(str_totext(" ; key id = ", target));
  110. dns_rdata_toregion(rdata, &tmpr);
  111. sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
  112. RETERR(str_totext(buf, target));
  113. if (algorithm == DNS_KEYALG_PRIVATEDNS) {
  114. RETERR(str_totext(tctx->linebreak, target));
  115. RETERR(str_totext("; alg = ", target));
  116. RETERR(str_totext(namebuf, target));
  117. }
  118. }
  119. return (ISC_R_SUCCESS);
  120. }
  121. static inline isc_result_t
  122. fromwire_dnskey(ARGS_FROMWIRE) {
  123. unsigned char algorithm;
  124. isc_region_t sr;
  125. REQUIRE(type == 48);
  126. UNUSED(type);
  127. UNUSED(rdclass);
  128. UNUSED(dctx);
  129. UNUSED(options);
  130. isc_buffer_activeregion(source, &sr);
  131. if (sr.length < 4)
  132. return (ISC_R_UNEXPECTEDEND);
  133. algorithm = sr.base[3];
  134. RETERR(mem_tobuffer(target, sr.base, 4));
  135. isc_region_consume(&sr, 4);
  136. isc_buffer_forward(source, 4);
  137. if (algorithm == DNS_KEYALG_PRIVATEDNS) {
  138. dns_name_t name;
  139. dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  140. dns_name_init(&name, NULL);
  141. RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  142. }
  143. isc_buffer_activeregion(source, &sr);
  144. isc_buffer_forward(source, sr.length);
  145. return (mem_tobuffer(target, sr.base, sr.length));
  146. }
  147. static inline isc_result_t
  148. towire_dnskey(ARGS_TOWIRE) {
  149. isc_region_t sr;
  150. REQUIRE(rdata->type == 48);
  151. REQUIRE(rdata->length != 0);
  152. UNUSED(cctx);
  153. dns_rdata_toregion(rdata, &sr);
  154. return (mem_tobuffer(target, sr.base, sr.length));
  155. }
  156. static inline int
  157. compare_dnskey(ARGS_COMPARE) {
  158. isc_region_t r1;
  159. isc_region_t r2;
  160. REQUIRE(rdata1->type == rdata2->type);
  161. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  162. REQUIRE(rdata1->type == 48);
  163. REQUIRE(rdata1->length != 0);
  164. REQUIRE(rdata2->length != 0);
  165. dns_rdata_toregion(rdata1, &r1);
  166. dns_rdata_toregion(rdata2, &r2);
  167. return (isc_region_compare(&r1, &r2));
  168. }
  169. static inline isc_result_t
  170. fromstruct_dnskey(ARGS_FROMSTRUCT) {
  171. dns_rdata_dnskey_t *dnskey = source;
  172. REQUIRE(type == 48);
  173. REQUIRE(source != NULL);
  174. REQUIRE(dnskey->common.rdtype == type);
  175. REQUIRE(dnskey->common.rdclass == rdclass);
  176. UNUSED(type);
  177. UNUSED(rdclass);
  178. /* Flags */
  179. RETERR(uint16_tobuffer(dnskey->flags, target));
  180. /* Protocol */
  181. RETERR(uint8_tobuffer(dnskey->protocol, target));
  182. /* Algorithm */
  183. RETERR(uint8_tobuffer(dnskey->algorithm, target));
  184. /* Data */
  185. return (mem_tobuffer(target, dnskey->data, dnskey->datalen));
  186. }
  187. static inline isc_result_t
  188. tostruct_dnskey(ARGS_TOSTRUCT) {
  189. dns_rdata_dnskey_t *dnskey = target;
  190. isc_region_t sr;
  191. REQUIRE(rdata->type == 48);
  192. REQUIRE(target != NULL);
  193. REQUIRE(rdata->length != 0);
  194. dnskey->common.rdclass = rdata->rdclass;
  195. dnskey->common.rdtype = rdata->type;
  196. ISC_LINK_INIT(&dnskey->common, link);
  197. dns_rdata_toregion(rdata, &sr);
  198. /* Flags */
  199. if (sr.length < 2)
  200. return (ISC_R_UNEXPECTEDEND);
  201. dnskey->flags = uint16_fromregion(&sr);
  202. isc_region_consume(&sr, 2);
  203. /* Protocol */
  204. if (sr.length < 1)
  205. return (ISC_R_UNEXPECTEDEND);
  206. dnskey->protocol = uint8_fromregion(&sr);
  207. isc_region_consume(&sr, 1);
  208. /* Algorithm */
  209. if (sr.length < 1)
  210. return (ISC_R_UNEXPECTEDEND);
  211. dnskey->algorithm = uint8_fromregion(&sr);
  212. isc_region_consume(&sr, 1);
  213. /* Data */
  214. dnskey->datalen = sr.length;
  215. dnskey->data = mem_maybedup(mctx, sr.base, dnskey->datalen);
  216. if (dnskey->data == NULL)
  217. return (ISC_R_NOMEMORY);
  218. dnskey->mctx = mctx;
  219. return (ISC_R_SUCCESS);
  220. }
  221. static inline void
  222. freestruct_dnskey(ARGS_FREESTRUCT) {
  223. dns_rdata_dnskey_t *dnskey = (dns_rdata_dnskey_t *) source;
  224. REQUIRE(source != NULL);
  225. REQUIRE(dnskey->common.rdtype == 48);
  226. if (dnskey->mctx == NULL)
  227. return;
  228. if (dnskey->data != NULL)
  229. isc_mem_free(dnskey->mctx, dnskey->data);
  230. dnskey->mctx = NULL;
  231. }
  232. static inline isc_result_t
  233. additionaldata_dnskey(ARGS_ADDLDATA) {
  234. REQUIRE(rdata->type == 48);
  235. UNUSED(rdata);
  236. UNUSED(add);
  237. UNUSED(arg);
  238. return (ISC_R_SUCCESS);
  239. }
  240. static inline isc_result_t
  241. digest_dnskey(ARGS_DIGEST) {
  242. isc_region_t r;
  243. REQUIRE(rdata->type == 48);
  244. dns_rdata_toregion(rdata, &r);
  245. return ((digest)(arg, &r));
  246. }
  247. static inline isc_boolean_t
  248. checkowner_dnskey(ARGS_CHECKOWNER) {
  249. REQUIRE(type == 48);
  250. UNUSED(name);
  251. UNUSED(type);
  252. UNUSED(rdclass);
  253. UNUSED(wildcard);
  254. return (ISC_TRUE);
  255. }
  256. static inline isc_boolean_t
  257. checknames_dnskey(ARGS_CHECKNAMES) {
  258. REQUIRE(rdata->type == 48);
  259. UNUSED(rdata);
  260. UNUSED(owner);
  261. UNUSED(bad);
  262. return (ISC_TRUE);
  263. }
  264. static inline int
  265. casecompare_dnskey(ARGS_COMPARE) {
  266. /*
  267. * Treat ALG 253 (private DNS) subtype name case sensistively.
  268. */
  269. return (compare_dnskey(rdata1, rdata2));
  270. }
  271. #endif /* RDATA_GENERIC_DNSKEY_48_C */