rrsig_46.cc 11 KB

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