rrsig_46.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "config.h"
  16. #include <string>
  17. #include <iomanip>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <vector>
  21. #include "base64.h"
  22. #include "buffer.h"
  23. #include "dnssectime.h"
  24. #include "messagerenderer.h"
  25. #include "name.h"
  26. #include "rrtype.h"
  27. #include "rrttl.h"
  28. #include "rdata.h"
  29. #include "rdataclass.h"
  30. #include <boost/lexical_cast.hpp>
  31. #include <stdio.h>
  32. #include <time.h>
  33. using namespace std;
  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. uint32_t timeexpire = timeFromText(expire_txt);
  87. uint32_t timeinception = timeFromText(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. {
  96. size_t pos = buffer.getPosition();
  97. if (rdata_len < RRSIG_MINIMUM_LEN) {
  98. isc_throw(InvalidRdataLength, "RRSIG too short");
  99. }
  100. RRType covered(buffer);
  101. uint8_t algorithm = buffer.readUint8();
  102. uint8_t labels = buffer.readUint8();
  103. uint32_t originalttl = buffer.readUint32();
  104. uint32_t timeexpire = buffer.readUint32();
  105. uint32_t timeinception = buffer.readUint32();
  106. uint16_t tag = buffer.readUint16();
  107. Name signer(buffer);
  108. // rdata_len must be sufficiently large to hold non empty signature data.
  109. if (rdata_len <= buffer.getPosition() - pos) {
  110. isc_throw(InvalidRdataLength, "RRSIG too short");
  111. }
  112. rdata_len -= (buffer.getPosition() - pos);
  113. vector<uint8_t> signature(rdata_len);
  114. buffer.readData(&signature[0], rdata_len);
  115. impl_ = new RRSIGImpl(covered, algorithm, labels,
  116. originalttl, timeexpire, timeinception, tag,
  117. signer, signature);
  118. }
  119. RRSIG::RRSIG(const RRSIG& source) :
  120. Rdata(), impl_(new RRSIGImpl(*source.impl_))
  121. {}
  122. RRSIG&
  123. RRSIG::operator=(const RRSIG& source)
  124. {
  125. if (impl_ == source.impl_) {
  126. return (*this);
  127. }
  128. RRSIGImpl* newimpl = new RRSIGImpl(*source.impl_);
  129. delete impl_;
  130. impl_ = newimpl;
  131. return (*this);
  132. }
  133. RRSIG::~RRSIG()
  134. {
  135. delete impl_;
  136. }
  137. string
  138. RRSIG::toText() const
  139. {
  140. string expire = timeToText(impl_->timeexpire_);
  141. string inception = timeToText(impl_->timeinception_);
  142. return (impl_->covered_.toText() +
  143. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_))
  144. + " " + boost::lexical_cast<string>(static_cast<int>(impl_->labels_))
  145. + " " + boost::lexical_cast<string>(impl_->originalttl_)
  146. + " " + expire
  147. + " " + inception
  148. + " " + boost::lexical_cast<string>(impl_->tag_)
  149. + " " + impl_->signer_.toText()
  150. + " " + encodeBase64(impl_->signature_));
  151. }
  152. void
  153. RRSIG::toWire(OutputBuffer& buffer) const
  154. {
  155. impl_->covered_.toWire(buffer);
  156. buffer.writeUint8(impl_->algorithm_);
  157. buffer.writeUint8(impl_->labels_);
  158. buffer.writeUint32(impl_->originalttl_);
  159. buffer.writeUint32(impl_->timeexpire_);
  160. buffer.writeUint32(impl_->timeinception_);
  161. buffer.writeUint16(impl_->tag_);
  162. impl_->signer_.toWire(buffer);
  163. buffer.writeData(&impl_->signature_[0], impl_->signature_.size());
  164. }
  165. void
  166. RRSIG::toWire(MessageRenderer& renderer) const
  167. {
  168. impl_->covered_.toWire(renderer);
  169. renderer.writeUint8(impl_->algorithm_);
  170. renderer.writeUint8(impl_->labels_);
  171. renderer.writeUint32(impl_->originalttl_);
  172. renderer.writeUint32(impl_->timeexpire_);
  173. renderer.writeUint32(impl_->timeinception_);
  174. renderer.writeUint16(impl_->tag_);
  175. renderer.writeName(impl_->signer_, false);
  176. renderer.writeData(&impl_->signature_[0], impl_->signature_.size());
  177. }
  178. int
  179. RRSIG::compare(const Rdata& other) const
  180. {
  181. const RRSIG& other_rrsig = dynamic_cast<const RRSIG&>(other);
  182. if (impl_->covered_.getCode() != other_rrsig.impl_->covered_.getCode()) {
  183. return (impl_->covered_.getCode() <
  184. other_rrsig.impl_->covered_.getCode() ? -1 : 1);
  185. }
  186. if (impl_->algorithm_ != other_rrsig.impl_->algorithm_) {
  187. return (impl_->algorithm_ < other_rrsig.impl_->algorithm_ ? -1 : 1);
  188. }
  189. if (impl_->labels_ != other_rrsig.impl_->labels_) {
  190. return (impl_->labels_ < other_rrsig.impl_->labels_ ? -1 : 1);
  191. }
  192. if (impl_->originalttl_ != other_rrsig.impl_->originalttl_) {
  193. return (impl_->originalttl_ < other_rrsig.impl_->originalttl_ ?
  194. -1 : 1);
  195. }
  196. if (impl_->timeexpire_ != other_rrsig.impl_->timeexpire_) {
  197. return (impl_->timeexpire_ < other_rrsig.impl_->timeexpire_ ?
  198. -1 : 1);
  199. }
  200. if (impl_->timeinception_ != other_rrsig.impl_->timeinception_) {
  201. return (impl_->timeinception_ < other_rrsig.impl_->timeinception_ ?
  202. -1 : 1);
  203. }
  204. if (impl_->tag_ != other_rrsig.impl_->tag_) {
  205. return (impl_->tag_ < other_rrsig.impl_->tag_ ? -1 : 1);
  206. }
  207. int cmp = compareNames(impl_->signer_, other_rrsig.impl_->signer_);
  208. if (cmp != 0) {
  209. return (cmp);
  210. }
  211. size_t this_len = impl_->signature_.size();
  212. size_t other_len = other_rrsig.impl_->signature_.size();
  213. size_t cmplen = min(this_len, other_len);
  214. cmp = memcmp(&impl_->signature_[0], &other_rrsig.impl_->signature_[0],
  215. cmplen);
  216. if (cmp != 0) {
  217. return (cmp);
  218. } else {
  219. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  220. }
  221. }
  222. // END_RDATA_NAMESPACE
  223. // END_ISC_NAMESPACE