tsigkey.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // $Id$
  15. #ifndef __TSIGKEY_H
  16. #define __TSIGKEY_H 1
  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. /// In the current implementation, \c algorithm_name must be a known
  67. /// algorithm to this implementation, which are defined via the
  68. /// <code>static const</code> member functions. For other names
  69. /// an exception of class \c InvalidParameter will be thrown.
  70. /// Note: This restriction may be too strict, and we may revisit it
  71. /// later.
  72. ///
  73. /// \c secret and \c secret_len must be consistent in that the latter
  74. /// is 0 if and only if the former is \c NULL;
  75. /// otherwise an exception of type \c InvalidParameter will be thrown.
  76. ///
  77. /// This constructor internally involves resource allocation, and if
  78. /// it fails, a corresponding standard exception will be thrown.
  79. ///
  80. /// \param key_name The name of the key as a domain name.
  81. /// \param algorithm_name The hash algorithm used for this key in the
  82. /// form of domain name. For example, it can be
  83. /// \c TSIGKey::HMACSHA256_NAME() for HMAC-SHA256.
  84. /// \param secret Point to a binary sequence of the shared secret to be
  85. /// used for this key, or \c NULL if the secret is empty.
  86. /// \param secret_len The size of the binary %data (\c secret) in bytes.
  87. TSIGKey(const Name& key_name, const Name& algorithm_name,
  88. const void* secret, size_t secret_len);
  89. /// \brief The copy constructor.
  90. ///
  91. /// It internally allocates a resource, and if it fails a corresponding
  92. /// standard exception will be thrown.
  93. /// This constructor never throws an exception otherwise.
  94. TSIGKey(const TSIGKey& source);
  95. /// \brief Assignment operator.
  96. ///
  97. /// It internally allocates a resource, and if it fails a corresponding
  98. /// standard exception will be thrown.
  99. /// This operator never throws an exception otherwise.
  100. ///
  101. /// This operator provides the strong exception guarantee: When an
  102. /// exception is thrown the content of the assignment target will be
  103. /// intact.
  104. TSIGKey& operator=(const TSIGKey& source);
  105. /// The destructor.
  106. ~TSIGKey();
  107. //@}
  108. ///
  109. /// \name Getter Methods
  110. ///
  111. /// These methods never throw an exception.
  112. //@{
  113. /// Return the key name.
  114. const Name& getKeyName() const;
  115. /// Return the algorithm name.
  116. const Name& getAlgorithmName() const;
  117. /// Return the length of the TSIG secret in bytes.
  118. size_t getSecretLength() const;
  119. /// Return the value of the TSIG secret.
  120. ///
  121. /// If it returns a non NULL pointer, the memory region beginning at the
  122. /// address returned by this method is valid up to the bytes specified
  123. /// by the return value of \c getSecretLength().
  124. ///
  125. /// The memory region is only valid while the corresponding \c TSIGKey
  126. /// object is valid. The caller must hold the \c TSIGKey object while
  127. /// it needs to refer to the region or it must make a local copy of the
  128. /// region.
  129. const void* getSecret() const;
  130. //@}
  131. ///
  132. /// \name Well known algorithm names as defined in RFC2845 and RFC4635.
  133. ///
  134. /// Note: we begin with the "mandatory" algorithms defined in RFC4635
  135. /// as a minimal initial set.
  136. /// We'll add others as we see the need for them.
  137. //@{
  138. static const Name& HMACMD5_NAME(); ///< HMAC-MD5 (RFC2845)
  139. static const Name& HMACSHA1_NAME(); ///< HMAC-SHA1 (RFC4635)
  140. static const Name& HMACSHA256_NAME(); ///< HMAC-SHA256 (RFC4635)
  141. //@}
  142. private:
  143. struct TSIGKeyImpl;
  144. const TSIGKeyImpl* impl_;
  145. };
  146. /// \brief A simple repository of a set of \c TSIGKey objects.
  147. ///
  148. /// This is a "key ring" to maintain TSIG keys (\c TSIGKey objects) and
  149. /// provides trivial operations such as add, remove, and find.
  150. ///
  151. /// The keys are identified by their key names.
  152. /// So, for example, two or more keys of the same key name but of different
  153. /// algorithms are considered to be the same, and cannot be stored in the
  154. /// key ring at the same time.
  155. ///
  156. /// <b>Implementation Note:</b>
  157. /// For simplicity the initial implementation requests the application make
  158. /// a copy of keys stored in the key ring if it needs to use the keys for
  159. /// a long period (during which some of the keys may be removed).
  160. /// This is based on the observations that a single server will not hold
  161. /// a huge number of keys nor use keys in many different contexts (such as
  162. /// in different DNS transactions).
  163. /// If this assumption does not hold and memory consumption becomes an issue
  164. /// we may have to revisit the design.
  165. class TSIGKeyRing {
  166. public:
  167. /// Result codes of various public methods of \c TSIGKeyRing
  168. enum Result {
  169. SUCCESS = 0, ///< The operation is successful.
  170. EXIST = 1, ///< A key is already stored in \c TSIGKeyRing.
  171. NOTFOUND = 2 ///< The specified key is not found in \c TSIGKeyRing.
  172. };
  173. /// \brief A helper structure to represent the search result of
  174. /// <code>TSIGKeyRing::find()</code>.
  175. ///
  176. /// This is a straightforward pair of the result code and a pointer
  177. /// to the found key to represent the result of \c find().
  178. /// We use this in order to avoid overloading the return value for both
  179. /// the result code ("success" or "not found") and the found object,
  180. /// i.e., avoid using \c NULL to mean "not found", etc.
  181. ///
  182. /// This is a simple value class with no internal state, so for
  183. /// convenience we allow the applications to refer to the members
  184. /// directly.
  185. ///
  186. /// See the description of \c find() for the semantics of the member
  187. /// variables.
  188. struct FindResult {
  189. FindResult(Result param_code, const TSIGKey* param_key) :
  190. code(param_code), key(param_key)
  191. {}
  192. const Result code;
  193. const TSIGKey* const key;
  194. };
  195. ///
  196. /// \name Constructors and Destructor.
  197. ///
  198. /// \b Note:
  199. /// The copy constructor and the assignment operator are
  200. /// intentionally defined as private, making this class non copyable.
  201. /// There is no technical reason why this class cannot be copied,
  202. /// but since the key ring can potentially have a large number of keys,
  203. /// a naive copy operation may cause unexpected overhead.
  204. /// It's generally expected for an application to share the same
  205. /// instance of key ring and share it throughout the program via
  206. /// references, so we prevent the copy operation explicitly to avoid
  207. /// unexpected copy operations.
  208. //@{
  209. private:
  210. TSIGKeyRing(const TSIGKeyRing& source);
  211. TSIGKeyRing& operator=(const TSIGKeyRing& source);
  212. public:
  213. /// \brief The default constructor.
  214. ///
  215. /// This constructor never throws an exception.
  216. TSIGKeyRing();
  217. /// The destructor.
  218. ~TSIGKeyRing();
  219. //@}
  220. /// Return the number of keys stored in the \c TSIGKeyRing.
  221. ///
  222. /// This method never throws an exception.
  223. unsigned int size() const;
  224. /// Add a \c TSIGKey to the \c TSIGKeyRing.
  225. ///
  226. /// This method will create a local copy of the given key, so the caller
  227. /// does not have to keep owning it.
  228. ///
  229. /// If internal resource allocation fails, a corresponding standard
  230. /// exception will be thrown.
  231. /// This method never throws an exception otherwise.
  232. ///
  233. /// \param key A \c TSIGKey to be added.
  234. /// \return \c SUCCESS If the key is successfully added to the key ring.
  235. /// \return \c EXIST The key ring already stores a key whose name is
  236. /// identical to that of \c key.
  237. Result add(const TSIGKey& key);
  238. /// Remove a \c TSIGKey for the given name from the \c TSIGKeyRing.
  239. ///
  240. /// This method never throws an exception.
  241. ///
  242. /// \param key_name The name of the key to be removed.
  243. /// \return \c SUCCESS If the key is successfully removed from the key
  244. /// ring.
  245. /// \return \c NOTFOUND The key ring does not store the key that matches
  246. /// \c key_name.
  247. Result remove(const Name& key_name);
  248. /// Find a \c TSIGKey for the given name in the \c TSIGKeyRing.
  249. ///
  250. /// It searches the internal storage for a \c TSIGKey whose name is
  251. /// \c key_name, and returns the result in the form of a \c FindResult
  252. /// object as follows:
  253. /// - \c code: \c SUCCESS if a key is found; otherwise \c NOTFOUND.
  254. /// - \c key: A pointer to the found \c TSIGKey object if one is found;
  255. /// otherwise \c NULL.
  256. ///
  257. /// The pointer returned in the \c FindResult object is only valid until
  258. /// the corresponding key is removed from the key ring.
  259. /// The caller must ensure that the key is held in the key ring while
  260. /// it needs to refer to it, or it must make a local copy of the key.
  261. ///
  262. /// This method never throws an exception.
  263. ///
  264. /// \param key_name The name of the key to be found.
  265. /// \return A \c FindResult object enclosing the search result (see above).
  266. FindResult find(const Name& key_name);
  267. private:
  268. struct TSIGKeyRingImpl;
  269. TSIGKeyRingImpl* impl_;
  270. };
  271. }
  272. }
  273. #endif // __TSIGKEY_H
  274. // Local Variables:
  275. // mode: c++
  276. // End: