code cleaning

This commit is contained in:
2023-08-29 15:15:06 +02:00
parent 865493c984
commit 15de81ddf2
3 changed files with 84 additions and 244 deletions

View File

@@ -42,9 +42,9 @@ enum PrefixType
ELC1 = 1
};
typedef uint32_t DWORD; // DWORD = unsigned 32 bit value
typedef uint16_t WORD; // WORD = unsigned 16 bit value
typedef uint8_t BYTE; // BYTE = unsigned 8 bit value
typedef uint32_t DWORD;
typedef uint16_t WORD;
typedef uint8_t BYTE;
typedef uint32_t UDINT;
typedef uint32_t DATE;
typedef uint8_t USINT;
@@ -227,20 +227,16 @@ private:
void processConfigFile(string &dataFileName); //process data from config file
void getSDData(); // reads SD card
string getLicenceName(BYTE licPostfix); // get proper licencname
void initCrypto(); // inits keys and vectors for encryptin
unsigned char *getLicenceContent(); // reads content of licence
string getLicenceName(BYTE licPostfix); // get proper licencename
void initCrypto(); // inits keys and vectors for encrypting
public:
string operationErrors = "";
PlcLicence();
PlcLicence(char *cid, char *csd, char *stationName, char *switchName, char *licenceType, char *binaryType, const char *dataFileName);
PlcLicence(string cid, string csd, char *stationName, char *switchName, char *licenceType, char *binaryType, const char *dataFileName);
PlcLicence(map<string, string> & arguments);
PlcLicence(char *cid, char *csd, string binaryType, string dataFileName);
bool CreateLicence(); // creates licence
bool CreateLicence(); // creates licence
bool ReadLicence(const char *dataFileName, WORD licType, BYTE licPostfix, char *cid, char *csd); // reads licence file to readable structures
};

View File

@@ -31,5 +31,11 @@ int main(int argc, char *argv[])
cerr << "Licence creation failed";
}
// PlcLicence plcLicence = PlcLicence();
// if (plcLicence.ReadLicence("ezlic_eovosv0_vaxvcpalxjx.lic", LidIdType::EovOsv, 0, cidArg, csdArg) == false)
// {
// cout << "Reading error: " << plcLicence.operationErrors << endl;
// }
return EXIT_SUCCESS;
}

View File

@@ -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
@@ -55,29 +55,21 @@ 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 +100,36 @@ 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();
string licType = doc.child("data").child("licenceType").child_value();
string licVersion = doc.child("data").child("licenceType").attribute("version").value();
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 +199,8 @@ 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 +229,9 @@ void PlcLicence::initCrypto()
}
}
/// @brief get proper licencename
/// @param licPostfix
/// @return
string PlcLicence::getLicenceName(BYTE licPostfix)
{
string result = "";
@@ -275,118 +244,49 @@ 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)
{
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)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
int count = 0;
cout << "fileSize:" << fileSize << "\n";
char fileContent[3000];
for (int i = 0; i < fileSize; i++)
{
char c = fgetc(fp);
fileContent[i] = c;
cout << c;
// printf("%c", c);;
count++;
}
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()
{
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
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
@@ -397,24 +297,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 +421,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 +438,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 +454,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; }
}
}
}
@@ -627,9 +469,9 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
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)
@@ -667,10 +509,7 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
const int sizeOfEncryptedData = size - sizeof(LicenceDataMainELC);
unsigned char encryptedData[sizeOfEncryptedData] = {};
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
for (int i = 0; i < sizeOfEncryptedData; i++) encryptedData[i] = licenceContent[i + sizeof(LicenceDataMainELC)];
BYTE prefixType = (int)licenceContent[3] - 0x30;
if (prefixType == PrefixType::ELC1)
@@ -683,7 +522,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 +555,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 +597,6 @@ 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;
}