tsigkey.h 13 KB

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