rrsig_46.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright (C) 2010-2013 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 <dns/rdata/generic/detail/lexer_util.h>
  30. #include <stdio.h>
  31. #include <time.h>
  32. using namespace std;
  33. using namespace isc::util;
  34. using namespace isc::util::encode;
  35. using isc::dns::rdata::generic::detail::createNameFromLexer;
  36. // BEGIN_ISC_NAMESPACE
  37. // BEGIN_RDATA_NAMESPACE
  38. namespace {
  39. // This is the minimum necessary length of all wire-format RRSIG RDATA:
  40. // - two 8-bit fields (algorithm and labels)
  41. // - two 16-bit fields (covered and tag)
  42. // - three 32-bit fields (original TTL, expire and inception)
  43. const size_t RRSIG_MINIMUM_LEN = 2 * sizeof(uint8_t) + 2 * sizeof(uint16_t) +
  44. 3 * sizeof(uint32_t);
  45. }
  46. struct RRSIGImpl {
  47. // straightforward representation of RRSIG RDATA fields
  48. RRSIGImpl(const RRType& covered, uint8_t algorithm, uint8_t labels,
  49. uint32_t originalttl, uint32_t timeexpire,
  50. uint32_t timeinception, uint16_t tag, const Name& signer,
  51. const vector<uint8_t>& signature) :
  52. covered_(covered), algorithm_(algorithm), labels_(labels),
  53. originalttl_(originalttl), timeexpire_(timeexpire),
  54. timeinception_(timeinception), tag_(tag), signer_(signer),
  55. signature_(signature)
  56. {}
  57. const RRType covered_;
  58. uint8_t algorithm_;
  59. uint8_t labels_;
  60. uint32_t originalttl_;
  61. uint32_t timeexpire_;
  62. uint32_t timeinception_;
  63. uint16_t tag_;
  64. const Name signer_;
  65. const vector<uint8_t> signature_;
  66. };
  67. // helper function for string and lexer constructors
  68. void
  69. RRSIG::createFromLexer(MasterLexer& lexer, const Name* origin) {
  70. const string covered_txt =
  71. lexer.getNextToken(MasterToken::STRING).getString();
  72. const uint32_t algorithm = lexer.getNextToken(MasterToken::NUMBER).
  73. getNumber();
  74. const uint32_t labels = lexer.getNextToken(MasterToken::NUMBER).
  75. getNumber();
  76. const uint32_t originalttl =
  77. RRTTL(lexer.getNextToken(MasterToken::STRING).getString()).getValue();
  78. const string expire_txt =
  79. lexer.getNextToken(MasterToken::STRING).getString();
  80. const string inception_txt =
  81. lexer.getNextToken(MasterToken::STRING).getString();
  82. const uint32_t tag =
  83. lexer.getNextToken(MasterToken::NUMBER).getNumber();
  84. const Name signer = createNameFromLexer(lexer, origin);
  85. const string signature_txt =
  86. lexer.getNextToken(MasterToken::STRING).getString();
  87. if (algorithm > 0xff) {
  88. isc_throw(InvalidRdataText, "RRSIG algorithm out of range");
  89. }
  90. if (labels > 0xff) {
  91. isc_throw(InvalidRdataText, "RRSIG labels out of range");
  92. }
  93. if (tag > 0xffff) {
  94. isc_throw(InvalidRdataText, "RRSIG key tag out of range");
  95. }
  96. const uint32_t timeexpire = timeFromText32(expire_txt);
  97. const uint32_t timeinception = timeFromText32(inception_txt);
  98. vector<uint8_t> signature;
  99. decodeBase64(signature_txt, signature);
  100. impl_ = new RRSIGImpl(RRType(covered_txt), algorithm, labels,
  101. originalttl, timeexpire, timeinception,
  102. static_cast<uint16_t>(tag), signer, signature);
  103. }
  104. /// \brief Constructor from string.
  105. ///
  106. /// The given string must represent a valid RRSIG RDATA. There can be extra
  107. /// space characters at the beginning or end of the text (which are simply
  108. /// ignored), but other extra text, including a new line, will make the
  109. /// construction fail with an exception.
  110. ///
  111. /// The Signer's Name must be absolute since there's no parameter that
  112. /// specifies the origin name; if this is not absolute, \c MissingNameOrigin
  113. /// exception will be thrown. This must not be represented as a quoted
  114. /// string.
  115. ///
  116. /// See the construction that takes \c MasterLexer for other fields.
  117. ///
  118. /// \throw Others Exception from the Name and RRTTL constructors.
  119. /// \throw InvalidRdataText Other general syntax errors.
  120. RRSIG::RRSIG(const std::string& rrsig_str) :
  121. impl_(NULL)
  122. {
  123. try {
  124. istringstream iss(rrsig_str);
  125. MasterLexer lexer;
  126. lexer.pushSource(iss);
  127. createFromLexer(lexer, NULL);
  128. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  129. isc_throw(InvalidRdataText, "extra input text for RRSIG: "
  130. << rrsig_str);
  131. }
  132. } catch (const MasterLexer::LexerError& ex) {
  133. isc_throw(InvalidRdataText, "Failed to construct RRSIG from '" <<
  134. rrsig_str << "': " << ex.what());
  135. }
  136. }
  137. /// \brief Constructor with a context of MasterLexer.
  138. ///
  139. /// The \c lexer should point to the beginning of valid textual representation
  140. /// of an RRSIG RDATA. The Signer's Name fields can be non absolute if \c
  141. /// origin is non NULL, in which case \c origin is used to make it absolute.
  142. /// This must not be represented as a quoted string.
  143. ///
  144. /// The Original TTL field can be either a valid decimal representation of an
  145. /// unsigned 32-bit integer or other valid textual representation of \c RRTTL
  146. /// such as "1H" (which means 3600).
  147. ///
  148. /// \throw MasterLexer::LexerError General parsing error such as missing field.
  149. /// \throw Other Exceptions from the Name and RRTTL constructors if
  150. /// construction of textual fields as these objects fail.
  151. ///
  152. /// \param lexer A \c MasterLexer object parsing a master file for the
  153. /// RDATA to be created
  154. /// \param origin If non NULL, specifies the origin of Signer's Name when
  155. /// it is non absolute.
  156. RRSIG::RRSIG(MasterLexer& lexer, const Name* origin,
  157. MasterLoader::Options, MasterLoaderCallbacks&) :
  158. impl_(NULL)
  159. {
  160. createFromLexer(lexer, origin);
  161. }
  162. RRSIG::RRSIG(InputBuffer& buffer, size_t rdata_len) {
  163. size_t pos = buffer.getPosition();
  164. if (rdata_len < RRSIG_MINIMUM_LEN) {
  165. isc_throw(InvalidRdataLength, "RRSIG too short");
  166. }
  167. RRType covered(buffer);
  168. uint8_t algorithm = buffer.readUint8();
  169. uint8_t labels = buffer.readUint8();
  170. uint32_t originalttl = buffer.readUint32();
  171. uint32_t timeexpire = buffer.readUint32();
  172. uint32_t timeinception = buffer.readUint32();
  173. uint16_t tag = buffer.readUint16();
  174. Name signer(buffer);
  175. // rdata_len must be sufficiently large to hold non empty signature data.
  176. if (rdata_len <= buffer.getPosition() - pos) {
  177. isc_throw(InvalidRdataLength, "RRSIG too short");
  178. }
  179. rdata_len -= (buffer.getPosition() - pos);
  180. vector<uint8_t> signature(rdata_len);
  181. buffer.readData(&signature[0], rdata_len);
  182. impl_ = new RRSIGImpl(covered, algorithm, labels,
  183. originalttl, timeexpire, timeinception, tag,
  184. signer, signature);
  185. }
  186. RRSIG::RRSIG(const RRSIG& source) :
  187. Rdata(), impl_(new RRSIGImpl(*source.impl_))
  188. {}
  189. RRSIG&
  190. RRSIG::operator=(const RRSIG& source) {
  191. if (impl_ == source.impl_) {
  192. return (*this);
  193. }
  194. RRSIGImpl* newimpl = new RRSIGImpl(*source.impl_);
  195. delete impl_;
  196. impl_ = newimpl;
  197. return (*this);
  198. }
  199. RRSIG::~RRSIG() {
  200. delete impl_;
  201. }
  202. string
  203. RRSIG::toText() const {
  204. return (impl_->covered_.toText() +
  205. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_))
  206. + " " + boost::lexical_cast<string>(static_cast<int>(impl_->labels_))
  207. + " " + boost::lexical_cast<string>(impl_->originalttl_)
  208. + " " + timeToText32(impl_->timeexpire_)
  209. + " " + timeToText32(impl_->timeinception_)
  210. + " " + boost::lexical_cast<string>(impl_->tag_)
  211. + " " + impl_->signer_.toText()
  212. + " " + encodeBase64(impl_->signature_));
  213. }
  214. void
  215. RRSIG::toWire(OutputBuffer& buffer) const {
  216. impl_->covered_.toWire(buffer);
  217. buffer.writeUint8(impl_->algorithm_);
  218. buffer.writeUint8(impl_->labels_);
  219. buffer.writeUint32(impl_->originalttl_);
  220. buffer.writeUint32(impl_->timeexpire_);
  221. buffer.writeUint32(impl_->timeinception_);
  222. buffer.writeUint16(impl_->tag_);
  223. impl_->signer_.toWire(buffer);
  224. buffer.writeData(&impl_->signature_[0], impl_->signature_.size());
  225. }
  226. void
  227. RRSIG::toWire(AbstractMessageRenderer& renderer) const {
  228. impl_->covered_.toWire(renderer);
  229. renderer.writeUint8(impl_->algorithm_);
  230. renderer.writeUint8(impl_->labels_);
  231. renderer.writeUint32(impl_->originalttl_);
  232. renderer.writeUint32(impl_->timeexpire_);
  233. renderer.writeUint32(impl_->timeinception_);
  234. renderer.writeUint16(impl_->tag_);
  235. renderer.writeName(impl_->signer_, false);
  236. renderer.writeData(&impl_->signature_[0], impl_->signature_.size());
  237. }
  238. int
  239. RRSIG::compare(const Rdata& other) const {
  240. const RRSIG& other_rrsig = dynamic_cast<const RRSIG&>(other);
  241. if (impl_->covered_.getCode() != other_rrsig.impl_->covered_.getCode()) {
  242. return (impl_->covered_.getCode() <
  243. other_rrsig.impl_->covered_.getCode() ? -1 : 1);
  244. }
  245. if (impl_->algorithm_ != other_rrsig.impl_->algorithm_) {
  246. return (impl_->algorithm_ < other_rrsig.impl_->algorithm_ ? -1 : 1);
  247. }
  248. if (impl_->labels_ != other_rrsig.impl_->labels_) {
  249. return (impl_->labels_ < other_rrsig.impl_->labels_ ? -1 : 1);
  250. }
  251. if (impl_->originalttl_ != other_rrsig.impl_->originalttl_) {
  252. return (impl_->originalttl_ < other_rrsig.impl_->originalttl_ ?
  253. -1 : 1);
  254. }
  255. if (impl_->timeexpire_ != other_rrsig.impl_->timeexpire_) {
  256. return (impl_->timeexpire_ < other_rrsig.impl_->timeexpire_ ?
  257. -1 : 1);
  258. }
  259. if (impl_->timeinception_ != other_rrsig.impl_->timeinception_) {
  260. return (impl_->timeinception_ < other_rrsig.impl_->timeinception_ ?
  261. -1 : 1);
  262. }
  263. if (impl_->tag_ != other_rrsig.impl_->tag_) {
  264. return (impl_->tag_ < other_rrsig.impl_->tag_ ? -1 : 1);
  265. }
  266. int cmp = compareNames(impl_->signer_, other_rrsig.impl_->signer_);
  267. if (cmp != 0) {
  268. return (cmp);
  269. }
  270. size_t this_len = impl_->signature_.size();
  271. size_t other_len = other_rrsig.impl_->signature_.size();
  272. size_t cmplen = min(this_len, other_len);
  273. cmp = memcmp(&impl_->signature_[0], &other_rrsig.impl_->signature_[0],
  274. cmplen);
  275. if (cmp != 0) {
  276. return (cmp);
  277. } else {
  278. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  279. }
  280. }
  281. const RRType&
  282. RRSIG::typeCovered() const {
  283. return (impl_->covered_);
  284. }
  285. // END_RDATA_NAMESPACE
  286. // END_ISC_NAMESPACE