diff --git a/configure.ac b/configure.ac index 6c1f661..bcdf2f6 100644 --- a/configure.ac +++ b/configure.ac @@ -30,6 +30,8 @@ AC_CONFIG_MACRO_DIR([m4]) m4_include([m4/ax_pthread.m4]) AX_PTHREAD() +m4_include([m4/ax_openssl_version.m4]) + if test x"${cross_compiling}" = "xno" ; then # Bring additional directories where things might be found into our # search path. I don't know why autoconf doesn't do this by default @@ -213,15 +215,37 @@ AM_CONDITIONAL(HAVE_PYTHON, test "$enable_python" = yes) ## Always check for headers, then libs, then functions ### -AC_CHECK_HEADERS([openssl/aes.h openssl/bio.h openssl/evp.h openssl/hmac.h openssl/md5.h openssl/rand.h openssl/rsa.h openssl/sha.h openssl/pem.h openssl/x509.h]) +AC_CHECK_HEADERS([openssl/opensslv.h openssl/bio.h openssl/evp.h openssl/hmac.h openssl/rand.h openssl/rsa.h openssl/pem.h openssl/x509.h]) AC_CHECK_LIB([crypto],[EVP_get_digestbyname],, AC_MSG_ERROR([OpenSSL developer library 'libssl-dev' or 'openssl-devel' not installed; cannot continue.])) AC_CHECK_LIB([md],[MD5]) # if libmd is available, get it -AC_CHECK_FUNCS([MD5 SHA1 AES_encrypt RAND_bytes des_read_pw_string EVP_read_pw_string EVP_MD_size]) -AC_CHECK_FUNCS([PEM_read_bio_RSA_PUBKEY]) +AC_CHECK_FUNCS([PEM_read_bio_RSA_PUBKEY des_read_pw_string EVP_read_pw_string EVP_MD_size]) + +dnl Detect the OpenSSL version in use (works with OpenSSL 1.0.2, 1.1.1, +dnl 3.x, 4.x and LibreSSL, including when cross-compiling). +AX_OPENSSL_VERSION + +dnl The crypto code is written against the stable EVP API (EVP_MD_CTX, +dnl EVP_CIPHER_CTX), which is supported on OpenSSL 1.0.2, 1.1.1, 3.x, 4.x +dnl and LibreSSL. +dnl Define HAVE_MD5 / HAVE_SHA1 / HAVE_AES_ENCRYPT -- previously derived from +dnl the now-deprecated low-level MD5/SHA1/AES symbols -- from probes of the +dnl EVP symbols instead. This keeps the build clean on OpenSSL 3.x and 4.x +dnl (including no-deprecated builds) and working on OpenSSL 1.0.2/1.1.1 +dnl (where EVP_MD_CTX_create/destroy are used) and LibreSSL. +AC_CHECK_FUNCS([EVP_MD_CTX_new EVP_MD_CTX_free EVP_MD_CTX_create EVP_MD_CTX_destroy EVP_DigestInit_ex EVP_DigestUpdate EVP_DigestFinal_ex]) +AC_CHECK_FUNCS([EVP_CIPHER_CTX_new EVP_CIPHER_CTX_free EVP_CIPHER_CTX_copy EVP_EncryptInit_ex EVP_EncryptUpdate EVP_EncryptFinal_ex EVP_DecryptInit_ex EVP_DecryptUpdate EVP_DecryptFinal_ex EVP_CIPHER_CTX_set_padding]) +AC_CHECK_FUNCS([EVP_md5 EVP_sha1 EVP_sha256 EVP_aes_128_cbc EVP_aes_256_cbc EVP_aes_256_ecb RAND_bytes]) + +AS_IF([test "$ac_cv_func_EVP_md5" = "yes" && (test "$ac_cv_func_EVP_MD_CTX_new" = "yes" || test "$ac_cv_func_EVP_MD_CTX_create" = "yes")], + [AC_DEFINE([HAVE_MD5],1,[Define to 1 if OpenSSL provides MD5 through the EVP API])]) +AS_IF([test "$ac_cv_func_EVP_sha1" = "yes" && (test "$ac_cv_func_EVP_MD_CTX_new" = "yes" || test "$ac_cv_func_EVP_MD_CTX_create" = "yes")], + [AC_DEFINE([HAVE_SHA1],1,[Define to 1 if OpenSSL provides SHA1 through the EVP API])]) +AS_IF([test "$ac_cv_func_EVP_aes_256_cbc" = "yes" && test "$ac_cv_func_EVP_CIPHER_CTX_new" = "yes" && test "$ac_cv_func_EVP_CIPHER_CTX_copy" = "yes"], + [AC_DEFINE([HAVE_AES_ENCRYPT],1,[Define to 1 if OpenSSL provides AES-256 through the EVP API])]) ################################################################ ## FUSE: Filesystem in Userspace @@ -296,6 +320,7 @@ AC_MSG_NOTICE([PYTHON Bindings: ${enable_python}]) AC_MSG_NOTICE([QEMU Image Drivers: ${enable_qemu}]) AC_MSG_NOTICE([FUSE: ${enable_fuse}]) AC_MSG_NOTICE([LIBEXPAT: ${have_expat} (needed for AFF signatures)]) +AC_MSG_NOTICE([OpenSSL: ${OPENSSL_VERSION} (EVP-based digest and cipher APIs)]) AC_MSG_NOTICE([]) AC_MSG_NOTICE([CFLAGS: ${CFLAGS}]) AC_MSG_NOTICE([CPPFLAGS: ${CPPFLAGS}]) diff --git a/include/afflib/afflib.h b/include/afflib/afflib.h index 4fb24e9..90ac78d 100644 --- a/include/afflib/afflib.h +++ b/include/afflib/afflib.h @@ -336,6 +336,8 @@ int af_display_as_hex(const char *segname); // afflib recommends displaying this /* Crypto */ /* AFF Base Encryption */ +int af_MD5(const unsigned char *buf,size_t buflen,unsigned char md[16]); // return 0 if success, -1 if no cipher +int af_sha1(const unsigned char *buf,size_t buflen,unsigned char md[20]); // return 0 if success, -1 if no cipher int af_SHA256(const unsigned char *buf,size_t buflen,unsigned char md[32]); // return 0 if success, -1 if no cipher int af_set_aes_key(AFFILE *af,const unsigned char *userKey,const int bits); int af_cannot_decrypt(AFFILE *af); // encrypted pages are present which cannot be decrypted diff --git a/include/afflib/afflib_i.h b/include/afflib/afflib_i.h index efa2019..38d75fb 100644 --- a/include/afflib/afflib_i.h +++ b/include/afflib/afflib_i.h @@ -105,11 +105,29 @@ void __cdecl AFDbgPrint (PCSTR Format,...); #endif #ifdef HAVE_LIBCRYPTO -#include +#include #include // a standard part of OpenSSL #include -#include -#include +#include // X509 is used by struct af_crypto +/* The AES block size (16 bytes). Formerly taken from ; + * that header is deprecated/removed in OpenSSL 3.x/4.x no-deprecated builds, + * so define it locally (AES blocks are always 16 bytes). + */ +#define AFF_AES_BLOCK_SIZE 16 + +/* Support OpenSSL before 1.1.0 and LibreSSL before 2.7.0 + * (renamed EVP_MD_CTX_create/destroy to EVP_MD_CTX_new/free). + * Note: EVP_CIPHER_CTX_new/free already exist under those names in all versions. + */ +#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)) || \ + (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) +#ifndef EVP_MD_CTX_new +#define EVP_MD_CTX_new EVP_MD_CTX_create +#endif +#ifndef EVP_MD_CTX_free +#define EVP_MD_CTX_free EVP_MD_CTX_destroy +#endif +#endif #endif #ifdef HAVE_PTHREAD @@ -432,9 +450,9 @@ struct af_crypto { uint32_t sealing_key_set:1; // encryption key has been set uint32_t auto_encrypt:1; // encrypt segments when we write uint32_t auto_decrypt:1; // automatically decrypto when we read -#ifdef AES_BLOCK_SIZE - AES_KEY ekey; // encrypt key - AES_KEY dkey; // decrypt key +#ifdef HAVE_AES_ENCRYPT + EVP_CIPHER_CTX *ekey; // EVP cipher context holding the encryption key (AES-256-CBC); NULL until set + EVP_CIPHER_CTX *dkey; // EVP cipher context holding the decryption key (AES-256-CBC); NULL until set #endif #ifdef HAVE_OPENSSL_EVP_H EVP_PKEY *sign_privkey; // signing private key (to write signatures) diff --git a/lib/Makefile.am b/lib/Makefile.am index 6f6de4f..e9b9be8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -28,7 +28,6 @@ libafflib_la_SOURCES = $(AFFLIB_SOURCES) QEMU_SOURCES = \ vnode_qemu.cpp \ vnode_qemu.h \ - qemu/aes.h \ qemu/aff-block-vmdk.h \ qemu/block-bochs.c \ qemu/block-cloop.c \ diff --git a/lib/afflib.cpp b/lib/afflib.cpp index a9d13d0..e6de193 100644 --- a/lib/afflib.cpp +++ b/lib/afflib.cpp @@ -32,6 +32,7 @@ #include #include +#include // vnode implementations. @@ -74,8 +75,10 @@ void af_initialize() assert(sizeof(struct af_segment_tail)==8); assert(sizeof(struct affkey)==AFFKEY_SIZE); - /* Make sure OpenSSL is working */ +#if OPENSSL_VERSION_NUMBER < 0x10100000L + /* Make sure OpenSSL is working (auto-initialized in OpenSSL 1.1.0+) */ OpenSSL_add_all_algorithms(); +#endif const char *val = getenv(AFFLIB_CACHE_DEBUG); if(val) af_cache_debug = atoi(val); @@ -637,87 +640,153 @@ int af_make_gid(AFFILE *af) /* Decrypt data and perform unblocking if necessary. - * This could eliminate a memory copy by doing the decryption for everything - * but the last block in place, and just doing a copy for the last block. + * Caller must hold AF_READLOCK(af). + * Uses a per-call EVP_CIPHER_CTX copied from the immutable af->crypto->dkey + * template so concurrent reads under AF_READLOCK do not share mutable state. + * Returns 0 on success, -1 on error (never fails open). */ -void af_aes_decrypt(AFFILE *af,const char *segname,unsigned char *data,size_t *datalen) +static int af_aes_decrypt_buf(AFFILE *af,const char *segname,unsigned char *data,size_t *datalen) { #ifdef HAVE_AES_ENCRYPT - if(datalen==0) return; // can't decrypt; no clue how long it is + if(datalen==0) return -1; // can't decrypt; no clue how long it is /* An encrypted segment was retrieved; decrypt and trunc the length as necessary */ - uint32_t extra = (*datalen) % AES_BLOCK_SIZE; - uint32_t pad = (AES_BLOCK_SIZE - extra) % AES_BLOCK_SIZE; + uint32_t extra = (*datalen) % AFF_AES_BLOCK_SIZE; + uint32_t pad = (AFF_AES_BLOCK_SIZE - extra) % AFF_AES_BLOCK_SIZE; + + if(extra!=0 && *datalen < AFF_AES_BLOCK_SIZE){ + *datalen = 0; // something is wrong + return -1; + } if(data==0){ // just wants to find out new length if(extra>0){ - *datalen -= AES_BLOCK_SIZE; + *datalen -= AFF_AES_BLOCK_SIZE; } - return; + return 0; } - if(extra!=0 && *datalen < AES_BLOCK_SIZE){ - *datalen = 0; // something is wrong - return; + *datalen -= extra; // *datalen is now a multiple of AFF_AES_BLOCK_SIZE + if(*datalen == 0){ + return 0; } - - if(data==0){ // just report the new size - if(extra!=0) *datalen -= AES_BLOCK_SIZE; // a block was added - return; + if(!af->crypto || !af->crypto->dkey){ + return -1; } - *datalen -= extra; // *datalen is now a multiple of AES_BLOCK_SIZE /* Create an IV */ - unsigned char iv[AES_BLOCK_SIZE]; + unsigned char iv[AFF_AES_BLOCK_SIZE]; memset(iv,0,sizeof(iv)); strlcpy((char *)iv,segname,sizeof(iv)); - /* Decrypt! */ - AF_READLOCK(af); - AES_cbc_encrypt(data,data,*datalen,&af->crypto->dkey,iv,AES_DECRYPT); - AF_UNLOCK(af); + /* Decrypt in place, using the stable EVP API (the legacy + * AES_cbc_encrypt is deprecated since OpenSSL 3.0 and hidden in + * no-deprecated OpenSSL 3.4+/4.0 builds). The EVP result is + * byte-identical to the legacy call for whole-block inputs. + */ + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + if(!ctx) return -1; + + int ok = 0; + if(EVP_CIPHER_CTX_copy(ctx,af->crypto->dkey)==1 && + EVP_DecryptInit_ex(ctx,NULL,NULL,NULL,iv)==1 && + EVP_CIPHER_CTX_set_padding(ctx,0)==1){ + const size_t max_chunk = (size_t)1 << 30; // 1 GiB: multiple of AFF_AES_BLOCK_SIZE and < INT_MAX + size_t total = *datalen; + size_t offset = 0; + ok = 1; + while(offset < total){ + int chunk = (int)((total - offset) > max_chunk ? max_chunk : (total - offset)); + int outlen = 0; + if(EVP_DecryptUpdate(ctx,data + offset,&outlen,data + offset,chunk)!=1 || outlen!=chunk){ + ok = 0; + break; + } + offset += (size_t)chunk; + } + if(ok){ + int flen = 0; + unsigned char final_buf[AFF_AES_BLOCK_SIZE]; + if(EVP_DecryptFinal_ex(ctx,final_buf,&flen)!=1 || flen!=0){ + ok = 0; + } + } + } + EVP_CIPHER_CTX_free(ctx); + if(!ok) return -1; + *datalen -= pad; // remove the padding + return 0; +#else + return -1; #endif } +/* Kept for backwards symbol compatibility; internal callers use af_aes_decrypt_buf(). */ +void af_aes_decrypt(AFFILE *af,const char *segname,unsigned char *data,size_t *datalen) +{ + AF_READLOCK(af); + (void)af_aes_decrypt_buf(af,segname,data,datalen); + AF_UNLOCK(af); +} + int af_get_seg(AFFILE *af,const char *segname,uint32_t *arg,unsigned char *data,size_t *datalen) { AF_READLOCK(af); if(af->v->get_seg==0){ errno = ENOTSUP; + AF_UNLOCK(af); return -1; // not supported by this file system } #ifdef HAVE_AES_ENCRYPT /* If we have encryption and it is turned on, check for encrypted segment first */ - if(AF_SEALING_VNODE(af) && af->crypto->auto_decrypt){ + if(AF_SEALING_VNODE(af) && af->crypto && af->crypto->auto_decrypt){ size_t datalen_orig = datalen ? *datalen : 0; char aesname[AF_MAX_NAME_LEN]; strlcpy(aesname,segname,sizeof(aesname)); strlcat(aesname,AF_AES256_SUFFIX,sizeof(aesname)); int r = (*af->v->get_seg)(af,aesname,arg,data,datalen); if(r==0){ - af_aes_decrypt(af,segname,data,datalen); + if(af_aes_decrypt_buf(af,segname,data,datalen)!=0){ + AF_UNLOCK(af); + return -1; + } + AF_UNLOCK(af); return 0; } - if(r==AF_ERROR_DATASMALL && datalen && (*datalen % AES_BLOCK_SIZE != 0)){ + if(r==AF_ERROR_DATASMALL && datalen && (*datalen % AFF_AES_BLOCK_SIZE != 0)){ /* Not enough space was provided to decrypt, probably because this was blocked out */ - size_t bigger_data_len = datalen_orig + AES_BLOCK_SIZE; + if(datalen_orig > ((size_t)-1) - AFF_AES_BLOCK_SIZE){ + AF_UNLOCK(af); + return -1; + } + size_t bigger_data_len = datalen_orig + AFF_AES_BLOCK_SIZE; unsigned char *bigger_data = (unsigned char *)malloc(bigger_data_len); - if(!bigger_data) return -1; // Malloc failed + if(!bigger_data){ + AF_UNLOCK(af); + return -1; // Malloc failed + } r = (*af->v->get_seg)(af,aesname,arg,bigger_data,&bigger_data_len); if(r!=0){ free(bigger_data); + AF_UNLOCK(af); return -1; // something deeper is wrong } - af_aes_decrypt(af,segname,bigger_data,&bigger_data_len); + if(af_aes_decrypt_buf(af,segname,bigger_data,&bigger_data_len)!=0){ + free(bigger_data); + AF_UNLOCK(af); + return -1; + } if(bigger_data_len > datalen_orig){ free(bigger_data); + AF_UNLOCK(af); return -1; // it's still too big } memcpy(data,bigger_data,bigger_data_len); *datalen = bigger_data_len; free(bigger_data); + AF_UNLOCK(af); return 0; // finally it fits } } @@ -742,7 +811,7 @@ int af_get_next_seg(AFFILE *af,char *segname,size_t segname_len,uint32_t *arg, #ifdef HAVE_AES_ENCRYPT if(AF_SEALING_VNODE(af) && ends_with(segname,AF_AES256_SUFFIX) - && af->crypto->auto_decrypt){ + && af->crypto && af->crypto->auto_decrypt){ segname[strlen(segname)-strlen(AF_AES256_SUFFIX)] = 0; /* An encrypted segment was retrieved. * If it fit, decrypt and return. @@ -752,11 +821,14 @@ int af_get_next_seg(AFFILE *af,char *segname,size_t segname_len,uint32_t *arg, * so the caller normally leaves enough space. */ if(r==0){ - af_aes_decrypt(af,segname,data,datalen); + if(af_aes_decrypt_buf(af,segname,data,datalen)!=0){ + AF_UNLOCK(af); + return -1; + } AF_UNLOCK(af); return 0; } - if(r==AF_ERROR_DATASMALL && datalen && (*datalen % AES_BLOCK_SIZE !=0)){ + if(r==AF_ERROR_DATASMALL && datalen && (*datalen % AFF_AES_BLOCK_SIZE !=0)){ *datalen = datalen_orig; AF_UNLOCK(af); return af_get_seg(af,segname,arg,data,datalen); @@ -809,8 +881,13 @@ int af_update_segf(AFFILE *af, const char *segname, unsigned char *newdata = 0; char aesname[AF_MAX_NAME_LEN]; if(AF_SEALING(af) && ((flag & AF_SIGFLAG_NOSEAL)==0) && af->crypto->auto_encrypt){ + if(!af->crypto->ekey){ + AF_UNLOCK(af); + return -1; + } + /* Create an IV */ - unsigned char iv[AES_BLOCK_SIZE]; + unsigned char iv[AFF_AES_BLOCK_SIZE]; memset(iv,0,sizeof(iv)); strlcpy((char *)iv,segname,sizeof(iv)); @@ -820,32 +897,83 @@ int af_update_segf(AFFILE *af, const char *segname, oldname = segname; segname = aesname; - /* Figure out if we need to padd out for encryption. Allocate space and + /* Figure out if we need to pad out for encryption. Allocate space and + * check for integer overflow before allocating. */ - uint32_t extra = (datalen) % AES_BLOCK_SIZE; - uint32_t pad = (AES_BLOCK_SIZE - extra) % AES_BLOCK_SIZE; - newdata = (unsigned char *)malloc(datalen+pad+extra); - memset(newdata+datalen,pad+extra,pad); // PKCS7 uses 01 for one pad byte, 02 02 for two, etc. - /* Encrypt */ - AES_cbc_encrypt((const unsigned char *)data, - newdata, - datalen+pad,&af->crypto->ekey,iv,AES_ENCRYPT); + uint32_t extra = (datalen) % AFF_AES_BLOCK_SIZE; + uint32_t pad = (AFF_AES_BLOCK_SIZE - extra) % AFF_AES_BLOCK_SIZE; + if(datalen > UINT32_MAX - (pad + extra)){ + errno = EOVERFLOW; + AF_UNLOCK(af); + return -1; + } + size_t alloc_len = (size_t)datalen + pad + extra; + newdata = (unsigned char *)malloc(alloc_len > 0 ? alloc_len : 1); + if(!newdata){ + AF_UNLOCK(af); + return -1; + } + if(datalen > 0) memcpy(newdata,(const unsigned char *)data,datalen); + if(pad > 0) memset(newdata+datalen,pad+extra,pad); // PKCS7 uses 01 for one pad byte, 02 02 for two, etc. + if(extra > 0) memset(newdata+datalen+pad,0,extra); // zero the trailing alignment region + /* Encrypt, using the stable EVP API (the legacy AES_cbc_encrypt is + * deprecated since OpenSSL 3.0 and hidden in no-deprecated OpenSSL + * 3.4+/4.0 builds). Encrypting from newdata also removes the legacy + * over-read of 'pad' bytes past the caller's data buffer. + */ + size_t enc_len = (size_t)datalen + pad; + if(enc_len > 0){ + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + int ok = 0; + if(ctx && + EVP_CIPHER_CTX_copy(ctx,af->crypto->ekey)==1 && + EVP_EncryptInit_ex(ctx,NULL,NULL,NULL,iv)==1 && + EVP_CIPHER_CTX_set_padding(ctx,0)==1){ + const size_t max_chunk = (size_t)1 << 30; // 1 GiB: multiple of AFF_AES_BLOCK_SIZE and < INT_MAX + size_t offset = 0; + ok = 1; + while(offset < enc_len){ + int chunk = (int)((enc_len - offset) > max_chunk ? max_chunk : (enc_len - offset)); + int ctlen = 0; + if(EVP_EncryptUpdate(ctx,newdata + offset,&ctlen,newdata + offset,chunk)!=1 || ctlen!=chunk){ + ok = 0; + break; + } + offset += (size_t)chunk; + } + if(ok){ + int flen = 0; + unsigned char final_buf[AFF_AES_BLOCK_SIZE]; + if(EVP_EncryptFinal_ex(ctx,final_buf,&flen)!=1 || flen!=0){ + ok = 0; + } + } + } + EVP_CIPHER_CTX_free(ctx); + if(!ok){ + OPENSSL_cleanse(newdata,alloc_len); + free(newdata); + AF_UNLOCK(af); + return -1; + } + } data = newdata; // we will write this out datalen += pad + extra; } #endif int r = (*af->v->update_seg)(af,segname,arg,data,datalen); // actually update the segment - if(r < 0) - { AF_UNLOCK(af); return r; } + if(r < 0){ +#ifdef HAVE_AES_ENCRYPT + if(newdata) free(newdata); +#endif + AF_UNLOCK(af); + return r; + } af->bytes_written += datalen; #ifdef HAVE_AES_ENCRYPT /* if we encrypted, make sure the unencrypted segment is deleted */ if(oldname && af->v->del_seg) (*af->v->del_seg)(af,oldname); - if(newdata){ - free(newdata); // free any allocated data - newdata = 0; - } #endif /* If we wrote out an unencrypted segment, make sure that the corresopnding encrypted * segment is deleted. @@ -870,6 +998,12 @@ int af_update_segf(AFFILE *af, const char *segname, // return code doesn't matter; it's either signed or not. af_sign_seg3(af,segname,arg,signdata,datalen,AF_SIGNATURE_MODE0); } +#endif +#ifdef HAVE_AES_ENCRYPT + if(newdata){ + free(newdata); // free any allocated data + newdata = 0; + } #endif AF_UNLOCK(af); return r; diff --git a/lib/afflib_pages.cpp b/lib/afflib_pages.cpp index 8960858..647b17a 100644 --- a/lib/afflib_pages.cpp +++ b/lib/afflib_pages.cpp @@ -388,7 +388,9 @@ int af_update_page(AFFILE *af,int64_t pagenum,unsigned char *data,int datalen) if(af->write_md5){ unsigned char md5_buf[16]; char md5name_buf[32]; - MD5(data,datalen,md5_buf); + if(af_MD5((const unsigned char *)data,datalen,md5_buf)!=0){ + return -1; + } snprintf(md5name_buf,sizeof(md5name_buf),AF_PAGE_MD5,pagenum); af_update_segf(af,md5name_buf,0,md5_buf,sizeof(md5_buf),AF_SIGFLAG_NOSIG); // ignore failure } @@ -398,7 +400,9 @@ int af_update_page(AFFILE *af,int64_t pagenum,unsigned char *data,int datalen) if(af->write_sha1){ unsigned char sha1_buf[20]; char sha1name_buf[32]; - SHA1(data,datalen,sha1_buf); + if(af_sha1((const unsigned char *)data,datalen,sha1_buf)!=0){ + return -1; + } snprintf(sha1name_buf,sizeof(sha1name_buf),AF_PAGE_SHA1,pagenum); af_update_segf(af,sha1name_buf,0,sha1_buf,sizeof(sha1_buf),AF_SIGFLAG_NOSIG); // ignore failure } diff --git a/lib/aftest.cpp b/lib/aftest.cpp index 9fc6738..d89d99e 100644 --- a/lib/aftest.cpp +++ b/lib/aftest.cpp @@ -38,11 +38,8 @@ const char *tempdir = "/tmp/"; #define MIN(x,y) ((x)<(y)?(x):(y)) #endif -/* Support OpenSSL before 1.1.0 */ -#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) -#define EVP_MD_CTX_new EVP_MD_CTX_create -#define EVP_MD_CTX_free EVP_MD_CTX_destroy -#endif +/* Note: OpenSSL < 1.1.0 / LibreSSL < 2.7.0 EVP_MD_CTX_new/free shims + * are provided by include/afflib/afflib_i.h. */ const char *filename(char *buf,int buflen,const char *base) { diff --git a/lib/crypto.cpp b/lib/crypto.cpp index 3e38267..b3821b0 100644 --- a/lib/crypto.cpp +++ b/lib/crypto.cpp @@ -25,11 +25,9 @@ using namespace std; #include #endif -/* Support OpenSSL before 1.1.0 */ -#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) -#define EVP_MD_CTX_new EVP_MD_CTX_create -#define EVP_MD_CTX_free EVP_MD_CTX_destroy -#endif +/* Note: support for OpenSSL before 1.1.0 and LibreSSL before 2.7.0 + * (EVP_MD_CTX_new/free aliases) is provided in include/afflib/afflib_i.h + */ /**************************************************************** @@ -76,21 +74,75 @@ void af_crypto_allocate(AFFILE *af) } -/** compute SHA256. +/** compute MD5 using the stable EVP API (the one-shot MD5() function is + * deprecated since OpenSSL 3.0). + * Return 0 if success, -1 if error. + */ +int af_MD5(const unsigned char *data,size_t datalen,unsigned char md[16]) +{ + if(!md || (datalen > 0 && !data)) return -1; + + const EVP_MD *md5 = EVP_md5(); + if(!md5) return -1; + + unsigned int md5_buflen = 0; + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + if(!ctx) return -1; + if(EVP_DigestInit_ex(ctx,md5,NULL)!=1 || + (datalen > 0 && EVP_DigestUpdate(ctx,data,datalen)!=1) || + EVP_DigestFinal_ex(ctx,md,&md5_buflen)!=1 || + md5_buflen != 16){ + EVP_MD_CTX_free(ctx); + return -1; + } + EVP_MD_CTX_free(ctx); + return 0; +} + +/** compute SHA1 using the stable EVP API (the one-shot SHA1() function is + * deprecated since OpenSSL 3.0). + * Return 0 if success, -1 if error. + */ +int af_sha1(const unsigned char *data,size_t datalen,unsigned char md[20]) +{ + if(!md || (datalen > 0 && !data)) return -1; + + const EVP_MD *sha1 = EVP_sha1(); + if(!sha1) return -1; + + unsigned int sha1_buflen = 0; + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + if(!ctx) return -1; + if(EVP_DigestInit_ex(ctx,sha1,NULL)!=1 || + (datalen > 0 && EVP_DigestUpdate(ctx,data,datalen)!=1) || + EVP_DigestFinal_ex(ctx,md,&sha1_buflen)!=1 || + sha1_buflen != 20){ + EVP_MD_CTX_free(ctx); + return -1; + } + EVP_MD_CTX_free(ctx); + return 0; +} + +/** compute SHA256 using the stable EVP API. * Return 0 if success, -1 if error. */ int af_SHA256(const unsigned char *data,size_t datalen,unsigned char md[32]) { - const EVP_MD *sha256 = EVP_get_digestbyname("SHA256"); + if(!md || (datalen > 0 && !data)) return -1; + + const EVP_MD *sha256 = EVP_sha256(); if(!sha256) return -1; - uint32_t sha256_buflen = 32; + unsigned int sha256_buflen = 0; EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - EVP_DigestInit(ctx,sha256); - EVP_DigestUpdate(ctx,data,datalen); - if(EVP_DigestFinal(ctx,md,&sha256_buflen)!=1){ // EVP_DigestFinal returns 1 for success - EVP_MD_CTX_free(ctx); - return -1; + if(!ctx) return -1; + if(EVP_DigestInit_ex(ctx,sha256,NULL)!=1 || + (datalen > 0 && EVP_DigestUpdate(ctx,data,datalen)!=1) || + EVP_DigestFinal_ex(ctx,md,&sha256_buflen)!=1 || + sha256_buflen != 32){ + EVP_MD_CTX_free(ctx); + return -1; } EVP_MD_CTX_free(ctx); return 0; @@ -98,9 +150,11 @@ int af_SHA256(const unsigned char *data,size_t datalen,unsigned char md[32]) void af_crypto_deallocate(AFFILE *af) { -#ifdef AES_BLOCK_SIZE - memset(&af->crypto->ekey,0,sizeof(af->crypto->ekey)); - memset(&af->crypto->dkey,0,sizeof(af->crypto->dkey)); +#ifdef HAVE_AES_ENCRYPT + EVP_CIPHER_CTX_free(af->crypto->ekey); // EVP_CIPHER_CTX_free(NULL) is a no-op + af->crypto->ekey = 0; + EVP_CIPHER_CTX_free(af->crypto->dkey); // EVP_CIPHER_CTX_free(NULL) is a no-op + af->crypto->dkey = 0; #endif #ifdef HAVE_PEM_READ_BIO_RSA_PUBKEY if(af->crypto->sign_privkey){ @@ -126,17 +180,53 @@ int af_set_aes_key(AFFILE *af,const unsigned char *userKey,const int bits) #ifdef HAVE_AES_ENCRYPT if(af->crypto->sealing_key_set){ if(userKey==0){ // key was set and it is being cleared + EVP_CIPHER_CTX_free(af->crypto->ekey); + af->crypto->ekey = 0; + EVP_CIPHER_CTX_free(af->crypto->dkey); + af->crypto->dkey = 0; af->crypto->sealing_key_set = 0; return 0; } return AF_ERROR_KEY_SET; // key is already set } - int r; - r = AES_set_encrypt_key(userKey,bits,&af->crypto->ekey); - if(r) return r; - r = AES_set_decrypt_key(userKey,bits,&af->crypto->dkey); - if(r) return r; + /* Set up immutable template EVP cipher contexts. The legacy + * AES_set_encrypt_key/AES_set_decrypt_key calls are deprecated since + * OpenSSL 3.0 and hidden in no-deprecated OpenSSL 3.4+/4.0 builds, so + * we use the stable EVP API instead. Callers copy these templates into + * per-operation contexts via EVP_CIPHER_CTX_copy() so concurrent reads + * under AF_READLOCK do not share mutable cipher state. + */ + const unsigned char *key = userKey; + if(key==0 || bits<=0 || (bits%8)!=0) return -1; // invalid key length + size_t keylen = (size_t)bits/8; + if(keylen<16 || keylen>32) return -1; // invalid key length + const EVP_CIPHER *cipher = + (bits==128) ? EVP_aes_128_cbc() : + (bits==192) ? EVP_aes_192_cbc() : + (bits==256) ? EVP_aes_256_cbc() : 0; + if(!cipher) return -1; + + EVP_CIPHER_CTX *ekey = EVP_CIPHER_CTX_new(); + EVP_CIPHER_CTX *dkey = EVP_CIPHER_CTX_new(); + if(!ekey || !dkey){ + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + return -1; + } + if(EVP_EncryptInit_ex(ekey,cipher,NULL,key,NULL)!=1 || // sets cipher+key, no IV yet + EVP_DecryptInit_ex(dkey,cipher,NULL,key,NULL)!=1 || + EVP_CIPHER_CTX_set_padding(ekey,0)!=1 || + EVP_CIPHER_CTX_set_padding(dkey,0)!=1){ + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + return -1; + } + + EVP_CIPHER_CTX_free(af->crypto->ekey); + EVP_CIPHER_CTX_free(af->crypto->dkey); + af->crypto->ekey = ekey; + af->crypto->dkey = dkey; af->crypto->sealing_key_set = 1; af->crypto->auto_encrypt = 1; // default @@ -162,7 +252,9 @@ int af_save_aes_key_with_passphrase(AFFILE *af,const char *passphrase, const u_c /* Make an encrypted copy of the AFFkey */ unsigned char passphrase_hash[32]; - af_SHA256((const unsigned char *)passphrase, strlen(passphrase), passphrase_hash); + if(af_SHA256((const unsigned char *)passphrase, strlen(passphrase), passphrase_hash)){ + return AF_ERROR_NO_SHA256; + } struct affkey affkey_seg; assert(sizeof(affkey_seg)==AFFKEY_SIZE); @@ -172,18 +264,44 @@ int af_save_aes_key_with_passphrase(AFFILE *af,const char *passphrase, const u_c memcpy(affkey_seg.version,(u_char *)&version_number,4); memcpy(affkey_seg.affkey_aes256,affkey,32); - /* Use the hash to encrypt the key and all zeros */ - AES_KEY ekey; - AES_set_encrypt_key(passphrase_hash,256,&ekey); - AES_encrypt(affkey_seg.affkey_aes256, - affkey_seg.affkey_aes256,&ekey); - AES_encrypt(affkey_seg.affkey_aes256+AES_BLOCK_SIZE, - affkey_seg.affkey_aes256+AES_BLOCK_SIZE,&ekey); - AES_encrypt(affkey_seg.zeros_aes256,affkey_seg.zeros_aes256,&ekey); + /* Use the hash to encrypt the key and all zeros. + * (AES-256-ECB, three 16-byte blocks, via the stable EVP API -- + * the legacy AES_set_encrypt_key/AES_encrypt are deprecated since + * OpenSSL 3.0. The result is byte-identical to the legacy code.) + */ + EVP_CIPHER_CTX *ectx = EVP_CIPHER_CTX_new(); + if(!ectx){ + OPENSSL_cleanse(passphrase_hash,sizeof(passphrase_hash)); + OPENSSL_cleanse(&affkey_seg,sizeof(affkey_seg)); // erase the temp data + return -1; + } + int blen1 = 0, blen2 = 0, blen3 = 0, flen = 0, ok = 0; + unsigned char final_buf[AFF_AES_BLOCK_SIZE]; + if(EVP_EncryptInit_ex(ectx,EVP_aes_256_ecb(),NULL,passphrase_hash,NULL)==1 && + EVP_CIPHER_CTX_set_padding(ectx,0)==1){ // must be called after Init_ex + ok = EVP_EncryptUpdate(ectx,affkey_seg.affkey_aes256,&blen1, + affkey_seg.affkey_aes256,AFF_AES_BLOCK_SIZE)==1 + && blen1 == AFF_AES_BLOCK_SIZE + && EVP_EncryptUpdate(ectx,affkey_seg.affkey_aes256+AFF_AES_BLOCK_SIZE,&blen2, + affkey_seg.affkey_aes256+AFF_AES_BLOCK_SIZE,AFF_AES_BLOCK_SIZE)==1 + && blen2 == AFF_AES_BLOCK_SIZE + && EVP_EncryptUpdate(ectx,affkey_seg.zeros_aes256,&blen3, + affkey_seg.zeros_aes256,AFF_AES_BLOCK_SIZE)==1 + && blen3 == AFF_AES_BLOCK_SIZE + && EVP_EncryptFinal_ex(ectx,final_buf,&flen)==1 + && flen == 0; + } + EVP_CIPHER_CTX_free(ectx); + OPENSSL_cleanse(passphrase_hash,sizeof(passphrase_hash)); + if(!ok){ + OPENSSL_cleanse(&affkey_seg,sizeof(affkey_seg)); // erase the temp data + return -1; + } /* Write this to a segment */ - if(af_update_seg(af,AF_AFFKEY,0,(const u_char *)&affkey_seg,sizeof(affkey_seg))) return -1; - memset((unsigned char *)&affkey_seg,0,sizeof(affkey_seg)); // erase the temp data + int r = af_update_seg(af,AF_AFFKEY,0,(const u_char *)&affkey_seg,sizeof(affkey_seg)); + OPENSSL_cleanse(&affkey_seg,sizeof(affkey_seg)); // erase the temp data + if(r) return -1; return 0; #endif #if !defined(HAVE_AES_ENCRYPT) @@ -253,24 +371,51 @@ int af_get_aes_key_from_passphrase(AFFILE *af,const char *passphrase, return AF_ERROR_NO_SHA256; } - /* Try to decrypt the key */ - - AES_KEY dkey; - AES_set_decrypt_key(passphrase_hash,256,&dkey); - AES_decrypt(affkey_seg.affkey_aes256, - affkey_seg.affkey_aes256,&dkey); - AES_decrypt(affkey_seg.affkey_aes256+AES_BLOCK_SIZE, - affkey_seg.affkey_aes256+AES_BLOCK_SIZE,&dkey); - AES_decrypt(affkey_seg.zeros_aes256,affkey_seg.zeros_aes256,&dkey); + /* Try to decrypt the key. + * (AES-256-ECB, three 16-byte blocks, via the stable EVP API -- + * the legacy AES_set_decrypt_key/AES_decrypt are deprecated since + * OpenSSL 3.0. The result is byte-identical to the legacy code.) + */ + EVP_CIPHER_CTX *dctx = EVP_CIPHER_CTX_new(); + if(!dctx){ + OPENSSL_cleanse(passphrase_hash,sizeof(passphrase_hash)); + OPENSSL_cleanse(&affkey_seg,sizeof(affkey_seg)); // erase the temp data + return AF_ERROR_NO_AES; + } + int dlen1 = 0, dlen2 = 0, dlen3 = 0, flen = 0, dok = 0; + unsigned char final_buf[AFF_AES_BLOCK_SIZE]; + if(EVP_DecryptInit_ex(dctx,EVP_aes_256_ecb(),NULL,passphrase_hash,NULL)==1 && + EVP_CIPHER_CTX_set_padding(dctx,0)==1){ // must be called after Init_ex + dok = EVP_DecryptUpdate(dctx,affkey_seg.affkey_aes256,&dlen1, + affkey_seg.affkey_aes256,AFF_AES_BLOCK_SIZE)==1 + && dlen1 == AFF_AES_BLOCK_SIZE + && EVP_DecryptUpdate(dctx,affkey_seg.affkey_aes256+AFF_AES_BLOCK_SIZE,&dlen2, + affkey_seg.affkey_aes256+AFF_AES_BLOCK_SIZE,AFF_AES_BLOCK_SIZE)==1 + && dlen2 == AFF_AES_BLOCK_SIZE + && EVP_DecryptUpdate(dctx,affkey_seg.zeros_aes256,&dlen3, + affkey_seg.zeros_aes256,AFF_AES_BLOCK_SIZE)==1 + && dlen3 == AFF_AES_BLOCK_SIZE + && EVP_DecryptFinal_ex(dctx,final_buf,&flen)==1 + && flen == 0; + } + EVP_CIPHER_CTX_free(dctx); + OPENSSL_cleanse(passphrase_hash,sizeof(passphrase_hash)); + if(!dok){ + OPENSSL_cleanse(&affkey_seg,sizeof(affkey_seg)); // erase the temp data + return AF_ERROR_NO_AES; + } /* See if its zero? */ for(u_int i=0;iopenflags & O_RDWR)){ +#ifdef HAVE_AES_ENCRYPT + EVP_CIPHER_CTX_free(af->crypto->ekey); + af->crypto->ekey = 0; + EVP_CIPHER_CTX_free(af->crypto->dkey); + af->crypto->dkey = 0; +#endif af->crypto->sealing_key_set = 0; return 0; } diff --git a/lib/qemu/aes.h b/lib/qemu/aes.h deleted file mode 100644 index 2386733..0000000 --- a/lib/qemu/aes.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/lib/qemu/block-qcow.c b/lib/qemu/block-qcow.c index c7b526f..a62688a 100644 --- a/lib/qemu/block-qcow.c +++ b/lib/qemu/block-qcow.c @@ -24,7 +24,7 @@ #include "qemu-common.h" #include "block_int.h" #include -#include "aes.h" +#include /**************************************************************/ /* QEMU COW block driver with compression and encryption support */ @@ -71,8 +71,8 @@ typedef struct BDRVQcowState { uint64_t cluster_cache_offset; uint32_t crypt_method; /* current crypt method, 0 if no key yet */ uint32_t crypt_method_header; - AES_KEY aes_encrypt_key; - AES_KEY aes_decrypt_key; + EVP_CIPHER_CTX *aes_encrypt_key; + EVP_CIPHER_CTX *aes_decrypt_key; } BDRVQcowState; static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); @@ -177,6 +177,7 @@ static int qcow_set_key(BlockDriverState *bs, const char *key) BDRVQcowState *s = bs->opaque; uint8_t keybuf[16]; int len, i; + EVP_CIPHER_CTX *ekey, *dkey; memset(keybuf, 0, 16); len = strlen(key); @@ -187,56 +188,105 @@ static int qcow_set_key(BlockDriverState *bs, const char *key) for(i = 0;i < len;i++) { keybuf[i] = key[i]; } - s->crypt_method = s->crypt_method_header; - if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) + /* Expand the key into immutable template EVP cipher contexts. + * (The legacy AES_set_encrypt_key/AES_set_decrypt_key calls are + * deprecated since OpenSSL 3.0 and hidden in no-deprecated OpenSSL + * 3.4+/4.0 builds.) encrypt_sectors() copies from these templates + * into a per-call context so concurrent/AIO operations never share + * mutable cipher state. */ + ekey = EVP_CIPHER_CTX_new(); + dkey = EVP_CIPHER_CTX_new(); + if (!ekey || !dkey) { + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + OPENSSL_cleanse(keybuf, sizeof(keybuf)); return -1; - if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) + } + if (EVP_EncryptInit_ex(ekey, EVP_aes_128_cbc(), NULL, keybuf, NULL) != 1 || + EVP_DecryptInit_ex(dkey, EVP_aes_128_cbc(), NULL, keybuf, NULL) != 1 || + EVP_CIPHER_CTX_set_padding(ekey, 0) != 1 || + EVP_CIPHER_CTX_set_padding(dkey, 0) != 1) { + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + OPENSSL_cleanse(keybuf, sizeof(keybuf)); return -1; -#if 0 - /* test */ - { - uint8_t in[16]; - uint8_t out[16]; - uint8_t tmp[16]; - for(i=0;i<16;i++) - in[i] = i; - AES_encrypt(in, tmp, &s->aes_encrypt_key); - AES_decrypt(tmp, out, &s->aes_decrypt_key); - for(i = 0; i < 16; i++) - printf(" %02x", tmp[i]); - printf("\n"); - for(i = 0; i < 16; i++) - printf(" %02x", out[i]); - printf("\n"); } -#endif + OPENSSL_cleanse(keybuf, sizeof(keybuf)); + + EVP_CIPHER_CTX_free(s->aes_encrypt_key); + EVP_CIPHER_CTX_free(s->aes_decrypt_key); + s->aes_encrypt_key = ekey; + s->aes_decrypt_key = dkey; + s->crypt_method = s->crypt_method_header; return 0; } /* The crypt function is compatible with the linux cryptoloop algorithm for < 4 GB images. NOTE: out_buf == in_buf is - supported */ -static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, - uint8_t *out_buf, const uint8_t *in_buf, - int nb_sectors, int enc, - const AES_KEY *key) + supported. + 'key' is the immutable template EVP_CIPHER_CTX created by qcow_set_key; + 'enc' is 1 to encrypt and 0 to decrypt (mirroring AES_ENCRYPT/AES_DECRYPT). + Returns 0 on success, -1 on error. */ +static int encrypt_sectors(BDRVQcowState *s, int64_t sector_num, + uint8_t *out_buf, const uint8_t *in_buf, + int nb_sectors, int enc, + const EVP_CIPHER_CTX *key) { union { uint64_t ll[2]; uint8_t b[16]; } ivec; - int i; + EVP_CIPHER_CTX *ctx; + int i, len, flen; + uint8_t final_buf[16]; + + if (nb_sectors <= 0) + return 0; + if (!key) + return -1; + + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return -1; + if (EVP_CIPHER_CTX_copy(ctx, key) != 1) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } for(i = 0; i < nb_sectors; i++) { ivec.ll[0] = cpu_to_le64(sector_num); ivec.ll[1] = 0; - AES_cbc_encrypt(in_buf, out_buf, 512, key, - ivec.b, enc); + len = 0; + flen = 0; + if (enc) { + /* re-init: same cipher and key, fresh per-sector IV */ + if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, ivec.b) != 1 || + EVP_CIPHER_CTX_set_padding(ctx, 0) != 1 || + EVP_EncryptUpdate(ctx, out_buf, &len, in_buf, 512) != 1 || + len != 512 || + EVP_EncryptFinal_ex(ctx, final_buf, &flen) != 1 || + flen != 0) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } + } else { + if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, ivec.b) != 1 || + EVP_CIPHER_CTX_set_padding(ctx, 0) != 1 || + EVP_DecryptUpdate(ctx, out_buf, &len, in_buf, 512) != 1 || + len != 512 || + EVP_DecryptFinal_ex(ctx, final_buf, &flen) != 1 || + flen != 0) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } + } sector_num++; in_buf += 512; out_buf += 512; } + EVP_CIPHER_CTX_free(ctx); + return 0; } /* 'allocate' is: @@ -353,10 +403,11 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, memset(s->cluster_data + 512, 0x00, 512); for(i = 0; i < s->cluster_sectors; i++) { if (i < n_start || i >= n_end) { - encrypt_sectors(s, start_sect + i, - s->cluster_data, - s->cluster_data + 512, 1, 1, - &s->aes_encrypt_key); + if (encrypt_sectors(s, start_sect + i, + s->cluster_data, + s->cluster_data + 512, 1, 1, + s->aes_encrypt_key) < 0) + return 0; if (bdrv_pwrite(s->hd, cluster_offset + i * 512, s->cluster_data, 512) != 512) return -1; @@ -475,8 +526,9 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num, if (ret != n * 512) return -1; if (s->crypt_method) { - encrypt_sectors(s, sector_num, buf, buf, n, 0, - &s->aes_decrypt_key); + if (encrypt_sectors(s, sector_num, buf, buf, n, 0, + s->aes_decrypt_key) < 0) + return -1; } } nb_sectors -= n; @@ -505,8 +557,9 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num, if (!cluster_offset) return -1; if (s->crypt_method) { - encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, - &s->aes_encrypt_key); + if (encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, + s->aes_encrypt_key) < 0) + return -1; ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, s->cluster_data, n * 512); } else { @@ -556,9 +609,12 @@ static void qcow_aio_read_cb(void *opaque, int ret) /* nothing to do */ } else { if (s->crypt_method) { - encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, - acb->n, 0, - &s->aes_decrypt_key); + if (encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, + acb->n, 0, + s->aes_decrypt_key) < 0) { + ret = -EIO; + goto fail; + } } } @@ -681,8 +737,11 @@ static void qcow_aio_write_cb(void *opaque, int ret) goto fail; } } - encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, - acb->n, 1, &s->aes_encrypt_key); + if (encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, + acb->n, 1, s->aes_encrypt_key) < 0) { + ret = -EIO; + goto fail; + } src_buf = acb->cluster_data; } else { src_buf = acb->buf; @@ -733,6 +792,10 @@ static void qcow_close(BlockDriverState *bs) qemu_free(s->cluster_cache); qemu_free(s->cluster_data); bdrv_delete(s->hd); + EVP_CIPHER_CTX_free(s->aes_encrypt_key); + s->aes_encrypt_key = NULL; + EVP_CIPHER_CTX_free(s->aes_decrypt_key); + s->aes_decrypt_key = NULL; } static int qcow_create(const char *filename, int64_t total_size, diff --git a/lib/qemu/block-qcow2.c b/lib/qemu/block-qcow2.c index 1eaa214..f2f43c9 100644 --- a/lib/qemu/block-qcow2.c +++ b/lib/qemu/block-qcow2.c @@ -24,7 +24,7 @@ #include "qemu-common.h" #include "block_int.h" #include -#include "aes.h" +#include #include /* @@ -143,8 +143,8 @@ typedef struct BDRVQcowState { uint32_t crypt_method; /* current crypt method, 0 if no key yet */ uint32_t crypt_method_header; - AES_KEY aes_encrypt_key; - AES_KEY aes_decrypt_key; + EVP_CIPHER_CTX *aes_encrypt_key; + EVP_CIPHER_CTX *aes_decrypt_key; uint64_t snapshots_offset; int snapshots_size; int nb_snapshots; @@ -304,6 +304,7 @@ static int qcow_set_key(BlockDriverState *bs, const char *key) BDRVQcowState *s = bs->opaque; uint8_t keybuf[16]; int len, i; + EVP_CIPHER_CTX *ekey, *dkey; memset(keybuf, 0, 16); len = strlen(key); @@ -314,56 +315,105 @@ static int qcow_set_key(BlockDriverState *bs, const char *key) for(i = 0;i < len;i++) { keybuf[i] = key[i]; } - s->crypt_method = s->crypt_method_header; - if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) + /* Expand the key into immutable template EVP cipher contexts. + * (The legacy AES_set_encrypt_key/AES_set_decrypt_key calls are + * deprecated since OpenSSL 3.0 and hidden in no-deprecated OpenSSL + * 3.4+/4.0 builds.) encrypt_sectors() copies from these templates + * into a per-call context so concurrent/AIO operations never share + * mutable cipher state. */ + ekey = EVP_CIPHER_CTX_new(); + dkey = EVP_CIPHER_CTX_new(); + if (!ekey || !dkey) { + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + OPENSSL_cleanse(keybuf, sizeof(keybuf)); return -1; - if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) + } + if (EVP_EncryptInit_ex(ekey, EVP_aes_128_cbc(), NULL, keybuf, NULL) != 1 || + EVP_DecryptInit_ex(dkey, EVP_aes_128_cbc(), NULL, keybuf, NULL) != 1 || + EVP_CIPHER_CTX_set_padding(ekey, 0) != 1 || + EVP_CIPHER_CTX_set_padding(dkey, 0) != 1) { + EVP_CIPHER_CTX_free(ekey); + EVP_CIPHER_CTX_free(dkey); + OPENSSL_cleanse(keybuf, sizeof(keybuf)); return -1; -#if 0 - /* test */ - { - uint8_t in[16]; - uint8_t out[16]; - uint8_t tmp[16]; - for(i=0;i<16;i++) - in[i] = i; - AES_encrypt(in, tmp, &s->aes_encrypt_key); - AES_decrypt(tmp, out, &s->aes_decrypt_key); - for(i = 0; i < 16; i++) - printf(" %02x", tmp[i]); - printf("\n"); - for(i = 0; i < 16; i++) - printf(" %02x", out[i]); - printf("\n"); } -#endif + OPENSSL_cleanse(keybuf, sizeof(keybuf)); + + EVP_CIPHER_CTX_free(s->aes_encrypt_key); + EVP_CIPHER_CTX_free(s->aes_decrypt_key); + s->aes_encrypt_key = ekey; + s->aes_decrypt_key = dkey; + s->crypt_method = s->crypt_method_header; return 0; } /* The crypt function is compatible with the linux cryptoloop algorithm for < 4 GB images. NOTE: out_buf == in_buf is - supported */ -static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, - uint8_t *out_buf, const uint8_t *in_buf, - int nb_sectors, int enc, - const AES_KEY *key) + supported. + 'key' is the immutable template EVP_CIPHER_CTX created by qcow_set_key; + 'enc' is 1 to encrypt and 0 to decrypt (mirroring AES_ENCRYPT/AES_DECRYPT). + Returns 0 on success, -1 on error. */ +static int encrypt_sectors(BDRVQcowState *s, int64_t sector_num, + uint8_t *out_buf, const uint8_t *in_buf, + int nb_sectors, int enc, + const EVP_CIPHER_CTX *key) { union { uint64_t ll[2]; uint8_t b[16]; } ivec; - int i; + EVP_CIPHER_CTX *ctx; + int i, len, flen; + uint8_t final_buf[16]; + + if (nb_sectors <= 0) + return 0; + if (!key) + return -1; + + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) + return -1; + if (EVP_CIPHER_CTX_copy(ctx, key) != 1) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } for(i = 0; i < nb_sectors; i++) { ivec.ll[0] = cpu_to_le64(sector_num); ivec.ll[1] = 0; - AES_cbc_encrypt(in_buf, out_buf, 512, key, - ivec.b, enc); + len = 0; + flen = 0; + if (enc) { + /* re-init: same cipher and key, fresh per-sector IV */ + if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, ivec.b) != 1 || + EVP_CIPHER_CTX_set_padding(ctx, 0) != 1 || + EVP_EncryptUpdate(ctx, out_buf, &len, in_buf, 512) != 1 || + len != 512 || + EVP_EncryptFinal_ex(ctx, final_buf, &flen) != 1 || + flen != 0) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } + } else { + if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, ivec.b) != 1 || + EVP_CIPHER_CTX_set_padding(ctx, 0) != 1 || + EVP_DecryptUpdate(ctx, out_buf, &len, in_buf, 512) != 1 || + len != 512 || + EVP_DecryptFinal_ex(ctx, final_buf, &flen) != 1 || + flen != 0) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } + } sector_num++; in_buf += 512; out_buf += 512; } + EVP_CIPHER_CTX_free(ctx); + return 0; } static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, @@ -379,10 +429,11 @@ static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, if (ret < 0) return ret; if (s->crypt_method) { - encrypt_sectors(s, start_sect + n_start, - s->cluster_data, - s->cluster_data, n, 1, - &s->aes_encrypt_key); + if (encrypt_sectors(s, start_sect + n_start, + s->cluster_data, + s->cluster_data, n, 1, + s->aes_encrypt_key) < 0) + return -1; } ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start, s->cluster_data, n); @@ -745,8 +796,9 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num, if (ret != n * 512) return -1; if (s->crypt_method) { - encrypt_sectors(s, sector_num, buf, buf, n, 0, - &s->aes_decrypt_key); + if (encrypt_sectors(s, sector_num, buf, buf, n, 0, + s->aes_decrypt_key) < 0) + return -1; } } nb_sectors -= n; @@ -774,8 +826,9 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num, if (!cluster_offset) return -1; if (s->crypt_method) { - encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, - &s->aes_encrypt_key); + if (encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, + s->aes_encrypt_key) < 0) + return -1; ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, s->cluster_data, n * 512); } else { @@ -825,9 +878,12 @@ static void qcow_aio_read_cb(void *opaque, int ret) /* nothing to do */ } else { if (s->crypt_method) { - encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, - acb->n, 0, - &s->aes_decrypt_key); + if (encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, + acb->n, 0, + s->aes_decrypt_key) < 0) { + ret = -EIO; + goto fail; + } } } @@ -968,8 +1024,11 @@ static void qcow_aio_write_cb(void *opaque, int ret) goto fail; } } - encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, - acb->n, 1, &s->aes_encrypt_key); + if (encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, + acb->n, 1, s->aes_encrypt_key) < 0) { + ret = -EIO; + goto fail; + } src_buf = acb->cluster_data; } else { src_buf = acb->buf; @@ -1016,6 +1075,10 @@ static void qcow_close(BlockDriverState *bs) qemu_free(s->cluster_data); refcount_close(bs); bdrv_delete(s->hd); + EVP_CIPHER_CTX_free(s->aes_encrypt_key); + s->aes_encrypt_key = NULL; + EVP_CIPHER_CTX_free(s->aes_decrypt_key); + s->aes_decrypt_key = NULL; } /* XXX: use std qcow open function ? */ diff --git a/lib/s3.cpp b/lib/s3.cpp index bbc9d38..5d7b6f0 100644 --- a/lib/s3.cpp +++ b/lib/s3.cpp @@ -25,8 +25,8 @@ #include -#ifdef HAVE_OPENSSL_MD5_H -#include +#ifdef HAVE_MD5 +#include #endif @@ -450,8 +450,9 @@ void s3_bandwidth(int argc,char **argv) /* Now verify the MD5 */ unsigned char md5[16]; memset(md5,0,sizeof(md5)); - MD5((const unsigned char *)r->base,r->len,md5); - if(memcmp(r->ETag,md5,16)!=0){ + if(af_MD5((const unsigned char *)r->base,r->len,md5)!=0){ + fprintf(out," MD5_FAILED "); + } else if(memcmp(r->ETag,md5,16)!=0){ fprintf(out," FAILED != "); for(int i=0;i<16;i++) fprintf(out,"%02x",md5[i]); } diff --git a/lib/s3_glue.cpp b/lib/s3_glue.cpp index 8544cf9..5b94e4e 100644 --- a/lib/s3_glue.cpp +++ b/lib/s3_glue.cpp @@ -18,7 +18,7 @@ #define BAD_STL #endif - +#include "afflib.h" #include "s3_glue.h" #include "curl/curl.h" #include "base64.h" @@ -27,8 +27,8 @@ #include #include -#ifdef HAVE_OPENSSL_MD5_H -#include +#ifdef HAVE_MD5 +#include #endif #ifdef HAVE_OPENSSL_HMAC_H @@ -43,7 +43,7 @@ #include #endif -#if !defined(HAVE_OPENSSL_MD5_H) +#if !defined(HAVE_MD5) #error S3 support requires MD5 support #endif @@ -641,7 +641,10 @@ int object_put(string bucket,string path, { unsigned char md5[16]; memset(md5,0,sizeof(md5)); - MD5((const unsigned char *)buf,buflen,md5); + if(af_MD5((const unsigned char *)buf,buflen,md5)!=0){ + errno = EIO; + return -1; + } for(int i=0;i0){ diff --git a/m4/ax_openssl_version.m4 b/m4/ax_openssl_version.m4 new file mode 100644 index 0000000..f4c36a1 --- /dev/null +++ b/m4/ax_openssl_version.m4 @@ -0,0 +1,64 @@ +# -------------------------------------------------------------------------- +# AX_OPENSSL_VERSION +# +# Detect the OpenSSL / LibreSSL version in use from . +# +# Works with OpenSSL 1.0.2, 1.1.1, 3.x, 4.x and LibreSSL, including when +# cross-compiling (uses AC_COMPUTE_INT compile-time checks). +# +# Defined macros (prefixed with AFF_ to avoid colliding with OpenSSL 3+'s +# own OPENSSL_VERSION_MAJOR / MINOR / PATCH macros): +# AFF_OPENSSL_VERSION_MAJOR +# AFF_OPENSSL_VERSION_MINOR +# AFF_OPENSSL_VERSION_PATCH +# +# Substituted variable: +# OPENSSL_VERSION (e.g. "3.0.13"; "unknown" when not determinable) +# +# Copyright (c) 2026 AFFLIB. Public domain / BSD-style, like the rest of AFFLIB. +# -------------------------------------------------------------------------- +AC_DEFUN([AX_OPENSSL_VERSION],[ +AC_LANG_PUSH([C]) + +AC_MSG_CHECKING([OpenSSL version]) +AC_COMPUTE_INT([ax_openssl_major], [AFF_OSSL_MAJ], [ +#include +#ifdef OPENSSL_VERSION_MAJOR +#define AFF_OSSL_MAJ OPENSSL_VERSION_MAJOR +#else +#define AFF_OSSL_MAJ ((OPENSSL_VERSION_NUMBER >> 28) & 0xF) +#endif +], [ax_openssl_major=""]) + +AC_COMPUTE_INT([ax_openssl_minor], [AFF_OSSL_MIN], [ +#include +#ifdef OPENSSL_VERSION_MINOR +#define AFF_OSSL_MIN OPENSSL_VERSION_MINOR +#else +#define AFF_OSSL_MIN ((OPENSSL_VERSION_NUMBER >> 20) & 0xFF) +#endif +], [ax_openssl_minor=""]) + +AC_COMPUTE_INT([ax_openssl_patch], [AFF_OSSL_PAT], [ +#include +#ifdef OPENSSL_VERSION_PATCH +#define AFF_OSSL_PAT OPENSSL_VERSION_PATCH +#else +#define AFF_OSSL_PAT ((OPENSSL_VERSION_NUMBER >> 12) & 0xFF) +#endif +], [ax_openssl_patch=""]) + +AC_LANG_POP([C]) + +if test -n "$ax_openssl_major" && test -n "$ax_openssl_minor" && test -n "$ax_openssl_patch" ; then + OPENSSL_VERSION="${ax_openssl_major}.${ax_openssl_minor}.${ax_openssl_patch}" + AC_MSG_RESULT([$OPENSSL_VERSION]) + AC_DEFINE_UNQUOTED([AFF_OPENSSL_VERSION_MAJOR],[$ax_openssl_major],[OpenSSL major version]) + AC_DEFINE_UNQUOTED([AFF_OPENSSL_VERSION_MINOR],[$ax_openssl_minor],[OpenSSL minor version]) + AC_DEFINE_UNQUOTED([AFF_OPENSSL_VERSION_PATCH],[$ax_openssl_patch],[OpenSSL patch version]) +else + OPENSSL_VERSION=unknown + AC_MSG_RESULT([unknown]) +fi +AC_SUBST([OPENSSL_VERSION]) +])dnl AX_OPENSSL_VERSION diff --git a/tools/aff_bom.cpp b/tools/aff_bom.cpp index 83744c5..5487925 100644 --- a/tools/aff_bom.cpp +++ b/tools/aff_bom.cpp @@ -35,11 +35,8 @@ #include #endif -/* Support OpenSSL before 1.1.0 */ -#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) -#define EVP_MD_CTX_new EVP_MD_CTX_create -#define EVP_MD_CTX_free EVP_MD_CTX_destroy -#endif +/* Note: OpenSSL < 1.1.0 / LibreSSL < 2.7.0 EVP_MD_CTX_new/free shims + * are provided by include/afflib/afflib_i.h. */ using namespace std; diff --git a/tools/affcompare.cpp b/tools/affcompare.cpp index 993d558..11988ef 100644 --- a/tools/affcompare.cpp +++ b/tools/affcompare.cpp @@ -35,8 +35,6 @@ using namespace aff; #include #include #include -#include -#include #include #ifdef HAVE_CSTRING diff --git a/tools/affconvert.cpp b/tools/affconvert.cpp index ea957e3..2e51b5d 100644 --- a/tools/affconvert.cpp +++ b/tools/affconvert.cpp @@ -15,8 +15,7 @@ #include "afflib_i.h" // we do enough mucking, we need the internal version #include "utils.h" -#include -#include +#include #ifdef WIN32 #include "unix4win32.h" @@ -320,11 +319,31 @@ int convert(const char *infile,char *outfile) *** stuff that we keep at the beginning of the file... ****************************************************************/ - MD5_CTX md5; - MD5_Init(&md5); - - SHA_CTX sha; - SHA1_Init(&sha); + /* Set up the MD5/SHA1 hash machinery via the stable EVP API + * (the low-level MD5/SHA1 state functions are deprecated + * since OpenSSL 3.0) */ + EVP_MD_CTX *md5ctx = EVP_MD_CTX_new(); + EVP_MD_CTX *sha1ctx = EVP_MD_CTX_new(); + if(!md5ctx || !sha1ctx){ + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(a_out); + af_close(a_in); + return -1; + } + { + const EVP_MD *md5_md = EVP_md5(); + const EVP_MD *sha1_md = EVP_sha1(); + if(!md5_md || !sha1_md || + EVP_DigestInit_ex(md5ctx,md5_md,NULL)!=1 || + EVP_DigestInit_ex(sha1ctx,sha1_md,NULL)!=1){ + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(a_out); + af_close(a_in); + return -1; + } + } /* Setup writing */ if(a_in->image_pagesize){ @@ -429,8 +448,10 @@ int convert(const char *infile,char *outfile) if(pagenum != prev_pagenum + 1) hash_valid = false; if(hash_valid && vni.supports_metadata){ - MD5_Update(&md5,data,data_len); - SHA1_Update(&sha,data,data_len); + if(EVP_DigestUpdate(md5ctx,data,data_len)!=1 || + EVP_DigestUpdate(sha1ctx,data,data_len)!=1){ + errx(1,"EVP_DigestUpdate failed"); + } prev_pagenum = pagenum; } last_byte_in_image = (int64_t)image_pagesize * pagenum + (int64_t)data_len; @@ -455,8 +476,10 @@ int convert(const char *infile,char *outfile) err(1,"af_write"); } if(vni.supports_metadata){ - MD5_Update(&md5,data,data_len); - SHA1_Update(&sha,data,data_len); + if(EVP_DigestUpdate(md5ctx,data,data_len)!=1 || + EVP_DigestUpdate(sha1ctx,data,data_len)!=1){ + errx(1,"EVP_DigestUpdate failed"); + } } } if(data_len<0) err(1,"af_read"); @@ -474,20 +497,28 @@ int convert(const char *infile,char *outfile) /* Write out the new hash if it is valid */ if(hash_valid && vni.supports_metadata){ u_char md5_buf[32],sha1_buf[40]; + unsigned int md5_len = 0, sha1_len = 0; char buf[256]; - MD5_Final(md5_buf,&md5); + if(EVP_DigestFinal_ex(md5ctx,md5_buf,&md5_len)!=1 || md5_len!=16){ + errx(1,"EVP_DigestFinal_ex (MD5) failed"); + } if(af_update_seg(a_out,AF_MD5,0,md5_buf,16) && errno!=ENOTSUP){ err(1,"Could not update AF_MD5"); } if(!opt_quiet) printf("md5: %s\n",af_hexbuf(buf,sizeof(buf),md5_buf,16,1)); - - SHA1_Final(sha1_buf,&sha); + + if(EVP_DigestFinal_ex(sha1ctx,sha1_buf,&sha1_len)!=1 || sha1_len!=20){ + errx(1,"EVP_DigestFinal_ex (SHA1) failed"); + } if(af_update_seg(a_out,AF_SHA1,0,sha1_buf,20) && errno!=ENOTSUP){ err(1,"Could not update AF_SHA1"); } if(!opt_quiet) printf("sha1: %s\n",af_hexbuf(buf,sizeof(buf),sha1_buf,20,1)); } + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + /* Finish the hash calculations and write to the db */ if(!opt_quiet){ printf("bytes converted: %" I64d " \n",total_bytes_converted); diff --git a/tools/affcopy.cpp b/tools/affcopy.cpp index 31593e2..2a6987c 100644 --- a/tools/affcopy.cpp +++ b/tools/affcopy.cpp @@ -31,8 +31,7 @@ using namespace aff; #include #include -#include -#include +#include #include #ifdef HAVE_UNISTD_H @@ -372,7 +371,12 @@ int affcopy(char *infile,vector &outfiles) /* Calculate the MD5 of this segment and remember it in the map */ md5blob md5; - MD5(segbuf,seglen,md5.buf); + if(af_MD5((const unsigned char *)segbuf,seglen,md5.buf)!=0){ + free(segbuf); + unlink_outfiles(outfiles); + errx(1,"Cannot compute MD5 hash for segment '%s' in %s.", + segname,af_filename(ain)); + } hashMap[segname] = md5; /* See if this is a page; if so, it is handled specially */ @@ -526,7 +530,12 @@ int affcopy(char *infile,vector &outfiles) * (But don't do this for preened segments. */ unsigned char md5_read[16]; - MD5(segbuf,seglen,md5_read); + if(af_MD5((const unsigned char *)segbuf,seglen,md5_read)!=0){ + free(segbuf); + unlink_outfiles(outfiles); + errx(1,"Cannot compute MD5 hash for segment '%s' in %s.", + segname,af_filename(aout->af)); + } if(memcmp(hashMap[segname].buf,md5_read,16)!=0){ unlink_outfiles(outfiles); errx(1,"Hash read from %s for segment %s doesn't validate.", diff --git a/tools/affinfo.cpp b/tools/affinfo.cpp index 743f58b..ecc9914 100644 --- a/tools/affinfo.cpp +++ b/tools/affinfo.cpp @@ -18,8 +18,7 @@ #include #include -#include -#include +#include #include #include @@ -189,21 +188,56 @@ void validate(const char *infile) last = *i; } - /* Set up the hash machinery */ - MD5_CTX md5; - MD5_Init(&md5); + /* Set up the hash machinery (via the stable EVP API; the low-level + * MD5 and SHA1 state functions are deprecated since OpenSSL 3.0) */ + EVP_MD_CTX *md5ctx = NULL; + EVP_MD_CTX *sha1ctx = NULL; + if(opt_validate & VALIDATE_MD5){ + const EVP_MD *md5_md = EVP_md5(); + md5ctx = EVP_MD_CTX_new(); + if(!md5ctx || !md5_md || EVP_DigestInit_ex(md5ctx,md5_md,NULL)!=1){ + fprintf(stderr,"affinfo: failed to initialize MD5 digest\n"); + affinfo_error_code |= ERROR_MD5; + EVP_MD_CTX_free(md5ctx); + af_close(af); + return; + } + } + if(opt_validate & VALIDATE_SHA1){ + const EVP_MD *sha1_md = EVP_sha1(); + sha1ctx = EVP_MD_CTX_new(); + if(!sha1ctx || !sha1_md || EVP_DigestInit_ex(sha1ctx,sha1_md,NULL)!=1){ + fprintf(stderr,"affinfo: failed to initialize SHA1 digest\n"); + affinfo_error_code |= ERROR_SHA1; + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(af); + return; + } + } - SHA_CTX sha; - SHA1_Init(&sha); - uint64_t total_bytes = 0; while(!af_eof(af)){ unsigned char buf[65536]; // a decent size size_t bytes = af_read(af,buf,sizeof(buf)); if(bytes==0) break; // reached sparse region of file total_bytes += bytes; - if(opt_validate & VALIDATE_MD5) MD5_Update(&md5,buf,bytes); - if(opt_validate & VALIDATE_SHA1) SHA1_Update(&sha,buf,bytes); + if((opt_validate & VALIDATE_MD5) && EVP_DigestUpdate(md5ctx,buf,bytes)!=1){ + fprintf(stderr,"affinfo: MD5 digest update failed\n"); + affinfo_error_code |= ERROR_MD5; + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(af); + return; + } + if((opt_validate & VALIDATE_SHA1) && EVP_DigestUpdate(sha1ctx,buf,bytes)!=1){ + fprintf(stderr,"affinfo: SHA1 digest update failed\n"); + affinfo_error_code |= ERROR_SHA1; + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(af); + return; + } } /* Finish the hash calculations and write to the db */ @@ -212,26 +246,32 @@ void validate(const char *infile) unsigned char md5_stored[16]; size_t md5len = sizeof(md5_stored); unsigned char md5_computed[16]; + unsigned int md5_outlen = 0; char buf[256]; - - MD5_Final(md5_computed,&md5); - printf("computed md5: %s\n", - af_hexbuf(buf,sizeof(buf),md5_computed,16,opt_hexbuf)); - if(af_get_seg(af,AF_MD5,0,md5_stored,&md5len)==0){ - printf(" stored md5: %s ", - af_hexbuf(buf,sizeof(buf),md5_stored,16,opt_hexbuf)); - if(md5len==16 && !memcmp((const char *)md5_stored, - (const char *)md5_computed,16)){ - printf(" MATCH\n"); + + if(EVP_DigestFinal_ex(md5ctx,md5_computed,&md5_outlen)!=1 || md5_outlen!=16){ + fprintf(stderr,"affinfo: MD5 digest finalization failed\n"); + affinfo_error_code |= ERROR_MD5; + } + else { + printf("computed md5: %s\n", + af_hexbuf(buf,sizeof(buf),md5_computed,16,opt_hexbuf)); + if(af_get_seg(af,AF_MD5,0,md5_stored,&md5len)==0){ + printf(" stored md5: %s ", + af_hexbuf(buf,sizeof(buf),md5_stored,16,opt_hexbuf)); + if(md5len==16 && !memcmp((const char *)md5_stored, + (const char *)md5_computed,16)){ + printf(" MATCH\n"); + } + else { + printf(" NO MATCH!\n"); + affinfo_error_code |= ERROR_MD5; + } } else { - printf(" NO MATCH!\n"); - affinfo_error_code |= ERROR_MD5; + printf("(no MD5 in AFF file)\n"); } } - else { - printf("(no MD5 in AFF file)\n"); - } } @@ -239,26 +279,35 @@ void validate(const char *infile) unsigned char sha1_stored[20]; size_t sha1len = sizeof(sha1_stored); unsigned char sha1_computed[20]; + unsigned int sha1_outlen = 0; char buf[256]; - SHA1_Final(sha1_computed,&sha); - printf("computed sha1: %s \n",af_hexbuf(buf,sizeof(buf),sha1_computed,20,opt_hexbuf)); - if(af_get_seg(af,AF_SHA1,0,sha1_stored,&sha1len)==0){ - printf(" stored sha1: %s ",af_hexbuf(buf,sizeof(buf),sha1_stored,20,opt_hexbuf)); - if(sha1len==20 && !memcmp((const char *)sha1_stored, - (const char *)sha1_computed,20)){ - printf(" MATCH\n"); + if(EVP_DigestFinal_ex(sha1ctx,sha1_computed,&sha1_outlen)!=1 || sha1_outlen!=20){ + fprintf(stderr,"affinfo: SHA1 digest finalization failed\n"); + affinfo_error_code |= ERROR_SHA1; + } + else { + printf("computed sha1: %s \n",af_hexbuf(buf,sizeof(buf),sha1_computed,20,opt_hexbuf)); + if(af_get_seg(af,AF_SHA1,0,sha1_stored,&sha1len)==0){ + printf(" stored sha1: %s ",af_hexbuf(buf,sizeof(buf),sha1_stored,20,opt_hexbuf)); + if(sha1len==20 && !memcmp((const char *)sha1_stored, + (const char *)sha1_computed,20)){ + printf(" MATCH\n"); + } + else { + printf(" NO MATCH!\n"); + affinfo_error_code |= ERROR_SHA1; + } } else { - printf(" NO MATCH!\n"); - affinfo_error_code |= ERROR_SHA1; + printf("(no SHA1 in AFF file)\n"); } } - else { - printf("(no SHA1 in AFF file)\n"); - } } - + + EVP_MD_CTX_free(md5ctx); + EVP_MD_CTX_free(sha1ctx); + af_close(af); } @@ -415,10 +464,29 @@ void print_info(AFFILE *af,const char *segname) cc = output_line + strlen(output_line); if(opt_preview_md5){ - u_char md5[32]; - MD5(data,data_len,md5); - memcpy(data,md5,32); - data_len = 32; + /* Show an MD5 preview of the segment (16 bytes). + * (Previously this used an oversized 32-byte buffer and copied 16 + * bytes of stack garbage after the hash; fixed while migrating + * off the deprecated one-shot MD5() API.) */ + u_char md5[16]; + if(af_MD5((const unsigned char *)data,data_len,md5)!=0){ + fprintf(stderr,"affinfo: failed to compute MD5 preview for segment %s\n",segname); + affinfo_error_code |= ERROR_MD5; + goto done; + } + if(data_len < sizeof(md5)){ + unsigned char *md5_data = (unsigned char *)malloc(sizeof(md5)); + if(!md5_data){ + fprintf(stderr,"affinfo: out of memory\n"); + goto done; + } + free(data); + data = md5_data; + } + memcpy(data,md5,sizeof(md5)); + data_len = sizeof(md5); + display_len = sizeof(md5); + dots = 0; } if(display_as_hex(segname,display_len)){ char buf[82]; @@ -481,8 +549,10 @@ void print_info(AFFILE *af,const char *segname) goto skip1; } - MD5(page_data,page_data_len,hash_calc); - if(memcmp(hash_buf,hash_calc,sizeof(hash_buf))!=0){ + if(af_MD5((const unsigned char *)page_data,page_data_len,hash_calc)!=0){ + printf("** MD5 CALCULATION FAILED ** "); + affinfo_error_code |= ERROR_MD5; + } else if(memcmp(hash_buf,hash_calc,sizeof(hash_buf))!=0){ char hb[32]; printf("** HASH INVALID **\n%30s Calculated %s\n","", af_hexbuf(hb,sizeof(hb),hash_calc,16,opt_hexbuf)); diff --git a/tools/affix.cpp b/tools/affix.cpp index da76e87..ca098ad 100644 --- a/tools/affix.cpp +++ b/tools/affix.cpp @@ -16,8 +16,6 @@ #include #include -#include -#include #include #ifdef HAVE_UNISTD_H diff --git a/tools/affrecover.cpp b/tools/affrecover.cpp index bc220cb..9573faf 100644 --- a/tools/affrecover.cpp +++ b/tools/affrecover.cpp @@ -15,8 +15,6 @@ #include #include -#include -#include #include #ifdef HAVE_UNISTD_H diff --git a/tools/affstats.cpp b/tools/affstats.cpp index 6005640..d9efe27 100644 --- a/tools/affstats.cpp +++ b/tools/affstats.cpp @@ -48,8 +48,6 @@ #include "afflib.h" #include "afflib_i.h" -#include -#include #include #include diff --git a/tools/affverify.cpp b/tools/affverify.cpp index fec5a07..db69737 100644 --- a/tools/affverify.cpp +++ b/tools/affverify.cpp @@ -27,11 +27,8 @@ #include #include -/* Support OpenSSL before 1.1.0 */ -#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) -#define EVP_MD_CTX_new EVP_MD_CTX_create -#define EVP_MD_CTX_free EVP_MD_CTX_destroy -#endif +/* Note: OpenSSL < 1.1.0 / LibreSSL < 2.7.0 EVP_MD_CTX_new/free shims + * are provided by include/afflib/afflib_i.h. */ using namespace std; using namespace aff; diff --git a/tools/hashextent.h b/tools/hashextent.h index cdffffb..fccf20e 100644 --- a/tools/hashextent.h +++ b/tools/hashextent.h @@ -18,11 +18,16 @@ #include #include -/* Support OpenSSL before 1.1.0 */ -#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) +/* Support OpenSSL before 1.1.0 and LibreSSL before 2.7.0 */ +#if (OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)) || \ + (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) +#ifndef EVP_MD_CTX_new #define EVP_MD_CTX_new EVP_MD_CTX_create +#endif +#ifndef EVP_MD_CTX_free #define EVP_MD_CTX_free EVP_MD_CTX_destroy #endif +#endif using std::string; using std::ostream;