dnskey_48.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // $Id$
  15. #include <iostream>
  16. #include <string>
  17. #include <sstream>
  18. #include <vector>
  19. #include "base64.h"
  20. #include "buffer.h"
  21. #include "messagerenderer.h"
  22. #include "name.h"
  23. #include "rdata.h"
  24. #include "rdataclass.h"
  25. #include <boost/lexical_cast.hpp>
  26. #include <boost/foreach.hpp>
  27. #include <stdio.h>
  28. #include <time.h>
  29. using namespace std;
  30. // BEGIN_ISC_NAMESPACE
  31. // BEGIN_RDATA_NAMESPACE
  32. struct DNSKEYImpl {
  33. // straightforward representation of DNSKEY RDATA fields
  34. DNSKEYImpl(uint16_t flags, uint8_t protocol, uint8_t algorithm,
  35. const vector<uint8_t>& keydata) :
  36. flags_(flags), protocol_(protocol), algorithm_(algorithm),
  37. keydata_(keydata)
  38. {}
  39. uint16_t flags_;
  40. uint8_t protocol_;
  41. uint8_t algorithm_;
  42. const vector<uint8_t> keydata_;
  43. };
  44. DNSKEY::DNSKEY(const string& dnskey_str) :
  45. impl_(NULL)
  46. {
  47. istringstream iss(dnskey_str);
  48. unsigned int flags, protocol, algorithm;
  49. stringbuf keydatabuf;
  50. iss >> flags >> protocol >> algorithm >> &keydatabuf;
  51. if (iss.bad() || iss.fail()) {
  52. isc_throw(InvalidRdataText, "Invalid DNSKEY text");
  53. }
  54. if (flags > 0xffff) {
  55. isc_throw(InvalidRdataText, "DNSKEY flags out of range");
  56. }
  57. if (protocol > 0xff) {
  58. isc_throw(InvalidRdataText, "DNSKEY protocol out of range");
  59. }
  60. if (algorithm > 0xff) {
  61. isc_throw(InvalidRdataText, "DNSKEY algorithm out of range");
  62. }
  63. vector<uint8_t> keydata;
  64. decodeBase64(keydatabuf.str(), keydata);
  65. if (algorithm == 1 && keydata.size() < 3) {
  66. isc_throw(InvalidRdataLength, "DNSKEY keydata too short");
  67. }
  68. impl_ = new DNSKEYImpl(flags, protocol, algorithm, keydata);
  69. }
  70. DNSKEY::DNSKEY(InputBuffer& buffer, size_t rdata_len)
  71. {
  72. if (rdata_len < 4) {
  73. isc_throw(InvalidRdataLength, "DNSKEY too short");
  74. }
  75. uint16_t flags = buffer.readUint16();
  76. uint16_t protocol = buffer.readUint8();
  77. uint16_t algorithm = buffer.readUint8();
  78. rdata_len -= 4;
  79. vector<uint8_t> keydata(rdata_len);
  80. buffer.readData(&keydata[0], rdata_len);
  81. impl_ = new DNSKEYImpl(flags, protocol, algorithm, keydata);
  82. }
  83. DNSKEY::DNSKEY(const DNSKEY& source) :
  84. impl_(new DNSKEYImpl(*source.impl_))
  85. {}
  86. DNSKEY&
  87. DNSKEY::operator=(const DNSKEY& source)
  88. {
  89. if (impl_ == source.impl_) {
  90. return (*this);
  91. }
  92. DNSKEYImpl* newimpl = new DNSKEYImpl(*source.impl_);
  93. delete impl_;
  94. impl_ = newimpl;
  95. return (*this);
  96. }
  97. DNSKEY::~DNSKEY()
  98. {
  99. delete impl_;
  100. }
  101. string
  102. DNSKEY::toText() const
  103. {
  104. return (boost::lexical_cast<string>(static_cast<int>(impl_->flags_)) +
  105. " " + boost::lexical_cast<string>(static_cast<int>(impl_->protocol_)) +
  106. " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_)) +
  107. " " + encodeBase64(impl_->keydata_));
  108. }
  109. void
  110. DNSKEY::toWire(OutputBuffer& buffer) const
  111. {
  112. buffer.writeUint16(impl_->flags_);
  113. buffer.writeUint8(impl_->protocol_);
  114. buffer.writeUint8(impl_->algorithm_);
  115. buffer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  116. }
  117. void
  118. DNSKEY::toWire(MessageRenderer& renderer) const
  119. {
  120. renderer.writeUint16(impl_->flags_);
  121. renderer.writeUint8(impl_->protocol_);
  122. renderer.writeUint8(impl_->algorithm_);
  123. renderer.writeData(&impl_->keydata_[0], impl_->keydata_.size());
  124. }
  125. int
  126. DNSKEY::compare(const Rdata& other) const
  127. {
  128. const DNSKEY& other_dnskey = dynamic_cast<const DNSKEY&>(other);
  129. if (impl_->flags_ != other_dnskey.impl_->flags_) {
  130. return (impl_->flags_ < other_dnskey.impl_->flags_ ? -1 : 1);
  131. }
  132. if (impl_->protocol_ != other_dnskey.impl_->protocol_) {
  133. return (impl_->protocol_ < other_dnskey.impl_->protocol_ ? -1 : 1);
  134. }
  135. if (impl_->algorithm_ != other_dnskey.impl_->algorithm_) {
  136. return (impl_->algorithm_ < other_dnskey.impl_->algorithm_ ? -1 : 1);
  137. }
  138. size_t this_len = impl_->keydata_.size();
  139. size_t other_len = other_dnskey.impl_->keydata_.size();
  140. size_t cmplen = min(this_len, other_len);
  141. int cmp = memcmp(&impl_->keydata_[0], &other_dnskey.impl_->keydata_[0],
  142. cmplen);
  143. if (cmp != 0) {
  144. return (cmp);
  145. } else {
  146. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  147. }
  148. }
  149. uint16_t
  150. DNSKEY::getTag() const
  151. {
  152. if (impl_->algorithm_ == 1) {
  153. int len = impl_->keydata_.size();
  154. return ((impl_->keydata_[len - 3] << 8) + impl_->keydata_[len - 2]);
  155. }
  156. uint32_t ac = impl_->flags_;
  157. ac += (impl_->protocol_ << 8);
  158. ac += impl_->algorithm_;
  159. size_t size = impl_->keydata_.size();
  160. for (int i = 0; i < size; i ++) {
  161. ac += (i & 1) ? impl_->keydata_[i] : (impl_->keydata_[i] << 8);
  162. }
  163. ac += (ac >> 16) & 0xffff;
  164. return (ac & 0xffff);
  165. }
  166. uint16_t
  167. DNSKEY::getFlags() const {
  168. return (impl_->flags_);
  169. }
  170. uint8_t
  171. DNSKEY::getAlgorithm() const {
  172. return (impl_->algorithm_);
  173. }
  174. // END_RDATA_NAMESPACE
  175. // END_ISC_NAMESPACE