From 1fd6bfde3e22db8407b55347606c1c1d08ce5f99 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 11 Jan 2017 21:38:05 +0100 Subject: [PATCH] Implemented PKP and BKP code generation. Certificate path temporary hardcoded. --- libEet/eetsigner.cpp | 39 ++++++++++++++++++++++++++++++++ libEet/eetsigner.h | 24 ++++++++++++++++++++ libEet/eettemplate.cpp | 50 ++++++++++++++++++++++++++++++++++++++++-- libEet/eettemplate.h | 3 +++ libEet/libEet.pro | 8 +++++-- 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 libEet/eetsigner.cpp create mode 100644 libEet/eetsigner.h diff --git a/libEet/eetsigner.cpp b/libEet/eetsigner.cpp new file mode 100644 index 0000000..43f4638 --- /dev/null +++ b/libEet/eetsigner.cpp @@ -0,0 +1,39 @@ +#include "eetsigner.h" + +#include +#include + +EetSigner::EetSigner(QObject *parent) : QObject(parent) +{ + +} + +QByteArray EetSigner::signData(const QByteArray &data) +{ + if (!QCA::isSupported("sha256")) + { + return QByteArray(); + } + + QCA::ConvertResult result; + QCA::KeyBundle bundle = QCA::KeyBundle::fromFile("/home/pepa/Dokumenty/dev/eet/01000003.p12", QCA::SecureArray("eet"), &result); + + if (result != QCA::ConvertGood || bundle.isNull()) + { + return QByteArray(); + } + + QCA::PrivateKey privKey = bundle.privateKey(); + + return privKey.signMessage(QCA::MemoryRegion(data), QCA::EMSA3_SHA256); +} + +QByteArray EetSigner::sha1HashData(const QByteArray &data) +{ + if (!QCA::isSupported("sha1")) + { + return QByteArray(); + } + + return QCA::Hash("sha1").hash(QCA::MemoryRegion(data)).toByteArray(); +} diff --git a/libEet/eetsigner.h b/libEet/eetsigner.h new file mode 100644 index 0000000..3f1bc08 --- /dev/null +++ b/libEet/eetsigner.h @@ -0,0 +1,24 @@ +#ifndef EETSIGNER_H +#define EETSIGNER_H + +#include +#include +#include + +class EetSigner : public QObject +{ + Q_OBJECT +public: + explicit EetSigner(QObject *parent = 0); + QByteArray signData(const QByteArray &data); + QByteArray sha1HashData(const QByteArray &data); + +private: + QCA::Initializer m_qcaInit; + +signals: + +public slots: +}; + +#endif // EETSIGNER_H diff --git a/libEet/eettemplate.cpp b/libEet/eettemplate.cpp index 5bbee5f..d5b9053 100644 --- a/libEet/eettemplate.cpp +++ b/libEet/eettemplate.cpp @@ -18,7 +18,32 @@ EetTemplate::EetTemplate(const QString &tmp, QObject *parent) : QObject(parent) QString EetTemplate::fillTemplate(EetRequest *request) { - return fillTemplateInternal(request, nullptr); + m_template = fillTemplateInternal(request, nullptr); + + QString pkp = getPkpString(request); + + QByteArray signedPkp = m_signer.signData(pkp.toUtf8()); + QByteArray sha1Bkp = m_signer.sha1HashData(signedPkp); + + QString base64Sign(signedPkp.toBase64()); + QString base16bkp(sha1Bkp.toHex()); + QString splitedBkp; + + for (int i = 0; i < base16bkp.length(); i++) + { + if (i > 0 && i % 8 == 0) + { + splitedBkp += "-"; + } + + splitedBkp += base16bkp[i]; + } + + QMap varMap; + varMap["pkp"] = base64Sign; + varMap["bkp"] = splitedBkp.toUpper(); + + return fillTemplate(varMap); } QString EetTemplate::fillTemplate(QMap map) @@ -69,7 +94,17 @@ QString EetTemplate::fillTemplateInternal(EetRequest *request, QMapproperty("dic_popl").toString(), + request->property("id_provoz").toString(), + request->property("id_pokl").toString(), + request->property("porad_cis").toString(), + request->property("dat_trzby").toString(), + request->property("celk_trzba").toString()); + return ret; +} diff --git a/libEet/eettemplate.h b/libEet/eettemplate.h index 4b8f986..0ab6ac6 100644 --- a/libEet/eettemplate.h +++ b/libEet/eettemplate.h @@ -6,6 +6,7 @@ #include #include "eetrequest.h" +#include "eetsigner.h" #define BODY_TEMPLATE ":/res/template_body.txt" #define REQUEST_TEMPLATE ":/res/template_request.txt" @@ -28,6 +29,8 @@ public slots: private: QString m_template; QString fillTemplateInternal(EetRequest *request, QMap *map); + QString getPkpString(EetRequest *request); + EetSigner m_signer; }; #endif // EETTEMPLATE_H diff --git a/libEet/libEet.pro b/libEet/libEet.pro index baa674f..cef5fd0 100644 --- a/libEet/libEet.pro +++ b/libEet/libEet.pro @@ -16,18 +16,22 @@ DEFINES += EETCPP_LIBRARY SOURCES += eetcpp.cpp \ eetrequest.cpp \ eettemplate.cpp \ - eetsender.cpp + eetsender.cpp \ + eetsigner.cpp HEADERS += eetcpp.h\ eetcpp_global.h \ eetrequest.h \ eettemplate.h \ - eetsender.h + eetsender.h \ + eetsigner.h unix { target.path = /usr/lib INSTALLS += target } +LIBS += -lqca-qt5 + RESOURCES += \ resource.qrc