Fix AES-128 writing/encryption.

This commit is contained in:
Michael R Sweet 2021-10-23 20:09:02 -04:00
parent dd56317635
commit 208c3419ff
No known key found for this signature in database
GPG Key ID: 999559A027815955
2 changed files with 29 additions and 0 deletions

View File

@ -239,6 +239,8 @@ _pdfio_crypto_cb_t // O - Decryption callback or `NULL` for none
// Hash it... // Hash it...
_pdfioCryptoMD5Init(&md5); _pdfioCryptoMD5Init(&md5);
_pdfioCryptoMD5Append(&md5, data, sizeof(data)); _pdfioCryptoMD5Append(&md5, data, sizeof(data));
if (pdf->encryption == PDFIO_ENCRYPTION_AES_128)
_pdfioCryptoMD5Append(&md5, (const uint8_t *)"sAlT", 4);
_pdfioCryptoMD5Finish(&md5, digest); _pdfioCryptoMD5Finish(&md5, digest);
// Initialize the RC4/AES context using the digest... // Initialize the RC4/AES context using the digest...
@ -301,6 +303,8 @@ _pdfio_crypto_cb_t // O - Encryption callback or `NULL` for none
// Hash it... // Hash it...
_pdfioCryptoMD5Init(&md5); _pdfioCryptoMD5Init(&md5);
_pdfioCryptoMD5Append(&md5, data, sizeof(data)); _pdfioCryptoMD5Append(&md5, data, sizeof(data));
if (pdf->encryption == PDFIO_ENCRYPTION_AES_128)
_pdfioCryptoMD5Append(&md5, (const uint8_t *)"sAlT", 4);
_pdfioCryptoMD5Finish(&md5, digest); _pdfioCryptoMD5Finish(&md5, digest);
// Initialize the RC4/AES context using the digest... // Initialize the RC4/AES context using the digest...

View File

@ -1085,6 +1085,8 @@ pdfioFileSetPermissions(
perm_bytes[4], // Permissions bytes perm_bytes[4], // Permissions bytes
*file_id; // File ID bytes *file_id; // File ID bytes
size_t file_id_len; // Length of file ID size_t file_id_len; // Length of file ID
pdfio_dict_t *cf_dict, // CF dictionary
*filter_dict; // CryptFilter dictionary
static uint8_t pad[32] = // Padding for passwords static uint8_t pad[32] = // Padding for passwords
{ {
0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
@ -1237,6 +1239,29 @@ pdfioFileSetPermissions(
pdfioDictSetNumber(dict, "R", encryption == PDFIO_ENCRYPTION_RC4_128 ? 3 : 4); pdfioDictSetNumber(dict, "R", encryption == PDFIO_ENCRYPTION_RC4_128 ? 3 : 4);
pdfioDictSetNumber(dict, "V", encryption == PDFIO_ENCRYPTION_RC4_128 ? 2 : 4); pdfioDictSetNumber(dict, "V", encryption == PDFIO_ENCRYPTION_RC4_128 ? 2 : 4);
pdfioDictSetBinary(dict, "U", pdf->user_key, sizeof(pdf->user_key)); pdfioDictSetBinary(dict, "U", pdf->user_key, sizeof(pdf->user_key));
if (encryption == PDFIO_ENCRYPTION_AES_128)
{
if ((cf_dict = pdfioDictCreate(pdf)) == NULL)
{
_pdfioFileError(pdf, "Unable to create Encryption CF dictionary.");
return (false);
}
if ((filter_dict = pdfioDictCreate(pdf)) == NULL)
{
_pdfioFileError(pdf, "Unable to create Encryption CryptFilter dictionary.");
return (false);
}
pdfioDictSetName(filter_dict, "Type", "CryptFilter");
pdfioDictSetName(filter_dict, "CFM", encryption == PDFIO_ENCRYPTION_RC4_128 ? "V2" : "AESV2");
pdfioDictSetDict(cf_dict, "PDFio", filter_dict);
pdfioDictSetDict(dict, "CF", cf_dict);
pdfioDictSetName(dict, "StmF", "PDFio");
pdfioDictSetName(dict, "StrF", "PDFio");
pdfioDictSetBoolean(dict, "EncryptMetadata", true);
}
break; break;
case PDFIO_ENCRYPTION_AES_256 : case PDFIO_ENCRYPTION_AES_256 :