improve key to token storage

- fix memory leak in case of an error while
   copying a private key onto a token
 - report proper error messages in case of non-RSA key or
   aborted PIN input
This commit is contained in:
Christian Hohnstaedt 2010-02-28 18:30:02 +01:00
parent 8b842925bc
commit 138f107913
2 changed files with 8 additions and 3 deletions

View File

@ -183,13 +183,15 @@ void db_key::toToken()
pki_key *key = static_cast<pki_scard*>(currentIdx.internalPointer());
if (!key || !pkcs11::loaded() || key->isToken())
return;
pki_scard *card = NULL;
try {
pkcs11 p11;
unsigned long slot;
if (!p11.selectToken(&slot, mainwin))
return;
pki_scard *card = new pki_scard(key->getIntName());
card = new pki_scard(key->getIntName());
card->store_token(slot, key->decryptKey());
QString msg = tr("Shall the original key '%1' be replaced by the key on the token?\nThis will delete the key '%1' and make it unexportable").
arg(key->getIntName());
@ -198,10 +200,13 @@ void db_key::toToken()
{
deletePKI();
insertPKI(card);
card = NULL;
}
} catch (errorEx &err) {
mainwin->Error(err);
}
if (card)
delete card;
}
void db_key::showPki(pki_base *pki)

View File

@ -316,7 +316,7 @@ void pki_scard::store_token(unsigned int slot, EVP_PKEY *pkey)
QList<CK_OBJECT_HANDLE> objects;
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA)
return;
throw errorEx(tr("only RSA keys can be stored on tokens"));
RSA *rsakey = pkey->pkey.rsa;
@ -369,7 +369,7 @@ void pki_scard::store_token(unsigned int slot, EVP_PKEY *pkey)
tkInfo ti = p11.tokenInfo();
if (p11.tokenLogin(ti.label(), false).isNull())
return;
throw errorEx(tr("PIN input aborted"));
p11.createObject(pub_atts);
p11.createObject(priv_atts);