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 #include <stdio.h>
00029 
00030 class PassphraseHandler: public QObject
00031 {
00032         Q_OBJECT
00033 public:
00034         QCA::EventHandler handler;
00035 
00036         PassphraseHandler(QObject *parent = 0) : QObject(parent)
00037         {
00038                 connect(&handler, SIGNAL(eventReady(int, const QCA::Event &)),
00039                         SLOT(eh_eventReady(int, const QCA::Event &)));
00040                 handler.start();
00041         }
00042 
00043 private slots:
00044         void eh_eventReady(int id, const QCA::Event &event)
00045         {
00046                 if(event.type() == QCA::Event::Password)
00047                 {
00048                         QCA::SecureArray pass;
00049                         QCA::ConsolePrompt prompt;
00050                         prompt.getHidden("Passphrase");
00051                         prompt.waitForFinished();
00052                         pass = prompt.result();
00053                         handler.submitPassword(id, pass);
00054                 }
00055                 else
00056                         handler.reject(id);
00057         }
00058 };
00059 
00060 class App : public QObject
00061 {
00062         Q_OBJECT
00063 public:
00064         QCA::KeyLoader keyLoader;
00065         QString str;
00066 
00067         App()
00068         {
00069                 connect(&keyLoader, SIGNAL(finished()), SLOT(kl_finished()));
00070         }
00071 
00072 public slots:
00073         void start()
00074         {
00075                 keyLoader.loadPrivateKeyFromPEMFile(str);
00076         }
00077 
00078 signals:
00079         void quit();
00080 
00081 private slots:
00082         void kl_finished()
00083         {
00084                 if(keyLoader.convertResult() == QCA::ConvertGood)
00085                 {
00086                         QCA::PrivateKey key = keyLoader.privateKey();
00087                         printf("Loaded successfully.  Bits: %d\n", key.bitSize());
00088                 }
00089                 else
00090                         printf("Unable to load.\n");
00091 
00092                 emit quit();
00093         }
00094 };
00095 
00096 int main(int argc, char **argv)
00097 {
00098         QCA::Initializer init;
00099         QCoreApplication qapp(argc, argv);
00100 
00101         if(argc < 2)
00102         {
00103                 printf("usage: keyloader [privatekey.pem]\n");
00104                 return 0;
00105         }
00106 
00107         PassphraseHandler passphraseHandler;
00108         App app;
00109         app.str = argv[1];
00110         QObject::connect(&app, SIGNAL(quit()), &qapp, SLOT(quit()));
00111         QTimer::singleShot(0, &app, SLOT(start()));
00112         qapp.exec();
00113         return 0;
00114 }
00115 
00116 #include "keyloader.moc"

Generated on Thu Sep 6 19:13:35 2007 for Qt Cryptographic Architecture by  doxygen 1.5.2