diff --git a/src/CreateLicence.cpp b/src/CreateLicence.cpp index 9e7a44a..f17a03d 100644 --- a/src/CreateLicence.cpp +++ b/src/CreateLicence.cpp @@ -8,7 +8,7 @@ /// @param argc /// @param argv parametry pro generování licence /// @return -int main(int argc, char *argv[]) +int main5(int argc, char *argv[]) { unordered_map arguments = getArguments(argc, argv); try diff --git a/src/ReadLicence.cpp b/src/ReadLicence.cpp index 99a8e68..9eea4bf 100644 --- a/src/ReadLicence.cpp +++ b/src/ReadLicence.cpp @@ -7,7 +7,7 @@ /// @param argc /// @param argv parametry pro generování licence /// @return -int main6() +int main() { try diff --git a/src/common/SDCard.d b/src/common/SDCard.d deleted file mode 100644 index d6aa52c..0000000 --- a/src/common/SDCard.d +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 030e0fd..0000000 Binary files a/src/common/SDCard.o and /dev/null differ diff --git a/src/common/licenceELC1.d b/src/common/licenceELC1.d deleted file mode 100644 index ee29b73..0000000 --- a/src/common/licenceELC1.d +++ /dev/null @@ -1,3 +0,0 @@ -src/common/licenceELC1.o: src/common/licenceELC1.cpp \ - include/common/licenceELC1.h include/common/licenceCommon.h \ - include/common/SDCard.h include/common/utils.h diff --git a/src/common/licenceELC1.o b/src/common/licenceELC1.o deleted file mode 100644 index 972db80..0000000 Binary files a/src/common/licenceELC1.o and /dev/null differ diff --git a/src/common/licenceELC2.d b/src/common/licenceELC2.d deleted file mode 100644 index 6e9dae4..0000000 --- a/src/common/licenceELC2.d +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 3a2b491..0000000 Binary files a/src/common/licenceELC2.o and /dev/null differ diff --git a/src/common/utils.cpp b/src/common/utils.cpp index efbdf67..6a109d2 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -315,7 +315,6 @@ string getCompletePath(string fileName) #ifdef WINDOWS return fileName; #else - char path[PATH_MAX + 1] = {}; ssize_t length = readlink("/proc/self/exe", path, PATH_MAX); path[length] = '\0'; diff --git a/src/common/utils.d b/src/common/utils.d deleted file mode 100644 index e1035c5..0000000 --- a/src/common/utils.d +++ /dev/null @@ -1,28 +0,0 @@ -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 deleted file mode 100644 index 29a406e..0000000 Binary files a/src/common/utils.o and /dev/null differ diff --git a/src/generator/LicenceGenerator.d b/src/generator/LicenceGenerator.d deleted file mode 100644 index e83f4d9..0000000 --- a/src/generator/LicenceGenerator.d +++ /dev/null @@ -1,8 +0,0 @@ -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/generator/licGenELC1.h include/common/utils.h \ - include/common/licenceELC1.h include/common/licenceCommon.h \ - include/common/SDCard.h include/common/SDCard.h \ - include/generator/licGenELC2.h include/common/licenceELC2.h \ - include/generator/licGenELC3.h include/generator/pugixml.hpp diff --git a/src/generator/LicenceGenerator.o b/src/generator/LicenceGenerator.o deleted file mode 100644 index 912fb08..0000000 Binary files a/src/generator/LicenceGenerator.o and /dev/null differ diff --git a/src/generator/licGenELC1.d b/src/generator/licGenELC1.d deleted file mode 100644 index c04b50d..0000000 --- a/src/generator/licGenELC1.d +++ /dev/null @@ -1,6 +0,0 @@ -src/generator/licGenELC1.o: src/generator/licGenELC1.cpp \ - include/generator/licGenELC1.h include/common/utils.h \ - include/common/licenceCommon.h include/common/licenceELC1.h \ - include/common/licenceCommon.h include/common/SDCard.h \ - include/common/SDCard.h include/generator/pugixml.hpp \ - include/generator/pugiconfig.hpp diff --git a/src/generator/licGenELC1.o b/src/generator/licGenELC1.o deleted file mode 100644 index b9cc07f..0000000 Binary files a/src/generator/licGenELC1.o and /dev/null differ diff --git a/src/generator/licGenELC2.d b/src/generator/licGenELC2.d deleted file mode 100644 index 4b8fdd4..0000000 --- a/src/generator/licGenELC2.d +++ /dev/null @@ -1,6 +0,0 @@ -src/generator/licGenELC2.o: src/generator/licGenELC2.cpp \ - include/generator/licGenELC2.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 include/generator/pugixml.hpp \ - include/generator/pugiconfig.hpp diff --git a/src/generator/licGenELC2.o b/src/generator/licGenELC2.o deleted file mode 100644 index 40a68ef..0000000 Binary files a/src/generator/licGenELC2.o and /dev/null differ diff --git a/src/generator/licGenELC3.d b/src/generator/licGenELC3.d deleted file mode 100644 index 7facddc..0000000 --- a/src/generator/licGenELC3.d +++ /dev/null @@ -1,2 +0,0 @@ -src/generator/licGenELC3.o: src/generator/licGenELC3.cpp \ - include/generator/licGenELC3.h diff --git a/src/generator/licGenELC3.o b/src/generator/licGenELC3.o deleted file mode 100644 index ba95669..0000000 Binary files a/src/generator/licGenELC3.o and /dev/null differ diff --git a/src/generator/pugixml.d b/src/generator/pugixml.d deleted file mode 100644 index fbb73ae..0000000 --- a/src/generator/pugixml.d +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 12d8d7d..0000000 Binary files a/src/generator/pugixml.o and /dev/null differ diff --git a/src/reader/LicenceReader.d b/src/reader/LicenceReader.d deleted file mode 100644 index 32a9821..0000000 --- a/src/reader/LicenceReader.d +++ /dev/null @@ -1,7 +0,0 @@ -src/reader/LicenceReader.o: src/reader/LicenceReader.cpp \ - include/reader/licenceReader.h include/common/utils.h \ - include/reader/licReaderELC1.h include/common/licenceCommon.h \ - include/common/licenceELC1.h include/common/licenceCommon.h \ - include/common/SDCard.h include/common/SDCard.h \ - include/reader/licReaderELC2.h include/common/licenceELC2.h \ - include/reader/licReaderELC3.h diff --git a/src/reader/LicenceReader.o b/src/reader/LicenceReader.o deleted file mode 100644 index 122f20a..0000000 Binary files a/src/reader/LicenceReader.o and /dev/null differ diff --git a/src/reader/licReaderELC1.d b/src/reader/licReaderELC1.d deleted file mode 100644 index c3f292a..0000000 --- a/src/reader/licReaderELC1.d +++ /dev/null @@ -1,5 +0,0 @@ -src/reader/licReaderELC1.o: src/reader/licReaderELC1.cpp \ - include/reader/licReaderELC1.h include/common/utils.h \ - include/common/licenceCommon.h include/common/licenceELC1.h \ - include/common/licenceCommon.h include/common/SDCard.h \ - include/common/SDCard.h diff --git a/src/reader/licReaderELC1.o b/src/reader/licReaderELC1.o deleted file mode 100644 index bb240a4..0000000 Binary files a/src/reader/licReaderELC1.o and /dev/null differ diff --git a/src/reader/licReaderELC2.d b/src/reader/licReaderELC2.d deleted file mode 100644 index 73c8317..0000000 --- a/src/reader/licReaderELC2.d +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index dcd1f62..0000000 Binary files a/src/reader/licReaderELC2.o and /dev/null differ diff --git a/src/reader/licReaderELC3.d b/src/reader/licReaderELC3.d deleted file mode 100644 index 5e6dbc8..0000000 --- a/src/reader/licReaderELC3.d +++ /dev/null @@ -1,2 +0,0 @@ -src/reader/licReaderELC3.o: src/reader/licReaderELC3.cpp \ - include/reader/licReaderELC3.h diff --git a/src/reader/licReaderELC3.o b/src/reader/licReaderELC3.o deleted file mode 100644 index 7e2c3ab..0000000 Binary files a/src/reader/licReaderELC3.o and /dev/null differ diff --git a/zdenda/Makefile b/zdenda/Makefile index 1dffacf..0dfe1f8 100644 --- a/zdenda/Makefile +++ b/zdenda/Makefile @@ -19,10 +19,10 @@ LFLAGS = OUTPUT := output # define source directory -SRC := src src/common src/reader +SRC := src src/common src/reader src/generator # define include directory -INCLUDE := include include/common include/reader +INCLUDE := include include/common include/reader include/generator # define lib directory LIB := lib diff --git a/zdenda/include/common/SDCard.h b/zdenda/include/common/SDCard.h new file mode 100644 index 0000000..dab8b1c --- /dev/null +++ b/zdenda/include/common/SDCard.h @@ -0,0 +1,70 @@ +#ifndef SDCARD_H_ +#define SDCARD_H_ + +#define CID_LENGTH 32 +#define CSD_LENGTH 32 + +using namespace std; + +typedef uint32_t DWORD; +typedef uint16_t WORD; +typedef uint8_t BYTE; +typedef uint32_t UDINT; +typedef uint32_t DATE; +typedef uint8_t USINT; +typedef int32_t DINT; +typedef uint16_t UINT; +typedef int16_t INT; + +const BYTE cnibblescount = 32; + +const string cidFilePath = ""; + +class SDCard +{ + + struct SDCardData + { + uint8_t isCorrectLoad = 0; // SD karta je korektne nactena //bool vs byte ? + uint8_t CID_nibble[32] = {0}; // surova data CID ze souboru + uint8_t CID[16] = {0}; // prekodovane CID informace + uint8_t manufacturerID = 0; // ID vyrobce 1B -> Byte 15 + char oemID[2] = {}; // ID aplikace/oem 2B -> Byte 13-14 // oemID: !!STRING(3); + char name[5] = {}; // jmeno produktu 5B -> Byte 8-12 !!STRING(6); + uint8_t productRevision_hw = 0; + uint8_t productRevision_sw = 0; + float productRevision = 0; // revize produktu 1B [bcd sh] -> Byte 7, bity 0-3 = revize HW, bity 4-7 = revize SW + uint32_t serialNumber = 0; // seriove cislo karty 4B -> Byte 3-6 + uint16_t manufacturerDate_year = 0; + uint8_t manufacturerDate_month = 0; + char manufacturerDate[10] = {}; //: STRING(10); // datum vyroby 2B [bcd -yym] -> Byte 1-2, bity 0-3 = mesic, bity 4-11 = posledni cislice roku or roku 2000, bity 12-15 nepouzito + uint8_t CRCOK = 0; // CRC 1B -> Byte 0, bity 1-7, bit 0 je vzdy = 1 + uint8_t CSD_nibble[32] = {}; //: ARRAY [0..31] OF BYTE; // surova data CSD ze souboru + uint8_t CSD[16] = {}; //: ARRAY [0..15] OF BYTE; // prekodovane CSD informace + uint32_t cardSize = 0; //: UDINT; // velikost SD karty + uint8_t cardGSize = 0; // prepocitana velikost na GB + bool crcCorrect = false; + }; + +bool readSDCard(); +bool getCIDFromFile(); +bool getCSDFromFile(); + +public: + + SDCardData cardData; + bool isLoaded = false; + char cid[32] = {}; + char csd[32] = {}; + string filePath = ""; + string cidString = ""; //pro předání pro starý generátor + string csdString = ""; //pro předání pro starý generátor + + SDCard(); + SDCard(string cid, string csd); + SDCard(const string filesPath); + + +}; + +#endif \ No newline at end of file diff --git a/zdenda/include/common/licenceCommon.h b/zdenda/include/common/licenceCommon.h new file mode 100644 index 0000000..f432daf --- /dev/null +++ b/zdenda/include/common/licenceCommon.h @@ -0,0 +1,210 @@ +#define CRC = 1 + +#ifndef LICENCE_COMMON_H_ +#define LICENCE_COMMON_H_ + +//---------------- společná hlavička pro všechny licence ---------------- + +#include +#include +#include +#include + +using namespace std; + +#define XML_VERSION 1 +#define SOFTWARE_VERSION 1 +#define SUCCES 0; + +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, + FileOpenError = 2, + FileReadError = 3, + FileWriteError = 4, + ELCNotImplemented = 5, + LicenceTypeNotImplemented = 6, + CompatibilityTypeNotImplemented = 7, + ELCMismatch = 8, + CRCMismatch = 9, + EncryptError = 10, + DecryptError = 11, + ParamMissing = 12, + IvanlidParam = 13, + SDCardReadError = 50, + LicenceReadError = 51, + LicenceSizeMismatch = 52, + LicenceSizeCardMismatch = 53, + LicenceMismatch = 54, + LicenceCRCMismatch = 54, + ItemsCountMismatch = 61 +}; + +enum class ELCType +{ + ELC1 = 1, + ELC2 = 2, + ELC3 = 3 +}; +enum class ELCSubType +{ + DEFAULT = 1 +}; // subtype = dataVersion +enum class LicenceType +{ + EOS_EOV, + DDTS, + DRT +}; // +enum class HeaderType +{ + DEFAULT = 0 +}; ////subtype = version + +enum class EncryptionType +{ + CID_AES256 = 10, + FIX_AES256 = 20 +}; +enum class BinaryType +{ + FILE = 1, + BASE64 = 2 +}; +enum class PlcType +{ + WAGO = 1, + TECO = 2 +}; +enum class FileNameGenerationType +{ + DEFAULT = 1 +}; + +struct Mapping +{ + unordered_map licMapTypes = {{"EOV_OSV", LicenceType::EOS_EOV}, {"DDTS", LicenceType::DDTS}, {"DRT", LicenceType::DRT}}; + unordered_map licMapEncryption = {{"CID_AES256", EncryptionType::CID_AES256}, {"FIX_AES256", EncryptionType::FIX_AES256}}; + unordered_map licMapPlcType = {{"WAGO", PlcType::WAGO}, {"TECO", PlcType::TECO}}; +}; + +struct ErrorMessage +{ + int code = 0; + string message = ""; +}; + +struct LicenceIdentification +{ + ELCType licElcType = ELCType::ELC2; + LicenceType licLicenceType; + string licTypeName = ""; + PlcType licPlcType; + + uint8_t licenceVersion = 1; // verze licence, určuje kodování, pojmenování souborů + uint8_t licenceIndex = 0; + uint8_t revision; + uint8_t licCompatibility = 0; // identikator hlavního ELC. 0 = nedefinová a vrací se defaultní k danému ELC + uint16_t licItemsCount = 0; + string cid_cds_path = ""; + string licenceFilePath = ""; +}; + +/// @brief obecná struktura pro reader +struct LicenceInfoGeneral +{ + int reqDataPointsCount = 0; + unordered_map licences = {}; +}; + +/// @brief obecná struktura polozky licence, defaultní kompatibilita +struct LicenceItem +{ + int protocolId = -1; + int dataPointsCount = 0; +}; + +/* struktury pro ELC 1 */ +//defaultní struktura pro ELC 1 +struct LicenceELC1Info +{ + bool isValid {false}; +}; + +//struktura ELC1, kompatibilita 1 +struct LicenceELC1Info_1 +{ + unordered_map licences = {}; +}; + +//defaultni struktura polozky licence ELC 1 +struct LicenceELC1Item +{ + int protocolId = -1; + int dataPointsCount = 0; +}; + + +/* struktury pro ELC 2 */ +//defaultní struktura pro ELC 2 +struct LicenceELC2Info +{ + unordered_map licences = {}; +}; + +struct LicenceELC2Info_1 +{ + unordered_map licences = {}; +}; + +//defaultni struktura polozky ELC 2 +struct LicenceELC2Item +{ + int protocolId = -1; + int dataPointsCount = 0; +}; + +//struktura polozky ELC 2, +struct LicenceELC2Item_1 +{ + int protocolId = -1; + int dataPointsCount = 0; +}; + + +class LicenceException : public std::exception +{ +public: + LicenceException(int errorCode, const std::string &errorMessage) + : errorCode_(errorCode), errorMessage_(errorMessage) {} + + const char *what() const noexcept override + { + return errorMessage_.c_str(); + } + + int getErrorCode() const + { + return errorCode_; + } + + const std::string &getErrorMessage() const + { + return errorMessage_; + } + +private: + int errorCode_; + std::string errorMessage_; +}; + +#endif diff --git a/zdenda/include/common/licenceELC1.h b/zdenda/include/common/licenceELC1.h new file mode 100644 index 0000000..f355658 --- /dev/null +++ b/zdenda/include/common/licenceELC1.h @@ -0,0 +1,265 @@ +#ifndef PLC_LICENCE1_COMMON_H +#define PLC_LICENCE1_COMMON_H + +#include +#include "licenceCommon.h" +#include "SDCard.h" + +/// @brief společná třída pro reader a generátor ELC1 +class LicenceELC1 +{ +public: + LicenceELC1(); + LicenceELC1(LicenceIdentification &licIdentification); + ~LicenceELC1(); + + bool getSDData(); // reads SD card + string getLicenceName(BYTE licPostfix); // get proper licencename + string getLicenceName(); + void initCrypto(); + LicenceIdentification lIdentification; + + string cid_cdsPath = ""; + string licenceFilePath = ""; + + Mapping mapping; + + ErrorMessage errorMessage; + +#define MAX_LICENCE_COUNT 100 + + enum InputLicenceType + { + EOSEOV = 1 + }; + + enum BinaryGenerationType + { + Base64Cout = 1, + File = 2 + }; + + enum PlcLicenceType + { + LicenceOther = 0, + LicenceEov = 1 + }; + enum LidIdType + { + Ddts = 1, + EovOsv = 2 + }; + enum PrefixType + { + ELC1 = 1 + }; + + typedef uint32_t DWORD; + typedef uint16_t WORD; + typedef uint8_t BYTE; + typedef uint32_t UDINT; + typedef uint32_t DATE; + typedef uint8_t USINT; + typedef int32_t DINT; + typedef uint16_t UINT; + typedef int16_t INT; + + /// @brief identifikace licence + typedef struct LicenceIdent + { + WORD licPrefixType = 0; // WORD; typ licencniho souboru - pro kazdy prefig je jiny typ souboru a jiny zpusob zpracovani dat [sifrovani, podpis, komprese, ...] + WORD licHeaderType = 0; //: WORD; typ hlavicky kriptovane casti licencniho souboru + WORD licDataType = 0; //: WORD; typ licence - pro kazdou hlavicku jsou samostatne typy licenci - mohou byt i shodne s typem jine hlavicky + WORD licSubType = 0; //: WORD; slouceny typ hlavicku a typ licence => typ hlavicky * 256 + typ licence + WORD licIDType = 0; //: WORD; id aplikace, pro kterou je licence urcena => 1 = ddts, 2 = eov-osv + BYTE licPostfix = 0; //: BYTE; rozliseni licence pro stejny typ - napr. pokud mam 2x SW pro DRT na 2 nezavisle ED + } LicenceIdent; + + /// @brief struktura pro přípravu dat na generování + typedef struct LicenceSourceData + { + + BYTE startGen; // na nabeznou hranu se vygeneruje licence + BYTE loadSD; //: BOOL; na nabeznou hranu se nacte SD karta + // zadavane hodnoty + BYTE licType_id; // vybrany typ licence + char licDescription1[40] = {}; //: STRING(39); jmeno stanice + char licDescription2[20] = {}; //: STRING(19); jmeno rozvadece + BYTE licPostfix; // rozliseni licence pro stejny typ - napr. pokud mam 2x SW pro DRT na 2 nezavisle ED + BYTE enInit; //: BOOL; inicializace (promazani) adresare pro licence + // interne generovane hodnoty + WORD licType; //: WORD; hlavicka licence + BYTE licIdOk; //: BOOL; korektne zvoleny typ licence + string licPostfixIdent; //[2]; //: STRING(2); vygenerovany postfix z cisla pro rozliseni postfixu v souborech a textech + } LicenceSourceData; + + /// @brief struktura obsahující dodatečné informace k licenci + typedef struct PlcData + { + string plcType = ""; + string licenceType = ""; // typ + string licenceVersion = "1"; + string licenceName = ""; // název + string station = ""; // stanice + string distributor = ""; // rozvadec + } PlcData; + + /* + MID (Manufacturer ID): 8 bits - Identifies the manufacturer of the SD card. + OID (OEM/Application ID): 16 bits - Identifies the OEM and application-specific information. + PNM (Product Name): 40 bits - A string representing the product name of the SD card. + PRV (Product Revision): 8 bits - The product revision/version. + PSN (Product Serial Number): 32 bits - A unique serial number assigned to the SD card during manufacturing. + MDT (Manufacturing Date): 12 bits - Represents the manufacturing date of the SD card. + CRC (CRC7 Checksum): 7 bits - A cyclic redundancy check for error detection. + unused: 4 bits - Reserved for future use. + */ + + /// @brief struktura pro načtení CIS a CSD dat + typedef struct SDData + { + BYTE isCorrectLoad = 0; // SD karta je korektne nactena //bool vs byte ? + BYTE CID_nibble[32] = {0}; // surova data CID ze souboru + BYTE CID[16] = {0}; // prekodovane CID informace + BYTE manufacturerID = 0; // ID vyrobce 1B -> Byte 15 + char oemID[2] = {}; // ID aplikace/oem 2B -> Byte 13-14 // oemID: !!STRING(3); + char name[5] = {}; // jmeno produktu 5B -> Byte 8-12 !!STRING(6); + BYTE productRevision_hw = 0; + BYTE productRevision_sw = 0; + float productRevision = 0; // revize produktu 1B [bcd sh] -> Byte 7, bity 0-3 = revize HW, bity 4-7 = revize SW + DWORD serialNumber = 0; // seriove cislo karty 4B -> Byte 3-6 + WORD manufacturerDate_year = 0; + BYTE manufacturerDate_month = 0; + char manufacturerDate[10] = {}; //: STRING(10); // datum vyroby 2B [bcd -yym] -> Byte 1-2, bity 0-3 = mesic, bity 4-11 = posledni cislice roku or roku 2000, bity 12-15 nepouzito + BYTE CRCOK = 0; // CRC 1B -> Byte 0, bity 1-7, bit 0 je vzdy = 1 + BYTE CSD_nibble[32] = {}; //: ARRAY [0..31] OF BYTE; // surova data CSD ze souboru + BYTE CSD[16] = {}; //: ARRAY [0..15] OF BYTE; // prekodovane CSD informace + UDINT cardSize = 0; //: UDINT; // velikost SD karty + BYTE cardGSize = 0; // prepocitana velikost na GB + } CidData; + + // nekodovana cast licence + + /// @brief nekryptovaná hlavička licence + typedef struct LicenceDataMainHeaderELC + { + BYTE licType = 0; + ; // typ licence => duplicitni polozka s hlavickou kodovane casti licence - pouze pro ucely dekodovani dat + BYTE dummy1 = 0; + WORD licSubType = 0; // horni byte = typ hlavicky kodovane casti, spodni byte = typ dat polozkove casti => duplicitni casti s id a hlavickou kodovane casti - pouze pro ucely dekodovani + char description[80] = {}; // viditelny popis licincniho souboru + UDINT sizeData = 0; // velikost kodovane casti licence v Bytech + } LicenceDataMainHeaderELC; + + // hlavní struktura licence + typedef struct LicenceDataMainELC + { + // prefix MUSI zustat jako prvni prvek struktury a MUSI byt typu DWORD + DWORD prefix = 0; // urcuje strukturu dat citelne casti licencniho souboru [teto struktury] + zpusob sifrovani, dig, podpis, kompresi... dat + LicenceDataMainHeaderELC licHeader{}; // nekodovana cast licence + } LicenceDataMain; + + // polozkova cast licence verze 1.0 + typedef struct LicenceDataItem + { + WORD protoId = 0; // id protokolu pro ktery je licence + WORD licCount = 0; // pocet datovych bodu licence + DWORD data1 = 0; // doplnkove nahodne cislo, slouzi pro overeni licence v runtime + } LicenceDataItem; + + // identifikace kodovane casti licence - !!! MUSI yustat pro vsechny verze STEJNA !!! + typedef struct LicenceDataId + { + WORD version = 0; // verze hlavicky + WORD rez = 0; + UDINT cardSize = 0; + } LicenceDataId; + + typedef struct LicenceDataHeader + { + BYTE licType = 0; // typ licence => 0...neznamy (chyba), 1...DDTS, 2...EOV-OSV + BYTE licReserve = 0; // rezerva + WORD licVersion = 0; //: WORD; verze polozkove casti licence + DATE licDate = 0; // datum vygenerovani licence + char licDescription1[40] = {}; // licDescription1: STRING(39); stanice, pro kterou byla licence generovana + char licDescription2[20] = {}; // STRING(19); rozvadec, pro ktery byla licence generovana + USINT licCount = 0; // aktivni pocet dat polozkove casti licence + BYTE licDummy = 0; // rezerva + WORD licReservew = 0; // rezerva + } LicenceDataHeader; + + // kodovana cast licence verze 1.1 s polozkovou casi licence verze 1.0 + typedef struct LicenceData + { + LicenceDataId id{}; // id !!! MUSI zustat na PRVNIM miste struktury + LicenceDataHeader header{}; + LicenceDataItem items[MAX_LICENCE_COUNT] = {}; // polozkova cast licence + WORD dummy = 0; // pro zarovnani struktur - jinak nebude fungovat vypocet a kontrola CRC !!! + WORD crc = 0; // kontrolni kod - !!! MUSI zustat na konci struktury + } LicenceData; + + typedef struct LicenceRetData + { + BYTE retCode = 0; // navratovy kod funkce pro nacteni SD + DINT subCode = 0; // doplnkova informace chyby + std::string text = ""; // textovy popis navratove hodnoty + bool isDone = false; // operace korektne dokoncena + } LicenceRetData; + + typedef struct CryptData + { + bool initDone = false; + DWORD cryptDataAddress = 0; // adresa na data, ktera se maji cryptovat + UINT cryptDataSize = 0; // velikostdat, ktera semaji cryptovat + BYTE aesInitVector[15] = {0}; //] : ARRAY [0..14] OF BYTE;// AES 128 bit IV + BYTE aesKey[32] = {0}; // : ARRAY [0..31] OF BYTE; // AES 256 bit key + DINT aesRetVal = 0; // navratova hodnota AES + DWORD retDataAddress = 0; // adresa na ulozeni cryptovana dat + UINT retDataSize = 0; // velikost cryptovanych dat + LicenceData *licenseData; + } CryptData; + + const BYTE licMaxCount = 100; + const WORD cEzLic_p78ou3_IDType_DDTS = 1; // aplikace DDTS + const WORD cEzLic_p78ou3_IDType_EOVOSV = 2; // aplikace EOV-OSV + const WORD cEzLic_p78ou3_IDType_DRT = 3; // aplikace DRT + const DWORD cEzLic_p78ou3_CheckLicNumber_ERR = 0xFFFFFFFF; // 16#FFFFFFFF; // chybna identifikace licence + const DWORD cEzLic_p78ou3_CheckLicNumber_EOV1 = 3781234965; // cislo pro overeni licence EOV, OSV verze 1 + const BYTE cEzLic_p78ou3_CheckLicNumberId_ERR = 0; // id licence pro neidentifikovanou licenci + const BYTE cEzLic_p78ou3_CheckLicNumberId_EOV1 = 1; // id licence pro EOV, OSV verze 1 + const BYTE cEzLic_p78ou3_MaxCheckLicNumberId = cEzLic_p78ou3_CheckLicNumberId_EOV1; // pocet identifikatoru licenci + const DWORD cEzLic_p78ou3_CheckLicNumber[2] = {cEzLic_p78ou3_CheckLicNumber_ERR, cEzLic_p78ou3_CheckLicNumber_EOV1}; + const WORD cEzLic_p78ou3_LicPrefixType_ELC1 = 1; // prefix typ1 = pouze zasifrovani dat + const WORD cEzLic_p78ou3_HeaderType_10 = 10; // hlavicka kriptovane casti verze 1.0 + const WORD cEzLic_p78ou3_DataType_10 = 10; // data licence verze 1.0 + const WORD cEzLic_p78ou3_SubType_10_10 = 0x0A0A; // subtype - verze hlavicky + verze data -> cEzLic_p78ou3_HeaderType_XX * 256 + cEzLic_p78ou3_DataType_XX + // ID aplikace + + const BYTE cnibblescount11 = 32; + + CryptData cryptData; // structure for encryp + CidData sdData; // data loaded from SD card + + + SDCard sdCard; + + string cidString = ""; + char cid[32] = {}; + string csdString = ""; + 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) + WORD licType = 2; // type of licence + BinaryGenerationType binaryGeneration = BinaryGenerationType::Base64Cout; // typ generování binárního souboru + + string dataLicenceType = ""; // type of licence from xmlFile; + string dataLicenceVersion = ""; // version type of licence from xmlFile; + string dataCryptoVersion = ""; // version of crypting from xmlFile + string dataGenerationType = ""; // version of dataGeneration from xmlFile + string dataLicenceDataFileName = ""; // name of licence file to read + + +}; + +#endif \ No newline at end of file diff --git a/zdenda/include/common/licenceELC2.h b/zdenda/include/common/licenceELC2.h new file mode 100644 index 0000000..37ba217 --- /dev/null +++ b/zdenda/include/common/licenceELC2.h @@ -0,0 +1,86 @@ +#ifndef PLC_LICENCE2_COMMON_H +#define PLC_LICENCE2_COMMON_H + + +#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 cid_cdsPath = ""; + string licenceFilePath = ""; + + Mapping mapping; + + ErrorMessage errorMessage; + + 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 = ""; + }; + + 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)[]); + string getLicenceName(); + +private: + + +}; + +#endif \ No newline at end of file diff --git a/zdenda/include/common/utils.h b/zdenda/include/common/utils.h new file mode 100644 index 0000000..0a29fce --- /dev/null +++ b/zdenda/include/common/utils.h @@ -0,0 +1,72 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include +#include +#include +#include +#include +#include + +typedef uint8_t BYTE; +typedef uint16_t WORD; +typedef uint32_t DWORD; +typedef uint32_t DATE; + +using namespace std; + +struct DateAndTime { + uint16_t date; // DATE data type in CODESYS + uint16_t time; // TIME data type in CODESYS +}; + +void generatePause(); +std::string right(const std::string& sourceString, size_t numChars); +void getCharsFromString(const string source, char *charArray); +uint16_t calculateCRC16(const uint8_t* data, size_t length); +WORD getCurrentDateAsCODESYSDate(); +DWORD getCurrentDateAsCODESYSDateDword(); +DateAndTime getCurrentDateTimeAsCODESYSDateTime(); +DATE getLicDate(); +string getDate(); +int encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext); +unsigned short crc16(const unsigned char* data_p, unsigned char length); +int decrypt(const unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext); +string convertToString(char* a, int size); +string base64_encode_ai(const std::string &input); +unordered_map getArguments(int argc, char *argv[]); +void getCharsFromString(string& source, char *charArray, size_t length); +string getCompletePath(string fileName); +void appendStringToVector(const std::string& str, std::vector& charVector); +uint16_t calculateCRC16(std::vector& charVector); +uint16_t calculateCRC16(std::vector &charVector); +uint16_t calculateCRC16(std::vector &charVector, int removeCount); +uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4); +uint32_t bytesToWord(uint8_t byte1, uint8_t byte2); +vector joinVectors(const std::vector& vector1, const std::vector& vector2); +bool readFile(string fileName, vector &output); + +#endif + + + + + + + + + + + + + + + + + + + + + + diff --git a/zdenda/include/reader/licReaderELC1.h b/zdenda/include/reader/licReaderELC1.h new file mode 100644 index 0000000..5f43c28 --- /dev/null +++ b/zdenda/include/reader/licReaderELC1.h @@ -0,0 +1,31 @@ +#ifndef ELC1_READER__H +#define ELC1_READER__H + +#include "utils.h" +#include "licenceCommon.h" +#include "licenceELC1.h" +#include "SDCard.h" + +using namespace std; + +namespace Reader +{ + class Licence1 : public LicenceELC1 + { + public: + + LicenceELC1Info licenceInfo; + + Licence1(); + ~Licence1(); + Licence1(LicenceIdentification &licIdentification); + + bool readLicence(LicenceInfoGeneral *licences); + bool getLicenceInfo(void *ptr); + bool getLicenceItemInfo(int protocolId, void *returnItemStructure); + void getLicenceItems(); + + }; +} + +#endif \ No newline at end of file diff --git a/zdenda/include/reader/licReaderELC2.h b/zdenda/include/reader/licReaderELC2.h new file mode 100644 index 0000000..237a41a --- /dev/null +++ b/zdenda/include/reader/licReaderELC2.h @@ -0,0 +1,30 @@ +#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: + LicenceELC2Info licenceInfo; + + Licence2(); + ~Licence2(); + Licence2(LicenceIdentification &licIdentification); + + bool readLicence(LicenceInfoGeneral *licences); + bool getLicenceInfo(void *ptr); + bool getLicenceItemInfo(int protocolId, void *returnItemStructure); + void getLicenceItems(); + + }; +} + +#endif \ No newline at end of file diff --git a/zdenda/include/reader/licReaderELC3.h b/zdenda/include/reader/licReaderELC3.h new file mode 100644 index 0000000..f8cae3a --- /dev/null +++ b/zdenda/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/zdenda/include/reader/licenceReader.h b/zdenda/include/reader/licenceReader.h new file mode 100644 index 0000000..3bed660 --- /dev/null +++ b/zdenda/include/reader/licenceReader.h @@ -0,0 +1,51 @@ +#ifndef LICENCE_READER_H_ +#define LICENCE_READER_H_ + +#include "utils.h" +#include "licReaderELC1.h" +#include "licReaderELC2.h" +#include "licReaderELC3.h" + +using namespace std; +using namespace Reader; + + +struct InitStructure +{ + int licenceType = 0; + int licenceVersion = 0; + int licenceIndex = 0; + int compatibility = 0; + string licenceFilePath = ""; + string cid_csd_filePath = ""; +}; + +class LicenceReader +{ + +public: + int elcSwitchType; + string operationErrors = ""; + bool argumentsCorrect = false; + ErrorMessage error; + Licence2 *licence2; + Licence1 *licence1; + + + LicenceReader(); + ~LicenceReader(); + bool init(int elcType, InitStructure &initStructure); + bool initread(int elcType, InitStructure &initStructure); + bool getLicenceInfo(void *returnStructure); + bool getLicenceItemInfo(int protocolId, void *returnItemStructure); + + LicenceInfoGeneral licenceInfo; + +private: + LicenceIdentification licIdentification; + string configFileName = ""; + void *licence = nullptr; + +}; + +#endif \ No newline at end of file diff --git a/zdenda/src/common/utils.cpp b/zdenda/src/common/utils.cpp index efbdf67..6a109d2 100644 --- a/zdenda/src/common/utils.cpp +++ b/zdenda/src/common/utils.cpp @@ -315,7 +315,6 @@ string getCompletePath(string fileName) #ifdef WINDOWS return fileName; #else - char path[PATH_MAX + 1] = {}; ssize_t length = readlink("/proc/self/exe", path, PATH_MAX); path[length] = '\0'; diff --git a/zdenda/src/output/ezlic_eovosv0_jjacdgpdxpb.lic b/zdenda/src/output/ezlic_eovosv0_jjacdgpdxpb.lic index 4efdd87..f2eee96 100644 Binary files a/zdenda/src/output/ezlic_eovosv0_jjacdgpdxpb.lic and b/zdenda/src/output/ezlic_eovosv0_jjacdgpdxpb.lic differ diff --git a/zdenda/src/output/ezlic_eovosv0_wqxcyjpdxji.lic b/zdenda/src/output/ezlic_eovosv0_wqxcyjpdxji.lic index 6731e88..3d0a18f 100644 Binary files a/zdenda/src/output/ezlic_eovosv0_wqxcyjpdxji.lic and b/zdenda/src/output/ezlic_eovosv0_wqxcyjpdxji.lic differ diff --git a/zdenda/src/output/licData.xml b/zdenda/src/output/licData.xml index 47acd9a..1cfdec3 100644 --- a/zdenda/src/output/licData.xml +++ b/zdenda/src/output/licData.xml @@ -1,27 +1,29 @@ -EOV_OSV -WAGO +EOV_OSV +wago Licence pro EOV_OSV -odb.zaluzi.drt.cz -POZ8 -Projekt XXX stanice YYY - - - Položka licence 1 - 111 - 40000 - - - Položka licence 2 - 222 - 51000 - - - Položka licence 3 - 333 - 62000 - - - +testovaci projekt + + +pt_Ez_iec104c +5 +2500 + + +pt_Ez_iec104s +6 +15000 + + +pt_Ez_mdbipc +9 +250 + + +pt_Ez_mdbips +10 +400 + + \ No newline at end of file diff --git a/zdenda/src/output/licDataOld.xml b/zdenda/src/output/licDataOld.xml new file mode 100644 index 0000000..47acd9a --- /dev/null +++ b/zdenda/src/output/licDataOld.xml @@ -0,0 +1,27 @@ + + +EOV_OSV +WAGO +Licence pro EOV_OSV +odb.zaluzi.drt.cz +POZ8 +Projekt XXX stanice YYY + + + Položka licence 1 + 111 + 40000 + + + Položka licence 2 + 222 + 51000 + + + Položka licence 3 + 333 + 62000 + + + + \ No newline at end of file diff --git a/zdenda/src/reader/LicenceReader.cpp b/zdenda/src/reader/LicenceReader.cpp index c86ab44..21cecfd 100644 --- a/zdenda/src/reader/LicenceReader.cpp +++ b/zdenda/src/reader/LicenceReader.cpp @@ -64,7 +64,7 @@ bool LicenceReader::initread(int elcType, InitStructure &initStructure) switch (this->licIdentification.licElcType) { case ELCType::ELC1: - { // old eoseov + { Reader::Licence1 licenceELC1 = Reader::Licence1(this->licIdentification); this->licence1 = &licenceELC1; licenceELC1.cid_cdsPath = initStructure.cid_csd_filePath; @@ -143,7 +143,7 @@ bool LicenceReader::getLicenceItemInfo(int protocolId, void *returnItemStructure if (!this->licIdentification.licCompatibility) // defaultní kompatibilita { LicenceELC2Item *resultPtr = static_cast(returnItemStructure); - resultPtr->protocolId = protocolId; // protocolId; + resultPtr->protocolId = protocolId; if (this->licenceInfo.licences.count(protocolId)) resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); else diff --git a/zdenda/src/reader/licReaderELC2.cpp b/zdenda/src/reader/licReaderELC2.cpp index 7ba21a6..1920220 100644 --- a/zdenda/src/reader/licReaderELC2.cpp +++ b/zdenda/src/reader/licReaderELC2.cpp @@ -62,10 +62,6 @@ namespace Reader vector encryptedPart(content.begin() + licBody.licenceIdentHeader.publicHeaderLength + 18, content.begin() + content.size()); - // cout << "encryptedPart content length: " << encryptedPart.size() << "\n"; - // for (auto x : encryptedPart) cout << (int)x << "-"; - // cout << "\n"; - vector privateContentDecrypted; privateContentDecrypted = decryptPrivateContent(encryptedPart); diff --git a/zdenda_reader.zip b/zdenda_reader.zip index f57b083..1d09976 100644 Binary files a/zdenda_reader.zip and b/zdenda_reader.zip differ