comparison ui/createcertlistdialog.cpp @ 465:88dfe16a0bb9

Implement certificatelist saving
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Apr 2014 15:34:53 +0000
parents efd1bd85112f
children 0d71ce440bcc
comparison
equal deleted inserted replaced
464:2e100d3e414a 465:88dfe16a0bb9
28 mPk(NULL) 28 mPk(NULL)
29 { 29 {
30 setWindowTitle(tr("Save certificate list")); 30 setWindowTitle(tr("Save certificate list"));
31 setupGUI(); 31 setupGUI();
32 resize(500, 200); 32 resize(500, 200);
33 mCertFile->setText(mAdminWindow->settings()->value("LastCert", QString()).toString()); 33 mKeyFile->setText(mAdminWindow->settings()->value("LastKey", QString()).toString());
34 mSaveDir->setText(mAdminWindow->settings()->value("LastOutputDir", QString()).toString());
35 if (!mKeyFile->text().isEmpty()) {
36 loadKeyFile(mKeyFile->text());
37 }
34 } 38 }
35 39
36 void CreateCertListDialog::setupGUI() 40 void CreateCertListDialog::setupGUI()
37 { 41 {
38 /* Top level layout / widgets */ 42 /* Top level layout / widgets */
56 headerLayout->addWidget(header); 60 headerLayout->addWidget(header);
57 headerLayout->addLayout(headerSubLayout); 61 headerLayout->addLayout(headerSubLayout);
58 headerLayout->addWidget(headerSeparator); 62 headerLayout->addWidget(headerSeparator);
59 headerLayout->insertSpacing(3, 10); 63 headerLayout->insertSpacing(3, 10);
60 64
61 QLabel *certLabel = new QLabel("Select signature certificate (secret key):"); 65 QLabel *certLabel = new QLabel("Select signing key:");
62 QLabel *saveLabel = new QLabel("Select output folder:"); 66 QLabel *saveLabel = new QLabel("Select output folder:");
63 labelLayout->addWidget(certLabel); 67 labelLayout->addWidget(certLabel);
64 labelLayout->addWidget(saveLabel); 68 labelLayout->addWidget(saveLabel);
65 69
66 mCertFile = new QLineEdit(); 70 mKeyFile = new QLineEdit();
67 mSaveFile = new QLineEdit(); 71 mSaveDir = new QLineEdit();
68 fieldLayout->addWidget(mCertFile); 72 fieldLayout->addWidget(mKeyFile);
69 fieldLayout->addWidget(mSaveFile); 73 fieldLayout->addWidget(mSaveDir);
70 74
71 QPushButton *certSelect = new QPushButton("..."); 75 QPushButton *certSelect = new QPushButton("...");
72 certSelect->setFixedWidth(30); 76 certSelect->setFixedWidth(30);
73 connect(certSelect, SIGNAL(clicked()), this, SLOT(openCertificateSelect())); 77 connect(certSelect, SIGNAL(clicked()), this, SLOT(openCertificateSelect()));
74 QPushButton *saveSelect = new QPushButton("..."); 78 QPushButton *saveSelect = new QPushButton("...");
115 void CreateCertListDialog::showErrorMessage(const QString &msg) 119 void CreateCertListDialog::showErrorMessage(const QString &msg)
116 { 120 {
117 QMessageBox::warning(this, tr("Error!"), msg); 121 QMessageBox::warning(this, tr("Error!"), msg);
118 } 122 }
119 123
120 void CreateCertListDialog::openCertificateSelect() 124 void CreateCertListDialog::loadKeyFile(const QString& fileName)
121 { 125 {
122 QString certFile = QFileDialog::getOpenFileName(
123 this, tr("Select certificate"), mCertFile->text().isEmpty() ?
124 QDir::homePath() : mCertFile->text(), "*.pem");
125 mCertFile->setText(certFile);
126
127 mAdminWindow->settings()->setValue("LastCert", certFile);
128
129 if (mPk != NULL) { 126 if (mPk != NULL) {
130 pk_free(mPk); 127 pk_free(mPk);
131 delete mPk; 128 delete mPk;
132 mPk = NULL; 129 mPk = NULL;
133 } 130 }
134 131
135 mPk = new pk_context; 132 mPk = new pk_context;
136 pk_init(mPk); 133 pk_init(mPk);
137 int ret = pk_parse_keyfile(mPk, mCertFile->text().toLocal8Bit().constData(), ""); 134 int ret = pk_parse_keyfile(mPk, mKeyFile->text().toLocal8Bit().constData(), "");
138 135
139 if (ret != 0) { 136 if (ret != 0) {
140 showErrorMessage(tr("Failed to load certificate: %1") 137 showErrorMessage(tr("Failed to load certificate: %1")
141 .arg(getPolarSSLErrorMsg(ret))); 138 .arg(getPolarSSLErrorMsg(ret)));
142 return; 139 pk_free(mPk);
143 } 140 delete mPk;
141 mPk = NULL;
142 return;
143 }
144
145 /* Check that it is a 3072 bit RSA key as specified */
146 if (!mPk->pk_info || pk_get_size(mPk) != 3072 ||
147 mPk->pk_info->type != POLARSSL_PK_RSA) {
148 showErrorMessage(tr("Only 3072 bit RSA keys are supported by the current format."));
149 pk_free(mPk);
150 delete mPk;
151 mPk = NULL;
152 return;
153 }
154 }
155
156 void CreateCertListDialog::openCertificateSelect()
157 {
158 QString keyFile = QFileDialog::getOpenFileName(
159 this, tr("Select certificate"), mKeyFile->text().isEmpty() ?
160 QDir::homePath() : mKeyFile->text(), "*.pem");
161 mKeyFile->setText(keyFile);
162
163 mAdminWindow->settings()->setValue("LastKey", keyFile);
164 loadKeyFile(keyFile);
165
166 return;
144 } 167 }
145 168
146 void CreateCertListDialog::openSaveLocation() 169 void CreateCertListDialog::openSaveLocation()
147 { 170 {
148 QString saveFile = QFileDialog::getExistingDirectory( 171 QString saveDir = QFileDialog::getExistingDirectory(
149 this, tr("Select target location"), QDir::homePath()); 172 this, tr("Select target location"),
150 mSaveFile->setText(saveFile); 173 mSaveDir->text().isEmpty() ? QDir::homePath() : mSaveDir->text());
174 mAdminWindow->settings()->setValue("LastOutputDir", saveDir);
175 mSaveDir->setText(saveDir);
176 }
177
178 CreateCertListDialog::~CreateCertListDialog()
179 {
180 if (mPk) {
181 pk_free(mPk);
182 delete mPk;
183 mPk = NULL;
184 }
151 } 185 }
152 186
153 void CreateCertListDialog::createList() 187 void CreateCertListDialog::createList()
154 { 188 {
155 //entropy_context mEntropy; 189 if (!mPk) {
156 //ctr_drbg_context mCtr_drbg; 190 showErrorMessage(tr("Please select a valid rsa key."));
157 191 }
158 qDebug() << "and now create the certificate list using:"; 192 if (mSaveDir->text().isEmpty()) {
159 qDebug() << "certificate: " << mCertFile->text(); 193 showErrorMessage(tr("Please select an output location first."));
160 qDebug() << "target" << mSaveFile->text(); 194 }
161 // TODO 195
162 } 196 QDateTime currentDateTimeUtc = QDateTime::currentDateTimeUtc();
197
198 /* Build up the list data */
199 QByteArray listData("F:1\r\n");
200 listData.append(currentDateTimeUtc.toString(Qt::ISODate) + "\r\n");
201
202 foreach (const Certificate& cert, mAdminWindow->certificates()) {
203 listData.append(QString::fromLatin1("D:") + cert.base64Line() + "\r\n");
204 }
205
206 QByteArray signature = rsaSignSHA256Hash(sha256sum(listData), mPk);
207 listData.prepend("\r\n");
208 listData.prepend(signature.toBase64());
209 listData.prepend("S:");
210
211 QString fileName = QString::fromLatin1("certificates-")
212 .append(currentDateTimeUtc.toString(("yyyyMMddHHmmss")))
213 .append(".txt");
214
215 QString filePath = mSaveDir->text().append("/").append(fileName);
216
217 QFile outputFile(filePath);
218
219 if (!outputFile.open(QIODevice::WriteOnly)) {
220 showErrorMessage(tr("Failed to open output file %1").arg(filePath));
221 return;
222 }
223
224 if (outputFile.write(listData) != listData.size()) {
225 showErrorMessage(tr("Failed to write certificate list."));
226 return;
227 }
228
229 /* Archive the list */
230 QDir archiveDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
231 if (!archiveDir.mkpath(archiveDir.path())) {
232 showErrorMessage(tr("Failed to create archive location."));
233 return;
234 }
235
236 if (!outputFile.copy(archiveDir.filePath(fileName))) {
237 showErrorMessage(tr("Failed Archive a copy."));
238 return;
239 }
240
241 if (!outputFile.copy(archiveDir.filePath("current_certificates.txt"))) {
242 showErrorMessage(tr("Failed to write current_certificates file."));
243 return;
244 }
245
246 QMessageBox::information(this, "", tr("Saved certificate list:\n%1").arg(fileName));
247 close();
248 }

http://wald.intevation.org/projects/trustbridge/