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

https://bitbucket.org/freebsd/freebsd-head/ · C · 582 lines · 321 code · 103 blank · 158 comment · 59 complexity · e072b96291f6fcc384477ac891f5b010 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 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. /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
  19. /* RFC2535 */
  20. #ifndef RDATA_GENERIC_SIG_24_C
  21. #define RDATA_GENERIC_SIG_24_C
  22. #define RRTYPE_SIG_ATTRIBUTES (0)
  23. static inline isc_result_t
  24. fromtext_sig(ARGS_FROMTEXT) {
  25. isc_token_t token;
  26. unsigned char c;
  27. long i;
  28. dns_rdatatype_t covered;
  29. char *e;
  30. isc_result_t result;
  31. dns_name_t name;
  32. isc_buffer_t buffer;
  33. isc_uint32_t time_signed, time_expire;
  34. REQUIRE(type == 24);
  35. UNUSED(type);
  36. UNUSED(rdclass);
  37. UNUSED(callbacks);
  38. /*
  39. * Type covered.
  40. */
  41. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  42. ISC_FALSE));
  43. result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
  44. if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
  45. i = strtol(DNS_AS_STR(token), &e, 10);
  46. if (i < 0 || i > 65535)
  47. RETTOK(ISC_R_RANGE);
  48. if (*e != 0)
  49. RETTOK(result);
  50. covered = (dns_rdatatype_t)i;
  51. }
  52. RETERR(uint16_tobuffer(covered, target));
  53. /*
  54. * Algorithm.
  55. */
  56. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  57. ISC_FALSE));
  58. RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
  59. RETERR(mem_tobuffer(target, &c, 1));
  60. /*
  61. * Labels.
  62. */
  63. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  64. ISC_FALSE));
  65. if (token.value.as_ulong > 0xffU)
  66. RETTOK(ISC_R_RANGE);
  67. c = (unsigned char)token.value.as_ulong;
  68. RETERR(mem_tobuffer(target, &c, 1));
  69. /*
  70. * Original ttl.
  71. */
  72. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  73. ISC_FALSE));
  74. RETERR(uint32_tobuffer(token.value.as_ulong, target));
  75. /*
  76. * Signature expiration.
  77. */
  78. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  79. ISC_FALSE));
  80. RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
  81. RETERR(uint32_tobuffer(time_expire, target));
  82. /*
  83. * Time signed.
  84. */
  85. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  86. ISC_FALSE));
  87. RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
  88. RETERR(uint32_tobuffer(time_signed, target));
  89. /*
  90. * Key footprint.
  91. */
  92. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
  93. ISC_FALSE));
  94. RETERR(uint16_tobuffer(token.value.as_ulong, target));
  95. /*
  96. * Signer.
  97. */
  98. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  99. ISC_FALSE));
  100. dns_name_init(&name, NULL);
  101. buffer_fromregion(&buffer, &token.value.as_region);
  102. origin = (origin != NULL) ? origin : dns_rootname;
  103. RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
  104. /*
  105. * Sig.
  106. */
  107. return (isc_base64_tobuffer(lexer, target, -1));
  108. }
  109. static inline isc_result_t
  110. totext_sig(ARGS_TOTEXT) {
  111. isc_region_t sr;
  112. char buf[sizeof("4294967295")];
  113. dns_rdatatype_t covered;
  114. unsigned long ttl;
  115. unsigned long when;
  116. unsigned long exp;
  117. unsigned long foot;
  118. dns_name_t name;
  119. dns_name_t prefix;
  120. isc_boolean_t sub;
  121. REQUIRE(rdata->type == 24);
  122. REQUIRE(rdata->length != 0);
  123. dns_rdata_toregion(rdata, &sr);
  124. /*
  125. * Type covered.
  126. */
  127. covered = uint16_fromregion(&sr);
  128. isc_region_consume(&sr, 2);
  129. /*
  130. * XXXAG We should have something like dns_rdatatype_isknown()
  131. * that does the right thing with type 0.
  132. */
  133. if (dns_rdatatype_isknown(covered) && covered != 0) {
  134. RETERR(dns_rdatatype_totext(covered, target));
  135. } else {
  136. char buf[sizeof("65535")];
  137. sprintf(buf, "%u", covered);
  138. RETERR(str_totext(buf, target));
  139. }
  140. RETERR(str_totext(" ", target));
  141. /*
  142. * Algorithm.
  143. */
  144. sprintf(buf, "%u", sr.base[0]);
  145. isc_region_consume(&sr, 1);
  146. RETERR(str_totext(buf, target));
  147. RETERR(str_totext(" ", target));
  148. /*
  149. * Labels.
  150. */
  151. sprintf(buf, "%u", sr.base[0]);
  152. isc_region_consume(&sr, 1);
  153. RETERR(str_totext(buf, target));
  154. RETERR(str_totext(" ", target));
  155. /*
  156. * Ttl.
  157. */
  158. ttl = uint32_fromregion(&sr);
  159. isc_region_consume(&sr, 4);
  160. sprintf(buf, "%lu", ttl);
  161. RETERR(str_totext(buf, target));
  162. RETERR(str_totext(" ", target));
  163. /*
  164. * Sig exp.
  165. */
  166. exp = uint32_fromregion(&sr);
  167. isc_region_consume(&sr, 4);
  168. RETERR(dns_time32_totext(exp, target));
  169. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  170. RETERR(str_totext(" (", target));
  171. RETERR(str_totext(tctx->linebreak, target));
  172. /*
  173. * Time signed.
  174. */
  175. when = uint32_fromregion(&sr);
  176. isc_region_consume(&sr, 4);
  177. RETERR(dns_time32_totext(when, target));
  178. RETERR(str_totext(" ", target));
  179. /*
  180. * Footprint.
  181. */
  182. foot = uint16_fromregion(&sr);
  183. isc_region_consume(&sr, 2);
  184. sprintf(buf, "%lu", foot);
  185. RETERR(str_totext(buf, target));
  186. RETERR(str_totext(" ", target));
  187. /*
  188. * Signer.
  189. */
  190. dns_name_init(&name, NULL);
  191. dns_name_init(&prefix, NULL);
  192. dns_name_fromregion(&name, &sr);
  193. isc_region_consume(&sr, name_length(&name));
  194. sub = name_prefix(&name, tctx->origin, &prefix);
  195. RETERR(dns_name_totext(&prefix, sub, target));
  196. /*
  197. * Sig.
  198. */
  199. RETERR(str_totext(tctx->linebreak, target));
  200. RETERR(isc_base64_totext(&sr, tctx->width - 2,
  201. tctx->linebreak, target));
  202. if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
  203. RETERR(str_totext(" )", target));
  204. return (ISC_R_SUCCESS);
  205. }
  206. static inline isc_result_t
  207. fromwire_sig(ARGS_FROMWIRE) {
  208. isc_region_t sr;
  209. dns_name_t name;
  210. REQUIRE(type == 24);
  211. UNUSED(type);
  212. UNUSED(rdclass);
  213. dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  214. isc_buffer_activeregion(source, &sr);
  215. /*
  216. * type covered: 2
  217. * algorithm: 1
  218. * labels: 1
  219. * original ttl: 4
  220. * signature expiration: 4
  221. * time signed: 4
  222. * key footprint: 2
  223. */
  224. if (sr.length < 18)
  225. return (ISC_R_UNEXPECTEDEND);
  226. isc_buffer_forward(source, 18);
  227. RETERR(mem_tobuffer(target, sr.base, 18));
  228. /*
  229. * Signer.
  230. */
  231. dns_name_init(&name, NULL);
  232. RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  233. /*
  234. * Sig.
  235. */
  236. isc_buffer_activeregion(source, &sr);
  237. isc_buffer_forward(source, sr.length);
  238. return (mem_tobuffer(target, sr.base, sr.length));
  239. }
  240. static inline isc_result_t
  241. towire_sig(ARGS_TOWIRE) {
  242. isc_region_t sr;
  243. dns_name_t name;
  244. dns_offsets_t offsets;
  245. REQUIRE(rdata->type == 24);
  246. REQUIRE(rdata->length != 0);
  247. dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
  248. dns_rdata_toregion(rdata, &sr);
  249. /*
  250. * type covered: 2
  251. * algorithm: 1
  252. * labels: 1
  253. * original ttl: 4
  254. * signature expiration: 4
  255. * time signed: 4
  256. * key footprint: 2
  257. */
  258. RETERR(mem_tobuffer(target, sr.base, 18));
  259. isc_region_consume(&sr, 18);
  260. /*
  261. * Signer.
  262. */
  263. dns_name_init(&name, offsets);
  264. dns_name_fromregion(&name, &sr);
  265. isc_region_consume(&sr, name_length(&name));
  266. RETERR(dns_name_towire(&name, cctx, target));
  267. /*
  268. * Signature.
  269. */
  270. return (mem_tobuffer(target, sr.base, sr.length));
  271. }
  272. static inline int
  273. compare_sig(ARGS_COMPARE) {
  274. isc_region_t r1;
  275. isc_region_t r2;
  276. dns_name_t name1;
  277. dns_name_t name2;
  278. int order;
  279. REQUIRE(rdata1->type == rdata2->type);
  280. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  281. REQUIRE(rdata1->type == 24);
  282. REQUIRE(rdata1->length != 0);
  283. REQUIRE(rdata2->length != 0);
  284. dns_rdata_toregion(rdata1, &r1);
  285. dns_rdata_toregion(rdata2, &r2);
  286. INSIST(r1.length > 18);
  287. INSIST(r2.length > 18);
  288. r1.length = 18;
  289. r2.length = 18;
  290. order = isc_region_compare(&r1, &r2);
  291. if (order != 0)
  292. return (order);
  293. dns_name_init(&name1, NULL);
  294. dns_name_init(&name2, NULL);
  295. dns_rdata_toregion(rdata1, &r1);
  296. dns_rdata_toregion(rdata2, &r2);
  297. isc_region_consume(&r1, 18);
  298. isc_region_consume(&r2, 18);
  299. dns_name_fromregion(&name1, &r1);
  300. dns_name_fromregion(&name2, &r2);
  301. order = dns_name_rdatacompare(&name1, &name2);
  302. if (order != 0)
  303. return (order);
  304. isc_region_consume(&r1, name_length(&name1));
  305. isc_region_consume(&r2, name_length(&name2));
  306. return (isc_region_compare(&r1, &r2));
  307. }
  308. static inline isc_result_t
  309. fromstruct_sig(ARGS_FROMSTRUCT) {
  310. dns_rdata_sig_t *sig = source;
  311. REQUIRE(type == 24);
  312. REQUIRE(source != NULL);
  313. REQUIRE(sig->common.rdtype == type);
  314. REQUIRE(sig->common.rdclass == rdclass);
  315. REQUIRE(sig->signature != NULL || sig->siglen == 0);
  316. UNUSED(type);
  317. UNUSED(rdclass);
  318. /*
  319. * Type covered.
  320. */
  321. RETERR(uint16_tobuffer(sig->covered, target));
  322. /*
  323. * Algorithm.
  324. */
  325. RETERR(uint8_tobuffer(sig->algorithm, target));
  326. /*
  327. * Labels.
  328. */
  329. RETERR(uint8_tobuffer(sig->labels, target));
  330. /*
  331. * Original TTL.
  332. */
  333. RETERR(uint32_tobuffer(sig->originalttl, target));
  334. /*
  335. * Expire time.
  336. */
  337. RETERR(uint32_tobuffer(sig->timeexpire, target));
  338. /*
  339. * Time signed.
  340. */
  341. RETERR(uint32_tobuffer(sig->timesigned, target));
  342. /*
  343. * Key ID.
  344. */
  345. RETERR(uint16_tobuffer(sig->keyid, target));
  346. /*
  347. * Signer name.
  348. */
  349. RETERR(name_tobuffer(&sig->signer, target));
  350. /*
  351. * Signature.
  352. */
  353. return (mem_tobuffer(target, sig->signature, sig->siglen));
  354. }
  355. static inline isc_result_t
  356. tostruct_sig(ARGS_TOSTRUCT) {
  357. isc_region_t sr;
  358. dns_rdata_sig_t *sig = target;
  359. dns_name_t signer;
  360. REQUIRE(rdata->type == 24);
  361. REQUIRE(target != NULL);
  362. REQUIRE(rdata->length != 0);
  363. sig->common.rdclass = rdata->rdclass;
  364. sig->common.rdtype = rdata->type;
  365. ISC_LINK_INIT(&sig->common, link);
  366. dns_rdata_toregion(rdata, &sr);
  367. /*
  368. * Type covered.
  369. */
  370. sig->covered = uint16_fromregion(&sr);
  371. isc_region_consume(&sr, 2);
  372. /*
  373. * Algorithm.
  374. */
  375. sig->algorithm = uint8_fromregion(&sr);
  376. isc_region_consume(&sr, 1);
  377. /*
  378. * Labels.
  379. */
  380. sig->labels = uint8_fromregion(&sr);
  381. isc_region_consume(&sr, 1);
  382. /*
  383. * Original TTL.
  384. */
  385. sig->originalttl = uint32_fromregion(&sr);
  386. isc_region_consume(&sr, 4);
  387. /*
  388. * Expire time.
  389. */
  390. sig->timeexpire = uint32_fromregion(&sr);
  391. isc_region_consume(&sr, 4);
  392. /*
  393. * Time signed.
  394. */
  395. sig->timesigned = uint32_fromregion(&sr);
  396. isc_region_consume(&sr, 4);
  397. /*
  398. * Key ID.
  399. */
  400. sig->keyid = uint16_fromregion(&sr);
  401. isc_region_consume(&sr, 2);
  402. dns_name_init(&signer, NULL);
  403. dns_name_fromregion(&signer, &sr);
  404. dns_name_init(&sig->signer, NULL);
  405. RETERR(name_duporclone(&signer, mctx, &sig->signer));
  406. isc_region_consume(&sr, name_length(&sig->signer));
  407. /*
  408. * Signature.
  409. */
  410. sig->siglen = sr.length;
  411. sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
  412. if (sig->signature == NULL)
  413. goto cleanup;
  414. sig->mctx = mctx;
  415. return (ISC_R_SUCCESS);
  416. cleanup:
  417. if (mctx != NULL)
  418. dns_name_free(&sig->signer, mctx);
  419. return (ISC_R_NOMEMORY);
  420. }
  421. static inline void
  422. freestruct_sig(ARGS_FREESTRUCT) {
  423. dns_rdata_sig_t *sig = (dns_rdata_sig_t *) source;
  424. REQUIRE(source != NULL);
  425. REQUIRE(sig->common.rdtype == 24);
  426. if (sig->mctx == NULL)
  427. return;
  428. dns_name_free(&sig->signer, sig->mctx);
  429. if (sig->signature != NULL)
  430. isc_mem_free(sig->mctx, sig->signature);
  431. sig->mctx = NULL;
  432. }
  433. static inline isc_result_t
  434. additionaldata_sig(ARGS_ADDLDATA) {
  435. REQUIRE(rdata->type == 24);
  436. UNUSED(rdata);
  437. UNUSED(add);
  438. UNUSED(arg);
  439. return (ISC_R_SUCCESS);
  440. }
  441. static inline isc_result_t
  442. digest_sig(ARGS_DIGEST) {
  443. REQUIRE(rdata->type == 24);
  444. UNUSED(rdata);
  445. UNUSED(digest);
  446. UNUSED(arg);
  447. return (ISC_R_NOTIMPLEMENTED);
  448. }
  449. static inline dns_rdatatype_t
  450. covers_sig(dns_rdata_t *rdata) {
  451. dns_rdatatype_t type;
  452. isc_region_t r;
  453. REQUIRE(rdata->type == 24);
  454. dns_rdata_toregion(rdata, &r);
  455. type = uint16_fromregion(&r);
  456. return (type);
  457. }
  458. static inline isc_boolean_t
  459. checkowner_sig(ARGS_CHECKOWNER) {
  460. REQUIRE(type == 24);
  461. UNUSED(name);
  462. UNUSED(type);
  463. UNUSED(rdclass);
  464. UNUSED(wildcard);
  465. return (ISC_TRUE);
  466. }
  467. static inline isc_boolean_t
  468. checknames_sig(ARGS_CHECKNAMES) {
  469. REQUIRE(rdata->type == 24);
  470. UNUSED(rdata);
  471. UNUSED(owner);
  472. UNUSED(bad);
  473. return (ISC_TRUE);
  474. }
  475. static inline int
  476. casecompare_sig(ARGS_COMPARE) {
  477. return (compare_sig(rdata1, rdata2));
  478. }
  479. #endif /* RDATA_GENERIC_SIG_24_C */