File indexing completed on 2024-04-28 04:43:39

0001 /*
0002  Copyright (C) 2007 Justin Karneges <justin@affinix.com>
0003 
0004  Permission is hereby granted, free of charge, to any person obtaining a copy
0005  of this software and associated documentation files (the "Software"), to deal
0006  in the Software without restriction, including without limitation the rights
0007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008  copies of the Software, and to permit persons to whom the Software is
0009  furnished to do so, subject to the following conditions:
0010 
0011  The above copyright notice and this permission notice shall be included in
0012  all copies or substantial portions of the Software.
0013 
0014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0017  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0018  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0019  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0020 */
0021 
0022 // QtCrypto has the declarations for all of QCA
0023 #include <QtCrypto>
0024 
0025 #include <QCoreApplication>
0026 #include <QFile>
0027 #include <QTimer>
0028 
0029 #include <cstdio>
0030 
0031 #ifdef QT_STATICPLUGIN
0032 #include "import_plugins.h"
0033 #endif
0034 
0035 class PassphraseHandler : public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     QCA::EventHandler handler;
0040 
0041     PassphraseHandler(QObject *parent = nullptr)
0042         : QObject(parent)
0043     {
0044         connect(&handler, &QCA::EventHandler::eventReady, this, &PassphraseHandler::eh_eventReady);
0045         handler.start();
0046     }
0047 
0048 private Q_SLOTS:
0049     void eh_eventReady(int id, const QCA::Event &event)
0050     {
0051         if (event.type() == QCA::Event::Password) {
0052             QCA::SecureArray   pass;
0053             QCA::ConsolePrompt prompt;
0054             prompt.getHidden(QStringLiteral("Passphrase"));
0055             prompt.waitForFinished();
0056             pass = prompt.result();
0057             handler.submitPassword(id, pass);
0058         } else
0059             handler.reject(id);
0060     }
0061 };
0062 
0063 class App : public QObject
0064 {
0065     Q_OBJECT
0066 public:
0067     QCA::KeyLoader keyLoader;
0068     QString        str;
0069 
0070     App()
0071     {
0072         connect(&keyLoader, &QCA::KeyLoader::finished, this, &App::kl_finished);
0073     }
0074 
0075 public Q_SLOTS:
0076     void start()
0077     {
0078         keyLoader.loadPrivateKeyFromPEMFile(str);
0079     }
0080 
0081 Q_SIGNALS:
0082     void quit();
0083 
0084 private Q_SLOTS:
0085     void kl_finished()
0086     {
0087         if (keyLoader.convertResult() == QCA::ConvertGood) {
0088             QCA::PrivateKey key = keyLoader.privateKey();
0089             printf("Loaded successfully.  Bits: %d\n", key.bitSize());
0090         } else
0091             printf("Unable to load.\n");
0092 
0093         emit quit();
0094     }
0095 };
0096 
0097 int main(int argc, char **argv)
0098 {
0099     QCA::Initializer init;
0100     QCoreApplication qapp(argc, argv);
0101 
0102     if (argc < 2) {
0103         printf("usage: keyloader [privatekey.pem]\n");
0104         return 0;
0105     }
0106 
0107     PassphraseHandler passphraseHandler;
0108     App               app;
0109     app.str = QFile::decodeName(argv[1]);
0110     QObject::connect(&app, &App::quit, &qapp, QCoreApplication::quit);
0111     QTimer::singleShot(0, &app, &App::start);
0112     qapp.exec();
0113     return 0;
0114 }
0115 
0116 #include "keyloader.moc"