diff --git a/include/plcLicence.h b/include/plcLicence.h index be1eac1..11fa5ec 100644 --- a/include/plcLicence.h +++ b/include/plcLicence.h @@ -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 & 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(); diff --git a/src/CreateLicence.cpp b/src/CreateLicence.cpp index cb500fe..a5efe49 100644 --- a/src/CreateLicence.cpp +++ b/src/CreateLicence.cpp @@ -14,32 +14,19 @@ int main(int argc, char *argv[]) { - map argumentsString = getArguments(argc, argv); + map 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, -licenceFileName, -licenceType=EOV_OSV, -licenceVersion - + // 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; } diff --git a/src/PlcLicence.cpp b/src/PlcLicence.cpp index e39e04b..5d55c01 100644 --- a/src/PlcLicence.cpp +++ b/src/PlcLicence.cpp @@ -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 &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,154 +459,25 @@ 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); - } - - int count = 0; - - unsigned char licenceContent[size]; - - for (int i = 0; i < size; i++) - { - ch = fgetc(licenceFile); - licenceContent[i] = ch; - count++; - } - - fclose(licenceFile); - - 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"; + operationErrors = "Error while opening the file.\n"; 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]; @@ -660,8 +491,8 @@ bool PlcLicence::ReadLicence(const char *dataFileName, WORD licType, BYTE licPos fclose(licenceFile); - this->cid = cid; - this->csd = csd; + // this->cid = cid; + // this->csd = csd; getSDData(); @@ -679,6 +510,8 @@ 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) { @@ -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; +} +