tsigkey.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #ifndef TSIGKEY_H
  15. #define TSIGKEY_H 1
  16. #include <cryptolink/cryptolink.h>
  17. namespace isc {
  18. namespace dns {
  19. class Name;
  20. /// \brief TSIG key.
  21. ///
  22. /// This class holds a TSIG key along with some related attributes as
  23. /// defined in RFC2845.
  24. ///
  25. /// A TSIG key consists of the following attributes:
  26. /// - Key name
  27. /// - Hash algorithm
  28. /// - Shared secret
  29. ///
  30. /// <b>Implementation Notes</b>
  31. ///
  32. /// We may add more attributes in future versions. For example, if and when
  33. /// we support the TKEY protocol (RFC2930), we may need to introduce the
  34. /// notion of inception and expiration times.
  35. /// At that point we may also have to introduce a class hierarchy to handle
  36. /// different types of keys in a polymorphic way.
  37. /// At the moment we use the straightforward value-type class with minimal
  38. /// attributes.
  39. ///
  40. /// In the TSIG protocol, hash algorithms are represented in the form of
  41. /// domain name.
  42. /// Our interfaces provide direct translation of this concept; for example,
  43. /// the constructor from parameters take a \c Name object to specify the
  44. /// algorithm.
  45. /// On one hand, this may be counter intuitive.
  46. /// An API user would rather specify "hmac-md5" instead of
  47. /// <code>Name("hmac-md5.sig-alg.reg.int")</code>.
  48. /// On the other hand, it may be more convenient for some kind of applications
  49. /// if we maintain the algorithm as the expected representation for
  50. /// protocol operations (such as sign and very a message).
  51. /// Considering these points, we adopt the interface closer to the protocol
  52. /// specification for now.
  53. /// To minimize the burden for API users, we also define a set of constants
  54. /// for commonly used algorithm names so that the users don't have to
  55. /// remember the actual domain names defined in the protocol specification.
  56. /// We may also have to add conversion routines between domain names
  57. /// and more intuitive representations (e.g. strings) for algorithms.
  58. class TSIGKey {
  59. public:
  60. ///
  61. /// \name Constructors, Assignment Operator and Destructor.
  62. ///
  63. //@{
  64. /// \brief Constructor from key parameters
  65. ///
  66. /// \c algorithm_name should generally be a known algorithm to this
  67. /// implementation, which are defined via the
  68. /// <code>static const</code> member functions.
  69. ///
  70. /// Other names are still accepted as long as the secret is empty
  71. /// (\c secret is \c NULL and \c secret_len is 0), however; in some cases
  72. /// we might want to treat just the pair of key name and algorithm name
  73. /// opaquely, e.g., when generating a response TSIG with a BADKEY error
  74. /// because the algorithm is unknown as specified in Section 3.2 of
  75. /// RFC2845 (in which case the algorithm name would be copied from the
  76. /// request to the response, and for that purpose it would be convenient
  77. /// if a \c TSIGKey object can hold a name for an "unknown" algorithm).
  78. ///
  79. /// \note RFC2845 does not specify which algorithm name should be used
  80. /// in such a BADKEY response. The behavior of using the same algorithm
  81. /// is derived from the BIND 9 implementation.
  82. ///
  83. /// It is unlikely that a TSIG key with an unknown algorithm is of any
  84. /// use with actual crypto operation, so care must be taken when dealing
  85. /// with such keys. (The restriction for the secret will prevent
  86. /// accidental creation of such a dangerous key, e.g., due to misspelling
  87. /// in a configuration file).
  88. /// If the given algorithm name is unknown and non empty secret is
  89. /// specified, an exception of type \c InvalidParameter will be thrown.
  90. ///
  91. /// \c secret and \c secret_len must be consistent in that the latter
  92. /// is 0 if and only if the former is \c NULL;
  93. /// otherwise an exception of type \c InvalidParameter will be thrown.
  94. ///
  95. /// This constructor internally involves resource allocation, and if
  96. /// it fails, a corresponding standard exception will be thrown.
  97. ///
  98. /// \param key_name The name of the key as a domain name.
  99. /// \param algorithm_name The hash algorithm used for this key in the
  100. /// form of domain name. For example, it can be
  101. /// \c TSIGKey::HMACSHA256_NAME() for HMAC-SHA256.
  102. /// \param secret Point to a binary sequence of the shared secret to be
  103. /// used for this key, or \c NULL if the secret is empty.
  104. /// \param secret_len The size of the binary %data (\c secret) in bytes.
  105. TSIGKey(const Name& key_name, const Name& algorithm_name,
  106. const void* secret, size_t secret_len);
  107. /// \brief Constructor from an input string
  108. ///
  109. /// The string must be of the form:
  110. /// name:secret[:algorithm]
  111. /// Where "name" is a domain name for the key, "secret" is a
  112. /// base64 representation of the key secret, and the optional
  113. /// "algorithm" is an algorithm identifier as specified in RFC 4635.
  114. /// The default algorithm is hmac-md5.sig-alg.reg.int.
  115. ///
  116. /// The same restriction about the algorithm name (and secret) as that
  117. /// for the other constructor applies.
  118. ///
  119. /// Since ':' is used as a separator here, it is not possible to
  120. /// use this constructor to create keys with a ':' character in
  121. /// their name.
  122. ///
  123. /// \exception InvalidParameter exception if the input string is
  124. /// invalid.
  125. ///
  126. /// \param str The string to make a TSIGKey from
  127. explicit TSIGKey(const std::string& str);
  128. /// \brief The copy constructor.
  129. ///
  130. /// It internally allocates a resource, and if it fails a corresponding
  131. /// standard exception will be thrown.
  132. /// This constructor never throws an exception otherwise.
  133. TSIGKey(const TSIGKey& source);
  134. /// \brief Assignment operator.
  135. ///
  136. /// It internally allocates a resource, and if it fails a corresponding
  137. /// standard exception will be thrown.
  138. /// This operator never throws an exception otherwise.
  139. ///
  140. /// This operator provides the strong exception guarantee: When an
  141. /// exception is thrown the content of the assignment target will be
  142. /// intact.
  143. TSIGKey& operator=(const TSIGKey& source);
  144. /// The destructor.
  145. ~TSIGKey();
  146. //@}
  147. ///
  148. /// \name Getter Methods
  149. ///
  150. /// These methods never throw an exception.
  151. //@{
  152. /// Return the key name.
  153. const Name& getKeyName() const;
  154. /// Return the algorithm name.
  155. const Name& getAlgorithmName() const;
  156. /// Return the hash algorithm name in the form of cryptolink::HashAlgorithm
  157. isc::cryptolink::HashAlgorithm getAlgorithm() const;
  158. /// Return the length of the TSIG secret in bytes.
  159. size_t getSecretLength() const;
  160. /// Return the value of the TSIG secret.
  161. ///
  162. /// If it returns a non NULL pointer, the memory region beginning at the
  163. /// address returned by this method is valid up to the bytes specified
  164. /// by the return value of \c getSecretLength().
  165. ///
  166. /// The memory region is only valid while the corresponding \c TSIGKey
  167. /// object is valid. The caller must hold the \c TSIGKey object while
  168. /// it needs to refer to the region or it must make a local copy of the
  169. /// region.
  170. const void* getSecret() const;
  171. //@}
  172. /// \brief Converts the TSIGKey to a string value
  173. ///
  174. /// The resulting string will be of the form
  175. /// name:secret:algorithm
  176. /// Where "name" is a domain name for the key, "secret" is a
  177. /// base64 representation of the key secret, and "algorithm" is
  178. /// an algorithm identifier as specified in RFC 4635.
  179. ///
  180. /// \return The string representation of the given TSIGKey.
  181. std::string toText() const;
  182. ///
  183. /// \name Well known algorithm names as defined in RFC2845 and RFC4635.
  184. ///
  185. /// Note: we begin with the "mandatory" algorithms defined in RFC4635
  186. /// as a minimal initial set.
  187. /// We'll add others as we see the need for them.
  188. //@{
  189. static const Name& HMACMD5_NAME(); ///< HMAC-MD5 (RFC2845)
  190. static const Name& HMACSHA1_NAME(); ///< HMAC-SHA1 (RFC4635)
  191. static const Name& HMACSHA256_NAME(); ///< HMAC-SHA256 (RFC4635)
  192. static const Name& HMACSHA224_NAME(); ///< HMAC-SHA256 (RFC4635)
  193. static const Name& HMACSHA384_NAME(); ///< HMAC-SHA256 (RFC4635)
  194. static const Name& HMACSHA512_NAME(); ///< HMAC-SHA256 (RFC4635)
  195. //@}
  196. private:
  197. struct TSIGKeyImpl;
  198. const TSIGKeyImpl* impl_;
  199. };
  200. /// \brief A simple repository of a set of \c TSIGKey objects.
  201. ///
  202. /// This is a "key ring" to maintain TSIG keys (\c TSIGKey objects) and
  203. /// provides trivial operations such as add, remove, and find.
  204. ///
  205. /// The keys are identified by their key names.
  206. /// So, for example, two or more keys of the same key name but of different
  207. /// algorithms are considered to be the same, and cannot be stored in the
  208. /// key ring at the same time.
  209. ///
  210. /// <b>Implementation Note:</b>
  211. /// For simplicity the initial implementation requests the application make
  212. /// a copy of keys stored in the key ring if it needs to use the keys for
  213. /// a long period (during which some of the keys may be removed).
  214. /// This is based on the observations that a single server will not hold
  215. /// a huge number of keys nor use keys in many different contexts (such as
  216. /// in different DNS transactions).
  217. /// If this assumption does not hold and memory consumption becomes an issue
  218. /// we may have to revisit the design.
  219. class TSIGKeyRing {
  220. public:
  221. /// Result codes of various public methods of \c TSIGKeyRing
  222. enum Result {
  223. SUCCESS = 0, ///< The operation is successful.
  224. EXIST = 1, ///< A key is already stored in \c TSIGKeyRing.
  225. NOTFOUND = 2 ///< The specified key is not found in \c TSIGKeyRing.
  226. };
  227. /// \brief A helper structure to represent the search result of
  228. /// <code>TSIGKeyRing::find()</code>.
  229. ///
  230. /// This is a straightforward pair of the result code and a pointer
  231. /// to the found key to represent the result of \c find().
  232. /// We use this in order to avoid overloading the return value for both
  233. /// the result code ("success" or "not found") and the found object,
  234. /// i.e., avoid using \c NULL to mean "not found", etc.
  235. ///
  236. /// This is a simple value class with no internal state, so for
  237. /// convenience we allow the applications to refer to the members
  238. /// directly.
  239. ///
  240. /// See the description of \c find() for the semantics of the member
  241. /// variables.
  242. struct FindResult {
  243. FindResult(Result param_code, const TSIGKey* param_key) :
  244. code(param_code), key(param_key)
  245. {}
  246. const Result code;
  247. const TSIGKey* const key;
  248. };
  249. ///
  250. /// \name Constructors and Destructor.
  251. ///
  252. /// \b Note:
  253. /// The copy constructor and the assignment operator are
  254. /// intentionally defined as private, making this class non copyable.
  255. /// There is no technical reason why this class cannot be copied,
  256. /// but since the key ring can potentially have a large number of keys,
  257. /// a naive copy operation may cause unexpected overhead.
  258. /// It's generally expected for an application to share the same
  259. /// instance of key ring and share it throughout the program via
  260. /// references, so we prevent the copy operation explicitly to avoid
  261. /// unexpected copy operations.
  262. //@{
  263. private:
  264. TSIGKeyRing(const TSIGKeyRing& source);
  265. TSIGKeyRing& operator=(const TSIGKeyRing& source);
  266. public:
  267. /// \brief The default constructor.
  268. ///
  269. /// This constructor never throws an exception.
  270. TSIGKeyRing();
  271. /// The destructor.
  272. ~TSIGKeyRing();
  273. //@}
  274. /// Return the number of keys stored in the \c TSIGKeyRing.
  275. ///
  276. /// This method never throws an exception.
  277. unsigned int size() const;
  278. /// Add a \c TSIGKey to the \c TSIGKeyRing.
  279. ///
  280. /// This method will create a local copy of the given key, so the caller
  281. /// does not have to keep owning it.
  282. ///
  283. /// If internal resource allocation fails, a corresponding standard
  284. /// exception will be thrown.
  285. /// This method never throws an exception otherwise.
  286. ///
  287. /// \param key A \c TSIGKey to be added.
  288. /// \return \c SUCCESS If the key is successfully added to the key ring.
  289. /// \return \c EXIST The key ring already stores a key whose name is
  290. /// identical to that of \c key.
  291. Result add(const TSIGKey& key);
  292. /// Remove a \c TSIGKey for the given name from the \c TSIGKeyRing.
  293. ///
  294. /// This method never throws an exception.
  295. ///
  296. /// \param key_name The name of the key to be removed.
  297. /// \return \c SUCCESS If the key is successfully removed from the key
  298. /// ring.
  299. /// \return \c NOTFOUND The key ring does not store the key that matches
  300. /// \c key_name.
  301. Result remove(const Name& key_name);
  302. /// Find a \c TSIGKey for the given name in the \c TSIGKeyRing.
  303. ///
  304. /// It searches the internal storage for a \c TSIGKey whose name is
  305. /// \c key_name.
  306. /// It returns the result in the form of a \c FindResult
  307. /// object as follows:
  308. /// - \c code: \c SUCCESS if a key is found; otherwise \c NOTFOUND.
  309. /// - \c key: A pointer to the found \c TSIGKey object if one is found;
  310. /// otherwise \c NULL.
  311. ///
  312. /// The pointer returned in the \c FindResult object is only valid until
  313. /// the corresponding key is removed from the key ring.
  314. /// The caller must ensure that the key is held in the key ring while
  315. /// it needs to refer to it, or it must make a local copy of the key.
  316. ///
  317. /// This method never throws an exception.
  318. ///
  319. /// \param key_name The name of the key to be found.
  320. /// \return A \c FindResult object enclosing the search result (see above).
  321. FindResult find(const Name& key_name) const;
  322. /// Find a \c TSIGKey for the given name in the \c TSIGKeyRing.
  323. ///
  324. /// It searches the internal storage for a \c TSIGKey whose name is
  325. /// \c key_name and that uses the hash algorithm identified by
  326. /// \c algorithm_name.
  327. /// It returns the result in the form of a \c FindResult
  328. /// object as follows:
  329. /// - \c code: \c SUCCESS if a key is found; otherwise \c NOTFOUND.
  330. /// - \c key: A pointer to the found \c TSIGKey object if one is found;
  331. /// otherwise \c NULL.
  332. ///
  333. /// The pointer returned in the \c FindResult object is only valid until
  334. /// the corresponding key is removed from the key ring.
  335. /// The caller must ensure that the key is held in the key ring while
  336. /// it needs to refer to it, or it must make a local copy of the key.
  337. ///
  338. /// This method never throws an exception.
  339. ///
  340. /// \param key_name The name of the key to be found.
  341. /// \param algorithm_name The name of the algorithm of the found key.
  342. /// \return A \c FindResult object enclosing the search result (see above).
  343. FindResult find(const Name& key_name, const Name& algorithm_name) const;
  344. private:
  345. struct TSIGKeyRingImpl;
  346. TSIGKeyRingImpl* impl_;
  347. };
  348. }
  349. }
  350. #endif // TSIGKEY_H
  351. // Local Variables:
  352. // mode: c++
  353. // End: