Skip to content

Commit

Permalink
More work on libcurl and friends (and set to used emulated version of…
Browse files Browse the repository at this point in the history
… curl if possible)
  • Loading branch information
ptitSeb committed Feb 8, 2024
1 parent 82ae65f commit b1f6ea8
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,13 +1212,14 @@ void LoadEnvVars(box64context_t *context)
printf_log(LOG_INFO, "\n");
}
}
// add libssl and libcrypto, prefer emulated version because of multiple version exist
// add libssl and libcrypto (and a few other) to prefer emulated version because of multiple version exist
AddPath("libssl.so.1", &context->box64_emulated_libs, 0);
AddPath("libssl.so.1.0.0", &context->box64_emulated_libs, 0);
AddPath("libcrypto.so.1", &context->box64_emulated_libs, 0);
AddPath("libcrypto.so.1.0.0", &context->box64_emulated_libs, 0);
AddPath("libunwind.so.8", &context->box64_emulated_libs, 0);
AddPath("libpng12.so.0", &context->box64_emulated_libs, 0);
AddPath("libcurl.so.4", &context->box64_emulated_libs, 0);

if(getenv("BOX64_SSE_FLUSHTO0")) {
if (strcmp(getenv("BOX64_SSE_FLUSHTO0"), "1")==0) {
Expand Down
3 changes: 3 additions & 0 deletions src/wrapped/generated/functions_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,8 @@ wrappedcrypto3:
- vFp:
- CRYPTO_set_id_callback
- CRYPTO_set_locking_callback
- pFp:
- OPENSSL_sk_new
- vFpp:
- X509_STORE_CTX_set_verify_cb
- pFpp:
Expand Down Expand Up @@ -3162,6 +3164,7 @@ wrappedcurl:
- curl_multi_setopt
- uFpup:
- curl_easy_setopt
- curl_share_setopt
wrappedd3dadapter9:
- pWp:
- D3DAdapter9GetProc
Expand Down
2 changes: 2 additions & 0 deletions src/wrapped/generated/wrappedcrypto3types.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#endif

typedef void (*vFp_t)(void*);
typedef void* (*pFp_t)(void*);
typedef void (*vFpp_t)(void*, void*);
typedef void* (*pFpp_t)(void*, void*);
typedef void (*vFppp_t)(void*, void*, void*);
Expand All @@ -27,6 +28,7 @@ typedef int32_t (*iFppppipp_t)(void*, void*, void*, void*, int32_t, void*, void*
#define SUPER() ADDED_FUNCTIONS() \
GO(CRYPTO_set_id_callback, vFp_t) \
GO(CRYPTO_set_locking_callback, vFp_t) \
GO(OPENSSL_sk_new, pFp_t) \
GO(X509_STORE_CTX_set_verify_cb, vFpp_t) \
GO(OPENSSL_sk_pop_free, pFpp_t) \
GO(EVP_MD_do_all_provided, vFppp_t) \
Expand Down
3 changes: 2 additions & 1 deletion src/wrapped/generated/wrappedcurltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef uint32_t (*uFpup_t)(void*, uint32_t, void*);

#define SUPER() ADDED_FUNCTIONS() \
GO(curl_multi_setopt, iFpup_t) \
GO(curl_easy_setopt, uFpup_t)
GO(curl_easy_setopt, uFpup_t) \
GO(curl_share_setopt, uFpup_t)

#endif // __wrappedcurlTYPES_H_
5 changes: 5 additions & 0 deletions src/wrapped/wrappedcrypto3.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ EXPORT void my3_OPENSSL_sk_pop_free(x64emu_t* emu, void* s, void* cb)
my->OPENSSL_sk_pop_free(s, find_free_fnc_Fct(cb));
}

EXPORT void* my3_OPENSSL_sk_new(x64emu_t* emu, void* f)
{
return my->OPENSSL_sk_new(find_cmp_fnc_Fct(f));
}

EXPORT void my3_ERR_set_error(x64emu_t* emu, int lib, int reason, void* fmt, uintptr_t* b)
{
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3);
Expand Down
1 change: 1 addition & 0 deletions src/wrapped/wrappedcrypto3_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,7 @@ GO(OPENSSL_no_config, vFv)
//GO(OPENSSL_rdtsc,
GO(OPENSSL_sk_find, iFpp)
GO(OPENSSL_sk_free, vFp) //1.1+
GOM(OPENSSL_sk_new, pFEp)
GO(OPENSSL_sk_new_null, pFv) //1.1+
GO(OPENSSL_sk_num, iFp) //1.1+
GO(OPENSSL_sk_pop, pFp) //1.1+
Expand Down
163 changes: 139 additions & 24 deletions src/wrapped/wrappedcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "box64context.h"
#include "emu/x64emu_private.h"
#include "callback.h"
#include "elfloader.h"

#ifdef ANDROID
const char* curlName = "libcurl.so";
Expand All @@ -34,6 +35,7 @@
#define STRINGPOINT 10000
#define FUNCTIONPOINT 20000
#define OFF_T 30000
#define BLOB 40000
#define CINIT(name,type,number) CURLOPT_ ## name = type + number
#define CURLOPT(na,t,nu) na = t + nu

Expand Down Expand Up @@ -309,6 +311,43 @@ typedef enum {
CINIT(HTTP09_ALLOWED, LONG, 285),
CINIT(ALTSVC_CTRL, LONG, 286),
CINIT(ALTSVC, STRINGPOINT, 287),
CINIT(MAXAGE_CONN, LONG, 288),
CINIT(SASL_AUTHZID, STRINGPOINT, 289),
CINIT(MAIL_RCPT_ALLOWFAILS, LONG, 290),
CINIT(SSLCERT_BLOB, BLOB, 291),
CINIT(SSLKEY_BLOB, BLOB, 292),
CINIT(PROXY_SSLCERT_BLOB, BLOB, 293),
CINIT(PROXY_SSLKEY_BLOB, BLOB, 294),
CINIT(ISSUERCERT_BLOB, BLOB, 295),
CINIT(PROXY_ISSUERCERT, STRINGPOINT, 296),
CINIT(PROXY_ISSUERCERT_BLOB, BLOB, 297),
CINIT(SSL_EC_CURVES, STRINGPOINT, 298),
CINIT(HSTS_CTRL, LONG, 299),
CINIT(HSTS, STRINGPOINT, 300),
CINIT(HSTSREADFUNCTION, FUNCTIONPOINT, 301),
CINIT(HSTSREADDATA, OBJECTPOINT, 302),
CINIT(HSTSWRITEFUNCTION, FUNCTIONPOINT, 303),
CINIT(HSTSWRITEDATA, OBJECTPOINT, 304),
CINIT(AWS_SIGV4, STRINGPOINT, 305),
CINIT(DOH_SSL_VERIFYPEER, LONG, 306),
CINIT(DOH_SSL_VERIFYHOST, LONG, 307),
CINIT(DOH_SSL_VERIFYSTATUS, LONG, 308),
CINIT(CAINFO_BLOB, BLOB, 309),
CINIT(PROXY_CAINFO_BLOB, BLOB, 310),
CINIT(SSH_HOST_PUBLIC_KEY_SHA256, STRINGPOINT, 311),
CINIT(PREREQFUNCTION, FUNCTIONPOINT, 312),
CINIT(PREREQDATA, OBJECTPOINT, 313),
CINIT(MAXLIFETIME_CONN, LONG, 314),
CINIT(MIME_OPTIONS, LONG, 315),
CINIT(SSH_HOSTKEYFUNCTION, FUNCTIONPOINT, 316),
CINIT(SSH_HOSTKEYDATA, OBJECTPOINT, 317),
CINIT(PROTOCOLS_STR, STRINGPOINT, 318),
CINIT(REDIR_PROTOCOLS_STR, STRINGPOINT, 319),
CINIT(WS_OPTIONS, LONG, 320),
CINIT(CA_CACHE_TIMEOUT, LONG, 321),
CINIT(QUICK_EXIT, LONG, 322),
CINIT(HAPROXY_CLIENT_IP, STRINGPOINT, 323),
CINIT(SERVER_RESPONSE_TIMEOUT_MS, LONG, 324),
CURLOPT_LASTENTRY /* the last unused */
} CURLoption;

Expand All @@ -332,6 +371,17 @@ typedef enum {
CURLMOPT_LASTENTRY /* the last unused */
} CURLMoption;

typedef enum {
CURLSHOPT_NONE,
CURLSHOPT_SHARE,
CURLSHOPT_UNSHARE,
CURLSHOPT_LOCKFUNC,
CURLSHOPT_UNLOCKFUNC,
CURLSHOPT_USERDATA,
CURLSHOPT_LAST /* never use */
} CURLSHoption;


#undef LONG
#undef OBJECTPOINT
#undef STRINGPOINT
Expand Down Expand Up @@ -599,65 +649,118 @@ static void* find_debug_Fct(void* fct)
return NULL;
}

// lockcb
#define GO(A) \
static uintptr_t my_lockcb_fct_##A = 0; \
static void my_lockcb_##A(void *a, int b, int c, void* d) \
{ \
RunFunctionFmt(my_lockcb_fct_##A, "piip", a, b, c, d); \
}
SUPER()
#undef GO
static void* find_lockcb_Fct(void* fct)
{
if(!fct) return NULL;
if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
#define GO(A) if(my_lockcb_fct_##A == (uintptr_t)fct) return my_lockcb_##A;
SUPER()
#undef GO
#define GO(A) if(my_lockcb_fct_##A == 0) {my_lockcb_fct_##A = (uintptr_t)fct; return my_lockcb_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for curl lockcb callback\n");
return NULL;
}

// unlockcb
#define GO(A) \
static uintptr_t my_unlockcb_fct_##A = 0; \
static void my_unlockcb_##A(void *a, int b, void* c) \
{ \
RunFunctionFmt(my_unlockcb_fct_##A, "pip", a, b, c); \
}
SUPER()
#undef GO
static void* find_unlockcb_Fct(void* fct)
{
if(!fct) return NULL;
if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
#define GO(A) if(my_unlockcb_fct_##A == (uintptr_t)fct) return my_unlockcb_##A;
SUPER()
#undef GO
#define GO(A) if(my_unlockcb_fct_##A == 0) {my_unlockcb_fct_##A = (uintptr_t)fct; return my_unlockcb_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for curl unlockcb callback\n");
return NULL;
}

// ssl_ctx_callback
#define GO(A) \
static uintptr_t my_ssl_ctx_callback_fct_##A = 0; \
static int my_ssl_ctx_callback_##A(void *a, void* b, void* c) \
{ \
return (int)RunFunctionFmt(my_ssl_ctx_callback_fct_##A, "ppp", a, b, c); \
}
SUPER()
#undef GO
static void* find_ssl_ctx_callback_Fct(void* fct)
{
if(!fct) return NULL;
if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
#define GO(A) if(my_ssl_ctx_callback_fct_##A == (uintptr_t)fct) return my_ssl_ctx_callback_##A;
SUPER()
#undef GO
#define GO(A) if(my_ssl_ctx_callback_fct_##A == 0) {my_ssl_ctx_callback_fct_##A = (uintptr_t)fct; return my_ssl_ctx_callback_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for curl ssl_ctx_callback callback\n");
return NULL;
}

#undef SUPER

EXPORT uint32_t my_curl_easy_setopt(x64emu_t* emu, void* handle, uint32_t option, void* param)
{
(void)emu;

switch(option) {
case CURLOPT_WRITEDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_WRITEFUNCTION:
return my->curl_easy_setopt(handle, option, find_write_Fct(param));
case CURLOPT_READDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_READFUNCTION:
return my->curl_easy_setopt(handle, option, find_read_Fct(param));
case CURLOPT_IOCTLDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_IOCTLFUNCTION:
return my->curl_easy_setopt(handle, option, find_ioctl_Fct(param));
case CURLOPT_SEEKDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_SEEKFUNCTION:
return my->curl_easy_setopt(handle, option, find_seek_Fct(param));
case CURLOPT_HEADERDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_HEADERFUNCTION:
return my->curl_easy_setopt(handle, option, find_header_Fct(param));
case CURLOPT_PROGRESSDATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_PROGRESSFUNCTION:
return my->curl_easy_setopt(handle, option, find_progress_Fct(param));
case CURLOPT_XFERINFOFUNCTION:
return my->curl_easy_setopt(handle, option, find_progress_int_Fct(param));
case CURLOPT_DEBUGFUNCTION:
return my->curl_easy_setopt(handle, option, find_debug_Fct(param));
case CURLOPT_DEBUGDATA:
case CURLOPT_SSL_CTX_FUNCTION:
return my->curl_easy_setopt(handle, option, find_ssl_ctx_callback_Fct(param));
case CURLOPT_SSL_CTX_DATA:
return my->curl_easy_setopt(handle, option, param);
case CURLOPT_SOCKOPTFUNCTION:
case CURLOPT_SOCKOPTDATA:
case CURLOPT_OPENSOCKETFUNCTION:
case CURLOPT_OPENSOCKETDATA:
case CURLOPT_CLOSESOCKETFUNCTION:
case CURLOPT_CLOSESOCKETDATA:
//case CURLOPT_XFERINFODATA:
case CURLOPT_SSL_CTX_FUNCTION:
case CURLOPT_SSL_CTX_DATA:
case CURLOPT_CONV_TO_NETWORK_FUNCTION:
case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
case CURLOPT_CONV_FROM_UTF8_FUNCTION:
case CURLOPT_INTERLEAVEFUNCTION:
case CURLOPT_INTERLEAVEDATA:
case CURLOPT_CHUNK_BGN_FUNCTION:
case CURLOPT_CHUNK_END_FUNCTION:
case CURLOPT_CHUNK_DATA:
case CURLOPT_FNMATCH_FUNCTION:
case CURLOPT_FNMATCH_DATA:
case CURLOPT_SUPPRESS_CONNECT_HEADERS:
case CURLOPT_RESOLVER_START_FUNCTION:
case CURLOPT_RESOLVER_START_DATA:
case CURLOPT_TRAILERFUNCTION:
case CURLOPT_HSTSREADFUNCTION:
case CURLOPT_HSTSWRITEFUNCTION:
case CURLOPT_PREREQFUNCTION:
case CURLOPT_SSH_HOSTKEYFUNCTION:
printf_log(LOG_NONE, "Error: unimplemented option %u in curl_easy_setopt\n", option);
return 48; //unknown option...
default:
Expand All @@ -681,6 +784,18 @@ EXPORT uint32_t my_curl_multi_setopt(x64emu_t* emu, void* handle, uint32_t optio
}
}

EXPORT uint32_t my_curl_share_setopt(x64emu_t* emu, void* handle, CURLSHoption option, void* param)
{
switch(option) {
case CURLSHOPT_LOCKFUNC:
return my->curl_share_setopt(handle, option, find_lockcb_Fct(param));
case CURLSHOPT_UNLOCKFUNC:
return my->curl_share_setopt(handle, option, find_unlockcb_Fct(param));
default:
return my->curl_share_setopt(handle, option, param);
}
}

#define CUSTOM_INIT \
getMy(lib);

Expand Down
12 changes: 6 additions & 6 deletions src/wrapped/wrappedcurl_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ GO(curl_easy_getinfo, uFpppppp) //vararg
GO(curl_easy_init, pFv)
//GO(curl_easy_pause,
GO(curl_easy_perform, uFp)
//GO(curl_easy_recv,
GO(curl_easy_recv, iFppLp)
GO(curl_easy_reset, vFp)
//GO(curl_easy_send,
GO(curl_easy_send, iFppLp)
GOM(curl_easy_setopt, uFEpup) // can set many callbacks!
GO(curl_easy_strerror, pFu)
//GO(curl_easy_unescape,
Expand All @@ -25,7 +25,7 @@ GO(curl_free, vFp)
GO(curl_global_cleanup, vFv)
GO(curl_global_init, uFu)
//GO(curl_global_init_mem,
//GO(curl_global_sslset,
GO(curl_global_sslset, uFupp)
//GO(curl_maprintf,
//GO(curl_mfprintf,
//GO(curl_mime_addpart,
Expand Down Expand Up @@ -66,9 +66,9 @@ GO(curl_multi_wait, iFppuip)
//GO(curl_mvsprintf,
//GO(curl_pushheader_byname,
//GO(curl_pushheader_bynum,
//GO(curl_share_cleanup,
//GO(curl_share_init,
//GO(curl_share_setopt,
GO(curl_share_cleanup, uFp)
GO(curl_share_init, pFv)
GOM(curl_share_setopt, uFEpup)
//GO(curl_share_strerror,
GO(curl_slist_append, pFpp)
GO(curl_slist_free_all, vFp)
Expand Down

0 comments on commit b1f6ea8

Please sign in to comment.