testování filip

This commit is contained in:
2024-03-28 14:17:49 +01:00
parent 22a97056f9
commit 2175852bc0
47 changed files with 882 additions and 114 deletions

View File

@@ -8,7 +8,7 @@
/// @param argc /// @param argc
/// @param argv parametry pro generování licence /// @param argv parametry pro generování licence
/// @return /// @return
int main(int argc, char *argv[]) int main5(int argc, char *argv[])
{ {
unordered_map<string, string> arguments = getArguments(argc, argv); unordered_map<string, string> arguments = getArguments(argc, argv);
try try

View File

@@ -7,7 +7,7 @@
/// @param argc /// @param argc
/// @param argv parametry pro generování licence /// @param argv parametry pro generování licence
/// @return /// @return
int main6() int main()
{ {
try try

View File

@@ -1,2 +0,0 @@
src/common/SDCard.o: src/common/SDCard.cpp include/common/utils.h \
include/common/SDCard.h

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -315,7 +315,6 @@ string getCompletePath(string fileName)
#ifdef WINDOWS #ifdef WINDOWS
return fileName; return fileName;
#else #else
char path[PATH_MAX + 1] = {}; char path[PATH_MAX + 1] = {};
ssize_t length = readlink("/proc/self/exe", path, PATH_MAX); ssize_t length = readlink("/proc/self/exe", path, PATH_MAX);
path[length] = '\0'; path[length] = '\0';

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -1,2 +0,0 @@
src/generator/licGenELC3.o: src/generator/licGenELC3.cpp \
include/generator/licGenELC3.h

Binary file not shown.

View File

@@ -1,2 +0,0 @@
src/generator/pugixml.o: src/generator/pugixml.cpp \
include/generator/pugixml.hpp include/generator/pugiconfig.hpp

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -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

Binary file not shown.

View File

@@ -1,2 +0,0 @@
src/reader/licReaderELC3.o: src/reader/licReaderELC3.cpp \
include/reader/licReaderELC3.h

Binary file not shown.

View File

@@ -19,10 +19,10 @@ LFLAGS =
OUTPUT := output OUTPUT := output
# define source directory # define source directory
SRC := src src/common src/reader SRC := src src/common src/reader src/generator
# define include directory # define include directory
INCLUDE := include include/common include/reader INCLUDE := include include/common include/reader include/generator
# define lib directory # define lib directory
LIB := lib LIB := lib

View File

@@ -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

View File

@@ -0,0 +1,210 @@
#define CRC = 1
#ifndef LICENCE_COMMON_H_
#define LICENCE_COMMON_H_
//---------------- společná hlavička pro všechny licence ----------------
#include <cstring>
#include <string>
#include <unordered_map>
#include <stdint.h>
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<string, LicenceType> licMapTypes = {{"EOV_OSV", LicenceType::EOS_EOV}, {"DDTS", LicenceType::DDTS}, {"DRT", LicenceType::DRT}};
unordered_map<string, EncryptionType> licMapEncryption = {{"CID_AES256", EncryptionType::CID_AES256}, {"FIX_AES256", EncryptionType::FIX_AES256}};
unordered_map<string, PlcType> 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<int, int> 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<int, int> 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<int, int> licences = {};
};
struct LicenceELC2Info_1
{
unordered_map<int, int> 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

View File

@@ -0,0 +1,265 @@
#ifndef PLC_LICENCE1_COMMON_H
#define PLC_LICENCE1_COMMON_H
#include <vector>
#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

View File

@@ -0,0 +1,86 @@
#ifndef PLC_LICENCE2_COMMON_H
#define PLC_LICENCE2_COMMON_H
#include <vector>
#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<licDataItem> dataItems;
WORD crc = 0;
};
struct LicenceBody
{
LicenceId licId;
LicenceIdentDataHeader licenceIdentHeader;
string publicHeader = ""; // JSON
PrivateContent privateContent;
};
LicenceBody licBody;
SDCard sdCard;
LicenceIdentification lIdentification;
vector<unsigned char> cryptPrivateContent(const std::vector<unsigned char> &content);
vector<unsigned char> decryptPrivateContent(const std::vector<unsigned char> &content);
void initVector(BYTE (&iVector)[], BYTE (&key)[]);
string getLicenceName();
private:
};
#endif

View File

@@ -0,0 +1,72 @@
#ifndef UTILS_H_
#define UTILS_H_
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <chrono>
#include <libgen.h>
#include <unistd.h>
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<string, string> 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<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 bytesToWord(uint8_t byte1, uint8_t byte2);
vector<unsigned char> joinVectors(const std::vector<unsigned char>& vector1, const std::vector<unsigned char>& vector2);
bool readFile(string fileName, vector<char> &output);
#endif

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,9 @@
#ifndef ELC3_READER__H
#define ELC3_READER__H
namespace Reader
{
}
#endif

View File

@@ -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

View File

@@ -315,7 +315,6 @@ string getCompletePath(string fileName)
#ifdef WINDOWS #ifdef WINDOWS
return fileName; return fileName;
#else #else
char path[PATH_MAX + 1] = {}; char path[PATH_MAX + 1] = {};
ssize_t length = readlink("/proc/self/exe", path, PATH_MAX); ssize_t length = readlink("/proc/self/exe", path, PATH_MAX);
path[length] = '\0'; path[length] = '\0';

View File

@@ -1,27 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<licence elc="2" xmlVersion="1" revision="1" compatibility="1"> <licence elc="2" xmlVersion="1" revision="1" compatibility="1">
<licenceType licenceVersion="1" licenceIndex="0">EOV_OSV</licenceType> <licenceType licenceVersion="1" licenceIndex="0">EOV_OSV</licenceType>
<plcType>WAGO</plcType> <plcType>wago</plcType>
<licenceName>Licence pro EOV_OSV</licenceName> <licenceName>Licence pro EOV_OSV</licenceName>
<station>odb.zaluzi.drt.cz</station> <project>testovaci projekt</project>
<distributor>POZ8</distributor> <items>
<project>Projekt XXX stanice YYY</project> <item>
<items> <name>pt_Ez_iec104c</name>
<item> <protoId>5</protoId>
<name>Položka licence 1</name> <dataPointsCount>2500</dataPointsCount>
<protoId>111</protoId> </item>
<dataPointsCount>40000</dataPointsCount> <item>
</item> <name>pt_Ez_iec104s</name>
<item> <protoId>6</protoId>
<name>Položka licence 2</name> <dataPointsCount>15000</dataPointsCount>
<protoId>222</protoId> </item>
<dataPointsCount>51000</dataPointsCount> <item>
</item> <name>pt_Ez_mdbipc</name>
<item> <protoId>9</protoId>
<name>Položka licence 3</name> <dataPointsCount>250</dataPointsCount>
<protoId>333</protoId> </item>
<dataPointsCount>62000</dataPointsCount> <item>
</item> <name>pt_Ez_mdbips</name>
</items> <protoId>10</protoId>
<dataPointsCount>400</dataPointsCount>
</item>
</items>
</licence> </licence>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<licence elc="2" xmlVersion="1" revision="1" compatibility="1">
<licenceType licenceVersion="1" licenceIndex="0">EOV_OSV</licenceType>
<plcType>WAGO</plcType>
<licenceName>Licence pro EOV_OSV</licenceName>
<station>odb.zaluzi.drt.cz</station>
<distributor>POZ8</distributor>
<project>Projekt XXX stanice YYY</project>
<items>
<item>
<name>Položka licence 1</name>
<protoId>111</protoId>
<dataPointsCount>40000</dataPointsCount>
</item>
<item>
<name>Položka licence 2</name>
<protoId>222</protoId>
<dataPointsCount>51000</dataPointsCount>
</item>
<item>
<name>Položka licence 3</name>
<protoId>333</protoId>
<dataPointsCount>62000</dataPointsCount>
</item>
</items>
</licence>

View File

@@ -64,7 +64,7 @@ bool LicenceReader::initread(int elcType, InitStructure &initStructure)
switch (this->licIdentification.licElcType) switch (this->licIdentification.licElcType)
{ {
case ELCType::ELC1: case ELCType::ELC1:
{ // old eoseov {
Reader::Licence1 licenceELC1 = Reader::Licence1(this->licIdentification); Reader::Licence1 licenceELC1 = Reader::Licence1(this->licIdentification);
this->licence1 = &licenceELC1; this->licence1 = &licenceELC1;
licenceELC1.cid_cdsPath = initStructure.cid_csd_filePath; licenceELC1.cid_cdsPath = initStructure.cid_csd_filePath;
@@ -143,7 +143,7 @@ bool LicenceReader::getLicenceItemInfo(int protocolId, void *returnItemStructure
if (!this->licIdentification.licCompatibility) // defaultní kompatibilita if (!this->licIdentification.licCompatibility) // defaultní kompatibilita
{ {
LicenceELC2Item *resultPtr = static_cast<LicenceELC2Item *>(returnItemStructure); LicenceELC2Item *resultPtr = static_cast<LicenceELC2Item *>(returnItemStructure);
resultPtr->protocolId = protocolId; // protocolId; resultPtr->protocolId = protocolId;
if (this->licenceInfo.licences.count(protocolId)) if (this->licenceInfo.licences.count(protocolId))
resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId); resultPtr->dataPointsCount = this->licenceInfo.licences.at(protocolId);
else else

View File

@@ -62,10 +62,6 @@ namespace Reader
vector<unsigned char> encryptedPart(content.begin() + licBody.licenceIdentHeader.publicHeaderLength + 18, content.begin() + content.size()); 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);

Binary file not shown.