cpsecplugins/cpadvancedsecplugin/src/cpsecmodmodel.cpp
branchRCL_3
changeset 22 03674e5abf46
parent 21 09b1ac925e3f
child 23 94da73d93b58
equal deleted inserted replaced
21:09b1ac925e3f 22:03674e5abf46
     1 /*
       
     2 * Copyright (c) 2005-2009 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:   Implementation of the CSecModUIModel class
       
    15 *
       
    16 */
       
    17 
       
    18 #include <unifiedkeystore.h>
       
    19 #include <mctauthobject.h>
       
    20 #include <mctkeystore.h>
       
    21 #include <securityerr.h>
       
    22 
       
    23 #include <qvector.h>
       
    24 #include <qpair.h>
       
    25 #include <qlabel.h>
       
    26 #include <qerrormessage.h>
       
    27 #include <qstring.h>
       
    28 
       
    29 #include <hbmessagebox.h>
       
    30 #include <../../inc/cpsecplugins.h>
       
    31 #include "cpsecmodmodel.h"
       
    32 #include "cpsecmodsyncwrapper.h"
       
    33 
       
    34 const TInt KWIMStoreUid ( 0x101F79D9 );
       
    35 _LIT(KSecModUIPanic, "Security Module UI panic");
       
    36 
       
    37 // ============================= LOCAL FUNCTIONS ===============================
       
    38 GLDEF_C void Panic(TInt aPanic)
       
    39   {
       
    40   User::Panic(KSecModUIPanic, aPanic);
       
    41   }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSecModUIModel::NewL()
       
    45 // Two-phased constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CSecModUIModel* CSecModUIModel::NewL()
       
    49     {
       
    50     RDEBUG("0", 0);
       
    51     CSecModUIModel* self = new( ELeave ) CSecModUIModel();
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop(self);
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CSecModUIModel::CSecModUIModel
       
    60 // C++ default constructor can NOT contain any code, that might leave.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CSecModUIModel::CSecModUIModel()
       
    64     {}
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CSecModUIModel::~CSecModUIModel
       
    68 // C++ destructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 
       
    72 CSecModUIModel::~CSecModUIModel()
       
    73     {
       
    74     delete iWrapper;
       
    75     ResetAndDestroyCTObjects();
       
    76     }
       
    77 // -----------------------------------------------------------------------------
       
    78 // CSecModUIModel::ResetAndDestroyCTObjects(): Cleans up all objects referenced
       
    79 // through the crypto token framework.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void CSecModUIModel::ResetAndDestroyCTObjects()
       
    83     {
       
    84     ResetAndDestroyAOs();
       
    85     iAOKeyStores.Reset();
       
    86     delete iUnifiedKeyStore;
       
    87     iUnifiedKeyStore = NULL;
       
    88     iKeyStore = NULL;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CSecModUIModel::ResetAndDestroyAOs(): Cleans up the list of auth objects.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CSecModUIModel::ResetAndDestroyAOs()
       
    96     {
       
    97     RDEBUG("0", 0);
       
    98     if (iAOList)
       
    99         {
       
   100         iAOList->Release();
       
   101         iAOList = NULL;
       
   102         }
       
   103     if (iAOArray.Count() > 0 &&
       
   104         iAOArray[0]->Token().TokenType().Type().iUid == KTokenTypeFileKeystore)
       
   105         {
       
   106         iAOArray.Reset();
       
   107         }
       
   108     else
       
   109         {
       
   110         iAOArray.Close();
       
   111         }
       
   112     }
       
   113     
       
   114 // -----------------------------------------------------------------------------
       
   115 // CSecModUIModel::ConstructL()
       
   116 // Symbian 2nd phase constructor can leave.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CSecModUIModel::ConstructL()
       
   120     {
       
   121     InitializeKeyStoreL();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSecModUIModel::InitializeKeyStoreL()
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CSecModUIModel::InitializeKeyStoreL()
       
   129     {
       
   130     RDEBUG("0", 0);
       
   131     ResetAndDestroyCTObjects();
       
   132     
       
   133     // Ensure that file server session is succesfully created
       
   134     User::LeaveIfError( iRfs.Connect() );
       
   135         
       
   136     iUnifiedKeyStore = CUnifiedKeyStore::NewL(iRfs);
       
   137 
       
   138     if (iWrapper == NULL)
       
   139         {
       
   140         iWrapper = CSecModUISyncWrapper::NewL();
       
   141         }
       
   142 
       
   143     TInt err = iWrapper->Initialize(*iUnifiedKeyStore);
       
   144 
       
   145     ShowErrorNote(err);
       
   146     if (KErrNone != err && KErrHardwareNotAvailable != err)
       
   147         {
       
   148         User::Leave(err);
       
   149         }
       
   150     TInt keyStoreCount = iUnifiedKeyStore->KeyStoreCount();
       
   151     if (0 >= keyStoreCount)
       
   152         {
       
   153         return;
       
   154         }
       
   155 
       
   156     RMPointerArray<CCTKeyInfo> keyInfos;
       
   157     CleanupClosePushL(keyInfos);
       
   158     TCTKeyAttributeFilter filter;
       
   159     TInt keyInfoCount = 0;
       
   160     filter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
       
   161 
       
   162     for (TInt i = 0; i < keyStoreCount; i++)
       
   163         {
       
   164 		err = iWrapper->ListKeys(iUnifiedKeyStore->KeyStore(i), keyInfos, filter);
       
   165 		User::LeaveIfError(err);
       
   166         //If list ok, append the AO, otherwise go next
       
   167 		keyInfoCount = keyInfos.Count();
       
   168 		for (TInt j = 0; j < keyInfoCount; j++)
       
   169 			{
       
   170 			// Check that keystore has at least one AO.
       
   171 			if (NULL != keyInfos[j]->Protector())
       
   172 				{
       
   173 				// If keystore has AO, add it to the array.
       
   174 				User::LeaveIfError(
       
   175 					iAOKeyStores.Append(&(iUnifiedKeyStore->KeyStore(i))));
       
   176 				break;
       
   177 				}
       
   178 			}     
       
   179         }
       
   180     CleanupStack::PopAndDestroy(&keyInfos);
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CSecModUIModel::LoadTokenLabelsL()
       
   185 // Retrieves the description and location of all tokens.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 QMap<QString,QString> CSecModUIModel::LoadTokenLabelsL()
       
   189     {
       
   190 		RDEBUG("0", 0);
       
   191     TInt keyStoreCount = iAOKeyStores.Count();
       
   192     QMap<QString, QString> labelList;
       
   193     
       
   194     if (0 == keyStoreCount)
       
   195         {
       
   196         return labelList;
       
   197         }
       
   198     
       
   199     for (TInt i = 0; i < keyStoreCount; i++)
       
   200         {
       
   201 		QString keystoreLabel = 0;
       
   202         if (iAOKeyStores[i]->Token().TokenType().Type().iUid == KTokenTypeFileKeystore )
       
   203             {
       
   204 			keystoreLabel = "Phone keystore";
       
   205             }
       
   206         else if(iAOKeyStores[i]->Token().TokenType().Type().iUid == KWIMStoreUid )
       
   207             {                
       
   208 			const TDesC& label = iAOKeyStores[i]->Token().Label();
       
   209 			keystoreLabel = QString( (QChar*) label.Ptr(), label.Length());
       
   210             }
       
   211         else
       
   212         	{
       
   213 			continue;
       
   214         	}
       
   215          QString location = Location(iAOKeyStores[i]->Token().TokenType().Type());
       
   216         labelList.insert(keystoreLabel,location);
       
   217         }
       
   218     return labelList;
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CSecModUIModel::Location() 
       
   223 // Identifies the location of the keystore based on the Uid.
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 QString CSecModUIModel::Location( TUid aUid ) const
       
   227     {
       
   228     RDEBUG("0", 0);
       
   229     QString location = 0;
       
   230 
       
   231     switch ( aUid.iUid )
       
   232         {
       
   233         case KTokenTypeFileKeystore:
       
   234             {
       
   235             location = "\nPhone memory";
       
   236             break;
       
   237             }
       
   238         case KWIMStoreUid:
       
   239             {
       
   240             location = "\nSmart card";
       
   241             break;
       
   242             }
       
   243         default:
       
   244             {
       
   245             break;
       
   246             }
       
   247         }
       
   248     return location;
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CSecModUIModel::ShowErrorNote(TInt aError) const
       
   253 // Displays appropriate error message based on the error code.
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CSecModUIModel::ShowErrorNote(TInt aError) const
       
   257     {
       
   258 	QString sError;
       
   259 		RDEBUG("aError", aError);
       
   260     switch (aError)
       
   261         {
       
   262         case KErrCancel:
       
   263         case KErrLocked:
       
   264         case KErrNone:
       
   265             {
       
   266             break;
       
   267             }
       
   268         case KErrInUse:
       
   269             {
       
   270             sError = "Key is currently in use. Unable to delete. Close applications and try again.";
       
   271             break;
       
   272             }
       
   273         case KErrBadPassphrase:
       
   274             {
       
   275             sError = "Code incorrect";
       
   276             break;
       
   277             }
       
   278         case KErrHardwareNotAvailable: // flow thru
       
   279         default:
       
   280             {
       
   281             sError = "Security module internal error";
       
   282             break;
       
   283             }
       
   284         }
       
   285 
       
   286     if ( sError.length() != 0 )
       
   287 		{
       
   288 		HbMessageBox::information(sError);
       
   289 		}
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CSecModUIModel::OpenTokenL(TInt aTokenIndex)
       
   294 // Retrieves the appropriate token based on the index.
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 void CSecModUIModel::OpenTokenL(TInt aTokenIndex)
       
   298     {
       
   299     RDEBUG("0", 0);
       
   300     ResetAndDestroyAOs();
       
   301     iKeyStore = iAOKeyStores[aTokenIndex];
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CSecModUIModel::CheckCodeViewStringsL()
       
   306 // Initialize the auth objects and return the total count.
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TInt CSecModUIModel::CheckCodeViewStringsL()
       
   310     {
       
   311     InitAuthObjectsL();
       
   312     ListAuthObjectsL();
       
   313     return iAOArray.Count();
       
   314     }
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CSecModUIModel::InitAuthObjectsL()
       
   318 // Retrieve the authentication object associated with the appropriate key store 
       
   319 // implementation.
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 void CSecModUIModel::InitAuthObjectsL()
       
   323     {
       
   324     RDEBUG("0", 0);
       
   325     TInt err = KErrNone;
       
   326     // Symbian's file key store does not support
       
   327     // MCTAuthenticationObjectList interface, so we need to use
       
   328     // other way to get AO.
       
   329     if (iKeyStore->Token().TokenType().Type().iUid == KTokenTypeFileKeystore)
       
   330         {
       
   331         if (iAOArray.Count()==0)
       
   332             {
       
   333             RMPointerArray<CCTKeyInfo> keyInfos;
       
   334             CleanupClosePushL(keyInfos);
       
   335             TCTKeyAttributeFilter filter;
       
   336             TInt keyInfoCount = 0;
       
   337             filter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
       
   338             err = iWrapper->ListKeys(*iKeyStore, keyInfos, filter);
       
   339             ShowErrorNote(err);
       
   340             User::LeaveIfError(err);
       
   341             keyInfoCount = keyInfos.Count();
       
   342             for (TInt j = 0; j < keyInfoCount; j++)
       
   343                 {
       
   344                 // Check that keystore has at least one AO.
       
   345                 if (NULL != keyInfos[j]->Protector())
       
   346                     {
       
   347                     // If keystore has AO, add it to the array.
       
   348                     User::LeaveIfError(iAOArray.Append(keyInfos[j]->Protector()));
       
   349                     break;
       
   350                     }
       
   351                 }
       
   352             CleanupStack::PopAndDestroy(&keyInfos);  //keyInfos
       
   353             }
       
   354         }
       
   355     else
       
   356         {
       
   357         if (NULL == iAOList)
       
   358             {
       
   359             MCTTokenInterface* tokenInterface = NULL;
       
   360             err = iWrapper->GetAuthObjectInterface(
       
   361                 iKeyStore->Token(), tokenInterface);
       
   362             if ( KErrNone != err || NULL == tokenInterface )
       
   363                 {
       
   364                 ShowErrorNote(err);
       
   365                 User::Leave(err);
       
   366                 }
       
   367             iAOList = (MCTAuthenticationObjectList*)tokenInterface;
       
   368             }
       
   369         }
       
   370     }
       
   371 // -----------------------------------------------------------------------------
       
   372 // CSecModUIModel::ListAuthObjectsL()
       
   373 // list the auth objects.
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 void CSecModUIModel::ListAuthObjectsL()
       
   377     {
       
   378     RDEBUG("0", 0);
       
   379     if (0 >= iAOArray.Count())
       
   380         {
       
   381         __ASSERT_ALWAYS(iAOList, Panic(EPanicNullPointer));
       
   382         TInt err = iWrapper->ListAuthObjects(*iAOList, iAOArray);
       
   383         if (err)
       
   384             {
       
   385             ShowErrorNote(err);
       
   386             User::Leave(err);
       
   387             }
       
   388         }
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CSecModUIModel::TokenLabelForTitle() const
       
   393 // Retrieves description of the token based on UID of the current active keystore.
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 QString CSecModUIModel::TokenLabelForTitle() const
       
   397     {
       
   398 	QString qLabel;
       
   399     if (iKeyStore->Token().TokenType().Type().iUid 
       
   400         == KTokenTypeFileKeystore)
       
   401         {
       
   402         qLabel = "Phone keystore";   
       
   403         }
       
   404     else
       
   405         {            
       
   406         const TDesC& label = iKeyStore->Token().Label();
       
   407         qLabel = QString((QChar*)label.Ptr(),label.Length());
       
   408         }
       
   409     return qLabel;
       
   410     }
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CSecModUIModel::AuthStatus(TInt aIndex) const
       
   414 // Returns status of the selected auth object. 
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 TUint32 CSecModUIModel::AuthStatus(TInt aIndex) const
       
   418 		{
       
   419 		return iAOArray[aIndex]->Status();
       
   420 		}
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CSecModUIModel::PINStatus( TInt aIndex, TBool aShowBlockedNote ) const
       
   424 // Returns status of the auth object based on its persistent properties set
       
   425 // by security module implementation.
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 QString CSecModUIModel::PINStatus(  TInt aIndex, TBool aShowBlockedNote ) const
       
   429     {
       
   430     RDEBUG("0", 0);
       
   431     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   432     QString pinStatus;
       
   433     TUint32 status = iAOArray[aIndex]->Status();
       
   434     
       
   435     if (status & EAuthObjectBlocked)
       
   436         {
       
   437         if (aShowBlockedNote)
       
   438             {
       
   439 			QString information = 0;
       
   440             if (status & EUnblockDisabled)
       
   441                 {
       
   442 				information = " totalblocked, contact your module vendor.";
       
   443                 }
       
   444             else
       
   445                 {
       
   446 				information = " is blocked. PUK code is needed to unblock the code";
       
   447                 }
       
   448             information.prepend(QString( (QChar*)(iAOArray[aIndex]->Label()).Ptr(),iAOArray[aIndex]->Label().Length() ) );
       
   449             HbMessageBox::information(information);        
       
   450             }            
       
   451         pinStatus = "Blocked";
       
   452         }
       
   453     else if ( PinOpen(aIndex) )
       
   454         {
       
   455 		pinStatus = "Open";
       
   456         }
       
   457     else
       
   458         {
       
   459 		pinStatus = "Closed";
       
   460         }
       
   461     return pinStatus;
       
   462     }
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CSecModUIModel::PinOpen(TInt aIndex) const
       
   466 // Checks whether the Pin is still available.
       
   467 // -----------------------------------------------------------------------------
       
   468 //
       
   469 TBool CSecModUIModel::PinOpen(TInt aIndex) const
       
   470     {
       
   471     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   472     TInt timeRemaining = 0;
       
   473     TInt err = iWrapper->TimeRemaining(*(iAOArray[aIndex]), timeRemaining);
       
   474     TBool ret = ETrue;
       
   475     if ( 0 < timeRemaining )
       
   476         {
       
   477         ret = ETrue;
       
   478         }
       
   479     else if( timeRemaining == -1 )
       
   480         {
       
   481         ret = ETrue;
       
   482         }
       
   483     else
       
   484         {
       
   485         ret = EFalse;
       
   486         }
       
   487     return ret;
       
   488     }
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CSecModUIModel::ChangeOrUnblockPinL(TInt aIndex)
       
   492 // Based on the property of auth object the pin can be changed or unblocked.
       
   493 // -----------------------------------------------------------------------------
       
   494 //
       
   495 void CSecModUIModel::ChangeOrUnblockPinL(TInt aIndex)
       
   496     {
       
   497     RDEBUG("0", 0);
       
   498     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   499     TUint32 status = iAOArray[aIndex]->Status();
       
   500 
       
   501     if ((status & EAuthObjectBlocked) && (status & EUnblockDisabled))
       
   502         {
       
   503         return; // We cannot do anything. PIN is total bolcked.
       
   504         }
       
   505     else if (status & EAuthObjectBlocked)
       
   506         {
       
   507         // Let's unblock the PIN
       
   508         UnblockPinL(aIndex);
       
   509         }
       
   510     else if (status & EEnabled)
       
   511         {
       
   512         // PIN is enabled. Let's change the PIN.
       
   513         ChangePinL(aIndex);
       
   514         }
       
   515     }
       
   516 
       
   517 // -----------------------------------------------------------------------------
       
   518 // CSecModUIModel::UnblockPinL(TInt aIndex)
       
   519 // Unblocks PIN.
       
   520 // -----------------------------------------------------------------------------
       
   521 //
       
   522 void CSecModUIModel::UnblockPinL(TInt aIndex)
       
   523     {
       
   524     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   525     TInt err = iWrapper->UnblockAuthObject(*(iAOArray[aIndex]));
       
   526     if (KErrNone == err)
       
   527         {
       
   528         HbMessageBox::information("Code unblocked");
       
   529         }
       
   530     else
       
   531         {
       
   532         ShowErrorNote(err);
       
   533         }
       
   534     }
       
   535 
       
   536 // -----------------------------------------------------------------------------
       
   537 // CSecModUIModel::ChangePinL(TInt aIndex)
       
   538 // Changes the value of the PIN.
       
   539 // -----------------------------------------------------------------------------
       
   540 //
       
   541 void CSecModUIModel::ChangePinL(TInt aIndex)
       
   542     {
       
   543     RDEBUG("0", 0);
       
   544     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   545     TInt err = KErrNone;
       
   546   
       
   547     err = iWrapper->ChangeReferenceData(*(iAOArray[aIndex]));
       
   548     if (err)
       
   549         {
       
   550         ShowErrorNote(err);
       
   551         }
       
   552     else
       
   553         {
       
   554 		HbMessageBox::information("Code changed");
       
   555         }
       
   556     }
       
   557 
       
   558 // -----------------------------------------------------------------------------
       
   559 // CSecModUIModel::ChangeCodeRequestL(TInt aIndex)
       
   560 // Toggles the value of auth object if possible.
       
   561 // -----------------------------------------------------------------------------
       
   562 //
       
   563 TInt CSecModUIModel::ChangeCodeRequest(TInt aIndex)
       
   564     {
       
   565     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   566     TUint32 status = iAOArray[aIndex]->Status();
       
   567     TInt err = KErrNone;
       
   568     if (status & EDisableAllowed &&  !(status & EAuthObjectBlocked))
       
   569         {
       
   570         if ( status & EEnabled )
       
   571             {
       
   572             err = iWrapper->DisableAuthObject(*(iAOArray[aIndex]));
       
   573             }
       
   574         else
       
   575             {
       
   576             err = iWrapper->EnableAuthObject(*(iAOArray[aIndex]));
       
   577             }
       
   578         ShowErrorNote(err);
       
   579         }
       
   580     return err;
       
   581     }
       
   582 
       
   583 // -----------------------------------------------------------------------------
       
   584 // CSecModUIModel::CloseAuthObjL(TInt aIndex)
       
   585 // If the auth object is open then closes it.
       
   586 // -----------------------------------------------------------------------------
       
   587 //
       
   588 void CSecModUIModel::CloseAuthObjL(TInt aIndex)
       
   589     {
       
   590     if (PinOpen(aIndex))
       
   591         {
       
   592         __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   593         TInt err = iWrapper->CloseAuthObject(*(iAOArray[aIndex]));
       
   594         if (err)
       
   595             {
       
   596             ShowErrorNote(err);
       
   597             User::Leave(err);
       
   598             }
       
   599         }
       
   600     }
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // CSecModUIModel::DeleteKeysL(TInt aTokenIndex)
       
   604 // Deletes key from the appropriate keystore.
       
   605 // -----------------------------------------------------------------------------
       
   606 //
       
   607 void CSecModUIModel::DeleteKeysL(TInt aTokenIndex)
       
   608     {
       
   609     RDEBUG("0", 0);
       
   610     __ASSERT_ALWAYS(aTokenIndex < iAOKeyStores.Count(), Panic(EPanicIndexOutOfRange));
       
   611     RMPointerArray<CCTKeyInfo> keyInfos;
       
   612 	CleanupClosePushL(keyInfos);
       
   613 	TCTKeyAttributeFilter filter;
       
   614 	filter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
       
   615 	TInt err = iWrapper->ListKeys(*iAOKeyStores[aTokenIndex], keyInfos, filter);
       
   616 	ShowErrorNote(err);
       
   617 	User::LeaveIfError(err);
       
   618 	for (TInt i = 0; i < keyInfos.Count(); ++i)
       
   619 		{
       
   620 		err = iWrapper->DeleteKey(*iUnifiedKeyStore, keyInfos[i]->Handle());
       
   621 		ShowErrorNote(err);
       
   622 
       
   623 		if (KErrHardwareNotAvailable == err )
       
   624 			{
       
   625 			break; // Break the loop, if keystore not available
       
   626 			}
       
   627 		}
       
   628 	CleanupStack::PopAndDestroy(&keyInfos);  //keyInfos
       
   629 	ResetAndDestroyCTObjects();
       
   630 	InitializeKeyStoreL();
       
   631 	}
       
   632 
       
   633 // -----------------------------------------------------------------------------
       
   634 // CSecModUIModel::SecModDetailsL(TInt aTokenIndex)
       
   635 // Retrieve security module details and cleansup all currently held auth objects.
       
   636 // -----------------------------------------------------------------------------
       
   637 //
       
   638 QVector< QPair<QString,QString> > CSecModUIModel::SecModDetailsL(TInt aTokenIndex)
       
   639     {
       
   640     iKeyStore = iAOKeyStores[aTokenIndex];
       
   641     InitAuthObjectsL();
       
   642     ListAuthObjectsL();
       
   643     QVector< QPair<QString,QString> > securityDetails = GetSecModDetailsL();
       
   644     iKeyStore = NULL; // not owned
       
   645     if (iAOList)
       
   646         {
       
   647         iAOList->Release();
       
   648         iAOList = NULL;
       
   649         }
       
   650     iAOArray.Reset();
       
   651     return securityDetails;
       
   652     }
       
   653 
       
   654 // -----------------------------------------------------------------------------
       
   655 // CSecModUIModel::GetSecModDetailsL()
       
   656 // Retrieve security module details.
       
   657 // -----------------------------------------------------------------------------
       
   658 //
       
   659 QVector< QPair<QString,QString> > CSecModUIModel::GetSecModDetailsL()
       
   660     {
       
   661     RDEBUG("0", 0);
       
   662     MCTToken& token = iKeyStore->Token();
       
   663     QVector< QPair<QString,QString> > securityModDetails;
       
   664     
       
   665     if (token.TokenType().Type().iUid == KTokenTypeFileKeystore)
       
   666         { 
       
   667 		QPair<QString,QString> name("Name:","Phone keystore");
       
   668         securityModDetails.append(name);
       
   669         }
       
   670     else 
       
   671         {            
       
   672 		QPair<QString,QString> name("Name:","(No label)");
       
   673         securityModDetails.append(name);
       
   674         }
       
   675 
       
   676     AppendItem(securityModDetails, "Version:", token.Information( MCTToken::EVersion ), "(Not defined)" );
       
   677     AppendLocation(securityModDetails, token.TokenType().Type(), QString("Security module location:"));
       
   678     
       
   679     if (token.TokenType().Type().iUid == KTokenTypeFileKeystore)
       
   680         { 
       
   681 		QPair<QString,QString> accessCode("Access code:","Phone keystore code");
       
   682         securityModDetails.append(accessCode);
       
   683         }
       
   684     else 
       
   685         {            
       
   686         AppendItem(securityModDetails, "Access code:", iAOArray[0]->Label(), "Access code:" );
       
   687         }
       
   688 
       
   689     AppendPinNRs(securityModDetails);
       
   690 
       
   691     AppendItem(	securityModDetails, "Serial number:",
       
   692 				token.Information( MCTToken::ESerialNo),
       
   693 				"(Not defined)");
       
   694 
       
   695     AppendItem(	securityModDetails, "Manufacturer:",
       
   696 				token.Information( MCTToken::EManufacturer),
       
   697 				"(Not defined)");
       
   698     return securityModDetails;
       
   699     }
       
   700 
       
   701 // -----------------------------------------------------------------------------
       
   702 // CSecModUIModel::AppendLocation()
       
   703 // Appends the location of the keystore.
       
   704 // -----------------------------------------------------------------------------
       
   705 //
       
   706 void CSecModUIModel::AppendLocation( QVector< QPair<QString,QString> >& aSecModDetails, 
       
   707 									 TUid aLocUid, 
       
   708 									 QString aCertDisplayDetails )
       
   709     {
       
   710     QString locationDetails = "(Not defined)";
       
   711 
       
   712     switch ( aLocUid.iUid )
       
   713         {
       
   714     	case KTokenTypeFileKeystore:
       
   715         	locationDetails = "Phone memory";
       
   716             break;
       
   717 
       
   718         case KWIMStoreUid:
       
   719         	locationDetails = "Smart card";
       
   720             break;
       
   721 
       
   722         default:
       
   723             break;
       
   724         }
       
   725     aSecModDetails.append(QPair<QString,QString>(aCertDisplayDetails, locationDetails));
       
   726     }
       
   727 
       
   728 // -----------------------------------------------------------------------------
       
   729 // CSecModUIModel::AppendItem()
       
   730 // Appends the value of a label if present otherwise appends empty descriotion.
       
   731 // -----------------------------------------------------------------------------
       
   732 //
       
   733 void CSecModUIModel::AppendItem( 	QVector< QPair<QString,QString> >& aSecModDetails, 
       
   734 									QString aLabel, 
       
   735 									const TDesC& aValue, 
       
   736 									QString aNullDescription)
       
   737 	{
       
   738 	RDEBUG("0", 0);
       
   739 	if( aValue.Length() == 0 )
       
   740 		{
       
   741 		aSecModDetails.append(QPair<QString,QString>(aLabel, aNullDescription));
       
   742 		}
       
   743 	else
       
   744 		{
       
   745 		QString value = QString((QChar*) aValue.Ptr(),aValue.Length());
       
   746 		aSecModDetails.append(QPair<QString,QString>(aLabel, value));
       
   747 		}
       
   748 	}
       
   749 
       
   750 // -------------------------------------------------------------------------------
       
   751 // CSecModUIModel::AppendPinNRs(QVector< QPair<QString,QString> >& aSecModDetails)
       
   752 // Generate details of signing security module.
       
   753 // -------------------------------------------------------------------------------
       
   754 //
       
   755 void CSecModUIModel::AppendPinNRs(QVector< QPair<QString,QString> >& aSecModDetails)
       
   756     {
       
   757     TInt pinCount = iAOArray.Count();
       
   758     TInt j = 0;
       
   759 
       
   760 	for (TInt i=1; i<pinCount; i++)
       
   761 		{
       
   762 		QString noLabelDetails = "Signing code ";
       
   763 		const TDesC& label = iAOArray[i]->Label();
       
   764 		if(label.Length() == 0)
       
   765 			{
       
   766 			noLabelDetails.append(++j);
       
   767 			}
       
   768 		QString signingCode = "Signing code:";
       
   769 		AppendItem(aSecModDetails, signingCode , iAOArray[i]->Label(), noLabelDetails);
       
   770 		}
       
   771     }
       
   772 
       
   773 // -----------------------------------------------------------------------------
       
   774 // CSecModUIModel::AuthDetails() const
       
   775 // Return the list of pair of description and status of each auth object.
       
   776 // -----------------------------------------------------------------------------
       
   777 //
       
   778 QVector<QPair<QString, TUint32> > CSecModUIModel::AuthDetails()
       
   779 	{
       
   780 	QVector<QPair<QString, TUint32> > authDetails;
       
   781 	
       
   782 	// Here is assumed that the first AO is always PIN-G
       
   783 	for (TInt i = 1; i < iAOArray.Count(); i++)
       
   784 		{
       
   785 		const TDesC& label = iAOArray[i]->Label();
       
   786 		TUint32 status = iAOArray[i]->Status();
       
   787 		QString sLabel = QString((QChar*) label.Ptr(), label.Length());
       
   788 		QPair<QString, TUint32> pair(sLabel, status);
       
   789 		authDetails.append(pair);
       
   790 		}
       
   791 	return authDetails;
       
   792 	}
       
   793 	
       
   794 // -----------------------------------------------------------------------------
       
   795 // CSecModUIModel::ChangePinNrL(TInt aIndex)
       
   796 // Change the pin value for the singing module.
       
   797 // -----------------------------------------------------------------------------
       
   798 //
       
   799 void CSecModUIModel::ChangePinNrL(TInt aIndex)
       
   800     {
       
   801     RDEBUG("0", 0);
       
   802     ChangePinL(aIndex+1);
       
   803     }
       
   804 
       
   805 
       
   806 // -----------------------------------------------------------------------------
       
   807 // CSecModUIModel::UnblockPinNrL(TInt aIndex)
       
   808 // Unblock the value for the signing code module.
       
   809 // -----------------------------------------------------------------------------
       
   810 //
       
   811 void CSecModUIModel::UnblockPinNrL(TInt aIndex)
       
   812     {
       
   813     UnblockPinL(aIndex+1);
       
   814     }
       
   815 
       
   816 // -----------------------------------------------------------------------------
       
   817 // CSecModUIModel::KeyStoreUID()
       
   818 // Retrieves the UID of the current active keystore.
       
   819 // -----------------------------------------------------------------------------
       
   820 //
       
   821 TInt CSecModUIModel::KeyStoreUID()
       
   822 	{
       
   823 	return iKeyStore->Token().TokenType().Type().iUid;
       
   824 	}
       
   825 
       
   826 // -----------------------------------------------------------------------------
       
   827 // CSecModUIModel::AuthObj(TInt aIndex)
       
   828 // Retrieves the auth object at the speficied index.
       
   829 // -----------------------------------------------------------------------------
       
   830 //
       
   831 const MCTAuthenticationObject& CSecModUIModel::AuthObj(TInt aIndex)
       
   832 	{
       
   833 	return *iAOArray[aIndex];
       
   834 	}
       
   835 
       
   836 // -----------------------------------------------------------------------------
       
   837 // CSecModUIModel::PinChangeable(TInt aIndex) const
       
   838 // Returns the status accordingly if the pin is changeable or not.
       
   839 // -----------------------------------------------------------------------------
       
   840 //
       
   841 TBool CSecModUIModel::PinChangeable(TInt aIndex) const
       
   842     {
       
   843     RDEBUG("0", 0);
       
   844     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   845     TUint32 status = iAOArray[aIndex]->Status();
       
   846     TBool ret = ETrue;
       
   847     if ( (status & EAuthObjectBlocked) || (status & EChangeDisabled) )
       
   848         {
       
   849         ret = EFalse;
       
   850         }
       
   851     else if ( status & EEnabled )
       
   852         {
       
   853         ret = ETrue;
       
   854         }
       
   855     else // PIN is disabled;
       
   856         {
       
   857         ret = EFalse;
       
   858         }
       
   859     return ret;
       
   860     }
       
   861 
       
   862 // -----------------------------------------------------------------------------
       
   863 // CSecModUIModel::PinUnblockable(TInt aIndex) const
       
   864 // Returns the status accordingly if the pin is unblockable or not.
       
   865 // -----------------------------------------------------------------------------
       
   866 //
       
   867 TBool CSecModUIModel::PinUnblockable(TInt aIndex) const
       
   868     {
       
   869     TUint32 status = iAOArray[aIndex]->Status();
       
   870     TBool ret = ETrue;
       
   871     if ( status & EUnblockDisabled )
       
   872         {
       
   873         ret = EFalse;
       
   874         }
       
   875     else if ( status & EAuthObjectBlocked )
       
   876         {
       
   877         ret = ETrue;
       
   878         }
       
   879     else // PIN is not blocked;
       
   880         {
       
   881         ret = EFalse;
       
   882         }
       
   883     return ret;
       
   884     }
       
   885 
       
   886 // -----------------------------------------------------------------------------
       
   887 // CSecModUIModel::PinRequestChangeable(TInt aIndex) const
       
   888 // Returns the status accordingly if the pin is changeable or not.
       
   889 // -----------------------------------------------------------------------------
       
   890 //
       
   891 TBool CSecModUIModel::PinRequestChangeable(TInt aIndex) const
       
   892     {
       
   893     RDEBUG("0", 0);
       
   894     __ASSERT_ALWAYS(aIndex < iAOArray.Count(), Panic(EPanicIndexOutOfRange));
       
   895     TUint32 status = iAOArray[aIndex]->Status();
       
   896     TBool ret = EFalse;
       
   897     if ( status & EAuthObjectBlocked )
       
   898         {
       
   899         ret = EFalse;
       
   900         }
       
   901     else if ( status & EDisableAllowed )
       
   902         {
       
   903         ret = ETrue;
       
   904         }
       
   905     else // Disable not allowed;
       
   906         {
       
   907         ret = EFalse;
       
   908         }
       
   909     return ret;
       
   910     }
       
   911 
       
   912 // -----------------------------------------------------------------------------
       
   913 // CSecModUIModel::IsTokenDeletable(TInt aTokenIndex)
       
   914 // Returns the status accordingly if the token is deletable or not.
       
   915 // -----------------------------------------------------------------------------
       
   916 //
       
   917 TBool CSecModUIModel::IsTokenDeletable(TInt aTokenIndex)
       
   918     {
       
   919     __ASSERT_ALWAYS(aTokenIndex < iAOKeyStores.Count(), Panic(EPanicIndexOutOfRange));
       
   920     TBool ret = EFalse;
       
   921     if (iAOKeyStores[aTokenIndex]->Token().TokenType().Type().iUid
       
   922         == KTokenTypeFileKeystore)
       
   923         {
       
   924         ret = ETrue;
       
   925         }
       
   926     else
       
   927         {
       
   928         ret = EFalse;
       
   929         }
       
   930     return ret;
       
   931     }
       
   932 
       
   933 //  End of File
       
   934 
       
   935