|
|
|
|
@@ -12,9 +12,9 @@ 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/";
|
|
|
|
|
const string cEzLic_p78ou3_licencfilepath_f10 = "/root/ez_sys/licence/";
|
|
|
|
|
const WORD cEzLic_p78ou3_maxDataToFile = 10000; // velikost datoveho bufferu pro ulozeni dat licence
|
|
|
|
|
const WORD maxDataToFile = 10000; // velikost datoveho bufferu pro ulozeni dat licence
|
|
|
|
|
|
|
|
|
|
const BYTE cEzLic_p78ou3_licMaxCount = 100;
|
|
|
|
|
const BYTE licMaxCount = 100;
|
|
|
|
|
const DWORD cEzLic_p78ou3_CheckLicNumber_ERR = 0xFFFFFFFF; // 16#FFFFFFFF; // chybna identifikace licence
|
|
|
|
|
const DWORD cEzLic_p78ou3_CheckLicNumber_EOV1 = 3781234965; // cislo pro overeni licence EOV, OSV verze 1
|
|
|
|
|
const BYTE cEzLic_p78ou3_CheckLicNumberId_ERR = 0; // id licence pro neidentifikovanou licenci
|
|
|
|
|
@@ -51,33 +51,27 @@ 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
|
|
|
|
|
|
|
|
|
|
const string eoseovLicenceType = "EOV_OSV";
|
|
|
|
|
|
|
|
|
|
PlcLicence::PlcLicence()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief process config file
|
|
|
|
|
/// @param dataFileName
|
|
|
|
|
void PlcLicence::processConfigFile(string &dataFileName)
|
|
|
|
|
PlcLicence::PlcLicence(char *cid, char *csd, string binaryType, string dataFileName)
|
|
|
|
|
{
|
|
|
|
|
string fullFile = getCompletePath(dataFileName);
|
|
|
|
|
const int fileNameLength = fullFile.length();
|
|
|
|
|
char fileName[fileNameLength] = {};
|
|
|
|
|
getCharsFromString(fullFile, fileName, fileNameLength);
|
|
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
pugi::xml_parse_result result = doc.load_file(fileName);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
this->stationName = doc.child("data").child("station").child_value();
|
|
|
|
|
this->distributor = doc.child("data").child("distributor").child_value();
|
|
|
|
|
;
|
|
|
|
|
// cout << doc.child("data").child("plcType").attribute("t").value() << endl;
|
|
|
|
|
// cout << doc.child("data").child("plcType").child_value() << endl;
|
|
|
|
|
}
|
|
|
|
|
this->cid = cid;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
|
|
|
|
|
this->stationName = "";
|
|
|
|
|
this->distributor = "";
|
|
|
|
|
this->licType = 2;
|
|
|
|
|
|
|
|
|
|
processConfigFile(dataFileName);
|
|
|
|
|
|
|
|
|
|
if (binaryType == "base64")
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Unable to open the file." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::File;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlcLicence::PlcLicence(map<string, string> &arguments)
|
|
|
|
|
@@ -108,28 +102,37 @@ PlcLicence::PlcLicence(map<string, string> &arguments)
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlcLicence::PlcLicence(string cid, string csd, char *stationName, char *switchName, char *licenceType, char *binaryType, const char *dataFileName)
|
|
|
|
|
/// @brief process config file
|
|
|
|
|
/// @param dataFileName
|
|
|
|
|
void PlcLicence::processConfigFile(string &dataFileName)
|
|
|
|
|
{
|
|
|
|
|
this->cidString = cid;
|
|
|
|
|
this->csdString = csd;
|
|
|
|
|
this->stationName = stationName;
|
|
|
|
|
this->distributor = switchName;
|
|
|
|
|
this->dataFileName = dataFileName;
|
|
|
|
|
this->licType = 2;
|
|
|
|
|
if (binaryType != nullptr)
|
|
|
|
|
string fullFile = getCompletePath(dataFileName);
|
|
|
|
|
const int fileNameLength = fullFile.length();
|
|
|
|
|
char fileName[fileNameLength] = {};
|
|
|
|
|
getCharsFromString(fullFile, fileName, fileNameLength);
|
|
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
pugi::xml_parse_result result = doc.load_file(fileName);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
if (binaryType[0] == '1')
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
else
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::File;
|
|
|
|
|
this->stationName = doc.child("data").child("station").child_value();
|
|
|
|
|
this->distributor = doc.child("data").child("distributor").child_value();
|
|
|
|
|
this->dataLicenceType = doc.child("data").child("licenceType").child_value();
|
|
|
|
|
this->dataLicenceVersion = doc.child("data").child("licenceType").attribute("version").value();
|
|
|
|
|
this->dataGenerationType = this->dataLicenceVersion;
|
|
|
|
|
this->dataCryptoVersion = this->dataLicenceVersion;
|
|
|
|
|
// if (licType == "EOV_OSV") this->licType = cEzLic_p78ou3_IDType_EOVOSV;// 1;
|
|
|
|
|
// cout << doc.child("data").child("plcType").attribute("t").value() << endl; //přiklad pro atributy
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Unable to open the file." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief reads data from CID a CSD
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
@@ -199,42 +202,7 @@ 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;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
|
|
|
|
|
this->stationName = "";
|
|
|
|
|
this->distributor = "";
|
|
|
|
|
this->licType = 2;
|
|
|
|
|
|
|
|
|
|
processConfigFile(dataFileName);
|
|
|
|
|
|
|
|
|
|
if (binaryType == "base64")
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
else
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::File;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlcLicence::PlcLicence(char *cid, char *csd, char *stationName, char *switchName, char *licenceType, char *binaryType, const char *dataFileName)
|
|
|
|
|
{
|
|
|
|
|
this->cid = cid;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
this->stationName = stationName;
|
|
|
|
|
this->distributor = switchName;
|
|
|
|
|
this->dataFileName = dataFileName;
|
|
|
|
|
this->licType = 2;
|
|
|
|
|
if (binaryType != nullptr)
|
|
|
|
|
{
|
|
|
|
|
if (binaryType[0] == '1')
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
else
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::File;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
this->binaryGeneration = BinaryGenerationType::Base64Cout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief inits keys and vectors for encrypting
|
|
|
|
|
void PlcLicence::initCrypto()
|
|
|
|
|
{
|
|
|
|
|
if (this->licType == cEzLic_p78ou3_IDType_EOVOSV)
|
|
|
|
|
@@ -263,6 +231,9 @@ void PlcLicence::initCrypto()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief get proper licencename
|
|
|
|
|
/// @param licPostfix
|
|
|
|
|
/// @return
|
|
|
|
|
string PlcLicence::getLicenceName(BYTE licPostfix)
|
|
|
|
|
{
|
|
|
|
|
string result = "";
|
|
|
|
|
@@ -275,120 +246,63 @@ string PlcLicence::getLicenceName(BYTE licPostfix)
|
|
|
|
|
if (licType == LicenceType::LicenceOther)
|
|
|
|
|
{
|
|
|
|
|
result = "ezlic_";
|
|
|
|
|
result += prefixChar + (this->sdData.CID[12] % 25); // 14 OK "v"
|
|
|
|
|
result += prefixChar + (this->sdData.CID[10] % 25); // 15 OK "a"
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[22] % 25); // 16 OK "x"
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[23] * 2) % 25); // 17 OK "v"
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[24] % 25); // 18 not ok ... má být c = 99
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[25] * 3) % 25); // 19 not ok a 112
|
|
|
|
|
result += prefixChar + (this->sdData.CID[9] % 25); // 20 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[11] % 25); // 21 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[2] % 25); // 22 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[1] % 25); // 23 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[3] % 25); // 24 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[12] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[10] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[22] % 25);
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[23] * 2) % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[24] % 25);
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[25] * 3) % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[9] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[11] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[2] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[1] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[3] % 25);
|
|
|
|
|
result += ".lic";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = "ezlic_eovosv" + to_string(licPostfix) + "_";
|
|
|
|
|
result += prefixChar + (this->sdData.CID[12] % 25); // 14 OK "v"
|
|
|
|
|
result += prefixChar + (this->sdData.CID[10] % 25); // 15 OK "a"
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[22] % 25); // 16 OK "x"
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[23] * 2) % 25); // 17 OK "v"
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[24] % 25); // 18 not ok ... má být c = 99
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[25] * 3) % 25); // 19 not ok a 112
|
|
|
|
|
result += prefixChar + (this->sdData.CID[9] % 25); // 20 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[11] % 25); // 21 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[2] % 25); // 22 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[1] % 25); // 23 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[3] % 25); // 24 OK
|
|
|
|
|
result += prefixChar + (this->sdData.CID[12] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[10] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[22] % 25);
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[23] * 2) % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID_nibble[24] % 25);
|
|
|
|
|
result += prefixChar + ((this->sdData.CID_nibble[25] * 3) % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[9] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[11] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[2] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[1] % 25);
|
|
|
|
|
result += prefixChar + (this->sdData.CID[3] % 25);
|
|
|
|
|
result += ".lic";
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct PlcData GetPlcData(const char *dataFileName)
|
|
|
|
|
bool PlcLicence::CreateLicence()
|
|
|
|
|
{
|
|
|
|
|
PlcData result;
|
|
|
|
|
result.plcType = "WAGO";
|
|
|
|
|
result.licenceName = "Licence 1";
|
|
|
|
|
result.licenceType = "";
|
|
|
|
|
result.licenceVersion = "1";
|
|
|
|
|
result.station = "";
|
|
|
|
|
result.distributor = "";
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
fp = fopen("licData.xml", "r"); // read mode
|
|
|
|
|
fseek(fp, 0, SEEK_END); // seek to end of file
|
|
|
|
|
int fileSize = ftell(fp); // get current file pointer
|
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
if (this->dataLicenceType == eoseovLicenceType)
|
|
|
|
|
{
|
|
|
|
|
perror("Error while opening the file.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
return CreateEosEovLicence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
cout << "fileSize:" << fileSize << "\n";
|
|
|
|
|
|
|
|
|
|
char fileContent[3000];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fileSize; i++)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char c = fgetc(fp);
|
|
|
|
|
fileContent[i] = c;
|
|
|
|
|
cout << c;
|
|
|
|
|
// printf("%c", c);;
|
|
|
|
|
count++;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
std::string fileContentStr(fileContent, sizeof(fileContent));
|
|
|
|
|
|
|
|
|
|
string typeContentString = getXmlContentFromString(fileContentStr, "type");
|
|
|
|
|
string nameContentString = getXmlContentFromString(fileContentStr, "name");
|
|
|
|
|
string stationContentString = getXmlContentFromString(fileContentStr, "station");
|
|
|
|
|
string switchContentString = getXmlContentFromString(fileContentStr, "switchboard");
|
|
|
|
|
|
|
|
|
|
cout << typeContentString << "\n";
|
|
|
|
|
cout << nameContentString << "\n";
|
|
|
|
|
cout << stationContentString << "\n";
|
|
|
|
|
cout << switchContentString << "\n";
|
|
|
|
|
|
|
|
|
|
if (typeContentString != "")
|
|
|
|
|
result.licenceType = typeContentString;
|
|
|
|
|
if (nameContentString != "")
|
|
|
|
|
result.licenceName = nameContentString;
|
|
|
|
|
if (stationContentString != "")
|
|
|
|
|
result.station = stationContentString;
|
|
|
|
|
if (switchContentString != "")
|
|
|
|
|
result.distributor = switchContentString;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PlcLicence::CreateLicence()
|
|
|
|
|
bool PlcLicence::CreateEosEovLicence()
|
|
|
|
|
{
|
|
|
|
|
getSDData();
|
|
|
|
|
initCrypto();
|
|
|
|
|
|
|
|
|
|
// promenne pro praci se soubory a adresari
|
|
|
|
|
DWORD sdFileHandle;
|
|
|
|
|
DWORD sdDirHandle;
|
|
|
|
|
string dirFileName;
|
|
|
|
|
string dirFilePath;
|
|
|
|
|
string dirInfo;
|
|
|
|
|
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
|
|
|
|
|
BYTE dataWriteToFile[maxDataToFile]; // OF BYTE; data zapisovana do souboru
|
|
|
|
|
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
|
|
|
|
|
@@ -397,24 +311,13 @@ bool PlcLicence::CreateLicence()
|
|
|
|
|
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";
|
|
|
|
|
plcData.station = stationName;
|
|
|
|
|
plcData.distributor = distributor;
|
|
|
|
|
|
|
|
|
|
LicenceSourceData licSourceData;
|
|
|
|
|
|
|
|
|
|
// cout << "size of WORD:" << sizeof(WORD) << endl;
|
|
|
|
|
// cout << "size of DWORD:" << sizeof(DWORD) << endl;
|
|
|
|
|
// cout << "size of DATE:" << sizeof(DATE) << endl;
|
|
|
|
|
// cout << "size of BYTE:" << sizeof(BYTE) << endl;
|
|
|
|
|
// cout << "size of int:" << sizeof(int) << endl;
|
|
|
|
|
// cout << "size of char:" << sizeof(char) << endl;
|
|
|
|
|
// cout << "size of float:" << sizeof(float) << endl;
|
|
|
|
|
// cout << "Size of dataToFile: " << sizeof(dataToFile) << endl;
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
|
|
if (plcData.licenceType == "EOV" || plcData.licenceType == to_string((int)LicenceType::LicenceEov))
|
|
|
|
|
{
|
|
|
|
|
licSourceData.licType = 0x1AA2; // 6818 ... proč ?
|
|
|
|
|
@@ -532,8 +435,6 @@ bool PlcLicence::CreateLicence()
|
|
|
|
|
for (int i = 0; i < finalEncryptedLength; i++)
|
|
|
|
|
totalEncryptedArray[i + dataMainLength] = encrypted[i];
|
|
|
|
|
|
|
|
|
|
// for (int i=0;i<totalEncryptedLength;i++) cout << totalEncryptedArray[i];
|
|
|
|
|
|
|
|
|
|
string fullFile = getCompletePath(licenseFileName);
|
|
|
|
|
|
|
|
|
|
if (binaryGeneration == BinaryGenerationType::File)
|
|
|
|
|
@@ -551,15 +452,6 @@ bool PlcLicence::CreateLicence()
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
@@ -576,43 +468,7 @@ bool PlcLicence::CreateLicence()
|
|
|
|
|
string base64Coded = base64_encode_ai(strToBase);
|
|
|
|
|
cout << base64Coded << endl;
|
|
|
|
|
|
|
|
|
|
// string decodedBase64 = base64_decode_ai(base64Coded);
|
|
|
|
|
// std::ofstream outputFile("outputLicence.txt", std::ios::out | std::ios::binary);
|
|
|
|
|
// if (outputFile.is_open())
|
|
|
|
|
// {
|
|
|
|
|
// outputFile << decodedBase64;
|
|
|
|
|
// outputFile.close();
|
|
|
|
|
// std::cout << "File written successfully." << std::endl;
|
|
|
|
|
// }
|
|
|
|
|
// else { std::cerr << "Unable to open the file." << std::endl; }
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// //file testing
|
|
|
|
|
// std::string filename = "source.txt ";
|
|
|
|
|
// std::string content;
|
|
|
|
|
// std::ifstream inputFile(filename);
|
|
|
|
|
// 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;
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// std::cerr << "Unable to open the file." << std::endl;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// std::ofstream outputFile("output.txt");
|
|
|
|
|
|
|
|
|
|
// if (outputFile.is_open())
|
|
|
|
|
// {
|
|
|
|
|
// string codedBase = base64_decode_ai(content);
|
|
|
|
|
// outputFile << "Velikost souboru orig souboru: " << codedBase.length() << "\n" << codedBase;
|
|
|
|
|
// outputFile.close();
|
|
|
|
|
// std::cout << "File written successfully." << std::endl;
|
|
|
|
|
// }
|
|
|
|
|
// else { std::cerr << "Unable to open the file." << std::endl; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -620,16 +476,32 @@ bool PlcLicence::CreateLicence()
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPostfix, char *cid, char *csd)
|
|
|
|
|
bool PlcLicence::ReadLicence(const char *dataFileName, string licenceType, string licenceVersion, char *cid, char *csd)
|
|
|
|
|
{
|
|
|
|
|
this->cid = cid;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
this->dataLicenceType = licenceType;
|
|
|
|
|
this->dataLicenceVersion = licenceVersion;
|
|
|
|
|
|
|
|
|
|
if (this->dataLicenceType == eoseovLicenceType)
|
|
|
|
|
{
|
|
|
|
|
return ReadEosEovLicence(dataFileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PlcLicence::ReadEosEovLicence(const char *dataFileName)
|
|
|
|
|
{
|
|
|
|
|
BYTE licPostfix = 0;
|
|
|
|
|
FILE *licenceFile;
|
|
|
|
|
char ch;
|
|
|
|
|
long lSize;
|
|
|
|
|
size_t result;
|
|
|
|
|
|
|
|
|
|
licenceFile = fopen(dataFileName, "rb"); // read mode
|
|
|
|
|
fseek(licenceFile, 0, SEEK_END); // seek to end of file
|
|
|
|
|
const int size = ftell(licenceFile); // get current file pointer
|
|
|
|
|
licenceFile = fopen(dataFileName, "rb");
|
|
|
|
|
fseek(licenceFile, 0, SEEK_END);
|
|
|
|
|
const int size = ftell(licenceFile);
|
|
|
|
|
fseek(licenceFile, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
if (licenceFile == NULL)
|
|
|
|
|
@@ -651,9 +523,6 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
|
|
|
|
|
|
|
|
|
|
fclose(licenceFile);
|
|
|
|
|
|
|
|
|
|
this->cid = cid;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
|
|
|
|
|
getSDData();
|
|
|
|
|
|
|
|
|
|
string licFileName = getLicenceName(licPostfix);
|
|
|
|
|
@@ -670,8 +539,6 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
|
|
|
|
|
for (int i = 0; i < sizeOfEncryptedData; i++)
|
|
|
|
|
encryptedData[i] = licenceContent[i + sizeof(LicenceDataMainELC)];
|
|
|
|
|
|
|
|
|
|
// original jsou testy na velikost přečteného file, zatím TODO
|
|
|
|
|
|
|
|
|
|
BYTE prefixType = (int)licenceContent[3] - 0x30;
|
|
|
|
|
if (prefixType == PrefixType::ELC1)
|
|
|
|
|
{
|
|
|
|
|
@@ -683,7 +550,6 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
|
|
|
|
|
if (licenceHeader.licHeader.licSubType == cEzLic_p78ou3_SubType_10_10) // zatim natvrdo
|
|
|
|
|
{
|
|
|
|
|
initCrypto();
|
|
|
|
|
// CryptData cryptData = initCrypto(sdData, licIdent.licIDType);
|
|
|
|
|
unsigned char decrypted[2000] = {};
|
|
|
|
|
int decrypted_len = decrypt(encryptedData, sizeof(encryptedData), cryptData.aesKey, cryptData.aesInitVector, decrypted);
|
|
|
|
|
|
|
|
|
|
@@ -717,7 +583,7 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// maxLic := MIN(EzLlic_5rq4_DataFromFile_10_10.header.licCount, cEzLic_p78ou3_licMaxCount); ?
|
|
|
|
|
int maxLic = min(licEncryptedData.header.licCount, cEzLic_p78ou3_licMaxCount);
|
|
|
|
|
int maxLic = min(licEncryptedData.header.licCount, licMaxCount);
|
|
|
|
|
for (int i = 1; i < maxLic; i++)
|
|
|
|
|
{
|
|
|
|
|
if (licEncryptedData.items[i].protoId > 0 || licEncryptedData.items[i].licCount > 0 || licEncryptedData.items[i].data1 > 0)
|
|
|
|
|
@@ -759,6 +625,147 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout << "Licence readed: " << size << "\n";
|
|
|
|
|
cout << "Licence readed: " << size << endl << ", data: " << licenceHeader.licHeader.description << endl;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPostfix, char *cid, char *csd)
|
|
|
|
|
{
|
|
|
|
|
FILE *licenceFile;
|
|
|
|
|
char ch;
|
|
|
|
|
long lSize;
|
|
|
|
|
size_t result;
|
|
|
|
|
|
|
|
|
|
licenceFile = fopen(dataFileName, "rb");
|
|
|
|
|
fseek(licenceFile, 0, SEEK_END);
|
|
|
|
|
const int size = ftell(licenceFile);
|
|
|
|
|
fseek(licenceFile, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
if (licenceFile == NULL)
|
|
|
|
|
{
|
|
|
|
|
perror("Error while opening the file.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
unsigned char licenceContent[size];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
ch = fgetc(licenceFile);
|
|
|
|
|
licenceContent[i] = ch;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(licenceFile);
|
|
|
|
|
|
|
|
|
|
this->cid = cid;
|
|
|
|
|
this->csd = csd;
|
|
|
|
|
|
|
|
|
|
getSDData();
|
|
|
|
|
|
|
|
|
|
string licFileName = getLicenceName(licPostfix);
|
|
|
|
|
|
|
|
|
|
LicenceDataMainELC licenceHeader;
|
|
|
|
|
LicenceIdent licIdent;
|
|
|
|
|
LicenceData licEncryptedData;
|
|
|
|
|
|
|
|
|
|
memset(&licenceHeader, 0, sizeof(LicenceDataMainELC));
|
|
|
|
|
memcpy(&licenceHeader, licenceContent, sizeof(LicenceDataMainELC));
|
|
|
|
|
|
|
|
|
|
const int sizeOfEncryptedData = size - sizeof(LicenceDataMainELC);
|
|
|
|
|
unsigned char encryptedData[sizeOfEncryptedData] = {};
|
|
|
|
|
for (int i = 0; i < sizeOfEncryptedData; i++)
|
|
|
|
|
encryptedData[i] = licenceContent[i + sizeof(LicenceDataMainELC)];
|
|
|
|
|
|
|
|
|
|
BYTE prefixType = (int)licenceContent[3] - 0x30;
|
|
|
|
|
if (prefixType == PrefixType::ELC1)
|
|
|
|
|
{
|
|
|
|
|
if (licenceHeader.licHeader.sizeData > 0)
|
|
|
|
|
{
|
|
|
|
|
licIdent.licIDType = licenceHeader.licHeader.licType;
|
|
|
|
|
licIdent.licSubType = licenceHeader.licHeader.licSubType;
|
|
|
|
|
|
|
|
|
|
if (licenceHeader.licHeader.licSubType == cEzLic_p78ou3_SubType_10_10) // zatim natvrdo
|
|
|
|
|
{
|
|
|
|
|
initCrypto();
|
|
|
|
|
unsigned char decrypted[2000] = {};
|
|
|
|
|
int decrypted_len = decrypt(encryptedData, sizeof(encryptedData), cryptData.aesKey, cryptData.aesInitVector, decrypted);
|
|
|
|
|
|
|
|
|
|
if (sizeof(licEncryptedData) != decrypted_len)
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "License size mismatch";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memset(&licEncryptedData, 0, sizeof(licEncryptedData));
|
|
|
|
|
memcpy(&licEncryptedData, decrypted, sizeof(licEncryptedData));
|
|
|
|
|
|
|
|
|
|
if (licEncryptedData.id.version == cEzLic_p78ou3_HeaderType_10 && licEncryptedData.header.licVersion == cEzLic_p78ou3_HeaderType_10)
|
|
|
|
|
{
|
|
|
|
|
if (licEncryptedData.header.licType == cEzLic_p78ou3_IDType_EOVOSV)
|
|
|
|
|
{
|
|
|
|
|
if (licEncryptedData.id.cardSize != sdData.cardSize)
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Size card mismatch";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (licEncryptedData.header.licCount > 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (licType == LicenceType::LicenceOther)
|
|
|
|
|
{
|
|
|
|
|
// TODO stará
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// maxLic := MIN(EzLlic_5rq4_DataFromFile_10_10.header.licCount, cEzLic_p78ou3_licMaxCount); ?
|
|
|
|
|
int maxLic = min(licEncryptedData.header.licCount, licMaxCount);
|
|
|
|
|
for (int i = 1; i < maxLic; i++)
|
|
|
|
|
{
|
|
|
|
|
if (licEncryptedData.items[i].protoId > 0 || licEncryptedData.items[i].licCount > 0 || licEncryptedData.items[i].data1 > 0)
|
|
|
|
|
{
|
|
|
|
|
// EzLlic_5rq4_CheckData.LicId[EzLlic_5rq4_DataFromFile_10_10.items[licId].protoId] := licId;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Licence items mismatch";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Size card info mismatch";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Licence mismatch";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Licence error";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
operationErrors = "Licence error";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout << "Licence readed: " << size << endl
|
|
|
|
|
<< ", data: " << licenceHeader.licHeader.description << endl;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|