# HG changeset patch # User Andre Heinecke # Date 1398268088 0 # Node ID 0d71ce440bcc28e82a7359f865a81bc3859ecb72 # Parent 88dfe16a0bb9af15631f62c864d29bef904d7ecf Factor out list creation to make it more testable diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/createcertlistdialog.cpp --- a/ui/createcertlistdialog.cpp Wed Apr 23 15:34:53 2014 +0000 +++ b/ui/createcertlistdialog.cpp Wed Apr 23 15:48:08 2014 +0000 @@ -184,6 +184,44 @@ } } +bool CreateCertListDialog::writeList(const QList& certs, + const QString& filePath, + const QDateTime& listDate, + pk_context *pk) +{ + /* Build up the list data */ + QByteArray listData("F:1\r\n"); + listData.append(listDate.toString(Qt::ISODate) + "\r\n"); + + foreach (const Certificate& cert, certs) { + listData.append(QString::fromLatin1("D:") + cert.base64Line() + "\r\n"); + } + + QByteArray signature = rsaSignSHA256Hash(sha256sum(listData), pk); + if (signature.size() != 3072 / 8) { + qDebug() << "Signature creation returned signature of invalid size."; + return false; + } + listData.prepend("\r\n"); + listData.prepend(signature.toBase64()); + listData.prepend("S:"); + + QFile outputFile(filePath); + + if (!outputFile.open(QIODevice::WriteOnly)) { + qDebug() << "Failed to open output file: " << filePath; + return false; + } + + if (outputFile.write(listData) != listData.size()) { + qDebug() << "Failed to write list: " << filePath; + outputFile.close(); + return false; + } + outputFile.close(); + return true; +} + void CreateCertListDialog::createList() { if (!mPk) { @@ -195,36 +233,18 @@ QDateTime currentDateTimeUtc = QDateTime::currentDateTimeUtc(); - /* Build up the list data */ - QByteArray listData("F:1\r\n"); - listData.append(currentDateTimeUtc.toString(Qt::ISODate) + "\r\n"); - - foreach (const Certificate& cert, mAdminWindow->certificates()) { - listData.append(QString::fromLatin1("D:") + cert.base64Line() + "\r\n"); - } - - QByteArray signature = rsaSignSHA256Hash(sha256sum(listData), mPk); - listData.prepend("\r\n"); - listData.prepend(signature.toBase64()); - listData.prepend("S:"); - QString fileName = QString::fromLatin1("certificates-") .append(currentDateTimeUtc.toString(("yyyyMMddHHmmss"))) .append(".txt"); QString filePath = mSaveDir->text().append("/").append(fileName); - QFile outputFile(filePath); - - if (!outputFile.open(QIODevice::WriteOnly)) { - showErrorMessage(tr("Failed to open output file %1").arg(filePath)); - return; + if (!writeList(mAdminWindow->certificates(), filePath, + currentDateTimeUtc, mPk)) { + showErrorMessage(tr("Failed to write list to: %1").arg(filePath)); } - if (outputFile.write(listData) != listData.size()) { - showErrorMessage(tr("Failed to write certificate list.")); - return; - } + QFile outputFile(filePath); /* Archive the list */ QDir archiveDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/createcertlistdialog.h --- a/ui/createcertlistdialog.h Wed Apr 23 15:34:53 2014 +0000 +++ b/ui/createcertlistdialog.h Wed Apr 23 15:48:08 2014 +0000 @@ -12,6 +12,8 @@ #include #include +#include "certificate.h" + #include /** * @file createinstallerdialog.h @@ -31,6 +33,19 @@ CreateCertListDialog(AdministratorWindow *parent); ~CreateCertListDialog(); + /** @brief write a signed certificate list. + * + * @param [in] certs The certificates to include in the list + * @param [in] filePath The path where the list should be written to + * @param [in] listDate The date to write in the list + * @param [in] pk The private key to sign with. + * + * @returns true on success false on error. + */ + static bool writeList(const QList& certs, + const QString& filePath, const QDateTime& listDate, pk_context *pk); + + private: void setupGUI(); diff -r 88dfe16a0bb9 -r 0d71ce440bcc ui/l10n/administrator_de_DE.ts --- a/ui/l10n/administrator_de_DE.ts Wed Apr 23 15:34:53 2014 +0000 +++ b/ui/l10n/administrator_de_DE.ts Wed Apr 23 15:48:08 2014 +0000 @@ -226,6 +226,11 @@ Zertifikat auswählen + + Failed to write list to: %1 + + + Failed to load certificate: %1 @@ -241,42 +246,32 @@ - + Please select a valid rsa key. - + Please select an output location first. - - Failed to open output file %1 - - - - - Failed to write certificate list. - - - - + Failed to create archive location. - + Failed Archive a copy. - + Failed to write current_certificates file. - + Saved certificate list: %1