apr_crypto.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_CRYPTO_H
  17. #define APR_CRYPTO_H
  18. #include "apu.h"
  19. #include "apr_pools.h"
  20. #include "apr_tables.h"
  21. #include "apr_hash.h"
  22. #include "apu_errno.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @file apr_crypto.h
  28. * @brief APR-UTIL Crypto library
  29. */
  30. /**
  31. * @defgroup APR_Util_Crypto Crypto routines
  32. * @ingroup APR_Util
  33. * @{
  34. */
  35. #if APU_HAVE_CRYPTO
  36. #ifndef APU_CRYPTO_RECOMMENDED_DRIVER
  37. #if APU_HAVE_OPENSSL
  38. #define APU_CRYPTO_RECOMMENDED_DRIVER "openssl"
  39. #else
  40. #if APU_HAVE_NSS
  41. #define APU_CRYPTO_RECOMMENDED_DRIVER "nss"
  42. #else
  43. #if APU_HAVE_MSCNG
  44. #define APU_CRYPTO_RECOMMENDED_DRIVER "mscng"
  45. #else
  46. #if APU_HAVE_MSCAPI
  47. #define APU_CRYPTO_RECOMMENDED_DRIVER "mscapi"
  48. #else
  49. #endif
  50. #endif
  51. #endif
  52. #endif
  53. #endif
  54. /**
  55. * Symmetric Key types understood by the library.
  56. *
  57. * NOTE: It is expected that this list will grow over time.
  58. *
  59. * Interoperability Matrix:
  60. *
  61. * The matrix is based on the testcrypto.c unit test, which attempts to
  62. * test whether a simple encrypt/decrypt will succeed, as well as testing
  63. * whether an encrypted string by one library can be decrypted by the
  64. * others.
  65. *
  66. * Some libraries will successfully encrypt and decrypt their own data,
  67. * but won't decrypt data from another library. It is hoped that over
  68. * time these anomalies will be found and fixed, but until then it is
  69. * recommended that ciphers are chosen that interoperate across platform.
  70. *
  71. * An X below means the test passes, it does not necessarily mean that
  72. * encryption performed is correct or secure. Applications should stick
  73. * to ciphers that pass the interoperablity tests on the right hand side
  74. * of the table.
  75. *
  76. * Aligned data is data whose length is a multiple of the block size for
  77. * the chosen cipher. Padded data is data that is not aligned by block
  78. * size and must be padded by the crypto library.
  79. *
  80. * OpenSSL NSS Interop
  81. * Align Pad Align Pad Align Pad
  82. * 3DES_192/CBC X X X X X X
  83. * 3DES_192/ECB X X
  84. * AES_256/CBC X X X X X X
  85. * AES_256/ECB X X X X
  86. * AES_192/CBC X X X X
  87. * AES_192/ECB X X X
  88. * AES_128/CBC X X X X
  89. * AES_128/ECB X X X
  90. *
  91. * Conclusion: for padded data, use 3DES_192/CBC or AES_256/CBC. For
  92. * aligned data, use 3DES_192/CBC, AES_256/CBC or AES_256/ECB.
  93. */
  94. typedef enum
  95. {
  96. APR_KEY_NONE, APR_KEY_3DES_192, /** 192 bit (3-Key) 3DES */
  97. APR_KEY_AES_128, /** 128 bit AES */
  98. APR_KEY_AES_192, /** 192 bit AES */
  99. APR_KEY_AES_256
  100. /** 256 bit AES */
  101. } apr_crypto_block_key_type_e;
  102. typedef enum
  103. {
  104. APR_MODE_NONE, /** An error condition */
  105. APR_MODE_ECB, /** Electronic Code Book */
  106. APR_MODE_CBC
  107. /** Cipher Block Chaining */
  108. } apr_crypto_block_key_mode_e;
  109. /* These are opaque structs. Instantiation is up to each backend */
  110. typedef struct apr_crypto_driver_t apr_crypto_driver_t;
  111. typedef struct apr_crypto_t apr_crypto_t;
  112. typedef struct apr_crypto_config_t apr_crypto_config_t;
  113. typedef struct apr_crypto_key_t apr_crypto_key_t;
  114. typedef struct apr_crypto_block_t apr_crypto_block_t;
  115. /**
  116. * @brief Perform once-only initialisation. Call once only.
  117. *
  118. * @param pool - pool to register any shutdown cleanups, etc
  119. * @return APR_NOTIMPL in case of no crypto support.
  120. */
  121. APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool);
  122. /**
  123. * @brief Register a cleanup to zero out the buffer provided
  124. * when the pool is cleaned up.
  125. *
  126. * @param pool - pool to register the cleanup
  127. * @param buffer - buffer to zero out
  128. * @param size - size of the buffer to zero out
  129. */
  130. APU_DECLARE(apr_status_t) apr_crypto_clear(apr_pool_t *pool, void *buffer,
  131. apr_size_t size);
  132. /**
  133. * @brief Get the driver struct for a name
  134. *
  135. * @param driver - pointer to driver struct.
  136. * @param name - driver name
  137. * @param params - array of initialisation parameters
  138. * @param result - result and error message on failure
  139. * @param pool - (process) pool to register cleanup
  140. * @return APR_SUCCESS for success
  141. * @return APR_ENOTIMPL for no driver (when DSO not enabled)
  142. * @return APR_EDSOOPEN if DSO driver file can't be opened
  143. * @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver
  144. * @remarks NSS: the params can have "dir", "key3", "cert7" and "secmod"
  145. * keys, each followed by an equal sign and a value. Such key/value pairs can
  146. * be delimited by space or tab. If the value contains a space, surround the
  147. * whole key value pair in quotes: "dir=My Directory".
  148. * @remarks OpenSSL: currently no params are supported.
  149. */
  150. APU_DECLARE(apr_status_t) apr_crypto_get_driver(
  151. const apr_crypto_driver_t **driver,
  152. const char *name, const char *params, const apu_err_t **result,
  153. apr_pool_t *pool);
  154. /**
  155. * @brief Return the name of the driver.
  156. *
  157. * @param driver - The driver in use.
  158. * @return The name of the driver.
  159. */
  160. APU_DECLARE(const char *) apr_crypto_driver_name(
  161. const apr_crypto_driver_t *driver);
  162. /**
  163. * @brief Get the result of the last operation on a context. If the result
  164. * is NULL, the operation was successful.
  165. * @param result - the result structure
  166. * @param f - context pointer
  167. * @return APR_SUCCESS for success
  168. */
  169. APU_DECLARE(apr_status_t) apr_crypto_error(const apu_err_t **result,
  170. const apr_crypto_t *f);
  171. /**
  172. * @brief Create a context for supporting encryption. Keys, certificates,
  173. * algorithms and other parameters will be set per context. More than
  174. * one context can be created at one time. A cleanup will be automatically
  175. * registered with the given pool to guarantee a graceful shutdown.
  176. * @param f - context pointer will be written here
  177. * @param driver - driver to use
  178. * @param params - array of key parameters
  179. * @param pool - process pool
  180. * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE
  181. * if the engine cannot be initialised.
  182. * @remarks NSS: currently no params are supported.
  183. * @remarks OpenSSL: the params can have "engine" as a key, followed by an equal
  184. * sign and a value.
  185. */
  186. APU_DECLARE(apr_status_t) apr_crypto_make(apr_crypto_t **f,
  187. const apr_crypto_driver_t *driver, const char *params,
  188. apr_pool_t *pool);
  189. /**
  190. * @brief Get a hash table of key types, keyed by the name of the type against
  191. * an integer pointer constant.
  192. *
  193. * @param types - hashtable of key types keyed to constants.
  194. * @param f - encryption context
  195. * @return APR_SUCCESS for success
  196. */
  197. APU_DECLARE(apr_status_t) apr_crypto_get_block_key_types(apr_hash_t **types,
  198. const apr_crypto_t *f);
  199. /**
  200. * @brief Get a hash table of key modes, keyed by the name of the mode against
  201. * an integer pointer constant.
  202. *
  203. * @param modes - hashtable of key modes keyed to constants.
  204. * @param f - encryption context
  205. * @return APR_SUCCESS for success
  206. */
  207. APU_DECLARE(apr_status_t) apr_crypto_get_block_key_modes(apr_hash_t **modes,
  208. const apr_crypto_t *f);
  209. /**
  210. * @brief Create a key from the given passphrase. By default, the PBKDF2
  211. * algorithm is used to generate the key from the passphrase. It is expected
  212. * that the same pass phrase will generate the same key, regardless of the
  213. * backend crypto platform used. The key is cleaned up when the context
  214. * is cleaned, and may be reused with multiple encryption or decryption
  215. * operations.
  216. * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
  217. * *key is not NULL, *key must point at a previously created structure.
  218. * @param key The key returned, see note.
  219. * @param ivSize The size of the initialisation vector will be returned, based
  220. * on whether an IV is relevant for this type of crypto.
  221. * @param pass The passphrase to use.
  222. * @param passLen The passphrase length in bytes
  223. * @param salt The salt to use.
  224. * @param saltLen The salt length in bytes
  225. * @param type 3DES_192, AES_128, AES_192, AES_256.
  226. * @param mode Electronic Code Book / Cipher Block Chaining.
  227. * @param doPad Pad if necessary.
  228. * @param iterations Number of iterations to use in algorithm
  229. * @param f The context to use.
  230. * @param p The pool to use.
  231. * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
  232. * error occurred while generating the key. APR_ENOCIPHER if the type or mode
  233. * is not supported by the particular backend. APR_EKEYTYPE if the key type is
  234. * not known. APR_EPADDING if padding was requested but is not supported.
  235. * APR_ENOTIMPL if not implemented.
  236. */
  237. APU_DECLARE(apr_status_t) apr_crypto_passphrase(apr_crypto_key_t **key,
  238. apr_size_t *ivSize, const char *pass, apr_size_t passLen,
  239. const unsigned char * salt, apr_size_t saltLen,
  240. const apr_crypto_block_key_type_e type,
  241. const apr_crypto_block_key_mode_e mode, const int doPad,
  242. const int iterations, const apr_crypto_t *f, apr_pool_t *p);
  243. /**
  244. * @brief Initialise a context for encrypting arbitrary data using the given key.
  245. * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
  246. * *ctx is not NULL, *ctx must point at a previously created structure.
  247. * @param ctx The block context returned, see note.
  248. * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
  249. * an IV will be created at random, in space allocated from the pool.
  250. * If the buffer pointed to is not NULL, the IV in the buffer will be
  251. * used.
  252. * @param key The key structure to use.
  253. * @param blockSize The block size of the cipher.
  254. * @param p The pool to use.
  255. * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
  256. * Returns APR_EINIT if the backend failed to initialise the context. Returns
  257. * APR_ENOTIMPL if not implemented.
  258. */
  259. APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_init(
  260. apr_crypto_block_t **ctx, const unsigned char **iv,
  261. const apr_crypto_key_t *key, apr_size_t *blockSize, apr_pool_t *p);
  262. /**
  263. * @brief Encrypt data provided by in, write it to out.
  264. * @note The number of bytes written will be written to outlen. If
  265. * out is NULL, outlen will contain the maximum size of the
  266. * buffer needed to hold the data, including any data
  267. * generated by apr_crypto_block_encrypt_finish below. If *out points
  268. * to NULL, a buffer sufficiently large will be created from
  269. * the pool provided. If *out points to a not-NULL value, this
  270. * value will be used as a buffer instead.
  271. * @param out Address of a buffer to which data will be written,
  272. * see note.
  273. * @param outlen Length of the output will be written here.
  274. * @param in Address of the buffer to read.
  275. * @param inlen Length of the buffer to read.
  276. * @param ctx The block context to use.
  277. * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
  278. * not implemented.
  279. */
  280. APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(unsigned char **out,
  281. apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
  282. apr_crypto_block_t *ctx);
  283. /**
  284. * @brief Encrypt final data block, write it to out.
  285. * @note If necessary the final block will be written out after being
  286. * padded. Typically the final block will be written to the
  287. * same buffer used by apr_crypto_block_encrypt, offset by the
  288. * number of bytes returned as actually written by the
  289. * apr_crypto_block_encrypt() call. After this call, the context
  290. * is cleaned and can be reused by apr_crypto_block_encrypt_init().
  291. * @param out Address of a buffer to which data will be written. This
  292. * buffer must already exist, and is usually the same
  293. * buffer used by apr_evp_crypt(). See note.
  294. * @param outlen Length of the output will be written here.
  295. * @param ctx The block context to use.
  296. * @return APR_ECRYPT if an error occurred.
  297. * @return APR_EPADDING if padding was enabled and the block was incorrectly
  298. * formatted.
  299. * @return APR_ENOTIMPL if not implemented.
  300. */
  301. APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish(unsigned char *out,
  302. apr_size_t *outlen, apr_crypto_block_t *ctx);
  303. /**
  304. * @brief Initialise a context for decrypting arbitrary data using the given key.
  305. * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
  306. * *ctx is not NULL, *ctx must point at a previously created structure.
  307. * @param ctx The block context returned, see note.
  308. * @param blockSize The block size of the cipher.
  309. * @param iv Optional initialisation vector.
  310. * @param key The key structure to use.
  311. * @param p The pool to use.
  312. * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
  313. * Returns APR_EINIT if the backend failed to initialise the context. Returns
  314. * APR_ENOTIMPL if not implemented.
  315. */
  316. APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_init(
  317. apr_crypto_block_t **ctx, apr_size_t *blockSize,
  318. const unsigned char *iv, const apr_crypto_key_t *key, apr_pool_t *p);
  319. /**
  320. * @brief Decrypt data provided by in, write it to out.
  321. * @note The number of bytes written will be written to outlen. If
  322. * out is NULL, outlen will contain the maximum size of the
  323. * buffer needed to hold the data, including any data
  324. * generated by apr_crypto_block_decrypt_finish below. If *out points
  325. * to NULL, a buffer sufficiently large will be created from
  326. * the pool provided. If *out points to a not-NULL value, this
  327. * value will be used as a buffer instead.
  328. * @param out Address of a buffer to which data will be written,
  329. * see note.
  330. * @param outlen Length of the output will be written here.
  331. * @param in Address of the buffer to read.
  332. * @param inlen Length of the buffer to read.
  333. * @param ctx The block context to use.
  334. * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
  335. * not implemented.
  336. */
  337. APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(unsigned char **out,
  338. apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
  339. apr_crypto_block_t *ctx);
  340. /**
  341. * @brief Decrypt final data block, write it to out.
  342. * @note If necessary the final block will be written out after being
  343. * padded. Typically the final block will be written to the
  344. * same buffer used by apr_crypto_block_decrypt, offset by the
  345. * number of bytes returned as actually written by the
  346. * apr_crypto_block_decrypt() call. After this call, the context
  347. * is cleaned and can be reused by apr_crypto_block_decrypt_init().
  348. * @param out Address of a buffer to which data will be written. This
  349. * buffer must already exist, and is usually the same
  350. * buffer used by apr_evp_crypt(). See note.
  351. * @param outlen Length of the output will be written here.
  352. * @param ctx The block context to use.
  353. * @return APR_ECRYPT if an error occurred.
  354. * @return APR_EPADDING if padding was enabled and the block was incorrectly
  355. * formatted.
  356. * @return APR_ENOTIMPL if not implemented.
  357. */
  358. APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish(unsigned char *out,
  359. apr_size_t *outlen, apr_crypto_block_t *ctx);
  360. /**
  361. * @brief Clean encryption / decryption context.
  362. * @note After cleanup, a context is free to be reused if necessary.
  363. * @param ctx The block context to use.
  364. * @return Returns APR_ENOTIMPL if not supported.
  365. */
  366. APU_DECLARE(apr_status_t) apr_crypto_block_cleanup(apr_crypto_block_t *ctx);
  367. /**
  368. * @brief Clean encryption / decryption context.
  369. * @note After cleanup, a context is free to be reused if necessary.
  370. * @param f The context to use.
  371. * @return Returns APR_ENOTIMPL if not supported.
  372. */
  373. APU_DECLARE(apr_status_t) apr_crypto_cleanup(apr_crypto_t *f);
  374. /**
  375. * @brief Shutdown the crypto library.
  376. * @note After shutdown, it is expected that the init function can be called again.
  377. * @param driver - driver to use
  378. * @return Returns APR_ENOTIMPL if not supported.
  379. */
  380. APU_DECLARE(apr_status_t) apr_crypto_shutdown(
  381. const apr_crypto_driver_t *driver);
  382. #endif /* APU_HAVE_CRYPTO */
  383. /** @} */
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif