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

https://bitbucket.org/freebsd/freebsd-head/ · C · 341 lines · 223 code · 69 blank · 49 comment · 54 complexity · 7667e212af6ab6382ba4372c6f5d0c14 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2006, 2007, 2009, 2010, 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. /* draft-ietf-dnsext-delegation-signer-05.txt */
  18. #ifndef RDATA_GENERIC_DLV_32769_C
  19. #define RDATA_GENERIC_DLV_32769_C
  20. #define RRTYPE_DLV_ATTRIBUTES 0
  21. #include <isc/sha1.h>
  22. #include <isc/sha2.h>
  23. #include <dns/ds.h>
  24. static inline isc_result_t
  25. fromtext_dlv(ARGS_FROMTEXT) {
  26. isc_token_t token;
  27. unsigned char c;
  28. int length;
  29. REQUIRE(type == 32769);
  30. UNUSED(type);
  31. UNUSED(rdclass);
  32. UNUSED(origin);
  33. UNUSED(options);
  34. UNUSED(callbacks);
  35. /*
  36. * Key tag.
  37. */
  38. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  39. ISC_FALSE));
  40. if (token.value.as_ulong > 0xffffU)
  41. RETTOK(ISC_R_RANGE);
  42. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  43. /*
  44. * Algorithm.
  45. */
  46. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  47. ISC_FALSE));
  48. if (token.value.as_ulong > 0xffU)
  49. RETTOK(ISC_R_RANGE);
  50. RETERR(uint8_tobuffer(token.value.as_ulong, target));
  51. /*
  52. * Digest type.
  53. */
  54. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  55. ISC_FALSE));
  56. if (token.value.as_ulong > 0xffU)
  57. RETTOK(ISC_R_RANGE);
  58. RETERR(uint8_tobuffer(token.value.as_ulong, target));
  59. c = (unsigned char) token.value.as_ulong;
  60. /*
  61. * Digest.
  62. */
  63. switch (c) {
  64. case DNS_DSDIGEST_SHA1:
  65. length = ISC_SHA1_DIGESTLENGTH;
  66. break;
  67. case DNS_DSDIGEST_SHA256:
  68. length = ISC_SHA256_DIGESTLENGTH;
  69. break;
  70. case DNS_DSDIGEST_GOST:
  71. length = ISC_GOST_DIGESTLENGTH;
  72. break;
  73. default:
  74. length = -1;
  75. break;
  76. }
  77. return (isc_hex_tobuffer(lexer, target, -1));
  78. }
  79. static inline isc_result_t
  80. totext_dlv(ARGS_TOTEXT) {
  81. isc_region_t sr;
  82. char buf[sizeof("64000 ")];
  83. unsigned int n;
  84. REQUIRE(rdata->type == 32769);
  85. REQUIRE(rdata->length != 0);
  86. UNUSED(tctx);
  87. dns_rdata_toregion(rdata, &sr);
  88. /*
  89. * Key tag.
  90. */
  91. n = uint16_fromregion(&sr);
  92. isc_region_consume(&sr, 2);
  93. sprintf(buf, "%u ", n);
  94. RETERR(str_totext(buf, target));
  95. /*
  96. * Algorithm.
  97. */
  98. n = uint8_fromregion(&sr);
  99. isc_region_consume(&sr, 1);
  100. sprintf(buf, "%u ", n);
  101. RETERR(str_totext(buf, target));
  102. /*
  103. * Digest type.
  104. */
  105. n = uint8_fromregion(&sr);
  106. isc_region_consume(&sr, 1);
  107. sprintf(buf, "%u", n);
  108. RETERR(str_totext(buf, target));
  109. /*
  110. * Digest.
  111. */
  112. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  113. RETERR(str_totext(" (", target));
  114. RETERR(str_totext(tctx->linebreak, target));
  115. RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, target));
  116. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  117. RETERR(str_totext(" )", target));
  118. return (ISC_R_SUCCESS);
  119. }
  120. static inline isc_result_t
  121. fromwire_dlv(ARGS_FROMWIRE) {
  122. isc_region_t sr;
  123. REQUIRE(type == 32769);
  124. UNUSED(type);
  125. UNUSED(rdclass);
  126. UNUSED(dctx);
  127. UNUSED(options);
  128. isc_buffer_activeregion(source, &sr);
  129. /*
  130. * Check digest lengths if we know them.
  131. */
  132. if (sr.length < 4 ||
  133. (sr.base[3] == DNS_DSDIGEST_SHA1 &&
  134. sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
  135. (sr.base[3] == DNS_DSDIGEST_SHA256 &&
  136. sr.length < 4 + ISC_SHA256_DIGESTLENGTH) ||
  137. (sr.base[3] == DNS_DSDIGEST_GOST &&
  138. sr.length < 4 + ISC_GOST_DIGESTLENGTH))
  139. return (ISC_R_UNEXPECTEDEND);
  140. /*
  141. * Only copy digest lengths if we know them.
  142. * If there is extra data dns_rdata_fromwire() will
  143. * detect that.
  144. */
  145. if (sr.base[3] == DNS_DSDIGEST_SHA1)
  146. sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
  147. else if (sr.base[3] == DNS_DSDIGEST_SHA256)
  148. sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
  149. else if (sr.base[3] == DNS_DSDIGEST_GOST)
  150. sr.length = 4 + ISC_GOST_DIGESTLENGTH;
  151. isc_buffer_forward(source, sr.length);
  152. return (mem_tobuffer(target, sr.base, sr.length));
  153. }
  154. static inline isc_result_t
  155. towire_dlv(ARGS_TOWIRE) {
  156. isc_region_t sr;
  157. REQUIRE(rdata->type == 32769);
  158. REQUIRE(rdata->length != 0);
  159. UNUSED(cctx);
  160. dns_rdata_toregion(rdata, &sr);
  161. return (mem_tobuffer(target, sr.base, sr.length));
  162. }
  163. static inline int
  164. compare_dlv(ARGS_COMPARE) {
  165. isc_region_t r1;
  166. isc_region_t r2;
  167. REQUIRE(rdata1->type == rdata2->type);
  168. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  169. REQUIRE(rdata1->type == 32769);
  170. REQUIRE(rdata1->length != 0);
  171. REQUIRE(rdata2->length != 0);
  172. dns_rdata_toregion(rdata1, &r1);
  173. dns_rdata_toregion(rdata2, &r2);
  174. return (isc_region_compare(&r1, &r2));
  175. }
  176. static inline isc_result_t
  177. fromstruct_dlv(ARGS_FROMSTRUCT) {
  178. dns_rdata_dlv_t *dlv = source;
  179. REQUIRE(type == 32769);
  180. REQUIRE(source != NULL);
  181. REQUIRE(dlv->common.rdtype == type);
  182. REQUIRE(dlv->common.rdclass == rdclass);
  183. switch (dlv->digest_type) {
  184. case DNS_DSDIGEST_SHA1:
  185. REQUIRE(dlv->length == ISC_SHA1_DIGESTLENGTH);
  186. break;
  187. case DNS_DSDIGEST_SHA256:
  188. REQUIRE(dlv->length == ISC_SHA256_DIGESTLENGTH);
  189. break;
  190. case DNS_DSDIGEST_GOST:
  191. REQUIRE(dlv->length == ISC_GOST_DIGESTLENGTH);
  192. break;
  193. }
  194. UNUSED(type);
  195. UNUSED(rdclass);
  196. RETERR(uint16_tobuffer(dlv->key_tag, target));
  197. RETERR(uint8_tobuffer(dlv->algorithm, target));
  198. RETERR(uint8_tobuffer(dlv->digest_type, target));
  199. return (mem_tobuffer(target, dlv->digest, dlv->length));
  200. }
  201. static inline isc_result_t
  202. tostruct_dlv(ARGS_TOSTRUCT) {
  203. dns_rdata_dlv_t *dlv = target;
  204. isc_region_t region;
  205. REQUIRE(rdata->type == 32769);
  206. REQUIRE(target != NULL);
  207. REQUIRE(rdata->length != 0);
  208. dlv->common.rdclass = rdata->rdclass;
  209. dlv->common.rdtype = rdata->type;
  210. ISC_LINK_INIT(&dlv->common, link);
  211. dns_rdata_toregion(rdata, &region);
  212. dlv->key_tag = uint16_fromregion(&region);
  213. isc_region_consume(&region, 2);
  214. dlv->algorithm = uint8_fromregion(&region);
  215. isc_region_consume(&region, 1);
  216. dlv->digest_type = uint8_fromregion(&region);
  217. isc_region_consume(&region, 1);
  218. dlv->length = region.length;
  219. dlv->digest = mem_maybedup(mctx, region.base, region.length);
  220. if (dlv->digest == NULL)
  221. return (ISC_R_NOMEMORY);
  222. dlv->mctx = mctx;
  223. return (ISC_R_SUCCESS);
  224. }
  225. static inline void
  226. freestruct_dlv(ARGS_FREESTRUCT) {
  227. dns_rdata_dlv_t *dlv = source;
  228. REQUIRE(dlv != NULL);
  229. REQUIRE(dlv->common.rdtype == 32769);
  230. if (dlv->mctx == NULL)
  231. return;
  232. if (dlv->digest != NULL)
  233. isc_mem_free(dlv->mctx, dlv->digest);
  234. dlv->mctx = NULL;
  235. }
  236. static inline isc_result_t
  237. additionaldata_dlv(ARGS_ADDLDATA) {
  238. REQUIRE(rdata->type == 32769);
  239. UNUSED(rdata);
  240. UNUSED(add);
  241. UNUSED(arg);
  242. return (ISC_R_SUCCESS);
  243. }
  244. static inline isc_result_t
  245. digest_dlv(ARGS_DIGEST) {
  246. isc_region_t r;
  247. REQUIRE(rdata->type == 32769);
  248. dns_rdata_toregion(rdata, &r);
  249. return ((digest)(arg, &r));
  250. }
  251. static inline isc_boolean_t
  252. checkowner_dlv(ARGS_CHECKOWNER) {
  253. REQUIRE(type == 32769);
  254. UNUSED(name);
  255. UNUSED(type);
  256. UNUSED(rdclass);
  257. UNUSED(wildcard);
  258. return (ISC_TRUE);
  259. }
  260. static inline isc_boolean_t
  261. checknames_dlv(ARGS_CHECKNAMES) {
  262. REQUIRE(rdata->type == 32769);
  263. UNUSED(rdata);
  264. UNUSED(owner);
  265. UNUSED(bad);
  266. return (ISC_TRUE);
  267. }
  268. static inline int
  269. casecompare_dlv(ARGS_COMPARE) {
  270. return (compare_dlv(rdata1, rdata2));
  271. }
  272. #endif /* RDATA_GENERIC_DLV_32769_C */