nsec3hash.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <exceptions/exceptions.h>
  18. namespace isc {
  19. namespace dns {
  20. class Name;
  21. namespace rdata {
  22. namespace generic {
  23. class NSEC3;
  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 an abstract base class that defines a simple interface to
  42. /// calculating NSEC3 hash values as defined in RFC5155.
  43. ///
  44. /// (Derived classes of) this class is designed to be "stateless" in that it
  45. /// basically doesn't hold mutable state once constructed, and hash
  46. /// calculation solely depends on the parameters given on construction and
  47. /// input to the \c calculate() method. In that sense this could be a
  48. /// single free function rather than a class, but we decided to provide the
  49. /// functionality as a class for two reasons: NSEC3 hash calculations would
  50. /// often take place more than one time in a single query or validation
  51. /// process, so it would be more efficient if we could hold some internal
  52. /// resources used for the calculation and reuse it over multiple calls to
  53. /// \c calculate() (a concrete implementation in this library actually does
  54. /// this); Second, we may want to customize the hash calculation logic for
  55. /// testing purposes or for other future extensions. For example, we may
  56. /// want to use a fake calculator for tests that returns pre-defined hash
  57. /// values (so a slight change to the test input wouldn't affect the test
  58. /// result). Using classes from this base would make it possible more
  59. /// transparently to the application.
  60. ///
  61. /// A specific derived class instance must be created by the factory method,
  62. /// \c create().
  63. ///
  64. /// There can be several ways to extend this class in future. Those include:
  65. /// - Allow customizing the factory method so the application change the
  66. /// behavior dynamically.
  67. /// - Allow to construct the class from a tuple of parameters, that is,
  68. /// integers for algorithm, iterations and flags, and opaque salt data.
  69. /// For example, we might want to use that version for validators.
  70. /// - Allow producing hash value as binary data
  71. /// - Allow updating NSEC3 parameters of a class object so we can still reuse
  72. /// the internal resources for different sets of parameters.
  73. class NSEC3Hash {
  74. protected:
  75. /// \brief The default constructor.
  76. ///
  77. /// This is defined as protected to prevent this class from being directly
  78. /// instantiated even if the class definition is modified (accidentally
  79. /// or intentionally) to have no pure virtual methods.
  80. NSEC3Hash() {}
  81. public:
  82. /// \brief Factory method of NSECHash from NSEC3PARAM RDATA.
  83. ///
  84. /// The hash algorithm given via \c param must be known to the
  85. /// implementation. Otherwise \c UnknownNSEC3HashAlgorithm exception
  86. /// will be thrown.
  87. ///
  88. /// This method creates an \c NSEC3Hash object using \c new. The caller
  89. /// is responsible for releasing it with \c delete that is compatible to
  90. /// the one used in this library. In practice, the application would
  91. /// generally need to store the returned pointer in some form of smart
  92. /// pointer; otherwise the resulting code will be quite fragile against
  93. /// exceptions (and in this case the application doesn't have to worry
  94. /// about explicit \c delete).
  95. ///
  96. /// \throw UnknownNSEC3HashAlgorithm The specified algorithm in \c param
  97. /// is unknown.
  98. /// \throw std::bad_alloc Internal resource allocation failure.
  99. ///
  100. /// \param param NSEC3 parameters used for subsequent calculation.
  101. /// \return A pointer to a concrete derived object of \c NSEC3Hash.
  102. static NSEC3Hash* create(const rdata::generic::NSEC3PARAM& param);
  103. /// \brief The destructor.
  104. virtual ~NSEC3Hash() {}
  105. /// \brief Calculate the NSEC3 hash.
  106. ///
  107. /// This method calculates the NSEC3 hash value for the given \c name
  108. /// with the hash parameters (algorithm, iterations and salt) given at
  109. /// construction, and returns the value as a base32hex-encoded string
  110. /// (without containing any white spaces). All US-ASCII letters in the
  111. /// string will be upper cased.
  112. ///
  113. /// \param name The domain name for which the hash value is to be
  114. /// calculated.
  115. /// \return Base32hex-encoded string of the hash value.
  116. virtual std::string calculate(const Name& name) const = 0;
  117. /// \brief Match given NSEC3 parameters with that of the hash.
  118. ///
  119. /// This method compares NSEC3 parameters used for hash calculation
  120. /// in the object with those in the given NSEC3 RDATA, and return
  121. /// true iff they completely match. In the current implementation
  122. /// only the algorithm, iterations and salt are compared; the flags
  123. /// are ignored (as they don't affect hash calculation per RFC5155).
  124. ///
  125. /// \throw None
  126. ///
  127. /// \param nsec3 An NSEC3 RDATA object whose hash parameters are to be
  128. /// matched
  129. /// \return true If the given parameters match the local ones; false
  130. /// otherwise.
  131. virtual bool match(const rdata::generic::NSEC3& nsec3) const = 0;
  132. };
  133. }
  134. }
  135. #endif // __NSEC3HASH_H
  136. // Local Variables:
  137. // mode: c++
  138. // End: