Implement MakeReader/Writer functions.

This commit is contained in:
Michael R Sweet 2021-10-10 23:08:56 -04:00
parent 3de55421b5
commit 0caea44f32
No known key found for this signature in database
GPG Key ID: 999559A027815955
2 changed files with 88 additions and 14 deletions

View File

@ -208,16 +208,52 @@ _pdfio_crypto_cb_t // O - Decryption callback or `NULL` for none
uint8_t *iv, // I - Buffer for initialization vector uint8_t *iv, // I - Buffer for initialization vector
size_t *ivlen) // IO - Size of initialization vector size_t *ivlen) // IO - Size of initialization vector
{ {
(void)pdf; uint8_t data[21]; /* Key data */
(void)obj; _pdfio_md5_t md5; /* MD5 state */
(void)ctx; uint8_t digest[16]; /* MD5 digest value */
(void)iv;
// No decryption... // Range check input...
*ivlen = 0; if (!pdf)
{
*ivlen = 0;
return (NULL);
}
return (NULL); switch (pdf->encryption)
{
default :
*ivlen = 0;
return (NULL);
case PDFIO_ENCRYPTION_RC4_128 :
case PDFIO_ENCRYPTION_AES_128 :
// Copy the key data for the MD5 hash.
memcpy(data, pdf->encryption_key, sizeof(pdf->encryption_key));
data[16] = (uint8_t)obj->number;
data[17] = (uint8_t)(obj->number >> 8);
data[18] = (uint8_t)(obj->number >> 16);
data[19] = (uint8_t)obj->generation;
data[20] = (uint8_t)(obj->generation >> 8);
// Hash it...
_pdfioCryptoMD5Init(&md5);
_pdfioCryptoMD5Append(&md5, data, sizeof(data));
_pdfioCryptoMD5Finish(&md5, digest);
// Initialize the RC4/AES context using the digest...
if (pdf->encryption == PDFIO_ENCRYPTION_RC4_128)
{
_pdfioCryptoRC4Init(&ctx->rc4, digest, sizeof(digest));
return ((_pdfio_crypto_cb_t)_pdfioCryptoRC4Crypt);
}
else
{
*ivlen = 16;
_pdfioCryptoAESInit(&ctx->aes, digest, sizeof(digest), iv);
return ((_pdfio_crypto_cb_t)_pdfioCryptoAESDecrypt);
}
}
} }
@ -233,14 +269,51 @@ _pdfio_crypto_cb_t // O - Encryption callback or `NULL` for none
uint8_t *iv, // I - Buffer for initialization vector uint8_t *iv, // I - Buffer for initialization vector
size_t *ivlen) // IO - Size of initialization vector size_t *ivlen) // IO - Size of initialization vector
{ {
(void)pdf; uint8_t data[21]; /* Key data */
(void)obj; _pdfio_md5_t md5; /* MD5 state */
(void)ctx; uint8_t digest[16]; /* MD5 digest value */
(void)iv;
// No encryption... // Range check input...
*ivlen = 0; if (!pdf)
{
*ivlen = 0;
return (NULL);
}
return (NULL); switch (pdf->encryption)
{
default :
*ivlen = 0;
return (NULL);
case PDFIO_ENCRYPTION_RC4_128 :
case PDFIO_ENCRYPTION_AES_128 :
// Copy the key data for the MD5 hash.
memcpy(data, pdf->encryption_key, sizeof(pdf->encryption_key));
data[16] = (uint8_t)obj->number;
data[17] = (uint8_t)(obj->number >> 8);
data[18] = (uint8_t)(obj->number >> 16);
data[19] = (uint8_t)obj->generation;
data[20] = (uint8_t)(obj->generation >> 8);
// Hash it...
_pdfioCryptoMD5Init(&md5);
_pdfioCryptoMD5Append(&md5, data, sizeof(data));
_pdfioCryptoMD5Finish(&md5, digest);
// Initialize the RC4/AES context using the digest...
if (pdf->encryption == PDFIO_ENCRYPTION_RC4_128)
{
_pdfioCryptoRC4Init(&ctx->rc4, digest, sizeof(digest));
return ((_pdfio_crypto_cb_t)_pdfioCryptoRC4Crypt);
}
else
{
*ivlen = 16;
_pdfioCryptoMakeRandom(iv, *ivlen);
_pdfioCryptoAESInit(&ctx->aes, digest, sizeof(digest), iv);
return ((_pdfio_crypto_cb_t)_pdfioCryptoAESEncrypt);
}
}
} }

View File

@ -2022,6 +2022,7 @@ write_trailer(pdfio_file_t *pdf) // I - PDF file
if ((pdf->trailer_dict = pdfioDictCreate(pdf)) == NULL) if ((pdf->trailer_dict = pdfioDictCreate(pdf)) == NULL)
{ {
_pdfioFileError(pdf, "Unable to create trailer.");
ret = false; ret = false;
goto done; goto done;
} }