From df78e4380e96707333a3281258294bb2848bdd67 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Tue, 8 Apr 2014 18:53:40 +0200 Subject: [PATCH] SF Bug #87: Unable to set default key length Added a "Remember as default" checkbox to the key input dialog. The default key size is now 2048 for RSA and DSA keys. --- lib/db_key.cpp | 5 ++++ ui/NewKey.ui | 9 +++++- widgets/MW_database.cpp | 8 ++++++ widgets/MainWindow.cpp | 6 ++++ widgets/MainWindow.h | 1 + widgets/NewKey.cpp | 63 ++++++++++++++++++++++++++++++++++++++++- widgets/NewKey.h | 7 ++++- 7 files changed, 96 insertions(+), 3 deletions(-) diff --git a/lib/db_key.cpp b/lib/db_key.cpp index ac9b7462..543136ed 100644 --- a/lib/db_key.cpp +++ b/lib/db_key.cpp @@ -161,6 +161,11 @@ void db_key::newItem(QString name) delete key; mainwin->Error(err); } + if (dlg->rememberDefault->isChecked()) { + QString def = dlg->getAsString(); + if (dlg->setDefault(def) == 0) + mainwin->setDefaultKey(def); + } status->removeWidget(bar); delete bar; delete dlg; diff --git a/ui/NewKey.ui b/ui/NewKey.ui index 6935701c..4bec10b4 100644 --- a/ui/NewKey.ui +++ b/ui/NewKey.ui @@ -7,7 +7,7 @@ 0 0 449 - 297 + 320 @@ -182,6 +182,13 @@ + + + + Remember as default + + + diff --git a/widgets/MW_database.cpp b/widgets/MW_database.cpp index e8143564..53336819 100644 --- a/widgets/MW_database.cpp +++ b/widgets/MW_database.cpp @@ -16,6 +16,7 @@ #include "lib/db_base.h" #include "lib/func.h" #include "widgets/ImportMulti.h" +#include "widgets/NewKey.h" int MainWindow::init_database() { @@ -145,6 +146,13 @@ int MainWindow::init_database() free(p); } } + mydb.first(); + if (!mydb.find(setting, "defaultkey")) { + if ((p = (char *)mydb.load(NULL))) { + NewKey::setDefault((QString(p))); + free(p); + } + } ASN1_STRING_set_default_mask_asc((char*)CCHAR(string_opt)); mydb.first(); if (!mydb.find(setting, "mw_geometry")) { diff --git a/widgets/MainWindow.cpp b/widgets/MainWindow.cpp index 3a1d63b8..0e3bbd7f 100644 --- a/widgets/MainWindow.cpp +++ b/widgets/MainWindow.cpp @@ -827,6 +827,12 @@ void MainWindow::setPath(QString str) mydb.set((const unsigned char *)CCHAR(str), str.length()+1, 1, setting, "workingdir"); } +void MainWindow::setDefaultKey(QString str) +{ + db mydb(dbfile); + mydb.set((const unsigned char *)CCHAR(str), str.length()+1, 1, setting, "defaultkey"); +} + void MainWindow::connNewX509(NewX509 *nx) { connect( nx, SIGNAL(genKey(QString)), keys, SLOT(newItem(QString)) ); diff --git a/widgets/MainWindow.h b/widgets/MainWindow.h index 62a1ca64..b3af620f 100644 --- a/widgets/MainWindow.h +++ b/widgets/MainWindow.h @@ -100,6 +100,7 @@ class MainWindow: public QMainWindow, public Ui::MainWindow void dropEvent(QDropEvent *event); void dragEnterEvent(QDragEnterEvent *event); int open_default_db(); + void setDefaultKey(QString def); public slots: int init_database(); diff --git a/widgets/NewKey.cpp b/widgets/NewKey.cpp index d238eed0..798eeddd 100644 --- a/widgets/NewKey.cpp +++ b/widgets/NewKey.cpp @@ -15,6 +15,7 @@ #include #include #include +#include struct typelist { const char *name; @@ -29,6 +30,10 @@ static const struct typelist typeList[] = { #endif }; +int NewKey::defaultType = EVP_PKEY_RSA; +int NewKey::defaultEcNid = NID_undef; +int NewKey::defaultSize = 2048; + class keyListItem { protected: @@ -71,6 +76,10 @@ class keyListItem { return tl->type; } + QString typeName() + { + return QString(tl->name); + } }; Q_DECLARE_METATYPE(keyListItem); @@ -100,6 +109,7 @@ NewKey::NewKey(QWidget *parent, QString name) keytypes << gk; } #ifndef OPENSSL_NO_EC + QString ec_default; for (i = 0; iaddItems(curve_x962); curveBox->addItems(curve_other); + curveBox->setCurrentIndex(curveBox->findText(ec_default)); + if (curveBox->currentIndex() == -1) + curveBox->setCurrentIndex(0); #endif - keyLength->setCurrentIndex(0); + keyLength->setEditText(QString::number(defaultSize) + " bit"); keyDesc->setFocus(); if (pkcs11::loaded()) try { pkcs11 p11; @@ -138,6 +153,8 @@ NewKey::NewKey(QWidget *parent, QString name) QVariant q; q.setValue(keytypes[i]); keyType->addItem(keytypes[i].printname, q); + if (!keytypes[i].card && keytypes[i].type() == defaultType) + keyType->setCurrentIndex(i); } buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create")); } @@ -194,3 +211,47 @@ slotid NewKey::getKeyCardSlot() keyListItem k = currentKey(keyType); return k.slot; } + +QString NewKey::getAsString() +{ + keyListItem k = currentKey(keyType); + QString data; + + if (k.card) + return QString(); + if (k.type() == EVP_PKEY_EC) { + data = OBJ_obj2QString(OBJ_nid2obj(getKeyCurve_nid()), 1); + } else { + data = QString::number(getKeysize()); + } + return QString("%1:%2").arg(currentKey(keyType).typeName()).arg(data); +} + +int NewKey::setDefault(QString def) +{ + int type = -1, size = 0, nid = NID_undef; + QStringList sl = def.split(':'); + + if (sl.size() != 2) + return -1; + for (unsigned i=0; i < ARRAY_SIZE(typeList); i++ ) { + if (sl[0] == typeList[i].name) { + type = typeList[i].type; + } + } + if (type == -1) + return -2; + if (type == EVP_PKEY_EC) { + nid = OBJ_txt2nid(sl[1].toAscii()); + if (nid == NID_undef) + return -3; + defaultEcNid = nid; + } else { + size = sl[1].toInt(); + if (size <= 0) + return -4; + defaultSize = size; + } + defaultType = type; + return 0; +} diff --git a/widgets/NewKey.h b/widgets/NewKey.h index dfaf56dd..923b08df 100644 --- a/widgets/NewKey.h +++ b/widgets/NewKey.h @@ -15,7 +15,10 @@ class NewKey: public QDialog, public Ui::NewKey { Q_OBJECT - + private: + static int defaultType; + static int defaultEcNid; + static int defaultSize; public: NewKey(QWidget *parent, QString name); int getKeytype(); @@ -23,6 +26,8 @@ class NewKey: public QDialog, public Ui::NewKey int getKeyCurve_nid(); slotid getKeyCardSlot(); bool isToken(); + QString getAsString(); + static int setDefault(QString def); public slots: void on_keyType_currentIndexChanged(int);