//
// cms_RecipientInfo_pwri_crypt from 0x876324 to 0x8768d0 (1452 bytes)
//
// 3 xrefs:
// > CMS_RecipientInfo_decrypt @ 0x8735d4
// > CMS_RecipientInfo_encrypt @ 0x8739d8
// > ??> @ 0x6a13c
//
// 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 cms_RecipientInfo_pwri_crypt(int a1, int a2, int a3)
{
int v3; // r10
int v4; // r4
int v5; // r5
int *v8; // r0
int *v9; // r6
char *v10; // r0
char *v11; // r0
int *cipherbyname; // r8
int *v13; // r0
int *v14; // r7
_BYTE *v15; // r5
int v16; // r7
unsigned int v17; // r8
unsigned int v18; // r5
unsigned int v19; // r0
_BOOL4 v20; // r3
size_t v21; // r8
_BYTE *v22; // r11
unsigned int v23; // r4
unsigned int v24; // r0
unsigned int v25; // r9
_BOOL4 v26; // r3
unsigned int *v27; // r3
unsigned int *v28; // r3
unsigned int v29; // r8
unsigned int v30; // r0
signed int v31; // r11
signed int v32; // r10
char *v33; // r0
char *v34; // r9
char *v35; // r3
int v36; // r10
char *v37; // [sp+8h] [bp-1Ch]
int v38; // [sp+Ch] [bp-18h]
size_t v39; // [sp+14h] [bp-10h]
size_t v40; // [sp+14h] [bp-10h]
int v41[2]; // [sp+1Ch] [bp-8h] BYREF
v3 = *(_DWORD *)(a2 + 4);
v4 = *(_DWORD *)(*(_DWORD *)(a1 + 4) + 12);
if ( *(_DWORD *)(v3 + 16) )
{
v5 = *(_DWORD *)(v3 + 8);
if ( !v5 || OBJ_obj2nid(*(_DWORD *)v5) != (_BYTE *)&stru_37C.st_name + 1 )
{
ERR_put_error(46, 167, 179, (int)"crypto/cms/cms_pwri.c", 299);
return 0;
}
v8 = (int *)ASN1_TYPE_unpack_sequence((int)X509_ALGOR_it, *(_DWORD **)(v5 + 4));
v9 = v8;
if ( v8 )
{
v10 = OBJ_obj2nid(*v8);
v11 = OBJ_nid2sn((unsigned int)v10);
cipherbyname = (int *)EVP_get_cipherbyname((int)v11);
if ( cipherbyname )
{
v13 = (int *)EVP_CIPHER_CTX_new();
v14 = v13;
if ( v13 )
{
v15 = nullptr;
if ( !EVP_CipherInit_ex(v13, cipherbyname, nullptr, 0, nullptr, a3) )
{
LABEL_9:
v15 = nullptr;
LABEL_10:
EVP_CIPHER_CTX_free(v14);
CRYPTO_free(v15, (int)"crypto/cms/cms_pwri.c", 387);
v16 = 0;
LABEL_11:
X509_ALGOR_free(v9);
return v16;
}
EVP_CIPHER_CTX_set_padding((int)v14, 0);
if ( EVP_CIPHER_asn1_to_param((int)v14, v9[1]) < 0 )
{
ERR_put_error(46, 167, 102, (int)"crypto/cms/cms_pwri.c", 330);
goto LABEL_10;
}
if ( EVP_PBE_CipherInit(
**(_DWORD **)(v3 + 4),
*(_DWORD *)(v3 + 16),
*(_DWORD *)(v3 + 20),
*(_DWORD *)(*(_DWORD *)(v3 + 4) + 4),
v14,
a3) < 0 )
{
ERR_put_error(46, 167, 6, (int)"crypto/cms/cms_pwri.c", 341);
goto LABEL_10;
}
if ( a3 )
{
v17 = *(_DWORD *)(v4 + 20);
v18 = EVP_CIPHER_CTX_block_size((int)v14);
v19 = v18 * ((v17 + 3 + v18) / v18);
v20 = v19 < 2 * v18;
if ( v17 > 0xFF )
v20 = 1;
if ( v20 )
goto LABEL_9;
v15 = CRYPTO_malloc((void *)v19, (int)"crypto/cms/cms_pwri.c", 352);
if ( !v15 )
goto LABEL_9;
v21 = *(_DWORD *)(v4 + 20);
v22 = *(_BYTE **)(v4 + 16);
v23 = EVP_CIPHER_CTX_block_size((int)v14);
v24 = (v21 + 3 + v23) / v23;
v25 = v23 * v24;
v26 = v23 * v24 < 2 * v23;
if ( v21 > 0xFF )
v26 = 1;
if ( v26 )
goto LABEL_10;
*v15 = v21;
v15[1] = ~*v22;
v15[2] = ~v22[1];
v15[3] = ~v22[2];
memcpy(v15 + 4, v22, v21);
if ( v25 > v21 + 4 && RAND_bytes((int)&v15[v21 + 4], -4 - v21 + v25) <= 0 )
goto LABEL_10;
if ( !EVP_EncryptUpdate(v14, v15, v41, v15, v25) || !EVP_EncryptUpdate(v14, v15, v41, v15, v25) )
goto LABEL_10;
v27 = *(unsigned int **)(v3 + 12);
v27[2] = (unsigned int)v15;
*v27 = v25;
LABEL_28:
EVP_CIPHER_CTX_free(v14);
v16 = 1;
goto LABEL_11;
}
v15 = CRYPTO_malloc(**(void ***)(v3 + 12), (int)"crypto/cms/cms_pwri.c", 362);
if ( !v15 )
{
ERR_put_error(46, 167, 65, (int)"crypto/cms/cms_pwri.c", 365);
goto LABEL_10;
}
v28 = *(unsigned int **)(v3 + 12);
v29 = *v28;
v37 = (char *)v28[2];
v30 = EVP_CIPHER_CTX_block_size((int)v14);
v31 = 2 * v30;
v32 = v30;
if ( v29 >= 2 * v30 )
{
v38 = v29 % v30;
if ( !(v29 % v30) )
{
v33 = (char *)CRYPTO_malloc((void *)v29, (int)"crypto/cms/cms_pwri.c", 191);
v34 = v33;
if ( v33 )
{
v39 = v29 - v31;
if ( EVP_DecryptUpdate(v14, &v33[v29 - v31], v41, &v37[v29 - v31], v31) )
{
v35 = &v34[v32 + v39];
v40 = v32 + v39;
if ( EVP_DecryptUpdate(v14, v34, v41, v35, v32) )
{
if ( EVP_DecryptUpdate(v14, v34, v41, v37, v40) )
{
if ( EVP_DecryptInit_ex(v14, (int *)v38, (_DWORD *)v38, v38, (void *)v38) )
{
if ( EVP_DecryptUpdate(v14, v34, v41, v34, v29) )
{
if ( (((unsigned __int8)v34[6]
^ (unsigned __int8)v34[3])
& ((unsigned __int8)v34[4]
^ (unsigned __int8)v34[1])
& ((unsigned __int8)v34[2]
^ (unsigned __int8)v34[5])) == 0xFF )
{
v36 = (unsigned __int8)*v34;
if ( v29 >= v36 - 4 )
{
memcpy(v15, v34 + 4, (unsigned __int8)*v34);
CRYPTO_clear_free(v34, v29, (int)"crypto/cms/cms_pwri.c", 225);
*(_DWORD *)(v4 + 16) = v15;
*(_DWORD *)(v4 + 20) = v36;
goto LABEL_28;
}
}
}
}
}
}
}
CRYPTO_clear_free(v34, v29, (int)"crypto/cms/cms_pwri.c", 225);
}
}
}
ERR_put_error(46, 167, 180, (int)"crypto/cms/cms_pwri.c", 371);
goto LABEL_10;
}
ERR_put_error(46, 167, 65, (int)"crypto/cms/cms_pwri.c", 321);
return 0;
}
else
{
ERR_put_error(46, 167, 148, (int)"crypto/cms/cms_pwri.c", 315);
return 0;
}
}
else
{
ERR_put_error(46, 167, 176, (int)"crypto/cms/cms_pwri.c", 308);
return 0;
}
}
else
{
ERR_put_error(46, 167, 178, (int)"crypto/cms/cms_pwri.c", 292);
return 0;
}
}