diff --git a/.vscode/settings.json b/.vscode/settings.json index 44bd1b7..3ef3365 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -83,7 +83,8 @@ "numbers": "cpp", "span": "cpp", "cinttypes": "cpp", - "variant": "cpp" + "variant": "cpp", + "climits": "cpp" }, "C_Cpp.errorSquiggles": "enabledIfIncludesResolve" } \ No newline at end of file diff --git a/Makefile b/Makefile index bddd9a5..be33279 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ CXX = g++ # define any compile-time flags # CXXFLAGS := -std=c++17 -Wall -Wextra -g -lssl -lcrypto -w -CXXFLAGS := -Wall -Wextra -g -lssl -lcrypto +CXXFLAGS := -Wall -Wextra -g -lssl -lcrypto -w # define library paths in addition to /usr/lib # if I wanted to include libraries not in /usr/lib I'd specify @@ -19,10 +19,10 @@ LFLAGS = OUTPUT := output # define source directory -SRC := src +SRC := src src/common src/reader src/generator # define include directory -INCLUDE := include +INCLUDE := include include/common include/reader include/generator # define lib directory LIB := lib diff --git a/include/SDCard.h b/include/common/SDCard.h similarity index 99% rename from include/SDCard.h rename to include/common/SDCard.h index 52acab6..dab8b1c 100644 --- a/include/SDCard.h +++ b/include/common/SDCard.h @@ -23,8 +23,6 @@ const string cidFilePath = ""; class SDCard { - - struct SDCardData { uint8_t isCorrectLoad = 0; // SD karta je korektne nactena //bool vs byte ? diff --git a/include/licenceCommon.h b/include/common/licenceCommon.h similarity index 68% rename from include/licenceCommon.h rename to include/common/licenceCommon.h index 5c0e644..c0de9fe 100644 --- a/include/licenceCommon.h +++ b/include/common/licenceCommon.h @@ -1,9 +1,12 @@ #ifndef LICENCE_COMMON_H_ #define LICENCE_COMMON_H_ +//---------------- společná hlavička pro všechny licence ---------------- + #include +#include #include -#include "pugixml.hpp" +#include using namespace std; @@ -15,6 +18,11 @@ const int cidSize = 32; const int csdSize = 32; const int generatorVersion = 1; +typedef uint8_t BYTE; +typedef uint16_t WORD; +typedef uint32_t DWORD; +typedef uint32_t DATE; + enum class GeneralError { GeneralError = 1, @@ -27,7 +35,9 @@ enum class GeneralError ELCMismatch = 8, CRCMismatch = 9, EncryptError = 10, - DecryptError = 11 + DecryptError = 11, + ParamMissing = 12, + IvanlidParam = 13 }; enum class ELCType @@ -91,7 +101,7 @@ struct LicenceIdentification string licTypeName = ""; PlcType licPlcType; - uint8_t licenceVersion = 1; // verze licence, určitě kodování, pojmenování souborů + uint8_t licenceVersion = 1; // verze licence, určuje kodování, pojmenování souborů uint8_t licenceIndex = 0; uint8_t revision; uint8_t licCompatibility = 1; // identikator hlavního ELC @@ -100,19 +110,24 @@ struct LicenceIdentification string cid_cds_path = ""; string licenceFilePath = ""; - }; -struct LicData +// struct LicData +// { +// string station = ""; +// string distributor = ""; +// string licenceName = ""; +// string projectDescription = ""; +// string cid = ""; // CID z SD karty +// string csd = ""; // CSD z SD karty +// string uid = ""; // jedinečný identifikátor z jiného systému +// pugi::xml_document *doc; +// }; + +struct LicenceItem { - string station = ""; - string distributor = ""; - string licenceName = ""; - string projectDescription = ""; - string cid = ""; // CID z SD karty - string csd = ""; // CSD z SD karty - string uid = ""; // jedinečný identifikátor z jiného systému - pugi::xml_document *doc; + int protocolId = -1; + int dataPointsCount = 0; }; struct LicenceItem11 @@ -133,29 +148,35 @@ struct LicenceItem31 int dataPointsCount = 0; }; +struct LicenceInfo +{ + int reqDataPointsCount = 0; + map licences = {}; +}; + struct LicenceInfo11 - { - int reqDataPointsCount = 0; - map licences = {}; - }; +{ + int reqDataPointsCount = 0; + map licences = {}; +}; - struct LicenceInfo21 - { - int reqDataPointsCount = 0; - map licences = {}; - }; +struct LicenceInfo21 +{ + int reqDataPointsCount = 0; + map licences = {}; +}; - struct LicenceInfo31 - { - int reqDataPointsCount = 0; - map licences = {}; - }; +struct LicenceInfo31 +{ + int reqDataPointsCount = 0; + map licences = {}; +}; - struct LicenceInfoGeneral - { - int reqDataPointsCount = 0; - map licences = {}; - }; +struct LicenceInfoGeneral +{ + int reqDataPointsCount = 0; + map licences = {}; +}; class LicenceException : public std::exception { diff --git a/include/common/licenceELC2.h b/include/common/licenceELC2.h new file mode 100644 index 0000000..555b2a0 --- /dev/null +++ b/include/common/licenceELC2.h @@ -0,0 +1,107 @@ +#ifndef PLC_LICENCE2_COMMON_H +#define PLC_LICENCE2_COMMON_H + +#include +#include +#include +#include "licenceCommon.h" +#include "SDCard.h" + +/// @brief společná třída pro pro reader i generator licence typu ELC2 +class LicenceELC2 +{ +public: + LicenceELC2(); + LicenceELC2(LicenceIdentification &licIdentification); + ~LicenceELC2(); + + string getLicenceName(); + string cid_cdsPath = ""; + string licenceFilePath = ""; + + + ErrorMessage errorMessage; + + + enum class Error + { + SDCardReadError = 50, + LicenceReadError = 51, + LicenceSizeMismatch = 52, + LicenceSizeCardMismatch = 53, + LicenceMismatch = 54, + ItemsCountMismatch = 61 + }; + + unordered_map mapErrors = { + {Error::SDCardReadError, "Nepodařilo se načíst SD kartu."}, + {Error::ItemsCountMismatch, "Nesouhlasí počet položek licence."}, + {Error::LicenceSizeMismatch, "Nesouhlasí velikost souboru licence."}, + {Error::LicenceSizeCardMismatch, "Nesouhlasí velikost SD karty."}, + {Error::LicenceMismatch, "Nesouhlasí licence."}, + {Error::ItemsCountMismatch, "Nesouhlasí počet položek licence."}, + }; + + struct LicenceId + { + char licIdent[5] = {'E', 'L', 'C', '0', '_'}; + }; + + // struct __attribute__((__packed__)) LicencePublicHeader + struct LicenceIdentDataHeader + { + BYTE licenceType = 0; // EOSEOV, DRT ... + BYTE licenceTypeVersion = 1; // verze licence, urcuje nuance sifrování a pojmenování souborů + BYTE licenceIndex = 0; // puvodní post fix, identifikátor pro více licencí + BYTE compatibilityVersion = 1; // udava verzi komplet PrivateContent + BYTE licItemCount = 0; // počet licenčních bodů + WORD publicHeaderLength = 0; // délka veřejné hlavičy + WORD cardSize = 0; // velikost SD karty + DWORD serialNumber = 0; // seriove cislo karty + }; + + /// + struct licDataItem + { + WORD protoId = 0; // id protokolu pro ktery je licence + WORD licCount = 0; // pocet datovych bodu licence + char dummy[64] = {}; // dummy pro větší velikost licence v případě méně licenčních bodů + }; + + struct PublicHeader + { + string version = ""; + string projectDescription = ""; + string date = ""; + string licenceType = ""; + int crc = 0; + }; + + struct PrivateContent // privátní šifrovaná část + { + LicenceIdentDataHeader licenceIdentHeader; + vector dataItems; + WORD crc = 0; + }; + + struct LicenceBody + { + LicenceId licId; + LicenceIdentDataHeader licenceIdentHeader; + string publicHeader = ""; // JSON + PrivateContent privateContent; + }; + + LicenceBody licBody; + SDCard sdCard; + LicenceIdentification lIdentification; + vector cryptPrivateContent(const std::vector &content); + vector decryptPrivateContent(const std::vector &content); + void initVector(BYTE (&iVector)[], BYTE (&key)[]); + +private: + + +}; + +#endif \ No newline at end of file diff --git a/include/utils.h b/include/common/utils.h similarity index 97% rename from include/utils.h rename to include/common/utils.h index b09d19b..2c5e144 100644 --- a/include/utils.h +++ b/include/common/utils.h @@ -27,7 +27,7 @@ struct DateAndTime { void generatePause(); std::string right(const std::string& sourceString, size_t numChars); -void getCharsFromString(string source, char *charArray); +void getCharsFromString(const string source, char *charArray); uint16_t calculateCRC16(const uint8_t* data, size_t length); WORD getCurrentDateAsCODESYSDate(); DWORD getCurrentDateAsCODESYSDateDword(); diff --git a/include/generator/licGenELC1.h b/include/generator/licGenELC1.h new file mode 100644 index 0000000..91191ca --- /dev/null +++ b/include/generator/licGenELC1.h @@ -0,0 +1,9 @@ +#ifndef ELC1_GENERATOR__H +#define ELC1_GENERATOR__H + +namespace Generator +{ + +} + +#endif \ No newline at end of file diff --git a/include/generator/licGenELC2.h b/include/generator/licGenELC2.h new file mode 100644 index 0000000..a4391d1 --- /dev/null +++ b/include/generator/licGenELC2.h @@ -0,0 +1,9 @@ +#ifndef ELC2_GENERATOR__H +#define ELC2_GENERATOR__H + +namespace Generator +{ + +} + +#endif \ No newline at end of file diff --git a/include/generator/licGenELC3.h b/include/generator/licGenELC3.h new file mode 100644 index 0000000..14eb29e --- /dev/null +++ b/include/generator/licGenELC3.h @@ -0,0 +1,9 @@ +#ifndef ELC3_GENERATOR__H +#define ELC3_GENERATOR__H + +namespace Generator +{ + +} + +#endif \ No newline at end of file diff --git a/include/licenceGenerator.h b/include/generator/licenceGenerator.h similarity index 72% rename from include/licenceGenerator.h rename to include/generator/licenceGenerator.h index 7e75f0a..33204e0 100644 --- a/include/licenceGenerator.h +++ b/include/generator/licenceGenerator.h @@ -11,16 +11,33 @@ #include #include #include +#include "pugixml.hpp" #include "licenceCommon.h" #include "licenceELC11.h" #include "licenceELC21.h" #include "licenceELC31.h" +#include "licGenELC1.h" +#include "licGenELC2.h" +#include "licGenELC3.h" + using namespace std; class LicenceGenerator { + struct LicData + { + string station = ""; + string distributor = ""; + string licenceName = ""; + string projectDescription = ""; + string cid = ""; // CID z SD karty + string csd = ""; // CSD z SD karty + string uid = ""; // jedinečný identifikátor z jiného systému + pugi::xml_document *doc; + }; + public: int elcSwitchType; string operationErrors = ""; @@ -34,17 +51,15 @@ public: LicenceGenerator(); ~LicenceGenerator(); LicenceGenerator(string uid, string cid, string csd, string dataFileName); - LicenceGenerator(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility); - + void createLicenceFile(); - void readLicenceFile(); - + int getDataPointsCount(int protocolId); - bool getLicenceInfo(int protocolId, void *returnStructure); - bool getLicenceItemInfo(int protocolId, void *returnItemStructure); + LicenceInfoGeneral licenceInfo; + private: LicenceIdentification licIdentification; LicData licData; @@ -58,9 +73,6 @@ private: void createLicenceELC2(); void createLicenceELC3(); - void readLicenceELC1(); - void readLicenceELC2(); - void readLicenceELC3(); }; #endif \ No newline at end of file diff --git a/include/pugiconfig.hpp b/include/generator/pugiconfig.hpp similarity index 100% rename from include/pugiconfig.hpp rename to include/generator/pugiconfig.hpp diff --git a/include/pugixml.hpp b/include/generator/pugixml.hpp similarity index 100% rename from include/pugixml.hpp rename to include/generator/pugixml.hpp diff --git a/include/licenceELC21.h b/include/licenceELC21.h index 468d5e8..ca8fb42 100644 --- a/include/licenceELC21.h +++ b/include/licenceELC21.h @@ -5,6 +5,7 @@ #include #include #include "licenceCommon.h" +#include "pugixml.hpp" #include "SDCard.h" using namespace std; @@ -12,22 +13,33 @@ using namespace std; class LicenceELC21 { + struct LicData + { + string station = ""; + string distributor = ""; + string licenceName = ""; + string projectDescription = ""; + string cid = ""; // CID z SD karty + string csd = ""; // CSD z SD karty + string uid = ""; // jedinečný identifikátor z jiného systému + pugi::xml_document *doc; + }; + public: - ErrorMessage errorMessage; LicenceInfo21 licenceInfo; - string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/ + string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/ string licenceFilePath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; LicenceELC21(); ~LicenceELC21(); - LicenceELC21(LicenceIdentification & licIdentification, LicData & icData); - LicenceELC21(LicenceIdentification & licIdentification); - LicenceELC21(LicData & icData); + LicenceELC21(LicenceIdentification &licIdentification, LicData &licData); + LicenceELC21(LicenceIdentification &licIdentification); + LicenceELC21(LicData &licData); bool createLicence(); - bool readLicence(LicenceInfoGeneral * licences); + bool readLicence(LicenceInfoGeneral *licences); int getDataPointsCount(int protocolId); - bool getLicenceInfo(void * ptr); + bool getLicenceInfo(void *ptr); bool getLicenceItemInfo(int protocolId, void *returnItemStructure); enum class Error21 @@ -48,31 +60,31 @@ public: {Error21::LicenceMismatch, "Nesouhlasí licence."}, {Error21::ItemsCountMismatch, "Nesouhlasí počet položek licence."}, }; - + struct LicenceId { - char licIdent[5] = { 'E', 'L', 'C', '0','_'}; + char licIdent[5] = {'E', 'L', 'C', '0', '_'}; }; - //struct __attribute__((__packed__)) LicencePublicHeader + // struct __attribute__((__packed__)) LicencePublicHeader struct LicenceIdentDataHeader { - BYTE licenceType = 0; //EOSEOV, DRT ... - BYTE licenceTypeVersion = 1; //verze licence, urcuje nuance sifrování a pojmenování souborů - BYTE licenceIndex = 0; //puvodní post fix, identifikátor pro více licencí - BYTE compatibilityVersion = 1; //udava verzi komplet PrivateContent - BYTE licItemCount = 0; //počet licenčních bodů - WORD publicHeaderLength = 0; //délka veřejné hlavičy - WORD cardSize = 0; //velikost SD karty - DWORD serialNumber = 0;//seriove cislo karty + BYTE licenceType = 0; // EOSEOV, DRT ... + BYTE licenceTypeVersion = 1; // verze licence, urcuje nuance sifrování a pojmenování souborů + BYTE licenceIndex = 0; // puvodní post fix, identifikátor pro více licencí + BYTE compatibilityVersion = 1; // udava verzi komplet PrivateContent + BYTE licItemCount = 0; // počet licenčních bodů + WORD publicHeaderLength = 0; // délka veřejné hlavičy + WORD cardSize = 0; // velikost SD karty + DWORD serialNumber = 0; // seriove cislo karty }; - /// + /// struct licDataItem { - WORD protoId = 0; // id protokolu pro ktery je licence - WORD licCount = 0; // pocet datovych bodu licence - char dummy[64] = {}; + WORD protoId = 0; // id protokolu pro ktery je licence + WORD licCount = 0; // pocet datovych bodu licence + char dummy[64] = {}; // dummy pro větší velikost licence v případě méně licenčních bodů }; struct PublicHeader @@ -84,7 +96,7 @@ public: int crc = 0; }; - struct PrivateContent //privátní šifrovaná část + struct PrivateContent // privátní šifrovaná část { LicenceIdentDataHeader licenceIdentHeader; vector dataItems; @@ -95,12 +107,11 @@ public: { LicenceId licId; LicenceIdentDataHeader licenceIdentHeader; - string publicHeader = ""; //JSON - PrivateContent privateContent; + string publicHeader = ""; // JSON + PrivateContent privateContent; }; - private: - +private: int licItemCount = 0; LicenceBody licBody; LicenceIdentification lIdentification; @@ -110,11 +121,10 @@ public: string getVersion(int middleVersion); void getLicenceItems(); bool processInputConfiguration(); - vector cryptPrivateContent(const std::vector& content); - vector decryptPrivateContent(const std::vector& content); + vector cryptPrivateContent(const std::vector &content); + vector decryptPrivateContent(const std::vector &content); void initVector(BYTE (&iVector)[], BYTE (&key)[]); string getLicenceName(); - }; #endif diff --git a/include/licenceELC31.h b/include/licenceELC31.h index 4447076..fdd33c3 100644 --- a/include/licenceELC31.h +++ b/include/licenceELC31.h @@ -6,11 +6,24 @@ #include #include "utils.h" #include "licenceCommon.h" +#include "pugixml.hpp" using namespace std; class LicenceELC31 { + struct LicData + { + string station = ""; + string distributor = ""; + string licenceName = ""; + string projectDescription = ""; + string cid = ""; // CID z SD karty + string csd = ""; // CSD z SD karty + string uid = ""; // jedinečný identifikátor z jiného systému + pugi::xml_document *doc; + }; + LicData lData; bool processInputConfiguration(); LicenceIdentification lIdentification; diff --git a/include/reader/licReaderELC1.h b/include/reader/licReaderELC1.h new file mode 100644 index 0000000..f054028 --- /dev/null +++ b/include/reader/licReaderELC1.h @@ -0,0 +1,9 @@ +#ifndef ELC1_READER__H +#define ELC1_READER__H + +namespace Reader +{ + +} + +#endif \ No newline at end of file diff --git a/include/reader/licReaderELC2.h b/include/reader/licReaderELC2.h new file mode 100644 index 0000000..e77453c --- /dev/null +++ b/include/reader/licReaderELC2.h @@ -0,0 +1,34 @@ +#ifndef ELC2_READER__H +#define ELC2_READER__H + +#include "utils.h" +#include "licenceCommon.h" +#include "licenceELC2.h" +#include "SDCard.h" + +using namespace std; + +namespace Reader +{ + class Licence2 : public LicenceELC2 + { + public: + LicenceInfo21 licenceInfo; + + + Licence2(); + ~Licence2(); + Licence2(LicenceIdentification &licIdentification); + + + bool readLicence(LicenceInfoGeneral *licences); + + int getDataPointsCount(int protocolId); + bool getLicenceInfo(void *ptr); + bool getLicenceItemInfo(int protocolId, void *returnItemStructure); + + private: + }; +} + +#endif \ No newline at end of file diff --git a/include/reader/licReaderELC3.h b/include/reader/licReaderELC3.h new file mode 100644 index 0000000..f8cae3a --- /dev/null +++ b/include/reader/licReaderELC3.h @@ -0,0 +1,9 @@ +#ifndef ELC3_READER__H +#define ELC3_READER__H + +namespace Reader +{ + +} + +#endif \ No newline at end of file diff --git a/include/licenceReader.h b/include/reader/licenceReader.h similarity index 69% rename from include/licenceReader.h rename to include/reader/licenceReader.h index 1d1fd2e..ead75d0 100644 --- a/include/licenceReader.h +++ b/include/reader/licenceReader.h @@ -2,12 +2,16 @@ #define LICENCE_READER_H_ #include "utils.h" -// #include "licenceCommon.h" #include "licenceELC11.h" -#include "licenceELC21.h" #include "licenceELC31.h" +#include "licReaderELC1.h" +#include "licReaderELC2.h" +#include "licReaderELC3.h" + using namespace std; +using namespace Reader; + struct InitStructure { @@ -28,18 +32,13 @@ public: bool argumentsCorrect = false; ErrorMessage error; LicenceELC11 *licence11; - LicenceELC21 *licence21; LicenceELC31 *licence31; + Licence2 *licence2; LicenceReader(); ~LicenceReader(); - bool init(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility); bool init(int elcType, InitStructure &initStructure); - bool initread(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility); bool initread(int elcType, InitStructure &initStructure); - - void readLicenceFile(); - bool getLicenceInfo(void *returnStructure); bool getLicenceItemInfo(int protocolId, void *returnItemStructure); @@ -47,14 +46,9 @@ public: private: LicenceIdentification licIdentification; - LicData licData; string configFileName = ""; - void *licence = nullptr; - void readLicenceELC1(); - void readLicenceELC2(); - void readLicenceELC3(); }; #endif \ No newline at end of file diff --git a/output/licenceGenerator b/output/licenceGenerator index 487aa39..43a8115 100644 Binary files a/output/licenceGenerator and b/output/licenceGenerator differ diff --git a/src/CreateLicence.cpp b/src/CreateLicence.cpp index b63d965..fcd7230 100644 --- a/src/CreateLicence.cpp +++ b/src/CreateLicence.cpp @@ -1,28 +1,26 @@ #include -#include "utils.h" #include "licenceGenerator.h" #include "licenceReader.h" -#include "licenceCommon.h" /// @brief hlavní funkce /// @param argc /// @param argv parametry pro generování licence /// @return -int main5(int argc, char *argv[]) +int main(int argc, char *argv[]) { unordered_map arguments = getArguments(argc, argv); - // try - // { - // unordered_map arguments = getArguments(argc, argv); - // LicenceGenerator generatorOld = LicenceGenerator(arguments["-uid"], arguments["-cid"], arguments["-csd"], arguments["-configFileName"]); - // generatorOld.createLicenceFile(); - // return SUCCES; - // } - // catch (...) - // { - // cerr << "Obecná chyba\n"; - // } + try + { + unordered_map arguments = getArguments(argc, argv); + LicenceGenerator generatorOld = LicenceGenerator(arguments["-uid"], arguments["-cid"], arguments["-csd"], arguments["-configFileName"]); + generatorOld.createLicenceFile(); + return SUCCES; + } + catch (...) + { + cerr << "Obecná chyba\n"; + } - // system("pause"); + system("pause"); return SUCCES; } diff --git a/src/LicenceELC11.cpp b/src/LicenceELC11.cpp index 7d9f605..7ce7551 100644 --- a/src/LicenceELC11.cpp +++ b/src/LicenceELC11.cpp @@ -484,148 +484,6 @@ bool LicenceELC11::createEosEovLicence() return false; } -bool LicenceELC11::readEosEovLicence(string dataFileName) -{ - BYTE licPostfix = 0; - FILE *licenceFile; - char ch; - - const int fileNameLength = dataFileName.length(); - char fileNameCh[fileNameLength] = {}; - for (unsigned 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 == nullptr) - { - perror("Error while opening the file.\n"); - operationErrors = "Error while opening the file.\n"; - return false; - } - - 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{}; - 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) - { - - if (licenceHeader.licHeader.licSubType == cEzLic_p78ou3_SubType_10_10) - { - 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 - { - - 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 == PlcLicenceType::LicenceOther) - { - // původní 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 LicenceELC11::readLicence(LicenceInfoGeneral *licences) { sdCard = SDCard(this->cid_cdsPath); @@ -745,22 +603,4 @@ bool LicenceELC11::readLicence(LicenceInfoGeneral *licences) return true; } -// bool LicenceELC11::readLicence(string dataFileName, string licenceType, string licenceVersion, string cidArg, string csdArg) -// { - -// for (unsigned int i = 0; i < cidArg.length(); i++) -// this->cid[i] = cidArg[i]; -// for (unsigned 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; -// } - LicenceELC11::~LicenceELC11() {} diff --git a/src/LicenceGenerator.cpp b/src/LicenceGenerator.cpp deleted file mode 100644 index dd472d1..0000000 --- a/src/LicenceGenerator.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include -#include -#include -#include -#include "utils.h" -#include "pugixml.hpp" -#include - -LicenceGenerator::LicenceGenerator() -{ -} - -LicenceGenerator::LicenceGenerator(string uid, string cid, string csd, string dataFileName) -{ - try - { - if (dataFileName.empty()) - throw std::runtime_error("Missing dataFileName par."); - - if (uid.empty() == false) - { - this->licData.uid = uid; - } - else - { - if (cid.empty()) - throw std::runtime_error("Missing CID par."); - if (csd.empty()) - throw std::runtime_error("Missing CSD par."); - } - } - catch (const std::exception &e) - { - this->argumentsCorrect = false; - std::cerr << "error: " << e.what() << endl; - return; - } - - this->configFileName = dataFileName; - this->licData.cid = cid; - this->licData.csd = csd; - - this->argumentsCorrect = true; - - if (processInputConfiguration() == false) - { - cerr << "Chyba: " << error.message << endl; - } -} - -bool LicenceGenerator::processInputConfiguration() -{ - string fullFile = getCompletePath(this->configFileName); - const int fileNameLength = fullFile.length(); - char fileName[fileNameLength] = {}; - getCharsFromString(fullFile, fileName, fileNameLength); - -#ifdef WINDOWS - pugi::xml_parse_result result = doc.load_file("licData.xml"); -#else - pugi::xml_parse_result result = doc.load_file(fileName); -#endif - - if (result) - { - const char *dataRootName = doc.child("data") ? "data" : "licence"; // kompatibilita s verzí, která měla ještě root "data" - Mapping mapping; - - if (atoi(&doc.child(dataRootName).attribute("xmlVersion").value()[0]) != XML_VERSION) - { - error.code = -1; - error.message = "Invalid XML VERSION"; - return false; - } - - this->licData.doc = &doc; - - string licType = doc.child(dataRootName).child("licenceType").child_value(); - if (!licType.empty()) - { - this->licIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV; - } - else - { - cerr << endl - << " ERROR MISSING licenceType " << endl; - return false; - } - - this->licIdentification.licenceVersion = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); - this->licIdentification.revision = doc.child(dataRootName).attribute("revision").value()[0]; - this->licIdentification.licenceIndex = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceIndex").value()[0]); - this->licIdentification.licElcType = (ELCType)atoi(&doc.child(dataRootName).attribute("elc").value()[0]); - - //licElcType = (ELCType)atoi(&doc.child(dataRootName).attribute("elc").value()[0]); - //licCompatibility = atoi(&doc.child(dataRootName).attribute("compatibility").value()[0]); - - this->licIdentification.licCompatibility = atoi(&doc.child(dataRootName).attribute("compatibility").value()[0]); - - string plcType = doc.child(dataRootName).child("plcType").child_value(); - if (!plcType.empty()) - { - this->licIdentification.licPlcType = mapping.licMapPlcType[plcType]; - } - } - else - { - std::cerr << "Unable to open the config file." << std::endl; - } - - return true; -} - -void LicenceGenerator::createLicenceFile() -{ - switch (this->licIdentification.licElcType) - { - case ELCType::ELC1: - createLicenceELC1(); - break; - - case ELCType::ELC2: - createLicenceELC2(); - break; - - case ELCType::ELC3: - createLicenceELC3(); - break; - } -} - -void LicenceGenerator::readLicenceFile() -{ - switch (this->licIdentification.licElcType) - { - case ELCType::ELC1: - readLicenceELC1(); - break; - - case ELCType::ELC2: - readLicenceELC2(); - break; - - case ELCType::ELC3: - readLicenceELC3(); - break; - } -} - -void LicenceGenerator::createLicenceELC1() // přejmenvat na ELC1 -{ - LicenceELC11 plcWriteLicence = LicenceELC11(this->licData.cid, this->licData.csd, "file", this->configFileName); - plcWriteLicence.createLicence(); -} - -void LicenceGenerator::createLicenceELC2() -{ - switch (this->licIdentification.licCompatibility) - { - case 1: - LicenceELC21 licence = LicenceELC21(this->licData); - licence.createLicence(); - break; - } -} - -void LicenceGenerator::createLicenceELC3() -{ - switch (this->licIdentification.licCompatibility) - { - case 1: - LicenceELC31 licence = LicenceELC31(this->licData); - licence.createLicence(); - break; - } -} - -void LicenceGenerator::readLicenceELC1() -{ - cout << "reading ELC1" << endl; -} - -void LicenceGenerator::readLicenceELC2() -{ - LicenceELC21 licence = LicenceELC21(this->licIdentification); -} - -void LicenceGenerator::readLicenceELC3() -{ - cout << "reading ELC3" << endl; -} - -LicenceGenerator::~LicenceGenerator() -{ -} diff --git a/src/LicenceReader.cpp b/src/LicenceReader.cpp deleted file mode 100644 index 6cd13f9..0000000 --- a/src/LicenceReader.cpp +++ /dev/null @@ -1,454 +0,0 @@ -#include - -ELCType licElcType = ELCType::ELC2; -LicenceType licLicenceType; -PlcType licPlcType; - -uint8_t licenceVersion = 1; // verze licence, určitě kodování, pojmenování souborů -uint8_t licenceIndex = 0; -uint8_t revision; -uint8_t licCompatibility = 1; // identikator hlavního ELC - -LicenceReader::LicenceReader() -{ -} - -// LicenceReader::LicenceReader(string uid, string cid, string csd, string dataFileName) -// { -// try -// { -// if (dataFileName.empty()) -// throw std::runtime_error("Missing dataFileName par."); - -// if (uid.empty() == false) -// { -// this->licData.uid = uid; -// } -// else -// { -// if (cid.empty()) -// throw std::runtime_error("Missing CID par."); -// if (csd.empty()) -// throw std::runtime_error("Missing CSD par."); -// } -// } -// catch (const std::exception &e) -// { -// this->argumentsCorrect = false; -// std::cerr << "error: " << e.what() << endl; -// return; -// } - -// this->configFileName = dataFileName; -// this->licData.cid = cid; -// this->licData.csd = csd; - -// this->argumentsCorrect = true; - -// if (processInputConfiguration() == false) -// { -// cerr << "Chyba: " << error.message << endl; -// } -// } - -// LicenceReader::LicenceReader(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility) -// { -// this->licIdentification.licElcType = (ELCType)elcType; -// this->licIdentification.licLicenceType = (LicenceType)licenceType; -// this->licIdentification.licenceVersion = licenceVersion; -// this->licIdentification.licenceIndex = licenceIndex; -// this->licIdentification.licCompatibility = compatibility; -// } - -/// @brief inicializace generátoru a načtení licenčních bodů -/// @param elcType -/// @param licenceType -/// @param licenceVersion -/// @param licenceIndex -/// @param compatibility -/// @return -bool LicenceReader::initread(int elcType, InitStructure &initStructure) -{ - try - { - elcSwitchType = elcType * 10 + licCompatibility; - - if (init(elcType, initStructure)) - { - switch (elcSwitchType) - { - case 11: - { // old eoseov - LicenceELC11 licence = LicenceELC11(this->licIdentification); - this->licence11 = &licence; - licence.cid_cdsPath = initStructure.cid_csd_filePath; - licence.licenceFilePath = initStructure.licenceFilePath;; - this->licence11->readLicence(&this->licenceInfo); - break; - } - case 21: - { - LicenceELC21 licence = LicenceELC21(this->licIdentification); - this->licence21 = &licence; - licence.cid_cdsPath = initStructure.cid_csd_filePath; - licence.licenceFilePath = initStructure.licenceFilePath;; - this->licence21->readLicence(&this->licenceInfo); - break; - } - case 31: - { - LicenceELC31 licence = LicenceELC31(this->licIdentification); - break; - } - } - } - else - return false; - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - - return true; // TODO testy -} - -bool LicenceReader::initread(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility) -{ - try - { - elcSwitchType = elcType * 10 + licCompatibility; - - if (init(elcType, licenceType, licenceVersion, licenceIndex, compatibility)) - { - switch (elcSwitchType) - { - case 11: - { // old eoseov - LicenceELC11 licence = LicenceELC11(this->licIdentification); - licence.readLicence(&this->licenceInfo); - break; - } - case 21: - { - LicenceELC21 licence = LicenceELC21(this->licIdentification); - this->licence21 = &licence; - this->licence21->readLicence(&this->licenceInfo); - break; - } - case 31: - { - LicenceELC31 licence = LicenceELC31(this->licIdentification); - break; - } - } - } - else - return false; - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - - return true; // TODO testy -} - -/// @brief inicicialize generátoro, pouze vstupní kontrola -/// @param elcType -/// @param licenceType -/// @param licenceVersion -/// @param licenceIndex -/// @param compatibility -/// @return -bool LicenceReader::init(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility) -{ - try - { - if (elcType > 3) - { - error.code = (int)GeneralError::ELCNotImplemented; - error.message = "ELC není implementováno."; - throw LicenceException((int)GeneralError::ELCNotImplemented, error.message); - return false; - } - - this->licIdentification.licElcType = (ELCType)elcType; - this->licIdentification.licLicenceType = (LicenceType)licenceType; - this->licIdentification.licenceVersion = licenceVersion; - this->licIdentification.licenceIndex = licenceIndex; - this->licIdentification.licCompatibility = compatibility; - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - - elcSwitchType = elcType * 10 + licCompatibility; - - return true; // TODO testy -} - -bool LicenceReader::init(int elcType, InitStructure &initStructure) -{ - try - { - if (elcType > 3) - { - error.code = (int)GeneralError::ELCNotImplemented; - error.message = "ELC není implementováno."; - throw LicenceException((int)GeneralError::ELCNotImplemented, error.message); - } - - this->licIdentification.licElcType = (ELCType)elcType; - this->licIdentification.licLicenceType = (LicenceType)initStructure.licenceType; - this->licIdentification.licenceVersion = licenceVersion; - this->licIdentification.licenceIndex = licenceIndex; - this->licIdentification.licCompatibility = initStructure.compatibility; - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - - elcSwitchType = elcType * 10 + licCompatibility; - - return true; // TODO testy -} - -// bool LicenceReader::init(InitStructure &initStructure) -// { -// try -// { -// if (initStructure.elcType > 3) -// { -// error.code = (int)GeneralError::ELCNotImplemented; -// error.message = "ELC není implementováno."; -// throw LicenceException((int)GeneralError::ELCNotImplemented, error.message); -// return false; -// } - -// this->licIdentification.licElcType = (ELCType)initStructure.elcType; -// this->licIdentification.licLicenceType = (LicenceType)initStructure.licenceType; -// this->licIdentification.licenceVersion = licenceVersion; -// this->licIdentification.licenceIndex = licenceIndex; -// this->licIdentification.licCompatibility = initStructure.compatibility; -// } -// catch (const LicenceException &ex) -// { -// error.code = ex.getErrorCode(); -// error.message = ex.getErrorMessage(); -// return false; -// } - -// elcSwitchType = initStructure.elcType * 10 + licCompatibility; - -// return true; // TODO testy -// } - -bool LicenceReader::getLicenceItemInfo(int protocolId, void *returnItemStructure) -{ - try - { - switch (elcSwitchType) - { - case 11: - { // old eoseov - LicenceItem11 *resultPtr = static_cast(returnItemStructure); - resultPtr->protocolId = protocolId; - if (this->licenceInfo.licences.count(protocolId)) - resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); - else - resultPtr->dataPointsCount = 0; - break; - } - case 21: - { - - LicenceItem21 *resultPtr = static_cast(returnItemStructure); - resultPtr->protocolId = protocolId; // protocolId; - if (this->licenceInfo.licences.count(protocolId)) - resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); - else - resultPtr->dataPointsCount = 0; - break; - } - case 31: - { - LicenceELC31 licence = LicenceELC31(this->licIdentification); - break; - } - default: - { - error.code = -1; - error.message = "Nepodařilo se identifikovat licenci"; - return false; - } - } - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - - return true; -} - -bool LicenceReader::getLicenceInfo(void *returnStructure) -{ - // const type_info& ti1 = typeid(returnStructure); - // cout << "TTTYP: " << ti1.name() << endl; - switch (elcSwitchType) - { - case 11: - { // old eoseov - break; - } - case 21: - { - LicenceELC21 licence = LicenceELC21(this->licIdentification); - try - { - licence.readLicence(&this->licenceInfo); - return licence.getLicenceInfo(returnStructure); - } - catch (const LicenceException &ex) - { - error.code = ex.getErrorCode(); - error.message = ex.getErrorMessage(); - return false; - } - break; - } - case 31: - { - LicenceELC31 licence = LicenceELC31(this->licIdentification); - // try - // { - // licence.readLicence(); - // return licence.getLicenceInfo(returnStructure); - // } - // catch (const LicenceException &ex) - // { - // error.code = ex.getErrorCode(); - // error.message = ex.getErrorMessage(); - // return false; - // } - break; - } - } - - error.code = -1; - error.message = "Nepodařilo se identifikovat licenci"; - return false; -} - -// bool LicenceReader::processInputConfiguration() -// { -// string fullFile = getCompletePath(this->configFileName); -// const size_t fileNameLength = fullFile.length(); -// char fileName[fileNameLength] = {}; -// getCharsFromString(fullFile, fileName, fileNameLength); - -// #ifdef WINDOWS -// pugi::xml_parse_result result = doc.load_file("licData.xml"); -// #else -// pugi::xml_parse_result result = doc.load_file(fileName); -// #endif - -// if (result) -// { -// const char *dataRootName = doc.child("data") ? "data" : "licence"; // kompatibilita s verzí, která měla ještě root "data" -// Mapping mapping; - -// if (atoi(&doc.child(dataRootName).attribute("xmlVersion").value()[0]) != XML_VERSION) -// { -// error.code = -1; -// error.message = "Invalid XML VERSION"; -// return false; -// } - -// this->licData.doc = &doc; - -// string licType = doc.child(dataRootName).child("licenceType").child_value(); -// if (!licType.empty()) -// { -// this->licIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV; -// } -// else -// { -// cerr << endl -// << " ERROR MISSING licenceType " << endl; -// return false; -// } - -// this->licIdentification.licenceVersion = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); -// this->licIdentification.revision = doc.child(dataRootName).attribute("revision").value()[0]; -// this->licIdentification.licenceIndex = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceIndex").value()[0]); -// this->licIdentification.licElcType = (ELCType)atoi(&doc.child(dataRootName).attribute("elc").value()[0]); - -// licElcType = (ELCType)atoi(&doc.child(dataRootName).attribute("elc").value()[0]); -// licCompatibility = atoi(&doc.child(dataRootName).attribute("compatibility").value()[0]); - -// this->licIdentification.licCompatibility = atoi(&doc.child(dataRootName).attribute("compatibility").value()[0]); - -// string plcType = doc.child(dataRootName).child("plcType").child_value(); -// if (!plcType.empty()) -// { -// this->licIdentification.licPlcType = mapping.licMapPlcType[plcType]; -// } -// } -// else -// { -// std::cerr << "Unable to open the config file." << std::endl; -// } - -// return true; -// } - -void LicenceReader::readLicenceFile() -{ - switch (this->licIdentification.licElcType) - { - case ELCType::ELC1: - readLicenceELC1(); - break; - - case ELCType::ELC2: - readLicenceELC2(); - break; - - case ELCType::ELC3: - readLicenceELC3(); - break; - } -} - -void LicenceReader::readLicenceELC1() -{ - cout << "reading ELC1" << endl; -} - -void LicenceReader::readLicenceELC2() -{ - LicenceELC21 licence = LicenceELC21(this->licIdentification); -} - -void LicenceReader::readLicenceELC3() -{ - cout << "reading ELC3" << endl; -} - -LicenceReader::~LicenceReader() -{ -} diff --git a/src/ReadLicence.cpp b/src/ReadLicence.cpp index d230594..8fda242 100644 --- a/src/ReadLicence.cpp +++ b/src/ReadLicence.cpp @@ -1,14 +1,13 @@ #include #include "utils.h" -#include "licenceGenerator.h" #include "licenceReader.h" -#include "licenceCommon.h" + /// @brief hlavní funkce /// @param argc /// @param argv parametry pro generování licence /// @return -int main() +int mainno() { // unordered_map arguments = getArguments(argc, argv); // try @@ -29,7 +28,8 @@ int main() try { - int protocolId = 222; + + int protocolId = 444; LicenceReader licenceReader1 {}; @@ -50,17 +50,15 @@ int main() if (licenceReader1.getLicenceInfo(&info)) { - std::cout << "Počet licenčních bodů pro : " << protocolId << ": " << info.reqDataPointsCount << std::endl; if (info.licences.count(protocolId) > 0) cout << "Počet z vectoru pro : " << protocolId << ": " << info.licences.at(protocolId) << endl; cout << "Všechny licenční body: " << endl; for (const auto &pair : info.licences) { std::cout << "<" << pair.first << ", " << pair.second << ">" << endl; } } - else - cerr << "Došlo k chybě: " << licenceReader1.error.message; + else cout << "Došlo k chybě: " << licenceReader1.error.message; } else { - cerr << "Došlo k chybě: " << licenceReader1.error.message; + cout << "Došlo k chybě: " << licenceReader1.error.message; } cout << "\n" << "-- verze #2: init a iterace" << "\n"; @@ -77,16 +75,16 @@ int main() std::cout << "Počet licenčních bodů pro " << info.protocolId << ": " << info.dataPointsCount << std::endl; } else - cerr << "Došlo k chybě: " << licenceReader2.error.message; + cout << "Došlo k chybě: " << licenceReader2.error.message; } else { - cerr << "Došlo k chybě: " << licenceReader2.error.message; + cout << "Došlo k chybě: " << licenceReader2.error.message; } } catch (...) { - cerr << "Obecná chyba\n"; + cout << "Obecná chyba\n"; } system("pause"); diff --git a/src/_notestest.cpp b/src/_notestest.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/SDCard.cpp b/src/common/SDCard.cpp similarity index 100% rename from src/SDCard.cpp rename to src/common/SDCard.cpp diff --git a/src/common/SDCard.d b/src/common/SDCard.d new file mode 100644 index 0000000..d6aa52c --- /dev/null +++ b/src/common/SDCard.d @@ -0,0 +1,2 @@ +src/common/SDCard.o: src/common/SDCard.cpp include/common/utils.h \ + include/common/SDCard.h diff --git a/src/common/SDCard.o b/src/common/SDCard.o new file mode 100644 index 0000000..d4a206d Binary files /dev/null and b/src/common/SDCard.o differ diff --git a/src/common/licenceELC2.cpp b/src/common/licenceELC2.cpp new file mode 100644 index 0000000..fa2b48d --- /dev/null +++ b/src/common/licenceELC2.cpp @@ -0,0 +1,172 @@ +#include "licenceELC2.h" +#include "utils.h" + + + LicenceELC2::LicenceELC2(){} + + LicenceELC2::~LicenceELC2(){} + + LicenceELC2::LicenceELC2(LicenceIdentification &licIdentification): lIdentification(licIdentification) + {} + + /// @brief get proper licencename +/// @param licPostfix +/// @return +string LicenceELC2::getLicenceName() +{ + string result = ""; + char prefixChar = 97; + int licType = (int)lIdentification.licLicenceType; + int lVersion = lIdentification.licenceVersion; + + unordered_map baseString; + baseString.insert(std::pair((int)LicenceType::EOS_EOV, "ezlic_eovosv")); + baseString.insert(std::pair((int)LicenceType::DDTS, "ezlic_ddts")); + baseString.insert(std::pair((int)LicenceType::DRT, "ezlic_drt")); + + struct Index + { + int index[11]; + }; + + std::unordered_map indexes; + Index indexes1 = {7, 16, 20, 23, 18, 4, 9, 11, 6, 9, 13}; + Index indexes2 = {12, 10, 22, 23, 24, 25, 9, 11, 2, 1, 3}; // puvodní indexy + Index indexes3 = {8, 13, 11, 9, 7, 11, 10, 13, 5, 20, 19}; + + indexes.insert(std::pair(1, indexes1)); + indexes.insert(std::pair(2, indexes2)); + indexes.insert(std::pair(3, indexes3)); + + result = baseString.at(licType) + to_string(lIdentification.licenceIndex) + "_"; + + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[0]] + (lIdentification.licenceIndex * 11)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[1]] + (lIdentification.licenceIndex * 39)) % 25); + + result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[2]] + (lIdentification.licenceIndex * 1)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[3]] * 2) % 25); + result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[4]] + (lIdentification.licenceIndex * 5)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[5]] * 3) % 25); + + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[6]] + (lIdentification.licenceIndex * 52)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[7]] + (lIdentification.licenceIndex * 34)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[8]] + (lIdentification.licenceIndex * 21)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[9]] + (lIdentification.licenceIndex * 47)) % 25); + result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[10]] + (lIdentification.licenceIndex * 7)) % 25); + + result += ".lic"; + return result; +} + +vector LicenceELC2::cryptPrivateContent(const std::vector &content) +{ + + BYTE initVector[15] = {}; + BYTE aesKey[32] = {}; + + LicenceELC2::initVector(initVector, aesKey); + + unsigned char encrypted[10000] = {}; + const unsigned char *plainTextArray = content.data(); + int finalEncryptedLength = encrypt(plainTextArray, content.size(), aesKey, initVector, encrypted); + + if (finalEncryptedLength <= 0) throw LicenceException((int)GeneralError::EncryptError, "Chyba při kryptování."); + + std::vector result(encrypted, encrypted + finalEncryptedLength); + + return result; +} + +vector LicenceELC2::decryptPrivateContent(const std::vector &content) +{ + + BYTE initVector[15] = {0}; + BYTE aesKey[32] = {0}; + + LicenceELC2::initVector(initVector, aesKey); + + const unsigned char *encryptedData = content.data(); + unsigned char decrypted[10000] = {}; + + int decrypted_len = decrypt(encryptedData, content.size(), aesKey, initVector, decrypted); + if (decrypted_len <= 0) throw LicenceException((int)GeneralError::DecryptError, "Chyba při dekryptování."); + + std::vector result(decrypted, decrypted + decrypted_len); + + return result; +} + +void LicenceELC2::initVector(BYTE (&iVector)[], BYTE (&key)[]) +{ + + struct Vector15 + { + int vec[15]; + }; + + Vector15 vec1 = {this->sdCard.cardData.CID[10], + this->sdCard.cardData.CID[12], + this->sdCard.cardData.CID[11], + this->sdCard.cardData.CID[9], + this->sdCard.cardData.CID_nibble[22] - 15, + this->sdCard.cardData.CID_nibble[24] - 15, + this->sdCard.cardData.CID_nibble[25] - 15, + this->sdCard.cardData.CID_nibble[21] - 15, + 9, 10, 11, 12, 13, 14, 15}; + Vector15 vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //TODO přidat smysluplnější indexy + Vector15 vec3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + + std::unordered_map vectors; + vectors.insert(std::pair(1, vec1)); + vectors.insert(std::pair(2, vec2)); + vectors.insert(std::pair(3, vec3)); + + struct Key32 + { + int key[32]; + }; + + Key32 key1 = {this->sdCard.cardData.CID[12], + this->sdCard.cardData.CID[23] - 15, + this->sdCard.cardData.CID[25] - 15, + this->sdCard.cardData.CID[11], + this->sdCard.cardData.CID[9], + this->sdCard.cardData.CID_nibble[21], + this->sdCard.cardData.CID[9] % 25, + this->sdCard.cardData.CID_nibble[22] - 15, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + Key32 key2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + Key32 key3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + + std::unordered_map keys; + keys.insert(std::pair(1, key1)); + keys.insert(std::pair(2, key2)); + keys.insert(std::pair(3, key3)); + + int lVersion = lIdentification.licenceVersion; + + iVector[0] = vectors.at(lVersion).vec[0]; + iVector[1] = vectors.at(lVersion).vec[1]; + iVector[2] = vectors.at(lVersion).vec[2]; + iVector[3] = vectors.at(lVersion).vec[3]; + iVector[4] = vectors.at(lVersion).vec[4]; + iVector[5] = vectors.at(lVersion).vec[5]; + iVector[6] = vectors.at(lVersion).vec[6]; + iVector[7] = vectors.at(lVersion).vec[7]; + + memcpy(&iVector[8], &iVector[0], 8); + + key[0] = keys.at(lVersion).key[0]; + key[1] = keys.at(lVersion).key[1]; + key[2] = keys.at(lVersion).key[2]; + key[3] = keys.at(lVersion).key[3]; + key[4] = keys.at(lVersion).key[4]; + key[5] = keys.at(lVersion).key[5]; + key[6] = keys.at(lVersion).key[6]; + key[7] = keys.at(lVersion).key[7]; + memcpy(&key[8], &key[0], 8); + memcpy(&key[16], &key[6], 8); + memcpy(&key[24], &key[12], 8); +} + + diff --git a/src/common/licenceELC2.d b/src/common/licenceELC2.d new file mode 100644 index 0000000..6e9dae4 --- /dev/null +++ b/src/common/licenceELC2.d @@ -0,0 +1,3 @@ +src/common/licenceELC2.o: src/common/licenceELC2.cpp \ + include/common/licenceELC2.h include/common/licenceCommon.h \ + include/common/SDCard.h include/common/utils.h diff --git a/src/common/licenceELC2.o b/src/common/licenceELC2.o new file mode 100644 index 0000000..841083c Binary files /dev/null and b/src/common/licenceELC2.o differ diff --git a/src/utils.cpp b/src/common/utils.cpp similarity index 100% rename from src/utils.cpp rename to src/common/utils.cpp diff --git a/src/common/utils.d b/src/common/utils.d new file mode 100644 index 0000000..e1035c5 --- /dev/null +++ b/src/common/utils.d @@ -0,0 +1,28 @@ +src/common/utils.o: src/common/utils.cpp include/openssl/conf.h \ + include/openssl/macros.h include/openssl/opensslconf.h \ + include/openssl/configuration.h include/openssl/opensslv.h \ + include/openssl/bio.h include/openssl/e_os2.h include/openssl/crypto.h \ + include/openssl/safestack.h include/openssl/stack.h \ + include/openssl/types.h include/openssl/cryptoerr.h \ + include/openssl/symhacks.h include/openssl/cryptoerr_legacy.h \ + include/openssl/core.h include/openssl/bioerr.h include/openssl/lhash.h \ + include/openssl/conferr.h include/openssl/conftypes.h \ + include/openssl/ssl.h include/openssl/comp.h include/openssl/comperr.h \ + include/openssl/x509.h include/openssl/buffer.h \ + include/openssl/buffererr.h include/openssl/evp.h \ + include/openssl/core_dispatch.h include/openssl/evperr.h \ + include/openssl/params.h include/openssl/bn.h include/openssl/bnerr.h \ + include/openssl/objects.h include/openssl/obj_mac.h \ + include/openssl/asn1.h include/openssl/asn1err.h \ + include/openssl/objectserr.h include/openssl/ec.h \ + include/openssl/ecerr.h include/openssl/rsa.h include/openssl/rsaerr.h \ + include/openssl/dsa.h include/openssl/dh.h include/openssl/dherr.h \ + include/openssl/dsaerr.h include/openssl/sha.h include/openssl/x509err.h \ + include/openssl/x509_vfy.h include/openssl/pkcs7.h \ + include/openssl/pkcs7err.h include/openssl/http.h include/openssl/pem.h \ + include/openssl/pemerr.h include/openssl/hmac.h include/openssl/async.h \ + include/openssl/asyncerr.h include/openssl/ct.h include/openssl/cterr.h \ + include/openssl/sslerr.h include/openssl/sslerr_legacy.h \ + include/openssl/prov_ssl.h include/openssl/ssl2.h include/openssl/ssl3.h \ + include/openssl/tls1.h include/openssl/dtls1.h include/openssl/srtp.h \ + include/common/utils.h diff --git a/src/common/utils.o b/src/common/utils.o new file mode 100644 index 0000000..1d5497e Binary files /dev/null and b/src/common/utils.o differ diff --git a/src/generator/LicenceGenerator.cpp b/src/generator/LicenceGenerator.cpp new file mode 100644 index 0000000..09e6250 --- /dev/null +++ b/src/generator/LicenceGenerator.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include "utils.h" +#include "pugixml.hpp" +#include + +LicenceGenerator::LicenceGenerator() +{ +} + +LicenceGenerator::LicenceGenerator(string uid, string cid, string csd, string dataFileName) +{ + + if (dataFileName.empty()) throw LicenceException((int)GeneralError::ParamMissing, "Missing par: dataFileName"); + + if (uid.empty() == false) + { + this->licData.uid = uid; + } + else + { + if (cid.empty()) throw LicenceException((int)GeneralError::ParamMissing, "Missing par: CID"); + if (csd.empty()) throw LicenceException((int)GeneralError::ParamMissing, "Missing par: CSD"); + } + + this->configFileName = dataFileName; + this->licData.cid = cid; + this->licData.csd = csd; + + this->argumentsCorrect = true; + + processInputConfiguration(); +} + +bool LicenceGenerator::processInputConfiguration() +{ + + string fullFile = getCompletePath(this->configFileName); + const int fileNameLength = fullFile.length(); + char fileName[fileNameLength] = {}; + getCharsFromString(fullFile, fileName, fileNameLength); + +#ifdef WINDOWS + pugi::xml_parse_result result = doc.load_file("licData.xml"); +#else + pugi::xml_parse_result result = doc.load_file(fileName); +#endif + + if (result) + { + const char *dataRootName = doc.child("data") ? "data" : "licence"; // kompatibilita s verzí, která měla ještě root "data" + Mapping mapping; + + if (atoi(&doc.child(dataRootName).attribute("xmlVersion").value()[0]) != XML_VERSION) + { + throw LicenceException((int)GeneralError::IvanlidParam, "Invalid param: XML VERSION"); + } + + this->licData.doc = &doc; + + string licType = doc.child(dataRootName).child("licenceType").child_value(); + if (!licType.empty()) + { + this->licIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV; + } + else + { + throw LicenceException((int)GeneralError::ParamMissing, "ERROR MISSING licenceType"); + } + + this->licIdentification.licenceVersion = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); + this->licIdentification.revision = doc.child(dataRootName).attribute("revision").value()[0]; + this->licIdentification.licenceIndex = atoi(&doc.child(dataRootName).child("licenceType").attribute("licenceIndex").value()[0]); + this->licIdentification.licElcType = (ELCType)atoi(&doc.child(dataRootName).attribute("elc").value()[0]); + + this->licIdentification.licCompatibility = atoi(&doc.child(dataRootName).attribute("compatibility").value()[0]); + + string plcType = doc.child(dataRootName).child("plcType").child_value(); + if (!plcType.empty()) + { + this->licIdentification.licPlcType = mapping.licMapPlcType[plcType]; + } + } + else + { + throw LicenceException((int)GeneralError::FileOpenError, "Unable to open the config file."); + } + + + return true; +} + +void LicenceGenerator::createLicenceFile() +{ + switch (this->licIdentification.licElcType) + { + case ELCType::ELC1: + createLicenceELC1(); + break; + + case ELCType::ELC2: + createLicenceELC2(); + break; + + case ELCType::ELC3: + createLicenceELC3(); + break; + } +} + +void LicenceGenerator::createLicenceELC1() // přejmenvat na ELC1 +{ + LicenceELC11 plcWriteLicence = LicenceELC11(this->licData.cid, this->licData.csd, "file", this->configFileName); + plcWriteLicence.createLicence(); +} + +void LicenceGenerator::createLicenceELC2() +{ + switch (this->licIdentification.licCompatibility) + { + case 1: + // LicenceELC21 licence = LicenceELC21(this->licData); + // licence.createLicence(); + break; + } +} + +void LicenceGenerator::createLicenceELC3() +{ + switch (this->licIdentification.licCompatibility) + { + case 1: + //LicenceELC31 licence = LicenceELC31(this->licData); + //licence.createLicence(); + break; + } +} + +LicenceGenerator::~LicenceGenerator() +{ +} diff --git a/src/generator/LicenceGenerator.d b/src/generator/LicenceGenerator.d new file mode 100644 index 0000000..ba7adcd --- /dev/null +++ b/src/generator/LicenceGenerator.d @@ -0,0 +1,7 @@ +src/generator/LicenceGenerator.o: src/generator/LicenceGenerator.cpp \ + include/generator/licenceGenerator.h include/generator/pugixml.hpp \ + include/generator/pugiconfig.hpp include/common/licenceCommon.h \ + include/licenceELC11.h include/common/SDCard.h include/licenceELC21.h \ + include/common/utils.h include/generator/pugixml.hpp \ + include/licenceELC31.h include/generator/licGenELC1.h \ + include/generator/licGenELC2.h include/generator/licGenELC3.h diff --git a/src/generator/LicenceGenerator.o b/src/generator/LicenceGenerator.o new file mode 100644 index 0000000..3dc1780 Binary files /dev/null and b/src/generator/LicenceGenerator.o differ diff --git a/src/generator/licGenELC1.cpp b/src/generator/licGenELC1.cpp new file mode 100644 index 0000000..e2880cf --- /dev/null +++ b/src/generator/licGenELC1.cpp @@ -0,0 +1,6 @@ +#include "licGenELC1.h" + +namespace Generator +{ + +} \ No newline at end of file diff --git a/src/generator/licGenELC1.d b/src/generator/licGenELC1.d new file mode 100644 index 0000000..cf73ea1 --- /dev/null +++ b/src/generator/licGenELC1.d @@ -0,0 +1,2 @@ +src/generator/licGenELC1.o: src/generator/licGenELC1.cpp \ + include/generator/licGenELC1.h diff --git a/src/generator/licGenELC1.o b/src/generator/licGenELC1.o new file mode 100644 index 0000000..afbc68a Binary files /dev/null and b/src/generator/licGenELC1.o differ diff --git a/src/generator/licGenELC2.cpp b/src/generator/licGenELC2.cpp new file mode 100644 index 0000000..fa7ede0 --- /dev/null +++ b/src/generator/licGenELC2.cpp @@ -0,0 +1,6 @@ +#include "licGenELC2.h" + +namespace Generator +{ + +} diff --git a/src/generator/licGenELC2.d b/src/generator/licGenELC2.d new file mode 100644 index 0000000..a466f01 --- /dev/null +++ b/src/generator/licGenELC2.d @@ -0,0 +1,2 @@ +src/generator/licGenELC2.o: src/generator/licGenELC2.cpp \ + include/generator/licGenELC2.h diff --git a/src/generator/licGenELC2.o b/src/generator/licGenELC2.o new file mode 100644 index 0000000..b033ffb Binary files /dev/null and b/src/generator/licGenELC2.o differ diff --git a/src/generator/licGenELC3.cpp b/src/generator/licGenELC3.cpp new file mode 100644 index 0000000..196b417 --- /dev/null +++ b/src/generator/licGenELC3.cpp @@ -0,0 +1,8 @@ +#include "licGenELC3.h" + +namespace Generator +{ + +} + + diff --git a/src/generator/licGenELC3.d b/src/generator/licGenELC3.d new file mode 100644 index 0000000..7facddc --- /dev/null +++ b/src/generator/licGenELC3.d @@ -0,0 +1,2 @@ +src/generator/licGenELC3.o: src/generator/licGenELC3.cpp \ + include/generator/licGenELC3.h diff --git a/src/generator/licGenELC3.o b/src/generator/licGenELC3.o new file mode 100644 index 0000000..7d3e288 Binary files /dev/null and b/src/generator/licGenELC3.o differ diff --git a/src/pugixml.cpp b/src/generator/pugixml.cpp similarity index 100% rename from src/pugixml.cpp rename to src/generator/pugixml.cpp diff --git a/src/generator/pugixml.d b/src/generator/pugixml.d new file mode 100644 index 0000000..fbb73ae --- /dev/null +++ b/src/generator/pugixml.d @@ -0,0 +1,2 @@ +src/generator/pugixml.o: src/generator/pugixml.cpp \ + include/generator/pugixml.hpp include/generator/pugiconfig.hpp diff --git a/src/generator/pugixml.o b/src/generator/pugixml.o new file mode 100644 index 0000000..a9643b0 Binary files /dev/null and b/src/generator/pugixml.o differ diff --git a/src/licenceELC21.cpp b/src/licenceELC21.cpp index 91445d6..3ce0ffb 100644 --- a/src/licenceELC21.cpp +++ b/src/licenceELC21.cpp @@ -1,6 +1,8 @@ #include "licenceELC21.h" #include "utils.h" #include "SDCard.h" +#include "pugixml.hpp" + LicenceELC21::LicenceELC21() { @@ -27,8 +29,8 @@ bool LicenceELC21::createLicence() getHeader(); sdCard = SDCard(lData.cid, lData.csd); - if (sdCard.isLoaded == false) - cerr << "SD card read error." << endl; + if (sdCard.isLoaded == false) throw LicenceException((int)Error21::SDCardReadError, "Chyba při čtení SD karty, cesta: " + cid_cdsPath); + this->licBody.licenceIdentHeader.cardSize = sdCard.cardData.cardSize; this->licBody.licenceIdentHeader.serialNumber = sdCard.cardData.serialNumber; this->licBody.licenceIdentHeader.licItemCount = this->licBody.privateContent.dataItems.size(); @@ -107,8 +109,7 @@ bool LicenceELC21::createLicence() // Check if the file is open if (!outputFile.is_open()) { - std::cerr << "Error opening file: " << licfileName << std::endl; - return false; + throw LicenceException((int)GeneralError::FileOpenError, "Chyba při zakládání souboru licence: " + cid_cdsPath); } std::copy(publicContent.cbegin(), publicContent.cend(), std::ostream_iterator(outputFile)); @@ -129,8 +130,7 @@ bool LicenceELC21::readLicence(LicenceInfoGeneral * licences) string licFileName = getLicenceName(); string licFilePath = this->licenceFilePath+licFileName; - cout << licFilePath; - + vector content; if (readFile(licFilePath, content) == false) { diff --git a/src/reader/LicenceReader.cpp b/src/reader/LicenceReader.cpp new file mode 100644 index 0000000..6ba77b9 --- /dev/null +++ b/src/reader/LicenceReader.cpp @@ -0,0 +1,262 @@ +#include + +ELCType licElcType = ELCType::ELC2; +LicenceType licLicenceType; +PlcType licPlcType; + +uint8_t licenceVersion = 1; // verze licence, určitě kodování, pojmenování souborů +uint8_t licenceIndex = 0; +uint8_t revision; +uint8_t licCompatibility = 1; // identikator hlavního ELC + +LicenceReader::LicenceReader() +{ +} + +// LicenceReader::LicenceReader(string uid, string cid, string csd, string dataFileName) +// { +// try +// { +// if (dataFileName.empty()) +// throw std::runtime_error("Missing dataFileName par."); + +// if (uid.empty() == false) +// { +// this->licData.uid = uid; +// } +// else +// { +// if (cid.empty()) +// throw std::runtime_error("Missing CID par."); +// if (csd.empty()) +// throw std::runtime_error("Missing CSD par."); +// } +// } +// catch (const std::exception &e) +// { +// this->argumentsCorrect = false; +// std::cerr << "error: " << e.what() << endl; +// return; +// } + +// this->configFileName = dataFileName; +// this->licData.cid = cid; +// this->licData.csd = csd; + +// this->argumentsCorrect = true; + +// if (processInputConfiguration() == false) +// { +// cerr << "Chyba: " << error.message << endl; +// } +// } + +// LicenceReader::LicenceReader(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility) +// { +// this->licIdentification.licElcType = (ELCType)elcType; +// this->licIdentification.licLicenceType = (LicenceType)licenceType; +// this->licIdentification.licenceVersion = licenceVersion; +// this->licIdentification.licenceIndex = licenceIndex; +// this->licIdentification.licCompatibility = compatibility; +// } + +/// @brief inicializace generátoru a načtení licenčních bodů +/// @param elcType +/// @param licenceType +/// @param licenceVersion +/// @param licenceIndex +/// @param compatibility +/// @return +bool LicenceReader::initread(int elcType, InitStructure &initStructure) // TOTO +{ + try + { + elcSwitchType = elcType * 10 + licCompatibility; + + if (init(elcType, initStructure)) + { + switch (elcSwitchType) + { + case 11: + { // old eoseov + LicenceELC11 licence = LicenceELC11(this->licIdentification); + this->licence11 = &licence; + licence.cid_cdsPath = initStructure.cid_csd_filePath; + licence.licenceFilePath = initStructure.licenceFilePath; + this->licence11->readLicence(&this->licenceInfo); + break; + } + case 21: + { + Reader::Licence2 licenceELC2 = Reader::Licence2(this->licIdentification); + this->licence2 = &licenceELC2; + licenceELC2.cid_cdsPath = initStructure.cid_csd_filePath; + licenceELC2.licenceFilePath = initStructure.licenceFilePath; + this->licence2->readLicence(&this->licenceInfo); + + break; + } + case 31: + { + LicenceELC31 licence = LicenceELC31(this->licIdentification); + break; + } + } + } + else + return false; + } + catch (const LicenceException &ex) + { + error.code = ex.getErrorCode(); + error.message = ex.getErrorMessage(); + return false; + } + + return true; // TODO testy +} + +bool LicenceReader::init(int elcType, InitStructure &initStructure) +{ + try + { + if (elcType > 3) + { + error.code = (int)GeneralError::ELCNotImplemented; + error.message = "ELC není implementováno."; + throw LicenceException((int)GeneralError::ELCNotImplemented, error.message); + } + + this->licIdentification.licElcType = (ELCType)elcType; + this->licIdentification.licLicenceType = (LicenceType)initStructure.licenceType; + this->licIdentification.licenceVersion = licenceVersion; + this->licIdentification.licenceIndex = licenceIndex; + this->licIdentification.licCompatibility = initStructure.compatibility; + } + catch (const LicenceException &ex) + { + error.code = ex.getErrorCode(); + error.message = ex.getErrorMessage(); + return false; + } + + elcSwitchType = elcType * 10 + licCompatibility; + + return true; // TODO testy +} + +bool LicenceReader::getLicenceItemInfo(int protocolId, void *returnItemStructure) +{ + try + { + switch (elcSwitchType) + { + case 11: + { // old eoseov + LicenceItem11 *resultPtr = static_cast(returnItemStructure); + resultPtr->protocolId = protocolId; + if (this->licenceInfo.licences.count(protocolId)) + resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); + else + resultPtr->dataPointsCount = 0; + break; + } + case 21: + { + + LicenceItem21 *resultPtr = static_cast(returnItemStructure); + resultPtr->protocolId = protocolId; // protocolId; + if (this->licenceInfo.licences.count(protocolId)) + resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); + else + resultPtr->dataPointsCount = 0; + break; + } + case 31: + { + LicenceELC31 licence = LicenceELC31(this->licIdentification); + break; + } + default: + { + error.code = -1; + error.message = "Nepodařilo se identifikovat licenci"; + return false; + } + } + } + catch (const LicenceException &ex) + { + error.code = ex.getErrorCode(); + error.message = ex.getErrorMessage(); + return false; + } + + return true; +} + +bool LicenceReader::getLicenceInfo(void *returnStructure) +{ + // const type_info& ti1 = typeid(returnStructure); + // cout << "TTTYP: " << ti1.name() << endl; + switch (elcSwitchType) + { + case 11: + { // old eoseov + break; + } + case 21: + { + try + { + Reader::Licence2 licenceELC2 = Reader::Licence2(this->licIdentification); + licenceELC2.readLicence(&this->licenceInfo); + return licenceELC2.getLicenceInfo(returnStructure); + } + catch (const LicenceException &ex) + { + error.code = ex.getErrorCode(); + error.message = ex.getErrorMessage(); + return false; + } + + // LicenceELC21 licence = LicenceELC21(this->licIdentification); + // try + // { + // licence.readLicence(&this->licenceInfo); + // return licence.getLicenceInfo(returnStructure); + // } + // catch (const LicenceException &ex) + // { + // error.code = ex.getErrorCode(); + // error.message = ex.getErrorMessage(); + // return false; + // } + break; + } + case 31: + { + LicenceELC31 licence = LicenceELC31(this->licIdentification); + // try + // { + // licence.readLicence(); + // return licence.getLicenceInfo(returnStructure); + // } + // catch (const LicenceException &ex) + // { + // error.code = ex.getErrorCode(); + // error.message = ex.getErrorMessage(); + // return false; + // } + break; + } + } + + error.code = -1; + error.message = "Nepodařilo se identifikovat licenci"; + return false; +} + +LicenceReader::~LicenceReader() +{ +} diff --git a/src/reader/LicenceReader.d b/src/reader/LicenceReader.d new file mode 100644 index 0000000..3983730 --- /dev/null +++ b/src/reader/LicenceReader.d @@ -0,0 +1,8 @@ +src/reader/LicenceReader.o: src/reader/LicenceReader.cpp \ + include/reader/licenceReader.h include/common/utils.h \ + include/licenceELC11.h include/common/licenceCommon.h \ + include/common/SDCard.h include/licenceELC31.h \ + include/generator/pugixml.hpp include/generator/pugiconfig.hpp \ + include/reader/licReaderELC1.h include/reader/licReaderELC2.h \ + include/common/licenceELC2.h include/common/licenceCommon.h \ + include/common/SDCard.h include/reader/licReaderELC3.h diff --git a/src/reader/LicenceReader.o b/src/reader/LicenceReader.o new file mode 100644 index 0000000..86125fc Binary files /dev/null and b/src/reader/LicenceReader.o differ diff --git a/src/reader/licReaderELC1.cpp b/src/reader/licReaderELC1.cpp new file mode 100644 index 0000000..209047c --- /dev/null +++ b/src/reader/licReaderELC1.cpp @@ -0,0 +1,6 @@ +#include "licReaderELC1.h" + +namespace Reader +{ + +} \ No newline at end of file diff --git a/src/reader/licReaderELC1.d b/src/reader/licReaderELC1.d new file mode 100644 index 0000000..eb26f09 --- /dev/null +++ b/src/reader/licReaderELC1.d @@ -0,0 +1,2 @@ +src/reader/licReaderELC1.o: src/reader/licReaderELC1.cpp \ + include/reader/licReaderELC1.h diff --git a/src/reader/licReaderELC1.o b/src/reader/licReaderELC1.o new file mode 100644 index 0000000..f7d0e7c Binary files /dev/null and b/src/reader/licReaderELC1.o differ diff --git a/src/reader/licReaderELC2.cpp b/src/reader/licReaderELC2.cpp new file mode 100644 index 0000000..ccad2d4 --- /dev/null +++ b/src/reader/licReaderELC2.cpp @@ -0,0 +1,118 @@ +#include "licReaderELC2.h" + +namespace Reader +{ + + Licence2::Licence2() + { + } + + Licence2::~Licence2() + { + } + + Licence2::Licence2(LicenceIdentification &licIdentification) : LicenceELC2(licIdentification) + { + } + + /// @brief načte licence do obecné struktury ELC2 + /// @param licences + /// @return + bool Licence2::readLicence(LicenceInfoGeneral *licences) + { + + // sdCard = SDCard("9f544930303030300000000b47015423", "400e00325b5900003a0d7f800a40008d"); + sdCard = SDCard(this->cid_cdsPath); + if (sdCard.isLoaded == false) + throw LicenceException((int)Error::SDCardReadError, "Chyba při čtení SD karty, cesta: " + cid_cdsPath); + + string licFileName = getLicenceName(); + string licFilePath = this->licenceFilePath + licFileName; + + vector content; + if (readFile(licFilePath, content) == false) + { + throw LicenceException((int)GeneralError::FileOpenError, "Chyba otevření souboru licence: " + licFilePath); + } + + this->licBody.licId.licIdent[0] = content[0]; + this->licBody.licId.licIdent[1] = content[1]; + this->licBody.licId.licIdent[2] = content[2]; + this->licBody.licId.licIdent[3] = content[3]; + this->licBody.licId.licIdent[4] = content[4]; + + this->licBody.licenceIdentHeader.licenceType = content[5]; + this->licBody.licenceIdentHeader.licenceTypeVersion = content[6]; + this->licBody.licenceIdentHeader.licenceIndex = content[7]; + this->licBody.licenceIdentHeader.compatibilityVersion = content[8]; + this->licBody.licenceIdentHeader.licItemCount = content[9]; + this->licBody.licenceIdentHeader.publicHeaderLength = bytesToWord(content[10], content[11]); + this->licBody.licenceIdentHeader.cardSize = bytesToWord(content[12], content[13]); + this->licBody.licenceIdentHeader.serialNumber = bytesToDword(content[14], content[15], content[16], content[17]); + + //~~~uint16_t crcPublic = bytesToWord(content[18 + licBody.licenceIdentHeader.publicHeaderLength], content[19 + licBody.licenceIdentHeader.publicHeaderLength]); + + int elcVersion = (int)licBody.licId.licIdent[3] - 48; + + if (elcVersion != (int)this->lIdentification.licElcType) + { + throw LicenceException((int)GeneralError::ELCMismatch, "Nesouhlasí ELC."); + } + + vector encryptedPart(content.begin() + licBody.licenceIdentHeader.publicHeaderLength + 20, content.begin() + content.size()); + + vector privateContentDecrypted; + privateContentDecrypted = decryptPrivateContent(encryptedPart); + + LicenceBody licBodyDecrypted; + licBodyDecrypted.licenceIdentHeader.licenceType = privateContentDecrypted[0]; + licBodyDecrypted.licenceIdentHeader.licenceTypeVersion = privateContentDecrypted[1]; + licBodyDecrypted.licenceIdentHeader.licenceIndex = privateContentDecrypted[2]; + licBodyDecrypted.licenceIdentHeader.compatibilityVersion = privateContentDecrypted[3]; + licBodyDecrypted.licenceIdentHeader.licItemCount = privateContentDecrypted[4]; + licBodyDecrypted.licenceIdentHeader.publicHeaderLength = bytesToWord(privateContentDecrypted[5], privateContentDecrypted[6]); + licBodyDecrypted.licenceIdentHeader.cardSize = bytesToWord(privateContentDecrypted[7], privateContentDecrypted[8]); + licBodyDecrypted.licenceIdentHeader.serialNumber = bytesToDword(privateContentDecrypted[9], privateContentDecrypted[10], privateContentDecrypted[11], privateContentDecrypted[12]); + + //~~~CRC + + if (licBodyDecrypted.licenceIdentHeader.licItemCount != this->licBody.licenceIdentHeader.licItemCount) + { + throw LicenceException((int)LicenceELC2::Error::ItemsCountMismatch, mapErrors.at(Error::ItemsCountMismatch)); + } + + int index = 13; + for (int i = 0; i < this->licBody.licenceIdentHeader.licItemCount; i++) + { + licDataItem item; + item.protoId = bytesToWord(privateContentDecrypted[index], privateContentDecrypted[index + 1]); + item.licCount = bytesToWord(privateContentDecrypted[index + 2], privateContentDecrypted[index + 3]); + index += sizeof(licDataItem); + this->licBody.privateContent.dataItems.push_back(item); + this->licenceInfo.licences.insert(pair(item.protoId, item.licCount)); + licences->licences.insert(pair(item.protoId, item.licCount)); + } + return true; + } + + bool Licence2::getLicenceInfo(void *returnStructure) +{ + if (returnStructure != nullptr) + { + LicenceInfo21 *resultPtr = static_cast(returnStructure); + for (auto item : this->licBody.privateContent.dataItems) + { + resultPtr->licences.insert(pair(item.protoId, item.licCount)); + } + } + else + { + errorMessage.code = 1; + errorMessage.message = "Error: Null pointer!"; + return false; + } + + return true; +} + +} \ No newline at end of file diff --git a/src/reader/licReaderELC2.d b/src/reader/licReaderELC2.d new file mode 100644 index 0000000..73c8317 --- /dev/null +++ b/src/reader/licReaderELC2.d @@ -0,0 +1,5 @@ +src/reader/licReaderELC2.o: src/reader/licReaderELC2.cpp \ + include/reader/licReaderELC2.h include/common/utils.h \ + include/common/licenceCommon.h include/common/licenceELC2.h \ + include/common/licenceCommon.h include/common/SDCard.h \ + include/common/SDCard.h diff --git a/src/reader/licReaderELC2.o b/src/reader/licReaderELC2.o new file mode 100644 index 0000000..2d7c212 Binary files /dev/null and b/src/reader/licReaderELC2.o differ diff --git a/src/reader/licReaderELC3.cpp b/src/reader/licReaderELC3.cpp new file mode 100644 index 0000000..6c0e341 --- /dev/null +++ b/src/reader/licReaderELC3.cpp @@ -0,0 +1,7 @@ +#include "licReaderELC3.h" + +namespace Reader +{ +} + + diff --git a/src/reader/licReaderELC3.d b/src/reader/licReaderELC3.d new file mode 100644 index 0000000..5e6dbc8 --- /dev/null +++ b/src/reader/licReaderELC3.d @@ -0,0 +1,2 @@ +src/reader/licReaderELC3.o: src/reader/licReaderELC3.cpp \ + include/reader/licReaderELC3.h diff --git a/src/reader/licReaderELC3.o b/src/reader/licReaderELC3.o new file mode 100644 index 0000000..c02dcff Binary files /dev/null and b/src/reader/licReaderELC3.o differ