Libgourou

Libgourou Commit Details

Date:2022-12-21 20:15:11 (9 months 1 day ago)
Author:Grégory Soutadé
Branch:master
Commit:afab1c001258d32a268f2dfdbda976e403f42fdf
Parents: 7878f91cdda79b4860ed6564fbe5086740abb7c0
Message:Fix over encrypted RSA key decryption algorithm

Changes:
Minclude/libgourou.h (1 diff)
Msrc/libgourou.cpp (5 diffs)

File differences

include/libgourou.h
231231
232232
233233
234
234
235235
236236
237237
void buildSignInRequest(pugi::xml_document& signInRequest, const std::string& adobeID, const std::string& adobePassword, const std::string& authenticationCertificate);
void fetchLicenseServiceCertificate(const std::string& licenseURL,
const std::string& operatorURL);
std::string encryptedKeyFirstPass(pugi::xml_document& rightsDoc, const std::string& encryptedKey, const std::string& keyType, ITEM_TYPE type);
std::string encryptedKeyFirstPass(pugi::xml_document& rightsDoc, const std::string& encryptedKey, const std::string& keyType);
void decryptADEPTKey(const std::string& encryptedKey, unsigned char* decryptedKey);
void removeEPubDRM(const std::string& filenameIn, const std::string& filenameOut, const unsigned char* encryptionKey, unsigned encryptionKeySize);
void generatePDFObjectKey(int version,
src/libgourou.cpp
963963
964964
965965
966
967
966
967
968968
969969
970970
971971
972
972
973973
974974
975975
......
979979
980980
981981
982
983
984
985
986
987
988
989
990
991
992
982
983
993984
985
986
987
994988
995989
996990
......
10211015
10221016
10231017
1024
1018
10251019
10261020
10271021
......
10671061
10681062
10691063
1070
1064
10711065
10721066
10731067
......
12621256
12631257
12641258
1265
1259
12661260
12671261
12681262
/**
* RSA Key can be over encrypted with AES128-CBC if keyType attribute is set
* For EPUB, Key = SHA256(keyType)[14:22] || SHA256(keyType)[7:13]
* For PDF, Key = SHA256(keyType)[6:19] || SHA256(keyType)[3:6]
* remainder = keyType % 16
* Key = SHA256(keyType)[remainder*2:remainder*2+(16-remainder)] || SHA256(keyType)[16-remainder:16]
* IV = DeviceID ^ FulfillmentId ^ VoucherId
*
* @return Base64 encoded decrypted key
*/
std::string DRMProcessor::encryptedKeyFirstPass(pugi::xml_document& rightsDoc, const std::string& encryptedKey, const std::string& keyType, ITEM_TYPE type)
std::string DRMProcessor::encryptedKeyFirstPass(pugi::xml_document& rightsDoc, const std::string& encryptedKey, const std::string& keyType)
{
unsigned char digest[32], key[16], iv[16];
unsigned int dataOutLength;
dumpBuffer(gourou::LG_LOG_DEBUG, "SHA of KeyType : ", digest, sizeof(digest));
switch(type)
{
case EPUB:
memcpy(key, &digest[14], 9);
memcpy(&key[9], &digest[7], 7);
break;
case PDF:
memcpy(key, &digest[6], 13);
memcpy(&key[13], &digest[3], 3);
break;
}
long nonce = std::stol(keyType);
int remainder = nonce % 16;
memcpy(key, &digest[remainder*2], 16-remainder);
memcpy(&key[16-remainder], &digest[remainder], remainder);
id = extractTextElem(rightsDoc, "/adept:rights/licenseToken/device");
if (id == "")
EXCEPTION(DRM_ERR_ENCRYPTION_KEY_FP, "Device id not found in rights.xml");
dumpBuffer(gourou::LG_LOG_DEBUG, "First pass IV : ", iv, sizeof(iv));
unsigned char* clearRSAKey = new unsigned char[arrayEncryptedKey.size()];
client->decrypt(CryptoInterface::ALGO_AES, CryptoInterface::CHAIN_CBC,
(const unsigned char*)key, (unsigned int)sizeof(key),
(const unsigned char*)iv, (unsigned int)sizeof(iv),
std::string keyType = extractTextAttribute(rightsDoc, "/adept:rights/licenseToken/encryptedKey", "keyType", false);
if (keyType != "")
encryptedKey = encryptedKeyFirstPass(rightsDoc, encryptedKey, keyType, EPUB);
encryptedKey = encryptedKeyFirstPass(rightsDoc, encryptedKey, keyType);
decryptADEPTKey(encryptedKey, decryptedKey);
std::string keyType = extractTextAttribute(rightsDoc, "/adept:rights/licenseToken/encryptedKey", "keyType", false);
if (keyType != "")
encryptedKey = encryptedKeyFirstPass(rightsDoc, encryptedKey, keyType, PDF);
encryptedKey = encryptedKeyFirstPass(rightsDoc, encryptedKey, keyType);
decryptADEPTKey(encryptedKey, decryptedKey);

Archive Download the corresponding diff file