ELC2 CRC check, čištění

This commit is contained in:
2024-02-07 13:36:25 +01:00
parent 37d02abd3f
commit 2bfc662b51
27 changed files with 317 additions and 234 deletions

View File

@@ -1,5 +1,2 @@
#!/bin/bash #!/bin/bash
rm src/CreateLicence.o
rm src/PlcLicence.o
rm src/utils.o
make make

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
chmod 777 output/licenceGenerator 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 cd output
./licenceGenerator -cid=0353445355313647801b1a9f6600c747 -csd=0353445355313647801b1a9f6600c747 -outputType=file -configFileName=licData.xml ./licenceGenerator -cid=9f54495344434954615ad803c50171bf -csd=400e00325b5900003be77f800a400043 -outputType=file -configFileName=licData.xml

View File

@@ -1,3 +1,5 @@
#define CRC = 1
#ifndef LICENCE_COMMON_H_ #ifndef LICENCE_COMMON_H_
#define LICENCE_COMMON_H_ #define LICENCE_COMMON_H_
@@ -5,7 +7,6 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <map>
#include <unordered_map> #include <unordered_map>
#include <stdint.h> #include <stdint.h>
@@ -44,6 +45,7 @@ enum class GeneralError
LicenceSizeMismatch = 52, LicenceSizeMismatch = 52,
LicenceSizeCardMismatch = 53, LicenceSizeCardMismatch = 53,
LicenceMismatch = 54, LicenceMismatch = 54,
LicenceCRCMismatch = 54,
ItemsCountMismatch = 61 ItemsCountMismatch = 61
}; };
@@ -90,9 +92,9 @@ enum class FileNameGenerationType
struct Mapping struct Mapping
{ {
std::map<string, LicenceType> licMapTypes = {{"EOV_OSV", LicenceType::EOS_EOV}, {"DDTS", LicenceType::DDTS}, {"DRT", LicenceType::DRT}}; unordered_map<string, LicenceType> licMapTypes = {{"EOV_OSV", LicenceType::EOS_EOV}, {"DDTS", LicenceType::DDTS}, {"DRT", LicenceType::DRT}};
std::map<string, EncryptionType> licMapEncryption = {{"CID_AES256", EncryptionType::CID_AES256}, {"FIX_AES256", EncryptionType::FIX_AES256}}; unordered_map<string, EncryptionType> licMapEncryption = {{"CID_AES256", EncryptionType::CID_AES256}, {"FIX_AES256", EncryptionType::FIX_AES256}};
std::map<string, PlcType> licMapPlcType = {{"WAGO", PlcType::WAGO}, {"TECO", PlcType::TECO}}; unordered_map<string, PlcType> licMapPlcType = {{"WAGO", PlcType::WAGO}, {"TECO", PlcType::TECO}};
}; };
struct ErrorMessage struct ErrorMessage

View File

@@ -1,8 +1,6 @@
#ifndef PLC_LICENCE1_COMMON_H #ifndef PLC_LICENCE1_COMMON_H
#define PLC_LICENCE1_COMMON_H #define PLC_LICENCE1_COMMON_H
#include <stdint.h>
#include <unordered_map>
#include <vector> #include <vector>
#include "licenceCommon.h" #include "licenceCommon.h"
#include "SDCard.h" #include "SDCard.h"

View File

@@ -1,8 +1,7 @@
#ifndef PLC_LICENCE2_COMMON_H #ifndef PLC_LICENCE2_COMMON_H
#define PLC_LICENCE2_COMMON_H #define PLC_LICENCE2_COMMON_H
#include <stdint.h>
#include <unordered_map>
#include <vector> #include <vector>
#include "licenceCommon.h" #include "licenceCommon.h"
#include "SDCard.h" #include "SDCard.h"
@@ -22,25 +21,6 @@ public:
ErrorMessage errorMessage; ErrorMessage errorMessage;
// enum class Error
// {
// SDCardReadError = 50,
// LicenceReadError = 51,
// LicenceSizeMismatch = 52,
// LicenceSizeCardMismatch = 53,
// LicenceMismatch = 54,
// ItemsCountMismatch = 61
// };
// unordered_map<Error, string> 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 struct LicenceId
{ {
char licIdent[5] = {'E', 'L', 'C', '0', '_'}; char licIdent[5] = {'E', 'L', 'C', '0', '_'};
@@ -73,7 +53,6 @@ public:
string projectDescription = ""; string projectDescription = "";
string date = ""; string date = "";
string licenceType = ""; string licenceType = "";
int crc = 0;
}; };
struct PrivateContent // privátní šifrovaná část struct PrivateContent // privátní šifrovaná část

