crypto.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2011 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 _ISC_CRYPTO_H
  15. #define _ISC_CRYPTO_H
  16. #include <string>
  17. #include <dns/buffer.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/noncopyable.hpp>
  20. #include <cryptolink/crypto_hmac.h>
  21. namespace isc {
  22. namespace cryptolink {
  23. /// General exception class that is the base for all crypto-related
  24. /// exceptions
  25. class CryptoLinkError : public Exception {
  26. public:
  27. CryptoLinkError(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what) {}
  29. };
  30. /// This exception is thrown if there was a problem initializing the
  31. /// crypto library
  32. class InitializationError : public CryptoLinkError {
  33. public:
  34. InitializationError(const char* file, size_t line, const char* what) :
  35. CryptoLinkError(file, line, what) {}
  36. };
  37. /// This exception is thrown when a cryptographic action is requested
  38. /// for an algorithm that is not supported by the underlying library.
  39. class UnsupportedAlgorithm : public CryptoLinkError {
  40. public:
  41. UnsupportedAlgorithm(const char* file, size_t line, const char* what) :
  42. CryptoLinkError(file, line, what) {}
  43. };
  44. /// This exception is thrown when the underlying library could not
  45. /// handle the key data.
  46. class BadKey : public CryptoLinkError {
  47. public:
  48. BadKey(const char* file, size_t line, const char* what) :
  49. CryptoLinkError(file, line, what) {}
  50. };
  51. /// This exception is raised when a general error that was not
  52. /// specifically caught is thrown by the underlying library. It
  53. /// is replaced by this one so as not have 'external' exceptions
  54. /// bubbling up
  55. class LibraryError : public CryptoLinkError {
  56. public:
  57. LibraryError(const char* file, size_t line, const char* what) :
  58. CryptoLinkError(file, line, what) {}
  59. };
  60. /// Forward declaration for pimpl
  61. class CryptoLinkImpl;
  62. /// \brief
  63. ///
  64. /// This is singleton class that serves as the entry point to
  65. /// the underlying cryptography library, and as a factory for objects
  66. /// within the cryptolink library.
  67. ///
  68. /// There is only one way to access it, through getCryptoLink(), which
  69. /// returns a reference to the initialized library. On the first call,
  70. /// it will be initialized automatically. You can however initialize it
  71. /// manually through a call to the initalize(), before your first call
  72. /// to getCryptoLink. Any subsequent call to initialize() will be a
  73. /// noop.
  74. ///
  75. /// \note All other classes within cryptolink should have private
  76. /// constructors as well, and should have a factory function from this
  77. /// class.
  78. ///
  79. // Internal note: we can use this class later to initialize and manage
  80. // dynamic (PKCS#11) libs
  81. class CryptoLink : private boost::noncopyable {
  82. public:
  83. /// \brief Returns a reference to the singleton instance
  84. ///
  85. /// If the library has not been initialized yet, it will be
  86. /// initialized with some default values.
  87. ///
  88. /// Since this class is noncopyable, you must use the return
  89. /// value directly, or store it in a reference variable.
  90. ///
  91. /// \exception InitializationError if initialization fails
  92. ///
  93. /// \return Reference to the singleton instance
  94. static CryptoLink& getCryptoLink();
  95. /// \brief Initialize the library manually
  96. ///
  97. /// If the library has already been initialized (either by a call
  98. /// to initialize() or automatically in getCryptoLink()), this
  99. /// function does nothing.
  100. ///
  101. /// \note A call to initialize() is not strictly necessary with
  102. /// the current implementation.
  103. ///
  104. /// \exception InitializationError if initialization fails
  105. ///
  106. static void initialize();
  107. /// \brief Factory function for HMAC objects
  108. ///
  109. /// CryptoLink objects cannot be constructed directly. This
  110. /// function creates a new HMAC object usable for signing or
  111. /// verification.
  112. ///
  113. /// The caller is responsible for deleting the object, and it is
  114. /// therefore highly recommended to place the return value of this
  115. /// function in a scoped_ptr or shared_ptr.
  116. ///
  117. /// Notes: if the secret is longer than the block size of its
  118. /// algorithm, the constructor will run it through the hash
  119. /// algorithm, and use the digest as the secret for this HMAC
  120. /// operation
  121. ///
  122. /// \exception UnsupportedAlgorithmException if the given algorithm
  123. /// is unknown or not supported by the underlying library
  124. /// \exception InvalidKeyLength if the given key secret_len is bad
  125. /// \exception LibraryError if there was any unexpected exception
  126. /// in the underlying library
  127. ///
  128. /// \param secret The secret to sign with
  129. /// \param secret_len The length of the secret
  130. /// \param hash_algorithm The hash algorithm
  131. HMAC* createHMAC(const void* secret, size_t secret_len,
  132. const HMAC::HashAlgorithm hash_algorithm);
  133. private:
  134. // To enable us to use an optional explicit initialization call,
  135. // the 'real' instance getter is private
  136. static CryptoLink& getCryptoLinkInternal();
  137. // To prevent people constructing their own, we make the constructor
  138. // private too.
  139. CryptoLink() : impl_(NULL) {};
  140. ~CryptoLink();
  141. CryptoLinkImpl* impl_;
  142. };
  143. } // namespace cryptolink
  144. } // namespace isc
  145. #endif // _ISC_CRYPTO_H