This commit is contained in:
2024-01-24 08:25:44 +01:00
parent f260e92596
commit 36a799057c
42 changed files with 926 additions and 2313 deletions

View File

@@ -45,6 +45,7 @@ class SDCard
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();

View File

@@ -15,8 +15,6 @@ const int cidSize = 32;
const int csdSize = 32;
const int generatorVersion = 1;
enum class GeneralError
{
GeneralError = 1,
@@ -27,18 +25,11 @@ enum class GeneralError
LicenceTypeNotImplemented = 6,
CompatibilityTypeNotImplemented = 7,
ELCMismatch = 8,
CRCMismatch = 9
CRCMismatch = 9,
EncryptError = 10,
DecryptError = 11
};
// map<GeneralError, string> mapGeneralErrors = {
// // 1,"test"
// // {CommonError::GeneralError, "Obecná chyba."},
// // {CommonError::FileReadError, "Chyba čtení souboru."},
// // {CommonError::FileReadError, "Chyba zápisu souboru."},
// // {CommonError::ELCNotImplemented, "ELC není implementováno."},
// // {CommonError::LicenceTypeNotImplemented, "Typ licence není implementován"}
// };
enum class ELCType
{
ELC1 = 1,

View File

@@ -146,7 +146,7 @@ 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
LicenceDataMainHeaderELC licHeader {}; // nekodovana cast licence
} LicenceDataMain;
// polozkova cast licence verze 1.0
@@ -181,9 +181,9 @@ typedef struct 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
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;
@@ -247,8 +247,6 @@ class LicenceELC11
string dataGenerationType = ""; //version of dataGeneration from xmlFile
string dataLicenceDataFileName = ""; //name of licence file to read
//InputLicenceType dataLicenceType = InputLicenceType::EOSEOV;
CryptData cryptData; // structure for encryp
CidData sdData; // data loaded from SD card
@@ -275,7 +273,6 @@ public:
LicenceELC11(LicenceIdentification & licIdentification);
bool createLicence(); // creates licence
bool readLicence(string dataFileName, string licenceType, string licenceVersion, string cidArg, string csdArg);
bool readLicence(LicenceInfoGeneral * licences);

View File

