dcrypt-openssl.c revision d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995
#include "lib.h"
#include "buffer.h"
#include "str.h"
#include "hex-binary.h"
#include "safe-memset.h"
#include "randgen.h"
#include "array.h"
#include "module-dir.h"
#include "dcrypt.h"
#include "dcrypt-private.h"
/**
key format documentation:
=========================
v1 key
------
algo id = openssl NID
enctype = 0 = none, 1 = ecdhe, 2 = password
key id = sha256(hex encoded public point)
public key
----------
1<tab>algo id<tab>public point
private key
-----------
- enctype none
1<tab>algo id<tab>0<tab>private point<tab>key id
- enctype ecdh (algorithm AES-256-CTR, key = SHA256(shared secret), IV = \0\0\0...)
1<tab>algo id<tab>1<tab>private point<tab>ephemeral public key<tab>encryption key id<tab>key id
- enctype password (algorithm AES-256-CTR, key = PBKDF2(SHA1, 16, password, salt), IV = \0\0\0...)
1<tab>algo id<tab>2<tab>private point<tab>salt<tab>key id
v2 key
------
algo oid = ASN1 OID of key algorithm (RSA or EC curve)
enctype = 0 = none, 1 = ecdhe, 2 = password
key id = SHA256(i2d_PUBKEY)
public key
----------
2<tab>HEX(i2d_PUBKEY)
- enctype none
2<tab>key algo oid<tab>0<tab>(RSA = i2d_PrivateKey, EC=Private Point)<tab>key id
- enctype ecdh, key,iv = PBKDF2(hash algo, rounds, shared secret, salt)
2<tab>key algo oid<tab>1<tab>symmetric algo name<tab>salt<tab>hash algo<tab>rounds<tab>E(RSA = i2d_PrivateKey, EC=Private Point)<tab>ephemeral public key<tab>encryption key id<tab>key id
- enctype password, key,iv = PBKDF2(hash algo, rounds, password, salt)
2<tab>key algo oid<tab>1<tab>symmetric algo name<tab>salt<tab>hash algo<tab>rounds<tab>E(RSA = i2d_PrivateKey, EC=Private Point)<tab>key id
**/
#if SSLEAY_VERSION_NUMBER < 0x1010000fL
#endif
struct dcrypt_context_symmetric {
const EVP_CIPHER *cipher;
unsigned char *key;
unsigned char *iv;
unsigned char *aad;
unsigned char *tag;
int padding;
int mode;
};
struct dcrypt_context_hmac {
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
unsigned char *key;
};
struct dcrypt_public_key {
void *ctx;
};
struct dcrypt_private_key {
void *ctx;
};
static
bool dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key, const char **error_r);
static
bool dcrypt_openssl_public_key_id(struct dcrypt_public_key *key, const char *algorithm, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_public_key_id_old(struct dcrypt_public_key *key, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key_r, const char **error_r ATTR_UNUSED);
static
static
static
bool dcrypt_openssl_rsa_decrypt(struct dcrypt_private_key *key, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_error(const char **error_r)
{
unsigned long ec = ERR_get_error();
return FALSE;
}
/* legacy function for old formats that generates
hex encoded point from EC public key
*/
static
{
const EC_POINT *p;
const EC_GROUP *g;
p = EC_KEY_get0_public_key(key);
g = EC_KEY_get0_group(key);
}
static
bool dcrypt_openssl_ctx_sym_create(const char *algorithm, enum dcrypt_sym_mode mode, struct dcrypt_context_symmetric **ctx_r, const char **error_r)
{
struct dcrypt_context_symmetric *ctx;
const EVP_CIPHER *cipher;
return FALSE;
}
/* allocate context */
return TRUE;
}
static
{
pool_unref(&pool);
}
static
void dcrypt_openssl_ctx_sym_set_key(struct dcrypt_context_symmetric *ctx, const unsigned char *key, size_t key_len)
{
}
static
void dcrypt_openssl_ctx_sym_set_iv(struct dcrypt_context_symmetric *ctx, const unsigned char *iv, size_t iv_len)
{
}
static
{
}
static
{
}
static
{
return TRUE;
}
static
{
return TRUE;
}
static
void dcrypt_openssl_ctx_sym_set_aad(struct dcrypt_context_symmetric *ctx, const unsigned char *aad, size_t aad_len)
{
/* allow empty aad */
}
static
{
return TRUE;
}
static
void dcrypt_openssl_ctx_sym_set_tag(struct dcrypt_context_symmetric *ctx, const unsigned char *tag, size_t tag_len)
{
/* unlike aad, tag cannot be empty */
}
static
{
return TRUE;
}
static
{
}
static
{
}
static
{
}
static
{
int ec;
int len;
return dcrypt_openssl_error(error_r);
len = 0;
return TRUE;
}
static
bool dcrypt_openssl_ctx_sym_update(struct dcrypt_context_symmetric *ctx, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r)
{
unsigned char *buf;
int outl;
/* From `man 3 evp_cipherupdate`:
EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes
the encrypted version to out. This function can be called multiple
times to encrypt successive blocks of data. The amount of data written
depends on the block alignment of the encrypted data: as a result the
amount of data written may be anything from zero bytes to
(inl + cipher_block_size - 1) so out should contain sufficient room.
The actual number of bytes written is placed in outl.
*/
outl = 0;
if (EVP_CipherUpdate
return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_ctx_sym_final(struct dcrypt_context_symmetric *ctx, buffer_t *result, const char **error_r)
{
unsigned char *buf;
int outl;
int ec;
/* From `man 3 evp_cipherupdate`:
If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
the "final" data, that is any data that remains in a partial block. It
uses standard block padding (aka PKCS padding). The encrypted final data
is written to out which should have sufficient space for one cipher
block. The number of bytes written is placed in outl. After this
function is called the encryption operation is finished and no further
calls to EVP_EncryptUpdate() should be made.
*/
outl = 0;
/* when **DECRYPTING** set expected tag */
} else ec = 1;
if (ec == 1)
if (ec == 1) {
/* when **ENCRYPTING** recover tag */
/* tag should be NULL here */
/* openssl claims taglen is always 16, go figure .. */
}
}
*error_r = "data authentication failed";
return ec == 1;
}
static
bool dcrypt_openssl_ctx_hmac_create(const char *algorithm, struct dcrypt_context_hmac **ctx_r, const char **error_r)
{
struct dcrypt_context_hmac *ctx;
return FALSE;
}
/* allocate context */
return TRUE;
}
static
{
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
pool_unref(&pool);
}
static
void dcrypt_openssl_ctx_hmac_set_key(struct dcrypt_context_hmac *ctx, const unsigned char *key, size_t key_len)
{
}
static
{
return TRUE;
}
static
{
}
static
{
}
static
{
int ec;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
return TRUE;
}
static
bool dcrypt_openssl_ctx_hmac_update(struct dcrypt_context_hmac *ctx, const unsigned char *data, size_t data_len, const char **error_r)
{
int ec;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
return TRUE;
}
static
bool dcrypt_openssl_ctx_hmac_final(struct dcrypt_context_hmac *ctx, buffer_t *result, const char **error_r)
{
int ec;
unsigned char buf[HMAC_MAX_MD_CBLOCK];
unsigned int outl;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
if (ec == 1) {
} else return dcrypt_openssl_error(error_r);
return TRUE;
}
static
{
/* generate parameters for EC */
{
return FALSE;
}
/* generate key from parameters */
{
return FALSE;
}
return TRUE;
}
static
{
int ec = 0;
ec = -1;
}
return ec == 0;
}
static
bool dcrypt_openssl_ecdh_derive_secret_local(struct dcrypt_private_key *local_key, buffer_t *R, buffer_t *S, const char **error_r)
{
/* convert ephemeral key data EC point */
{
return dcrypt_openssl_error(error_r);
}
/* convert point to public key */
/* make sure it looks like a valid key */
return dcrypt_openssl_error(error_r);
}
/* initialize derivation */
return dcrypt_openssl_error(error_r);
}
/* have to do it twice to get the data length */
return dcrypt_openssl_error(error_r);
}
return dcrypt_openssl_error(error_r);
}
return TRUE;
}
static
bool dcrypt_openssl_ecdh_derive_secret_peer(struct dcrypt_public_key *peer_key, buffer_t *R, buffer_t *S, const char **error_r)
{
/* ensure peer_key is EC key */
*error_r = "Only ECC key can be used";
return FALSE;
}
/* generate another key from same group */
/* initialize */
return dcrypt_openssl_error(error_r);
}
/* derive */
return dcrypt_openssl_error(error_r);
}
return dcrypt_openssl_error(error_r);
}
/* get ephemeral key (=R) */
return TRUE;
}
static
bool dcrypt_openssl_pbkdf2(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len,
const char *hash, unsigned int rounds, buffer_t *result, unsigned int result_len, const char **error_r)
{
int ret;
i_assert(result_len > 0);
T_BEGIN {
/* determine MD */
return FALSE;
}
unsigned char buffer[result_len];
}
} T_END;
return TRUE;
}
static
bool dcrypt_openssl_generate_keypair(struct dcrypt_keypair *pair_r, enum dcrypt_key_type kind, unsigned int bits, const char *curve, const char **error_r)
{
if (kind == DCRYPT_KEY_RSA) {
} else return dcrypt_openssl_error(error_r);
} else if (kind == DCRYPT_KEY_EC) {
return FALSE;
}
} else return dcrypt_openssl_error(error_r);
}
*error_r = "Key type not supported in this build";
return FALSE;
}
static
bool dcrypt_openssl_decrypt_point_v1(buffer_t *data, buffer_t *key, BIGNUM **point_r, const char **error_r)
{
struct dcrypt_context_symmetric *dctx;
return FALSE;
}
/* v1 KEYS have all-zero IV - have to use it ourselves too */
dcrypt_openssl_ctx_sym_set_iv(dctx, (const unsigned char*)"\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16);
return FALSE;
}
buffer_set_used_size(key, 0);
return dcrypt_openssl_error(error_r);
return TRUE;
}
static
{
bool res;
return FALSE;
/* run it thru SHA256 once */
unsigned char digest[SHA256_DIGEST_LENGTH];
/* then use this as key */
return res;
}
static
{
struct dcrypt_context_symmetric *dctx;
/* aes-256-ctr uses 32 byte key, and v1 uses all-zero IV */
return FALSE;
}
}
static
const char **error_r)
{
*error_r = "Corrupted data";
return FALSE;
}
*error_r = "Corrupted data";
return FALSE;
}
/* decode and optionally decipher private key value */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_NONE) {
return dcrypt_openssl_error(error_r);
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PASSWORD) {
/* by password */
return FALSE;
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
/* by key */
return FALSE;
}
} else {
*error_r = "Invalid key data";
return FALSE;
}
/* assign private key */
/* calculate public key */
/* make sure it looks OK and is correct */
unsigned char digest[SHA256_DIGEST_LENGTH];
/* validate that the key was loaded correctly */
*error_r = "Key id mismatch after load";
return FALSE;
}
return TRUE;
}
return dcrypt_openssl_error(error_r);
}
static
{
struct dcrypt_context_symmetric *dctx;
bool res;
return FALSE;
}
if (!res) {
return FALSE;
}
/* perform ciphering */
dcrypt_openssl_ctx_sym_set_iv(dctx, kd+dcrypt_openssl_ctx_sym_get_key_length(dctx), dcrypt_openssl_ctx_sym_get_iv_length(dctx));
} else {
/* provide result if succeeded */
}
/* and ensure no data leaks */
return res;
}
static
const char **error_r)
{
int enctype;
/* check for encryption type */
*error_r = "Corrupted data";
return FALSE;
}
*error_r = "Corrupted data";
return FALSE;
}
/* match encryption type to field counts */
*error_r = "Corrupted data";
return FALSE;
}
/* get key type */
return dcrypt_openssl_error(error_r);
/* decode and possibly decipher private key value */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_NONE) {
*error_r = "Corrupted data";
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
unsigned int rounds;
*error_r = "Corrupted data";
return FALSE;
}
/* check that we have correct decryption key */
return FALSE;
}
*error_r = "No private key available";
return FALSE;
}
buffer_set_used_size(data, 0);
return FALSE;
} else {
/* perform ECDH */
return FALSE;
}
/* decrypt key */
return FALSE;
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PASSWORD) {
unsigned int rounds;
*error_r = "Corrupted data";
return FALSE;
}
*error_r = "Corrupted data";
return FALSE;
}
return FALSE;
}
}
/* decode actual key */
return dcrypt_openssl_error(error_r);
}
} else {
int ec;
return dcrypt_openssl_error(error_r);
}
return dcrypt_openssl_error(error_r);
}
/* calculate public key */
/* make sure the EC key is valid */
} else {
return dcrypt_openssl_error(error_r);
}
}
/* finally compare key to key id */
*error_r = "Key id mismatch after load";
return FALSE;
}
return TRUE;
}
static
const char **error_r)
{
bool ret;
T_BEGIN {
if (len < 4) {
*error_r = "Corrupted data";
} else if (*(input[0])== '1')
else if (*(input[0])== '2')
else {
*error_r = "Unsupported key version";
}
} T_END;
return ret;
}
static
{
int nid;
if (len != 3) {
*error_r = "Corrupted data";
return -1;
}
*error_r = "Corrupted data";
return -1;
}
return -1;
}
return -1;
}
return 0;
}
return -1;
}
static
{
*error_r = "Corrupted data";
return -1;
}
return -1;
}
return 0;
}
static
{
int ec = 0;
T_BEGIN {
} else {
*error_r = "Unsupported key version";
ec = -1;
}
} T_END;
}
static
bool dcrypt_openssl_encrypt_private_key_dovecot(buffer_t *key, int enctype, const char *cipher, const char *password,
{
bool res;
unsigned char *ptr;
unsigned char salt[8];
/* so we don't have to make new version if we ever upgrade these */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
/* peer key, in this case, is encrypted secret, which is 16 bytes of data */
return FALSE;
}
/* generate secret by ECDHE */
return FALSE;
}
} else {
*error_r = "Unsupported encryption key";
return FALSE;
}
/* add encryption key id, reuse peer_key buffer */
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PASSWORD) {
}
/* encrypt key using secret and salt */
/* some additional fields or private key version */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
/* for RSA, this is the actual encrypted secret */
return FALSE;
}
return res;
}
static
bool dcrypt_openssl_store_private_key_dovecot(struct dcrypt_private_key *key, const char *cipher, buffer_t *destination,
{
/* because otherwise we get wrong nid */
} else {
}
int enctype = 0;
if (ln < 1)
return dcrypt_openssl_error(error_r);
*error_r = "Object identifier too long";
return FALSE;
}
/* convert key to private key value */
unsigned char *ptr;
if (ln < 1)
return dcrypt_openssl_error(error_r);
unsigned char *ptr;
/* serialize to MPI which is portable */
} else {
*error_r = "Unsupported key type";
return FALSE;
}
/* see if we want ECDH based or password based encryption */
}
/* put in OID and encryption type */
/* perform encryption if desired */
if (enctype > 0) {
if (!dcrypt_openssl_encrypt_private_key_dovecot(buf, enctype, cipher2, password, enc_key, destination, error_r)) {
return FALSE;
}
} else {
}
/* append public key id */
return FALSE;
}
buffer_set_used_size(buf, 0);
if (!res) {
/* well, that didn't end well */
return FALSE;
}
return TRUE;
}
static
bool dcrypt_openssl_store_public_key_dovecot(struct dcrypt_public_key *key, buffer_t *destination, const char **error_r)
{
return dcrypt_openssl_error(error_r);
/* then store it */
return TRUE;
}
static
bool dcrypt_openssl_load_private_key(struct dcrypt_private_key **key_r, enum dcrypt_key_format format,
const char **error_r)
{
if (format == DCRYPT_FORMAT_DOVECOT)
key = EVP_PKEY_new();
return dcrypt_openssl_error(error_r);
}
}
return TRUE;
}
static
bool dcrypt_openssl_load_public_key(struct dcrypt_public_key **key_r, enum dcrypt_key_format format,
{
if (format == DCRYPT_FORMAT_DOVECOT)
/* read the header */
return dcrypt_openssl_error(error_r);
}
*error_r = "Missing public key header";
return FALSE;
}
key = EVP_PKEY_new();
}
}
return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_store_private_key(struct dcrypt_private_key *key, enum dcrypt_key_format format,
const char **error_r)
{
int ec;
if (format == DCRYPT_FORMAT_DOVECOT) {
bool ret;
T_BEGIN {
ret = dcrypt_openssl_store_private_key_dovecot(key, cipher, destination, password, enc_key, error_r);
} T_END;
return ret;
}
return FALSE;
}
}
if (ec != 1) {
return dcrypt_openssl_error(error_r);
}
long bs;
char *buf;
return TRUE;
}
static
bool dcrypt_openssl_store_public_key(struct dcrypt_public_key *key, enum dcrypt_key_format format, buffer_t *destination, const char **error_r)
{
int ec;
if (format == DCRYPT_FORMAT_DOVECOT)
else {
}
if (ec != 1) {
return dcrypt_openssl_error(error_r);
}
long bs;
char *buf;
return TRUE;
}
static
bool dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key_r, const char **error_r)
{
pk = EVP_PKEY_new();
else
{
} else {
*error_r = "Invalid private key";
return FALSE;
}
return TRUE;
}
static
bool dcrypt_openssl_key_string_get_info(const char *key_data, enum dcrypt_key_format *format_r, enum dcrypt_key_version *version_r,
enum dcrypt_key_kind *kind_r, enum dcrypt_key_encryption_type *encryption_type_r, const char **encryption_key_hash_r,
const char **key_hash_r, const char **error_r)
{
char *encryption_key_hash = NULL;
*error_r = "NULL key passed";
return FALSE;
}
/* is it PEM key */
}
else {
return FALSE;
}
} else T_BEGIN {
int nfields;
if (nfields < 2) {
*error_r = "Unknown key format";
return FALSE;
}
/* field 1 - version */
if (nfields == 3) {
if (encryption_key_hash_r != NULL)
} else {
*error_r = "Invalid dovecot v1 encoding";
return FALSE;
}
if (nfields == 2) {
if (encryption_key_hash_r != NULL)
} else {
*error_r = "Invalid dovecot v2 encoding";
return FALSE;
}
}
/* last field is always key hash */
if (key_hash_r != NULL)
} T_END;
if (encryption_key_hash_r != NULL) {
}
if (key_hash_r != NULL) {
}
return TRUE;
}
static
{
}
static
{
}
static
{
}
static
bool dcrypt_openssl_rsa_encrypt(struct dcrypt_public_key *key, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r)
{
int ec;
ec = -1;
} else {
ec = 0;
}
return ec == 0;
}
static
bool dcrypt_openssl_rsa_decrypt(struct dcrypt_private_key *key, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r)
{
int ec;
ec = -1;
} else {
ec = 0;
}
return ec == 0;
}
static
{
const char *name;
return NULL;
}
return name;
}
static
{
return dcrypt_openssl_error(error_r);
*error_r = "Object has no OID assigned";
return FALSE;
}
return TRUE;
}
return dcrypt_openssl_error(error_r);
}
static
bool dcrypt_openssl_private_key_type(struct dcrypt_private_key *key, enum dcrypt_key_type *key_type)
{
return FALSE;
}
static
{
return FALSE;
}
/** this is the v1 old legacy way of doing key id's **/
static
bool dcrypt_openssl_public_key_id_old(struct dcrypt_public_key *key, buffer_t *result, const char **error_r)
{
unsigned char buf[SHA256_DIGEST_LENGTH];
*error_r = "key is NULL";
return FALSE;
}
*error_r = "Only EC key supported";
return FALSE;
}
/* digest this */
return TRUE;
}
/** this is the new which uses H(der formatted public key) **/
static
bool dcrypt_openssl_public_key_id(struct dcrypt_public_key *key, const char *algorithm, buffer_t *result, const char **error_r)
{
return FALSE;
}
const char *ptr;
bool res;
*error_r = "key is NULL";
return FALSE;
}
}
BIO_vfree(b);
return dcrypt_openssl_error(error_r);
}
/* then hash it */
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
} else {
}
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
#else
#endif
BIO_vfree(b);
return res;
}
static struct dcrypt_vfs dcrypt_openssl_vfs = {
};
{
}
void dcrypt_openssl_deinit(void)
{
#if OPENSSL_API_COMPAT < 0x10100000L
OBJ_cleanup();
#endif
}