Adapt Certificate Index creation to the new database capabilities

This commit is contained in:
Christian Hohnstaedt 2018-03-07 22:58:15 +01:00
parent 68cc947fc7
commit c62339e1ce
6 changed files with 55 additions and 70 deletions

View File

@ -295,26 +295,21 @@ QList<pki_x509*> 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<pki_x509*>(_child);
child->writeIndexEntry(newfname, append);
append = true;
}
}
QString dir = fname + "/";
dir = nativeSeparator(dir);
QList<pki_x509*> issuers = sqlSELECTpki<pki_x509>(
"SELECT DISTINCT issuer FROM certs WHERE issuer != item");
foreach(pki_x509 *ca, issuers) {
writeIndex(dir + ca->getUnderlinedName() + ".txt",
sqlSELECTpki<pki_x509>(
"SELECT item FROM certs WHERE issuer=?",
QList<QVariant>() << QVariant(ca->getSqlItemId())
)
);
}
} else {
FOR_ALL_pki(pki, pki_x509) {
pki->writeIndexEntry(fname, append);
append = true;
}
writeIndex(fname,sqlSELECTpki<pki_x509>("SELECT item FROM certs"));
}
}
@ -679,6 +674,7 @@ void db_x509::store(QModelIndexList list)
return;
}
QString fname = dlg->filename->text();
QList<pki_x509*> 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<pki_x509*>(idx.internalPointer());
crt->writeIndexEntry(fname, append);
append = true;
}
foreach(QModelIndex idx, list)
certs << static_cast<pki_x509*>
(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<pki_x509*> 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)
{

View File

@ -42,6 +42,7 @@ class db_x509: public db_x509super
QList<pki_x509*> getAllIssuers();
QList<pki_x509*> getCerts(bool unrevoked);
void writeIndex(const QString fname, bool hierarchy);
void writeIndex(const QString fname, QList<pki_x509*> items);
void writeAllCerts(const QString fname, bool unrevoked);
pki_base *insert(pki_base *item);
void markRequestSigned(pki_x509req *req, pki_x509 *cert);

View File

@ -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)

View File

@ -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;

View File

@ -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);
}

View File

@ -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);