summaryrefslogtreecommitdiff
path: root/crypto/apr_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/apr_crypto.c')
-rw-r--r--crypto/apr_crypto.c280
1 files changed, 181 insertions, 99 deletions
diff --git a/crypto/apr_crypto.c b/crypto/apr_crypto.c
index ef843102..2ca391a0 100644
--- a/crypto/apr_crypto.c
+++ b/crypto/apr_crypto.c
@@ -19,7 +19,6 @@
#include "apu_config.h"
#include "apu.h"
-
#include "apr_pools.h"
#include "apr_dso.h"
#include "apr_strings.h"
@@ -40,6 +39,34 @@ static apr_hash_t *drivers = NULL;
#define CLEANUP_CAST (apr_status_t (*)(void*))
+#define APR_TYPEDEF_STRUCT(type, incompletion) \
+struct type { \
+ incompletion \
+ void *unk[]; \
+};
+
+APR_TYPEDEF_STRUCT(apr_crypto_t,
+ apr_pool_t *pool;
+ apr_crypto_driver_t *provider;
+)
+
+APR_TYPEDEF_STRUCT(apr_crypto_key_t,
+ apr_pool_t *pool;
+ apr_crypto_driver_t *provider;
+ const apr_crypto_t *f;
+)
+
+APR_TYPEDEF_STRUCT(apr_crypto_block_t,
+ apr_pool_t *pool;
+ apr_crypto_driver_t *provider;
+ const apr_crypto_t *f;
+)
+
+typedef struct apr_crypto_clear_t {
+ void *buffer;
+ apr_size_t size;
+} apr_crypto_clear_t;
+
#if !APU_DSO_BUILD
#define DRIVER_LOAD(name,driver,pool,params) \
{ \
@@ -51,7 +78,8 @@ static apr_hash_t *drivers = NULL;
}
#endif
-static apr_status_t apr_crypto_term(void *ptr) {
+static apr_status_t apr_crypto_term(void *ptr)
+{
/* set drivers to NULL so init can work again */
drivers = NULL;
@@ -61,8 +89,8 @@ static apr_status_t apr_crypto_term(void *ptr) {
return APR_SUCCESS;
}
-APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool,
- const apr_array_header_t *params) {
+APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool)
+{
apr_status_t ret = APR_SUCCESS;
apr_pool_t *parent;
@@ -101,9 +129,35 @@ APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool,
return ret;
}
-APU_DECLARE(apr_status_t) apr_crypto_get_driver(apr_pool_t *pool, const char *name,
- const apr_crypto_driver_t **driver, const apr_array_header_t *params,
- const apu_err_t **result) {
+static apr_status_t crypto_clear(void *ptr)
+{
+ apr_crypto_clear_t *clear = (apr_crypto_clear_t *)ptr;
+
+ memset(clear->buffer, 0, clear->size);
+ clear->buffer = NULL;
+ clear->size = 0;
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_crypto_clear(apr_pool_t *pool,
+ void *buffer, apr_size_t size)
+{
+ apr_crypto_clear_t *clear = apr_palloc(pool, sizeof(apr_crypto_clear_t));
+
+ clear->buffer = buffer;
+ clear->size = size;
+
+ apr_pool_cleanup_register(pool, clear, crypto_clear,
+ apr_pool_cleanup_null);
+
+ return APR_SUCCESS;
+}
+
+APU_DECLARE(apr_status_t) apr_crypto_get_driver(
+ const apr_crypto_driver_t **driver, const char *name,
+ const char *params, const apu_err_t **result, apr_pool_t *pool)
+{
#if APU_DSO_BUILD
char modname[32];
char symname[34];
@@ -121,7 +175,7 @@ APU_DECLARE(apr_status_t) apr_crypto_get_driver(apr_pool_t *pool, const char *na
#endif
*driver = apr_hash_get(drivers, name, APR_HASH_KEY_STRING);
if (*driver) {
-#if APU_DSO_BUILD
+#if APU_DSO_BUILD
apu_dso_mutex_unlock();
#endif
return APR_SUCCESS;
@@ -138,7 +192,8 @@ APU_DECLARE(apr_status_t) apr_crypto_get_driver(apr_pool_t *pool, const char *na
apr_snprintf(modname, sizeof(modname),
"apr_crypto_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".dll", name);
#else
- apr_snprintf(modname, sizeof(modname), "apr_crypto_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".so", name);
+ apr_snprintf(modname, sizeof(modname),
+ "apr_crypto_%s-" APU_STRINGIFY(APU_MAJOR_VERSION) ".so", name);
#endif
apr_snprintf(symname, sizeof(symname), "apr_crypto_%s_driver", name);
rv = apu_dso_load(&dso, &symbol, modname, symname, pool);
@@ -181,36 +236,75 @@ APU_DECLARE(apr_status_t) apr_crypto_get_driver(apr_pool_t *pool, const char *na
/**
* @brief Return the name of the driver.
*
- * @param pool - pool to register any shutdown cleanups, etc
- * @return APR_SUCCESS for success.
+ * @param driver - The driver in use.
+ * @return The name of the driver.
*/
-APU_DECLARE(const char *)apr_crypto_driver_name (const apr_crypto_driver_t *driver)
+APU_DECLARE(const char *)apr_crypto_driver_name (
+ const apr_crypto_driver_t *driver)
{
return driver->name;
}
/**
- * @brief Get the result of a previous operation on this context.
- * @param pool - process pool
- * @param params - array of key parameters
- * @param factory - factory pointer will be written here
+ * @brief Get the result of the last operation on a context. If the result
+ * is NULL, the operation was successful.
+ * @param result - the result structure
+ * @param f - context pointer
+ * @return APR_SUCCESS for success
*/
-APU_DECLARE(apr_status_t) apr_crypto_error(const apr_crypto_t *f,
- const apu_err_t **result) {
- *result = f->result;
- return APR_SUCCESS;
+APU_DECLARE(apr_status_t) apr_crypto_error(const apu_err_t **result,
+ const apr_crypto_t *f)
+{
+ return f->provider->error(result, f);
}
/**
- * @brief Create a general encryption context
+ * @brief Create a context for supporting encryption. Keys, certificates,
+ * algorithms and other parameters will be set per context. More than
+ * one context can be created at one time. A cleanup will be automatically
+ * registered with the given pool to guarantee a graceful shutdown.
+ * @param f - context pointer will be written here
* @param driver - driver to use
- * @param pool - process pool
* @param params - array of key parameters
- * @param factory - factory pointer will be written here
+ * @param pool - process pool
+ * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE
+ * if the engine cannot be initialised.
+ * @remarks NSS: currently no params are supported.
+ * @remarks OpenSSL: the params can have "engine" as a key, followed by an equal
+ * sign and a value.
+ */
+APU_DECLARE(apr_status_t) apr_crypto_make(apr_crypto_t **f,
+ const apr_crypto_driver_t *driver, const char *params, apr_pool_t *pool)
+{
+ return driver->make(f, driver, params, pool);
+}
+
+/**
+ * @brief Get a hash table of key types, keyed by the name of the type against
+ * an integer pointer constant.
+ *
+ * @param types - hashtable of key types keyed to constants.
+ * @param f - encryption context
+ * @return APR_SUCCESS for success
*/
-APU_DECLARE(apr_status_t) apr_crypto_factory(const apr_crypto_driver_t *driver,
- apr_pool_t *pool, const apr_array_header_t *params, apr_crypto_t **f) {
- return driver->factory(pool, params, f);
+APU_DECLARE(apr_status_t) apr_crypto_get_block_key_types(apr_hash_t **types,
+ const apr_crypto_t *f)
+{
+ return f->provider->get_block_key_types(types, f);
+}
+
+/**
+ * @brief Get a hash table of key modes, keyed by the name of the mode against
+ * an integer pointer constant.
+ *
+ * @param modes - hashtable of key modes keyed to constants.
+ * @param f - encryption context
+ * @return APR_SUCCESS for success
+ */
+APU_DECLARE(apr_status_t) apr_crypto_get_block_key_modes(apr_hash_t **modes,
+ const apr_crypto_t *f)
+{
+ return f->provider->get_block_key_modes(modes, f);
}
/**
@@ -222,9 +316,9 @@ APU_DECLARE(apr_status_t) apr_crypto_factory(const apr_crypto_driver_t *driver,
* operations.
* @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
* *key is not NULL, *key must point at a previously created structure.
- * @param driver - driver to use
- * @param p The pool to use.
- * @param f The context to use.
+ * @param key The key returned, see note.
+ * @param ivSize The size of the initialisation vector will be returned, based
+ * on whether an IV is relevant for this type of crypto.
* @param pass The passphrase to use.
* @param passLen The passphrase length in bytes
* @param salt The salt to use.
@@ -232,49 +326,47 @@ APU_DECLARE(apr_status_t) apr_crypto_factory(const apr_crypto_driver_t *driver,
* @param type 3DES_192, AES_128, AES_192, AES_256.
* @param mode Electronic Code Book / Cipher Block Chaining.
* @param doPad Pad if necessary.
- * @param key The key returned, see note.
- * @param ivSize The size of the initialisation vector will be returned, based
- * on whether an IV is relevant for this type of crypto.
+ * @param iterations Number of iterations to use in algorithm
+ * @param f The context to use.
+ * @param p The pool to use.
* @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
* error occurred while generating the key. APR_ENOCIPHER if the type or mode
* is not supported by the particular backend. APR_EKEYTYPE if the key type is
* not known. APR_EPADDING if padding was requested but is not supported.
* APR_ENOTIMPL if not implemented.
*/
-APU_DECLARE(apr_status_t) apr_crypto_passphrase(const apr_crypto_driver_t *driver,
- apr_pool_t *p, const apr_crypto_t *f, const char *pass,
- apr_size_t passLen, const unsigned char * salt, apr_size_t saltLen,
+APU_DECLARE(apr_status_t) apr_crypto_passphrase(apr_crypto_key_t **key,
+ apr_size_t *ivSize, const char *pass, apr_size_t passLen,
+ const unsigned char * salt, apr_size_t saltLen,
const apr_crypto_block_key_type_e type,
const apr_crypto_block_key_mode_e mode, const int doPad,
- const int iterations, apr_crypto_key_t **key, apr_size_t *ivSize) {
- return driver->passphrase(p, f, pass, passLen, salt, saltLen, type, mode,
- doPad, iterations, key, ivSize);
+ const int iterations, const apr_crypto_t *f, apr_pool_t *p)
+{
+ return f->provider->passphrase(key, ivSize, pass, passLen, salt, saltLen,
+ type, mode, doPad, iterations, f, p);
}
/**
* @brief Initialise a context for encrypting arbitrary data using the given key.
* @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
* *ctx is not NULL, *ctx must point at a previously created structure.
- * @param driver - driver to use
- * @param p The pool to use.
- * @param f The block factory to use.
- * @param key The key structure to use.
+ * @param ctx The block context returned, see note.
* @param iv Optional initialisation vector. If the buffer pointed to is NULL,
* an IV will be created at random, in space allocated from the pool.
* If the buffer pointed to is not NULL, the IV in the buffer will be
* used.
- * @param ctx The block context returned, see note.
+ * @param key The key structure to use.
* @param blockSize The block size of the cipher.
+ * @param p The pool to use.
* @return Returns APR_ENOIV if an initialisation vector is required but not specified.
* Returns APR_EINIT if the backend failed to initialise the context. Returns
* APR_ENOTIMPL if not implemented.
*/
APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_init(
- const apr_crypto_driver_t *driver, apr_pool_t *p,
- const apr_crypto_t *f, const apr_crypto_key_t *key,
- const unsigned char **iv, apr_crypto_block_t **ctx,
- apr_size_t *blockSize) {
- return driver->block_encrypt_init(p, f, key, iv, ctx, blockSize);
+ apr_crypto_block_t **ctx, const unsigned char **iv,
+ const apr_crypto_key_t *key, apr_size_t *blockSize, apr_pool_t *p)
+{
+ return key->provider->block_encrypt_init(ctx, iv, key, blockSize, p);
}
/**
@@ -286,21 +378,20 @@ APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_init(
* to NULL, a buffer sufficiently large will be created from
* the pool provided. If *out points to a not-NULL value, this
* value will be used as a buffer instead.
- * @param driver - driver to use
- * @param ctx The block context to use.
* @param out Address of a buffer to which data will be written,
* see note.
* @param outlen Length of the output will be written here.
* @param in Address of the buffer to read.
* @param inlen Length of the buffer to read.
+ * @param ctx The block context to use.
* @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
* not implemented.
*/
-APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(
- const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx,
- unsigned char **out, apr_size_t *outlen, const unsigned char *in,
- apr_size_t inlen) {
- return driver->block_encrypt(ctx, out, outlen, in, inlen);
+APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(unsigned char **out,
+ apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
+ apr_crypto_block_t *ctx)
+{
+ return ctx->provider->block_encrypt(out, outlen, in, inlen, ctx);
}
/**
@@ -311,44 +402,40 @@ APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(
* number of bytes returned as actually written by the
* apr_crypto_block_encrypt() call. After this call, the context
* is cleaned and can be reused by apr_crypto_block_encrypt_init().
- * @param driver - driver to use
- * @param ctx The block context to use.
* @param out Address of a buffer to which data will be written. This
* buffer must already exist, and is usually the same
* buffer used by apr_evp_crypt(). See note.
* @param outlen Length of the output will be written here.
+ * @param ctx The block context to use.
* @return APR_ECRYPT if an error occurred.
* @return APR_EPADDING if padding was enabled and the block was incorrectly
* formatted.
* @return APR_ENOTIMPL if not implemented.
*/
-APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish(
- const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx,
- unsigned char *out, apr_size_t *outlen) {
- return driver->block_encrypt_finish(ctx, out, outlen);
+APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish(unsigned char *out,
+ apr_size_t *outlen, apr_crypto_block_t *ctx)
+{
+ return ctx->provider->block_encrypt_finish(out, outlen, ctx);
}
/**
* @brief Initialise a context for decrypting arbitrary data using the given key.
* @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
* *ctx is not NULL, *ctx must point at a previously created structure.
- * @param driver - driver to use
- * @param p The pool to use.
- * @param f The block factory to use.
- * @param key The key structure to use.
- * @param iv Optional initialisation vector.
* @param ctx The block context returned, see note.
* @param blockSize The block size of the cipher.
+ * @param iv Optional initialisation vector.
+ * @param key The key structure to use.
+ * @param p The pool to use.
* @return Returns APR_ENOIV if an initialisation vector is required but not specified.
* Returns APR_EINIT if the backend failed to initialise the context. Returns
* APR_ENOTIMPL if not implemented.
*/
APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_init(
- const apr_crypto_driver_t *driver, apr_pool_t *p,
- const apr_crypto_t *f, const apr_crypto_key_t *key,
- const unsigned char *iv, apr_crypto_block_t **ctx,
- apr_size_t *blockSize) {
- return driver->block_decrypt_init(p, f, key, iv, ctx, blockSize);
+ apr_crypto_block_t **ctx, apr_size_t *blockSize,
+ const unsigned char *iv, const apr_crypto_key_t *key, apr_pool_t *p)
+{
+ return key->provider->block_decrypt_init(ctx, blockSize, iv, key, p);
}
/**
@@ -360,21 +447,20 @@ APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_init(
* to NULL, a buffer sufficiently large will be created from
* the pool provided. If *out points to a not-NULL value, this
* value will be used as a buffer instead.
- * @param driver - driver to use
- * @param ctx The block context to use.
* @param out Address of a buffer to which data will be written,
* see note.
* @param outlen Length of the output will be written here.
* @param in Address of the buffer to read.
* @param inlen Length of the buffer to read.
+ * @param ctx The block context to use.
* @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
* not implemented.
*/
-APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(
- const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx,
- unsigned char **out, apr_size_t *outlen, const unsigned char *in,
- apr_size_t inlen) {
- return driver->block_decrypt(ctx, out, outlen, in, inlen);
+APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(unsigned char **out,
+ apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
+ apr_crypto_block_t *ctx)
+{
+ return ctx->provider->block_decrypt(out, outlen, in, inlen, ctx);
}
/**
@@ -385,57 +471,53 @@ APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(
* number of bytes returned as actually written by the
* apr_crypto_block_decrypt() call. After this call, the context
* is cleaned and can be reused by apr_crypto_block_decrypt_init().
- * @param driver - driver to use
- * @param ctx The block context to use.
* @param out Address of a buffer to which data will be written. This
* buffer must already exist, and is usually the same
* buffer used by apr_evp_crypt(). See note.
* @param outlen Length of the output will be written here.
+ * @param ctx The block context to use.
* @return APR_ECRYPT if an error occurred.
* @return APR_EPADDING if padding was enabled and the block was incorrectly
* formatted.
* @return APR_ENOTIMPL if not implemented.
*/
-APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish(
- const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx,
- unsigned char *out, apr_size_t *outlen) {
- return driver->block_decrypt_finish(ctx, out, outlen);
+APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish(unsigned char *out,
+ apr_size_t *outlen, apr_crypto_block_t *ctx)
+{
+ return ctx->provider->block_decrypt_finish(out, outlen, ctx);
}
/**
* @brief Clean encryption / decryption context.
* @note After cleanup, a context is free to be reused if necessary.
- * @param driver - driver to use
* @param ctx The block context to use.
* @return Returns APR_ENOTIMPL if not supported.
*/
-APU_DECLARE(apr_status_t) apr_crypto_block_cleanup(
- const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx) {
- return driver->block_cleanup(ctx);
+APU_DECLARE(apr_status_t) apr_crypto_block_cleanup(apr_crypto_block_t *ctx)
+{
+ return ctx->provider->block_cleanup(ctx);
}
/**
- * @brief Clean encryption / decryption factory.
- * @note After cleanup, a factory is free to be reused if necessary.
- * @param driver - driver to use
- * @param f The factory to use.
+ * @brief Clean encryption / decryption context.
+ * @note After cleanup, a context is free to be reused if necessary.
+ * @param f The context to use.
* @return Returns APR_ENOTIMPL if not supported.
*/
-APU_DECLARE(apr_status_t) apr_crypto_cleanup(const apr_crypto_driver_t *driver,
- apr_crypto_t *f) {
- return driver->cleanup(f);
+APU_DECLARE(apr_status_t) apr_crypto_cleanup(apr_crypto_t *f)
+{
+ return f->provider->cleanup(f);
}
/**
* @brief Shutdown the crypto library.
* @note After shutdown, it is expected that the init function can be called again.
* @param driver - driver to use
- * @param p The pool to use.
* @return Returns APR_ENOTIMPL if not supported.
*/
-APU_DECLARE(apr_status_t) apr_crypto_shutdown(const apr_crypto_driver_t *driver,
- apr_pool_t *p) {
- return driver->shutdown(p);
+APU_DECLARE(apr_status_t) apr_crypto_shutdown(const apr_crypto_driver_t *driver)
+{
+ return driver->shutdown();
}
#endif /* APU_HAVE_CRYPTO */