nsec3param_51.cc 5.4 KB

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