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

https://bitbucket.org/freebsd/freebsd-head/ · C · 377 lines · 246 code · 85 blank · 46 comment · 52 complexity · 768c512c545454043ec177e70d89cf8c MD5 · raw file

  1. /*
  2. * Copyright (C) 2009, 2012 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. /* $Id$ */
  17. #ifndef GENERIC_KEYDATA_65533_C
  18. #define GENERIC_KEYDATA_65533_C 1
  19. #include <dst/dst.h>
  20. #define RRTYPE_KEYDATA_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
  21. static inline isc_result_t
  22. fromtext_keydata(ARGS_FROMTEXT) {
  23. isc_token_t token;
  24. dns_secalg_t alg;
  25. dns_secproto_t proto;
  26. dns_keyflags_t flags;
  27. isc_uint32_t refresh, addhd, removehd;
  28. REQUIRE(type == 65533);
  29. UNUSED(type);
  30. UNUSED(rdclass);
  31. UNUSED(origin);
  32. UNUSED(options);
  33. UNUSED(callbacks);
  34. /* refresh timer */
  35. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  36. ISC_FALSE));
  37. RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &refresh));
  38. RETERR(uint32_tobuffer(refresh, target));
  39. /* add hold-down */
  40. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  41. ISC_FALSE));
  42. RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &addhd));
  43. RETERR(uint32_tobuffer(addhd, target));
  44. /* remove hold-down */
  45. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  46. ISC_FALSE));
  47. RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &removehd));
  48. RETERR(uint32_tobuffer(removehd, target));
  49. /* flags */
  50. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  51. ISC_FALSE));
  52. RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
  53. RETERR(uint16_tobuffer(flags, target));
  54. /* protocol */
  55. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  56. ISC_FALSE));
  57. RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
  58. RETERR(mem_tobuffer(target, &proto, 1));
  59. /* algorithm */
  60. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  61. ISC_FALSE));
  62. RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
  63. RETERR(mem_tobuffer(target, &alg, 1));
  64. /* No Key? */
  65. if ((flags & 0xc000) == 0xc000)
  66. return (ISC_R_SUCCESS);
  67. return (isc_base64_tobuffer(lexer, target, -1));
  68. }
  69. static inline isc_result_t
  70. totext_keydata(ARGS_TOTEXT) {
  71. isc_region_t sr;
  72. char buf[sizeof("64000")];
  73. unsigned int flags;
  74. unsigned char algorithm;
  75. unsigned long when;
  76. REQUIRE(rdata->type == 65533);
  77. REQUIRE(rdata->length != 0);
  78. dns_rdata_toregion(rdata, &sr);
  79. /* refresh timer */
  80. when = uint32_fromregion(&sr);
  81. isc_region_consume(&sr, 4);
  82. RETERR(dns_time32_totext(when, target));
  83. RETERR(str_totext(" ", target));
  84. /* add hold-down */
  85. when = uint32_fromregion(&sr);
  86. isc_region_consume(&sr, 4);
  87. RETERR(dns_time32_totext(when, target));
  88. RETERR(str_totext(" ", target));
  89. /* remove hold-down */
  90. when = uint32_fromregion(&sr);
  91. isc_region_consume(&sr, 4);
  92. RETERR(dns_time32_totext(when, target));
  93. RETERR(str_totext(" ", target));
  94. /* flags */
  95. flags = uint16_fromregion(&sr);
  96. isc_region_consume(&sr, 2);
  97. sprintf(buf, "%u", flags);
  98. RETERR(str_totext(buf, target));
  99. RETERR(str_totext(" ", target));
  100. /* protocol */
  101. sprintf(buf, "%u", sr.base[0]);
  102. isc_region_consume(&sr, 1);
  103. RETERR(str_totext(buf, target));
  104. RETERR(str_totext(" ", target));
  105. /* algorithm */
  106. algorithm = sr.base[0];
  107. sprintf(buf, "%u", algorithm);
  108. isc_region_consume(&sr, 1);
  109. RETERR(str_totext(buf, target));
  110. /* No Key? */
  111. if ((flags & 0xc000) == 0xc000)
  112. return (ISC_R_SUCCESS);
  113. /* key */
  114. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  115. RETERR(str_totext(" (", target));
  116. RETERR(str_totext(tctx->linebreak, target));
  117. RETERR(isc_base64_totext(&sr, tctx->width - 2,
  118. tctx->linebreak, target));
  119. if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
  120. RETERR(str_totext(tctx->linebreak, target));
  121. else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  122. RETERR(str_totext(" ", target));
  123. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  124. RETERR(str_totext(")", target));
  125. if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
  126. isc_region_t tmpr;
  127. RETERR(str_totext(" ; key id = ", target));
  128. dns_rdata_toregion(rdata, &tmpr);
  129. /* Skip over refresh, addhd, and removehd */
  130. isc_region_consume(&tmpr, 12);
  131. sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
  132. RETERR(str_totext(buf, target));
  133. }
  134. return (ISC_R_SUCCESS);
  135. }
  136. static inline isc_result_t
  137. fromwire_keydata(ARGS_FROMWIRE) {
  138. isc_region_t sr;
  139. REQUIRE(type == 65533);
  140. UNUSED(type);
  141. UNUSED(rdclass);
  142. UNUSED(dctx);
  143. UNUSED(options);
  144. isc_buffer_activeregion(source, &sr);
  145. if (sr.length < 4)
  146. return (ISC_R_UNEXPECTEDEND);
  147. isc_buffer_forward(source, sr.length);
  148. return (mem_tobuffer(target, sr.base, sr.length));
  149. }
  150. static inline isc_result_t
  151. towire_keydata(ARGS_TOWIRE) {
  152. isc_region_t sr;
  153. REQUIRE(rdata->type == 65533);
  154. REQUIRE(rdata->length != 0);
  155. UNUSED(cctx);
  156. dns_rdata_toregion(rdata, &sr);
  157. return (mem_tobuffer(target, sr.base, sr.length));
  158. }
  159. static inline int
  160. compare_keydata(ARGS_COMPARE) {
  161. isc_region_t r1;
  162. isc_region_t r2;
  163. REQUIRE(rdata1->type == rdata2->type);
  164. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  165. REQUIRE(rdata1->type == 65533);
  166. REQUIRE(rdata1->length != 0);
  167. REQUIRE(rdata2->length != 0);
  168. dns_rdata_toregion(rdata1, &r1);
  169. dns_rdata_toregion(rdata2, &r2);
  170. return (isc_region_compare(&r1, &r2));
  171. }
  172. static inline isc_result_t
  173. fromstruct_keydata(ARGS_FROMSTRUCT) {
  174. dns_rdata_keydata_t *keydata = source;
  175. REQUIRE(type == 65533);
  176. REQUIRE(source != NULL);
  177. REQUIRE(keydata->common.rdtype == type);
  178. REQUIRE(keydata->common.rdclass == rdclass);
  179. UNUSED(type);
  180. UNUSED(rdclass);
  181. /* Refresh timer */
  182. RETERR(uint32_tobuffer(keydata->refresh, target));
  183. /* Add hold-down */
  184. RETERR(uint32_tobuffer(keydata->addhd, target));
  185. /* Remove hold-down */
  186. RETERR(uint32_tobuffer(keydata->removehd, target));
  187. /* Flags */
  188. RETERR(uint16_tobuffer(keydata->flags, target));
  189. /* Protocol */
  190. RETERR(uint8_tobuffer(keydata->protocol, target));
  191. /* Algorithm */
  192. RETERR(uint8_tobuffer(keydata->algorithm, target));
  193. /* Data */
  194. return (mem_tobuffer(target, keydata->data, keydata->datalen));
  195. }
  196. static inline isc_result_t
  197. tostruct_keydata(ARGS_TOSTRUCT) {
  198. dns_rdata_keydata_t *keydata = target;
  199. isc_region_t sr;
  200. REQUIRE(rdata->type == 65533);
  201. REQUIRE(target != NULL);
  202. REQUIRE(rdata->length != 0);
  203. keydata->common.rdclass = rdata->rdclass;
  204. keydata->common.rdtype = rdata->type;
  205. ISC_LINK_INIT(&keydata->common, link);
  206. dns_rdata_toregion(rdata, &sr);
  207. /* Refresh timer */
  208. if (sr.length < 4)
  209. return (ISC_R_UNEXPECTEDEND);
  210. keydata->refresh = uint32_fromregion(&sr);
  211. isc_region_consume(&sr, 4);
  212. /* Add hold-down */
  213. if (sr.length < 4)
  214. return (ISC_R_UNEXPECTEDEND);
  215. keydata->addhd = uint32_fromregion(&sr);
  216. isc_region_consume(&sr, 4);
  217. /* Remove hold-down */
  218. if (sr.length < 4)
  219. return (ISC_R_UNEXPECTEDEND);
  220. keydata->removehd = uint32_fromregion(&sr);
  221. isc_region_consume(&sr, 4);
  222. /* Flags */
  223. if (sr.length < 2)
  224. return (ISC_R_UNEXPECTEDEND);
  225. keydata->flags = uint16_fromregion(&sr);
  226. isc_region_consume(&sr, 2);
  227. /* Protocol */
  228. if (sr.length < 1)
  229. return (ISC_R_UNEXPECTEDEND);
  230. keydata->protocol = uint8_fromregion(&sr);
  231. isc_region_consume(&sr, 1);
  232. /* Algorithm */
  233. if (sr.length < 1)
  234. return (ISC_R_UNEXPECTEDEND);
  235. keydata->algorithm = uint8_fromregion(&sr);
  236. isc_region_consume(&sr, 1);
  237. /* Data */
  238. keydata->datalen = sr.length;
  239. keydata->data = mem_maybedup(mctx, sr.base, keydata->datalen);
  240. if (keydata->data == NULL)
  241. return (ISC_R_NOMEMORY);
  242. keydata->mctx = mctx;
  243. return (ISC_R_SUCCESS);
  244. }
  245. static inline void
  246. freestruct_keydata(ARGS_FREESTRUCT) {
  247. dns_rdata_keydata_t *keydata = (dns_rdata_keydata_t *) source;
  248. REQUIRE(source != NULL);
  249. REQUIRE(keydata->common.rdtype == 65533);
  250. if (keydata->mctx == NULL)
  251. return;
  252. if (keydata->data != NULL)
  253. isc_mem_free(keydata->mctx, keydata->data);
  254. keydata->mctx = NULL;
  255. }
  256. static inline isc_result_t
  257. additionaldata_keydata(ARGS_ADDLDATA) {
  258. REQUIRE(rdata->type == 65533);
  259. UNUSED(rdata);
  260. UNUSED(add);
  261. UNUSED(arg);
  262. return (ISC_R_SUCCESS);
  263. }
  264. static inline isc_result_t
  265. digest_keydata(ARGS_DIGEST) {
  266. isc_region_t r;
  267. REQUIRE(rdata->type == 65533);
  268. dns_rdata_toregion(rdata, &r);
  269. return ((digest)(arg, &r));
  270. }
  271. static inline isc_boolean_t
  272. checkowner_keydata(ARGS_CHECKOWNER) {
  273. REQUIRE(type == 65533);
  274. UNUSED(name);
  275. UNUSED(type);
  276. UNUSED(rdclass);
  277. UNUSED(wildcard);
  278. return (ISC_TRUE);
  279. }
  280. static inline isc_boolean_t
  281. checknames_keydata(ARGS_CHECKNAMES) {
  282. REQUIRE(rdata->type == 65533);
  283. UNUSED(rdata);
  284. UNUSED(owner);
  285. UNUSED(bad);
  286. return (ISC_TRUE);
  287. }
  288. static inline int
  289. casecompare_keydata(ARGS_COMPARE) {
  290. return (compare_keydata(rdata1, rdata2));
  291. }
  292. #endif /* GENERIC_KEYDATA_65533_C */