tsigkey.cc 8.3 KB

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