tsigkey.cc 7.3 KB

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