dnskey_48.cc 9.2 KB

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