@@ -1,11 +1,9 @@
#ifndef PLC_LICENCE21_H
#define PLC_LICENCE21_H
#include <stdint.h>
#include <iostream>
#include <unordered_map>
#include <vector>
#include "utils.h"
#include <stdint.h>
#include <unordered_map>
#include "licenceCommon.h"
#include "SDCard.h"
@@ -13,8 +11,25 @@ using namespace std;
class LicenceELC21
{
public:
ErrorMessage errorMessage;
LicenceInfo21 licenceInfo;
string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/
string licenceFilePath = ""; //"c:\\_projects\\LicenceGenerator\\output\\";
LicenceELC21();
~LicenceELC21();
LicenceELC21(LicenceIdentification & licIdentification, LicData & icData);
LicenceELC21(LicenceIdentification & licIdentification);
LicenceELC21(LicData & icData);
bool createLicence();
bool readLicence(LicenceInfoGeneral * licences);
int getDataPointsCount(int protocolId);
bool getLicenceInfo(void * ptr);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
enum class Error21
{
SDCardReadError = 50,
@@ -34,14 +49,12 @@ class LicenceELC21
{Error21::ItemsCountMismatch, "Nesouhlasí počet položek licence."},
};
struct LicenceId
{
char licIdent[5] = { 'E', 'L', 'C', '0','_'};
};
//struct __attribute__((__packed__)) LicencePublicHeader
struct LicenceIdentDataHeader
{
BYTE licenceType = 0; //EOSEOV, DRT ...
@@ -102,26 +115,6 @@ class LicenceELC21
void initVector(BYTE (&iVector)[], BYTE (&key)[]);
string getLicenceName();
public:
ErrorMessage errorMessage;
LicenceInfo21 licenceInfo;
string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/
string licenceFilePath = ""; //"c:\\_projects\\LicenceGenerator\\output\\";
LicenceELC21();
~LicenceELC21();
LicenceELC21(LicenceIdentification & licIdentification, LicData & icData);
LicenceELC21(LicenceIdentification & licIdentification);
LicenceELC21(LicData & icData);
bool createLicence();
bool readLicence(LicenceInfoGeneral * licences);
int getDataPointsCount(int protocolId);
bool getLicenceInfo(int protocolId, void * ptr);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
};
#endif

View File

@@ -13,6 +13,8 @@ class LicenceELC31
{
LicData lData;
bool processInputConfiguration();
LicenceIdentification lIdentification;
public:
LicenceELC31();
@@ -22,7 +24,6 @@ class LicenceELC31
void createLicence();
void readLicence();
int getDataPointsCount(int protocolId);
bool getLicenceInfo(int protocolId, void * ptr);
};
#endif

View File

@@ -17,17 +17,6 @@
#include "licenceELC31.h"
using namespace std;
struct InitStructure
{
int elcType = 0;
int licenceType = 0;
int licenceVersion = 0;
int licenceIndex = 0;
int compatibility = 0;
string licenceFilePath = "";
string cid_csd_filePath = "";
};
class LicenceGenerator
{
@@ -44,14 +33,8 @@ public:
LicenceGenerator();
~LicenceGenerator();
LicenceGenerator(map<string, string> &arguments);
// LicenceGenerator(string cid, string csd, string dataFileName, string binaryType);
LicenceGenerator(string uid, string cid, string csd, string dataFileName);
LicenceGenerator(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool init(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool init(InitStructure &initStructure);
bool initread(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool initread(InitStructure &initStructure);
void createLicenceFile();
void readLicenceFile();

60
include/licenceReader.h Normal file
View File

@@ -0,0 +1,60 @@
#ifndef LICENCE_READER_H_
#define LICENCE_READER_H_
#include "utils.h"
// #include "licenceCommon.h"
#include "licenceELC11.h"
#include "licenceELC21.h"
#include "licenceELC31.h"
using namespace std;
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;
LicenceELC11 *licence11;
LicenceELC21 *licence21;
LicenceELC31 *licence31;
LicenceReader();
~LicenceReader();
bool init(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool init(int elcType, InitStructure &initStructure);
bool initread(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool initread(int elcType, InitStructure &initStructure);
void readLicenceFile();
bool getLicenceInfo(void *returnStructure);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
LicenceInfoGeneral licenceInfo;
private:
LicenceIdentification licIdentification;
LicData licData;
string configFileName = "";
void *licence = nullptr;
void readLicenceELC1();
void readLicenceELC2();
void readLicenceELC3();
};
#endif

View File

@@ -1,258 +0,0 @@
#ifndef PLC_LICENCE_H
#define PLC_LICENCE_H
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <wchar.h>
#include <time.h>
#include <map>
#include <stdint.h>
using namespace std;
#define MAX_LICENCE_COUNT 100
enum InputLicenceType
{
EOSEOV = 1
};
enum BinaryGenerationType
{
Base64Cout = 1,
File = 2
};
enum LicenceType
{
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;
/// @brief licence class
class PlcLicence
{
private:
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
//InputLicenceType dataLicenceType = InputLicenceType::EOSEOV;
CryptData cryptData; // structure for encryp
CidData sdData; // data loaded from SD card
void processConfigFile(string &dataFileName); //process data from config file
void getSDData(); // reads SD card
string getLicenceName(BYTE licPostfix); // get proper licencename
void initCrypto();
bool CreateEosEovLicence(); // create licence for EOV_OSV
bool ReadEosEovLicence(string dataFileName);
public:
string operationErrors = "";
PlcLicence();
PlcLicence(map<string, string> & arguments);
PlcLicence(string cid, string csd, string binaryType, string dataFileName);
bool CreateLicence(); // creates licence
bool ReadLicence(string dataFileName, string licenceType, string licenceVersion, string cidArg, string csdArg);
// bool CreateEosEovLicence();
};
#endif

View File

@@ -1,27 +1,17 @@
#define LINUX 1
//#define LINUX 1
#define WINDOWS 1
#ifndef UTILS_H_
#define UTILS_H_
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <chrono>
#include <bitset>
#include <unordered_map>
#include <filesystem>
#include <vector>
#define PEM_BUFSIZE_TEXT 1024
#define BitVal(data, y) ((data >> y) & 1) /** Return Data.Y value **/
#define SetBit(data, y) data |= (1 << y) /** Set Data.Y to 1 **/
#define ClearBit(data, y) data &= ~(1 << y) /** Clear Data.Y to 0 **/
#define TogleBit(data, y) (data ^= BitVal(y)) /** Togle Data.Y value **/
#define Togle(data) (data = ~data) /** Togle Data value **/
#define PEM_BUFSIZE_TEXT 1024
#include <chrono>
#include <format>
#include <libgen.h> // dirname
#include <climits>
typedef uint8_t BYTE;
typedef uint16_t WORD;
@@ -35,45 +25,32 @@ struct DateAndTime {
uint16_t time; // TIME data type in CODESYS
};
void TestFunction(int);
void generatePause();
string getXmlContentFromString(string source, string xmlTag);
float byteArrayToFloat(const BYTE* byteArray);
std::string right(const std::string& sourceString, size_t numChars);
std::tm dt_to_date(const std::chrono::system_clock::time_point& tp);
void getCharsFromString(string source, char *charArray);
std::string parseCID(const std::string& cidHex);
void bitsToCharArray(const std::string& bitsStr, char* charArray);
uint16_t calculateCRC16(const uint8_t* data, size_t length);
uint16_t calculateCrc(const uint8_t* data, size_t length);
WORD getCurrentDateAsCODESYSDate();
DWORD getCurrentDateAsCODESYSDateDword();
DateAndTime getCurrentDateTimeAsCODESYSDateTime();
uint16_t convertToCODESYSDate(int day, int month, int year);
uint16_t convertToCODESYSTime(int hour, int minute);
DATE getLicDate();
string getDate();
int encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext);
bool CompareFiles(const char *fileName, const char *fileName1);
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);
std::string base64_encode_ai(const std::string &input);
std::string base64_decode_ai(const std::string &encoded);
string base64_encode_ai(const std::string &input);
unordered_map<string, string> getArguments(int argc, char *argv[]);
unordered_map<string, const char*> mapArguments(int argc, char *argv[]);
char* getCharArray(string source);
void getCharsFromString1(string source, char *charArray);
void getCharsFromString(string& source, char *charArray, int length);
void getCharsFromString(string& source, char *charArray, size_t length);
string getCompletePath(string fileName);
char* getFileContent(string fileName);
void appendStringToVector(const std::string& str, std::vector<unsigned char>& charVector);
void coutVector (std::vector<unsigned char>& charVector);
uint16_t calculateCRC16(std::vector<unsigned char>& charVector);
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<unsigned char> joinVectors(const std::vector<unsigned char>& vector1, const std::vector<unsigned char>& vector2);
string readFileToString(const std::string& filename, std::size_t sizeS);
bool readFile(string fileName, vector<char> &output);
#endif