dnskey_48.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /// It is okay for the key data to be missing. Note: BIND 9 also accepts
  59. /// DNSKEY missing key data. While the RFC is silent in this case, and it
  60. /// may be debatable what an implementation should do, but since this field
  61. /// is algorithm dependent and this implementations doesn't reject unknown
  62. /// algorithms, it's lenient here.
  63. ///
  64. /// \throw InvalidRdataText if any fields are out of their valid range,
  65. /// or are incorrect.
  66. ///
  67. /// \param dnskey_str A string containing the RDATA to be created
  68. DNSKEY::DNSKEY(const std::string& dnskey_str) :
  69. impl_(NULL)
  70. {
  71. // We use auto_ptr here because if there is an exception in this
  72. // constructor, the destructor is not called and there could be a
  73. // leak of the DNSKEYImpl that constructFromLexer() returns.
  74. std::auto_ptr<DNSKEYImpl> impl_ptr(NULL);
  75. try {
  76. std::istringstream ss(dnskey_str);
  77. MasterLexer lexer;
  78. lexer.pushSource(ss);
  79. impl_ptr.reset(constructFromLexer(lexer));
  80. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  81. isc_throw(InvalidRdataText,
  82. "Extra input text for DNSKEY: " << dnskey_str);
  83. }
  84. } catch (const MasterLexer::LexerError& ex) {
  85. isc_throw(InvalidRdataText,
  86. "Failed to construct DNSKEY from '" << dnskey_str << "': "
  87. << ex.what());
  88. }
  89. impl_ = impl_ptr.release();
  90. }
  91. /// \brief Constructor from InputBuffer.
  92. ///
  93. /// The passed buffer must contain a valid DNSKEY RDATA.
  94. ///
  95. /// The Protocol and Algorithm fields are not checked for unknown
  96. /// values. It is okay for the key data to be missing (see the description
  97. /// of the constructor from string).
  98. DNSKEY::DNSKEY(InputBuffer& buffer, size_t rdata_len) :
  99. impl_(NULL)
  100. {
  101. if (rdata_len < 4) {
  102. isc_throw(InvalidRdataLength, "DNSKEY too short: " << rdata_len);
  103. }
  104. const uint16_t flags = buffer.readUint16();
  105. const uint16_t protocol = buffer.readUint8();
  106. const uint16_t algorithm = buffer.readUint8();
  107. rdata_len -= 4;
  108. vector<uint8_t> keydata;
  109. // If key data is missing, it's OK. See the API documentation of the
  110. // constructor.
  111. if (rdata_len > 0) {
  112. keydata.resize(rdata_len);
  113. buffer.readData(&keydata[0], rdata_len);
  114. }
  115. impl_ = new DNSKEYImpl(flags, protocol, algorithm, keydata);
  116. }
  117. /// \brief Constructor with a context of MasterLexer.
  118. ///
  119. /// The \c lexer should point to the beginning of valid textual
  120. /// representation of an DNSKEY RDATA.
  121. ///
  122. /// See \c DNSKEY::DNSKEY(const std::string&) for description of the
  123. /// expected RDATA fields.
  124. ///
  125. /// \throw MasterLexer::LexerError General parsing error such as
  126. /// missing field.
  127. /// \throw InvalidRdataText if any fields are out of their valid range,
  128. /// or are incorrect.
  129. ///
  130. /// \param lexer A \c MasterLexer object parsing a master file for the
  131. /// RDATA to be created
  132. DNSKEY::DNSKEY(MasterLexer& lexer, const Name*,
  133. MasterLoader::Options, MasterLoaderCallbacks&) :
  134. impl_(NULL)
  135. {
  136. impl_ = constructFromLexer(lexer);
  137. }
  138. DNSKEYImpl*
  139. DNSKEY::constructFromLexer(MasterLexer& lexer) {
  140. const uint32_t flags = lexer.getNextToken(MasterToken::NUMBER).getNumber();
  141. if (flags > 0xffff) {
  142. isc_throw(InvalidRdataText,
  143. "DNSKEY flags out of range: " << flags);
  144. }
  145. const uint32_t protocol =
  146. lexer.getNextToken(MasterToken::NUMBER).getNumber();
  147. if (protocol > 0xff) {
  148. isc_throw(InvalidRdataText,
  149. "DNSKEY protocol out of range: " << protocol);
  150. }
  151. const uint32_t algorithm =
  152. lexer.getNextToken(MasterToken::NUMBER).getNumber();
  153. if (algorithm > 0xff) {
  154. isc_throw(InvalidRdataText,
  155. "DNSKEY algorithm out of range: " << algorithm);
  156. }
  157. std::string keydata_str;
  158. std::string keydata_substr;
  159. while (true) {
  160. const MasterToken& token =
  161. lexer.getNextToken(MasterToken::STRING, true);
  162. if ((token.getType() == MasterToken::END_OF_FILE) ||
  163. (token.getType() == MasterToken::END_OF_LINE)) {
  164. break;
  165. }
  166. // token is now assured to be of type STRING.
  167. token.getString(keydata_substr);
  168. keydata_str.append(keydata_substr);
  169. }
  170. lexer.ungetToken();
  171. vector<uint8_t> keydata;
  172. // If key data is missing, it's OK. See the API documentation of the
  173. // constructor.
  174. if (keydata_str.size() > 0) {
  175. decodeBase64(keydata_str, keydata);
  176. }
  177. return (new DNSKEYImpl(flags, protocol, algorithm, keydata));
  178. }
  179. DNSKEY::DNSKEY(const DNSKEY& source) :
  180. Rdata(), impl_(new DNSKEYImpl(*source.impl_))
  181. {}
  182. DNSKEY&
  183. DNSKEY::operator=(const DNSKEY& source) {
  184. if (this == &source) {
  185. return (*this);
  186. }
  187. DNSKEYImpl* newimpl = new DNSKEYImpl(*source.impl_);
  188. delete impl_;
  189. impl_ = newimpl;
  190. return (*this);
  191. }
  192. DNSKEY::~DNSKEY() {
  193. delete impl_;
  194. }
  195. string
  196. DNSKEY::toText() const {
  197. return (boost::lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  198. " " + boost::lexical_cast<string>(static_cast<int>(impl_->protocol_)) +
  199. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_)) +
  200. " " + encodeBase64(impl_->keydata_));
  201. }
  202. void
  203. DNSKEY::toWire(OutputBuffer& buffer) const {
  204. buffer.writeUint16(impl_->flags_);
  205. buffer.writeUint8(impl_->protocol_);
  206. buffer.writeUint8(impl_->algorithm_);
  207. buffer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  208. }
  209. void
  210. DNSKEY::toWire(AbstractMessageRenderer& renderer) const {
  211. renderer.writeUint16(impl_->flags_);
  212. renderer.writeUint8(impl_->protocol_);
  213. renderer.writeUint8(impl_->algorithm_);
  214. renderer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  215. }
  216. int
  217. DNSKEY::compare(const Rdata& other) const {
  218. const DNSKEY& other_dnskey = dynamic_cast<const DNSKEY&>(other);
  219. if (impl_->flags_ != other_dnskey.impl_->flags_) {
  220. return (impl_->flags_ < other_dnskey.impl_->flags_ ? -1 : 1);
  221. }
  222. if (impl_->protocol_ != other_dnskey.impl_->protocol_) {
  223. return (impl_->protocol_ < other_dnskey.impl_->protocol_ ? -1 : 1);
  224. }
  225. if (impl_->algorithm_ != other_dnskey.impl_->algorithm_) {
  226. return (impl_->algorithm_ < other_dnskey.impl_->algorithm_ ? -1 : 1);
  227. }
  228. const size_t this_len = impl_->keydata_.size();
  229. const size_t other_len = other_dnskey.impl_->keydata_.size();
  230. const size_t cmplen = min(this_len, other_len);
  231. const int cmp = memcmp(&impl_->keydata_[0],
  232. &other_dnskey.impl_->keydata_[0], cmplen);
  233. if (cmp != 0) {
  234. return (cmp);
  235. } else {
  236. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  237. }
  238. }
  239. uint16_t
  240. DNSKEY::getTag() const {
  241. if (impl_->algorithm_ == 1) {
  242. // See RFC 4034 appendix B.1 for why the key data must contain
  243. // at least 4 bytes with RSA/MD5: 3 trailing bytes to extract
  244. // the tag from, and 1 byte of exponent length subfield before
  245. // modulus.
  246. const int len = impl_->keydata_.size();
  247. if (len < 4) {
  248. isc_throw(isc::OutOfRange,
  249. "DNSKEY keydata too short for tag extraction");
  250. }
  251. return ((impl_->keydata_[len - 3] << 8) + impl_->keydata_[len - 2]);
  252. }
  253. uint32_t ac = impl_->flags_;
  254. ac += (impl_->protocol_ << 8);
  255. ac += impl_->algorithm_;
  256. const size_t size = impl_->keydata_.size();
  257. for (size_t i = 0; i < size; i ++) {
  258. ac += (i & 1) ? impl_->keydata_[i] : (impl_->keydata_[i] << 8);
  259. }
  260. ac += (ac >> 16) & 0xffff;
  261. return (ac & 0xffff);
  262. }
  263. uint16_t
  264. DNSKEY::getFlags() const {
  265. return (impl_->flags_);
  266. }
  267. uint8_t
  268. DNSKEY::getAlgorithm() const {
  269. return (impl_->algorithm_);
  270. }
  271. // END_RDATA_NAMESPACE
  272. // END_ISC_NAMESPACE