dnskey_48.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <iostream>
  15. #include <string>
  16. #include <sstream>
  17. #include <vector>
  18. #include <boost/lexical_cast.hpp>
  19. #include <boost/foreach.hpp>
  20. #include <util/encode/base64.h>
  21. #include <util/buffer.h>
  22. #include <dns/messagerenderer.h>
  23. #include <dns/name.h>
  24. #include <dns/rdata.h>
  25. #include <dns/rdataclass.h>
  26. #include <memory>
  27. #include <stdio.h>
  28. #include <time.h>
  29. using namespace std;
  30. using namespace isc::util;
  31. using namespace isc::util::encode;
  32. // BEGIN_ISC_NAMESPACE
  33. // BEGIN_RDATA_NAMESPACE
  34. struct DNSKEYImpl {
  35. // straightforward representation of DNSKEY RDATA fields
  36. DNSKEYImpl(uint16_t flags, uint8_t protocol, uint8_t algorithm,
  37. const vector<uint8_t>& keydata) :
  38. flags_(flags), protocol_(protocol), algorithm_(algorithm),
  39. keydata_(keydata)
  40. {}
  41. uint16_t flags_;
  42. uint8_t protocol_;
  43. uint8_t algorithm_;
  44. const vector<uint8_t> keydata_;
  45. };
  46. /// \brief Constructor from string.
  47. ///
  48. /// The given string must represent a valid DNSKEY RDATA. There can be
  49. /// extra space characters at the beginning or end of the text (which
  50. /// are simply ignored), but other extra text, including a new line,
  51. /// will make the construction fail with an exception.
  52. ///
  53. /// The Protocol and Algorithm fields must be within their valid
  54. /// ranges. The Public Key field must be present and must contain a
  55. /// Base64 encoding of the public key. Whitespace is allowed within the
  56. /// Base64 text.
  57. ///
  58. /// \throw InvalidRdataText if any fields are out of their valid range,
  59. /// or are incorrect.
  60. ///
  61. /// \param dnskey_str A string containing the RDATA to be created
  62. DNSKEY::DNSKEY(const std::string& dnskey_str) :
  63. impl_(NULL)
  64. {
  65. std::auto_ptr<DNSKEYImpl> impl_ptr(NULL);
  66. try {
  67. std::istringstream ss(dnskey_str);
  68. MasterLexer lexer;
  69. lexer.pushSource(ss);
  70. impl_ptr.reset(constructFromLexer(lexer));
  71. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  72. isc_throw(InvalidRdataText,
  73. "Extra input text for DNSKEY: " << dnskey_str);
  74. }
  75. } catch (const MasterLexer::LexerError& ex) {
  76. isc_throw(InvalidRdataText,
  77. "Failed to construct DNSKEY from '" << dnskey_str << "': "
  78. << ex.what());
  79. }
  80. impl_ = impl_ptr.release();
  81. }
  82. DNSKEY::DNSKEY(InputBuffer& buffer, size_t rdata_len) :
  83. impl_(NULL)
  84. {
  85. if (rdata_len < 4) {
  86. isc_throw(InvalidRdataLength, "DNSKEY too short: " << rdata_len);
  87. }
  88. const uint16_t flags = buffer.readUint16();
  89. const uint16_t protocol = buffer.readUint8();
  90. const uint16_t algorithm = buffer.readUint8();
  91. rdata_len -= 4;
  92. vector<uint8_t> keydata;
  93. // If key data is missing, it's OK. BIND 9 seems to accept such
  94. // cases. What we should do could be debatable, but since this field
  95. // is algorithm dependent and our implementation doesn't reject
  96. // unknown algorithms, we are lenient here.
  97. if (rdata_len > 0) {
  98. keydata.resize(rdata_len);
  99. buffer.readData(&keydata[0], rdata_len);
  100. }
  101. impl_ = new DNSKEYImpl(flags, protocol, algorithm, keydata);
  102. }
  103. /// \brief Constructor with a context of MasterLexer.
  104. ///
  105. /// The \c lexer should point to the beginning of valid textual
  106. /// representation of an DNSKEY RDATA.
  107. ///
  108. /// See \c DNSKEY::DNSKEY(const std::string&) for description of the
  109. /// expected RDATA fields.
  110. ///
  111. /// \throw MasterLexer::LexerError General parsing error such as
  112. /// missing field.
  113. /// \throw InvalidRdataText if any fields are out of their valid range,
  114. /// or are incorrect.
  115. ///
  116. /// \param lexer A \c MasterLexer object parsing a master file for the
  117. /// RDATA to be created
  118. DNSKEY::DNSKEY(MasterLexer& lexer, const Name*,
  119. MasterLoader::Options, MasterLoaderCallbacks&) :
  120. impl_(NULL)
  121. {
  122. impl_ = constructFromLexer(lexer);
  123. }
  124. DNSKEYImpl*
  125. DNSKEY::constructFromLexer(MasterLexer& lexer) {
  126. const uint32_t flags = lexer.getNextToken(MasterToken::NUMBER).getNumber();
  127. if (flags > 0xffff) {
  128. isc_throw(InvalidRdataText,
  129. "DNSKEY flags out of range: " << flags);
  130. }
  131. const uint32_t protocol =
  132. lexer.getNextToken(MasterToken::NUMBER).getNumber();
  133. if (protocol > 0xff) {
  134. isc_throw(InvalidRdataText,
  135. "DNSKEY protocol out of range: " << protocol);
  136. }
  137. const uint32_t algorithm =
  138. lexer.getNextToken(MasterToken::NUMBER).getNumber();
  139. if (algorithm > 0xff) {
  140. isc_throw(InvalidRdataText,
  141. "DNSKEY algorithm out of range: " << algorithm);
  142. }
  143. std::string keydata_str;
  144. std::string keydata_substr;
  145. while (true) {
  146. const MasterToken& token =
  147. lexer.getNextToken(MasterToken::STRING, true);
  148. if ((token.getType() == MasterToken::END_OF_FILE) ||
  149. (token.getType() == MasterToken::END_OF_LINE)) {
  150. break;
  151. }
  152. // token is now assured to be of type STRING.
  153. assert(token.getType() == MasterToken::STRING);
  154. token.getString(keydata_substr);
  155. keydata_str.append(keydata_substr);
  156. }
  157. lexer.ungetToken();
  158. vector<uint8_t> keydata;
  159. // If key data is missing, it's OK. BIND 9 seems to accept such
  160. // cases. What we should do could be debatable, but since this field
  161. // is algorithm dependent and our implementation doesn't reject
  162. // unknown algorithms, we are lenient here.
  163. if (keydata_str.size() > 0) {
  164. decodeBase64(keydata_str, keydata);
  165. }
  166. return (new DNSKEYImpl(flags, protocol, algorithm, keydata));
  167. }
  168. DNSKEY::DNSKEY(const DNSKEY& source) :
  169. Rdata(), impl_(new DNSKEYImpl(*source.impl_))
  170. {}
  171. DNSKEY&
  172. DNSKEY::operator=(const DNSKEY& source) {
  173. if (impl_ == source.impl_) {
  174. return (*this);
  175. }
  176. DNSKEYImpl* newimpl = new DNSKEYImpl(*source.impl_);
  177. delete impl_;
  178. impl_ = newimpl;
  179. return (*this);
  180. }
  181. DNSKEY::~DNSKEY() {
  182. delete impl_;
  183. }
  184. string
  185. DNSKEY::toText() const {
  186. return (boost::lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  187. " " + boost::lexical_cast<string>(static_cast<int>(impl_->protocol_)) +
  188. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_)) +
  189. " " + encodeBase64(impl_->keydata_));
  190. }
  191. void
  192. DNSKEY::toWire(OutputBuffer& buffer) const {
  193. buffer.writeUint16(impl_->flags_);
  194. buffer.writeUint8(impl_->protocol_);
  195. buffer.writeUint8(impl_->algorithm_);
  196. buffer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  197. }
  198. void
  199. DNSKEY::toWire(AbstractMessageRenderer& renderer) const {
  200. renderer.writeUint16(impl_->flags_);
  201. renderer.writeUint8(impl_->protocol_);
  202. renderer.writeUint8(impl_->algorithm_);
  203. renderer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  204. }
  205. int
  206. DNSKEY::compare(const Rdata& other) const {
  207. const DNSKEY& other_dnskey = dynamic_cast<const DNSKEY&>(other);
  208. if (impl_->flags_ != other_dnskey.impl_->flags_) {
  209. return (impl_->flags_ < other_dnskey.impl_->flags_ ? -1 : 1);
  210. }
  211. if (impl_->protocol_ != other_dnskey.impl_->protocol_) {
  212. return (impl_->protocol_ < other_dnskey.impl_->protocol_ ? -1 : 1);
  213. }
  214. if (impl_->algorithm_ != other_dnskey.impl_->algorithm_) {
  215. return (impl_->algorithm_ < other_dnskey.impl_->algorithm_ ? -1 : 1);
  216. }
  217. const size_t this_len = impl_->keydata_.size();
  218. const size_t other_len = other_dnskey.impl_->keydata_.size();
  219. const size_t cmplen = min(this_len, other_len);
  220. const int cmp = memcmp(&impl_->keydata_[0],
  221. &other_dnskey.impl_->keydata_[0], cmplen);
  222. if (cmp != 0) {
  223. return (cmp);
  224. } else {
  225. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  226. }
  227. }
  228. uint16_t
  229. DNSKEY::getTag() const {
  230. if (impl_->algorithm_ == 1) {
  231. const int len = impl_->keydata_.size();
  232. // See RFC 4034 appendix B.1 for why the key data must contain
  233. // at least 4 bytes with RSA/MD5: 3 trailing bytes to extract
  234. // the tag from, and 1 byte of exponent length subfield before
  235. // modulus.
  236. if (len < 4) {
  237. isc_throw(isc::OutOfRange,
  238. "DNSKEY keydata too short for tag extraction");
  239. }
  240. return ((impl_->keydata_[len - 3] << 8) + impl_->keydata_[len - 2]);
  241. }
  242. uint32_t ac = impl_->flags_;
  243. ac += (impl_->protocol_ << 8);
  244. ac += impl_->algorithm_;
  245. const size_t size = impl_->keydata_.size();
  246. for (size_t i = 0; i < size; i ++) {
  247. ac += (i & 1) ? impl_->keydata_[i] : (impl_->keydata_[i] << 8);
  248. }
  249. ac += (ac >> 16) & 0xffff;
  250. return (ac & 0xffff);
  251. }
  252. uint16_t
  253. DNSKEY::getFlags() const {
  254. return (impl_->flags_);
  255. }
  256. uint8_t
  257. DNSKEY::getAlgorithm() const {
  258. return (impl_->algorithm_);
  259. }
  260. // END_RDATA_NAMESPACE
  261. // END_ISC_NAMESPACE