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,12 +964,27 @@ 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");
std::string encryptionType = encryptionMethod.attribute("Algorithm").value();
std::string encryptedFile = cipherReference.attribute("URI").value();
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");
}
GOUROU_LOG(DEBUG, "Encrypted file " << encryptedFile); GOUROU_LOG(DEBUG, "Encrypted file " << encryptedFile);
@ -988,14 +1004,32 @@ namespace gourou
// Add 'Z' at the end, done in ineptepub.py // Add 'Z' at the end, done in ineptepub.py
_clearData[dataOutLength] = 'Z'; _clearData[dataOutLength] = 'Z';
clearData.resize(dataOutLength+1);
client->inflate(clearData, inflateData); client->inflate(clearData, inflateData);
client->zipWriteFile(zipHandler, encryptedFile, 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");
if (removeEncryptionXML)
client->zipDeleteFile(zipHandler, "META-INF/encryption.xml"); 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);
} }