Send a warning when libgourou can't handle encryption algorithm during a DRM removal attempt (+ keep encryption.xml in output)

This commit is contained in:
Grégory Soutadé 2021-12-23 21:12:03 +01:00
parent f33891ef1c
commit e4c05bd6b3
2 changed files with 56 additions and 21 deletions

View File

@ -117,7 +117,8 @@ namespace gourou
DRM_VERSION_NOT_SUPPORTED, DRM_VERSION_NOT_SUPPORTED,
DRM_FILE_ERROR, DRM_FILE_ERROR,
DRM_FORMAT_NOT_SUPPORTED, DRM_FORMAT_NOT_SUPPORTED,
DRM_IN_OUT_EQUALS DRM_IN_OUT_EQUALS,
DRM_MISSING_PARAMETER
}; };
/** /**

View File

@ -948,6 +948,7 @@ namespace gourou
void DRMProcessor::removeEPubDRM(const std::string& filenameIn, const std::string& filenameOut) void DRMProcessor::removeEPubDRM(const std::string& filenameIn, const std::string& filenameOut)
{ {
ByteArray zipData; ByteArray zipData;
bool removeEncryptionXML = true;
void* zipHandler = client->zipOpen(filenameOut); void* zipHandler = client->zipOpen(filenameOut);
client->zipReadFile(zipHandler, "META-INF/rights.xml", zipData); client->zipReadFile(zipHandler, "META-INF/rights.xml", zipData);
@ -963,39 +964,72 @@ namespace gourou
pugi::xml_document encryptionDoc; pugi::xml_document encryptionDoc;
encryptionDoc.load_string((const char*)zipData.data()); encryptionDoc.load_string((const char*)zipData.data());
pugi::xpath_node_set nodeSet = encryptionDoc.select_nodes("//CipherReference"); pugi::xpath_node_set nodeSet = encryptionDoc.select_nodes("//EncryptedData");
for (pugi::xpath_node_set::const_iterator it = nodeSet.begin(); for (pugi::xpath_node_set::const_iterator it = nodeSet.begin();
it != nodeSet.end(); ++it) it != nodeSet.end(); ++it)
{ {
std::string encryptedFile = it->node().attribute("URI").value(); pugi::xml_node encryptionMethod = it->node().child("EncryptionMethod");
pugi::xml_node cipherReference = it->node().child("CipherData").child("CipherReference");
GOUROU_LOG(DEBUG, "Encrypted file " << encryptedFile); std::string encryptionType = encryptionMethod.attribute("Algorithm").value();
std::string encryptedFile = cipherReference.attribute("URI").value();
client->zipReadFile(zipHandler, encryptedFile, zipData, false); if (encryptionType == "")
{
EXCEPTION(DRM_MISSING_PARAMETER, "Missing Algorithm attribute in encryption.xml");
}
else if (encryptionType == "http://www.w3.org/2001/04/xmlenc#aes128-cbc")
{
if (encryptedFile == "")
{
EXCEPTION(DRM_MISSING_PARAMETER, "Missing URI attribute in encryption.xml");
}
unsigned char* _data = zipData.data(); GOUROU_LOG(DEBUG, "Encrypted file " << encryptedFile);
ByteArray clearData(zipData.length()-16+1, true); /* Reserve 1 byte for 'Z' */
unsigned char* _clearData = clearData.data();
gourou::ByteArray inflateData(true);
unsigned int dataOutLength;
client->Decrypt(CryptoInterface::ALGO_AES, CryptoInterface::CHAIN_CBC, client->zipReadFile(zipHandler, encryptedFile, zipData, false);
decryptedKey+RSA_KEY_SIZE-16, 16, /* Key */
_data, 16, /* IV */
&_data[16], zipData.length()-16,
_clearData, &dataOutLength);
// Add 'Z' at the end, done in ineptepub.py unsigned char* _data = zipData.data();
_clearData[dataOutLength] = 'Z'; ByteArray clearData(zipData.length()-16+1, true); /* Reserve 1 byte for 'Z' */
unsigned char* _clearData = clearData.data();
gourou::ByteArray inflateData(true);
unsigned int dataOutLength;
client->inflate(clearData, inflateData); client->Decrypt(CryptoInterface::ALGO_AES, CryptoInterface::CHAIN_CBC,
decryptedKey+RSA_KEY_SIZE-16, 16, /* Key */
_data, 16, /* IV */
&_data[16], zipData.length()-16,
_clearData, &dataOutLength);
client->zipWriteFile(zipHandler, encryptedFile, inflateData); // Add 'Z' at the end, done in ineptepub.py
_clearData[dataOutLength] = 'Z';
clearData.resize(dataOutLength+1);
client->inflate(clearData, inflateData);
client->zipWriteFile(zipHandler, encryptedFile, inflateData);
it->node().parent().remove_child(it->node());
}
else
{
GOUROU_LOG(WARN, "Unsupported encryption algorithm " << encryptionType << ", for file " << encryptedFile);
removeEncryptionXML = false;
}
} }
client->zipDeleteFile(zipHandler, "META-INF/rights.xml"); client->zipDeleteFile(zipHandler, "META-INF/rights.xml");
client->zipDeleteFile(zipHandler, "META-INF/encryption.xml"); if (removeEncryptionXML)
client->zipDeleteFile(zipHandler, "META-INF/encryption.xml");
else
{
StringXMLWriter xmlWriter;
encryptionDoc.save(xmlWriter, " ");
std::string xmlStr = xmlWriter.getResult();
ByteArray ba(xmlStr);
client->zipWriteFile(zipHandler, "META-INF/encryption.xml", ba);
}
client->zipClose(zipHandler); client->zipClose(zipHandler);
} }