nsec3_50.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // $Id$
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <string>
  18. #include <sstream>
  19. #include <vector>
  20. #include <boost/lexical_cast.hpp>
  21. #include <dns/util/base32hex.h>
  22. #include <dns/buffer.h>
  23. #include <dns/exceptions.h>
  24. #include <dns/util/hex.h>
  25. #include <dns/messagerenderer.h>
  26. #include <dns/name.h>
  27. #include <dns/rrtype.h>
  28. #include <dns/rrttl.h>
  29. #include <dns/rdata.h>
  30. #include <dns/rdataclass.h>
  31. #include <stdio.h>
  32. #include <time.h>
  33. using namespace std;
  34. // BEGIN_ISC_NAMESPACE
  35. // BEGIN_RDATA_NAMESPACE
  36. struct NSEC3Impl {
  37. // straightforward representation of NSEC3 RDATA fields
  38. NSEC3Impl(uint8_t hashalg, uint8_t flags, uint16_t iterations,
  39. vector<uint8_t>salt, vector<uint8_t>next,
  40. vector<uint8_t> typebits) :
  41. hashalg_(hashalg), flags_(flags), iterations_(iterations),
  42. salt_(salt), next_(next), typebits_(typebits)
  43. {}
  44. uint8_t hashalg_;
  45. uint8_t flags_;
  46. uint16_t iterations_;
  47. vector<uint8_t> salt_;
  48. vector<uint8_t> next_;
  49. vector<uint8_t> typebits_;
  50. };
  51. NSEC3::NSEC3(const string& nsec3_str) :
  52. impl_(NULL)
  53. {
  54. istringstream iss(nsec3_str);
  55. unsigned int hashalg, flags, iterations;
  56. string salthex;
  57. iss >> hashalg >> flags >> iterations >> salthex;
  58. if (iss.bad() || iss.fail()) {
  59. isc_throw(InvalidRdataText, "Invalid NSEC3 text");
  60. }
  61. if (hashalg > 0xf) {
  62. isc_throw(InvalidRdataText, "NSEC3 hash algorithm out of range");
  63. }
  64. if (flags > 0xff) {
  65. isc_throw(InvalidRdataText, "NSEC3 flags out of range");
  66. }
  67. if (iterations > 0xffff) {
  68. isc_throw(InvalidRdataText, "NSEC3 iterations out of range");
  69. }
  70. vector<uint8_t> salt;
  71. decodeHex(salthex, salt);
  72. string nextstr;
  73. iss >> setw(32) >> nextstr;
  74. vector<uint8_t> next;
  75. if (iss.bad() || iss.fail()) {
  76. isc_throw(InvalidRdataText, "Invalid NSEC3 hash algorithm");
  77. }
  78. decodeBase32Hex(nextstr, next);
  79. uint8_t bitmap[8 * 1024]; // 64k bits
  80. vector<uint8_t> typebits;
  81. memset(bitmap, 0, sizeof(bitmap));
  82. do {
  83. string type;
  84. int code;
  85. iss >> type;
  86. if (type.length() != 0) {
  87. try {
  88. code = RRType(type).getCode();
  89. bitmap[code / 8] |= (0x80 >> (code % 8));
  90. } catch (...) {
  91. isc_throw(InvalidRdataText, "Invalid RRtype in NSEC3");
  92. }
  93. }
  94. } while(!iss.eof());
  95. for (int window = 0; window < 256; window++) {
  96. int octet;
  97. for (octet = 31; octet >= 0; octet--) {
  98. if (bitmap[window * 32 + octet] != 0) {
  99. break;
  100. }
  101. }
  102. if (octet < 0)
  103. continue;
  104. typebits.push_back(window);
  105. typebits.push_back(octet + 1);
  106. for (int i = 0; i <= octet; i++) {
  107. typebits.push_back(bitmap[window * 32 + i]);
  108. }
  109. }
  110. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  111. }
  112. NSEC3::NSEC3(InputBuffer& buffer, size_t rdata_len) {
  113. if (rdata_len < 5) {
  114. isc_throw(InvalidRdataLength, "NSEC3 too short");
  115. }
  116. uint8_t hashalg = buffer.readUint8();
  117. uint8_t flags = buffer.readUint8();
  118. uint16_t iterations = buffer.readUint16();
  119. rdata_len -= 4;
  120. uint8_t saltlen = buffer.readUint8();
  121. --rdata_len;
  122. if (rdata_len < saltlen) {
  123. isc_throw(InvalidRdataLength, "NSEC3 salt too short");
  124. }
  125. vector<uint8_t> salt(saltlen);
  126. buffer.readData(&salt[0], saltlen);
  127. rdata_len -= saltlen;
  128. uint8_t nextlen = buffer.readUint8();
  129. --rdata_len;
  130. if (rdata_len < nextlen) {
  131. isc_throw(InvalidRdataLength, "NSEC3 next hash too short");
  132. }
  133. vector<uint8_t> next(nextlen);
  134. buffer.readData(&next[0], nextlen);
  135. rdata_len -= nextlen;
  136. if (rdata_len == 0) {
  137. isc_throw(InvalidRdataLength, "NSEC3 type bitmap too short");
  138. }
  139. vector<uint8_t> typebits(rdata_len);
  140. buffer.readData(&typebits[0], rdata_len);
  141. int len = 0;
  142. for (int i = 0; i < typebits.size(); i += len) {
  143. if (i + 2 > typebits.size()) {
  144. isc_throw(DNSMessageFORMERR, "Invalid rdata: "
  145. "bad NSEC3 type bitmap");
  146. }
  147. len = typebits[i + 1];
  148. if (len > 31) {
  149. isc_throw(DNSMessageFORMERR, "Invalid rdata: "
  150. "bad NSEC3 type bitmap");
  151. }
  152. i += 2;
  153. }
  154. impl_ = new NSEC3Impl(hashalg, flags, iterations, salt, next, typebits);
  155. }
  156. NSEC3::NSEC3(const NSEC3& source) :
  157. Rdata(), impl_(new NSEC3Impl(*source.impl_))
  158. {}
  159. NSEC3&
  160. NSEC3::operator=(const NSEC3& source) {
  161. if (impl_ == source.impl_) {
  162. return (*this);
  163. }
  164. NSEC3Impl* newimpl = new NSEC3Impl(*source.impl_);
  165. delete impl_;
  166. impl_ = newimpl;
  167. return (*this);
  168. }
  169. NSEC3::~NSEC3() {
  170. delete impl_;
  171. }
  172. string
  173. NSEC3::toText() const {
  174. ostringstream s;
  175. int len = 0;
  176. for (int i = 0; i < impl_->typebits_.size(); i += len) {
  177. assert(i + 2 <= impl_->typebits_.size());
  178. int window = impl_->typebits_[i];
  179. len = impl_->typebits_[i + 1];
  180. assert(len >= 0 && len < 32);
  181. i += 2;
  182. for (int j = 0; j < len; j++) {
  183. if (impl_->typebits_[i + j] == 0) {
  184. continue;
  185. }
  186. for (int k = 0; k < 8; k++) {
  187. if ((impl_->typebits_[i + j] & (0x80 >> k)) == 0) {
  188. continue;
  189. }
  190. int t = window * 256 + j * 8 + k;
  191. s << " " << RRType(t).toText();
  192. }
  193. }
  194. }
  195. using namespace boost;
  196. return (lexical_cast<string>(static_cast<int>(impl_->hashalg_)) +
  197. " " + lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  198. " " + lexical_cast<string>(static_cast<int>(impl_->iterations_)) +
  199. " " + encodeHex(impl_->salt_) +
  200. " " + encodeBase32Hex(impl_->next_) + s.str());
  201. }
  202. void
  203. NSEC3::toWire(OutputBuffer& buffer) const {
  204. buffer.writeUint8(impl_->hashalg_);
  205. buffer.writeUint8(impl_->flags_);
  206. buffer.writeUint16(impl_->iterations_);
  207. buffer.writeUint8(impl_->salt_.size());
  208. buffer.writeData(&impl_->salt_[0], impl_->salt_.size());
  209. buffer.writeUint8(impl_->next_.size());
  210. buffer.writeData(&impl_->next_[0], impl_->next_.size());
  211. buffer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  212. }
  213. void
  214. NSEC3::toWire(MessageRenderer& renderer) const {
  215. renderer.writeUint8(impl_->hashalg_);
  216. renderer.writeUint8(impl_->flags_);
  217. renderer.writeUint16(impl_->iterations_);
  218. renderer.writeUint8(impl_->salt_.size());
  219. renderer.writeData(&impl_->salt_[0], impl_->salt_.size());
  220. renderer.writeUint8(impl_->next_.size());
  221. renderer.writeData(&impl_->next_[0], impl_->next_.size());
  222. renderer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  223. }
  224. int
  225. NSEC3::compare(const Rdata& other) const {
  226. const NSEC3& other_nsec3 = dynamic_cast<const NSEC3&>(other);
  227. if (impl_->hashalg_ != other_nsec3.impl_->hashalg_) {
  228. return (impl_->hashalg_ < other_nsec3.impl_->hashalg_ ? -1 : 1);
  229. }
  230. if (impl_->flags_ != other_nsec3.impl_->flags_) {
  231. return (impl_->flags_ < other_nsec3.impl_->flags_ ? -1 : 1);
  232. }
  233. if (impl_->iterations_ != other_nsec3.impl_->iterations_) {
  234. return (impl_->iterations_ < other_nsec3.impl_->iterations_ ? -1 : 1);
  235. }
  236. size_t this_len = impl_->salt_.size();
  237. size_t other_len = other_nsec3.impl_->salt_.size();
  238. size_t cmplen = min(this_len, other_len);
  239. int cmp = memcmp(&impl_->salt_[0], &other_nsec3.impl_->salt_[0], cmplen);
  240. if (cmp != 0) {
  241. return (cmp);
  242. } else if (this_len < other_len) {
  243. return (-1);
  244. } else if (this_len > other_len) {
  245. return (1);
  246. }
  247. this_len = impl_->salt_.size();
  248. other_len = other_nsec3.impl_->salt_.size();
  249. cmplen = min(this_len, other_len);
  250. cmp = memcmp(&impl_->next_[0], &other_nsec3.impl_->next_[0], cmplen);
  251. if (cmp != 0) {
  252. return (cmp);
  253. } else if (this_len < other_len) {
  254. return (-1);
  255. } else if (this_len > other_len) {
  256. return (1);
  257. }
  258. this_len = impl_->typebits_.size();
  259. other_len = other_nsec3.impl_->typebits_.size();
  260. cmplen = min(this_len, other_len);
  261. cmp = memcmp(&impl_->typebits_[0], &other_nsec3.impl_->typebits_[0],
  262. cmplen);
  263. if (cmp != 0) {
  264. return (cmp);
  265. } else if (this_len < other_len) {
  266. return (-1);
  267. } else if (this_len > other_len) {
  268. return (1);
  269. } else {
  270. return (0);
  271. }
  272. }
  273. uint8_t
  274. NSEC3::getHashalg() const {
  275. return (impl_->hashalg_);
  276. }
  277. uint8_t
  278. NSEC3::getFlags() const {
  279. return (impl_->flags_);
  280. }
  281. uint16_t
  282. NSEC3::getIterations() const {
  283. return (impl_->iterations_);
  284. }
  285. vector<uint8_t>&
  286. NSEC3::getSalt() const {
  287. return (impl_->salt_);
  288. }
  289. // END_RDATA_NAMESPACE
  290. // END_ISC_NAMESPACE