rrsig_46.cc 11 KB

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