//
// cms_RecipientInfo_pwri_crypt from 0xe20cb8 to 0xe2121c (1380 bytes)
//
// 3 xrefs:
// > CMS_RecipientInfo_decrypt @ 0xe1dff4
// > CMS_RecipientInfo_encrypt @ 0xe1e464
// > ??> @ 0x546f0
//
// 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
//
__int64 __fastcall cms_RecipientInfo_pwri_crypt(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // x21
__int64 v5; // x23
__int64 *v6; // x19
__int64 *v8; // x0
__int64 v9; // x24
unsigned int v10; // w0
char *v11; // x0
unsigned int *cipherbyname; // x20
__int64 v13; // x0
__int64 *v14; // x19
_BYTE *v15; // x22
__int64 *v16; // x0
unsigned int v17; // w19
unsigned __int64 v18; // x20
unsigned __int64 v19; // x2
unsigned __int64 v20; // x0
size_t v21; // x25
_BYTE *v22; // x26
int v23; // w0
unsigned __int64 v24; // x23
__int64 v25; // x0
__int64 *v26; // x0
int *v27; // x2
unsigned __int64 v28; // x21
char *v29; // d8
int v30; // w0
unsigned __int64 v31; // x26
unsigned __int64 v32; // x27
char *v33; // x25
unsigned __int64 v34; // x27
int v35; // w26
int v36; // [xsp+88h] [xbp+88h]
int v37; // [xsp+9Ch] [xbp+9Ch] BYREF
v4 = *(_QWORD *)(a2 + 8);
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 24LL);
if ( *(_QWORD *)(v4 + 32) )
{
v6 = *(__int64 **)(v4 + 16);
if ( !v6 || (unsigned int)OBJ_obj2nid(*v6) != 893 )
{
ERR_put_error(46, 167, 179, (__int64)"crypto/cms/cms_pwri.c", 299);
return 0;
}
v8 = (__int64 *)ASN1_TYPE_unpack_sequence((__int64)X509_ALGOR_it, v6[1]);
v9 = (__int64)v8;
if ( v8 )
{
v10 = OBJ_obj2nid(*v8);
v11 = OBJ_nid2sn(v10);
cipherbyname = (unsigned int *)EVP_get_cipherbyname((__int64)v11);
if ( cipherbyname )
{
v13 = EVP_CIPHER_CTX_new();
v14 = (__int64 *)v13;
if ( v13 )
{
if ( !(unsigned int)EVP_CipherInit_ex(v13, cipherbyname, 0, 0, nullptr, a3) )
{
LABEL_9:
v15 = nullptr;
LABEL_10:
v16 = v14;
v17 = 0;
EVP_CIPHER_CTX_free(v16);
CRYPTO_free(v15, (__int64)"crypto/cms/cms_pwri.c", 387);
LABEL_11:
X509_ALGOR_free(v9);
return v17;
}
EVP_CIPHER_CTX_set_padding((__int64)v14, 0);
if ( (EVP_CIPHER_asn1_to_param((__int64)v14, *(_QWORD *)(v9 + 8)) & 0x80000000) != 0 )
{
v15 = nullptr;
ERR_put_error(46, 167, 102, (__int64)"crypto/cms/cms_pwri.c", 330);
goto LABEL_10;
}
if ( (EVP_PBE_CipherInit(
**(_QWORD **)(v4 + 8),
*(_QWORD *)(v4 + 32),
*(unsigned int *)(v4 + 40),
*(_QWORD *)(*(_QWORD *)(v4 + 8) + 8LL),
v14,
a3)
& 0x80000000) != 0 )
{
v15 = nullptr;
ERR_put_error(46, 167, 6, (__int64)"crypto/cms/cms_pwri.c", 341);
goto LABEL_10;
}
if ( a3 )
{
v18 = *(_QWORD *)(v5 + 40);
v19 = (int)EVP_CIPHER_CTX_block_size((__int64)v14);
v20 = (v18 + 3 + v19) / v19 * v19;
if ( v20 < 2 * v19 || v18 > 0xFF )
{
v15 = nullptr;
goto LABEL_10;
}
v15 = CRYPTO_malloc((void *)v20, (__int64)"crypto/cms/cms_pwri.c", 352);
if ( !v15 )
goto LABEL_9;
v21 = *(_QWORD *)(v5 + 40);
v22 = *(_BYTE **)(v5 + 32);
v23 = EVP_CIPHER_CTX_block_size((__int64)v14);
v24 = (v21 + 3 + v23) / v23 * v23;
if ( v24 < 2LL * v23 )
goto LABEL_10;
if ( v21 > 0xFF )
goto LABEL_10;
*v15 = v21;
v15[1] = ~*v22;
v15[2] = ~v22[1];
v15[3] = ~v22[2];
memcpy(v15 + 4, v22, v21);
if ( v24 > v21 + 4 && (int)RAND_bytes((__int64)&v15[v21 + 4], (unsigned int)(v24 - v21 - 4)) <= 0 )
goto LABEL_10;
if ( !(unsigned int)EVP_EncryptUpdate(v14, v15, &v37, v15, v24)
|| !(unsigned int)EVP_EncryptUpdate(v14, v15, &v37, v15, v24) )
{
goto LABEL_10;
}
v25 = *(_QWORD *)(v4 + 24);
*(_QWORD *)(v25 + 8) = v15;
*(_DWORD *)v25 = v24;
LABEL_25:
v26 = v14;
v17 = 1;
EVP_CIPHER_CTX_free(v26);
goto LABEL_11;
}
v15 = CRYPTO_malloc((void *)**(int **)(v4 + 24), (__int64)"crypto/cms/cms_pwri.c", 362);
if ( !v15 )
{
ERR_put_error(46, 167, 65, (__int64)"crypto/cms/cms_pwri.c", 365);
goto LABEL_10;
}
v27 = *(int **)(v4 + 24);
v28 = *v27;
v29 = *((char **)v27 + 1);
v30 = EVP_CIPHER_CTX_block_size((__int64)v14);
v31 = v30;
v32 = 2LL * v30;
if ( v28 >= v32 && !(v28 % v30) )
{
v36 = v30;
v33 = (char *)CRYPTO_malloc((void *)v28, (__int64)"crypto/cms/cms_pwri.c", 191);
if ( v33 )
{
v34 = v28 - v32;
if ( (unsigned int)EVP_DecryptUpdate(v14, &v33[v34], &v37, &v29[v34], 2 * v36) )
{
if ( (unsigned int)EVP_DecryptUpdate(v14, v33, &v37, &v33[v31 + v34], v36) )
{
if ( (unsigned int)EVP_DecryptUpdate(v14, v33, &v37, v29, (int)v28 - v36) )
{
if ( (unsigned int)EVP_DecryptInit_ex(
(__int64)v14,
(unsigned int *)(v28 % v31),
v28 % v31,
v28 % v31,
(const void *)(v28 % v31)) )
{
if ( (unsigned int)EVP_DecryptUpdate(v14, v33, &v37, v33, v28) )
{
if ( (unsigned __int8)((v33[5] ^ v33[2]) & (v33[4] ^ v33[1]) & (v33[6] ^ v33[3])) == 0xFF )
{
v35 = (unsigned __int8)*v33;
if ( v28 >= v35 - 4 )
{
memcpy(v15, v33 + 4, (unsigned __int8)v35);
CRYPTO_clear_free(v33, v28, (__int64)"crypto/cms/cms_pwri.c", 0xE1u);
*(_QWORD *)(v5 + 32) = v15;
*(_QWORD *)(v5 + 40) = (unsigned __int8)v35;
goto LABEL_25;
}
}
}
}
}
}
}
CRYPTO_clear_free(v33, v28, (__int64)"crypto/cms/cms_pwri.c", 0xE1u);
}
}
ERR_put_error(46, 167, 180, (__int64)"crypto/cms/cms_pwri.c", 371);
goto LABEL_10;
}
ERR_put_error(46, 167, 65, (__int64)"crypto/cms/cms_pwri.c", 321);
return 0;
}
else
{
ERR_put_error(46, 167, 148, (__int64)"crypto/cms/cms_pwri.c", 315);
return 0;
}
}
else
{
ERR_put_error(46, 167, 176, (__int64)"crypto/cms/cms_pwri.c", 308);
return 0;
}
}
else
{
ERR_put_error(46, 167, 178, (__int64)"crypto/cms/cms_pwri.c", 292);
return 0;
}
}