pkiutilities/securitydialognotifier/tsrc/srvauthfaildlglauncher/srvauthfaildlglauncher.cpp
branchRCL_3
changeset 50 03674e5abf46
parent 49 09b1ac925e3f
child 54 94da73d93b58
equal deleted inserted replaced
49:09b1ac925e3f 50:03674e5abf46
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Test application for untrusted certificate dialog.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "srvauthfaildlglauncher.h"
       
    19 #include <HbMainWindow>
       
    20 #include <HbView>
       
    21 #include <HbLabel>
       
    22 #include <HbComboBox>
       
    23 #include <HbTextEdit>
       
    24 #include <HbCheckBox>
       
    25 #include <HbPushButton>
       
    26 #include <HbDeviceDialog>
       
    27 #include <HbMessageBox>
       
    28 #include <QDir>
       
    29 #include <QBuffer>
       
    30 #include <QGraphicsLinearLayout>
       
    31 
       
    32 
       
    33 #ifdef Q_OS_SYMBIAN
       
    34 #include <securitydefs.h>       // TValidationError
       
    35 #include <secdlgimpldefs.h>     // TServerAuthenticationFailureInput, KUidSecurityDialogNotifier
       
    36 
       
    37 HBufC8* GetInputBufferL( const QString& server, const QByteArray& certificate, int reason )
       
    38     {
       
    39     const TPtrC16 serverNameUnicode(reinterpret_cast<const TText*>(server.constData()),
       
    40         server.length());
       
    41     const TPtrC8 encodedCert(reinterpret_cast<const TText8*>(certificate.constData()),
       
    42         certificate.length());
       
    43 
       
    44     const TInt KServerNameMaxLength = 512;
       
    45     TBuf8<KServerNameMaxLength> serverName;
       
    46     serverName.Copy(serverNameUnicode);
       
    47 
       
    48     TServerAuthenticationFailureInput serverAuthenticationInput;
       
    49     serverAuthenticationInput.iOperation = EServerAuthenticationFailure;
       
    50     serverAuthenticationInput.iFailureReason = static_cast<TValidationError>(reason);
       
    51     serverAuthenticationInput.iServerNameLength = serverName.Length();
       
    52     serverAuthenticationInput.iEncodedCertLength = encodedCert.Length();
       
    53 
       
    54     TServerAuthenticationFailureInputBuf inputBuf( serverAuthenticationInput );
       
    55 
       
    56     TInt bufferSize = sizeof( inputBuf ) + serverName.Length() + encodedCert.Length();
       
    57     HBufC8* packedBuffer = HBufC8::NewL( bufferSize );
       
    58     TPtr8 packedBufferPtr( packedBuffer->Des() );
       
    59 
       
    60     packedBufferPtr.Append( inputBuf );
       
    61     packedBufferPtr.Append( serverName );
       
    62     packedBufferPtr.Append( encodedCert );
       
    63 
       
    64     return packedBuffer;
       
    65     }
       
    66 
       
    67 bool ShowDialogL( const QString& server, const QByteArray& certificate, int reason, bool cancel )
       
    68     {
       
    69     RNotifier notifier;
       
    70     User::LeaveIfError( notifier.Connect() );
       
    71     CleanupClosePushL( notifier );
       
    72 
       
    73     HBufC8* buffer = GetInputBufferL( server, certificate, reason );
       
    74     CleanupStack::PushL( buffer );
       
    75 
       
    76     TRequestStatus status;
       
    77     TPckgBuf<TServerAuthenticationFailureDialogResult> resultPckg;
       
    78     notifier.StartNotifierAndGetResponse( status, KUidSecurityDialogNotifier,
       
    79         *buffer, resultPckg );
       
    80     if( cancel )
       
    81         {
       
    82         const TInt KTenSecDelay = 10000000;
       
    83         User::After( KTenSecDelay );
       
    84         notifier.CancelNotifier( KUidSecurityDialogNotifier );
       
    85         }
       
    86     User::WaitForRequest( status );
       
    87     User::LeaveIfError( status.Int() );
       
    88 
       
    89     CleanupStack::PopAndDestroy( buffer );
       
    90     CleanupStack::PopAndDestroy( &notifier );
       
    91     return( resultPckg() == EContinue );
       
    92     }
       
    93 
       
    94 #endif  // Q_OS_SYMBIAN
       
    95 
       
    96 
       
    97 const QString KTestCertDir = "c:/data/testCerts";
       
    98 
       
    99 SrvAuthFailDlgLauncher::SrvAuthFailDlgLauncher(int& argc, char* argv[])
       
   100     : HbApplication(argc, argv), mMainWindow(0), mMainView(0)
       
   101 {
       
   102     mMainWindow = new HbMainWindow();
       
   103     mMainView = new HbView();
       
   104     mMainView->setTitle(tr("SrvAuthFailDlgLauncher"));
       
   105 
       
   106     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
   107 
       
   108     layout->addItem(new HbLabel(tr("Server host name:")));
       
   109     mHostName = new HbTextEdit(tr("some.host.com"));
       
   110     layout->addItem(mHostName);
       
   111     layout->addStretch();
       
   112 
       
   113     layout->addItem(new HbLabel(tr("Certificate file:")));
       
   114     mFilesList = new HbComboBox;
       
   115     mFilesList->setEditable(false);
       
   116     QDir dir(KTestCertDir);
       
   117     if (dir.exists()) {
       
   118         QFileInfoList list = dir.entryInfoList(QDir::Files);
       
   119         QListIterator<QFileInfo> iter(list);
       
   120         while (iter.hasNext()) {
       
   121             const QFileInfo &info(iter.next());
       
   122             mFilesList->addItem(info.fileName());
       
   123         }
       
   124     }
       
   125     layout->addItem(mFilesList);
       
   126     layout->addStretch();
       
   127 
       
   128     layout->addItem(new HbLabel(tr("Validation result:")));
       
   129     mValidationResult = new HbComboBox;
       
   130     QStringList validationResults;
       
   131     validationResults
       
   132 	    << "EValidatedOK"
       
   133 	    << "EChainHasNoRoot"
       
   134 	    << "ESignatureInvalid"
       
   135 	    << "EDateOutOfRange"
       
   136 	    << "ENameIsExcluded"
       
   137 	    << "ENameNotPermitted"
       
   138 	    << "ENotCACert"
       
   139 	    << "ECertificateRevoked"
       
   140 	    << "EUnrecognizedCriticalExtension"
       
   141 	    << "ENoBasicConstraintInCACert"
       
   142 	    << "ENoAcceptablePolicy"
       
   143 	    << "EPathTooLong"
       
   144 	    << "ENegativePathLengthSpecified"
       
   145 	    << "ENamesDontChain"
       
   146 	    << "ERequiredPolicyNotFound"
       
   147 	    << "EBadKeyUsage"
       
   148 	    << "ERootCertNotSelfSigned"
       
   149 	    << "ECriticalExtendedKeyUsage"
       
   150 	    << "ECriticalCertPoliciesWithQualifiers"
       
   151 	    << "ECriticalPolicyMapping"
       
   152 	    << "ECriticalDeviceId"
       
   153 	    << "ECriticalSid"
       
   154 	    << "ECriticalVid"
       
   155 	    << "ECriticalCapabilities";
       
   156     mValidationResult->addItems(validationResults);
       
   157     mValidationResult->setCurrentIndex(1);      // EChainHasNoRoot
       
   158     layout->addItem(mValidationResult);
       
   159     layout->addStretch();
       
   160 
       
   161     mCancelShortly = new HbCheckBox("Cancel after 10 seconds");
       
   162     layout->addItem(mCancelShortly);
       
   163 
       
   164     HbPushButton *button = 0;
       
   165     button = new HbPushButton("Launch dialog");
       
   166     connect(button, SIGNAL(clicked()), this, SLOT(activateDialog()));
       
   167     layout->addItem(button);
       
   168 
       
   169     button = new HbPushButton("Exit");
       
   170     connect(button, SIGNAL(clicked()), qApp, SLOT(quit()));
       
   171     layout->addItem(button);
       
   172 
       
   173     mMainView->setLayout(layout);
       
   174     mMainWindow->addView(mMainView);
       
   175     mMainWindow->show();
       
   176 }
       
   177 
       
   178 SrvAuthFailDlgLauncher::~SrvAuthFailDlgLauncher()
       
   179 {
       
   180     delete mMainView;
       
   181     delete mMainWindow;
       
   182 }
       
   183 
       
   184 void SrvAuthFailDlgLauncher::activateDialog()
       
   185 {
       
   186     QString server = mHostName->toPlainText();
       
   187 
       
   188     QString fileName = mFilesList->currentText();
       
   189     QFile file;
       
   190     QDir::setCurrent(KTestCertDir);
       
   191     file.setFileName(fileName);
       
   192     file.open(QIODevice::ReadOnly);
       
   193     QByteArray fileContent = file.readAll();
       
   194     file.close();
       
   195 
       
   196     int reason = mValidationResult->currentIndex();
       
   197     bool cancel = mCancelShortly->isChecked();
       
   198 
       
   199     int error = 0;
       
   200     bool accepted = false;
       
   201 #ifdef Q_OS_SYMBIAN
       
   202     TRAP( error, accepted = ShowDialogL(server, fileContent, reason, cancel) );
       
   203 #endif // Q_OS_SYMBIAN
       
   204     if (!error) {
       
   205         if (accepted) {
       
   206             HbMessageBox::information(QString("Accepted"));
       
   207         } else {
       
   208             HbMessageBox::information(QString("Declined"));
       
   209         }
       
   210     } else {
       
   211         if (error == KErrCancel) {
       
   212             HbMessageBox::information(QString("Cancelled"));
       
   213         } else {
       
   214             HbMessageBox::information(QString("Failed, error %1").arg(error));
       
   215         }
       
   216     }
       
   217 }
       
   218