View File

@@ -6,9 +6,7 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <chrono> #include <chrono>
#include <format> #include <libgen.h>
#include <libgen.h> // dirname
#include <climits>
#include <unistd.h> #include <unistd.h>
typedef uint8_t BYTE; typedef uint8_t BYTE;
@@ -42,6 +40,8 @@ void getCharsFromString(string& source, char *charArray, size_t length);
string getCompletePath(string fileName); string getCompletePath(string fileName);
void appendStringToVector(const std::string& str, std::vector<unsigned char>& charVector); void appendStringToVector(const std::string& str, std::vector<unsigned char>& charVector);
uint16_t calculateCRC16(std::vector<unsigned char>& charVector); uint16_t calculateCRC16(std::vector<unsigned char>& charVector);
uint16_t calculateCRC16(std::vector<char> &charVector);
uint16_t calculateCRC16(std::vector<unsigned char> &charVector, int removeCount);
uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4); uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4);
uint32_t bytesToWord(uint8_t byte1, uint8_t byte2); uint32_t bytesToWord(uint8_t byte1, uint8_t byte2);
vector<unsigned char> joinVectors(const std::vector<unsigned char>& vector1, const std::vector<unsigned char>& vector2); vector<unsigned char> joinVectors(const std::vector<unsigned char>& vector1, const std::vector<unsigned char>& vector2);

View File

@@ -2,7 +2,6 @@
#define LICENCE_READER_H_ #define LICENCE_READER_H_
#include "utils.h" #include "utils.h"
#include "licReaderELC1.h" #include "licReaderELC1.h"
#include "licReaderELC2.h" #include "licReaderELC2.h"
#include "licReaderELC3.h" #include "licReaderELC3.h"

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -10,17 +10,17 @@
<item> <item>
<name>Položka licence 1</name> <name>Položka licence 1</name>
<protoId>111</protoId> <protoId>111</protoId>
<dataPointsCount>7454</dataPointsCount> <dataPointsCount>40000</dataPointsCount>
</item> </item>
<item> <item>
<name>Položka licence 2</name> <name>Položka licence 2</name>
<protoId>222</protoId> <protoId>222</protoId>
<dataPointsCount>6477</dataPointsCount> <dataPointsCount>51000</dataPointsCount>
</item> </item>
<item> <item>
<name>Položka licence 3</name> <name>Položka licence 3</name>
<protoId>333</protoId> <protoId>333</protoId>
<dataPointsCount>7844</dataPointsCount> <dataPointsCount>62000</dataPointsCount>
</item> </item>
</items> </items>

Binary file not shown.

View File

