tsigkey.cc 6.7 KB

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