@@ -1109,14 +1109,16 @@ void _parse_pkcs7(
1109
1109
PKCS7 * pkcs7 ,
1110
1110
int * counter )
1111
1111
{
1112
- int i ;
1112
+ int i , j ;
1113
1113
STACK_OF (X509 )* certs ;
1114
+
1115
+ if (* counter >= MAX_PE_CERTS )
1116
+ return ;
1117
+
1114
1118
certs = PKCS7_get0_signers (pkcs7 , NULL , 0 );
1115
1119
1116
1120
if (!certs )
1117
- {
1118
1121
return ;
1119
- }
1120
1122
1121
1123
for (i = 0 ; i < sk_X509_num (certs ); i ++ )
1122
1124
{
@@ -1129,18 +1131,18 @@ void _parse_pkcs7(
1129
1131
unsigned char thumbprint [YR_SHA1_LEN ];
1130
1132
char thumbprint_ascii [YR_SHA1_LEN * 2 + 1 ];
1131
1133
1132
- PKCS7_SIGNER_INFO * signer_info ;
1133
- ASN1_INTEGER * serial ;
1134
- ASN1_TYPE * nested ;
1135
- ASN1_STRING * value ;
1136
- PKCS7 * nested_pkcs7 ;
1134
+ PKCS7_SIGNER_INFO * signer_info = NULL ;
1135
+ PKCS7 * nested_pkcs7 = NULL ;
1136
+ ASN1_INTEGER * serial = NULL ;
1137
+ ASN1_TYPE * nested = NULL ;
1138
+ ASN1_STRING * value = NULL ;
1137
1139
1138
1140
X509 * cert = sk_X509_value (certs , i );
1139
1141
1140
1142
X509_digest (cert , sha1_digest , thumbprint , NULL );
1141
1143
1142
- for (i = 0 ; i < YR_SHA1_LEN ; i ++ )
1143
- sprintf (thumbprint_ascii + (i * 2 ), "%02x" , thumbprint [i ]);
1144
+ for (j = 0 ; j < YR_SHA1_LEN ; j ++ )
1145
+ sprintf (thumbprint_ascii + (j * 2 ), "%02x" , thumbprint [j ]);
1144
1146
1145
1147
set_string (
1146
1148
(char * ) thumbprint_ascii ,
@@ -1227,8 +1229,6 @@ void _parse_pkcs7(
1227
1229
1228
1230
if (serial_ascii )
1229
1231
{
1230
- int j ;
1231
-
1232
1232
for (j = 0 ; j < bytes ; j ++ )
1233
1233
{
1234
1234
// Don't put the colon on the last one.
@@ -1292,6 +1292,7 @@ void pe_parse_certificates(
1292
1292
int counter = 0 ;
1293
1293
1294
1294
const uint8_t * eod ;
1295
+ const unsigned char * cert_p ;
1295
1296
uintptr_t end ;
1296
1297
1297
1298
PWIN_CERTIFICATE win_cert ;
@@ -1339,7 +1340,6 @@ void pe_parse_certificates(
1339
1340
(uint8_t * ) win_cert + sizeof (WIN_CERTIFICATE ) < eod &&
1340
1341
(uint8_t * ) win_cert + yr_le32toh (win_cert -> Length ) <= eod )
1341
1342
{
1342
- BIO * cert_bio ;
1343
1343
PKCS7 * pkcs7 ;
1344
1344
1345
1345
// Some sanity checks
@@ -1357,30 +1357,24 @@ void pe_parse_certificates(
1357
1357
if (yr_le16toh (win_cert -> Revision ) != WIN_CERT_REVISION_2_0 ||
1358
1358
yr_le16toh (win_cert -> CertificateType ) != WIN_CERT_TYPE_PKCS_SIGNED_DATA )
1359
1359
{
1360
- uintptr_t end = (uintptr_t )
1361
- ((uint8_t * ) win_cert ) + yr_le32toh (win_cert -> Length );
1360
+ end = (uintptr_t )((uint8_t * ) win_cert ) + yr_le32toh (win_cert -> Length );
1362
1361
1363
1362
win_cert = (PWIN_CERTIFICATE ) (end + (end % 8 ));
1364
1363
continue ;
1365
1364
}
1366
1365
1367
- cert_bio = BIO_new_mem_buf (
1368
- win_cert -> Certificate , yr_le32toh (win_cert -> Length ) - WIN_CERTIFICATE_HEADER_SIZE );
1369
-
1370
- if (!cert_bio )
1371
- break ;
1372
-
1373
- pkcs7 = d2i_PKCS7_bio (cert_bio , NULL );
1374
- if (pkcs7 != NULL )
1366
+ cert_p = win_cert -> Certificate ;
1367
+ end = (uintptr_t )((uint8_t * ) win_cert ) + yr_le32toh (win_cert -> Length );
1368
+ while ((uintptr_t ) cert_p < end && counter < MAX_PE_CERTS )
1375
1369
{
1370
+ pkcs7 = d2i_PKCS7 (NULL , & cert_p , (win_cert -> Length ));
1371
+ if (pkcs7 == NULL )
1372
+ break ;
1376
1373
_parse_pkcs7 (pe , pkcs7 , & counter );
1377
1374
PKCS7_free (pkcs7 );
1378
1375
}
1379
1376
1380
- end = (uintptr_t )((uint8_t * ) win_cert ) + yr_le32toh (win_cert -> Length );
1381
- win_cert = (PWIN_CERTIFICATE )(end + (end % 8 ));
1382
-
1383
- BIO_free (cert_bio );
1377
+ win_cert = (PWIN_CERTIFICATE ) (end + (end % 8 ));
1384
1378
}
1385
1379
1386
1380
set_integer (counter , pe -> object , "number_of_signatures" );
0 commit comments