diff --git a/docker/generate.sh b/docker/generate.sh index 57e4ad9..7c07b9c 100644 --- a/docker/generate.sh +++ b/docker/generate.sh @@ -1,5 +1,2 @@ #!/bin/bash -rm src/CreateLicence.o -rm src/PlcLicence.o -rm src/utils.o make \ No newline at end of file diff --git a/docker/start.sh b/docker/start.sh index be6f26b..a5cab3f 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -1,7 +1,7 @@ #!/bin/bash chmod 777 output/licenceGenerator -#./output/licenceGenerator -cid=0353445355313647801b1a9f6600c747 -csd=0353445355313647801b1a9f6600c747 -outputType=file -configFileName=licData.xml +#./output/licenceGenerator -cid=9f54495344434954615ad803c50171bf -csd=400e00325b5900003be77f800a400043 -outputType=file -configFileName=licData.xml cd output -./licenceGenerator -cid=0353445355313647801b1a9f6600c747 -csd=0353445355313647801b1a9f6600c747 -outputType=file -configFileName=licData.xml \ No newline at end of file +./licenceGenerator -cid=9f54495344434954615ad803c50171bf -csd=400e00325b5900003be77f800a400043 -outputType=file -configFileName=licData.xml \ No newline at end of file diff --git a/include/common/licenceCommon.h b/include/common/licenceCommon.h index 2bd2d2a..f432daf 100644 --- a/include/common/licenceCommon.h +++ b/include/common/licenceCommon.h @@ -1,3 +1,5 @@ +#define CRC = 1 + #ifndef LICENCE_COMMON_H_ #define LICENCE_COMMON_H_ @@ -5,7 +7,6 @@ #include #include -#include #include #include @@ -44,6 +45,7 @@ enum class GeneralError LicenceSizeMismatch = 52, LicenceSizeCardMismatch = 53, LicenceMismatch = 54, + LicenceCRCMismatch = 54, ItemsCountMismatch = 61 }; @@ -90,9 +92,9 @@ enum class FileNameGenerationType struct Mapping { - std::map licMapTypes = {{"EOV_OSV", LicenceType::EOS_EOV}, {"DDTS", LicenceType::DDTS}, {"DRT", LicenceType::DRT}}; - std::map licMapEncryption = {{"CID_AES256", EncryptionType::CID_AES256}, {"FIX_AES256", EncryptionType::FIX_AES256}}; - std::map licMapPlcType = {{"WAGO", PlcType::WAGO}, {"TECO", PlcType::TECO}}; + 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 diff --git a/include/common/licenceELC1.h b/include/common/licenceELC1.h index 970c26f..f355658 100644 --- a/include/common/licenceELC1.h +++ b/include/common/licenceELC1.h @@ -1,8 +1,6 @@ #ifndef PLC_LICENCE1_COMMON_H #define PLC_LICENCE1_COMMON_H -#include -#include #include #include "licenceCommon.h" #include "SDCard.h" diff --git a/include/common/licenceELC2.h b/include/common/licenceELC2.h index 2ff0d2a..37ba217 100644 --- a/include/common/licenceELC2.h +++ b/include/common/licenceELC2.h @@ -1,8 +1,7 @@ #ifndef PLC_LICENCE2_COMMON_H #define PLC_LICENCE2_COMMON_H -#include -#include + #include #include "licenceCommon.h" #include "SDCard.h" @@ -22,25 +21,6 @@ public: 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', '_'}; @@ -73,7 +53,6 @@ public: string projectDescription = ""; string date = ""; string licenceType = ""; - int crc = 0; }; struct PrivateContent // privátní šifrovaná část diff --git a/include/common/utils.h b/include/common/utils.h index eb03acc..0a29fce 100644 --- a/include/common/utils.h +++ b/include/common/utils.h @@ -6,9 +6,7 @@ #include #include #include -#include -#include // dirname -#include +#include #include typedef uint8_t BYTE; @@ -42,6 +40,8 @@ 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); diff --git a/include/reader/licenceReader.h b/include/reader/licenceReader.h index 334c6ce..3bed660 100644 --- a/include/reader/licenceReader.h +++ b/include/reader/licenceReader.h @@ -2,7 +2,6 @@ #define LICENCE_READER_H_ #include "utils.h" - #include "licReaderELC1.h" #include "licReaderELC2.h" #include "licReaderELC3.h" diff --git a/output/ezlic_eovosv0_jjacdgpdxpb.lic b/output/ezlic_eovosv0_jjacdgpdxpb.lic index 9942ae6..4efdd87 100644 Binary files a/output/ezlic_eovosv0_jjacdgpdxpb.lic and b/output/ezlic_eovosv0_jjacdgpdxpb.lic differ diff --git a/output/ezlic_eovosv0_wqxcyjpdxji.lic b/output/ezlic_eovosv0_wqxcyjpdxji.lic index c67a8bd..6731e88 100644 Binary files a/output/ezlic_eovosv0_wqxcyjpdxji.lic and b/output/ezlic_eovosv0_wqxcyjpdxji.lic differ diff --git a/output/ezlic_eovosv0_xjxvxgalxab.lic b/output/ezlic_eovosv0_xjxvxgalxab.lic deleted file mode 100644 index 4e1b4f2..0000000 Binary files a/output/ezlic_eovosv0_xjxvxgalxab.lic and /dev/null differ diff --git a/output/generate b/output/generate index 4b2a8cb..a0b9147 100644 Binary files a/output/generate and b/output/generate differ diff --git a/output/licData.xml b/output/licData.xml index 870d8ac..47acd9a 100644 --- a/output/licData.xml +++ b/output/licData.xml @@ -10,17 +10,17 @@ Položka licence 1 111 - 7454 + 40000 Položka licence 2 222 - 6477 + 51000 Položka licence 3 333 - 7844 + 62000 diff --git a/output/licenceGenerator b/output/licenceGenerator index bba4edc..fba0e59 100644 Binary files a/output/licenceGenerator and b/output/licenceGenerator differ diff --git a/src/ReadLicence.cpp b/src/ReadLicence.cpp index 74c4a0f..775895a 100644 --- a/src/ReadLicence.cpp +++ b/src/ReadLicence.cpp @@ -1,7 +1,6 @@ #define LINUX 1 // #define WINDOWS 1 -#include #include "licenceReader.h" /// @brief hlavní funkce @@ -10,6 +9,7 @@ /// @return int main() { + try { InitStructure initStructure; @@ -21,7 +21,7 @@ int main() // pro ELC 1 LicenceELC1Info s výstupem isValid, pro ELC 2 strukura { int protocolId = -1; int dataPointsCount = 0; } initStructure.cid_csd_filePath = ""; // cesta k cidu/csd pro načtení a kontorlu licence - // verze původní ELC 1 + //verze původní ELC 1 LicenceReader licenceReaderELC1{}; if (licenceReaderELC1.initread(1, initStructure)) // iniciacni nacteni { @@ -43,7 +43,6 @@ int main() // verze ELC 2 pro jeden protokol LicenceReader licenceReaderELC2{}; - if (licenceReaderELC2.initread(2, initStructure)) // iniciacni nacteni { // v případě kompatibility 0, či nezadané výstup nativně je defaultní. @@ -52,7 +51,7 @@ int main() // if () int protocolId = 333; - // if (initStructure.compatibility == 1) //ukazka kompatibilita + // if (initStructure.compatibility == 1) //ukazka kompatibilita 1 // { // LicenceELC2Item_1 info; //jiná struktura, zbytek stejný // if (licenceReaderELC2.getLicenceItemInfo(protocolId, &info)) @@ -75,8 +74,7 @@ int main() } LicenceReader licenceReaderCompleteELC2{}; - - // verze ELC 2 kompletní načtení + //verze ELC 2 kompletní načtení if (licenceReaderCompleteELC2.init(2, initStructure)) // iniciacni nacteni { LicenceELC2Info info; // podle ELC a kompatibility určit strukuru (LicenceInfo11, LicenceInfo21, LicenceInfo31) @@ -96,20 +94,29 @@ int main() } else cout << "CHYBA: " << licenceReaderCompleteELC2.error.message; + } else { cout << "CHYBA: " << licenceReaderCompleteELC2.error.message; } + } catch (...) { cout << "Obecna chyba\n"; } - // system("pause"); return SUCCES; } -// cid: 0353445355313647801b1a9f6600c747 csd: 400e00325b59000076b27f800a404013 -// https://sprava.app.elzel.cz/device/417 cid: 9f54495344434954615ad803c50171bf csd: 400e00325b5900003be77f800a400043 \ No newline at end of file +/* +CRC public size: 353 +CRC gen public: 27885 + +CRC private size: 285 +CRC gen private: 18500 + +CRC complete size: 638 +CRC gen complete: 42051 +*/ \ No newline at end of file diff --git a/src/common/SDCard.o b/src/common/SDCard.o index 4a65c6c..030e0fd 100644 Binary files a/src/common/SDCard.o and b/src/common/SDCard.o differ diff --git a/src/common/licenceELC1.o b/src/common/licenceELC1.o index 0b9ce0c..972db80 100644 Binary files a/src/common/licenceELC1.o and b/src/common/licenceELC1.o differ diff --git a/src/common/licenceELC2.o b/src/common/licenceELC2.o index 008171b..3a2b491 100644 Binary files a/src/common/licenceELC2.o and b/src/common/licenceELC2.o differ diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 28181da..efbdf67 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -338,6 +338,33 @@ void appendStringToVector(const std::string &str, std::vector &ch } } +uint16_t calculateCRC16(std::vector &charVector) +{ + const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial + uint16_t crc = 0xFFFF; // Initial value + + size_t length = charVector.size(); + + for (size_t i = 0; i < length; i++) + { + crc ^= charVector[i]; // XOR with the current data byte + + for (int j = 0; j < 8; j++) + { + if (crc & 0x0001) + { + crc = (crc >> 1) ^ polynomial; + } + else + { + crc = crc >> 1; + } + } + } + + return crc; +} + uint16_t calculateCRC16(std::vector &charVector) { const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial @@ -365,6 +392,34 @@ uint16_t calculateCRC16(std::vector &charVector) return crc; } +uint16_t calculateCRC16(std::vector &charVector, int removeCount) +{ + const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial + uint16_t crc = 0xFFFF; // Initial value + + size_t length = charVector.size(); + length = length-removeCount; + + for (size_t i = 0; i < length; i++) + { + crc ^= charVector[i]; // XOR with the current data byte + + for (int j = 0; j < 8; j++) + { + if (crc & 0x0001) + { + crc = (crc >> 1) ^ polynomial; + } + else + { + crc = crc >> 1; + } + } + } + + return crc; +} + uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) { return static_cast(byte1) | diff --git a/src/common/utils.o b/src/common/utils.o index ef95f8a..29a406e 100644 Binary files a/src/common/utils.o and b/src/common/utils.o differ diff --git a/src/generator/LicenceGenerator.o b/src/generator/LicenceGenerator.o index ef0d9a9..06b6f48 100644 Binary files a/src/generator/LicenceGenerator.o and b/src/generator/LicenceGenerator.o differ diff --git a/src/generator/licGenELC1.o b/src/generator/licGenELC1.o index 41a10e6..b9cc07f 100644 Binary files a/src/generator/licGenELC1.o and b/src/generator/licGenELC1.o differ diff --git a/src/generator/licGenELC2.cpp b/src/generator/licGenELC2.cpp index eece34f..1801bda 100644 --- a/src/generator/licGenELC2.cpp +++ b/src/generator/licGenELC2.cpp @@ -1,3 +1,5 @@ + + #include "licGenELC2.h" namespace Generator @@ -6,16 +8,16 @@ namespace Generator { } - Licence2::~Licence2(){} + Licence2::~Licence2() {} - - Licence2::Licence2(string cid, string csd, pugi::xml_document * xmlDoc) + Licence2::Licence2(string cid, string csd, pugi::xml_document *xmlDoc) { this->cid = cid; this->csd = csd; - + this->xmlDoc = xmlDoc; - if (processInputConfiguration() == false) throw LicenceException((int)GeneralError::LicenceReadError, "Chyba při čtení licence"); + if (processInputConfiguration() == false) + throw LicenceException((int)GeneralError::LicenceReadError, "Chyba při čtení licence"); } void Licence2::getHeader() @@ -90,195 +92,215 @@ namespace Generator } string Licence2::getVersion(int middleVersion) -{ - string result = ""; - result.append(to_string(generatorVersion)); - result.append("."); - result.append(to_string(middleVersion)); - result.append("."); - string tempLicenceCount = "3"; // TODO - result.append(tempLicenceCount); - return result; -} - -bool Licence2::processInputConfiguration() -{ - const char *dataRootName = "licence"; - - string licType = xmlDoc->child(dataRootName).child("licenceType").child_value(); - if (!licType.empty()) { - this->lIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV; - this->lIdentification.licTypeName = licType; - } - else - { - errorMessage.code = (int)GeneralError::LicenceMismatch; - errorMessage.message = "ERROR MISSING licenceType"; - return false; + string result = ""; + result.append(to_string(generatorVersion)); + result.append("."); + result.append(to_string(middleVersion)); + result.append("."); + string tempLicenceCount = "3"; // TODO + result.append(tempLicenceCount); + return result; } - this->lIdentification.licenceVersion = atoi(&xmlDoc->child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); - this->lIdentification.revision = xmlDoc->child(dataRootName).attribute("revision").value()[0]; - this->lIdentification.licenceIndex = atoi(&xmlDoc->child(dataRootName).child("licenceType").attribute("licenceIndex").value()[0]); - this->lIdentification.licElcType = (ELCType)atoi(&xmlDoc->child(dataRootName).attribute("elc").value()[0]); - - this->lIdentification.licCompatibility = atoi(&xmlDoc->child(dataRootName).attribute("compatibility").value()[0]); - - string plcType = xmlDoc->child(dataRootName).child("plcType").child_value(); - if (!plcType.empty()) + bool Licence2::processInputConfiguration() { - this->lIdentification.licPlcType = mapping.licMapPlcType[plcType]; - } + const char *dataRootName = "licence"; - this->projectDescription = &xmlDoc->child(dataRootName).child("project").child_value()[0]; - - licBody.licenceIdentHeader.cardSize = 0; - licBody.licenceIdentHeader.compatibilityVersion = this->lIdentification.licCompatibility; - licBody.licenceIdentHeader.licenceIndex = this->lIdentification.licenceIndex; - licBody.licenceIdentHeader.licenceType = this->lIdentification.licenceVersion; - licBody.licenceIdentHeader.licenceTypeVersion = this->lIdentification.licenceVersion; - licBody.licenceIdentHeader.serialNumber = 0; - - return true; -} - -void Licence2::getLicenceItems() -{ - int nodeIndex = 0; - int itemIndex = 0; - - for (pugi::xml_node licItemNode : xmlDoc->child("licence").child("items")) - { - licDataItem item; - - item.protoId = atoi(licItemNode.child("protoId").child_value()); - item.licCount = atoi(licItemNode.child("dataPointsCount").child_value()); - - this->licBody.privateContent.dataItems.push_back(item); - - nodeIndex = 0; - itemIndex++; - for (pugi::xml_node child : licItemNode.children()) + string licType = xmlDoc->child(dataRootName).child("licenceType").child_value(); + if (!licType.empty()) { - nodeIndex++; - if (nodeIndex == 1) - { - licBody.publicHeader.append("\"name\":\""); - licBody.publicHeader.append(child.child_value()); - licBody.publicHeader.append("\","); - continue; - } - if (nodeIndex == 3) - { - licBody.publicHeader.append("\"dataPointsCount\":\""); - licBody.publicHeader.append(child.child_value()); - licBody.publicHeader.append("\""); - } - } - if (itemIndex != lIdentification.licItemsCount) - { - licBody.publicHeader.append("},"); + this->lIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV; + this->lIdentification.licTypeName = licType; } else { - licBody.publicHeader.append("}"); + errorMessage.code = (int)GeneralError::LicenceMismatch; + errorMessage.message = "ERROR MISSING licenceType"; + return false; + } + + this->lIdentification.licenceVersion = atoi(&xmlDoc->child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); + this->lIdentification.revision = xmlDoc->child(dataRootName).attribute("revision").value()[0]; + this->lIdentification.licenceIndex = atoi(&xmlDoc->child(dataRootName).child("licenceType").attribute("licenceIndex").value()[0]); + this->lIdentification.licElcType = (ELCType)atoi(&xmlDoc->child(dataRootName).attribute("elc").value()[0]); + + this->lIdentification.licCompatibility = atoi(&xmlDoc->child(dataRootName).attribute("compatibility").value()[0]); + + string plcType = xmlDoc->child(dataRootName).child("plcType").child_value(); + if (!plcType.empty()) + { + this->lIdentification.licPlcType = mapping.licMapPlcType[plcType]; + } + + this->projectDescription = &xmlDoc->child(dataRootName).child("project").child_value()[0]; + + licBody.licenceIdentHeader.cardSize = 0; + licBody.licenceIdentHeader.compatibilityVersion = this->lIdentification.licCompatibility; + licBody.licenceIdentHeader.licenceIndex = this->lIdentification.licenceIndex; + licBody.licenceIdentHeader.licenceType = this->lIdentification.licenceVersion; + licBody.licenceIdentHeader.licenceTypeVersion = this->lIdentification.licenceVersion; + licBody.licenceIdentHeader.serialNumber = 0; + + return true; + } + + void Licence2::getLicenceItems() + { + int nodeIndex = 0; + int itemIndex = 0; + + for (pugi::xml_node licItemNode : xmlDoc->child("licence").child("items")) + { + licDataItem item; + + item.protoId = atoi(licItemNode.child("protoId").child_value()); + item.licCount = atoi(licItemNode.child("dataPointsCount").child_value()); + + this->licBody.privateContent.dataItems.push_back(item); + + nodeIndex = 0; + itemIndex++; + for (pugi::xml_node child : licItemNode.children()) + { + nodeIndex++; + if (nodeIndex == 1) + { + licBody.publicHeader.append("\"name\":\""); + licBody.publicHeader.append(child.child_value()); + licBody.publicHeader.append("\","); + continue; + } + if (nodeIndex == 3) + { + licBody.publicHeader.append("\"dataPointsCount\":\""); + licBody.publicHeader.append(child.child_value()); + licBody.publicHeader.append("\""); + } + } + if (itemIndex != lIdentification.licItemsCount) + { + licBody.publicHeader.append("},"); + } + else + { + licBody.publicHeader.append("}"); + } } } -} -bool Licence2::createLicence() -{ - getLicenceItems(); - getHeader(); - - sdCard = SDCard(this->cid, this->csd); - if (sdCard.isLoaded == false) throw LicenceException((int)GeneralError::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(); - this->licBody.licenceIdentHeader.publicHeaderLength = this->licBody.publicHeader.length(); - - vector publicContent; - vector privateContent; - vector privateContentEncrypted; - - - publicContent.push_back(this->licBody.licId.licIdent[0]); - publicContent.push_back(this->licBody.licId.licIdent[1]); - publicContent.push_back(this->licBody.licId.licIdent[2]); - publicContent.push_back(((char)48 + (int)this->lIdentification.licElcType)); - publicContent.push_back(this->licBody.licId.licIdent[4]); - publicContent.push_back(this->licBody.licenceIdentHeader.licenceType); - publicContent.push_back(this->licBody.licenceIdentHeader.licenceTypeVersion); - publicContent.push_back(this->licBody.licenceIdentHeader.licenceIndex); - publicContent.push_back(this->licBody.licenceIdentHeader.compatibilityVersion); - publicContent.push_back(this->licBody.privateContent.dataItems.size()); - publicContent.push_back(this->licBody.licenceIdentHeader.publicHeaderLength & 0xFF); - publicContent.push_back((this->licBody.licenceIdentHeader.publicHeaderLength >> 8) & 0xFF); - publicContent.push_back(this->licBody.licenceIdentHeader.cardSize & 0xFF); - publicContent.push_back((this->licBody.licenceIdentHeader.cardSize >> 8) & 0xFF); - publicContent.push_back(this->licBody.licenceIdentHeader.serialNumber & 0xFF); - publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 8) & 0xFF); - publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 16) & 0xFF); - publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 24) & 0xFF); - - appendStringToVector(this->licBody.publicHeader, publicContent); - uint16_t crcPublic = calculateCRC16(publicContent); - - publicContent.push_back(crcPublic & 0xFF); - publicContent.push_back((crcPublic >> 8) & 0xFF); - - privateContent.push_back(this->licBody.licenceIdentHeader.licenceType); - privateContent.push_back(this->licBody.licenceIdentHeader.licenceTypeVersion); - privateContent.push_back(this->licBody.licenceIdentHeader.licenceIndex); - privateContent.push_back(this->licBody.licenceIdentHeader.compatibilityVersion); - privateContent.push_back(this->licBody.privateContent.dataItems.size()); - privateContent.push_back(this->licBody.licenceIdentHeader.publicHeaderLength & 0xFF); - privateContent.push_back((this->licBody.licenceIdentHeader.publicHeaderLength >> 8) & 0xFF); - privateContent.push_back(this->licBody.licenceIdentHeader.cardSize & 0xFF); - privateContent.push_back((this->licBody.licenceIdentHeader.cardSize >> 8) & 0xFF); - privateContent.push_back(this->licBody.licenceIdentHeader.serialNumber & 0xFF); - privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 8) & 0xFF); - privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 16) & 0xFF); - privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 24) & 0xFF); - - for (licDataItem dataItem : this->licBody.privateContent.dataItems) + bool Licence2::createLicence() { - privateContent.push_back(dataItem.protoId & 0xFF); - privateContent.push_back((dataItem.protoId >> 8) & 0xFF); - privateContent.push_back(dataItem.licCount & 0xFF); - privateContent.push_back((dataItem.licCount >> 8) & 0xFF); - for (unsigned int i = 0; i < sizeof(dataItem.dummy); i++) - privateContent.push_back(i); + getLicenceItems(); + getHeader(); + + sdCard = SDCard(this->cid, this->csd); + if (sdCard.isLoaded == false) + throw LicenceException((int)GeneralError::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(); + this->licBody.licenceIdentHeader.publicHeaderLength = this->licBody.publicHeader.length(); + + vector publicContent; + vector privateContent; + vector privateContentEncrypted; + + publicContent.push_back(this->licBody.licId.licIdent[0]); + publicContent.push_back(this->licBody.licId.licIdent[1]); + publicContent.push_back(this->licBody.licId.licIdent[2]); + publicContent.push_back(((char)48 + (int)this->lIdentification.licElcType)); + publicContent.push_back(this->licBody.licId.licIdent[4]); + publicContent.push_back(this->licBody.licenceIdentHeader.licenceType); + publicContent.push_back(this->licBody.licenceIdentHeader.licenceTypeVersion); + publicContent.push_back(this->licBody.licenceIdentHeader.licenceIndex); + publicContent.push_back(this->licBody.licenceIdentHeader.compatibilityVersion); + publicContent.push_back(this->licBody.privateContent.dataItems.size()); + publicContent.push_back(this->licBody.licenceIdentHeader.publicHeaderLength & 0xFF); + publicContent.push_back((this->licBody.licenceIdentHeader.publicHeaderLength >> 8) & 0xFF); + publicContent.push_back(this->licBody.licenceIdentHeader.cardSize & 0xFF); + publicContent.push_back((this->licBody.licenceIdentHeader.cardSize >> 8) & 0xFF); + publicContent.push_back(this->licBody.licenceIdentHeader.serialNumber & 0xFF); + publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 8) & 0xFF); + publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 16) & 0xFF); + publicContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 24) & 0xFF); + + appendStringToVector(this->licBody.publicHeader, publicContent); + +#ifdef CRCCHECK + cout << "CRC public size: " << publicContent.size() << "\n"; + cout << "CRC gen public: " << calculateCRC16(publicContent) << "\n"; +#endif + + privateContent.push_back(this->licBody.licenceIdentHeader.licenceType); + privateContent.push_back(this->licBody.licenceIdentHeader.licenceTypeVersion); + privateContent.push_back(this->licBody.licenceIdentHeader.licenceIndex); + privateContent.push_back(this->licBody.licenceIdentHeader.compatibilityVersion); + privateContent.push_back(this->licBody.privateContent.dataItems.size()); + privateContent.push_back(this->licBody.licenceIdentHeader.publicHeaderLength & 0xFF); + privateContent.push_back((this->licBody.licenceIdentHeader.publicHeaderLength >> 8) & 0xFF); + privateContent.push_back(this->licBody.licenceIdentHeader.cardSize & 0xFF); + privateContent.push_back((this->licBody.licenceIdentHeader.cardSize >> 8) & 0xFF); + privateContent.push_back(this->licBody.licenceIdentHeader.serialNumber & 0xFF); + privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 8) & 0xFF); + privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 16) & 0xFF); + privateContent.push_back((this->licBody.licenceIdentHeader.serialNumber >> 24) & 0xFF); + + for (licDataItem dataItem : this->licBody.privateContent.dataItems) + { + privateContent.push_back(dataItem.protoId & 0xFF); + privateContent.push_back((dataItem.protoId >> 8) & 0xFF); + privateContent.push_back(dataItem.licCount & 0xFF); + privateContent.push_back((dataItem.licCount >> 8) & 0xFF); + for (unsigned int i = 0; i < sizeof(dataItem.dummy); i++) + privateContent.push_back(i); + } + +#ifdef CRCCHECK + cout << "CRC private size: " << privateContent.size() << "\n"; + cout << "CRC gen private: " << calculateCRC16(privateContent) << "\n"; +#endif + + vector completeVector = joinVectors(publicContent, privateContent); + uint16_t crcComplete = calculateCRC16(completeVector); + +#ifdef CRCCHECK + cout << "CRC complete size: " << completeVector.size() << "\n"; + cout << "CRC gen complete: " << crcComplete << "\n"; +#endif + + privateContent.push_back(crcComplete & 0xFF); + privateContent.push_back((crcComplete >> 8) & 0xFF); + + // cout << "crc complete: " << crcComplete << "\n"; + + // cout << "privateContent length: " << privateContent.size() << "\n"; + // for (auto x : privateContent) cout << (int)x << "-"; + // cout << "\n"; + + privateContentEncrypted = cryptPrivateContent(privateContent); + + // cout << "privateContentEncrypted length: " << privateContentEncrypted.size() << "\n"; + // for (auto x : privateContentEncrypted) cout << (int)x << "-"; + // cout << "\n"; + + string licfileName = getLicenceName(); + std::ofstream outputFile(licfileName, std::ios::out | std::ios::binary); + + // Check if the file is open + if (!outputFile.is_open()) + { + 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)); + std::copy(privateContentEncrypted.cbegin(), privateContentEncrypted.cend(), std::ostream_iterator(outputFile)); + + outputFile.close(); + + cout << licfileName; + return true; } - vector completeVector = joinVectors(publicContent, privateContent); - licBody.privateContent.crc = calculateCRC16(completeVector); - uint16_t crcComplete = calculateCRC16(completeVector); - - privateContent.push_back(crcComplete & 0xFF); - privateContent.push_back((crcComplete >> 8) & 0xFF); - - privateContentEncrypted = cryptPrivateContent(privateContent); - - string licfileName = getLicenceName(); - std::ofstream outputFile(licfileName, std::ios::out | std::ios::binary); - - // Check if the file is open - if (!outputFile.is_open()) - { - 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)); - std::copy(privateContentEncrypted.cbegin(), privateContentEncrypted.cend(), std::ostream_iterator(outputFile)); - - outputFile.close(); - - cout << licfileName; - return true; -} - } diff --git a/src/generator/licGenELC2.o b/src/generator/licGenELC2.o index a13bec4..a41ba6a 100644 Binary files a/src/generator/licGenELC2.o and b/src/generator/licGenELC2.o differ diff --git a/src/reader/LicenceReader.o b/src/reader/LicenceReader.o index 27a7056..122f20a 100644 Binary files a/src/reader/LicenceReader.o and b/src/reader/LicenceReader.o differ diff --git a/src/reader/licReaderELC1.o b/src/reader/licReaderELC1.o index bf94c02..bb240a4 100644 Binary files a/src/reader/licReaderELC1.o and b/src/reader/licReaderELC1.o differ diff --git a/src/reader/licReaderELC2.cpp b/src/reader/licReaderELC2.cpp index 4df790b..7ba21a6 100644 --- a/src/reader/licReaderELC2.cpp +++ b/src/reader/licReaderELC2.cpp @@ -46,18 +46,38 @@ namespace Reader //~~~uint16_t crcPublic = bytesToWord(content[18 + licBody.licenceIdentHeader.publicHeaderLength], content[19 + licBody.licenceIdentHeader.publicHeaderLength]); - int elcVersion = (int)licBody.licId.licIdent[3] - 48; + int elcVersion = (int)licBody.licId.licIdent[3] - 48; // verze je text, musí se odečíst 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 publicPart(content.begin(), content.begin() + 18 + this->licBody.licenceIdentHeader.publicHeaderLength); + +#ifdef CRCCHECK + cout << "CRC read public size: " << publicPart.size() << "\n"; + cout << "CRC read public: " << calculateCRC16(publicPart) << "\n"; +#endif + + 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); +#ifdef CRCCHECK + cout << "CRC read private size: " << privateContentDecrypted.size() << "\n"; + cout << "CRC read private: " << calculateCRC16(privateContentDecrypted) << "\n"; +#endif + + // cout << "privateContentDecrypted content length: " << privateContentDecrypted.size() << "\n"; + // for (auto x : privateContentDecrypted) cout << (int)x << "-"; + // cout << "\n"; + LicenceBody licBodyDecrypted; licBodyDecrypted.licenceIdentHeader.licenceType = privateContentDecrypted[0]; licBodyDecrypted.licenceIdentHeader.licenceTypeVersion = privateContentDecrypted[1]; @@ -68,8 +88,6 @@ namespace Reader 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)GeneralError::ItemsCountMismatch, "Nesouhlasí počet položek licence."); @@ -86,6 +104,12 @@ namespace Reader this->licenceInfo.licences.insert(pair(item.protoId, item.licCount)); licences->licences.insert(pair(item.protoId, item.licCount)); } + + uint16_t crcComplete = bytesToWord(privateContentDecrypted[index], privateContentDecrypted[index + 1]); + + vector completeVector = joinVectors(publicPart, privateContentDecrypted); + if (calculateCRC16(completeVector, 2) != crcComplete) throw LicenceException((int)GeneralError::LicenceCRCMismatch, "Nesouhlasí CRC."); + return true; } diff --git a/src/reader/licReaderELC2.o b/src/reader/licReaderELC2.o index b09d02b..b532ae4 100644 Binary files a/src/reader/licReaderELC2.o and b/src/reader/licReaderELC2.o differ