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

https://bitbucket.org/freebsd/freebsd-head/ · C · 559 lines · 318 code · 91 blank · 150 comment · 69 complexity · ae855f42611d5798b968debb54634d81 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-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: Thu Mar 16 17:35:30 PST 2000 by halley.
  20. */
  21. /* draft-ietf-dnsext-tkey-01.txt */
  22. #ifndef RDATA_GENERIC_TKEY_249_C
  23. #define RDATA_GENERIC_TKEY_249_C
  24. #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
  25. static inline isc_result_t
  26. fromtext_tkey(ARGS_FROMTEXT) {
  27. isc_token_t token;
  28. dns_rcode_t rcode;
  29. dns_name_t name;
  30. isc_buffer_t buffer;
  31. long i;
  32. char *e;
  33. REQUIRE(type == 249);
  34. UNUSED(type);
  35. UNUSED(rdclass);
  36. UNUSED(callbacks);
  37. /*
  38. * Algorithm.
  39. */
  40. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  41. ISC_FALSE));
  42. dns_name_init(&name, NULL);
  43. buffer_fromregion(&buffer, &token.value.as_region);
  44. origin = (origin != NULL) ? origin : dns_rootname;
  45. RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
  46. /*
  47. * Inception.
  48. */
  49. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  50. ISC_FALSE));
  51. RETERR(uint32_tobuffer(token.value.as_ulong, target));
  52. /*
  53. * Expiration.
  54. */
  55. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  56. ISC_FALSE));
  57. RETERR(uint32_tobuffer(token.value.as_ulong, target));
  58. /*
  59. * Mode.
  60. */
  61. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  62. ISC_FALSE));
  63. if (token.value.as_ulong > 0xffffU)
  64. RETTOK(ISC_R_RANGE);
  65. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  66. /*
  67. * Error.
  68. */
  69. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  70. ISC_FALSE));
  71. if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
  72. != ISC_R_SUCCESS)
  73. {
  74. i = strtol(DNS_AS_STR(token), &e, 10);
  75. if (*e != 0)
  76. RETTOK(DNS_R_UNKNOWN);
  77. if (i < 0 || i > 0xffff)
  78. RETTOK(ISC_R_RANGE);
  79. rcode = (dns_rcode_t)i;
  80. }
  81. RETERR(uint16_tobuffer(rcode, target));
  82. /*
  83. * Key Size.
  84. */
  85. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  86. ISC_FALSE));
  87. if (token.value.as_ulong > 0xffffU)
  88. RETTOK(ISC_R_RANGE);
  89. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  90. /*
  91. * Key Data.
  92. */
  93. RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
  94. /*
  95. * Other Size.
  96. */
  97. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  98. ISC_FALSE));
  99. if (token.value.as_ulong > 0xffffU)
  100. RETTOK(ISC_R_RANGE);
  101. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  102. /*
  103. * Other Data.
  104. */
  105. return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
  106. }
  107. static inline isc_result_t
  108. totext_tkey(ARGS_TOTEXT) {
  109. isc_region_t sr, dr;
  110. char buf[sizeof("4294967295 ")];
  111. unsigned long n;
  112. dns_name_t name;
  113. dns_name_t prefix;
  114. isc_boolean_t sub;
  115. REQUIRE(rdata->type == 249);
  116. REQUIRE(rdata->length != 0);
  117. dns_rdata_toregion(rdata, &sr);
  118. /*
  119. * Algorithm.
  120. */
  121. dns_name_init(&name, NULL);
  122. dns_name_init(&prefix, NULL);
  123. dns_name_fromregion(&name, &sr);
  124. sub = name_prefix(&name, tctx->origin, &prefix);
  125. RETERR(dns_name_totext(&prefix, sub, target));
  126. RETERR(str_totext(" ", target));
  127. isc_region_consume(&sr, name_length(&name));
  128. /*
  129. * Inception.
  130. */
  131. n = uint32_fromregion(&sr);
  132. isc_region_consume(&sr, 4);
  133. sprintf(buf, "%lu ", n);
  134. RETERR(str_totext(buf, target));
  135. /*
  136. * Expiration.
  137. */
  138. n = uint32_fromregion(&sr);
  139. isc_region_consume(&sr, 4);
  140. sprintf(buf, "%lu ", n);
  141. RETERR(str_totext(buf, target));
  142. /*
  143. * Mode.
  144. */
  145. n = uint16_fromregion(&sr);
  146. isc_region_consume(&sr, 2);
  147. sprintf(buf, "%lu ", n);
  148. RETERR(str_totext(buf, target));
  149. /*
  150. * Error.
  151. */
  152. n = uint16_fromregion(&sr);
  153. isc_region_consume(&sr, 2);
  154. if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
  155. RETERR(str_totext(" ", target));
  156. else {
  157. sprintf(buf, "%lu ", n);
  158. RETERR(str_totext(buf, target));
  159. }
  160. /*
  161. * Key Size.
  162. */
  163. n = uint16_fromregion(&sr);
  164. isc_region_consume(&sr, 2);
  165. sprintf(buf, "%lu", n);
  166. RETERR(str_totext(buf, target));
  167. /*
  168. * Key Data.
  169. */
  170. REQUIRE(n <= sr.length);
  171. dr = sr;
  172. dr.length = n;
  173. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  174. RETERR(str_totext(" (", target));
  175. RETERR(str_totext(tctx->linebreak, target));
  176. RETERR(isc_base64_totext(&dr, tctx->width - 2,
  177. tctx->linebreak, target));
  178. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  179. RETERR(str_totext(" ) ", target));
  180. else
  181. RETERR(str_totext(" ", target));
  182. isc_region_consume(&sr, n);
  183. /*
  184. * Other Size.
  185. */
  186. n = uint16_fromregion(&sr);
  187. isc_region_consume(&sr, 2);
  188. sprintf(buf, "%lu", n);
  189. RETERR(str_totext(buf, target));
  190. /*
  191. * Other Data.
  192. */
  193. REQUIRE(n <= sr.length);
  194. if (n != 0U) {
  195. dr = sr;
  196. dr.length = n;
  197. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  198. RETERR(str_totext(" (", target));
  199. RETERR(str_totext(tctx->linebreak, target));
  200. RETERR(isc_base64_totext(&dr, tctx->width - 2,
  201. tctx->linebreak, target));
  202. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  203. RETERR(str_totext(" )", target));
  204. }
  205. return (ISC_R_SUCCESS);
  206. }
  207. static inline isc_result_t
  208. fromwire_tkey(ARGS_FROMWIRE) {
  209. isc_region_t sr;
  210. unsigned long n;
  211. dns_name_t name;
  212. REQUIRE(type == 249);
  213. UNUSED(type);
  214. UNUSED(rdclass);
  215. dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  216. /*
  217. * Algorithm.
  218. */
  219. dns_name_init(&name, NULL);
  220. RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  221. /*
  222. * Inception: 4
  223. * Expiration: 4
  224. * Mode: 2
  225. * Error: 2
  226. */
  227. isc_buffer_activeregion(source, &sr);
  228. if (sr.length < 12)
  229. return (ISC_R_UNEXPECTEDEND);
  230. RETERR(mem_tobuffer(target, sr.base, 12));
  231. isc_region_consume(&sr, 12);
  232. isc_buffer_forward(source, 12);
  233. /*
  234. * Key Length + Key Data.
  235. */
  236. if (sr.length < 2)
  237. return (ISC_R_UNEXPECTEDEND);
  238. n = uint16_fromregion(&sr);
  239. if (sr.length < n + 2)
  240. return (ISC_R_UNEXPECTEDEND);
  241. RETERR(mem_tobuffer(target, sr.base, n + 2));
  242. isc_region_consume(&sr, n + 2);
  243. isc_buffer_forward(source, n + 2);
  244. /*
  245. * Other Length + Other Data.
  246. */
  247. if (sr.length < 2)
  248. return (ISC_R_UNEXPECTEDEND);
  249. n = uint16_fromregion(&sr);
  250. if (sr.length < n + 2)
  251. return (ISC_R_UNEXPECTEDEND);
  252. isc_buffer_forward(source, n + 2);
  253. return (mem_tobuffer(target, sr.base, n + 2));
  254. }
  255. static inline isc_result_t
  256. towire_tkey(ARGS_TOWIRE) {
  257. isc_region_t sr;
  258. dns_name_t name;
  259. dns_offsets_t offsets;
  260. REQUIRE(rdata->type == 249);
  261. REQUIRE(rdata->length != 0);
  262. dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
  263. /*
  264. * Algorithm.
  265. */
  266. dns_rdata_toregion(rdata, &sr);
  267. dns_name_init(&name, offsets);
  268. dns_name_fromregion(&name, &sr);
  269. RETERR(dns_name_towire(&name, cctx, target));
  270. isc_region_consume(&sr, name_length(&name));
  271. return (mem_tobuffer(target, sr.base, sr.length));
  272. }
  273. static inline int
  274. compare_tkey(ARGS_COMPARE) {
  275. isc_region_t r1;
  276. isc_region_t r2;
  277. dns_name_t name1;
  278. dns_name_t name2;
  279. int order;
  280. REQUIRE(rdata1->type == rdata2->type);
  281. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  282. REQUIRE(rdata1->type == 249);
  283. REQUIRE(rdata1->length != 0);
  284. REQUIRE(rdata2->length != 0);
  285. /*
  286. * Algorithm.
  287. */
  288. dns_rdata_toregion(rdata1, &r1);
  289. dns_rdata_toregion(rdata2, &r2);
  290. dns_name_init(&name1, NULL);
  291. dns_name_init(&name2, NULL);
  292. dns_name_fromregion(&name1, &r1);
  293. dns_name_fromregion(&name2, &r2);
  294. if ((order = dns_name_rdatacompare(&name1, &name2)) != 0)
  295. return (order);
  296. isc_region_consume(&r1, name_length(&name1));
  297. isc_region_consume(&r2, name_length(&name2));
  298. return (isc_region_compare(&r1, &r2));
  299. }
  300. static inline isc_result_t
  301. fromstruct_tkey(ARGS_FROMSTRUCT) {
  302. dns_rdata_tkey_t *tkey = source;
  303. REQUIRE(type == 249);
  304. REQUIRE(source != NULL);
  305. REQUIRE(tkey->common.rdtype == type);
  306. REQUIRE(tkey->common.rdclass == rdclass);
  307. UNUSED(type);
  308. UNUSED(rdclass);
  309. /*
  310. * Algorithm Name.
  311. */
  312. RETERR(name_tobuffer(&tkey->algorithm, target));
  313. /*
  314. * Inception: 32 bits.
  315. */
  316. RETERR(uint32_tobuffer(tkey->inception, target));
  317. /*
  318. * Expire: 32 bits.
  319. */
  320. RETERR(uint32_tobuffer(tkey->expire, target));
  321. /*
  322. * Mode: 16 bits.
  323. */
  324. RETERR(uint16_tobuffer(tkey->mode, target));
  325. /*
  326. * Error: 16 bits.
  327. */
  328. RETERR(uint16_tobuffer(tkey->error, target));
  329. /*
  330. * Key size: 16 bits.
  331. */
  332. RETERR(uint16_tobuffer(tkey->keylen, target));
  333. /*
  334. * Key.
  335. */
  336. RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
  337. /*
  338. * Other size: 16 bits.
  339. */
  340. RETERR(uint16_tobuffer(tkey->otherlen, target));
  341. /*
  342. * Other data.
  343. */
  344. return (mem_tobuffer(target, tkey->other, tkey->otherlen));
  345. }
  346. static inline isc_result_t
  347. tostruct_tkey(ARGS_TOSTRUCT) {
  348. dns_rdata_tkey_t *tkey = target;
  349. dns_name_t alg;
  350. isc_region_t sr;
  351. REQUIRE(rdata->type == 249);
  352. REQUIRE(target != NULL);
  353. REQUIRE(rdata->length != 0);
  354. tkey->common.rdclass = rdata->rdclass;
  355. tkey->common.rdtype = rdata->type;
  356. ISC_LINK_INIT(&tkey->common, link);
  357. dns_rdata_toregion(rdata, &sr);
  358. /*
  359. * Algorithm Name.
  360. */
  361. dns_name_init(&alg, NULL);
  362. dns_name_fromregion(&alg, &sr);
  363. dns_name_init(&tkey->algorithm, NULL);
  364. RETERR(name_duporclone(&alg, mctx, &tkey->algorithm));
  365. isc_region_consume(&sr, name_length(&tkey->algorithm));
  366. /*
  367. * Inception.
  368. */
  369. tkey->inception = uint32_fromregion(&sr);
  370. isc_region_consume(&sr, 4);
  371. /*
  372. * Expire.
  373. */
  374. tkey->expire = uint32_fromregion(&sr);
  375. isc_region_consume(&sr, 4);
  376. /*
  377. * Mode.
  378. */
  379. tkey->mode = uint16_fromregion(&sr);
  380. isc_region_consume(&sr, 2);
  381. /*
  382. * Error.
  383. */
  384. tkey->error = uint16_fromregion(&sr);
  385. isc_region_consume(&sr, 2);
  386. /*
  387. * Key size.
  388. */
  389. tkey->keylen = uint16_fromregion(&sr);
  390. isc_region_consume(&sr, 2);
  391. /*
  392. * Key.
  393. */
  394. tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
  395. if (tkey->key == NULL)
  396. goto cleanup;
  397. isc_region_consume(&sr, tkey->keylen);
  398. /*
  399. * Other size.
  400. */
  401. tkey->otherlen = uint16_fromregion(&sr);
  402. isc_region_consume(&sr, 2);
  403. /*
  404. * Other.
  405. */
  406. tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
  407. if (tkey->other == NULL)
  408. goto cleanup;
  409. tkey->mctx = mctx;
  410. return (ISC_R_SUCCESS);
  411. cleanup:
  412. if (mctx != NULL)
  413. dns_name_free(&tkey->algorithm, mctx);
  414. if (mctx != NULL && tkey->key != NULL)
  415. isc_mem_free(mctx, tkey->key);
  416. return (ISC_R_NOMEMORY);
  417. }
  418. static inline void
  419. freestruct_tkey(ARGS_FREESTRUCT) {
  420. dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
  421. REQUIRE(source != NULL);
  422. if (tkey->mctx == NULL)
  423. return;
  424. dns_name_free(&tkey->algorithm, tkey->mctx);
  425. if (tkey->key != NULL)
  426. isc_mem_free(tkey->mctx, tkey->key);
  427. if (tkey->other != NULL)
  428. isc_mem_free(tkey->mctx, tkey->other);
  429. tkey->mctx = NULL;
  430. }
  431. static inline isc_result_t
  432. additionaldata_tkey(ARGS_ADDLDATA) {
  433. UNUSED(rdata);
  434. UNUSED(add);
  435. UNUSED(arg);
  436. REQUIRE(rdata->type == 249);
  437. return (ISC_R_SUCCESS);
  438. }
  439. static inline isc_result_t
  440. digest_tkey(ARGS_DIGEST) {
  441. UNUSED(rdata);
  442. UNUSED(digest);
  443. UNUSED(arg);
  444. REQUIRE(rdata->type == 249);
  445. return (ISC_R_NOTIMPLEMENTED);
  446. }
  447. static inline isc_boolean_t
  448. checkowner_tkey(ARGS_CHECKOWNER) {
  449. REQUIRE(type == 249);
  450. UNUSED(name);
  451. UNUSED(type);
  452. UNUSED(rdclass);
  453. UNUSED(wildcard);
  454. return (ISC_TRUE);
  455. }
  456. static inline isc_boolean_t
  457. checknames_tkey(ARGS_CHECKNAMES) {
  458. REQUIRE(rdata->type == 249);
  459. UNUSED(rdata);
  460. UNUSED(owner);
  461. UNUSED(bad);
  462. return (ISC_TRUE);
  463. }
  464. static inline isc_result_t
  465. casecompare_tkey(ARGS_COMPARE) {
  466. return (compare_tkey(rdata1, rdata2));
  467. }
  468. #endif /* RDATA_GENERIC_TKEY_249_C */