//
// sub_641F78 from 0x641f78 to 0x6421bc (580 bytes)
//
// 2 xrefs:
// > sub_644CCC @ 0x644d14
// > sub_644E50 @ 0x644e8e
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
int __fastcall sub_641F78(int a1, int a2)
{
size_t v4; // r7
int *v5; // r10
int v6; // r5
int v7; // r8
int v8; // r0
int error; // r0
void *v10; // r0
unsigned int v11; // r0
bool v12; // zf
unsigned int v13; // r5
int verify_result; // r0
const char *v15; // r0
int v16; // r4
int result; // r0
int v18; // r0
int v19; // r0
const char *v20; // r5
int v21; // r3
const char *v22; // r2
int v23; // r3
int v24; // r3
int current_cipher; // r0
const char *name; // r0
const char *v27; // [sp+4h] [bp-104h] BYREF
int v28[64]; // [sp+8h] [bp-100h] BYREF
v4 = *(_DWORD *)a1;
if ( *(_DWORD *)(a1 + 248) != 2
|| (*(_DWORD *)(a1 + 420) == -1 ? (v21 = 0) : (v21 = 24), *(_DWORD *)(v21 + a1 + 504) == 2) )
{
v5 = (int *)(v4 + 680);
}
else
{
v5 = (int *)(v4 + 772);
}
ERR_clear_error();
v6 = a1 + 24 * a2;
v7 = v6 + 464;
v8 = SSL_connect(*(_DWORD *)(v6 + 468));
if ( v8 != 1 )
{
error = SSL_get_error(*(_DWORD *)(v6 + 468), v8);
if ( error == 2 || error == 3 )
{
*(_DWORD *)(v6 + 460) = error;
return 0;
}
v28[0] = 0;
v10 = memset(&v28[1], 0, 0xFCu);
*(_DWORD *)(v6 + 460) = 1;
v11 = ERR_get_error(v10);
v12 = (v11 & 0xFFF) == 134;
if ( (v11 & 0xFFF) == 0x86 )
v12 = HIBYTE(v11) == 20;
v13 = v11;
if ( v12 )
{
verify_result = SSL_get_verify_result(*(_DWORD *)(v7 + 4));
if ( verify_result )
{
*v5 = verify_result;
v15 = (const char *)X509_verify_cert_error_string();
sub_624640(v28, 256, "SSL certificate problem: %s", v15);
}
else
{
strcpy((char *)v28, "SSL certificate verification failed");
}
v16 = 60;
}
else
{
ERR_error_string_n();
if ( !v13 )
{
if ( *(_DWORD *)(a1 + 248) != 2
|| (*(_DWORD *)(a1 + 420) == -1 ? (v24 = 0) : (v24 = 24), *(_DWORD *)(v24 + a1 + 504) == 2) )
{
v22 = *(const char **)(a1 + 152);
v23 = *(_DWORD *)(a1 + 264);
}
else
{
v22 = *(const char **)(a1 + 236);
v23 = *(_DWORD *)(a1 + 260);
}
sub_6180F8(v4, "Unknown SSL protocol error in connection to %s:%ld ", v22, v23);
return 35;
}
v16 = 35;
}
sub_6180F8(v4, "%s", (const char *)v28);
return v16;
}
*(_DWORD *)(v6 + 460) = 4;
v18 = *(_DWORD *)(v6 + 468);
if ( !v18 )
{
v20 = &byte_9B1912;
goto LABEL_42;
}
v19 = SSL_version();
if ( v19 == 769 )
{
v18 = *(_DWORD *)(v6 + 468);
v20 = "TLSv1.0";
goto LABEL_42;
}
if ( v19 > 769 )
{
if ( v19 != 770 )
{
if ( v19 == 771 )
{
v18 = *(_DWORD *)(v6 + 468);
v20 = "TLSv1.2";
goto LABEL_42;
}
goto LABEL_48;
}
v18 = *(_DWORD *)(v6 + 468);
v20 = "TLSv1.1";
}
else
{
if ( v19 != 2 )
{
if ( v19 == 768 )
{
v18 = *(_DWORD *)(v6 + 468);
v20 = "SSLv3";
goto LABEL_42;
}
LABEL_48:
v20 = "unknown";
v18 = *(_DWORD *)(a1 + 24 * a2 + 468);
goto LABEL_42;
}
v18 = *(_DWORD *)(v6 + 468);
v20 = "SSLv2";
}
LABEL_42:
current_cipher = SSL_get_current_cipher(v18);
name = (const char *)SSL_CIPHER_get_name(current_cipher);
sub_61807C(v4, "SSL connection using %s / %s\n", v20, name);
result = *(unsigned __int8 *)(a1 + 644);
if ( *(_BYTE *)(a1 + 644) )
{
SSL_get0_alpn_selected(*(_DWORD *)(a1 + 24 * a2 + 468), &v27, v28);
if ( v28[0] )
{
sub_61807C(v4, "ALPN, server accepted to use %.*s\n", v28[0], v27);
if ( v28[0] == 8 && !memcmp("http/1.1", v27, 8u) )
*(_DWORD *)(a1 + 1264) = 2;
}
else
{
sub_61807C(v4, "ALPN, server did not agree to a protocol\n");
}
return 0;
}
return result;
}