nsec3_50.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/encode/base32hex.h>
  21. #include <util/encode/hex.h>
  22. #include <util/buffer.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::encode;
  36. using namespace isc::util;
  37. // BEGIN_ISC_NAMESPACE
  38. // BEGIN_RDATA_NAMESPACE
  39. struct NSEC3Impl {
  40. // straightforward representation of NSEC3 RDATA fields
  41. NSEC3Impl(uint8_t hashalg, uint8_t flags, uint16_t iterations,
  42. vector<uint8_t>salt, vector<uint8_t>next,
  43. vector<uint8_t> typebits) :
  44. hashalg_(hashalg), flags_(flags), iterations_(iterations),
  45. salt_(salt), next_(next), typebits_(typebits)
  46. {}
  47. uint8_t hashalg_;
  48. uint8_t flags_;
  49. uint16_t iterations_;
  50. vector<uint8_t> salt_;
  51. vector<uint8_t> next_;
  52. vector<uint8_t> typebits_;
  53. };
  54. NSEC3::NSEC3(const string& nsec3_str) :
  55. impl_(NULL)
  56. {
  57. istringstream iss(nsec3_str);
  58. unsigned int hashalg, flags, iterations;
  59. string iterations_str, salthex, nexthash;
  60. iss >> hashalg >> flags >> iterations_str >> salthex >> nexthash;
  61. if (iss.bad() || iss.fail()) {
  62. isc_throw(InvalidRdataText, "Invalid NSEC3 text: " << nsec3_str);
  63. }
  64. if (hashalg > 0xff) {
  65. isc_throw(InvalidRdataText,
  66. "NSEC3 hash algorithm out of range: " << hashalg);
  67. }
  68. if (flags > 0xff) {
  69. isc_throw(InvalidRdataText, "NSEC3 flags out of range: " << flags);
  70. }
  71. // Convert iteration. To reject an invalid case where there's no space
  72. // between iteration and salt, we extract this field as string and convert
  73. // to integer.
  74. try {
  75. iterations = boost::lexical_cast<unsigned int>(iterations_str);
  76. } catch (const boost::bad_lexical_cast&) {
  77. isc_throw(InvalidRdataText, "Bad NSEC3 iteration: " << iterations_str);
  78. }
  79. if (iterations > 0xffff) {
  80. isc_throw(InvalidRdataText, "NSEC3 iterations out of range: " <<
  81. iterations);
  82. }
  83. vector<uint8_t> salt;
  84. if (salthex != "-") { // "-" means a 0-length salt
  85. decodeHex(salthex, salt);
  86. }
  87. if (salt.size() > 255) {
  88. isc_throw(InvalidRdataText, "NSEC3 salt is too long: "
  89. << salt.size() << " bytes");
  90. }
  91. vector<uint8_t> next;
  92. decodeBase32Hex(nexthash, next);
  93. if (next.size() > 255) {
  94. isc_throw(InvalidRdataText, "NSEC3 hash is too long: "
  95. << next.size() << " bytes");
  96. }
  97. // For NSEC3 empty bitmap is possible and allowed.
  98. if (iss.eof()) {
  99. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next,
  100. vector<uint8_t>());
  101. return;
  102. }
  103. vector<uint8_t> typebits;
  104. uint8_t bitmap[8 * 1024]; // 64k bits
  105. memset(bitmap, 0, sizeof(bitmap));
  106. do {
  107. string type;
  108. iss >> type;
  109. if (type.length() != 0) {
  110. try {
  111. const int code = RRType(type).getCode();
  112. bitmap[code / 8] |= (0x80 >> (code % 8));
  113. } catch (...) {
  114. isc_throw(InvalidRdataText, "Invalid RRtype in NSEC3");
  115. }
  116. }
  117. } while (!iss.eof());
  118. for (int window = 0; window < 256; window++) {
  119. int octet;
  120. for (octet = 31; octet >= 0; octet--) {
  121. if (bitmap[window * 32 + octet] != 0) {
  122. break;
  123. }
  124. }
  125. if (octet < 0)
  126. continue;
  127. typebits.push_back(window);
  128. typebits.push_back(octet + 1);
  129. for (int i = 0; i <= octet; i++) {
  130. typebits.push_back(bitmap[window * 32 + i]);
  131. }
  132. }
  133. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  134. }
  135. NSEC3::NSEC3(InputBuffer& buffer, size_t rdata_len) {
  136. // NSEC3 RR must have at least 5 octets:
  137. // hash algorithm(1), flags(1), iteration(2), saltlen(1)
  138. if (rdata_len < 5) {
  139. isc_throw(DNSMessageFORMERR, "NSEC3 too short, length: " << rdata_len);
  140. }
  141. const uint8_t hashalg = buffer.readUint8();
  142. const uint8_t flags = buffer.readUint8();
  143. const uint16_t iterations = buffer.readUint16();
  144. const uint8_t saltlen = buffer.readUint8();
  145. rdata_len -= 5;
  146. if (rdata_len < saltlen) {
  147. isc_throw(DNSMessageFORMERR, "NSEC3 salt length is too large: " <<
  148. static_cast<unsigned int>(saltlen));
  149. }
  150. vector<uint8_t> salt(saltlen);
  151. if (saltlen > 0) {
  152. buffer.readData(&salt[0], saltlen);
  153. rdata_len -= saltlen;
  154. }
  155. const uint8_t nextlen = buffer.readUint8();
  156. --rdata_len;
  157. if (nextlen == 0 || rdata_len < nextlen) {
  158. isc_throw(DNSMessageFORMERR, "NSEC3 invalid hash length: " <<
  159. static_cast<unsigned int>(nextlen));
  160. }
  161. vector<uint8_t> next(nextlen);
  162. buffer.readData(&next[0], nextlen);
  163. rdata_len -= nextlen;
  164. vector<uint8_t> typebits(rdata_len);
  165. if (rdata_len > 0) {
  166. // Read and parse the bitmaps only when they exist; empty bitmap
  167. // is possible for NSEC3.
  168. buffer.readData(&typebits[0], rdata_len);
  169. checkRRTypeBitmaps("NSEC3", typebits);
  170. }
  171. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  172. }
  173. NSEC3::NSEC3(const NSEC3& source) :
  174. Rdata(), impl_(new NSEC3Impl(*source.impl_))
  175. {}
  176. NSEC3&
  177. NSEC3::operator=(const NSEC3& source) {
  178. if (impl_ == source.impl_) {
  179. return (*this);
  180. }
  181. NSEC3Impl* newimpl = new NSEC3Impl(*source.impl_);
  182. delete impl_;
  183. impl_ = newimpl;
  184. return (*this);
  185. }
  186. NSEC3::~NSEC3() {
  187. delete impl_;
  188. }
  189. string
  190. NSEC3::toText() const {
  191. ostringstream s;
  192. int len = 0;
  193. for (size_t i = 0; i < impl_->typebits_.size(); i += len) {
  194. assert(i + 2 <= impl_->typebits_.size());
  195. int window = impl_->typebits_[i];
  196. len = impl_->typebits_[i + 1];
  197. assert(len > 0 && len <= 32);
  198. i += 2;
  199. for (int j = 0; j < len; j++) {
  200. if (impl_->typebits_[i + j] == 0) {
  201. continue;
  202. }
  203. for (int k = 0; k < 8; k++) {
  204. if ((impl_->typebits_[i + j] & (0x80 >> k)) == 0) {
  205. continue;
  206. }
  207. int t = window * 256 + j * 8 + k;
  208. s << " " << RRType(t).toText();
  209. }
  210. }
  211. }
  212. using namespace boost;
  213. return (lexical_cast<string>(static_cast<int>(impl_->hashalg_)) +
  214. " " + lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  215. " " + lexical_cast<string>(static_cast<int>(impl_->iterations_)) +
  216. " " + encodeHex(impl_->salt_) +
  217. " " + encodeBase32Hex(impl_->next_) + s.str());
  218. }
  219. template <typename OUTPUT_TYPE>
  220. void
  221. toWireHelper(const NSEC3Impl& impl, OUTPUT_TYPE& output) {
  222. output.writeUint8(impl.hashalg_);
  223. output.writeUint8(impl.flags_);
  224. output.writeUint16(impl.iterations_);
  225. output.writeUint8(impl.salt_.size());
  226. output.writeData(&impl.salt_[0], impl.salt_.size());
  227. output.writeUint8(impl.next_.size());
  228. output.writeData(&impl.next_[0], impl.next_.size());
  229. if (!impl.typebits_.empty()) {
  230. output.writeData(&impl.typebits_[0], impl.typebits_.size());
  231. }
  232. }
  233. void
  234. NSEC3::toWire(OutputBuffer& buffer) const {
  235. toWireHelper(*impl_, buffer);
  236. }
  237. void
  238. NSEC3::toWire(AbstractMessageRenderer& renderer) const {
  239. toWireHelper(*impl_, renderer);
  240. }
  241. int
  242. NSEC3::compare(const Rdata& other) const {
  243. const NSEC3& other_nsec3 = dynamic_cast<const NSEC3&>(other);
  244. if (impl_->hashalg_ != other_nsec3.impl_->hashalg_) {
  245. return (impl_->hashalg_ < other_nsec3.impl_->hashalg_ ? -1 : 1);
  246. }
  247. if (impl_->flags_ != other_nsec3.impl_->flags_) {
  248. return (impl_->flags_ < other_nsec3.impl_->flags_ ? -1 : 1);
  249. }
  250. if (impl_->iterations_ != other_nsec3.impl_->iterations_) {
  251. return (impl_->iterations_ < other_nsec3.impl_->iterations_ ? -1 : 1);
  252. }
  253. size_t this_len = impl_->salt_.size();
  254. size_t other_len = other_nsec3.impl_->salt_.size();
  255. size_t cmplen = min(this_len, other_len);
  256. int cmp = memcmp(&impl_->salt_[0], &other_nsec3.impl_->salt_[0], cmplen);
  257. if (cmp != 0) {
  258. return (cmp);
  259. } else if (this_len < other_len) {
  260. return (-1);
  261. } else if (this_len > other_len) {
  262. return (1);
  263. }
  264. this_len = impl_->salt_.size();
  265. other_len = other_nsec3.impl_->salt_.size();
  266. cmplen = min(this_len, other_len);
  267. cmp = memcmp(&impl_->next_[0], &other_nsec3.impl_->next_[0], cmplen);
  268. if (cmp != 0) {
  269. return (cmp);
  270. } else if (this_len < other_len) {
  271. return (-1);
  272. } else if (this_len > other_len) {
  273. return (1);
  274. }
  275. this_len = impl_->typebits_.size();
  276. other_len = other_nsec3.impl_->typebits_.size();
  277. cmplen = min(this_len, other_len);
  278. cmp = memcmp(&impl_->typebits_[0], &other_nsec3.impl_->typebits_[0],
  279. cmplen);
  280. if (cmp != 0) {
  281. return (cmp);
  282. } else if (this_len < other_len) {
  283. return (-1);
  284. } else if (this_len > other_len) {
  285. return (1);
  286. } else {
  287. return (0);
  288. }
  289. }
  290. uint8_t
  291. NSEC3::getHashalg() const {
  292. return (impl_->hashalg_);
  293. }
  294. uint8_t
  295. NSEC3::getFlags() const {
  296. return (impl_->flags_);
  297. }
  298. uint16_t
  299. NSEC3::getIterations() const {
  300. return (impl_->iterations_);
  301. }
  302. const vector<uint8_t>&
  303. NSEC3::getSalt() const {
  304. return (impl_->salt_);
  305. }
  306. const vector<uint8_t>&
  307. NSEC3::getNext() const {
  308. return (impl_->next_);
  309. }
  310. // END_RDATA_NAMESPACE
  311. // END_ISC_NAMESPACE