apr_crypto_openssl.c [plain text]
#include "apr_lib.h"
#include "apu.h"
#include "apu_errno.h"
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#include "apr_strings.h"
#include "apr_time.h"
#include "apr_buckets.h"
#include "apr_crypto_internal.h"
#if APU_HAVE_CRYPTO
#include <openssl/evp.h>
#include <openssl/engine.h>
#define LOG_PREFIX "apr_crypto_openssl: "
struct apr_crypto_t {
apr_pool_t *pool;
const apr_crypto_driver_t *provider;
apu_err_t *result;
apr_array_header_t *keys;
apr_crypto_config_t *config;
apr_hash_t *types;
apr_hash_t *modes;
};
struct apr_crypto_config_t {
ENGINE *engine;
};
struct apr_crypto_key_t {
apr_pool_t *pool;
const apr_crypto_driver_t *provider;
const apr_crypto_t *f;
const EVP_CIPHER * cipher;
unsigned char *key;
int keyLen;
int doPad;
int ivSize;
};
struct apr_crypto_block_t {
apr_pool_t *pool;
const apr_crypto_driver_t *provider;
const apr_crypto_t *f;
EVP_CIPHER_CTX cipherCtx;
int initialised;
int ivSize;
int blockSize;
int doPad;
};
static int key_3des_192 = APR_KEY_3DES_192;
static int key_aes_128 = APR_KEY_AES_128;
static int key_aes_192 = APR_KEY_AES_192;
static int key_aes_256 = APR_KEY_AES_256;
static int mode_ecb = APR_MODE_ECB;
static int mode_cbc = APR_MODE_CBC;
static apr_status_t crypto_error(const apu_err_t **result,
const apr_crypto_t *f)
{
*result = f->result;
return APR_SUCCESS;
}
static apr_status_t crypto_shutdown(void)
{
ERR_free_strings();
EVP_cleanup();
ENGINE_cleanup();
return APR_SUCCESS;
}
static apr_status_t crypto_shutdown_helper(void *data)
{
return crypto_shutdown();
}
static apr_status_t crypto_init(apr_pool_t *pool, const char *params,
const apu_err_t **result)
{
CRYPTO_malloc_init();
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
ENGINE_load_builtin_engines();
ENGINE_register_all_complete();
apr_pool_cleanup_register(pool, pool, crypto_shutdown_helper,
apr_pool_cleanup_null);
return APR_SUCCESS;
}
static apr_status_t crypto_block_cleanup(apr_crypto_block_t *ctx)
{
if (ctx->initialised) {
EVP_CIPHER_CTX_cleanup(&ctx->cipherCtx);
ctx->initialised = 0;
}
return APR_SUCCESS;
}
static apr_status_t crypto_block_cleanup_helper(void *data)
{
apr_crypto_block_t *block = (apr_crypto_block_t *) data;
return crypto_block_cleanup(block);
}
static apr_status_t crypto_cleanup(apr_crypto_t *f)
{
if (f->config->engine) {
ENGINE_finish(f->config->engine);
ENGINE_free(f->config->engine);
f->config->engine = NULL;
}
return APR_SUCCESS;
}
static apr_status_t crypto_cleanup_helper(void *data)
{
apr_crypto_t *f = (apr_crypto_t *) data;
return crypto_cleanup(f);
}
static apr_status_t crypto_make(apr_crypto_t **ff,
const apr_crypto_driver_t *provider, const char *params,
apr_pool_t *pool)
{
apr_crypto_config_t *config = NULL;
apr_crypto_t *f = apr_pcalloc(pool, sizeof(apr_crypto_t));
const char *engine = NULL;
struct {
const char *field;
const char *value;
int set;
} fields[] = {
{ "engine", NULL, 0 },
{ NULL, NULL, 0 }
};
const char *ptr;
size_t klen;
char **elts = NULL;
char *elt;
int i = 0, j;
apr_status_t status;
if (params) {
if (APR_SUCCESS != (status = apr_tokenize_to_argv(params, &elts, pool))) {
return status;
}
while ((elt = elts[i])) {
ptr = strchr(elt, '=');
if (ptr) {
for (klen = ptr - elt; klen && apr_isspace(elt[klen - 1]); --klen)
;
ptr++;
}
else {
for (klen = strlen(elt); klen && apr_isspace(elt[klen - 1]); --klen)
;
}
elt[klen] = 0;
for (j = 0; fields[j].field != NULL; ++j) {
if (!strcasecmp(fields[j].field, elt)) {
fields[j].set = 1;
if (ptr) {
fields[j].value = ptr;
}
break;
}
}
i++;
}
engine = fields[0].value;
}
if (!f) {
return APR_ENOMEM;
}
*ff = f;
f->pool = pool;
f->provider = provider;
config = f->config = apr_pcalloc(pool, sizeof(apr_crypto_config_t));
if (!config) {
return APR_ENOMEM;
}
f->result = apr_pcalloc(pool, sizeof(apu_err_t));
if (!f->result) {
return APR_ENOMEM;
}
f->keys = apr_array_make(pool, 10, sizeof(apr_crypto_key_t));
if (!f->keys) {
return APR_ENOMEM;
}
f->types = apr_hash_make(pool);
if (!f->types) {
return APR_ENOMEM;
}
apr_hash_set(f->types, "3des192", APR_HASH_KEY_STRING, &(key_3des_192));
apr_hash_set(f->types, "aes128", APR_HASH_KEY_STRING, &(key_aes_128));
apr_hash_set(f->types, "aes192", APR_HASH_KEY_STRING, &(key_aes_192));
apr_hash_set(f->types, "aes256", APR_HASH_KEY_STRING, &(key_aes_256));
f->modes = apr_hash_make(pool);
if (!f->modes) {
return APR_ENOMEM;
}
apr_hash_set(f->modes, "ecb", APR_HASH_KEY_STRING, &(mode_ecb));
apr_hash_set(f->modes, "cbc", APR_HASH_KEY_STRING, &(mode_cbc));
apr_pool_cleanup_register(pool, f, crypto_cleanup_helper,
apr_pool_cleanup_null);
if (engine) {
config->engine = ENGINE_by_id(engine);
if (!config->engine) {
return APR_ENOENGINE;
}
if (!ENGINE_init(config->engine)) {
ENGINE_free(config->engine);
config->engine = NULL;
return APR_EINITENGINE;
}
}
return APR_SUCCESS;
}
static apr_status_t crypto_get_block_key_types(apr_hash_t **types,
const apr_crypto_t *f)
{
*types = f->types;
return APR_SUCCESS;
}
static apr_status_t crypto_get_block_key_modes(apr_hash_t **modes,
const apr_crypto_t *f)
{
*modes = f->modes;
return APR_SUCCESS;
}
static apr_status_t crypto_passphrase(apr_crypto_key_t **k, 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, const apr_crypto_t *f, apr_pool_t *p)
{
apr_crypto_key_t *key = *k;
if (!key) {
*k = key = apr_array_push(f->keys);
}
if (!key) {
return APR_ENOMEM;
}
key->f = f;
key->provider = f->provider;
switch (type) {
case (APR_KEY_3DES_192):
if (mode == APR_MODE_CBC) {
key->cipher = EVP_des_ede3_cbc();
}
else {
key->cipher = EVP_des_ede3_ecb();
}
break;
case (APR_KEY_AES_128):
if (mode == APR_MODE_CBC) {
key->cipher = EVP_aes_128_cbc();
}
else {
key->cipher = EVP_aes_128_ecb();
}
break;
case (APR_KEY_AES_192):
if (mode == APR_MODE_CBC) {
key->cipher = EVP_aes_192_cbc();
}
else {
key->cipher = EVP_aes_192_ecb();
}
break;
case (APR_KEY_AES_256):
if (mode == APR_MODE_CBC) {
key->cipher = EVP_aes_256_cbc();
}
else {
key->cipher = EVP_aes_256_ecb();
}
break;
default:
return APR_EKEYTYPE;
}
key->keyLen = EVP_CIPHER_key_length(key->cipher);
key->key = apr_pcalloc(p, key->keyLen);
if (!key->key) {
return APR_ENOMEM;
}
apr_crypto_clear(p, key->key, key->keyLen);
if (PKCS5_PBKDF2_HMAC_SHA1(pass, passLen, (unsigned char *) salt, saltLen,
iterations, key->keyLen, key->key) == 0) {
return APR_ENOKEY;
}
key->doPad = doPad;
if (APR_MODE_ECB != mode) {
key->ivSize = EVP_CIPHER_iv_length(key->cipher);
}
if (ivSize) {
*ivSize = key->ivSize;
}
return APR_SUCCESS;
}
static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx,
const unsigned char **iv, const apr_crypto_key_t *key,
apr_size_t *blockSize, apr_pool_t *p)
{
unsigned char *usedIv;
apr_crypto_config_t *config = key->f->config;
apr_crypto_block_t *block = *ctx;
if (!block) {
*ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
}
if (!block) {
return APR_ENOMEM;
}
block->f = key->f;
block->pool = p;
block->provider = key->provider;
apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
apr_pool_cleanup_null);
EVP_CIPHER_CTX_init(&block->cipherCtx);
block->initialised = 1;
usedIv = NULL;
if (key->ivSize) {
if (iv == NULL) {
return APR_ENOIV;
}
if (*iv == NULL) {
usedIv = apr_pcalloc(p, key->ivSize);
if (!usedIv) {
return APR_ENOMEM;
}
apr_crypto_clear(p, usedIv, key->ivSize);
if (!((RAND_status() == 1)
&& (RAND_bytes(usedIv, key->ivSize) == 1))) {
return APR_ENOIV;
}
*iv = usedIv;
}
else {
usedIv = (unsigned char *) *iv;
}
}
#if CRYPTO_OPENSSL_CONST_BUFFERS
if (!EVP_EncryptInit_ex(&block->cipherCtx, key->cipher, config->engine,
key->key, usedIv)) {
#else
if (!EVP_EncryptInit_ex(&block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) usedIv)) {
#endif
return APR_EINIT;
}
if (!EVP_CIPHER_CTX_set_padding(&block->cipherCtx, key->doPad)) {
return APR_EPADDING;
}
if (blockSize) {
*blockSize = EVP_CIPHER_block_size(key->cipher);
}
return APR_SUCCESS;
}
static apr_status_t crypto_block_encrypt(unsigned char **out,
apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
apr_crypto_block_t *ctx)
{
int outl = *outlen;
unsigned char *buffer;
if (!out) {
*outlen = inlen + EVP_MAX_BLOCK_LENGTH;
return APR_SUCCESS;
}
if (!*out) {
buffer = apr_palloc(ctx->pool, inlen + EVP_MAX_BLOCK_LENGTH);
if (!buffer) {
return APR_ENOMEM;
}
apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH);
*out = buffer;
}
#if CRYPT_OPENSSL_CONST_BUFFERS
if (!EVP_EncryptUpdate(&ctx->cipherCtx, (*out), &outl, in, inlen)) {
#else
if (!EVP_EncryptUpdate(&ctx->cipherCtx, (*out), &outl,
(unsigned char *) in, inlen)) {
#endif
return APR_ECRYPT;
}
*outlen = outl;
return APR_SUCCESS;
}
static apr_status_t crypto_block_encrypt_finish(unsigned char *out,
apr_size_t *outlen, apr_crypto_block_t *ctx)
{
int len = *outlen;
if (EVP_EncryptFinal_ex(&ctx->cipherCtx, out, &len) == 0) {
return APR_EPADDING;
}
*outlen = len;
return APR_SUCCESS;
}
static apr_status_t crypto_block_decrypt_init(apr_crypto_block_t **ctx,
apr_size_t *blockSize, const unsigned char *iv,
const apr_crypto_key_t *key, apr_pool_t *p)
{
apr_crypto_config_t *config = key->f->config;
apr_crypto_block_t *block = *ctx;
if (!block) {
*ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
}
if (!block) {
return APR_ENOMEM;
}
block->f = key->f;
block->pool = p;
block->provider = key->provider;
apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
apr_pool_cleanup_null);
EVP_CIPHER_CTX_init(&block->cipherCtx);
block->initialised = 1;
if (key->ivSize) {
if (iv == NULL) {
return APR_ENOIV;
}
}
#if CRYPTO_OPENSSL_CONST_BUFFERS
if (!EVP_DecryptInit_ex(&block->cipherCtx, key->cipher, config->engine,
key->key, iv)) {
#else
if (!EVP_DecryptInit_ex(&block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) iv)) {
#endif
return APR_EINIT;
}
if (!EVP_CIPHER_CTX_set_padding(&block->cipherCtx, key->doPad)) {
return APR_EPADDING;
}
if (blockSize) {
*blockSize = EVP_CIPHER_block_size(key->cipher);
}
return APR_SUCCESS;
}
static apr_status_t crypto_block_decrypt(unsigned char **out,
apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
apr_crypto_block_t *ctx)
{
int outl = *outlen;
unsigned char *buffer;
if (!out) {
*outlen = inlen + EVP_MAX_BLOCK_LENGTH;
return APR_SUCCESS;
}
if (!(*out)) {
buffer = apr_palloc(ctx->pool, inlen + EVP_MAX_BLOCK_LENGTH);
if (!buffer) {
return APR_ENOMEM;
}
apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH);
*out = buffer;
}
#if CRYPT_OPENSSL_CONST_BUFFERS
if (!EVP_DecryptUpdate(&ctx->cipherCtx, *out, &outl, in, inlen)) {
#else
if (!EVP_DecryptUpdate(&ctx->cipherCtx, *out, &outl, (unsigned char *) in,
inlen)) {
#endif
return APR_ECRYPT;
}
*outlen = outl;
return APR_SUCCESS;
}
static apr_status_t crypto_block_decrypt_finish(unsigned char *out,
apr_size_t *outlen, apr_crypto_block_t *ctx)
{
int len = *outlen;
if (EVP_DecryptFinal_ex(&ctx->cipherCtx, out, &len) == 0) {
return APR_EPADDING;
}
*outlen = len;
return APR_SUCCESS;
}
APU_MODULE_DECLARE_DATA const apr_crypto_driver_t apr_crypto_openssl_driver = {
"openssl", crypto_init, crypto_make, crypto_get_block_key_types,
crypto_get_block_key_modes, crypto_passphrase,
crypto_block_encrypt_init, crypto_block_encrypt,
crypto_block_encrypt_finish, crypto_block_decrypt_init,
crypto_block_decrypt, crypto_block_decrypt_finish,
crypto_block_cleanup, crypto_cleanup, crypto_shutdown, crypto_error
};
#endif