diff --git a/lib/db_x509.cpp b/lib/db_x509.cpp index 43027c96..7b1a49b2 100644 --- a/lib/db_x509.cpp +++ b/lib/db_x509.cpp @@ -295,26 +295,21 @@ QList db_x509::getCerts(bool unrevoked) void db_x509::writeIndex(const QString fname, bool hierarchy) { - bool append = false; if (hierarchy) { - FOR_ALL_pki(pki, pki_x509) { - if (pki->childCount()) { - - QString newfname = fname + "." + pki->getIntName().replace(QRegExp("[^a-zA-Z0-9]"),QString(""));; - - append = false; - foreach(pki_base *_child, pki->childItems) { - pki_x509 *child = static_cast(_child); - child->writeIndexEntry(newfname, append); - append = true; - } - } + QString dir = fname + "/"; + dir = nativeSeparator(dir); + QList issuers = sqlSELECTpki( + "SELECT DISTINCT issuer FROM certs WHERE issuer != item"); + foreach(pki_x509 *ca, issuers) { + writeIndex(dir + ca->getUnderlinedName() + ".txt", + sqlSELECTpki( + "SELECT item FROM certs WHERE issuer=?", + QList() << QVariant(ca->getSqlItemId()) + ) + ); } } else { - FOR_ALL_pki(pki, pki_x509) { - pki->writeIndexEntry(fname, append); - append = true; - } + writeIndex(fname,sqlSELECTpki("SELECT item FROM certs")); } } @@ -679,6 +674,7 @@ void db_x509::store(QModelIndexList list) return; } QString fname = dlg->filename->text(); + QList certs; enum exportType::etype type = dlg->type(); delete dlg; try { @@ -748,12 +744,10 @@ void db_x509::store(QModelIndexList list) crt->writeCert(fname, true, true); break; case exportType::Index: - append = false; - foreach(QModelIndex idx, list) { - crt = static_cast(idx.internalPointer()); - crt->writeIndexEntry(fname, append); - append = true; - } + foreach(QModelIndex idx, list) + certs << static_cast + (idx.internalPointer()); + writeIndex(fname, certs); break; default: exit(1); @@ -764,6 +758,23 @@ void db_x509::store(QModelIndexList list) } } +void db_x509::writeIndex(const QString fname, QList items) +{ + QFile file(fname); + file.open(QFile::ReadWrite); + if (file.error()) { + throw errorEx(tr("Error opening file: '%1': %2") + .arg(fname).arg(strerror(errno))); + return; + } + + QString index; + foreach(pki_x509 *cert, items) { + if (cert) + index += cert->getIndexEntry(); + } + file.write(index.toUtf8()); +} void db_x509::writePKCS12(pki_x509 *cert, QString s, bool chain) { diff --git a/lib/db_x509.h b/lib/db_x509.h index 85900661..6b59180c 100644 --- a/lib/db_x509.h +++ b/lib/db_x509.h @@ -42,6 +42,7 @@ class db_x509: public db_x509super QList getAllIssuers(); QList getCerts(bool unrevoked); void writeIndex(const QString fname, bool hierarchy); + void writeIndex(const QString fname, QList items); void writeAllCerts(const QString fname, bool unrevoked); pki_base *insert(pki_base *item); void markRequestSigned(pki_x509req *req, pki_x509 *cert); diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index 59fec958..4b1739c0 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -683,7 +683,7 @@ void pki_x509::writeCert(const QString fname, bool PEM, bool append) fopen_error(fname); } -void pki_x509::writeIndexEntry(FILE *fp) +QString pki_x509::getIndexEntry() { QString flag = NULL; bool revoked = isRevoked(); @@ -695,28 +695,11 @@ void pki_x509::writeIndexEntry(FILE *fp) else flag = "E"; - QString line = QString("%1\t%2\t%3\t%4\tunknown\t%5\n").arg( - flag, getNotAfter().toPlainUTC(), (revoked ? revocation.getDate().toPlainUTC() : ""), - getSerial().toHex(), QString(X509_NAME_oneline(getSubject().get(), NULL, 0))); - - QByteArray ba = line.toUtf8(); - fwrite(ba.constData(), ba.size(), 1, fp); -} - -void pki_x509::writeIndexEntry(const QString fname, bool append) -{ - FILE *fp; - const char *p = "w"; - if (append) - p = "a"; - fp = fopen(QString2filename(fname), p); - if (fp != NULL) { - if (cert) - writeIndexEntry(fp); - fclose(fp); - pki_openssl_error(); - } else - fopen_error(fname); + return QString("%1\t%2\t%3\t%4\tunknown\t%5\n").arg( + flag, getNotAfter().toPlainUTC(), + revoked ? revocation.getDate().toPlainUTC() : "", + getSerial().toHex(), + QString(X509_NAME_oneline(getSubject().get(), NULL, 0))); } BIO *pki_x509::pem(BIO *b, int format) diff --git a/lib/pki_x509.h b/lib/pki_x509.h index cb85c85a..7042281c 100644 --- a/lib/pki_x509.h +++ b/lib/pki_x509.h @@ -87,8 +87,7 @@ class pki_x509 : public pki_x509super bool isCA() const; bool canSign() const; void writeCert(const QString fname, bool PEM, bool append = false); - void writeIndexEntry(FILE *fp); - void writeIndexEntry(const QString fname, bool append = false); + QString getIndexEntry(); bool verify(pki_x509 *signer); bool verify_only(pki_x509 *signer); pki_key *getPubKey() const; diff --git a/widgets/ExportDialog.cpp b/widgets/ExportDialog.cpp index f39f9814..d2b98f14 100644 --- a/widgets/ExportDialog.cpp +++ b/widgets/ExportDialog.cpp @@ -85,7 +85,7 @@ ExportDialog::ExportDialog(MainWindow *mw, QString title, QString filt, help[exportType::PKCS8_encrypt] = tr("Encrypted private key in PKCS#8 text format"); help[exportType::SSH2_public] = tr("The public key encoded in SSH2 format"); - help[exportType::Index] = tr("Certificate Index file"); + help[exportType::Index] = tr("OpenSSL specific Certificate Index file as created by the 'ca' command and required by the OCSP tool"); on_exportFormat_highlighted(0); } diff --git a/widgets/MainWindow.cpp b/widgets/MainWindow.cpp index c0699434..7dab38ea 100644 --- a/widgets/MainWindow.cpp +++ b/widgets/MainWindow.cpp @@ -822,45 +822,36 @@ pki_multi *MainWindow::probeAnything(QString file, int *ret) void MainWindow::exportIndex() { - exportIndex(NULL, false); + exportIndex(QString(), false); } void MainWindow::exportIndexHierarchy() { - exportIndex(NULL, true); + exportIndex(QString(), true); } int MainWindow::exportIndex(QString fname, bool hierarchy) { - if (fname == NULL || fname.isEmpty()) { - - QString filter = tr("Certificate Index ( index.txt )") + ";;" + tr("All files ( * )"); - - fname = QFileDialog::getSaveFileName(this, QString(), fname, filter, NULL); + if (fname.isEmpty()) { + if (hierarchy) + fname = QFileDialog::getExistingDirectory( + this, tr(XCA_TITLE), getPath()); + else + fname = QFileDialog::getSaveFileName(this, tr(XCA_TITLE), + getPath(), + tr("Certificate Index ( index.txt )") + ";;" + + tr("All files ( * )")); if (fname.isEmpty()) return 1; - nativeSeparator(fname); } - if (certs == NULL) { open_default_db(); if (certs == NULL) return 2; } - - bool needChangeView = hierarchy && !certs->treeview; - if (needChangeView) { - certView->changeView(); - } - certs->writeIndex(fname, hierarchy); - - if (needChangeView) { - certView->changeView(); - } - return 0; } @@ -909,7 +900,7 @@ void MainWindow::generateDHparam() QString fname = QString("%1/dh%2.pem").arg(homedir).arg(num); fname = QFileDialog::getSaveFileName(this, QString(), - fname, "All files ( * )", NULL); + fname, tr("All files ( * )"), NULL); if (fname == "") throw errorEx(""); fp = fopen_write(fname);