diff --git a/eetsend/main.cpp b/eetsend/main.cpp index 3e47923..c060e9b 100644 --- a/eetsend/main.cpp +++ b/eetsend/main.cpp @@ -17,6 +17,7 @@ int main(int argc, char *argv[]) request.setCelkTrzba(100); EetSender sender; + sender.setupSigner("/home/pepa/Dokumenty/dev/eet/01000003.p12", "eet"); sender.sendRequest(&request); return a.exec(); diff --git a/libEet/eetsender.cpp b/libEet/eetsender.cpp index 565a022..3f8a19f 100644 --- a/libEet/eetsender.cpp +++ b/libEet/eetsender.cpp @@ -1,16 +1,51 @@ #include "eetsender.h" +#include "eetsigner.h" #include EetSender::EetSender(QObject *parent) : QObject(parent) { - + m_signer = nullptr; } void EetSender::sendRequest(EetRequest *request) { + if (m_signer == nullptr) + { + emit certError(); + return; + } + + request->setUuidZpravy(QUuid::createUuid()); + EetTemplate tempBody(BODY_TEMPLATE); + tempBody.setSigner(m_signer); QString strBody = tempBody.fillTemplate(request); - qDebug() << strBody; + QByteArray digest = m_signer->sha256HashData(strBody.toUtf8()); + QMap val; + val["digest"] = QString(digest.toBase64()); + EetTemplate tempSignature(SIGNATURE_TEMPLATE); + QString strSignature = tempSignature.fillTemplate(val); + + QByteArray sign = m_signer->signData(strSignature.toUtf8()); + val["signature"] = QString(sign.toBase64()); + val["soap:Body"] = strBody; + val["certb64"] = m_signer->getCertificate(); + EetTemplate tempRequest(REQUEST_TEMPLATE); + + QString strRequest = tempRequest.fillTemplate(val); + + qDebug() << strRequest; +} + +void EetSender::setupSigner(const QString &certPath, const QString &passwd) +{ + if (m_signer != nullptr) + { + delete m_signer; + } + + m_signer = new EetSigner(this); + m_signer->setup(certPath, QCA::SecureArray(passwd.toUtf8())); } diff --git a/libEet/eetsender.h b/libEet/eetsender.h index a38d677..b202383 100644 --- a/libEet/eetsender.h +++ b/libEet/eetsender.h @@ -14,8 +14,15 @@ public: explicit EetSender(QObject *parent = 0); void sendRequest(EetRequest *request); + void setupSigner(const QString &certPath, const QString &passwd); + +private: + EetSigner *m_signer; signals: + void certError(); + void sendError(); + void responseRecieved(); public slots: }; diff --git a/libEet/eetsigner.cpp b/libEet/eetsigner.cpp index 43f4638..ef61ec4 100644 --- a/libEet/eetsigner.cpp +++ b/libEet/eetsigner.cpp @@ -5,7 +5,6 @@ EetSigner::EetSigner(QObject *parent) : QObject(parent) { - } QByteArray EetSigner::signData(const QByteArray &data) @@ -15,15 +14,12 @@ QByteArray EetSigner::signData(const QByteArray &data) 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()) + if (m_result != QCA::ConvertGood || m_bundle.isNull()) { return QByteArray(); } - QCA::PrivateKey privKey = bundle.privateKey(); + QCA::PrivateKey privKey = m_bundle.privateKey(); return privKey.signMessage(QCA::MemoryRegion(data), QCA::EMSA3_SHA256); } @@ -37,3 +33,38 @@ QByteArray EetSigner::sha1HashData(const QByteArray &data) return QCA::Hash("sha1").hash(QCA::MemoryRegion(data)).toByteArray(); } + +QByteArray EetSigner::sha256HashData(const QByteArray &data) +{ + if (!QCA::isSupported("sha256")) + { + return QByteArray(); + } + + return QCA::Hash("sha256").hash(QCA::MemoryRegion(data)).toByteArray(); +} + +QString EetSigner::getCertificate() +{ + if (!QCA::isSupported("cert")) + { + return ""; + } + + if (m_result != QCA::ConvertGood || m_bundle.isNull()) + { + return ""; + } + + if (!m_bundle.certificateChain().isEmpty()) + { + return m_bundle.certificateChain().primary().toPEM(); + } + + return ""; +} + +void EetSigner::setup(const QString &certPath, const QCA::SecureArray &certPasswd) +{ + m_bundle = QCA::KeyBundle::fromFile(certPath, certPasswd, &m_result); +} diff --git a/libEet/eetsigner.h b/libEet/eetsigner.h index 3f1bc08..90dc67c 100644 --- a/libEet/eetsigner.h +++ b/libEet/eetsigner.h @@ -10,11 +10,17 @@ class EetSigner : public QObject Q_OBJECT public: explicit EetSigner(QObject *parent = 0); + QByteArray signData(const QByteArray &data); QByteArray sha1HashData(const QByteArray &data); + QByteArray sha256HashData(const QByteArray &data); + QString getCertificate(); + void setup(const QString &certPath, const QCA::SecureArray &certPasswd); private: QCA::Initializer m_qcaInit; + QCA::KeyBundle m_bundle; + QCA::ConvertResult m_result; signals: diff --git a/libEet/eettemplate.cpp b/libEet/eettemplate.cpp index d5b9053..0c795c4 100644 --- a/libEet/eettemplate.cpp +++ b/libEet/eettemplate.cpp @@ -22,8 +22,8 @@ QString EetTemplate::fillTemplate(EetRequest *request) QString pkp = getPkpString(request); - QByteArray signedPkp = m_signer.signData(pkp.toUtf8()); - QByteArray sha1Bkp = m_signer.sha1HashData(signedPkp); + QByteArray signedPkp = m_signer->signData(pkp.toUtf8()); + QByteArray sha1Bkp = m_signer->sha1HashData(signedPkp); QString base64Sign(signedPkp.toBase64()); QString base16bkp(sha1Bkp.toHex()); diff --git a/libEet/eettemplate.h b/libEet/eettemplate.h index 0ab6ac6..b2e5256 100644 --- a/libEet/eettemplate.h +++ b/libEet/eettemplate.h @@ -21,6 +21,7 @@ public: QString fillTemplate(EetRequest *request); QString fillTemplate(QMap map); + void setSigner(EetSigner *signer) { m_signer = signer; } signals: @@ -30,7 +31,7 @@ private: QString m_template; QString fillTemplateInternal(EetRequest *request, QMap *map); QString getPkpString(EetRequest *request); - EetSigner m_signer; + EetSigner *m_signer; }; #endif // EETTEMPLATE_H