tsigkey.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <map>
  15. #include <utility>
  16. #include <vector>
  17. #include <exceptions/exceptions.h>
  18. #include <dns/name.h>
  19. #include <dns/util/base64.h>
  20. #include <dns/tsigkey.h>
  21. using namespace std;
  22. namespace isc {
  23. namespace dns {
  24. struct
  25. TSIGKey::TSIGKeyImpl {
  26. TSIGKeyImpl(const Name& key_name, const Name& algorithm_name,
  27. const void* secret, size_t secret_len) :
  28. key_name_(key_name), algorithm_name_(algorithm_name),
  29. secret_(static_cast<const uint8_t*>(secret),
  30. static_cast<const uint8_t*>(secret) + secret_len)
  31. {
  32. // Convert the name to the canonical form.
  33. algorithm_name_.downcase();
  34. }
  35. const Name key_name_;
  36. Name algorithm_name_;
  37. const vector<uint8_t> secret_;
  38. };
  39. TSIGKey::TSIGKey(const Name& key_name, const Name& algorithm_name,
  40. const void* secret, size_t secret_len) : impl_(NULL)
  41. {
  42. if (algorithm_name != HMACMD5_NAME() &&
  43. algorithm_name != HMACSHA1_NAME() &&
  44. algorithm_name != HMACSHA256_NAME()) {
  45. isc_throw(InvalidParameter, "Unknown TSIG algorithm is specified: " <<
  46. algorithm_name);
  47. }
  48. if ((secret != NULL && secret_len == 0) ||
  49. (secret == NULL && secret_len != 0)) {
  50. isc_throw(InvalidParameter,
  51. "TSIGKey secret and its length are inconsistent");
  52. }
  53. impl_ = new TSIGKeyImpl(key_name, algorithm_name, secret, secret_len);
  54. }
  55. TSIGKey::TSIGKey(const std::string& str) : impl_(NULL) {
  56. size_t pos = str.find(':');
  57. if (pos == 0 || pos == str.npos || pos == str.size()-1) {
  58. // error
  59. isc_throw(InvalidParameter, "Invalid TSIG key string");
  60. }
  61. try {
  62. Name key_name(str.substr(0, pos));
  63. Name algo_name("hmac-md5.sig-alg.reg.int");
  64. // optional algorithm part
  65. size_t pos2 = str.find(':', pos+1);
  66. if (pos2 != str.npos) {
  67. if (pos2 == pos + 1) {
  68. isc_throw(InvalidParameter, "Invalid TSIG key string");
  69. }
  70. algo_name = Name(str.substr(pos2+1));
  71. } else {
  72. pos2 = str.size() - pos;
  73. }
  74. std::string secret_str = str.substr(pos + 1, pos2 - pos - 1);
  75. if (algo_name != HMACMD5_NAME() &&
  76. algo_name != HMACSHA1_NAME() &&
  77. algo_name != HMACSHA256_NAME()) {
  78. isc_throw(InvalidParameter, "Unknown TSIG algorithm is specified: " <<
  79. algo_name);
  80. }
  81. vector<uint8_t> secret;
  82. decodeBase64(secret_str, secret);
  83. impl_ = new TSIGKeyImpl(key_name, algo_name, &secret[0],
  84. secret.size());
  85. } catch (const Exception& e) {
  86. // 'reduce' the several types of exceptions name parsing and
  87. // Base64 decoding can throw to just the InvalidParameter
  88. isc_throw(InvalidParameter, e.what());
  89. }
  90. }
  91. TSIGKey::TSIGKey(const TSIGKey& source) : impl_(new TSIGKeyImpl(*source.impl_))
  92. {}
  93. TSIGKey&
  94. TSIGKey::operator=(const TSIGKey& source) {
  95. if (impl_ == source.impl_) {
  96. return (*this);
  97. }
  98. TSIGKeyImpl* newimpl = new TSIGKeyImpl(*source.impl_);
  99. delete impl_;
  100. impl_ = newimpl;
  101. return (*this);
  102. }
  103. TSIGKey::~TSIGKey() {
  104. delete impl_;
  105. }
  106. const Name&
  107. TSIGKey::getKeyName() const {
  108. return (impl_->key_name_);
  109. }
  110. const Name&
  111. TSIGKey::getAlgorithmName() const {
  112. return (impl_->algorithm_name_);
  113. }
  114. const void*
  115. TSIGKey::getSecret() const {
  116. return ((impl_->secret_.size() > 0) ? &impl_->secret_[0] : NULL);
  117. }
  118. size_t
  119. TSIGKey::getSecretLength() const {
  120. return (impl_->secret_.size());
  121. }
  122. std::string
  123. TSIGKey::toText() const {
  124. const vector<uint8_t> secret_v(static_cast<const uint8_t*>(getSecret()),
  125. static_cast<const uint8_t*>(getSecret()) +
  126. getSecretLength());
  127. std::string secret_str = encodeBase64(secret_v);
  128. return (getKeyName().toText() + ":" + secret_str + ":" +
  129. getAlgorithmName().toText());
  130. }
  131. const
  132. Name& TSIGKey::HMACMD5_NAME() {
  133. static Name alg_name("hmac-md5.sig-alg.reg.int");
  134. return (alg_name);
  135. }
  136. const
  137. Name& TSIGKey::HMACSHA1_NAME() {
  138. static Name alg_name("hmac-sha1");
  139. return (alg_name);
  140. }
  141. const
  142. Name& TSIGKey::HMACSHA256_NAME() {
  143. static Name alg_name("hmac-sha256");
  144. return (alg_name);
  145. }
  146. struct TSIGKeyRing::TSIGKeyRingImpl {
  147. typedef map<Name, TSIGKey> TSIGKeyMap;
  148. typedef pair<Name, TSIGKey> NameAndKey;
  149. TSIGKeyMap keys;
  150. };
  151. TSIGKeyRing::TSIGKeyRing() : impl_(new TSIGKeyRingImpl) {
  152. }
  153. TSIGKeyRing::~TSIGKeyRing() {
  154. delete impl_;
  155. }
  156. unsigned int
  157. TSIGKeyRing::size() const {
  158. return (impl_->keys.size());
  159. }
  160. TSIGKeyRing::Result
  161. TSIGKeyRing::add(const TSIGKey& key) {
  162. if (impl_->keys.insert(
  163. TSIGKeyRingImpl::NameAndKey(key.getKeyName(), key)).second
  164. == true) {
  165. return (SUCCESS);
  166. } else {
  167. return (EXIST);
  168. }
  169. }
  170. TSIGKeyRing::Result
  171. TSIGKeyRing::remove(const Name& key_name) {
  172. return (impl_->keys.erase(key_name) == 1 ? SUCCESS : NOTFOUND);
  173. }
  174. TSIGKeyRing::FindResult
  175. TSIGKeyRing::find(const Name& key_name) {
  176. TSIGKeyRingImpl::TSIGKeyMap::const_iterator found =
  177. impl_->keys.find(key_name);
  178. if (found == impl_->keys.end()) {
  179. return (FindResult(NOTFOUND, NULL));
  180. }
  181. return (FindResult(SUCCESS, &((*found).second)));
  182. }
  183. } // namespace dns
  184. } // namespace isc