Calculate padding length

This commit is contained in:
olszomal 2023-03-29 14:05:03 +02:00 committed by Michał Trojnara
parent 09555e8c05
commit dd365d68c4
2 changed files with 7 additions and 8 deletions

4
cab.c
View File

@ -529,7 +529,7 @@ static int cab_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
} }
i2d_PKCS7(p7, &p); i2d_PKCS7(p7, &p);
p -= len; p -= len;
padlen = (8 - len % 8) % 8; padlen = len % 8 ? 8 - len % 8 : 0;
BIO_write(outdata, p, len); BIO_write(outdata, p, len);
/* pad (with 0's) asn1 blob to 8 byte boundary */ /* pad (with 0's) asn1 blob to 8 byte boundary */
if (padlen > 0) { if (padlen > 0) {
@ -562,7 +562,7 @@ static void cab_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
} }
(void)BIO_seek(outdata, 0x30); (void)BIO_seek(outdata, 0x30);
len = i2d_PKCS7(p7, NULL); len = i2d_PKCS7(p7, NULL);
padlen = (8 - len % 8) % 8; padlen = len % 8 ? 8 - len % 8 : 0;
PUT_UINT32_LE(len + padlen, buf); PUT_UINT32_LE(len + padlen, buf);
BIO_write(outdata, buf, 4); BIO_write(outdata, buf, 4);
} }

11
pe.c
View File

@ -206,8 +206,8 @@ static int pe_check_file(FILE_FORMAT_CTX *ctx, int detached)
*/ */
while (sum < ctx->pe_ctx->siglen) { while (sum < ctx->pe_ctx->siglen) {
uint32_t len = GET_UINT32_LE(ctx->options->indata + ctx->pe_ctx->sigpos + sum); uint32_t len = GET_UINT32_LE(ctx->options->indata + ctx->pe_ctx->sigpos + sum);
if (len % 8) /* quadword align data */
len += (8 - len % 8); len += len % 8 ? 8 - len % 8 : 0;
sum += len; sum += len;
} }
if (sum != ctx->pe_ctx->siglen) { if (sum != ctx->pe_ctx->siglen) {
@ -490,7 +490,7 @@ static int pe_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
} }
i2d_PKCS7(p7, &p); i2d_PKCS7(p7, &p);
p -= len; p -= len;
padlen = (8 - len % 8) % 8; padlen = len % 8 ? 8 - len % 8 : 0;
PUT_UINT32_LE(len + 8 + padlen, buf); PUT_UINT32_LE(len + 8 + padlen, buf);
PUT_UINT16_LE(WIN_CERT_REVISION_2_0, buf + 4); PUT_UINT16_LE(WIN_CERT_REVISION_2_0, buf + 4);
PUT_UINT16_LE(WIN_CERT_TYPE_PKCS_SIGNED_DATA, buf + 6); PUT_UINT16_LE(WIN_CERT_TYPE_PKCS_SIGNED_DATA, buf + 6);
@ -524,7 +524,7 @@ static void pe_update_data_size(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
} }
if (ctx->options->cmd != CMD_REMOVE) { if (ctx->options->cmd != CMD_REMOVE) {
int len = i2d_PKCS7(p7, NULL); int len = i2d_PKCS7(p7, NULL);
int padlen = (8 - len % 8) % 8; int padlen = len % 8 ? 8 - len % 8 : 0;
/* Update signature position and size */ /* Update signature position and size */
(void)BIO_seek(outdata, (void)BIO_seek(outdata,
@ -692,8 +692,7 @@ static PKCS7 *pe_pkcs7_get_file(char *indata, PE_CTX *pe_ctx)
return d2i_PKCS7(NULL, &blob, len - 8); return d2i_PKCS7(NULL, &blob, len - 8);
} }
/* quadword align data */ /* quadword align data */
if (len % 8) len += len % 8 ? 8 - len % 8 : 0;
len += (8 - len % 8);
pos += len; pos += len;
} }
return NULL; /* FAILED */ return NULL; /* FAILED */