ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

272 lines
7.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_CRYPTO_H
  9. #define CPPCMS_CRYPTO_H
  10. #include <cppcms/defs.h>
  11. #include <booster/noncopyable.h>
  12. #include <booster/hold_ptr.h>
  13. #include <booster/auto_ptr_inc.h>
  14. #include <string>
  15. namespace cppcms {
  16. ///
  17. /// \brief This namespace holds basic cryptographic utilities useful for save interaction with user.
  18. ///
  19. /// One of the limitations of these functions is the fact that they do not use so called cryptographic memory,
  20. /// however it is not required as all secret keys are stored as plain text in configuration files,
  21. /// The only protection that is given is that values of keys are erased when the object is deleted
  22. /// to prevent key-leaks due to use of uninitialized memory in user applications.
  23. ///
  24. ///
  25. namespace crypto {
  26. ///
  27. /// \brief Key object, holds the string that represents the binary key.
  28. ///
  29. /// When the key is destroyed it zeros all the memory it uses to prevent accidental
  30. /// leaks of the highly confidential data
  31. ///
  32. class CPPCMS_API key {
  33. public:
  34. ///
  35. /// Create an empty key on 0 length
  36. ///
  37. key();
  38. ///
  39. /// Copy the key
  40. ///
  41. key(key const &other);
  42. ///
  43. /// Assign the key
  44. ///
  45. key const &operator=(key const &);
  46. ///
  47. /// Destroy they key object clearing the area used by it.
  48. ///
  49. ~key();
  50. ///
  51. /// Create a key using binary representation pointed by \a data of \a length bytes
  52. ///
  53. key(void const *data,size_t length);
  54. ///
  55. /// Create a key using the hexadecimal representation
  56. ///
  57. explicit key(char const *s);
  58. ///
  59. /// Create a key using the hexadecimal representation
  60. ///
  61. explicit key(std::string const &);
  62. ///
  63. /// Get the pointer to data, never returns NULL even if size() == 0
  64. ///
  65. char const *data() const;
  66. ///
  67. /// Get the size of the key
  68. ///
  69. size_t size() const;
  70. ///
  71. /// Clear the key - and clear the area it was stored it
  72. ///
  73. void reset();
  74. ///
  75. /// Set the binary value for the key
  76. ///
  77. void set(void const *ptr,size_t len);
  78. ///
  79. /// Set the value for the key using hexadecimal representation
  80. ///
  81. void set_hex(char const *ptr,size_t len);
  82. ///
  83. /// Read the key from file. Under windows file_name should be UTF-8 encoded
  84. /// string.
  85. ///
  86. void read_from_file(std::string const &file_name);
  87. private:
  88. static unsigned from_hex(char c);
  89. char *data_;
  90. size_t size_;
  91. };
  92. ///
  93. /// \brief this class provides an API to calculate various cryptographic hash functions
  94. ///
  95. class CPPCMS_API message_digest : public booster::noncopyable {
  96. protected:
  97. /// It should be implemented in derived classes
  98. message_digest()
  99. {
  100. }
  101. public:
  102. virtual ~message_digest()
  103. {
  104. }
  105. ///
  106. /// Get the size of message digest, for example for MD5 it is 16, for SHA1 it is 20
  107. ///
  108. virtual unsigned digest_size() const = 0;
  109. ///
  110. /// Get processing block size, returns 64 or 128, used mostly for correct HMAC calculations
  111. ///
  112. virtual unsigned block_size() const = 0;
  113. ///
  114. /// Add more data of size bytes for processing
  115. ///
  116. virtual void append(void const *ptr,size_t size) = 0;
  117. ///
  118. /// Read the message digest for the data and reset it into initial state,
  119. /// provided buffer must be digest_size() bytes
  120. ///
  121. virtual void readout(void *ptr) = 0;
  122. ///
  123. /// Make a polymorphic copy of this object, note the state of copied object is reset to
  124. /// initial
  125. ///
  126. virtual message_digest *clone() const = 0;
  127. ///
  128. /// Get the name of the hash function
  129. ///
  130. virtual char const *name() const = 0;
  131. ///
  132. /// Create MD5 message digest
  133. ///
  134. static std::auto_ptr<message_digest> md5();
  135. ///
  136. /// Create SHA1 message digest
  137. ///
  138. static std::auto_ptr<message_digest> sha1();
  139. ///
  140. /// Create message digest by name, more then sha1 and md5 may be supported,
  141. /// if CppCMS is compiled with cryptography library like libgcrypt or openssl
  142. ///
  143. static std::auto_ptr<message_digest> create_by_name(std::string const &name);
  144. };
  145. ///
  146. /// \brief This object calculates the HMAC signature for the input data
  147. ///
  148. class CPPCMS_API hmac : public booster::noncopyable {
  149. public:
  150. ///
  151. /// Create hmac that uses given \a digest algorithm and a binary key - \a key
  152. ///
  153. hmac(std::auto_ptr<message_digest> digest,key const &k);
  154. ///
  155. /// Create hmac that uses message digest algorithm called \a name and use a binary key - \a key
  156. ///
  157. hmac(std::string const &name,key const &k);
  158. ~hmac();
  159. ///
  160. /// Get the size of the signtature
  161. ///
  162. unsigned digest_size() const;
  163. ///
  164. /// Add data for signing
  165. ///
  166. void append(void const *ptr,size_t size);
  167. ///
  168. /// Get the signature for all the data, after calling this function
  169. /// the state of the hmac is reset and it can be used for signing again
  170. ///
  171. /// Note: provided buffer must be digest_size() bytes long.
  172. ///
  173. void readout(void *ptr);
  174. private:
  175. void init();
  176. struct data_;
  177. booster::hold_ptr<data_> d;
  178. std::auto_ptr<message_digest> md_,md_opad_;
  179. key key_;
  180. };
  181. ///
  182. /// \brief Cipher-block chaining encryption and decryption cryptographic service.
  183. ///
  184. /// \note In order to use it, you \b must compile CppCMS with OpenSSL (libcrypto) or GNU-TLS (libgcrypt) library.
  185. ///
  186. class CPPCMS_API cbc : public booster::noncopyable {
  187. public:
  188. ///
  189. /// CBC encryption type
  190. ///
  191. typedef enum {
  192. aes128 = 0, ///< AES-128
  193. aes192 = 1, ///< AES-192
  194. aes256 = 2 ///< AES-256
  195. } cbc_type;
  196. ///
  197. /// Create a new cbc object that performs encryption using \a type method.
  198. ///
  199. /// If the encryption method is not supported returns an empty pointer!
  200. ///
  201. static std::auto_ptr<cbc> create(cbc_type type);
  202. ///
  203. /// Create a new cbc object that performs encryption using algorithm \a name
  204. ///
  205. /// If the encryption method is not supported returns an empty pointer!
  206. ///
  207. /// Currently supported aes128, aes192, aes256, with names "aes" = "aes-128" = "aes128" , "aes-192" "aes192",
  208. /// "aes-256" = "aes256". They require CppCMS to be compiled with OpenSSL or GNU-TLS library
  209. ///
  210. static std::auto_ptr<cbc> create(std::string const &name);
  211. ///
  212. /// Get the size of the block CBC works on
  213. ///
  214. virtual unsigned block_size() const = 0;
  215. ///
  216. /// Get the required key size in bytes
  217. ///
  218. virtual unsigned key_size() const = 0;
  219. ///
  220. /// Set the key value
  221. ///
  222. virtual void set_key(key const &) = 0;
  223. ///
  224. /// Set initial vector value, size should be equal to block_size()
  225. ///
  226. virtual void set_iv(void const *ptr,size_t size) = 0;
  227. ///
  228. /// Set randomly created initial vector value
  229. ///
  230. virtual void set_nonce_iv() = 0;
  231. ///
  232. /// Encrypt the data \a in to \a out of size \a len. \a len should be multiple of block_size()
  233. ///
  234. virtual void encrypt(void const *in,void *out,unsigned len) = 0;
  235. ///
  236. /// Decrypt the data \a in to \a out of size \a len. \a len should be multiple of block_size()
  237. ///
  238. virtual void decrypt(void const *in,void *out,unsigned len) = 0;
  239. virtual ~cbc()
  240. {
  241. }
  242. };
  243. } // crypto
  244. } // cppcms
  245. #endif