Add "export private key" feature

This commit is contained in:
Grégory Soutadé 2021-08-25 21:53:54 +02:00
parent 3d9e343734
commit 2ac917619e
3 changed files with 94 additions and 39 deletions

View File

@ -164,6 +164,8 @@ namespace gourou
*/ */
std::string serializeRSAPrivateKey(void* rsa); std::string serializeRSAPrivateKey(void* rsa);
void exportPrivateLicenseKey(std::string path);
/** /**
* @brief Get current user * @brief Get current user
*/ */

View File

@ -899,6 +899,19 @@ namespace gourou
return res.toBase64(); return res.toBase64();
} }
void DRMProcessor::exportPrivateLicenseKey(std::string path)
{
int fd = open(path.c_str(), O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
if (fd <= 0)
EXCEPTION(GOUROU_FILE_ERROR, "Unable to open " << path);
ByteArray privateLicenseKey = ByteArray::fromBase64(user->getPrivateLicenseKey());
/* In adobekey.py, we get base64 decoded data [26:] */
write(fd, privateLicenseKey.data()+26, privateLicenseKey.length()-1-26);
close(fd);
}
int DRMProcessor::getLogLevel() {return (int)gourou::logLevel;} int DRMProcessor::getLogLevel() {return (int)gourou::logLevel;}
void DRMProcessor::setLogLevel(int logLevel) {gourou::logLevel = (GOUROU_LOG_LEVEL)logLevel;} void DRMProcessor::setLogLevel(int logLevel) {gourou::logLevel = (GOUROU_LOG_LEVEL)logLevel;}
} }

View File

