/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
#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 "dovecot-openssl-common.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)<tab>key id
- 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
**/
#ifndef HAVE_EVP_PKEY_get0
#endif
#ifndef HAVE_OBJ_LENGTH
#endif
#ifndef HAVE_EVP_MD_CTX_NEW
#endif
#ifndef HAVE_HMAC_CTX_NEW
#else
#endif
struct dcrypt_context_symmetric {
unsigned char *key;
unsigned char *iv;
unsigned char *aad;
unsigned char *tag;
int padding;
int mode;
};
struct dcrypt_context_hmac {
#ifdef HAVE_HMAC_CTX_NEW
#else
#endif
unsigned char *key;
};
struct dcrypt_public_key {
unsigned int ref;
};
struct dcrypt_private_key {
unsigned int ref;
};
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_key_id(struct dcrypt_private_key *key, const char *algorithm, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_private_key_id_old(struct dcrypt_private_key *key, buffer_t *result, const char **error_r);
static
void dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key_r);
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_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);
static
{
return FALSE;
}
static
{
return FALSE;
}
return TRUE;
}
/* 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)
{
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)
{
return FALSE;
}
/* allocate context */
return TRUE;
}
static
{
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;
#ifdef HAVE_HMAC_CTX_NEW
#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;
return TRUE;
}
static
bool dcrypt_openssl_ctx_hmac_final(struct dcrypt_context_hmac *ctx, buffer_t *result, const char **error_r)
{
int ec;
unsigned int outl;
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)
{
return dcrypt_openssl_error(error_r);
/* convert ephemeral key data EC point */
{
return dcrypt_openssl_error(error_r);
}
/* convert point to public key */
int ec = 0;
ec = -1;
else
/* make sure it looks like a valid key */
return dcrypt_openssl_error(error_r);
}
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);
/* determine MD */
return FALSE;
}
}
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) {
return TRUE;
} else return dcrypt_openssl_error(error_r);
} else if (kind == DCRYPT_KEY_EC) {
return FALSE;
}
return TRUE;
} 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)
{
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 */
/* then use this as key */
return res;
}
static
{
/* 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;
}
return FALSE;
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
/* by key */
return FALSE;
}
return FALSE;
}
} else {
*error_r = "Invalid key data";
return FALSE;
}
/* assign private key */
return dcrypt_openssl_error(error_r);
}
return dcrypt_openssl_error(error_r);
}
/* calculate public key */
/* make sure it looks OK and is correct */
/* validate that the key was loaded correctly */
return dcrypt_openssl_error(error_r);
}
*error_r = "Key id mismatch after load";
return FALSE;
}
return dcrypt_openssl_error(error_r);
}
return TRUE;
}
return dcrypt_openssl_error(error_r);
}
static
{
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) {
return FALSE;
}
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) {
return FALSE;
}
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);
}
return dcrypt_openssl_error(error_r);
}
} else {
int ec;
return dcrypt_openssl_error(error_r);
}
return dcrypt_openssl_error(error_r);
}
ec = -1;
else {
/* 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
{
switch (version) {
case DCRYPT_KEY_VERSION_1:
case DCRYPT_KEY_VERSION_2:
case DCRYPT_KEY_VERSION_NA:
i_unreached();
}
return FALSE;
}
static
{
int nid;
*error_r = "Corrupted data";
return FALSE;
}
return FALSE;
}
return FALSE;
}
/* make sure digest matches */
*error_r = "Key id mismatch after load";
return FALSE;
}
return TRUE;
}
return FALSE;
}
static
{
const unsigned char *ptr;
return FALSE;
}
/* make sure digest matches */
*error_r = "Key id mismatch after load";
return FALSE;
}
return TRUE;
}
static
const char **error_r)
{
switch (version) {
case DCRYPT_KEY_VERSION_1:
break;
case DCRYPT_KEY_VERSION_2:
break;
case DCRYPT_KEY_VERSION_NA:
i_unreached();
}
return FALSE;
}
static
bool dcrypt_openssl_encrypt_private_key_dovecot(buffer_t *key, int enctype, const char *cipher, const char *password,
{
bool res;
unsigned char *ptr;
/* 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 {
/* Loading the key should have failed */
i_unreached();
}
/* 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 {
}
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 {
/* Loading the key should have failed */
i_unreached();
}
/* see if we want ECDH based or password based encryption */
} else if (enctype == DCRYPT_KEY_ENCRYPTION_TYPE_NONE) {
}
/* put in OID and encryption type */
/* perform encryption if desired */
if (enctype != DCRYPT_KEY_ENCRYPTION_TYPE_NONE) {
if (!dcrypt_openssl_encrypt_private_key_dovecot(buf, enctype, cipher2, password, enc_key, destination, error_r)) {
return FALSE;
}
} else {
}
/* append public key id */
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 */
/* append public key ID */
if (!res) {
return FALSE;
}
return TRUE;
}
static
{
return FALSE;
}
if (kind != DCRYPT_KEY_KIND_PRIVATE) {
return FALSE;
}
if (format == DCRYPT_FORMAT_DOVECOT)
key = EVP_PKEY_new();
return dcrypt_openssl_error(error_r);
}
}
return TRUE;
}
static
{
return FALSE;
}
if (kind != DCRYPT_KEY_KIND_PUBLIC) {
return FALSE;
}
if (format == DCRYPT_FORMAT_DOVECOT)
return dcrypt_openssl_error(error_r);
/* read the header */
return dcrypt_openssl_error(error_r);
}
*error_r = "Missing public key header";
return FALSE;
}
return dcrypt_openssl_error(error_r);
}
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;
ret = dcrypt_openssl_store_private_key_dovecot(key, cipher, destination, password, enc_key, error_r);
return ret;
}
return dcrypt_openssl_error(error_r);
return FALSE;
}
}
ec = -1;
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)
return dcrypt_openssl_error(error_r);
ec = -1;
else {
ec = -1;
ec = -1;
}
if (ec != 1) {
return dcrypt_openssl_error(error_r);
}
long bs;
char *buf;
return TRUE;
}
static
void dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key_r)
{
pk = EVP_PKEY_new();
{
} else {
/* Loading the key should have failed */
i_unreached();
}
}
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)
{
/* is it PEM key */
key_data += 11;
*error_r = "RSA private key format not "
"supported, convert it to PKEY format "
"with openssl pkey";
return FALSE;
}
key_data += 10;
}
else {
return FALSE;
}
} else {
*error_r = "Dovecot v1 key format "
"uses tab to separate fields";
return FALSE;
*error_r = "Dovecot v2 key format uses "
"colon to separate fields";
return FALSE;
}
if (nfields < 2) {
*error_r = "Unknown key format";
return FALSE;
}
/* field 1 - version */
if (nfields == 4) {
if (encryption_key_hash_r != NULL)
} else {
*error_r = "Invalid dovecot v1 encoding";
return FALSE;
}
if (nfields == 3) {
if (encryption_key_hash_r != NULL)
} else {
*error_r = "Invalid dovecot v2 encoding";
return FALSE;
}
} else {
*error_r = "Invalid dovecot key version";
return FALSE;
}
/* last field is always key hash */
if (key_hash_r != NULL)
}
if (encryption_key_hash_r != NULL) {
}
if (key_hash_r != NULL) {
}
return TRUE;
}
static
{
}
static
{
}
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);
if (len == 0)
{
*error_r = "Object has no OID assigned";
return FALSE;
}
return TRUE;
}
return dcrypt_openssl_error(error_r);
}
static
{
else i_unreached();
}
static
{
else i_unreached();
}
/** 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)
{
*error_r = "Only EC key supported";
return FALSE;
}
if (pub_pt_hex == NULL)
return dcrypt_openssl_error(error_r);
/* digest this */
return TRUE;
}
static
bool dcrypt_openssl_private_key_id_old(struct dcrypt_private_key *key, buffer_t *result, const char **error_r)
{
*error_r = "Only EC key supported";
return FALSE;
}
if (pub_pt_hex == NULL)
return dcrypt_openssl_error(error_r);
/* digest this */
return TRUE;
}
/** this is the new which uses H(der formatted public key) **/
static
bool dcrypt_openssl_public_key_id_evp(EVP_PKEY *key, const EVP_MD *md, buffer_t *result, const char **error_r)
{
}
BIO_vfree(b);
return dcrypt_openssl_error(error_r);
}
/* then hash it */
} else {
}
BIO_vfree(b);
return res;
}
static
bool dcrypt_openssl_public_key_id(struct dcrypt_public_key *key, const char *algorithm, buffer_t *result, const char **error_r)
{
return FALSE;
}
}
static
bool dcrypt_openssl_private_key_id(struct dcrypt_private_key *key, const char *algorithm, buffer_t *result, const char **error_r)
{
return FALSE;
}
}
};
{
}
void dcrypt_openssl_deinit(void)
{
}