generování s cestou

This commit is contained in:
2023-08-18 12:21:27 +02:00
parent b7811685e2
commit b2260178f6
4 changed files with 79 additions and 331 deletions

View File

@@ -1,12 +1,12 @@
#include <cstring>
#include <fstream>
#include <filesystem>
#include "plcLicence.h"
#include "utils.h"
#define CID_LENGTH 32
#define CSD_LENGTH 32
const std::string cEzLic_p78ou3_sdinfofilepath = "/sys/block/mmcblk0/device/";
const std::string cEzLic_p78ou3_licencfilepath_f21 = "/mnt/mmc/ez_sys/licence/";
const std::string cEzLic_p78ou3_licencfilepath_f17 = "/home/admin/ez/licence/";
@@ -50,23 +50,26 @@ BYTE cHexNibble_to_No[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
std::string cEzLic_p78ou3_CipherAES = "aes-256-cbc"; // konfigurace kriptovaciho algoritmu
PlcLicence::PlcLicence()
{
}
/// @brief process config file
/// @param dataFileName
/// @brief process config file
/// @param dataFileName
void PlcLicence::processConfigFile(string &dataFileName)
{
std::string content;
#ifdef LINUX
// dataFileName = getCompletePath(dataFileName);
#endif
std::ifstream inputFile(dataFileName);
if (inputFile.is_open())
{
content.assign((std::istreambuf_iterator<char>(inputFile)), (std::istreambuf_iterator<char>()));
inputFile.close();
//std::cout << "File content:\n" << content << std::endl;
// std::cout << "File content:\n" << content << std::endl;
}
else
{
@@ -84,22 +87,18 @@ void PlcLicence::processConfigFile(string &dataFileName)
// char stationCh[station.length()] = {};
// getCharsFromString(station, stationCh);
// char distributorCh[distributor.length()] = {};
// getCharsFromString(distributor, distributorCh);
// this->stationName = stationCh;
// this->distributor = distributorCh;
// cout << this->stationName << "\n";
// cout << this->distributor << "\n";
// this->stationName = "ceskatrebova";
// this->distributor = "RO1";
// cout << this->stationName << "\n";
// cout << this->distributor << "\n";
}
PlcLicence::PlcLicence(map<string, string> &arguments)
@@ -117,21 +116,6 @@ PlcLicence::PlcLicence(map<string, string> &arguments)
<< "CID 1:" << cid << " delka: " << strlen(cid) << "\n";
}
// this->cid = "9f544930303030300000000b47015423";
// this->csd = "400e00325b5900003a0d7f800a40008d";
cout << "\n"
<< "CID 2:"
<< " delka: " << strlen(cid) << "\n";
// if (arguments.count("-csd") > 0)
// {
// const int csdLength = arguments["-csd"].length();
// char csdArg[csdLength] = {};
// getCharsFromString(arguments["-csd"], csdArg);
// this->csd = csdArg;
// }
this->licType = 2;
if (arguments.count("-outputType") > 0)
@@ -143,7 +127,6 @@ PlcLicence::PlcLicence(map<string, string> &arguments)
}
else
this->binaryGeneration = BinaryGenerationType::Base64Cout;
}
PlcLicence::PlcLicence(string cid, string csd, char *stationName, char *switchName, char *licenceType, char *binaryType, const char *dataFileName)
@@ -163,16 +146,13 @@ PlcLicence::PlcLicence(string cid, string csd, char *stationName, char *switchNa
}
else
this->binaryGeneration = BinaryGenerationType::Base64Cout;
}
void PlcLicence::getSDData()
{
//int length = strlen(cid); // Get the length of the char array
for (int i = 0; i < CID_LENGTH; i++) this->sdData.CID_nibble[i] = (BYTE)cid[i];
// int length = strlen(cid); // Get the length of the char array
for (int i = 0; i < CID_LENGTH; i++)
this->sdData.CID_nibble[i] = (BYTE)cid[i];
for (int i = 0; i < cnibblescount / 2; i++)
{
@@ -204,7 +184,8 @@ void PlcLicence::getSDData()
this->sdData.manufacturerDate[i] = date[i];
// CSD
for (int i = 0; i < CSD_LENGTH; i++) this->sdData.CSD_nibble[i] = (BYTE)csd[i];
for (int i = 0; i < CSD_LENGTH; i++)
this->sdData.CSD_nibble[i] = (BYTE)csd[i];
for (int i = 0; i < cnibblescount / 2; i++)
{
@@ -239,8 +220,6 @@ void PlcLicence::getSDData()
this->sdData.CRCOK = ((sdCrc << 1) | 1) == this->sdData.CID[15];
}
PlcLicence::PlcLicence(char *cid, char *csd, string binaryType, string dataFileName)
{
this->cid = cid;
@@ -251,7 +230,7 @@ PlcLicence::PlcLicence(char *cid, char *csd, string binaryType, string dataFileN
this->licType = 2;
processConfigFile(dataFileName);
if (binaryType == "base64")
this->binaryGeneration = BinaryGenerationType::Base64Cout;
else
@@ -277,8 +256,6 @@ PlcLicence::PlcLicence(char *cid, char *csd, char *stationName, char *switchName
this->binaryGeneration = BinaryGenerationType::Base64Cout;
}
void PlcLicence::initCrypto()
{
if (this->licType == cEzLic_p78ou3_IDType_EOVOSV)
@@ -307,165 +284,6 @@ void PlcLicence::initCrypto()
}
}
unsigned char *PlcLicence::getLicenceContent()
{
getSDData();
initCrypto();
// promenne pro praci se soubory a adresari
DWORD sdFileHandle;
DWORD sdDirHandle;
string dirFileName;
string dirFilePath; //: STRING;
string dirInfo; //: DIRECTORY_INFO; - NAJIT
LicenceDataMainELC dataMainToFileELC1; //: tEz_LicenceDataMainELC1; hlavicka urcena pro zapis do souboru - typ ELC1
LicenceData dataToFile; // tEz_LicenceData_10_10;// data urcena pro sifrovani a zapis do soubotu - verze => hlavicka: 1.0, data: 1.0
// pomocne promenne
BYTE dataWriteToFile[cEzLic_p78ou3_maxDataToFile]; // OF BYTE; data zapisovana do souboru
DWORD retDataCount; //: DWORD; // pocty zapisovanych a ctenych dat - navratova hodnota jednotlivych funkci
string licencePostfix; //: STRING; pomocna promenna pro kontrolu postfixu
string mainLicDescription; //: STRING(79); globalni pojmenovani licence v nesifrovane casti licence
UINT freeDataSize; //: UINT; velikost volne citelnych dat v souboru licenci
UINT cryptDataSize; //: UINT; velikost volne cryptovanych dat v souboru licenci
UINT fullDataSize; //: UINT; celkova velikost souboru licenci
USINT generation = 2; //: USINT; verze SW pro licence
PlcData plcData; // = GetPlcData(dataFileName);// "licData.xml");
plcData.licenceName = "Licence";
plcData.licenceType = "1";
plcData.station = stationName; // "Test";
plcData.distributor = distributor; //"RO1";
LicenceSourceData licSourceData;
if (plcData.licenceType == "EOV" || plcData.licenceType == to_string((int)LicenceType::LicenceEov))
{
licSourceData.licType = 0x1AA2; // 6818 ... proč ?
licSourceData.licPostfix = 0;
getCharsFromString(plcData.station, licSourceData.licDescription1);
getCharsFromString(plcData.station, dataToFile.header.licDescription1);
getCharsFromString(plcData.distributor, licSourceData.licDescription2);
getCharsFromString(plcData.distributor, dataToFile.header.licDescription2);
}
LicenceIdent licIdent;
licIdent.licPrefixType = licSourceData.licType >> 12;
licIdent.licHeaderType = licSourceData.licType >> 8 & 0x000F;
licIdent.licDataType = licSourceData.licType >> 4 & 0x000F;
licIdent.licSubType = licIdent.licHeaderType << 8 | licIdent.licHeaderType;
licIdent.licIDType = licSourceData.licType & 0x000F;
if (licSourceData.licPostfix > 9)
licSourceData.licPostfix = 0;
licencePostfix = to_string(licSourceData.licPostfix);
string licenseFileName = getLicenceName(0);
licSourceData.licPostfixIdent = right(licencePostfix, 1);
int sizeOfData = sizeof(dataToFile);
if (licIdent.licHeaderType == cEzLic_p78ou3_HeaderType_10)
{
if (licIdent.licDataType == cEzLic_p78ou3_DataType_10)
{
dataToFile.id.version = 10;
dataToFile.id.cardSize = this->sdData.cardSize;
switch (licIdent.licIDType)
{
case cEzLic_p78ou3_IDType_DDTS:
mainLicDescription = "ddts ";
break;
case cEzLic_p78ou3_IDType_EOVOSV:
mainLicDescription = "eov, osv ";
break;
default:
mainLicDescription = "neznama app ";
}
mainLicDescription += licSourceData.licPostfixIdent;
mainLicDescription += " => ";
dataToFile.header.licVersion = 10;
dataToFile.header.licType = licIdent.licIDType;
dataToFile.header.licDate = getLicDate();
mainLicDescription += dataToFile.header.licDescription1;
mainLicDescription += " [";
mainLicDescription += dataToFile.header.licDescription2;
mainLicDescription += "]";
if (licIdent.licIDType == cEzLic_p78ou3_IDType_EOVOSV)
{
if (generation == 1) // stará verze
{
dataToFile.header.licCount = 1;
dataToFile.items[0].licCount = 65535;
dataToFile.items[0].protoId = cEzLic_p78ou3_CheckLicNumberId_EOV1;
dataToFile.items[0].data1 = cEzLic_p78ou3_CheckLicNumber_EOV1;
}
else
{
dataToFile.header.licCount = 1;
dataToFile.items[1].licCount = 65535;
dataToFile.items[1].protoId = cEzLic_p78ou3_CheckLicNumberId_EOV1;
dataToFile.items[1].data1 = cEzLic_p78ou3_CheckLicNumber_EOV1;
}
}
unsigned char encrypted[10000] = {};
const int s = sizeof(dataToFile);
unsigned char byteArray[s] = {};
memcpy(byteArray, &dataToFile, sizeof(dataToFile));
dataToFile.crc = calculateCRC16(byteArray, s - sizeof(dataToFile.crc)); // 47535 | 884:38382
memcpy(byteArray, &dataToFile, sizeof(dataToFile));
int finalEncryptedLength = encrypt(byteArray, sizeof(dataToFile), this->cryptData.aesKey, this->cryptData.aesInitVector, encrypted);
if (licIdent.licPrefixType == cEzLic_p78ou3_LicPrefixType_ELC1) // typ1 = pouze zasifrovani dat
{
freeDataSize = sizeof(dataMainToFileELC1); // melo by byt 92
if (1) // EzLic_p78ou3_CryptoData.aesRetVal > 0 crypto
{
dataMainToFileELC1.prefix = 0x31434C45;
getCharsFromString(mainLicDescription, dataMainToFileELC1.licHeader.description);
dataMainToFileELC1.licHeader.sizeData = finalEncryptedLength; // sizeof(dataToFile); // 884;
dataMainToFileELC1.licHeader.licType = licIdent.licIDType;
dataMainToFileELC1.licHeader.licSubType = licIdent.licHeaderType << 8 | licIdent.licDataType;
}
}
const int dataMainLength = sizeof(dataMainToFileELC1);
const int dataToFileLength = sizeof(dataToFile);
const int dataEncryptedLength = finalEncryptedLength;
const int totalLength = dataMainLength + dataToFileLength;
const int totalEncryptedLength = dataMainLength + finalEncryptedLength;
unsigned char bdataMainToFileELC1[dataMainLength] = {};
memcpy(bdataMainToFileELC1, &dataMainToFileELC1, dataMainLength);
unsigned char bdataToFile[dataToFileLength] = {};
memcpy(bdataToFile, &dataToFile, dataToFileLength);
unsigned char totalEncryptedArray[totalEncryptedLength] = {};
for (int i = 0; i < dataMainLength; i++)
totalEncryptedArray[i] = bdataMainToFileELC1[i];
for (int i = 0; i < finalEncryptedLength; i++)
totalEncryptedArray[i + dataMainLength] = encrypted[i];
return totalEncryptedArray;
}
}
return nullptr;
}
string PlcLicence::getLicenceName(BYTE licPostfix)
{
string result = "";
@@ -603,7 +421,7 @@ bool PlcLicence::CreateLicence()
; // = GetPlcData(dataFileName);// "licData.xml");
plcData.licenceName = "Licence";
plcData.licenceType = "1";
plcData.station = stationName; // "Test";
plcData.station = stationName; // "Test";
plcData.distributor = distributor; //"RO1";
LicenceSourceData licSourceData;
@@ -668,7 +486,7 @@ bool PlcLicence::CreateLicence()
dataToFile.header.licVersion = 10;
dataToFile.header.licType = licIdent.licIDType;
dataToFile.header.licDate = getLicDate(); //1692230400; //getLicDate(); //1692144000;// getLicDate();
dataToFile.header.licDate = getLicDate(); // 1692230400; //getLicDate(); //1692144000;// getLicDate();
mainLicDescription += dataToFile.header.licDescription1;
mainLicDescription += " [";
mainLicDescription += dataToFile.header.licDescription2;
@@ -726,8 +544,6 @@ bool PlcLicence::CreateLicence()
unsigned char bdataMainToFileELC1[dataMainLength] = {};
memcpy(bdataMainToFileELC1, &dataMainToFileELC1, dataMainLength);
unsigned char bdataToFile[dataToFileLength] = {};
memcpy(bdataToFile, &dataToFile, dataToFileLength);
unsigned char totalEncryptedArray[totalEncryptedLength] = {};
@@ -737,29 +553,41 @@ bool PlcLicence::CreateLicence()
for (int i = 0; i < finalEncryptedLength; i++)
totalEncryptedArray[i + dataMainLength] = encrypted[i];
// char chFileName[licenseFileName.length()];
char licFileNameToSave[licenseFileName.length()] = {};
getCharsFromString(licenseFileName, licFileNameToSave);
int totalFileSize = sizeof(totalEncryptedArray);
// for (int i=0;i<totalEncryptedLength;i++) cout << totalEncryptedArray[i];
string fullFile = getCompletePath(licenseFileName);
if (binaryGeneration == BinaryGenerationType::File)
{
FILE *fileLicence = fopen(licFileNameToSave, "wb");
if (fileLicence)
std::ofstream outputFile(fullFile);
if (outputFile.is_open())
{
size_t r1 = fwrite(&totalEncryptedArray, sizeof(totalEncryptedArray), 1, fileLicence);
//printf("License binary saved.\n");
fclose(fileLicence);
cout << licFileNameToSave << endl;
return true;
outputFile.write(reinterpret_cast<const char *>(totalEncryptedArray), totalEncryptedLength);
outputFile.close();
std::cout << licenseFileName << std::endl;
}
else
{
std::cerr << "Unable to open the file." << std::endl;
}
return true;
// FILE *fileLicence = fopen(licFileNameToSave, "wb");
// if (fileLicence)
// {
// size_t r1 = fwrite(&totalEncryptedArray, sizeof(totalEncryptedArray), 1, fileLicence);
// // printf("License binary saved.\n");
// fclose(fileLicence);
// cout << licenseFileName << endl;
// return true;
// }
}
else
{
//cout << "data:text/plain;base64,";
int totalFileSize = sizeof(totalEncryptedArray);
// cout << "data:text/plain;base64,";
cout << "data:application/octet-stream;base64,";
char encryptedChars[totalFileSize];
for (int i = 0; i < totalFileSize; i++)