@ -46,6 +46,7 @@ static const char* deviceFile = "device.xml";
static const char* activationFile = "activation.xml"; static const char* activationFile = "activation.xml";
static const char* devicekeyFile = "devicesalt"; static const char* devicekeyFile = "devicesalt";
static const char* acsmFile = 0; static const char* acsmFile = 0;
static bool exportPrivateKey = false;
static const char* outputFile = 0; static const char* outputFile = 0;
static const char* outputDir = 0; static const char* outputDir = 0;
static const char* defaultDirs[] = { static const char* defaultDirs[] = {
@ -71,42 +72,65 @@ public:
{ {
DRMProcessorClientImpl client; DRMProcessorClientImpl client;
gourou::DRMProcessor processor(&client, deviceFile, activationFile, devicekeyFile); gourou::DRMProcessor processor(&client, deviceFile, activationFile, devicekeyFile);
gourou::User* user = processor.getUser();
gourou::FulfillmentItem* item = processor.fulfill(acsmFile); if (exportPrivateKey)
std::string filename;
if (!outputFile)
{ {
filename = item->getMetadata("title"); std::string filename;
if (filename == "") if (!outputFile)
filename = "output"; filename = std::string("Adobe_PrivateLicenseKey--") + user->getUsername() + ".der";
if (outputDir)
{
QDir dir(outputDir);
if (!dir.exists(outputDir))
dir.mkpath(outputDir);
filename = std::string(outputDir) + "/" + filename;
}
processor.exportPrivateLicenseKey(filename);
std::cout << "Private license key exported to " << filename << std::endl;
} }
else else
filename = outputFile;
if (outputDir)
{ {
QDir dir(outputDir); gourou::FulfillmentItem* item = processor.fulfill(acsmFile);
if (!dir.exists(outputDir))
dir.mkpath(outputDir);
filename = std::string(outputDir) + "/" + filename; std::string filename;
} if (!outputFile)
{
gourou::DRMProcessor::ITEM_TYPE type = processor.download(item, filename); filename = item->getMetadata("title");
if (filename == "")
if (!outputFile) filename = "output";
{ }
std::string finalName = filename;
if (type == gourou::DRMProcessor::ITEM_TYPE::PDF)
finalName += ".pdf";
else else
finalName += ".epub"; filename = outputFile;
QDir dir;
dir.rename(filename.c_str(), finalName.c_str()); if (outputDir)
filename = finalName; {
QDir dir(outputDir);
if (!dir.exists(outputDir))
dir.mkpath(outputDir);
filename = std::string(outputDir) + "/" + filename;
}
gourou::DRMProcessor::ITEM_TYPE type = processor.download(item, filename);
if (!outputFile)
{
std::string finalName = filename;
if (type == gourou::DRMProcessor::ITEM_TYPE::PDF)
finalName += ".pdf";
else
finalName += ".epub";
QDir dir;
dir.rename(filename.c_str(), finalName.c_str());
filename = finalName;
}
std::cout << "Created " << filename << std::endl;
} }
std::cout << "Created " << filename << std::endl;
} catch(std::exception& e) } catch(std::exception& e)
{ {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
@ -149,14 +173,15 @@ static void usage(const char* cmd)
{ {
std::cout << "Download EPUB file from ACSM request file" << std::endl; std::cout << "Download EPUB file from ACSM request file" << std::endl;
std::cout << "Usage: " << cmd << " [(-d|--device-file) device.xml] [(-a|--activation-file) activation.xml] [(-s|--device-key-file) devicesalt] [(-O|--output-dir) dir] [(-o|--output-file) output.epub] [(-v|--verbose)] [(-h|--help)] (-f|--acsm-file) file.acsm" << std::endl << std::endl; std::cout << "Usage: " << cmd << " [(-d|--device-file) device.xml] [(-a|--activation-file) activation.xml] [(-s|--device-key-file) devicesalt] [(-O|--output-dir) dir] [(-o|--output-file) output(.epub|.pdf|.der)] [(-v|--verbose)] [(-h|--help)] (-f|--acsm-file) file.acsm|(-e|--export-private-key)" << std::endl << std::endl;
std::cout << " " << "-d|--device-file" << "\t" << "device.xml file from eReader" << std::endl; std::cout << " " << "-d|--device-file" << "\t" << "device.xml file from eReader" << std::endl;
std::cout << " " << "-a|--activation-file" << "\t" << "activation.xml file from eReader" << std::endl; std::cout << " " << "-a|--activation-file" << "\t" << "activation.xml file from eReader" << std::endl;
std::cout << " " << "-k|--device-key-file" << "\t" << "private device key file (eg devicesalt/devkey.bin) from eReader" << std::endl; std::cout << " " << "-k|--device-key-file" << "\t" << "private device key file (eg devicesalt/devkey.bin) from eReader" << std::endl;
std::cout << " " << "-O|--output-dir" << "\t" << "Optional output directory were to put result (default ./)" << std::endl; std::cout << " " << "-O|--output-dir" << "\t" << "Optional output directory were to put result (default ./)" << std::endl;
std::cout << " " << "-o|--output-file" << "\t" << "Optional output epub filename (default <title.epub>)" << std::endl; std::cout << " " << "-o|--output-file" << "\t" << "Optional output filename (default <title.(epub|pdf|der)>)" << std::endl;
std::cout << " " << "-f|--acsm-file" << "\t" << "ACSM request file for epub download" << std::endl; std::cout << " " << "-f|--acsm-file" << "\t" << "ACSM request file for epub download" << std::endl;
std::cout << " " << "-e|--export-private-key"<< "\t" << "Export private key in DER format" << std::endl;
std::cout << " " << "-v|--verbose" << "\t\t" << "Increase verbosity, can be set multiple times" << std::endl; std::cout << " " << "-v|--verbose" << "\t\t" << "Increase verbosity, can be set multiple times" << std::endl;
std::cout << " " << "-V|--version" << "\t\t" << "Display libgourou version" << std::endl; std::cout << " " << "-V|--version" << "\t\t" << "Display libgourou version" << std::endl;
std::cout << " " << "-h|--help" << "\t\t" << "This help" << std::endl; std::cout << " " << "-h|--help" << "\t\t" << "This help" << std::endl;
@ -185,13 +210,14 @@ int main(int argc, char** argv)
{"output-dir", required_argument, 0, 'O' }, {"output-dir", required_argument, 0, 'O' },
{"output-file", required_argument, 0, 'o' }, {"output-file", required_argument, 0, 'o' },
{"acsm-file", required_argument, 0, 'f' }, {"acsm-file", required_argument, 0, 'f' },
{"export-private-key",no_argument, 0, 'e' },
{"verbose", no_argument, 0, 'v' }, {"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' }, {"version", no_argument, 0, 'V' },
{"help", no_argument, 0, 'h' }, {"help", no_argument, 0, 'h' },
{0, 0, 0, 0 } {0, 0, 0, 0 }
}; };
c = getopt_long(argc, argv, "d:a:k:O:o:f:vVh", c = getopt_long(argc, argv, "d:a:k:O:o:f:evVh",
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -215,6 +241,9 @@ int main(int argc, char** argv)
case 'o': case 'o':
outputFile = optarg; outputFile = optarg;
break; break;
case 'e':
exportPrivateKey = true;
break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
@ -232,7 +261,7 @@ int main(int argc, char** argv)
gourou::DRMProcessor::setLogLevel(verbose); gourou::DRMProcessor::setLogLevel(verbose);
if (!acsmFile || (outputDir && !outputDir[0]) || if ((!acsmFile && !exportPrivateKey) || (outputDir && !outputDir[0]) ||
(outputFile && !outputFile[0])) (outputFile && !outputFile[0]))
{ {
usage(argv[0]); usage(argv[0]);
@ -254,12 +283,23 @@ int main(int argc, char** argv)
} }
} }
QFile file(acsmFile); if (exportPrivateKey)
if (!file.exists())
{ {
std::cout << "Error : " << acsmFile << " doesn't exists" << std::endl; if (acsmFile)
ret = -1; {
goto end; usage(argv[0]);
return -1;
}
}
else
{
QFile file(acsmFile);
if (!file.exists())
{
std::cout << "Error : " << acsmFile << " doesn't exists" << std::endl;
ret = -1;
goto end;
}
} }
QThreadPool::globalInstance()->start(&downloader); QThreadPool::globalInstance()->start(&downloader);