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

https://bitbucket.org/freebsd/freebsd-head/ · C · 319 lines · 212 code · 70 blank · 37 comment · 40 complexity · d1590bef6b6ce45c73fa108501926f9f MD5 · raw file

  1. /*
  2. * Copyright (C) 2008, 2009 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: nsec3param_51.c,v 1.7 2009/12/04 21:09:34 marka Exp $ */
  17. /*
  18. * Copyright (C) 2004 Nominet, Ltd.
  19. *
  20. * Permission to use, copy, modify, and distribute this software for any
  21. * purpose with or without fee is hereby granted, provided that the above
  22. * copyright notice and this permission notice appear in all copies.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINET DISCLAIMS ALL WARRANTIES WITH
  25. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  26. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  27. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  28. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  29. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  30. * PERFORMANCE OF THIS SOFTWARE.
  31. */
  32. /* RFC 5155 */
  33. #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
  34. #define RDATA_GENERIC_NSEC3PARAM_51_C
  35. #include <isc/iterated_hash.h>
  36. #include <isc/base32.h>
  37. #define RRTYPE_NSEC3PARAM_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
  38. static inline isc_result_t
  39. fromtext_nsec3param(ARGS_FROMTEXT) {
  40. isc_token_t token;
  41. unsigned int flags = 0;
  42. unsigned char hashalg;
  43. REQUIRE(type == 51);
  44. UNUSED(type);
  45. UNUSED(rdclass);
  46. UNUSED(callbacks);
  47. UNUSED(origin);
  48. UNUSED(options);
  49. /* Hash. */
  50. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  51. ISC_FALSE));
  52. RETTOK(dns_hashalg_fromtext(&hashalg, &token.value.as_textregion));
  53. RETERR(uint8_tobuffer(hashalg, target));
  54. /* Flags. */
  55. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  56. ISC_FALSE));
  57. flags = token.value.as_ulong;
  58. if (flags > 255U)
  59. RETTOK(ISC_R_RANGE);
  60. RETERR(uint8_tobuffer(flags, target));
  61. /* Iterations. */
  62. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  63. ISC_FALSE));
  64. if (token.value.as_ulong > 0xffffU)
  65. RETTOK(ISC_R_RANGE);
  66. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  67. /* Salt. */
  68. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  69. ISC_FALSE));
  70. if (token.value.as_textregion.length > (255*2))
  71. RETTOK(DNS_R_TEXTTOOLONG);
  72. if (strcmp(DNS_AS_STR(token), "-") == 0) {
  73. RETERR(uint8_tobuffer(0, target));
  74. } else {
  75. RETERR(uint8_tobuffer(strlen(DNS_AS_STR(token)) / 2, target));
  76. RETERR(isc_hex_decodestring(DNS_AS_STR(token), target));
  77. }
  78. return (ISC_R_SUCCESS);
  79. }
  80. static inline isc_result_t
  81. totext_nsec3param(ARGS_TOTEXT) {
  82. isc_region_t sr;
  83. unsigned int i, j;
  84. unsigned char hash;
  85. unsigned char flags;
  86. char buf[sizeof("65535 ")];
  87. isc_uint32_t iterations;
  88. REQUIRE(rdata->type == 51);
  89. REQUIRE(rdata->length != 0);
  90. UNUSED(tctx);
  91. dns_rdata_toregion(rdata, &sr);
  92. hash = uint8_fromregion(&sr);
  93. isc_region_consume(&sr, 1);
  94. flags = uint8_fromregion(&sr);
  95. isc_region_consume(&sr, 1);
  96. iterations = uint16_fromregion(&sr);
  97. isc_region_consume(&sr, 2);
  98. sprintf(buf, "%u ", hash);
  99. RETERR(str_totext(buf, target));
  100. sprintf(buf, "%u ", flags);
  101. RETERR(str_totext(buf, target));
  102. sprintf(buf, "%u ", iterations);
  103. RETERR(str_totext(buf, target));
  104. j = uint8_fromregion(&sr);
  105. isc_region_consume(&sr, 1);
  106. INSIST(j <= sr.length);
  107. if (j != 0) {
  108. i = sr.length;
  109. sr.length = j;
  110. RETERR(isc_hex_totext(&sr, 1, "", target));
  111. sr.length = i - j;
  112. } else
  113. RETERR(str_totext("-", target));
  114. return (ISC_R_SUCCESS);
  115. }
  116. static inline isc_result_t
  117. fromwire_nsec3param(ARGS_FROMWIRE) {
  118. isc_region_t sr, rr;
  119. unsigned int saltlen;
  120. REQUIRE(type == 51);
  121. UNUSED(type);
  122. UNUSED(rdclass);
  123. UNUSED(options);
  124. UNUSED(dctx);
  125. isc_buffer_activeregion(source, &sr);
  126. rr = sr;
  127. /* hash(1), flags(1), iterations(2), saltlen(1) */
  128. if (sr.length < 5U)
  129. RETERR(DNS_R_FORMERR);
  130. saltlen = sr.base[4];
  131. isc_region_consume(&sr, 5);
  132. if (sr.length < saltlen)
  133. RETERR(DNS_R_FORMERR);
  134. isc_region_consume(&sr, saltlen);
  135. RETERR(mem_tobuffer(target, rr.base, rr.length));
  136. isc_buffer_forward(source, rr.length);
  137. return (ISC_R_SUCCESS);
  138. }
  139. static inline isc_result_t
  140. towire_nsec3param(ARGS_TOWIRE) {
  141. isc_region_t sr;
  142. REQUIRE(rdata->type == 51);
  143. REQUIRE(rdata->length != 0);
  144. UNUSED(cctx);
  145. dns_rdata_toregion(rdata, &sr);
  146. return (mem_tobuffer(target, sr.base, sr.length));
  147. }
  148. static inline int
  149. compare_nsec3param(ARGS_COMPARE) {
  150. isc_region_t r1;
  151. isc_region_t r2;
  152. REQUIRE(rdata1->type == rdata2->type);
  153. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  154. REQUIRE(rdata1->type == 51);
  155. REQUIRE(rdata1->length != 0);
  156. REQUIRE(rdata2->length != 0);
  157. dns_rdata_toregion(rdata1, &r1);
  158. dns_rdata_toregion(rdata2, &r2);
  159. return (isc_region_compare(&r1, &r2));
  160. }
  161. static inline isc_result_t
  162. fromstruct_nsec3param(ARGS_FROMSTRUCT) {
  163. dns_rdata_nsec3param_t *nsec3param = source;
  164. REQUIRE(type == 51);
  165. REQUIRE(source != NULL);
  166. REQUIRE(nsec3param->common.rdtype == type);
  167. REQUIRE(nsec3param->common.rdclass == rdclass);
  168. UNUSED(type);
  169. UNUSED(rdclass);
  170. RETERR(uint8_tobuffer(nsec3param->hash, target));
  171. RETERR(uint8_tobuffer(nsec3param->flags, target));
  172. RETERR(uint16_tobuffer(nsec3param->iterations, target));
  173. RETERR(uint8_tobuffer(nsec3param->salt_length, target));
  174. RETERR(mem_tobuffer(target, nsec3param->salt,
  175. nsec3param->salt_length));
  176. return (ISC_R_SUCCESS);
  177. }
  178. static inline isc_result_t
  179. tostruct_nsec3param(ARGS_TOSTRUCT) {
  180. isc_region_t region;
  181. dns_rdata_nsec3param_t *nsec3param = target;
  182. REQUIRE(rdata->type == 51);
  183. REQUIRE(target != NULL);
  184. REQUIRE(rdata->length != 0);
  185. nsec3param->common.rdclass = rdata->rdclass;
  186. nsec3param->common.rdtype = rdata->type;
  187. ISC_LINK_INIT(&nsec3param->common, link);
  188. region.base = rdata->data;
  189. region.length = rdata->length;
  190. nsec3param->hash = uint8_consume_fromregion(&region);
  191. nsec3param->flags = uint8_consume_fromregion(&region);
  192. nsec3param->iterations = uint16_consume_fromregion(&region);
  193. nsec3param->salt_length = uint8_consume_fromregion(&region);
  194. nsec3param->salt = mem_maybedup(mctx, region.base,
  195. nsec3param->salt_length);
  196. if (nsec3param->salt == NULL)
  197. return (ISC_R_NOMEMORY);
  198. isc_region_consume(&region, nsec3param->salt_length);
  199. nsec3param->mctx = mctx;
  200. return (ISC_R_SUCCESS);
  201. }
  202. static inline void
  203. freestruct_nsec3param(ARGS_FREESTRUCT) {
  204. dns_rdata_nsec3param_t *nsec3param = source;
  205. REQUIRE(source != NULL);
  206. REQUIRE(nsec3param->common.rdtype == 51);
  207. if (nsec3param->mctx == NULL)
  208. return;
  209. if (nsec3param->salt != NULL)
  210. isc_mem_free(nsec3param->mctx, nsec3param->salt);
  211. nsec3param->mctx = NULL;
  212. }
  213. static inline isc_result_t
  214. additionaldata_nsec3param(ARGS_ADDLDATA) {
  215. REQUIRE(rdata->type == 51);
  216. UNUSED(rdata);
  217. UNUSED(add);
  218. UNUSED(arg);
  219. return (ISC_R_SUCCESS);
  220. }
  221. static inline isc_result_t
  222. digest_nsec3param(ARGS_DIGEST) {
  223. isc_region_t r;
  224. REQUIRE(rdata->type == 51);
  225. dns_rdata_toregion(rdata, &r);
  226. return ((digest)(arg, &r));
  227. }
  228. static inline isc_boolean_t
  229. checkowner_nsec3param(ARGS_CHECKOWNER) {
  230. REQUIRE(type == 51);
  231. UNUSED(name);
  232. UNUSED(type);
  233. UNUSED(rdclass);
  234. UNUSED(wildcard);
  235. return (ISC_TRUE);
  236. }
  237. static inline isc_boolean_t
  238. checknames_nsec3param(ARGS_CHECKNAMES) {
  239. REQUIRE(rdata->type == 51);
  240. UNUSED(rdata);
  241. UNUSED(owner);
  242. UNUSED(bad);
  243. return (ISC_TRUE);
  244. }
  245. static inline int
  246. casecompare_nsec3param(ARGS_COMPARE) {
  247. return (compare_nsec3param(rdata1, rdata2));
  248. }
  249. #endif /* RDATA_GENERIC_NSEC3PARAM_51_C */