nsec3hash.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2012 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 __NSEC3HASH_H
  15. #define __NSEC3HASH_H 1
  16. #include <string>
  17. #include <boost/noncopyable.hpp>
  18. #include <exceptions/exceptions.h>
  19. namespace isc {
  20. namespace dns {
  21. class Name;
  22. namespace rdata {
  23. namespace generic {
  24. class NSEC3PARAM;
  25. }
  26. }
  27. /// \brief An exception that is thrown for when an \c NSEC3Hash object is
  28. /// constructed with an unknown hash algorithm.
  29. ///
  30. /// A specific exception class is used so that the caller can selectively
  31. /// catch this exception, e.g., while loading a zone, and handle it
  32. /// accordingly.
  33. class UnknownNSEC3HashAlgorithm : public isc::Exception {
  34. public:
  35. UnknownNSEC3HashAlgorithm(const char* file, size_t line,
  36. const char* what) :
  37. isc::Exception(file, line, what) {}
  38. };
  39. /// \brief A calculator of NSEC3 hashes.
  40. ///
  41. /// This is a simple class that encapsulates the algorithm of calculating
  42. /// NSEC3 hash values as defined in RFC5155.
  43. ///
  44. /// This class is designed to be "stateless" in that it basically doesn't
  45. /// hold mutable state once constructed, and hash calculation solely depends
  46. /// on the parameters given on construction and input to the \c calculate()
  47. /// method. In that sense this could be a single free function rather than
  48. /// a class, but we decided to provide the functionality as a class for
  49. /// two reasons: NSEC3 hash calculations would often take place more than one
  50. /// time in a single query or validation process, so it would be more
  51. /// efficient if we could hold some internal resources used for the
  52. /// calculation and reuse it over multiple calls to \c calculate() (this
  53. /// implementation actually does this); Second, for testing purposes we may
  54. /// want to use a fake calculator that returns pre-defined hash values
  55. /// (so a slight change to the test input wouldn't affect the test result).
  56. /// Using a class would make it possible by introducing a common base class
  57. /// and having the application depend on that base class (then the fake
  58. /// calculator will be defined as a separate subclass of the base).
  59. ///
  60. /// The initial implementation makes this class non copyable as it wouldn't
  61. /// used be passed from one place to another, especially if and when it's
  62. /// used via a base class abstraction. But there's no fundamental reason
  63. /// this cannot be copied, so if we see a specific need for it, this
  64. /// restriction can be revisited.
  65. ///
  66. /// There can be several ways to extend this class in future. Those include:
  67. /// - Introduce a base class and make it derived from it (mainly for testing
  68. /// purposes as noted above)
  69. /// - Allow to construct the class from a tuple of parameters, that is,
  70. /// integers for algorithm, iterations and flags, and opaque salt data.
  71. /// For example, we might want to use that version for validators.
  72. /// - Allow producing hash value as binary data
  73. /// - Allow updating NSEC3 parameters of a class object so we can still reuse
  74. /// the internal resources for different sets of parameters.
  75. class NSEC3Hash : public boost::noncopyable {
  76. public:
  77. /// \brief Constructor from NSEC3PARAM RDATA.
  78. ///
  79. /// The hash algorithm given via \c param must be known to the
  80. /// implementation. Otherwise \c UnknownNSEC3HashAlgorithm exception
  81. /// will be thrown.
  82. ///
  83. /// \throw UnknownNSEC3HashAlgorithm The specified algorithm in \c param
  84. /// is unknown.
  85. /// \throw std::bad_alloc Internal resource allocation failure.
  86. ///
  87. /// \param param NSEC3 parameters used for subsequent calculation.
  88. NSEC3Hash(const rdata::generic::NSEC3PARAM& param);
  89. /// \brief The destructor.
  90. ~NSEC3Hash();
  91. /// \brief Calculate the NSEC3 hash.
  92. ///
  93. /// This method calculates the NSEC3 hash value for the given \c name
  94. /// with the hash parameters (algorithm, iterations and salt) given at
  95. /// construction, and returns the value in a base32hex-encoded string
  96. /// (without containing any white spaces). All alphabets in the string
  97. /// will be upper cased.
  98. ///
  99. /// \param name The domain name for which the hash value is to be
  100. /// calculated.
  101. /// \return Base32hex-encoded string of the hash value.
  102. std::string calculate(const Name& name) const;
  103. private:
  104. struct NSEC3HashImpl;
  105. NSEC3HashImpl* impl_;
  106. };
  107. }
  108. }
  109. #endif // __NSEC3HASH_H
  110. // Local Variables:
  111. // mode: c++
  112. // End: