QCA

keyloader.cpp
1 /*
2  Copyright (C) 2007 Justin Karneges <[email protected]>
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21 
22 // QtCrypto has the declarations for all of QCA
23 #include <QtCrypto>
24 
25 #include <QCoreApplication>
26 #include <QFile>
27 #include <QTimer>
28 
29 #include <cstdio>
30 
31 #ifdef QT_STATICPLUGIN
32 #include "import_plugins.h"
33 #endif
34 
35 class PassphraseHandler : public QObject
36 {
37  Q_OBJECT
38 public:
39  QCA::EventHandler handler;
40 
41  PassphraseHandler(QObject *parent = nullptr)
42  : QObject(parent)
43  {
44  connect(&handler, &QCA::EventHandler::eventReady, this, &PassphraseHandler::eh_eventReady);
45  handler.start();
46  }
47 
48 private Q_SLOTS:
49  void eh_eventReady(int id, const QCA::Event &event)
50  {
51  if (event.type() == QCA::Event::Password) {
52  QCA::SecureArray pass;
53  QCA::ConsolePrompt prompt;
54  prompt.getHidden(QStringLiteral("Passphrase"));
55  prompt.waitForFinished();
56  pass = prompt.result();
57  handler.submitPassword(id, pass);
58  } else
59  handler.reject(id);
60  }
61 };
62 
63 class App : public QObject
64 {
65  Q_OBJECT
66 public:
67  QCA::KeyLoader keyLoader;
68  QString str;
69 
70  App()
71  {
72  connect(&keyLoader, &QCA::KeyLoader::finished, this, &App::kl_finished);
73  }
74 
75 public Q_SLOTS:
76  void start()
77  {
78  keyLoader.loadPrivateKeyFromPEMFile(str);
79  }
80 
81 Q_SIGNALS:
82  void quit();
83 
84 private Q_SLOTS:
85  void kl_finished()
86  {
87  if (keyLoader.convertResult() == QCA::ConvertGood) {
88  QCA::PrivateKey key = keyLoader.privateKey();
89  printf("Loaded successfully. Bits: %d\n", key.bitSize());
90  } else
91  printf("Unable to load.\n");
92 
93  emit quit();
94  }
95 };
96 
97 int main(int argc, char **argv)
98 {
100  QCoreApplication qapp(argc, argv);
101 
102  if (argc < 2) {
103  printf("usage: keyloader [privatekey.pem]\n");
104  return 0;
105  }
106 
107  PassphraseHandler passphraseHandler;
108  App app;
109  app.str = QFile::decodeName(argv[1]);
110  QObject::connect(&app, &App::quit, &qapp, QCoreApplication::quit);
111  QTimer::singleShot(0, &app, &App::start);
112  qapp.exec();
113  return 0;
114 }
115 
116 #include "keyloader.moc"
An asynchronous event.
Definition: qca_core.h:1390
Type type() const
the Type of this event
void loadPrivateKeyFromPEMFile(const QString &fileName)
Initiate an asynchronous loading of a PrivateKey from a PEM format file.
void getHidden(const QString &promptStr)
Allow the user to enter data without it being echo&#39;d to the terminal.
Console prompt handler.
Definition: qca_support.h:863
Generic private key.
void reject(int id)
function to call to indicate that the user declined to provide a password, passphrase, PIN or token.
Interface class for password / passphrase / PIN and token handlers.
Definition: qca_core.h:1578
Asking for a password, PIN or passphrase.
Definition: qca_core.h:1400
Conversion succeeded, results should be valid.
void submitPassword(int id, const SecureArray &password)
function to call to return the user provided password, passphrase or PIN.
Q_OBJECTQ_OBJECT
int bitSize() const
Report the number of bits in the key.
void start()
mandatory function to call after connecting the signal to a slot in your application specific passwor...
Asynchronous private key loader.
Definition: qca_cert.h:2612
QCA_EXPORT void init()
Initialise QCA.
Secure array of bytes.
Definition: qca_tools.h:316
void eventReady(int id, const QCA::Event &context)
signal emitted when an Event requires attention.
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:659
ConvertResult convertResult() const
The result of the loading process.
PrivateKey privateKey() const
The private key that has been loaded.
void finished()
Signal that is emitted when the load process has completed.
Q_SLOTSQ_SLOTS
void waitForFinished()
Block waiting for user input.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
SecureArray result() const
Obtain the result of the user input.
QString decodeName(const QByteArray &localFileName)
const QList< QKeySequence > & quit()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Sep 25 2021 23:05:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.