nsec3param_51.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <string>
  17. #include <sstream>
  18. #include <vector>
  19. #include <boost/lexical_cast.hpp>
  20. #include <dns/buffer.h>
  21. #include <dns/util/hex.h>
  22. #include <dns/messagerenderer.h>
  23. #include <dns/name.h>
  24. #include <dns/rdata.h>
  25. #include <dns/rdataclass.h>
  26. #include <stdio.h>
  27. #include <time.h>
  28. using namespace std;
  29. // BEGIN_ISC_NAMESPACE
  30. // BEGIN_RDATA_NAMESPACE
  31. struct NSEC3PARAMImpl {
  32. // straightforward representation of NSEC3PARAM RDATA fields
  33. NSEC3PARAMImpl(uint8_t hashalg, uint8_t flags, uint16_t iterations,
  34. const vector<uint8_t>& salt) :
  35. hashalg_(hashalg), flags_(flags), iterations_(iterations), salt_(salt)
  36. {}
  37. uint8_t hashalg_;
  38. uint8_t flags_;
  39. uint16_t iterations_;
  40. const vector<uint8_t> salt_;
  41. };
  42. NSEC3PARAM::NSEC3PARAM(const string& nsec3param_str) :
  43. impl_(NULL)
  44. {
  45. istringstream iss(nsec3param_str);
  46. uint16_t hashalg, flags, iterations;
  47. stringbuf saltbuf;
  48. iss >> hashalg >> flags >> iterations >> &saltbuf;
  49. if (iss.bad() || iss.fail()) {
  50. isc_throw(InvalidRdataText, "Invalid NSEC3PARAM text");
  51. }
  52. if (hashalg > 0xf) {
  53. isc_throw(InvalidRdataText, "NSEC3PARAM hash algorithm out of range");
  54. }
  55. if (flags > 0xff) {
  56. isc_throw(InvalidRdataText, "NSEC3PARAM flags out of range");
  57. }
  58. vector<uint8_t> salt;
  59. decodeHex(saltbuf.str(), salt);
  60. impl_ = new NSEC3PARAMImpl(hashalg, flags, iterations, salt);
  61. }
  62. NSEC3PARAM::NSEC3PARAM(InputBuffer& buffer, size_t rdata_len)
  63. {
  64. if (rdata_len < 4) {
  65. isc_throw(InvalidRdataLength, "NSEC3PARAM too short");
  66. }
  67. uint8_t hashalg = buffer.readUint8();
  68. uint8_t flags = buffer.readUint8();
  69. uint16_t iterations = buffer.readUint16();
  70. rdata_len -= 4;
  71. uint8_t saltlen = buffer.readUint8();
  72. --rdata_len;
  73. if (rdata_len < saltlen) {
  74. isc_throw(InvalidRdataLength, "NSEC3PARAM salt too short");
  75. }
  76. vector<uint8_t> salt(saltlen);
  77. buffer.readData(&salt[0], saltlen);
  78. impl_ = new NSEC3PARAMImpl(hashalg, flags, iterations, salt);
  79. }
  80. NSEC3PARAM::NSEC3PARAM(const NSEC3PARAM& source) :
  81. Rdata(), impl_(new NSEC3PARAMImpl(*source.impl_))
  82. {}
  83. NSEC3PARAM&
  84. NSEC3PARAM::operator=(const NSEC3PARAM& source)
  85. {
  86. if (impl_ == source.impl_) {
  87. return (*this);
  88. }
  89. NSEC3PARAMImpl* newimpl = new NSEC3PARAMImpl(*source.impl_);
  90. delete impl_;
  91. impl_ = newimpl;
  92. return (*this);
  93. }
  94. NSEC3PARAM::~NSEC3PARAM()
  95. {
  96. delete impl_;
  97. }
  98. string
  99. NSEC3PARAM::toText() const
  100. {
  101. using namespace boost;
  102. return (lexical_cast<string>(static_cast<int>(impl_->hashalg_)) +
  103. " " + lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  104. " " + lexical_cast<string>(static_cast<int>(impl_->iterations_)) +
  105. " " + encodeHex(impl_->salt_));
  106. }
  107. void
  108. NSEC3PARAM::toWire(OutputBuffer& buffer) const
  109. {
  110. buffer.writeUint8(impl_->hashalg_);
  111. buffer.writeUint8(impl_->flags_);
  112. buffer.writeUint16(impl_->iterations_);
  113. buffer.writeUint8(impl_->salt_.size());
  114. buffer.writeData(&impl_->salt_[0], impl_->salt_.size());
  115. }
  116. void
  117. NSEC3PARAM::toWire(MessageRenderer& renderer) const
  118. {
  119. renderer.writeUint8(impl_->hashalg_);
  120. renderer.writeUint8(impl_->flags_);
  121. renderer.writeUint16(impl_->iterations_);
  122. renderer.writeUint8(impl_->salt_.size());
  123. renderer.writeData(&impl_->salt_[0], impl_->salt_.size());
  124. }
  125. int
  126. NSEC3PARAM::compare(const Rdata& other) const
  127. {
  128. const NSEC3PARAM& other_param = dynamic_cast<const NSEC3PARAM&>(other);
  129. if (impl_->hashalg_ != other_param.impl_->hashalg_) {
  130. return (impl_->hashalg_ < other_param.impl_->hashalg_ ? -1 : 1);
  131. }
  132. if (impl_->flags_ != other_param.impl_->flags_) {
  133. return (impl_->flags_ < other_param.impl_->flags_ ? -1 : 1);
  134. }
  135. if (impl_->iterations_ != other_param.impl_->iterations_) {
  136. return (impl_->iterations_ < other_param.impl_->iterations_ ? -1 : 1);
  137. }
  138. size_t this_len = impl_->salt_.size();
  139. size_t other_len = other_param.impl_->salt_.size();
  140. size_t cmplen = min(this_len, other_len);
  141. int cmp = memcmp(&impl_->salt_[0], &other_param.impl_->salt_[0],
  142. cmplen);
  143. if (cmp != 0) {
  144. return (cmp);
  145. } else {
  146. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  147. }
  148. }
  149. uint8_t
  150. NSEC3PARAM::getHashalg() const {
  151. return (impl_->hashalg_);
  152. }
  153. uint8_t
  154. NSEC3PARAM::getFlags() const {
  155. return (impl_->flags_);
  156. }
  157. uint16_t
  158. NSEC3PARAM::getIterations() const {
  159. return (impl_->iterations_);
  160. }
  161. const vector<uint8_t>&
  162. NSEC3PARAM::getSalt() const {
  163. return (impl_->salt_);
  164. }
  165. // END_RDATA_NAMESPACE
  166. // END_ISC_NAMESPACE