qca
eventhandlerdemo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <QtCrypto>
00024
00025 #include <QCoreApplication>
00026
00027 #include <iostream>
00028
00032 class ClientPassphraseHandler: public QObject
00033 {
00034 Q_OBJECT
00035 public:
00036 ClientPassphraseHandler(QObject *parent = 0) : QObject( parent )
00037 {
00038
00039
00040
00041 connect( &m_handler, SIGNAL( eventReady(int, const QCA::Event &) ),
00042 SLOT( my_eventReady(int, const QCA::Event &) ) );
00043
00044
00045
00046 m_handler.start();
00047 }
00048
00049 private slots:
00050
00051
00052 void my_eventReady(int id, const QCA::Event &event)
00053 {
00054
00055 if ( event.isNull() ) {
00056 return;
00057 }
00058
00059
00060
00061 if ( event.source() == QCA::Event::KeyStore ) {
00062 std::cout << "Event is associated with a key store operation" << std::endl;
00063 } else if ( event.source() == QCA::Event::Data ) {
00064 std::cout << "Event is associated with a file or some other data" << std::endl;
00065
00066
00067 std::cout << " Filename: " << qPrintable( event.fileName() ) << std::endl;
00068 } else {
00069 std::cout << "Unexpected Source for Event" << std::endl;
00070 }
00071
00072
00073 if ( event.type() == QCA::Event::Token ) {
00074
00075 std::cout << "Request for token" << std::endl;
00076
00077 m_handler.tokenOkay( id );
00078
00079
00080 } else if ( event.type() == QCA::Event::Password ) {
00081 std::cout << "Request for password, passphrase or PIN" << std::endl;
00082
00083 if ( event.passwordStyle() == QCA::Event::StylePassword ) {
00084 std::cout << " [Password request]" << std::endl;
00085 } else if ( event.passwordStyle() == QCA::Event::StylePassphrase ) {
00086 std::cout << " [Passphrase request]" << std::endl;
00087 } else if ( event.passwordStyle() == QCA::Event::StylePIN ){
00088 std::cout << " [PIN request]" << std::endl;
00089 } else {
00090 std::cout << " [unexpect request style]" << std::endl;
00091 }
00092
00093
00094 m_handler.submitPassword( id, QCA::SecureArray( "hello" ) );
00095
00096 } else {
00097 std::cout << "Unexpected event type" << std::endl;
00098 }
00099 }
00100 private:
00101 QCA::EventHandler m_handler;
00102
00103 };
00104
00105 void asker_procedure();
00106
00107 class AskerThread : public QThread
00108 {
00109 Q_OBJECT
00110 protected:
00111 virtual void run()
00112 {
00113 asker_procedure();
00114 }
00115 };
00116
00117 int main(int argc, char **argv)
00118 {
00119
00120
00121 QCA::Initializer init;
00122
00123 QCoreApplication exampleApp(argc, argv);
00124
00125 ClientPassphraseHandler cph;
00126
00127
00128 AskerThread askerThread;
00129 QObject::connect(&askerThread, SIGNAL(finished()), &exampleApp, SLOT(quit()));
00130 askerThread.start();
00131
00132 exampleApp.exec();
00133 return 0;
00134 }
00135
00136 void asker_procedure()
00137 {
00138 QCA::PasswordAsker pwAsker;
00139
00140 pwAsker.ask( QCA::Event::StylePassword, "foo.tmp", 0 );
00141
00142 pwAsker.waitForResponse();
00143
00144 std::cout << "Password was: " << pwAsker.password().toByteArray().data() << std::endl;
00145
00146 std::cout << std::endl << "Now do token:" << std::endl;
00147
00148 QCA::TokenAsker tokenAsker;
00149
00150 tokenAsker.ask( QCA::KeyStoreInfo( QCA::KeyStore::SmartCard, "Token Id", "Token Name" ), QCA::KeyStoreEntry(), 0 );
00151
00152 tokenAsker.waitForResponse();
00153
00154 if ( tokenAsker.accepted() ) {
00155 std::cout << "Token was accepted" << std::endl;
00156 } else {
00157 std::cout << "Token was not accepted" << std::endl;
00158 }
00159 }
00160
00161 #include "eventhandlerdemo.moc"