keyloader.cpp

The code below shows how to load a private key from a PEM format file, including handling any requirement for a passphrase.

The code below shows how to load a private key from a PEM format file, including handling any requirement for a passphrase. This is done using the QCA::KeyLoader class.

/*
Copyright (C) 2007 Justin Karneges <justin@affinix.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// QtCrypto has the declarations for all of QCA
#include <QtCrypto>
#include <QCoreApplication>
#include <QFile>
#include <QTimer>
#include <cstdio>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
class PassphraseHandler : public QObject
{
Q_OBJECT
public:
QCA::EventHandler handler;
PassphraseHandler(QObject *parent = nullptr)
: QObject(parent)
{
connect(&handler, &QCA::EventHandler::eventReady, this, &PassphraseHandler::eh_eventReady);
handler.start();
}
private Q_SLOTS:
void eh_eventReady(int id, const QCA::Event &event)
{
if (event.type() == QCA::Event::Password) {
QCA::SecureArray pass;
QCA::ConsolePrompt prompt;
prompt.getHidden(QStringLiteral("Passphrase"));
prompt.waitForFinished();
pass = prompt.result();
handler.submitPassword(id, pass);
} else
handler.reject(id);
}
};
class App : public QObject
{
Q_OBJECT
public:
QCA::KeyLoader keyLoader;
QString str;
App()
{
connect(&keyLoader, &QCA::KeyLoader::finished, this, &App::kl_finished);
}
public Q_SLOTS:
void start()
{
keyLoader.loadPrivateKeyFromPEMFile(str);
}
Q_SIGNALS:
void quit();
private Q_SLOTS:
void kl_finished()
{
if (keyLoader.convertResult() == QCA::ConvertGood) {
QCA::PrivateKey key = keyLoader.privateKey();
printf("Loaded successfully. Bits: %d\n", key.bitSize());
} else
printf("Unable to load.\n");
emit quit();
}
};
int main(int argc, char **argv)
{
QCA::Initializer init;
QCoreApplication qapp(argc, argv);
if (argc < 2) {
printf("usage: keyloader [privatekey.pem]\n");
return 0;
}
PassphraseHandler passphraseHandler;
App app;
app.str = QFile::decodeName(argv[1]);
QObject::connect(&app, &App::quit, &qapp, QCoreApplication::quit);
QTimer::singleShot(0, &app, &App::start);
qapp.exec();
return 0;
}
#include "keyloader.moc"
QCA::ConsolePrompt
Console prompt handler.
Definition qca_support.h:864
QCA::ConsolePrompt::waitForFinished
void waitForFinished()
Block waiting for user input.
QCA::ConsolePrompt::getHidden
void getHidden(const QString &promptStr)
Allow the user to enter data without it being echo'd to the terminal.
QCA::ConsolePrompt::result
SecureArray result() const
Obtain the result of the user input.
QCA::EventHandler
Interface class for password / passphrase / PIN and token handlers.
Definition qca_core.h:1579
QCA::EventHandler::submitPassword
void submitPassword(int id, const SecureArray &password)
function to call to return the user provided password, passphrase or PIN.
QCA::EventHandler::eventReady
void eventReady(int id, const QCA::Event &context)
signal emitted when an Event requires attention.
QCA::EventHandler::reject
void reject(int id)
function to call to indicate that the user declined to provide a password, passphrase,...
QCA::EventHandler::start
void start()
mandatory function to call after connecting the signal to a slot in your application specific passwor...
QCA::Event
An asynchronous event.
Definition qca_core.h:1391
QCA::Event::type
Type type() const
the Type of this event
QCA::Event::Password
@ Password
Asking for a password, PIN or passphrase.
Definition qca_core.h:1400
QCA::Initializer
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
QCA::KeyLoader
Asynchronous private key loader.
Definition qca_cert.h:2613
QCA::KeyLoader::convertResult
ConvertResult convertResult() const
The result of the loading process.
QCA::KeyLoader::loadPrivateKeyFromPEMFile
void loadPrivateKeyFromPEMFile(const QString &fileName)
Initiate an asynchronous loading of a PrivateKey from a PEM format file.
QCA::KeyLoader::privateKey
PrivateKey privateKey() const
The private key that has been loaded.
QCA::KeyLoader::finished
void finished()
Signal that is emitted when the load process has completed.
QCA::PKey::bitSize
int bitSize() const
Report the number of bits in the key.
QCA::SecureArray
Secure array of bytes.
Definition qca_tools.h:317
QObject
QCA::init
QCA_EXPORT void init()
Initialise QCA.
QCA::ConvertGood
@ ConvertGood
Conversion succeeded, results should be valid.
Definition qca_publickey.h:120