keyloader.cpp

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.

00001 /*
00002  Copyright (C) 2007 Justin Karneges <justin@affinix.com>
00003 
00004  Permission is hereby granted, free of charge, to any person obtaining a copy
00005  of this software and associated documentation files (the "Software"), to deal
00006  in the Software without restriction, including without limitation the rights
00007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  copies of the Software, and to permit persons to whom the Software is
00009  furnished to do so, subject to the following conditions:
00010 
00011  The above copyright notice and this permission notice shall be included in
00012  all copies or substantial portions of the Software.
00013 
00014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00017  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020 */
00021 
00022 // QtCrypto has the declarations for all of QCA
00023 #include <QtCrypto>
00024 
00025 #include <QCoreApplication>
00026 #include <QTimer>
00027 
00028 class PassphraseHandler: public QObject
00029 {
00030         Q_OBJECT
00031 public:
00032         QCA::EventHandler handler;
00033 
00034         PassphraseHandler(QObject *parent = 0) : QObject(parent)
00035         {
00036                 connect(&handler, SIGNAL(eventReady(int, const QCA::Event &)),
00037                         SLOT(eh_eventReady(int, const QCA::Event &)));
00038                 handler.start();
00039         }
00040 
00041 private slots:
00042         void eh_eventReady(int id, const QCA::Event &event)
00043         {
00044                 if(event.type() == QCA::Event::Password)
00045                 {
00046                         QCA::SecureArray pass;
00047                         QCA::ConsolePrompt prompt;
00048                         prompt.getHidden("Passphrase");
00049                         prompt.waitForFinished();
00050                         pass = prompt.result();
00051                         handler.submitPassword(id, pass);
00052                 }
00053                 else
00054                         handler.reject(id);
00055         }
00056 };
00057 
00058 class App : public QObject
00059 {
00060         Q_OBJECT
00061 public:
00062         QCA::KeyLoader keyLoader;
00063         QString str;
00064 
00065         App()
00066         {
00067                 connect(&keyLoader, SIGNAL(finished()), SLOT(kl_finished()));
00068         }
00069 
00070 public slots:
00071         void start()
00072         {
00073                 keyLoader.loadPrivateKeyFromPEMFile(str);
00074         }
00075 
00076 signals:
00077         void quit();
00078 
00079 private slots:
00080         void kl_finished()
00081         {
00082                 if(keyLoader.convertResult() == QCA::ConvertGood)
00083                 {
00084                         QCA::PrivateKey key = keyLoader.privateKey();
00085                         printf("Loaded successfully.  Bits: %d\n", key.bitSize());
00086                 }
00087                 else
00088                         printf("Unable to load.\n");
00089 
00090                 emit quit();
00091         }
00092 };
00093 
00094 int main(int argc, char **argv)
00095 {
00096         QCA::Initializer init;
00097         QCoreApplication qapp(argc, argv);
00098 
00099         if(argc < 2)
00100         {
00101                 printf("usage: keyloader [privatekey.pem]\n");
00102                 return 0;
00103         }
00104 
00105         PassphraseHandler passphraseHandler;
00106         App app;
00107         app.str = argv[1];
00108         QObject::connect(&app, SIGNAL(quit()), &qapp, SLOT(quit()));
00109         QTimer::singleShot(0, &app, SLOT(start()));
00110         qapp.exec();
00111         return 0;
00112 }
00113 
00114 #include "keyloader.moc"

Generated on Fri Jul 6 12:14:03 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6