| 1 | /* |
| 2 | Copyright (C) 2007 Justin Karneges <justin@affinix.com> |
| 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(sender: &handler, signal: &QCA::EventHandler::eventReady, context: this, slot: &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, password: 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(sender: &keyLoader, signal: &QCA::KeyLoader::finished, context: this, slot: &App::kl_finished); |
| 73 | } |
| 74 | |
| 75 | public Q_SLOTS: |
| 76 | void start() |
| 77 | { |
| 78 | keyLoader.loadPrivateKeyFromPEMFile(fileName: 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(format: "Loaded successfully. Bits: %d\n" , key.bitSize()); |
| 90 | } else |
| 91 | printf(format: "Unable to load.\n" ); |
| 92 | |
| 93 | emit quit(); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | int main(int argc, char **argv) |
| 98 | { |
| 99 | QCA::Initializer init; |
| 100 | QCoreApplication qapp(argc, argv); |
| 101 | |
| 102 | if (argc < 2) { |
| 103 | printf(format: "usage: keyloader [privatekey.pem]\n" ); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | PassphraseHandler passphraseHandler; |
| 108 | App app; |
| 109 | app.str = QFile::decodeName(localFileName: argv[1]); |
| 110 | QObject::connect(sender: &app, signal: &App::quit, context: &qapp, slot&: QCoreApplication::quit); |
| 111 | QTimer::singleShot(interval: 0, receiver: &app, slot: &App::start); |
| 112 | qapp.exec(); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | #include "keyloader.moc" |
| 117 | |