tsig_250.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 <sstream>
  16. #include <vector>
  17. #include <boost/lexical_cast.hpp>
  18. #include <util/buffer.h>
  19. #include <util/strutil.h>
  20. #include <util/encode/base64.h>
  21. #include <dns/messagerenderer.h>
  22. #include <dns/name.h>
  23. #include <dns/rdata.h>
  24. #include <dns/rdataclass.h>
  25. #include <dns/tsigerror.h>
  26. using namespace std;
  27. using namespace boost;
  28. using namespace isc::util;
  29. using namespace isc::util::encode;
  30. using namespace isc::util::str;
  31. // BEGIN_ISC_NAMESPACE
  32. // BEGIN_RDATA_NAMESPACE
  33. /// This is a straightforward representation of TSIG RDATA fields.
  34. struct TSIG::TSIGImpl {
  35. TSIGImpl(const Name& algorithm, uint64_t time_signed, uint16_t fudge,
  36. vector<uint8_t>& mac, uint16_t original_id, uint16_t error,
  37. vector<uint8_t>& other_data) :
  38. algorithm_(algorithm), time_signed_(time_signed), fudge_(fudge),
  39. mac_(mac), original_id_(original_id), error_(error),
  40. other_data_(other_data)
  41. {}
  42. TSIGImpl(const Name& algorithm, uint64_t time_signed, uint16_t fudge,
  43. size_t macsize, const void* mac, uint16_t original_id,
  44. uint16_t error, size_t other_len, const void* other_data) :
  45. algorithm_(algorithm), time_signed_(time_signed), fudge_(fudge),
  46. mac_(static_cast<const uint8_t*>(mac),
  47. static_cast<const uint8_t*>(mac) + macsize),
  48. original_id_(original_id), error_(error),
  49. other_data_(static_cast<const uint8_t*>(other_data),
  50. static_cast<const uint8_t*>(other_data) + other_len)
  51. {}
  52. template <typename Output>
  53. void toWireCommon(Output& output) const;
  54. const Name algorithm_;
  55. const uint64_t time_signed_;
  56. const uint16_t fudge_;
  57. const vector<uint8_t> mac_;
  58. const uint16_t original_id_;
  59. const uint16_t error_;
  60. const vector<uint8_t> other_data_;
  61. };
  62. /// \brief Constructor from string.
  63. ///
  64. /// \c tsig_str must be formatted as follows:
  65. /// \code <Alg> <Time> <Fudge> <MACsize> [<MAC>] <OrigID> <Error> <OtherLen> [<OtherData>]
  66. /// \endcode
  67. /// where
  68. /// - &lt;Alg&gt; is a valid textual representation of domain name.
  69. /// - &lt;Time&gt; is an unsigned 48-bit decimal integer.
  70. /// - &lt;MACSize&gt;, &lt;OrigID&gt;, and &lt;OtherLen&gt; are an unsigned
  71. /// 16-bit decimal
  72. /// integer.
  73. /// - &lt;Error&gt; is an unsigned 16-bit decimal integer or a valid mnemonic
  74. /// for the Error field specified in RFC2845. Currently, "BADSIG", "BADKEY",
  75. /// and "BADTIME" are supported (case sensitive). In future versions
  76. /// other representations that are compatible with the DNS RCODE will be
  77. /// supported.
  78. /// - &lt;MAC&gt; and &lt;OtherData&gt; is a BASE-64 encoded string that does
  79. /// not contain space characters.
  80. /// When &lt;MACSize&gt; and &lt;OtherLen&gt; is 0, &lt;MAC&gt; and
  81. /// &lt;OtherData&gt; must not appear in \c tsig_str, respectively.
  82. /// - The decoded data of &lt;MAC&gt; is &lt;MACSize&gt; bytes of binary
  83. /// stream.
  84. /// - The decoded data of &lt;OtherData&gt; is &lt;OtherLen&gt; bytes of
  85. /// binary stream.
  86. ///
  87. /// An example of valid string is:
  88. /// \code "hmac-sha256. 853804800 300 3 AAAA 2845 0 0" \endcode
  89. /// In this example &lt;OtherData&gt; is missing because &lt;OtherLen&gt; is 0.
  90. ///
  91. /// Note that RFC2845 does not define the standard presentation format
  92. /// of %TSIG RR, so the above syntax is implementation specific.
  93. /// This is, however, compatible with the format acceptable to BIND 9's
  94. /// RDATA parser.
  95. ///
  96. /// <b>Exceptions</b>
  97. ///
  98. /// If &lt;Alg&gt; is not a valid domain name, a corresponding exception from
  99. /// the \c Name class will be thrown;
  100. /// if &lt;MAC&gt; or &lt;OtherData&gt; is not validly encoded in BASE-64, an
  101. /// exception of class \c isc::BadValue will be thrown;
  102. /// if %any of the other bullet points above is not met, an exception of
  103. /// class \c InvalidRdataText will be thrown.
  104. /// This constructor internally involves resource allocation, and if it fails
  105. /// a corresponding standard exception will be thrown.
  106. TSIG::TSIG(const std::string& tsig_str) : impl_(NULL) {
  107. istringstream iss(tsig_str);
  108. try {
  109. const Name algorithm(getToken(iss));
  110. const int64_t time_signed = tokenToNum<int64_t, 48>(getToken(iss));
  111. const int32_t fudge = tokenToNum<int32_t, 16>(getToken(iss));
  112. const int32_t macsize = tokenToNum<int32_t, 16>(getToken(iss));
  113. const string mac_txt = (macsize > 0) ? getToken(iss) : "";
  114. vector<uint8_t> mac;
  115. decodeBase64(mac_txt, mac);
  116. if (mac.size() != macsize) {
  117. isc_throw(InvalidRdataText, "TSIG MAC size and data are inconsistent");
  118. }
  119. const int32_t orig_id = tokenToNum<int32_t, 16>(getToken(iss));
  120. const string error_txt = getToken(iss);
  121. int32_t error = 0;
  122. // XXX: In the initial implementation we hardcode the mnemonics.
  123. // We'll soon generalize this.
  124. if (error_txt == "BADSIG") {
  125. error = 16;
  126. } else if (error_txt == "BADKEY") {
  127. error = 17;
  128. } else if (error_txt == "BADTIME") {
  129. error = 18;
  130. } else {
  131. error = tokenToNum<int32_t, 16>(error_txt);
  132. }
  133. const int32_t otherlen = tokenToNum<int32_t, 16>(getToken(iss));
  134. const string otherdata_txt = (otherlen > 0) ? getToken(iss) : "";
  135. vector<uint8_t> other_data;
  136. decodeBase64(otherdata_txt, other_data);
  137. if (!iss.eof()) {
  138. isc_throw(InvalidRdataText, "Unexpected input for TSIG RDATA: " <<
  139. tsig_str);
  140. }
  141. impl_ = new TSIGImpl(algorithm, time_signed, fudge, mac, orig_id,
  142. error, other_data);
  143. } catch (const StringTokenError& ste) {
  144. isc_throw(InvalidRdataText, "Invalid TSIG text: " << ste.what() <<
  145. ": " << tsig_str);
  146. }
  147. }
  148. /// \brief Constructor from wire-format data.
  149. ///
  150. /// When a read operation on \c buffer fails (e.g., due to a corrupted
  151. /// message) a corresponding exception from the \c InputBuffer class will
  152. /// be thrown.
  153. /// If the wire-format data does not begin with a valid domain name,
  154. /// a corresponding exception from the \c Name class will be thrown.
  155. /// In addition, this constructor internally involves resource allocation,
  156. /// and if it fails a corresponding standard exception will be thrown.
  157. ///
  158. /// According to RFC3597, the Algorithm field must be a non compressed form
  159. /// of domain name. But this implementation accepts a %TSIG RR even if that
  160. /// field is compressed.
  161. ///
  162. /// \param buffer A buffer storing the wire format data.
  163. /// \param rdata_len The length of the RDATA in bytes, normally expected
  164. /// to be the value of the RDLENGTH field of the corresponding RR.
  165. /// But this constructor does not use this parameter; if necessary, the caller
  166. /// must check consistency between the length parameter and the actual
  167. /// RDATA length.
  168. TSIG::TSIG(InputBuffer& buffer, size_t) : impl_(NULL) {
  169. Name algorithm(buffer);
  170. uint8_t time_signed_buf[6];
  171. buffer.readData(time_signed_buf, sizeof(time_signed_buf));
  172. const uint64_t time_signed =
  173. (static_cast<uint64_t>(time_signed_buf[0]) << 40 |
  174. static_cast<uint64_t>(time_signed_buf[1]) << 32 |
  175. static_cast<uint64_t>(time_signed_buf[2]) << 24 |
  176. static_cast<uint64_t>(time_signed_buf[3]) << 16 |
  177. static_cast<uint64_t>(time_signed_buf[4]) << 8 |
  178. static_cast<uint64_t>(time_signed_buf[5]));
  179. const uint16_t fudge = buffer.readUint16();
  180. const uint16_t mac_size = buffer.readUint16();
  181. vector<uint8_t> mac(mac_size);
  182. if (mac_size > 0) {
  183. buffer.readData(&mac[0], mac_size);
  184. }
  185. const uint16_t original_id = buffer.readUint16();
  186. const uint16_t error = buffer.readUint16();
  187. const uint16_t other_len = buffer.readUint16();
  188. vector<uint8_t> other_data(other_len);
  189. if (other_len > 0) {
  190. buffer.readData(&other_data[0], other_len);
  191. }
  192. impl_ = new TSIGImpl(algorithm, time_signed, fudge, mac, original_id,
  193. error, other_data);
  194. }
  195. TSIG::TSIG(const Name& algorithm, uint64_t time_signed, uint16_t fudge,
  196. uint16_t mac_size, const void* mac, uint16_t original_id,
  197. uint16_t error, uint16_t other_len, const void* other_data) :
  198. impl_(NULL)
  199. {
  200. // Time Signed is a 48-bit value.
  201. if ((time_signed >> 48) != 0) {
  202. isc_throw(OutOfRange, "TSIG Time Signed is too large: " <<
  203. time_signed);
  204. }
  205. if ((mac_size == 0 && mac != NULL) || (mac_size > 0 && mac == NULL)) {
  206. isc_throw(InvalidParameter, "TSIG MAC size and data inconsistent");
  207. }
  208. if ((other_len == 0 && other_data != NULL) ||
  209. (other_len > 0 && other_data == NULL)) {
  210. isc_throw(InvalidParameter,
  211. "TSIG Other data length and data inconsistent");
  212. }
  213. impl_ = new TSIGImpl(algorithm, time_signed, fudge, mac_size, mac,
  214. original_id, error, other_len, other_data);
  215. }
  216. /// \brief The copy constructor.
  217. ///
  218. /// It internally allocates a resource, and if it fails a corresponding
  219. /// standard exception will be thrown.
  220. /// This constructor never throws an exception otherwise.
  221. TSIG::TSIG(const TSIG& source) : Rdata(), impl_(new TSIGImpl(*source.impl_))
  222. {}
  223. TSIG&
  224. TSIG::operator=(const TSIG& source) {
  225. if (impl_ == source.impl_) {
  226. return (*this);
  227. }
  228. TSIGImpl* newimpl = new TSIGImpl(*source.impl_);
  229. delete impl_;
  230. impl_ = newimpl;
  231. return (*this);
  232. }
  233. TSIG::~TSIG() {
  234. delete impl_;
  235. }
  236. /// \brief Convert the \c TSIG to a string.
  237. ///
  238. /// The output of this method is formatted as described in the "from string"
  239. /// constructor (\c TSIG(const std::string&))).
  240. ///
  241. /// If internal resource allocation fails, a corresponding
  242. /// standard exception will be thrown.
  243. ///
  244. /// \return A \c string object that represents the \c TSIG object.
  245. std::string
  246. TSIG::toText() const {
  247. string result;
  248. result += impl_->algorithm_.toText() + " " +
  249. lexical_cast<string>(impl_->time_signed_) + " " +
  250. lexical_cast<string>(impl_->fudge_) + " " +
  251. lexical_cast<string>(impl_->mac_.size()) + " ";
  252. if (impl_->mac_.size() > 0) {
  253. result += encodeBase64(impl_->mac_) + " ";
  254. }
  255. result += lexical_cast<string>(impl_->original_id_) + " ";
  256. result += TSIGError(impl_->error_).toText() + " ";
  257. result += lexical_cast<string>(impl_->other_data_.size());
  258. if (impl_->other_data_.size() > 0) {
  259. result += " " + encodeBase64(impl_->other_data_);
  260. }
  261. return (result);
  262. }
  263. // Common sequence of toWire() operations used for the two versions of
  264. // toWire().
  265. template <typename Output>
  266. void
  267. TSIG::TSIGImpl::toWireCommon(Output& output) const {
  268. output.writeUint16(time_signed_ >> 32);
  269. output.writeUint32(time_signed_ & 0xffffffff);
  270. output.writeUint16(fudge_);
  271. const uint16_t mac_size = mac_.size();
  272. output.writeUint16(mac_size);
  273. if (mac_size > 0) {
  274. output.writeData(&mac_[0], mac_size);
  275. }
  276. output.writeUint16(original_id_);
  277. output.writeUint16(error_);
  278. const uint16_t other_len = other_data_.size();
  279. output.writeUint16(other_len);
  280. if (other_len > 0) {
  281. output.writeData(&other_data_[0], other_len);
  282. }
  283. }
  284. /// \brief Render the \c TSIG in the wire format without name compression.
  285. ///
  286. /// If internal resource allocation fails, a corresponding
  287. /// standard exception will be thrown.
  288. /// This method never throws an exception otherwise.
  289. ///
  290. /// \param buffer An output buffer to store the wire data.
  291. void
  292. TSIG::toWire(OutputBuffer& buffer) const {
  293. impl_->algorithm_.toWire(buffer);
  294. impl_->toWireCommon<OutputBuffer>(buffer);
  295. }
  296. /// \brief Render the \c TSIG in the wire format with taking into account
  297. /// compression.
  298. ///
  299. /// As specified in RFC3597, the Algorithm field (a domain name) will not
  300. /// be compressed. However, the domain name could be a target of compression
  301. /// of other compressible names (though pretty unlikely), the offset
  302. /// information of the algorithm name may be recorded in \c renderer.
  303. ///
  304. /// If internal resource allocation fails, a corresponding
  305. /// standard exception will be thrown.
  306. /// This method never throws an exception otherwise.
  307. ///
  308. /// \param renderer DNS message rendering context that encapsulates the
  309. /// output buffer and name compression information.
  310. void
  311. TSIG::toWire(AbstractMessageRenderer& renderer) const {
  312. renderer.writeName(impl_->algorithm_, false);
  313. impl_->toWireCommon<AbstractMessageRenderer>(renderer);
  314. }
  315. // A helper function commonly used for TSIG::compare().
  316. int
  317. vectorComp(const vector<uint8_t>& v1, const vector<uint8_t>& v2) {
  318. const size_t this_size = v1.size();
  319. const size_t other_size = v2.size();
  320. if (this_size != other_size) {
  321. return (this_size < other_size ? -1 : 1);
  322. }
  323. if (this_size > 0) {
  324. return (memcmp(&v1[0], &v2[0], this_size));
  325. }
  326. return (0);
  327. }
  328. /// \brief Compare two instances of \c TSIG RDATA.
  329. ///
  330. /// This method compares \c this and the \c other \c TSIG objects
  331. /// in terms of the DNSSEC sorting order as defined in RFC4034, and returns
  332. /// the result as an integer.
  333. ///
  334. /// This method is expected to be used in a polymorphic way, and the
  335. /// parameter to compare against is therefore of the abstract \c Rdata class.
  336. /// However, comparing two \c Rdata objects of different RR types
  337. /// is meaningless, and \c other must point to a \c TSIG object;
  338. /// otherwise, the standard \c bad_cast exception will be thrown.
  339. /// This method never throws an exception otherwise.
  340. ///
  341. /// \param other the right-hand operand to compare against.
  342. /// \return < 0 if \c this would be sorted before \c other.
  343. /// \return 0 if \c this is identical to \c other in terms of sorting order.
  344. /// \return > 0 if \c this would be sorted after \c other.
  345. int
  346. TSIG::compare(const Rdata& other) const {
  347. const TSIG& other_tsig = dynamic_cast<const TSIG&>(other);
  348. const int ncmp = compareNames(impl_->algorithm_,
  349. other_tsig.impl_->algorithm_);
  350. if (ncmp != 0) {
  351. return (ncmp);
  352. }
  353. if (impl_->time_signed_ != other_tsig.impl_->time_signed_) {
  354. return (impl_->time_signed_ < other_tsig.impl_->time_signed_ ? -1 : 1);
  355. }
  356. if (impl_->fudge_ != other_tsig.impl_->fudge_) {
  357. return (impl_->fudge_ < other_tsig.impl_->fudge_ ? -1 : 1);
  358. }
  359. const int vcmp = vectorComp(impl_->mac_, other_tsig.impl_->mac_);
  360. if (vcmp != 0) {
  361. return (vcmp);
  362. }
  363. if (impl_->original_id_ != other_tsig.impl_->original_id_) {
  364. return (impl_->original_id_ < other_tsig.impl_->original_id_ ? -1 : 1);
  365. }
  366. if (impl_->error_ != other_tsig.impl_->error_) {
  367. return (impl_->error_ < other_tsig.impl_->error_ ? -1 : 1);
  368. }
  369. return (vectorComp(impl_->other_data_, other_tsig.impl_->other_data_));
  370. }
  371. const Name&
  372. TSIG::getAlgorithm() const {
  373. return (impl_->algorithm_);
  374. }
  375. uint64_t
  376. TSIG::getTimeSigned() const {
  377. return (impl_->time_signed_);
  378. }
  379. uint16_t
  380. TSIG::getFudge() const {
  381. return (impl_->fudge_);
  382. }
  383. uint16_t
  384. TSIG::getMACSize() const {
  385. return (impl_->mac_.size());
  386. }
  387. const void*
  388. TSIG::getMAC() const {
  389. if (impl_->mac_.size() > 0) {
  390. return (&impl_->mac_[0]);
  391. } else {
  392. return (NULL);
  393. }
  394. }
  395. uint16_t
  396. TSIG::getOriginalID() const {
  397. return (impl_->original_id_);
  398. }
  399. uint16_t
  400. TSIG::getError() const {
  401. return (impl_->error_);
  402. }
  403. uint16_t
  404. TSIG::getOtherLen() const {
  405. return (impl_->other_data_.size());
  406. }
  407. const void*
  408. TSIG::getOtherData() const {
  409. if (impl_->other_data_.size() > 0) {
  410. return (&impl_->other_data_[0]);
  411. } else {
  412. return (NULL);
  413. }
  414. }
  415. // END_RDATA_NAMESPACE
  416. // END_ISC_NAMESPACE