nsec3_50.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <string>
  17. #include <sstream>
  18. #include <vector>
  19. #include <boost/lexical_cast.hpp>
  20. #include <util/base32hex.h>
  21. #include <util/buffer.h>
  22. #include <util/hex.h>
  23. #include <dns/exceptions.h>
  24. #include <dns/messagerenderer.h>
  25. #include <dns/name.h>
  26. #include <dns/rrtype.h>
  27. #include <dns/rrttl.h>
  28. #include <dns/rdata.h>
  29. #include <dns/rdataclass.h>
  30. #include <dns/rdata/generic/detail/nsec_bitmap.h>
  31. #include <stdio.h>
  32. #include <time.h>
  33. using namespace std;
  34. using namespace isc::dns::rdata::generic::detail::nsec;
  35. using namespace isc::util;
  36. // BEGIN_ISC_NAMESPACE
  37. // BEGIN_RDATA_NAMESPACE
  38. struct NSEC3Impl {
  39. // straightforward representation of NSEC3 RDATA fields
  40. NSEC3Impl(uint8_t hashalg, uint8_t flags, uint16_t iterations,
  41. vector<uint8_t>salt, vector<uint8_t>next,
  42. vector<uint8_t> typebits) :
  43. hashalg_(hashalg), flags_(flags), iterations_(iterations),
  44. salt_(salt), next_(next), typebits_(typebits)
  45. {}
  46. uint8_t hashalg_;
  47. uint8_t flags_;
  48. uint16_t iterations_;
  49. vector<uint8_t> salt_;
  50. vector<uint8_t> next_;
  51. vector<uint8_t> typebits_;
  52. };
  53. NSEC3::NSEC3(const string& nsec3_str) :
  54. impl_(NULL)
  55. {
  56. istringstream iss(nsec3_str);
  57. unsigned int hashalg, flags, iterations;
  58. string iterations_str, salthex, nexthash;
  59. iss >> hashalg >> flags >> iterations_str >> salthex >> nexthash;
  60. if (iss.bad() || iss.fail()) {
  61. isc_throw(InvalidRdataText, "Invalid NSEC3 text: " << nsec3_str);
  62. }
  63. if (hashalg > 0xff) {
  64. isc_throw(InvalidRdataText,
  65. "NSEC3 hash algorithm out of range: " << hashalg);
  66. }
  67. if (flags > 0xff) {
  68. isc_throw(InvalidRdataText, "NSEC3 flags out of range: " << flags);
  69. }
  70. // Convert iteration. To reject an invalid case where there's no space
  71. // between iteration and salt, we extract this field as string and convert
  72. // to integer.
  73. try {
  74. iterations = boost::lexical_cast<unsigned int>(iterations_str);
  75. } catch (const boost::bad_lexical_cast&) {
  76. isc_throw(InvalidRdataText, "Bad NSEC3 iteration: " << iterations_str);
  77. }
  78. if (iterations > 0xffff) {
  79. isc_throw(InvalidRdataText, "NSEC3 iterations out of range: " <<
  80. iterations);
  81. }
  82. vector<uint8_t> salt;
  83. if (salthex != "-") { // "-" means a 0-length salt
  84. decodeHex(salthex, salt);
  85. }
  86. if (salt.size() > 255) {
  87. isc_throw(InvalidRdataText, "NSEC3 salt is too long: "
  88. << salt.size() << " bytes");
  89. }
  90. vector<uint8_t> next;
  91. decodeBase32Hex(nexthash, next);
  92. if (next.size() > 255) {
  93. isc_throw(InvalidRdataText, "NSEC3 hash is too long: "
  94. << next.size() << " bytes");
  95. }
  96. // For NSEC3 empty bitmap is possible and allowed.
  97. if (iss.eof()) {
  98. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next,
  99. vector<uint8_t>());
  100. return;
  101. }
  102. vector<uint8_t> typebits;
  103. uint8_t bitmap[8 * 1024]; // 64k bits
  104. memset(bitmap, 0, sizeof(bitmap));
  105. do {
  106. string type;
  107. iss >> type;
  108. if (type.length() != 0) {
  109. try {
  110. const int code = RRType(type).getCode();
  111. bitmap[code / 8] |= (0x80 >> (code % 8));
  112. } catch (...) {
  113. isc_throw(InvalidRdataText, "Invalid RRtype in NSEC3");
  114. }
  115. }
  116. } while (!iss.eof());
  117. for (int window = 0; window < 256; window++) {
  118. int octet;
  119. for (octet = 31; octet >= 0; octet--) {
  120. if (bitmap[window * 32 + octet] != 0) {
  121. break;
  122. }
  123. }
  124. if (octet < 0)
  125. continue;
  126. typebits.push_back(window);
  127. typebits.push_back(octet + 1);
  128. for (int i = 0; i <= octet; i++) {
  129. typebits.push_back(bitmap[window * 32 + i]);
  130. }
  131. }
  132. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  133. }
  134. NSEC3::NSEC3(InputBuffer& buffer, size_t rdata_len) {
  135. // NSEC3 RR must have at least 5 octets:
  136. // hash algorithm(1), flags(1), iteration(2), saltlen(1)
  137. if (rdata_len < 5) {
  138. isc_throw(DNSMessageFORMERR, "NSEC3 too short, length: " << rdata_len);
  139. }
  140. const uint8_t hashalg = buffer.readUint8();
  141. const uint8_t flags = buffer.readUint8();
  142. const uint16_t iterations = buffer.readUint16();
  143. const uint8_t saltlen = buffer.readUint8();
  144. rdata_len -= 5;
  145. if (rdata_len < saltlen) {
  146. isc_throw(DNSMessageFORMERR, "NSEC3 salt length is too large: " <<
  147. static_cast<unsigned int>(saltlen));
  148. }
  149. vector<uint8_t> salt(saltlen);
  150. if (saltlen > 0) {
  151. buffer.readData(&salt[0], saltlen);
  152. rdata_len -= saltlen;
  153. }
  154. const uint8_t nextlen = buffer.readUint8();
  155. --rdata_len;
  156. if (nextlen == 0 || rdata_len < nextlen) {
  157. isc_throw(DNSMessageFORMERR, "NSEC3 invalid hash length: " <<
  158. static_cast<unsigned int>(nextlen));
  159. }
  160. vector<uint8_t> next(nextlen);
  161. buffer.readData(&next[0], nextlen);
  162. rdata_len -= nextlen;
  163. vector<uint8_t> typebits(rdata_len);
  164. if (rdata_len > 0) {
  165. // Read and parse the bitmaps only when they exist; empty bitmap
  166. // is possible for NSEC3.
  167. buffer.readData(&typebits[0], rdata_len);
  168. checkRRTypeBitmaps("NSEC3", typebits);
  169. }
  170. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  171. }
  172. NSEC3::NSEC3(const NSEC3& source) :
  173. Rdata(), impl_(new NSEC3Impl(*source.impl_))
  174. {}
  175. NSEC3&
  176. NSEC3::operator=(const NSEC3& source) {
  177. if (impl_ == source.impl_) {
  178. return (*this);
  179. }
  180. NSEC3Impl* newimpl = new NSEC3Impl(*source.impl_);
  181. delete impl_;
  182. impl_ = newimpl;
  183. return (*this);
  184. }
  185. NSEC3::~NSEC3() {
  186. delete impl_;
  187. }
  188. string
  189. NSEC3::toText() const {
  190. ostringstream s;
  191. int len = 0;
  192. for (int i = 0; i < impl_->typebits_.size(); i += len) {
  193. assert(i + 2 <= impl_->typebits_.size());
  194. int window = impl_->typebits_[i];
  195. len = impl_->typebits_[i + 1];
  196. assert(len >= 0 && len < 32);
  197. i += 2;
  198. for (int j = 0; j < len; j++) {
  199. if (impl_->typebits_[i + j] == 0) {
  200. continue;
  201. }
  202. for (int k = 0; k < 8; k++) {
  203. if ((impl_->typebits_[i + j] & (0x80 >> k)) == 0) {
  204. continue;
  205. }
  206. int t = window * 256 + j * 8 + k;
  207. s << " " << RRType(t).toText();
  208. }
  209. }
  210. }
  211. using namespace boost;
  212. return (lexical_cast<string>(static_cast<int>(impl_->hashalg_)) +
  213. " " + lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  214. " " + lexical_cast<string>(static_cast<int>(impl_->iterations_)) +
  215. " " + encodeHex(impl_->salt_) +
  216. " " + encodeBase32Hex(impl_->next_) + s.str());
  217. }
  218. void
  219. NSEC3::toWire(OutputBuffer& buffer) const {
  220. buffer.writeUint8(impl_->hashalg_);
  221. buffer.writeUint8(impl_->flags_);
  222. buffer.writeUint16(impl_->iterations_);
  223. buffer.writeUint8(impl_->salt_.size());
  224. buffer.writeData(&impl_->salt_[0], impl_->salt_.size());
  225. buffer.writeUint8(impl_->next_.size());
  226. buffer.writeData(&impl_->next_[0], impl_->next_.size());
  227. buffer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  228. }
  229. void
  230. NSEC3::toWire(MessageRenderer& renderer) const {
  231. renderer.writeUint8(impl_->hashalg_);
  232. renderer.writeUint8(impl_->flags_);
  233. renderer.writeUint16(impl_->iterations_);
  234. renderer.writeUint8(impl_->salt_.size());
  235. renderer.writeData(&impl_->salt_[0], impl_->salt_.size());
  236. renderer.writeUint8(impl_->next_.size());
  237. renderer.writeData(&impl_->next_[0], impl_->next_.size());
  238. renderer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  239. }
  240. int
  241. NSEC3::compare(const Rdata& other) const {
  242. const NSEC3& other_nsec3 = dynamic_cast<const NSEC3&>(other);
  243. if (impl_->hashalg_ != other_nsec3.impl_->hashalg_) {
  244. return (impl_->hashalg_ < other_nsec3.impl_->hashalg_ ? -1 : 1);
  245. }
  246. if (impl_->flags_ != other_nsec3.impl_->flags_) {
  247. return (impl_->flags_ < other_nsec3.impl_->flags_ ? -1 : 1);
  248. }
  249. if (impl_->iterations_ != other_nsec3.impl_->iterations_) {
  250. return (impl_->iterations_ < other_nsec3.impl_->iterations_ ? -1 : 1);
  251. }
  252. size_t this_len = impl_->salt_.size();
  253. size_t other_len = other_nsec3.impl_->salt_.size();
  254. size_t cmplen = min(this_len, other_len);
  255. int cmp = memcmp(&impl_->salt_[0], &other_nsec3.impl_->salt_[0], cmplen);
  256. if (cmp != 0) {
  257. return (cmp);
  258. } else if (this_len < other_len) {
  259. return (-1);
  260. } else if (this_len > other_len) {
  261. return (1);
  262. }
  263. this_len = impl_->salt_.size();
  264. other_len = other_nsec3.impl_->salt_.size();
  265. cmplen = min(this_len, other_len);
  266. cmp = memcmp(&impl_->next_[0], &other_nsec3.impl_->next_[0], cmplen);
  267. if (cmp != 0) {
  268. return (cmp);
  269. } else if (this_len < other_len) {
  270. return (-1);
  271. } else if (this_len > other_len) {
  272. return (1);
  273. }
  274. this_len = impl_->typebits_.size();
  275. other_len = other_nsec3.impl_->typebits_.size();
  276. cmplen = min(this_len, other_len);
  277. cmp = memcmp(&impl_->typebits_[0], &other_nsec3.impl_->typebits_[0],
  278. cmplen);
  279. if (cmp != 0) {
  280. return (cmp);
  281. } else if (this_len < other_len) {
  282. return (-1);
  283. } else if (this_len > other_len) {
  284. return (1);
  285. } else {
  286. return (0);
  287. }
  288. }
  289. uint8_t
  290. NSEC3::getHashalg() const {
  291. return (impl_->hashalg_);
  292. }
  293. uint8_t
  294. NSEC3::getFlags() const {
  295. return (impl_->flags_);
  296. }
  297. uint16_t
  298. NSEC3::getIterations() const {
  299. return (impl_->iterations_);
  300. }
  301. const vector<uint8_t>&
  302. NSEC3::getSalt() const {
  303. return (impl_->salt_);
  304. }
  305. const vector<uint8_t>&
  306. NSEC3::getNext() const {
  307. return (impl_->next_);
  308. }
  309. // END_RDATA_NAMESPACE
  310. // END_ISC_NAMESPACE