diff options
Diffstat (limited to 'src/lib/crypt.c')
-rw-r--r-- | src/lib/crypt.c | 589 |
1 files changed, 291 insertions, 298 deletions
diff --git a/src/lib/crypt.c b/src/lib/crypt.c index f3f86084..8b18140e 100644 --- a/src/lib/crypt.c +++ b/src/lib/crypt.c @@ -1,8 +1,7 @@ /* - * Ouroboros - Copyright (C) 2016 - 2022 + * Ouroboros - Copyright (C) 2016 - 2024 * - * Elliptic curve Diffie-Hellman key exchange and - * AES encryption for flows using OpenSSL + * Cryptographic operations * * Dimitri Staessens <dimitri@ouroboros.rocks> * Sander Vrijders <sander@ouroboros.rocks> @@ -21,425 +20,419 @@ * Foundation, Inc., http://www.fsf.org/about/contact/. */ +#include <config.h> + +#include <ouroboros/crypt.h> +#include <ouroboros/errno.h> #ifdef HAVE_OPENSSL + #include "crypt/openssl.h" +#endif /* HAVE_OPENSSL */ -#include <openssl/evp.h> -#include <openssl/ec.h> -#include <openssl/pem.h> +#include <assert.h> +#include <string.h> -#include <openssl/bio.h> +struct crypt_ctx { + void * ctx; + uint8_t key[SYMMKEYSZ]; +}; -#define IVSZ 16 -/* SYMMKEYSZ defined in dev.c */ +struct auth_ctx { + void * store; +}; -/* - * Derive the common secret from - * your public key pair (kp) - * the remote public key (pub). - * Store it in a preallocated buffer (s). - */ -static int __openssl_ecdh_derive_secret(EVP_PKEY * kp, - EVP_PKEY * pub, - uint8_t * s) +int crypt_dh_pkp_create(void ** pkp, + uint8_t * pk) { - EVP_PKEY_CTX * ctx; - int ret; - uint8_t * secret; - size_t secret_len; - - ctx = EVP_PKEY_CTX_new(kp, NULL); - if (ctx == NULL) - goto fail_new; - - ret = EVP_PKEY_derive_init(ctx); - if (ret != 1) - goto fail_ctx; - - ret = EVP_PKEY_derive_set_peer(ctx, pub); - if (ret != 1) - goto fail_ctx; - - ret = EVP_PKEY_derive(ctx, NULL, &secret_len); - if (ret != 1) - goto fail_ctx; - - if (secret_len < SYMMKEYSZ) - goto fail_ctx; - - secret = OPENSSL_malloc(secret_len); - if (secret == NULL) - goto fail_ctx; - - ret = EVP_PKEY_derive(ctx, secret, &secret_len); - if (ret != 1) - goto fail_derive; - - /* Hash the secret for use as AES key. */ - mem_hash(HASH_SHA3_256, s, secret, secret_len); +#ifdef HAVE_OPENSSL + assert(pkp != NULL); + *pkp = NULL; + return openssl_ecdh_pkp_create(pkp, pk); +#else + (void) pkp; + (void) pk; - OPENSSL_free(secret); - EVP_PKEY_CTX_free(ctx); + *pkp = NULL; return 0; - - fail_derive: - OPENSSL_free(secret); - fail_ctx: - EVP_PKEY_CTX_free(ctx); - fail_new: - return -ECRYPT; +#endif } -static int __openssl_ecdh_gen_key(void ** kp) +void crypt_dh_pkp_destroy(void * pkp) { - EVP_PKEY_CTX * ctx = NULL; - EVP_PKEY_CTX * kctx = NULL; - EVP_PKEY * params = NULL; - int ret; + if (pkp == NULL) + return; +#ifdef HAVE_OPENSSL + openssl_ecdh_pkp_destroy(pkp); +#else + (void) pkp; - ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); - if (ctx == NULL) - goto fail_new_id; + return; +#endif +} - ret = EVP_PKEY_paramgen_init(ctx); - if (ret != 1) - goto fail_paramgen; +int crypt_dh_derive(void * pkp, + buffer_t pk, + uint8_t * s) +{ +#ifdef HAVE_OPENSSL + return openssl_ecdh_derive(pkp, pk, s); +#else + (void) pkp; + (void) pk; - ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_X9_62_prime256v1); - if (ret != 1) - goto fail_paramgen; + memset(s, 0, SYMMKEYSZ); - ret = EVP_PKEY_paramgen(ctx, ¶ms); - if (ret != 1) - goto fail_paramgen; + return -ECRYPT; +#endif +} - kctx = EVP_PKEY_CTX_new(params, NULL); - if (kctx == NULL) - goto fail_keygen_init; +int crypt_encrypt(struct crypt_ctx * ctx, + buffer_t in, + buffer_t * out) +{ + assert(ctx != NULL); + assert(ctx->ctx != NULL); - ret = EVP_PKEY_keygen_init(kctx); - if (ret != 1) - goto fail_keygen; +#ifdef HAVE_OPENSSL + return openssl_encrypt(ctx->ctx, ctx->key, in, out); +#else + (void) ctx; + (void) in; + (void) out; - ret = EVP_PKEY_keygen(kctx, (EVP_PKEY **) kp); - if (ret != 1) - goto fail_keygen; + return -ECRYPT; +#endif +} - EVP_PKEY_free(params); - EVP_PKEY_CTX_free(kctx); - EVP_PKEY_CTX_free(ctx); +int crypt_decrypt(struct crypt_ctx * ctx, + buffer_t in, + buffer_t * out) +{ + assert(ctx != NULL); + assert(ctx->ctx != NULL); - return 0; +#ifdef HAVE_OPENSSL + return openssl_decrypt(ctx->ctx, ctx->key, in, out); +#else + (void) ctx; + (void) in; + (void) out; - fail_keygen: - EVP_PKEY_CTX_free(kctx); - fail_keygen_init: - EVP_PKEY_free(params); - fail_paramgen: - EVP_PKEY_CTX_free(ctx); - fail_new_id: return -ECRYPT; +#endif } -static ssize_t openssl_ecdh_pkp_create(void ** pkp, - uint8_t * pk) +struct crypt_ctx * crypt_create_ctx(const uint8_t * key) { - uint8_t * pos; - ssize_t len; - - assert(pkp != NULL); - assert(*pkp == NULL); - assert(pk != NULL); - - if (__openssl_ecdh_gen_key(pkp) < 0) - return -ECRYPT; + struct crypt_ctx * crypt; - assert(*pkp != NULL); + crypt = malloc(sizeof(*crypt)); + if (crypt == NULL) + goto fail_crypt; - pos = pk; /* i2d_PUBKEY increments the pointer, don't use buf! */ - len = i2d_PUBKEY(*pkp, &pos); - if (len < 0) { - EVP_PKEY_free(*pkp); - return -ECRYPT; - } + memset(crypt, 0, sizeof(*crypt)); - return len; + if (key != NULL) + memcpy(crypt->key, key, SYMMKEYSZ); +#ifdef HAVE_OPENSSL + crypt->ctx=openssl_crypt_create_ctx(); + if (crypt->ctx == NULL) + goto fail_ctx; +#endif + return crypt; +#ifdef HAVE_OPENSSL + fail_ctx: + free(crypt); +#endif + fail_crypt: + return NULL; } -static void openssl_ecdh_pkp_destroy(void * pkp) +void crypt_destroy_ctx(struct crypt_ctx * crypt) { - EVP_PKEY_free((EVP_PKEY *) pkp); + if (crypt == NULL) + return; + +#ifdef HAVE_OPENSSL + assert(crypt->ctx != NULL); + openssl_crypt_destroy_ctx(crypt->ctx); +#else + assert(crypt->ctx == NULL); +#endif + free(crypt); } -static int openssl_ecdh_derive(void * pkp, - uint8_t * pk, - size_t len, - uint8_t * s) +int crypt_load_privkey_file(const char * path, + void ** key) { - uint8_t * pos; - EVP_PKEY * pub; + *key = NULL; - pos = pk; /* d2i_PUBKEY increments the pointer, don't use key ptr! */ - pub = d2i_PUBKEY(NULL, (const uint8_t **) &pos, (long) len); - if (pub == NULL) - return -ECRYPT; - - if (__openssl_ecdh_derive_secret(pkp, pub, s) < 0) { - EVP_PKEY_free(pub); - return -ECRYPT; - } - - EVP_PKEY_free(pub); +#ifdef HAVE_OPENSSL + return openssl_load_privkey_file(path, key); +#else + (void) path; return 0; +#endif } -/* - * AES encryption calls. If FRCT is disabled, we should generate a - * 128-bit random IV and append it to the packet. If the flow is - * reliable, we could initialize the context once, and consider the - * stream a single encrypted message to avoid initializing the - * encryption context for each packet. - */ - -static int openssl_encrypt(struct flow * f, - struct shm_du_buff * sdb) +int crypt_load_privkey_str(const char * str, + void ** key) { - uint8_t * out; - uint8_t * in; - uint8_t * head; - uint8_t iv[IVSZ]; - int in_sz; - int out_sz; - int tmp_sz; - int ret; - - in = shm_du_buff_head(sdb); - in_sz = shm_du_buff_tail(sdb) - in; + *key = NULL; - assert(in_sz > 0); - - if (random_buffer(iv, IVSZ) < 0) - goto fail_iv; - - out = malloc(in_sz + EVP_MAX_BLOCK_LENGTH); - if (out == NULL) - goto fail_iv; - - EVP_CIPHER_CTX_reset(f->ctx); - - ret = EVP_EncryptInit_ex(f->ctx, EVP_aes_256_cbc(), NULL, f->key, iv); - if (ret != 1) - goto fail_encrypt_init; - - ret = EVP_EncryptUpdate(f->ctx, out, &tmp_sz, in, in_sz); - if (ret != 1) - goto fail_encrypt; - - out_sz = tmp_sz; - ret = EVP_EncryptFinal_ex(f->ctx, out + tmp_sz, &tmp_sz); - if (ret != 1) - goto fail_encrypt; - - out_sz += tmp_sz; - - EVP_CIPHER_CTX_cleanup(f->ctx); +#ifdef HAVE_OPENSSL + return openssl_load_privkey_str(str, key); +#else + (void) str; - assert(out_sz >= in_sz); + return 0; +#endif +} - head = shm_du_buff_head_alloc(sdb, IVSZ); - if (head == NULL) - goto fail_encrypt; +int crypt_load_pubkey_str(const char * str, + void ** key) +{ + *key = NULL; - if (shm_du_buff_tail_alloc(sdb, out_sz - in_sz) == NULL) - goto fail_tail_alloc; +#ifdef HAVE_OPENSSL + return openssl_load_pubkey_str(str, key); +#else + (void) str; - memcpy(head, iv, IVSZ); - memcpy(in, out, out_sz); + return 0; +#endif +} - free(out); +int crypt_cmp_key(const void * key1, + const void * key2) +{ +#ifdef HAVE_OPENSSL + return openssl_cmp_key(key1, key2); +#else + (void) key1; + (void) key2; return 0; - - fail_tail_alloc: - shm_du_buff_head_release(sdb, IVSZ); - fail_encrypt: - EVP_CIPHER_CTX_cleanup(f->ctx); - fail_encrypt_init: - free(out); - fail_iv: - return -ECRYPT; +#endif } -static int openssl_decrypt(struct flow * f, - struct shm_du_buff * sdb) +void crypt_free_key(void * key) { - uint8_t * in; - uint8_t * out; - uint8_t iv[IVSZ]; - int ret; - int out_sz; - int in_sz; - int tmp_sz; + if (key == NULL) + return; - in_sz = shm_du_buff_len(sdb); - if (in_sz < IVSZ) - return -ECRYPT; +#ifdef HAVE_OPENSSL + openssl_free_key(key); +#endif +} - in = shm_du_buff_head_release(sdb, IVSZ); +int crypt_load_crt_file(const char * path, + void ** crt) +{ + assert(crt != NULL); - memcpy(iv, in, IVSZ); + *crt = NULL; - in = shm_du_buff_head(sdb); - in_sz = shm_du_buff_tail(sdb) - in; +#ifdef HAVE_OPENSSL + return openssl_load_crt_file(path, crt); +#else + (void) path; - out = malloc(in_sz); - if (out == NULL) - goto fail_malloc; + return 0; +#endif +} - EVP_CIPHER_CTX_reset(f->ctx); +int crypt_load_crt_str(const char * str, + void ** crt) +{ + assert(crt != NULL); - ret = EVP_DecryptInit_ex(f->ctx, EVP_aes_256_cbc(), NULL, f->key, iv); - if (ret != 1) - goto fail_decrypt_init; + *crt = NULL; - ret = EVP_DecryptUpdate(f->ctx, out, &tmp_sz, in, in_sz); - if (ret != 1) - goto fail_decrypt; +#ifdef HAVE_OPENSSL + return openssl_load_crt_str(str, crt); +#else + (void) str; - out_sz = tmp_sz; + return 0; +#endif +} - ret = EVP_DecryptFinal_ex(f->ctx, out + tmp_sz, &tmp_sz); - if (ret != 1) - goto fail_decrypt; +int crypt_load_crt_der(const buffer_t buf, + void ** crt) +{ + assert(crt != NULL); +#ifdef HAVE_OPENSSL + return openssl_load_crt_der(buf, crt); +#else + *crt = NULL; - out_sz += tmp_sz; + (void) buf; - assert(out_sz <= in_sz); + return 0; +#endif +} - shm_du_buff_tail_release(sdb, in_sz - out_sz); +int crypt_get_pubkey_crt(void * crt, + void ** pk) +{ + assert(crt != NULL); + assert(pk != NULL); - memcpy(in, out, out_sz); +#ifdef HAVE_OPENSSL + return openssl_get_pubkey_crt(crt, pk); +#else + (void) crt; - free(out); + clrbuf(*pk); return 0; +#endif +} - fail_decrypt: - EVP_CIPHER_CTX_cleanup(f->ctx); - fail_decrypt_init: - free(out); - fail_malloc: - return -ECRYPT; - +void crypt_free_crt(void * crt) +{ + if (crt == NULL) + return; +#ifdef HAVE_OPENSSL + openssl_free_crt(crt); +#endif } -static int openssl_crypt_init(void ** ctx) +int crypt_crt_str(const void * crt, + char * buf) { - *ctx = EVP_CIPHER_CTX_new(); - if (*ctx == NULL) - return -ECRYPT; +#ifdef HAVE_OPENSSL + return openssl_crt_str(crt, buf); +#else + (void) crt; + (void) buf; return 0; +#endif } -static void openssl_crypt_fini(void * ctx) +int crypt_crt_der(const void * crt, + buffer_t * buf) { - EVP_CIPHER_CTX_free(ctx); -} + assert(crt != NULL); + assert(buf != NULL); -#endif /* HAVE_OPENSSL */ - -static int crypt_dh_pkp_create(void ** pkp, - uint8_t * pk) -{ #ifdef HAVE_OPENSSL - assert(pkp != NULL); - *pkp = NULL; - return openssl_ecdh_pkp_create(pkp, pk); + return openssl_crt_der(crt, buf); #else - (void) pkp; - (void) pk; + (void) crt; - memset(pk, 0, MSGBUFSZ); - *pkp = NULL; + clrbuf(*buf); return 0; #endif } -static void crypt_dh_pkp_destroy(void * pkp) +int crypt_check_crt_name(void * crt, + const char * name) { #ifdef HAVE_OPENSSL - openssl_ecdh_pkp_destroy(pkp); + return openssl_check_crt_name(crt, name); #else - (void) pkp; - return; + (void) crt; + (void) name; + + return 0; #endif } -static int crypt_dh_derive(void * pkp, - uint8_t * pk, - size_t len, - uint8_t * s) +struct auth_ctx * auth_create_ctx(void) { -#ifdef HAVE_OPENSSL - return openssl_ecdh_derive(pkp, pk, len, s); -#else - (void) pkp; - (void) pk; - (void) len; + struct auth_ctx * ctx; - memset(s, 0, SYMMKEYSZ); + ctx = malloc(sizeof(*ctx)); + if (ctx == NULL) + goto fail_malloc; - return -ECRYPT; + memset(ctx, 0, sizeof(*ctx)); +#ifdef HAVE_OPENSSL + ctx->store = openssl_auth_create_store(); + if (ctx->store == NULL) + goto fail_store; +#endif + return ctx; +#ifdef HAVE_OPENSSL + fail_store: + free(ctx); +#endif + fail_malloc: + return NULL; +} + +void auth_destroy_ctx(struct auth_ctx * ctx) +{ + if (ctx == NULL) + return; +#ifdef HAVE_OPENSSL + openssl_auth_destroy_store(ctx->store); #endif + free(ctx); } -static int crypt_encrypt(struct flow * f, - struct shm_du_buff * sdb) +int auth_add_crt_to_store(struct auth_ctx * ctx, + void * crt) { + assert(ctx != NULL); + assert(crt != NULL); + #ifdef HAVE_OPENSSL - return openssl_encrypt(f, sdb); + return openssl_auth_add_crt_to_store(ctx->store, crt); #else - (void) f; - (void) sdb; + (void) ctx; + (void) crt; return 0; #endif } -static int crypt_decrypt(struct flow * f, - struct shm_du_buff * sdb) +int auth_verify_crt(struct auth_ctx * ctx, + void * crt) { #ifdef HAVE_OPENSSL - return openssl_decrypt(f, sdb); + return openssl_verify_crt(ctx->store, crt); #else - (void) f; - (void) sdb; + (void) ctx; + (void) crt; - return -ECRYPT; + return 0; #endif } -static int crypt_init(void ** ctx) +int auth_sign(void * pkp, + buffer_t msg, + buffer_t * sig) { #ifdef HAVE_OPENSSL - return openssl_crypt_init(ctx); + return openssl_sign(pkp, msg, sig); #else - assert(ctx != NULL); - *ctx = NULL; + (void) pkp; + (void) msg; + (void) sig; + + clrbuf(*sig); return 0; #endif } -static void crypt_fini(void * ctx) +int auth_verify_sig(void * pk, + buffer_t msg, + buffer_t sig) { #ifdef HAVE_OPENSSL - openssl_crypt_fini(ctx); + return openssl_verify_sig(pk, msg, sig); #else - assert(ctx == NULL); - (void) ctx; + (void) pk; + (void) msg; + (void) sig; + + return 0; #endif } |