string clean

This commit is contained in:
2023-08-30 15:37:57 +02:00
parent 051a13e697
commit 31c23daf5e
3 changed files with 53 additions and 213 deletions

View File

@@ -213,9 +213,9 @@ class PlcLicence
private:
string cidString = "";
char *cid; // CID
char cid[32] = {};
string csdString = "";
char *csd; // CSD
char csd[32] = {}; // CSD
string stationName; // name of station
string distributor; // name of switch
const char *dataFileName; // name of xml containing data (if not taken from commandline)
@@ -236,17 +236,19 @@ private:
string getLicenceName(BYTE licPostfix); // get proper licencename
void initCrypto();
bool CreateEosEovLicence(); // create licence for EOV_OSV
bool ReadEosEovLicence(const char *dataFileName);
bool ReadEosEovLicence(string dataFileName);
public:
string operationErrors = "";
PlcLicence();
PlcLicence(map<string, string> & arguments);
PlcLicence(char *cid, char *csd, string binaryType, string dataFileName);
PlcLicence(string cid, string csd, string binaryType, string dataFileName);
bool CreateLicence(); // creates licence
bool ReadLicence(const char *dataFileName, WORD licType, BYTE licPostfix, char *cid, char *csd); // reads licence file to readable structures
bool ReadLicence(const char *dataFileName, string licenceType, string licenceVersion, char *cid, char *csd);
bool ReadLicence(string dataFileName, string licenceType, string licenceVersion, string cidArg, string csdArg);
// bool CreateEosEovLicence();

View File

@@ -14,32 +14,19 @@
int main(int argc, char *argv[])
{
map<string, string> argumentsString = getArguments(argc, argv);
map<string, string> arguments = getArguments(argc, argv);
const int csdLength = argumentsString["-csd"].length();
char csdArg[32] = {};
getCharsFromString(argumentsString["-csd"], csdArg, csdLength);
const int cidLength = argumentsString["-cid"].length();
char cidArg[32] = {};
getCharsFromString(argumentsString["-cid"], cidArg, cidLength);
PlcLicence plcWriteLicence = PlcLicence(cidArg, csdArg, argumentsString["-outputType"], argumentsString["-configFileName"]);
PlcLicence plcWriteLicence = PlcLicence(arguments["-cid"], arguments["-csd"], arguments["-outputType"], arguments["-configFileName"]);
if (plcWriteLicence.CreateLicence() == false)
{
cerr << "Licence creation failed";
}
/* READ LICENCE CODE
// arguments: -cid, -csd, -licenceFi leName, -licenceType=EOV_OSV, -licenceVersion
PlcLicence plcLicence = PlcLicence();
const int fileNameLength = argumentsString["-licenceFileName"].length();
char fileNameArg[fileNameLength] = {};
getCharsFromString(argumentsString["-licenceFileName"], fileNameArg, fileNameLength);
if (plcLicence.ReadLicence(fileNameArg, argumentsString["-licenceType"], argumentsString["-licenceVersion"], cidArg, csdArg) == false)
if (plcLicence.ReadLicence(argumentsString["-licenceFileName"], argumentsString["-licenceType"], argumentsString["-licenceVersion"], argumentsString["-cid"], argumentsString["-csd"]) == false)
{
cout << "Reading error: " << plcLicence.operationErrors << endl;
}

View File

@@ -57,10 +57,13 @@ PlcLicence::PlcLicence()
{
}
PlcLicence::PlcLicence(char *cid, char *csd, string binaryType, string dataFileName)
PlcLicence::PlcLicence(string cisArg, string csdArg, string binaryType, string dataFileName)
{
this->cid = cid;
this->csd = csd;
for (int i = 0; i < cisArg.length(); i++)
this->cid[i] = cisArg[i];
for (int i = 0; i < csdArg.length(); i++)
this->csd[i] = csdArg[i];
this->stationName = "";
this->distributor = "";
@@ -74,34 +77,6 @@ PlcLicence::PlcLicence(char *cid, char *csd, string binaryType, string dataFileN
this->binaryGeneration = BinaryGenerationType::File;
}
PlcLicence::PlcLicence(map<string, string> &arguments)
{
if (arguments.count("-cid") > 0)
{
const int cidLength = arguments["-cid"].length();
cout << "\n";
cout << "argument: " << arguments["-cid"] << endl;
cout << "argument delka: " << arguments["-cid"].length() << endl;
char cidArg[cidLength] = {};
getCharsFromString1(arguments["-cid"], cidArg);
this->cid = cidArg;
cout << "\n"
<< "CID 1:" << cid << " delka: " << strlen(cid) << "\n";
}
this->licType = 2;
if (arguments.count("-outputType") > 0)
{
if (arguments["-outputType"] == "base64")
this->binaryGeneration = BinaryGenerationType::Base64Cout;
else
this->binaryGeneration = BinaryGenerationType::File;
}
else
this->binaryGeneration = BinaryGenerationType::Base64Cout;
}
/// @brief process config file
/// @param dataFileName
void PlcLicence::processConfigFile(string &dataFileName)
@@ -476,22 +451,7 @@ bool PlcLicence::CreateEosEovLicence()
return false;
}
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)
bool PlcLicence::ReadEosEovLicence(string dataFileName)
{
BYTE licPostfix = 0;
FILE *licenceFile;
@@ -499,15 +459,23 @@ bool PlcLicence::ReadEosEovLicence(const char *dataFileName)
long lSize;
size_t result;
licenceFile = fopen(dataFileName, "rb");
fseek(licenceFile, 0, SEEK_END);
const int size = ftell(licenceFile);
const int fileNameLength = dataFileName.length();
char fileNameCh[fileNameLength] = {};
for (int i = 0; i < dataFileName.length(); i++)
fileNameCh[i] = dataFileName[i];
cout << dataFileName << endl;
licenceFile = fopen(fileNameCh, "rb"); // read mode
fseek(licenceFile, 0, SEEK_END); // seek to end of file
const int size = ftell(licenceFile); // get current file pointer
fseek(licenceFile, 0, SEEK_SET);
if (licenceFile == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
operationErrors = "Error while opening the file.\n";
return false;
}
int count = 0;
@@ -523,6 +491,9 @@ bool PlcLicence::ReadEosEovLicence(const char *dataFileName)
fclose(licenceFile);
// this->cid = cid;
// this->csd = csd;
getSDData();
string licFileName = getLicenceName(licPostfix);
@@ -539,145 +510,7 @@ bool PlcLicence::ReadEosEovLicence(const char *dataFileName)
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;
}
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)];
// original jsou testy na velikost přečteného file, zatím TODO
BYTE prefixType = (int)licenceContent[3] - 0x30;
if (prefixType == PrefixType::ELC1)
@@ -690,6 +523,7 @@ 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);
@@ -769,3 +603,20 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos
<< ", data: " << licenceHeader.licHeader.description << endl;
return true;
}
bool PlcLicence::ReadLicence(string dataFileName, string licenceType, string licenceVersion, string cidArg, string csdArg)
{
for (int i = 0; i < cidArg.length(); i++) this->cid[i] = cidArg[i];
for (int i = 0; i < csdArg.length(); i++) this->csd[i] = csdArg[i];
this->dataLicenceType = licenceType;
this->dataLicenceVersion = licenceVersion;
if (this->dataLicenceType == eoseovLicenceType)
{
return ReadEosEovLicence(dataFileName);
}
return true;
}