rrsig_46.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <string>
  15. #include <iomanip>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <vector>
  19. #include <boost/lexical_cast.hpp>
  20. #include <util/encode/base64.h>
  21. #include <util/buffer.h>
  22. #include <util/time_utilities.h>
  23. #include <dns/messagerenderer.h>
  24. #include <dns/name.h>
  25. #include <dns/rrtype.h>
  26. #include <dns/rrttl.h>
  27. #include <dns/rdata.h>
  28. #include <dns/rdataclass.h>
  29. #include <stdio.h>
  30. #include <time.h>
  31. using namespace std;
  32. using namespace isc::util;
  33. using namespace isc::util::encode;
  34. // BEGIN_ISC_NAMESPACE
  35. // BEGIN_RDATA_NAMESPACE
  36. namespace {
  37. // This is the minimum necessary length of all wire-format RRSIG RDATA:
  38. // - two 8-bit fields (algorithm and labels)
  39. // - two 16-bit fields (covered and tag)
  40. // - three 32-bit fields (original TTL, expire and inception)
  41. const size_t RRSIG_MINIMUM_LEN = 2 * sizeof(uint8_t) + 2 * sizeof(uint16_t) +
  42. 3 * sizeof(uint32_t);
  43. }
  44. struct RRSIGImpl {
  45. // straightforward representation of RRSIG RDATA fields
  46. RRSIGImpl(const RRType& covered, uint8_t algorithm, uint8_t labels,
  47. uint32_t originalttl, uint32_t timeexpire, uint32_t timeinception,
  48. uint16_t tag, const Name& signer,
  49. const vector<uint8_t>& signature) :
  50. covered_(covered), algorithm_(algorithm), labels_(labels),
  51. originalttl_(originalttl), timeexpire_(timeexpire),
  52. timeinception_(timeinception), tag_(tag), signer_(signer),
  53. signature_(signature)
  54. {}
  55. const RRType covered_;
  56. uint8_t algorithm_;
  57. uint8_t labels_;
  58. uint32_t originalttl_;
  59. uint32_t timeexpire_;
  60. uint32_t timeinception_;
  61. uint16_t tag_;
  62. const Name signer_;
  63. const vector<uint8_t> signature_;
  64. };
  65. RRSIG::RRSIG(const string& rrsig_str) :
  66. impl_(NULL)
  67. {
  68. istringstream iss(rrsig_str);
  69. string covered_txt, signer_txt, expire_txt, inception_txt;
  70. unsigned int algorithm, labels;
  71. uint32_t originalttl;
  72. uint16_t tag;
  73. stringbuf signaturebuf;
  74. iss >> covered_txt >> algorithm >> labels >> originalttl
  75. >> expire_txt >> inception_txt >> tag >> signer_txt
  76. >> &signaturebuf;
  77. if (iss.bad() || iss.fail()) {
  78. isc_throw(InvalidRdataText, "Invalid RRSIG text");
  79. }
  80. if (algorithm > 0xff) {
  81. isc_throw(InvalidRdataText, "RRSIG algorithm out of range");
  82. }
  83. if (labels > 0xff) {
  84. isc_throw(InvalidRdataText, "RRSIG labels out of range");
  85. }
  86. const uint32_t timeexpire = timeFromText32(expire_txt);
  87. const uint32_t timeinception = timeFromText32(inception_txt);
  88. vector<uint8_t> signature;
  89. decodeBase64(signaturebuf.str(), signature);
  90. impl_ = new RRSIGImpl(RRType(covered_txt), algorithm, labels,
  91. originalttl, timeexpire, timeinception, tag,
  92. Name(signer_txt), signature);
  93. }
  94. RRSIG::RRSIG(InputBuffer& buffer, size_t rdata_len) {
  95. size_t pos = buffer.getPosition();
  96. if (rdata_len < RRSIG_MINIMUM_LEN) {
  97. isc_throw(InvalidRdataLength, "RRSIG too short");
  98. }
  99. RRType covered(buffer);
  100. uint8_t algorithm = buffer.readUint8();
  101. uint8_t labels = buffer.readUint8();
  102. uint32_t originalttl = buffer.readUint32();
  103. uint32_t timeexpire = buffer.readUint32();
  104. uint32_t timeinception = buffer.readUint32();
  105. uint16_t tag = buffer.readUint16();
  106. Name signer(buffer);
  107. // rdata_len must be sufficiently large to hold non empty signature data.
  108. if (rdata_len <= buffer.getPosition() - pos) {
  109. isc_throw(InvalidRdataLength, "RRSIG too short");
  110. }
  111. rdata_len -= (buffer.getPosition() - pos);
  112. vector<uint8_t> signature(rdata_len);
  113. buffer.readData(&signature[0], rdata_len);
  114. impl_ = new RRSIGImpl(covered, algorithm, labels,
  115. originalttl, timeexpire, timeinception, tag,
  116. signer, signature);
  117. }
  118. RRSIG::RRSIG(const RRSIG& source) :
  119. Rdata(), impl_(new RRSIGImpl(*source.impl_))
  120. {}
  121. RRSIG&
  122. RRSIG::operator=(const RRSIG& source) {
  123. if (impl_ == source.impl_) {
  124. return (*this);
  125. }
  126. RRSIGImpl* newimpl = new RRSIGImpl(*source.impl_);
  127. delete impl_;
  128. impl_ = newimpl;
  129. return (*this);
  130. }
  131. RRSIG::~RRSIG() {
  132. delete impl_;
  133. }
  134. string
  135. RRSIG::toText() const {
  136. return (impl_->covered_.toText() +
  137. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_))
  138. + " " + boost::lexical_cast<string>(static_cast<int>(impl_->labels_))
  139. + " " + boost::lexical_cast<string>(impl_->originalttl_)
  140. + " " + timeToText32(impl_->timeexpire_)
  141. + " " + timeToText32(impl_->timeinception_)
  142. + " " + boost::lexical_cast<string>(impl_->tag_)
  143. + " " + impl_->signer_.toText()
  144. + " " + encodeBase64(impl_->signature_));
  145. }
  146. void
  147. RRSIG::toWire(OutputBuffer& buffer) const {
  148. impl_->covered_.toWire(buffer);
  149. buffer.writeUint8(impl_->algorithm_);
  150. buffer.writeUint8(impl_->labels_);
  151. buffer.writeUint32(impl_->originalttl_);
  152. buffer.writeUint32(impl_->timeexpire_);
  153. buffer.writeUint32(impl_->timeinception_);
  154. buffer.writeUint16(impl_->tag_);
  155. impl_->signer_.toWire(buffer);
  156. buffer.writeData(&impl_->signature_[0], impl_->signature_.size());
  157. }
  158. void
  159. RRSIG::toWire(AbstractMessageRenderer& renderer) const {
  160. impl_->covered_.toWire(renderer);
  161. renderer.writeUint8(impl_->algorithm_);
  162. renderer.writeUint8(impl_->labels_);
  163. renderer.writeUint32(impl_->originalttl_);
  164. renderer.writeUint32(impl_->timeexpire_);
  165. renderer.writeUint32(impl_->timeinception_);
  166. renderer.writeUint16(impl_->tag_);
  167. renderer.writeName(impl_->signer_, false);
  168. renderer.writeData(&impl_->signature_[0], impl_->signature_.size());
  169. }
  170. int
  171. RRSIG::compare(const Rdata& other) const {
  172. const RRSIG& other_rrsig = dynamic_cast<const RRSIG&>(other);
  173. if (impl_->covered_.getCode() != other_rrsig.impl_->covered_.getCode()) {
  174. return (impl_->covered_.getCode() <
  175. other_rrsig.impl_->covered_.getCode() ? -1 : 1);
  176. }
  177. if (impl_->algorithm_ != other_rrsig.impl_->algorithm_) {
  178. return (impl_->algorithm_ < other_rrsig.impl_->algorithm_ ? -1 : 1);
  179. }
  180. if (impl_->labels_ != other_rrsig.impl_->labels_) {
  181. return (impl_->labels_ < other_rrsig.impl_->labels_ ? -1 : 1);
  182. }
  183. if (impl_->originalttl_ != other_rrsig.impl_->originalttl_) {
  184. return (impl_->originalttl_ < other_rrsig.impl_->originalttl_ ?
  185. -1 : 1);
  186. }
  187. if (impl_->timeexpire_ != other_rrsig.impl_->timeexpire_) {
  188. return (impl_->timeexpire_ < other_rrsig.impl_->timeexpire_ ?
  189. -1 : 1);
  190. }
  191. if (impl_->timeinception_ != other_rrsig.impl_->timeinception_) {
  192. return (impl_->timeinception_ < other_rrsig.impl_->timeinception_ ?
  193. -1 : 1);
  194. }
  195. if (impl_->tag_ != other_rrsig.impl_->tag_) {
  196. return (impl_->tag_ < other_rrsig.impl_->tag_ ? -1 : 1);
  197. }
  198. int cmp = compareNames(impl_->signer_, other_rrsig.impl_->signer_);
  199. if (cmp != 0) {
  200. return (cmp);
  201. }
  202. size_t this_len = impl_->signature_.size();
  203. size_t other_len = other_rrsig.impl_->signature_.size();
  204. size_t cmplen = min(this_len, other_len);
  205. cmp = memcmp(&impl_->signature_[0], &other_rrsig.impl_->signature_[0],
  206. cmplen);
  207. if (cmp != 0) {
  208. return (cmp);
  209. } else {
  210. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  211. }
  212. }
  213. const RRType&
  214. RRSIG::typeCovered() const {
  215. return (impl_->covered_);
  216. }
  217. // END_RDATA_NAMESPACE
  218. // END_ISC_NAMESPACE