35class MyPrompter :
public Prompter
48 void fileSuccess(
const QString &fileName)
51 known[fileName] = maybe[fileName];
56 void fileFailed(
const QString &fileName)
74 maybe[
event.fileName()] = password;
84 for (
int n = 0; n < in.
length(); ++n) {
87 else if (in[n] ==
':')
89 else if (in[n] ==
'\n')
100 for (
int n = 0; n < in.
length(); ++n) {
102 if (n + 1 < in.
length()) {
106 else if (in[n] ==
'c')
108 else if (in[n] ==
'n')
123 StorageType storageType;
147 if (storageType == File) {
148 parts +=
"privateFile";
152 parts +=
"privateEntry";
153 if (!keyStoreEntry.
isNull())
156 parts += keyStoreEntryString;
160 for (
int n = 0; n < parts.
count(); ++n)
161 parts[n] = escape(parts[n]);
162 return parts.
join(
":");
165 bool fromString(
const QString &in)
168 for (
int n = 0; n < parts.
count(); ++n)
169 parts[n] = unescape(parts[n]);
171 if (parts.
count() < 3)
175 int chainCount = parts[1].toInt();
176 if (chainCount < 1 || chainCount > parts.
count() - 2)
179 for (
int n = 0; n < chainCount; ++n) {
185 int at = chain.
count() + 2;
187 if (at < parts.
count()) {
191 if (parts[at] ==
"privateFile") {
193 fileName = parts[at + 1];
196 }
else if (parts[at] ==
"privateEntry") {
197 storageType = KeyStore;
198 keyStoreEntryString = parts[at + 1];
200 if (!keyStoreEntry.
isNull())
214CertItem::CertItem(
const CertItem &from)
223CertItem &CertItem::operator=(
const CertItem &from)
239bool CertItem::havePrivate()
const
241 return d->havePrivate;
244CertItem::StorageType CertItem::storageType()
const
246 return d->storageType;
249bool CertItem::isUsable()
const
257static MyPrompter *g_prompter = 0;
258static int g_prompter_refs = 0;
260class CertItemStorePrivate :
public QObject
265 MyPrompter *prompter;
282 CertItemStorePrivate(CertItemStore *_q)
289 g_prompter =
new MyPrompter;
294 prompter = g_prompter;
297 ~CertItemStorePrivate()
299 foreach (
const LoaderItem &i, loaders)
303 if (g_prompter_refs == 0) {
320 foreach (
const CertItem &i, list) {
321 if (i.name() == tryname) {
340 str =
tr(
"Incorrect passphrase.");
342 str =
tr(
"Unable to open or read file.");
345 str =
tr(
"Unable to decode format.");
351 void loader_finished()
355 for (
int n = 0; n < loaders.
count(); ++n) {
356 if (loaders[n].keyLoader == keyLoader) {
363 int req_id = loaders[at].req_id;
364 QString fileName = loaders[at].fileName;
370 prompter->fileFailed(fileName);
374 tr(
"Error importing certificate and private key.\nReason: %1").arg(convertErrorToString(r)));
375 emit q->addFailed(req_id);
379 prompter->fileSuccess(fileName);
390 i.d =
new CertItem::Private;
393 i.d->havePrivate =
true;
394 i.d->storageType = CertItem::File;
396 i.d->fileName = fileName;
405 emit q->addSuccess(req_id,
id);
409CertItemStore::CertItemStore(
QObject *parent)
412 d =
new CertItemStorePrivate(
this);
415CertItemStore::~CertItemStore()
420int CertItemStore::idFromRow(
int row)
const
422 return d->idList[row];
425int CertItemStore::rowFromId(
int id)
const
427 for (
int n = 0; n < d->idList.
count(); ++n) {
428 if (d->idList[n] ==
id)
434CertItem CertItemStore::itemFromId(
int id)
const
436 return d->list[rowFromId(
id)];
439CertItem CertItemStore::itemFromRow(
int row)
const
452 foreach (
const CertItem &i, d->list)
453 out += i.d->toString();
461 foreach (
const QString &s,
in) {
463 i.d =
new CertItem::Private;
464 if (i.d->fromString(s)) {
466 addIdList += d->next_id++;
475 d->idList += addIdList;
481int CertItemStore::addFromFile(
const QString &fileName)
483 CertItemStorePrivate::LoaderItem i;
484 i.req_id = d->next_req_id++;
486 i.fileName = fileName;
487 connect(i.keyLoader, SIGNAL(finished()), d, SLOT(loader_finished()));
503 i.d =
new CertItem::Private;
506 i.d->havePrivate =
true;
507 i.d->storageType = CertItem::KeyStore;
509 i.d->keyStoreEntry = entry;
511 int id = d->next_id++;
518 int req_id = d->next_req_id++;
530 i.d =
new CertItem::Private;
534 int id = d->next_id++;
541 int req_id = d->next_req_id++;
548 int at = rowFromId(
id);
549 d->list[at].d->chain = chain;
552void CertItemStore::removeItem(
int id)
554 int at = rowFromId(
id);
562void CertItemStore::setIcon(IconType type,
const QPixmap &icon)
564 d->iconset[
type] = icon;
567int CertItemStore::rowCount(
const QModelIndex &parent)
const
570 return d->list.
count();
586 if (list[at].havePrivate() && !list[at].isUsable())
587 str +=
QString(
" ") +
tr(
"(not usable)");
590 return list[at].name();
592 if (list[at].havePrivate())
593 return d->iconset[CertItemStore::IconKeyBundle];
595 return d->iconset[CertItemStore::IconCert];
622class CertItemPrivateLoaderPrivate :
public QObject
626 CertItemPrivateLoader *q;
627 CertItemStore *store;
632 CertItemPrivateLoaderPrivate(CertItemPrivateLoader *_q)
639 void loader_finished()
645 store->d->prompter->fileFailed(fileName);
649 tr(
"Error accessing private key.\nReason: %1").arg(CertItemStorePrivate::convertErrorToString(r)));
654 store->d->prompter->fileSuccess(fileName);
663CertItemPrivateLoader::CertItemPrivateLoader(CertItemStore *store,
QObject *parent)
666 d =
new CertItemPrivateLoaderPrivate(
this);
670CertItemPrivateLoader::~CertItemPrivateLoader()
675void CertItemPrivateLoader::start(
int id)
677 CertItem i = d->store->itemFromId(
id);
679 if (i.storageType() == CertItem::KeyStore) {
680 d->key = i.d->keyStoreEntry.keyBundle().privateKey();
686 d->fileName = i.d->fileName;
688 connect(d->loader, SIGNAL(finished()), d, SLOT(loader_finished()));
697#include "certitem.moc"
Base64 encoding / decoding
A chain of related Certificates.
const Certificate & primary() const
Return the primary (end-user) Certificate.
Public Key (X.509) certificate.
QString commonName() const
The common name of the subject of the certificate.
static Certificate fromDER(const QByteArray &a, ConvertResult *result=nullptr, const QString &provider=QString())
Import the certificate from DER.
bool isNull() const
Test if the certificate is empty (null)
QByteArray toDER() const
Export the Certificate into a DER format.
@ Data
File or bytearray generated the event.
Certificate chain and private key pair.
CertificateChain certificateChain() const
The public certificate part of this bundle.
PrivateKey privateKey() const
The private key part of this bundle.
Asynchronous private key loader.
void loadKeyBundleFromFile(const QString &fileName)
Initiate an asynchronous loading of a KeyBundle from a file.
ConvertResult convertResult() const
The result of the loading process.
KeyBundle keyBundle() const
The key bundle that has been loaded.
Single entry in a KeyStore.
QString toString() const
Serialize into a string for use as a passive entry.
KeyBundle keyBundle() const
If a KeyBundle is stored in this object, return that bundle.
QString name() const
The name associated with the key stored in this object.
bool isNull() const
Test if this key is empty (null)
QString arrayToString(const MemoryRegion &a)
Process an array in the "forward" direction, returning a QString.
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
ConvertResult
Return value from a format conversion.
@ ErrorFile
Failure because of incorrect file.
@ ErrorPassphrase
Failure because of incorrect passphrase.
@ ConvertGood
Conversion succeeded, results should be valid.
@ ErrorDecode
General failure in the decode stage.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool exists() const const
qsizetype count() const const
bool isEmpty() const const
void removeAt(qsizetype i)
qsizetype size() const const
bool contains(const Key &key) const const
size_type remove(const Key &key)
T value(const Key &key, const T &defaultValue) const const
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
QObject * sender() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(Args &&... args) const const
qsizetype length() const const
QString number(double n, char format, int precision)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
QString toString() const const