@@ -1,7 +1,6 @@
#define LINUX 1 #define LINUX 1
// #define WINDOWS 1 // #define WINDOWS 1
#include <stdio.h>
#include "licenceReader.h" #include "licenceReader.h"
/// @brief hlavní funkce /// @brief hlavní funkce
@@ -10,6 +9,7 @@
/// @return /// @return
int main() int main()
{ {
try try
{ {
InitStructure initStructure; 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; } // 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 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{}; LicenceReader licenceReaderELC1{};
if (licenceReaderELC1.initread(1, initStructure)) // iniciacni nacteni if (licenceReaderELC1.initread(1, initStructure)) // iniciacni nacteni
{ {
@@ -43,7 +43,6 @@ int main()
// verze ELC 2 pro jeden protokol // verze ELC 2 pro jeden protokol
LicenceReader licenceReaderELC2{}; LicenceReader licenceReaderELC2{};
if (licenceReaderELC2.initread(2, initStructure)) // iniciacni nacteni if (licenceReaderELC2.initread(2, initStructure)) // iniciacni nacteni
{ {
// v případě kompatibility 0, či nezadané výstup nativně je defaultní. // v případě kompatibility 0, či nezadané výstup nativně je defaultní.
@@ -52,7 +51,7 @@ int main()
// if () // if ()
int protocolId = 333; int protocolId = 333;
// if (initStructure.compatibility == 1) //ukazka kompatibilita // if (initStructure.compatibility == 1) //ukazka kompatibilita 1
// { // {
// LicenceELC2Item_1 info; //jiná struktura, zbytek stejný // LicenceELC2Item_1 info; //jiná struktura, zbytek stejný
// if (licenceReaderELC2.getLicenceItemInfo(protocolId, &info)) // if (licenceReaderELC2.getLicenceItemInfo(protocolId, &info))
@@ -75,8 +74,7 @@ int main()
} }
LicenceReader licenceReaderCompleteELC2{}; LicenceReader licenceReaderCompleteELC2{};
//verze ELC 2 kompletní načtení
// verze ELC 2 kompletní načtení
if (licenceReaderCompleteELC2.init(2, initStructure)) // iniciacni nacteni if (licenceReaderCompleteELC2.init(2, initStructure)) // iniciacni nacteni
{ {
LicenceELC2Info info; // podle ELC a kompatibility určit strukuru (LicenceInfo11, LicenceInfo21, LicenceInfo31) LicenceELC2Info info; // podle ELC a kompatibility určit strukuru (LicenceInfo11, LicenceInfo21, LicenceInfo31)
@@ -96,20 +94,29 @@ int main()
} }
else else
cout << "CHYBA: " << licenceReaderCompleteELC2.error.message; cout << "CHYBA: " << licenceReaderCompleteELC2.error.message;
} }
else else
{ {
cout << "CHYBA: " << licenceReaderCompleteELC2.error.message; cout << "CHYBA: " << licenceReaderCompleteELC2.error.message;
} }
} }
catch (...) catch (...)
{ {
cout << "Obecna chyba\n"; cout << "Obecna chyba\n";
} }
// system("pause"); // system("pause");
return SUCCES; return SUCCES;
} }
// cid: 0353445355313647801b1a9f6600c747 csd: 400e00325b59000076b27f800a404013 /*
// https://sprava.app.elzel.cz/device/417 cid: 9f54495344434954615ad803c50171bf csd: 400e00325b5900003be77f800a400043 CRC public size: 353
CRC gen public: 27885
CRC private size: 285
CRC gen private: 18500
CRC complete size: 638
CRC gen complete: 42051
*/

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -338,6 +338,33 @@ void appendStringToVector(const std::string &str, std::vector<unsigned char> &ch
} }
} }
uint16_t calculateCRC16(std::vector<char> &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<unsigned char> &charVector) uint16_t calculateCRC16(std::vector<unsigned char> &charVector)
{ {
const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial
@@ -365,6 +392,34 @@ uint16_t calculateCRC16(std::vector<unsigned char> &charVector)
return crc; return crc;
} }
uint16_t calculateCRC16(std::vector<unsigned char> &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) uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
{ {
return static_cast<uint32_t>(byte1) | return static_cast<uint32_t>(byte1) |

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +1,5 @@
#include "licGenELC2.h" #include "licGenELC2.h"
namespace Generator 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->cid = cid;
this->csd = csd; this->csd = csd;
this->xmlDoc = xmlDoc; 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() void Licence2::getHeader()
@@ -90,195 +92,215 @@ namespace Generator
} }
string Licence2::getVersion(int middleVersion) 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; string result = "";
this->lIdentification.licTypeName = licType; result.append(to_string(generatorVersion));
} result.append(".");
else result.append(to_string(middleVersion));
{ result.append(".");
errorMessage.code = (int)GeneralError::LicenceMismatch; string tempLicenceCount = "3"; // TODO
errorMessage.message = "ERROR MISSING licenceType"; result.append(tempLicenceCount);
return false; return result;
} }
this->lIdentification.licenceVersion = atoi(&xmlDoc->child(dataRootName).child("licenceType").attribute("licenceVersion").value()[0]); bool Licence2::processInputConfiguration()
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]; const char *dataRootName = "licence";
}
this->projectDescription = &xmlDoc->child(dataRootName).child("project").child_value()[0]; string licType = xmlDoc->child(dataRootName).child("licenceType").child_value();
if (!licType.empty())
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++; this->lIdentification.licLicenceType = mapping.licMapTypes[licType]; // LicenceType::EOS_EOV;
if (nodeIndex == 1) this->lIdentification.licTypeName = licType;
{
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 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() 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<unsigned char> publicContent;
vector<unsigned char> privateContent;
vector<unsigned char> 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)
{ {
privateContent.push_back(dataItem.protoId & 0xFF); getLicenceItems();
privateContent.push_back((dataItem.protoId >> 8) & 0xFF); getHeader();
privateContent.push_back(dataItem.licCount & 0xFF);
privateContent.push_back((dataItem.licCount >> 8) & 0xFF); sdCard = SDCard(this->cid, this->csd);
for (unsigned int i = 0; i < sizeof(dataItem.dummy); i++) if (sdCard.isLoaded == false)
privateContent.push_back(i); 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<unsigned char> publicContent;
vector<unsigned char> privateContent;
vector<unsigned char> 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<unsigned char> 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<unsigned char>(outputFile));
std::copy(privateContentEncrypted.cbegin(), privateContentEncrypted.cend(), std::ostream_iterator<unsigned char>(outputFile));
outputFile.close();
cout << licfileName;
return true;
} }
vector<unsigned char> 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<unsigned char>(outputFile));
std::copy(privateContentEncrypted.cbegin(), privateContentEncrypted.cend(), std::ostream_iterator<unsigned char>(outputFile));
outputFile.close();
cout << licfileName;
return true;
}
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -46,18 +46,38 @@ namespace Reader
//~~~uint16_t crcPublic = bytesToWord(content[18 + licBody.licenceIdentHeader.publicHeaderLength], content[19 + licBody.licenceIdentHeader.publicHeaderLength]); //~~~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) if (elcVersion != (int)this->lIdentification.licElcType)
{ {
throw LicenceException((int)GeneralError::ELCMismatch, "Nesouhlasí ELC."); throw LicenceException((int)GeneralError::ELCMismatch, "Nesouhlasí ELC.");
} }
vector<unsigned char> encryptedPart(content.begin() + licBody.licenceIdentHeader.publicHeaderLength + 20, content.begin() + content.size()); vector<unsigned char> 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<unsigned char> 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<unsigned char> privateContentDecrypted; vector<unsigned char> privateContentDecrypted;
privateContentDecrypted = decryptPrivateContent(encryptedPart); 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; LicenceBody licBodyDecrypted;
licBodyDecrypted.licenceIdentHeader.licenceType = privateContentDecrypted[0]; licBodyDecrypted.licenceIdentHeader.licenceType = privateContentDecrypted[0];
licBodyDecrypted.licenceIdentHeader.licenceTypeVersion = privateContentDecrypted[1]; licBodyDecrypted.licenceIdentHeader.licenceTypeVersion = privateContentDecrypted[1];
@@ -68,8 +88,6 @@ namespace Reader
licBodyDecrypted.licenceIdentHeader.cardSize = bytesToWord(privateContentDecrypted[7], privateContentDecrypted[8]); licBodyDecrypted.licenceIdentHeader.cardSize = bytesToWord(privateContentDecrypted[7], privateContentDecrypted[8]);
licBodyDecrypted.licenceIdentHeader.serialNumber = bytesToDword(privateContentDecrypted[9], privateContentDecrypted[10], privateContentDecrypted[11], privateContentDecrypted[12]); licBodyDecrypted.licenceIdentHeader.serialNumber = bytesToDword(privateContentDecrypted[9], privateContentDecrypted[10], privateContentDecrypted[11], privateContentDecrypted[12]);
//~~~CRC
if (licBodyDecrypted.licenceIdentHeader.licItemCount != this->licBody.licenceIdentHeader.licItemCount) if (licBodyDecrypted.licenceIdentHeader.licItemCount != this->licBody.licenceIdentHeader.licItemCount)
{ {
throw LicenceException((int)GeneralError::ItemsCountMismatch, "Nesouhlasí počet položek licence."); throw LicenceException((int)GeneralError::ItemsCountMismatch, "Nesouhlasí počet položek licence.");
@@ -86,6 +104,12 @@ namespace Reader
this->licenceInfo.licences.insert(pair<int, int>(item.protoId, item.licCount)); this->licenceInfo.licences.insert(pair<int, int>(item.protoId, item.licCount));
licences->licences.insert(pair<int, int>(item.protoId, item.licCount)); licences->licences.insert(pair<int, int>(item.protoId, item.licCount));
} }
uint16_t crcComplete = bytesToWord(privateContentDecrypted[index], privateContentDecrypted[index + 1]);
vector<unsigned char> completeVector = joinVectors(publicPart, privateContentDecrypted);
if (calculateCRC16(completeVector, 2) != crcComplete) throw LicenceException((int)GeneralError::LicenceCRCMismatch, "Nesouhlasí CRC.");
return true; return true;
} }

Binary file not shown.