pkiutilities/CertSaver/src/CertSaverModel.cpp
branchRCL_3
changeset 50 03674e5abf46
parent 49 09b1ac925e3f
child 54 94da73d93b58
equal deleted inserted replaced
49:09b1ac925e3f 50:03674e5abf46
    14 * Description:   Class that handles user input and launching of dialogs
    14 * Description:   Class that handles user input and launching of dialogs
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
       
    19 // INCLUDE FILES
       
    20 #include <aknnotewrappers.h>        // Note dialogs
       
    21 #include <aknmessagequerydialog.h>  // For CAknMessageQueryDialog
    19 #include <StringLoader.h>           // For loading resource strings
    22 #include <StringLoader.h>           // For loading resource strings
    20 #include <unifiedcertstore.h>       // For saving the certificates
    23 #include <unifiedcertstore.h>       // For saving the certificates
    21 #include <mctwritablecertstore.h>   // For saving the certificates
    24 #include <mctwritablecertstore.h>   // For saving the certificates
    22 #include <sysutil.h>                // For FFSSpaceBelowCriticalLevelL(..)
    25 #include <sysutil.h>                // For FFSSpaceBelowCriticalLevelL(..)
    23 #include <X509CertNameParser.h>     // For default label
    26 #include <X509CertNameParser.h>     // For default label
    30 #include <asnpkcs.h>
    33 #include <asnpkcs.h>
    31 #include <unifiedkeystore.h>
    34 #include <unifiedkeystore.h>
    32 #include <x509certext.h>
    35 #include <x509certext.h>
    33 #include <mctkeystore.h>
    36 #include <mctkeystore.h>
    34 #include <TrustedSitesStore.h>
    37 #include <TrustedSitesStore.h>
    35 #include <eikenv.h>                 // CEikonEnv
       
    36 #include <AknUtils.h>               // AknTextUtils
       
    37 
    38 
    38 #include <CertSaver.rsg>
    39 #include <CertSaver.rsg>
    39 #include "CertSaverModel.h"
    40 #include "CertSaverModel.h"
    40 #include "CertSaverDocument.h"
    41 #include "CertSaverDocument.h"
    41 #include "CertSaverAppUi.h"
    42 #include "CertSaverAppUi.h"
    42 #include "CertSaverSyncWrapper.h"
    43 #include "CertSaverSyncWrapper.h"
    43 #include "certsaver.hrh"
    44 #include "certsaver.hrh"
    44 #include "securityuisvariant.hrh"
    45 #include "securityuisvariant.hrh"
    45 
    46 
    46 #include <hbdevicemessageboxsymbian.h>
       
    47 #include <hbdevicenotificationdialogsymbian.h>
       
    48 #include <hbsymbianvariant.h>
       
    49 
       
    50 #include "SecQueryUi.h"                   // needed for label dialog
       
    51 
       
    52 // CONSTANTS
    47 // CONSTANTS
    53 
       
    54 const TInt32 KWTLSTrusterUID( 268479059 );
    48 const TInt32 KWTLSTrusterUID( 268479059 );
    55 const TInt32 KInternetTrusterUID( 268441661 );
    49 const TInt32 KInternetTrusterUID( 268441661 );
    56 const TInt32 KApplicationControllerTrusterUID( 268452523 );
    50 const TInt32 KApplicationControllerTrusterUID( 268452523 );
    57 const TInt32 KJavaInstallTrusterUID( 270506792 );
    51 const TInt32 KJavaInstallTrusterUID( 270506792 );
    58 const TInt32 KOCSPTrusterUID( 268478646 );
    52 const TInt32 KOCSPTrusterUID( 268478646 );
    60 
    54 
    61 const TInt KTrusterArrayInitSize( 2 );
    55 const TInt KTrusterArrayInitSize( 2 );
    62 
    56 
    63 const TInt KMaxLengthTextMeassageBody( 5000 );
    57 const TInt KMaxLengthTextMeassageBody( 5000 );
    64 const TInt KMaxLengthTextDateString( 11 );    // "dd/mm/yyyy0"
    58 const TInt KMaxLengthTextDateString( 11 );    // "dd/mm/yyyy0"
    65 const TInt KMaxLengthTextCheckBoxData( 255 );    // "dd/mm/yyyy0"
       
    66 
    59 
    67 //Issuer and Owner max visible length
    60 //Issuer and Owner max visible length
    68 const TInt KMaxLengthTextCertIdentifierVisible( 1000 );
    61 const TInt KMaxLengthTextCertIdentifierVisible( 1000 );
    69 const TInt KAttempts( 3 );
    62 const TInt KAttempts( 3 );
    70 
    63 
    71 const TInt KFingerprintLength( 50 );
    64 const TInt KFingerprintLength( 50 );
    72 const TInt KTokenLength( 32 );
       
    73 
    65 
    74 _LIT( KCertSaverListBoxItemPrefix, "1\t" );
    66 _LIT( KCertSaverListBoxItemPrefix, "1\t" );
    75 const TInt KItemBufLen = 2 + KMaxName;
    67 const TInt KItemBufLen = 2 + KMaxName;
    76 
    68 
    77 _LIT( KPrivateKeyLabel, "PrivateKey " );
    69 _LIT( KPrivateKeyLabel, "PrivateKey " );
   122 void CCertSaverModel::SaveCertificateL(
   114 void CCertSaverModel::SaveCertificateL(
   123             const CX509Certificate& aCertificate,
   115             const CX509Certificate& aCertificate,
   124             const TCertificateOwnerType& aOwnerType,
   116             const TCertificateOwnerType& aOwnerType,
   125             const TCertificateFormat& aCertFormat )
   117             const TCertificateFormat& aCertFormat )
   126     {
   118     {
   127 
       
   128     iCertOwnerType = aOwnerType;
   119     iCertOwnerType = aOwnerType;
   129     iCertFormat = aCertFormat;
   120     iCertFormat = aCertFormat;
   130     iNewCert = &aCertificate;
   121     iNewCert = &aCertificate;
   131 
   122 
   132     if ( aOwnerType == EPeerCertificate )
   123     if ( aOwnerType == EPeerCertificate )
   146 //
   137 //
   147 // ----------------------------------------------------------
   138 // ----------------------------------------------------------
   148 //
   139 //
   149 void CCertSaverModel::DoSavePrivateKeyL( const TDesC8& aKey )
   140 void CCertSaverModel::DoSavePrivateKeyL( const TDesC8& aKey )
   150     {
   141     {
   151 
       
   152     CheckFSSpaceL( aKey );
   142     CheckFSSpaceL( aKey );
   153 
   143 
   154     TKeyIdentifier keyIdentifier;
   144     TKeyIdentifier keyIdentifier;
   155     CDecPKCS8Data* pkcs8Data = TASN1DecPKCS8::DecodeDERL( aKey );
   145     CDecPKCS8Data* pkcs8Data = TASN1DecPKCS8::DecodeDERL( aKey );
   156     CleanupStack::PushL( pkcs8Data );
   146     CleanupStack::PushL( pkcs8Data );
   166     TBuf<KPrivaKeyLabelLength> keyLabel( KPrivateKeyLabel );
   156     TBuf<KPrivaKeyLabelLength> keyLabel( KPrivateKeyLabel );
   167     TKeyUsagePKCS15 keyUsage = KeyUsageL( keyIdentifier, pkcs8Data->Algorithm() );
   157     TKeyUsagePKCS15 keyUsage = KeyUsageL( keyIdentifier, pkcs8Data->Algorithm() );
   168     CleanupStack::PopAndDestroy( pkcs8Data );
   158     CleanupStack::PopAndDestroy( pkcs8Data );
   169     if (KeyAlreadyExistsL( startDate, endDate, keyIdentifier, keyUsage) )
   159     if (KeyAlreadyExistsL( startDate, endDate, keyIdentifier, keyUsage) )
   170         {
   160         {
   171         // used to leave with error none
   161         User::Leave( KErrNone );
   172         return;
       
   173         }
   162         }
   174 
   163 
   175     TInt accessType( 0 );
   164     TInt accessType( 0 );
   176 
   165 
   177     accessType |= CCTKeyInfo::EExtractable;
   166     accessType |= CCTKeyInfo::EExtractable;
   205             break;
   194             break;
   206             }
   195             }
   207         case KErrKeySize:
   196         case KErrKeySize:
   208         case KErrArgument:
   197         case KErrArgument:
   209             {
   198             {
   210             ShowInformationNoteL( R_CERTSAVER_KEY_TYPE_NOT_SUPPORTED );
   199             ShowErrorNoteL( R_CERTSAVER_KEY_TYPE_NOT_SUPPORTED );
   211             User::Leave( KErrCancel );
   200             User::Leave( KErrCancel );
   212             break;
   201             break;
   213             }
   202             }
   214         case KErrKeyValidity:
   203         case KErrKeyValidity:
   215             {
   204             {
   216             ShowInformationNoteL( R_CERTSAVER_QTN_CM_PKCS12_EXPIRED );
   205             ShowInformationNoteL( R_CERTSAVER_QTN_CM_PKCS12_EXPIRED );
   217             User::Leave( KErrCancel );
   206             User::Leave( KErrCancel );
   218             }
   207             }
   219         case KErrKeyUsage:
   208         case KErrKeyUsage:
   220             {
   209             {
   221             ShowInformationNoteL( R_CERTSAVER_PRIVATE_KEY_CORRUPTED );
   210             ShowErrorNoteL( R_CERTSAVER_PRIVATE_KEY_CORRUPTED );
   222             User::Leave( KErrCancel );
   211             User::Leave( KErrCancel );
   223             break;
   212             break;
   224             }
   213             }
   225         case KErrCancel:
   214         case KErrCancel:
   226         case KErrPermissionDenied:
   215         case KErrPermissionDenied:
   227             {
   216             {
   228             ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
   217             ShowErrorNoteL( R_CERTSAVER_PKCS12_DISCARDED );
   229             User::Leave( KErrCancel );
   218             User::Leave( KErrCancel );
   230             break;
   219             break;
   231             }
   220             }
   232         case KErrCorrupt:
   221         case KErrCorrupt:
   233         case KErrEof:
   222         case KErrEof:
   234             {
   223             {
   235             ShowInformationNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   224             ShowErrorNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   236             User::Leave( KErrCancel );
   225             User::Leave( KErrCancel );
   237             break;
   226             break;
   238             }
   227             }
   239         case KErrAlreadyExists:
   228         case KErrAlreadyExists:
   240             {
   229             {
   263     TTime& aStartDate,
   252     TTime& aStartDate,
   264     TTime& aEndDate,
   253     TTime& aEndDate,
   265     const TKeyIdentifier& aKeyIdentifier,
   254     const TKeyIdentifier& aKeyIdentifier,
   266     TKeyUsagePKCS15& aKeyUsage )
   255     TKeyUsagePKCS15& aKeyUsage )
   267     {
   256     {
   268 
       
   269     TBool ret = EFalse;
   257     TBool ret = EFalse;
   270     TCTKeyAttributeFilter keyFilter;
   258     TCTKeyAttributeFilter keyFilter;
   271     keyFilter.iKeyAlgorithm = CKeyInfoBase::EInvalidAlgorithm;
   259     keyFilter.iKeyAlgorithm = CKeyInfoBase::EInvalidAlgorithm;
   272     keyFilter.iKeyId = aKeyIdentifier;
   260     keyFilter.iKeyId = aKeyIdentifier;
   273     keyFilter.iPolicyFilter = TCTKeyAttributeFilter::EManageableKeys;
   261     keyFilter.iPolicyFilter = TCTKeyAttributeFilter::EManageableKeys;
   282             break;
   270             break;
   283             }
   271             }
   284         case KErrCorrupt:
   272         case KErrCorrupt:
   285         case KErrEof:
   273         case KErrEof:
   286             {
   274             {
   287             ShowInformationNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   275             ShowErrorNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   288             User::Leave( KErrCancel );
   276             User::Leave( KErrCancel );
   289             }
   277             }
   290         default:
   278         default:
   291             {
   279             {
   292             User::Leave( err );
   280             User::Leave( err );
   314     TTime& aStartDate,
   302     TTime& aStartDate,
   315     TTime& aEndDate,
   303     TTime& aEndDate,
   316     const TKeyIdentifier& aKeyIdentifier )
   304     const TKeyIdentifier& aKeyIdentifier )
   317     {
   305     {
   318 
   306 
   319 
       
   320     for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
   307     for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
   321         {
   308         {
   322         const CX509Certificate* cert = iParser.UserCertificates().At( i );
   309         const CX509Certificate* cert = iParser.UserCertificates().At( i );
   323         if ( cert->KeyIdentifierL() == aKeyIdentifier )
   310         if ( cert->KeyIdentifierL() == aKeyIdentifier )
   324             {
   311             {
   341 //
   328 //
   342 // ----------------------------------------------------------
   329 // ----------------------------------------------------------
   343 //
   330 //
   344 void CCertSaverModel::CreateKeyLabelL( TDes& aLabel )
   331 void CCertSaverModel::CreateKeyLabelL( TDes& aLabel )
   345     {
   332     {
   346 
       
   347     TTime time;
   333     TTime time;
   348     time.UniversalTime();
   334     time.UniversalTime();
   349     TBuf<KPrivaKeyLabelLength> dateBuf;
   335     TBuf<KPrivaKeyLabelLength> dateBuf;
   350     time.FormatL( dateBuf, KDateString );
   336     time.FormatL( dateBuf, KDateString );
   351     aLabel.Append( dateBuf );
   337     aLabel.Append( dateBuf );
   361 //
   347 //
   362 // ----------------------------------------------------------
   348 // ----------------------------------------------------------
   363 //
   349 //
   364 void CCertSaverModel::SavePrivateKeyL()
   350 void CCertSaverModel::SavePrivateKeyL()
   365     {
   351     {
   366 
       
   367     if ( iParser.Keys().Count() <= 0 )
   352     if ( iParser.Keys().Count() <= 0 )
   368         {
   353         {
   369         return;
   354         return;
   370         }
   355         }
   371     if ( NULL == iWrapper )
   356     if ( NULL == iWrapper )
   409 //
   394 //
   410 TKeyUsagePKCS15 CCertSaverModel::KeyUsageL(
   395 TKeyUsagePKCS15 CCertSaverModel::KeyUsageL(
   411     const TKeyIdentifier& aKeyIdentifier,
   396     const TKeyIdentifier& aKeyIdentifier,
   412     TAlgorithmId aAlgorithm )
   397     TAlgorithmId aAlgorithm )
   413     {
   398     {
   414 
       
   415 
   399 
   416     TKeyUsagePKCS15 pkcs15KeyUsage = EPKCS15UsageNone;
   400     TKeyUsagePKCS15 pkcs15KeyUsage = EPKCS15UsageNone;
   417     TKeyUsageX509 x509Usage = EX509UsageNone;
   401     TKeyUsageX509 x509Usage = EX509UsageNone;
   418 
   402 
   419     for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
   403     for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
   499 //    const TDesC8& aDataToSave)
   483 //    const TDesC8& aDataToSave)
   500 // ----------------------------------------------------------
   484 // ----------------------------------------------------------
   501 //
   485 //
   502 void CCertSaverModel::CheckFSSpaceL( const TDesC8& aDataToSave )
   486 void CCertSaverModel::CheckFSSpaceL( const TDesC8& aDataToSave )
   503     {
   487     {
   504 
       
   505     if (SysUtil::FFSSpaceBelowCriticalLevelL( &iFs, aDataToSave.Size() ))
   488     if (SysUtil::FFSSpaceBelowCriticalLevelL( &iFs, aDataToSave.Size() ))
   506         {
   489         {
   507         ShowInformationNoteL(R_CERTSAVER_MEMORY);
   490         HBufC* p = StringLoader::LoadLC( R_CERTSAVER_MEMORY );
       
   491         CAknErrorNote* note = new (ELeave) CAknErrorNote( ETrue );
       
   492         note->ExecuteLD( p->Des() );
       
   493         CleanupStack::PopAndDestroy( p );
   508         User::Leave( KErrExitApp );
   494         User::Leave( KErrExitApp );
   509         }
   495         }
   510     }
   496     }
       
   497 
   511 // ----------------------------------------------------------
   498 // ----------------------------------------------------------
   512 // CCertSaverModel::SaveCertL()
   499 // CCertSaverModel::SaveCertL()
   513 // Saves certificate
   500 // Saves certificate
   514 // ----------------------------------------------------------
   501 // ----------------------------------------------------------
   515 //
   502 //
   516 void CCertSaverModel::SaveCertL()
   503 void CCertSaverModel::SaveCertL()
   517   {
   504   {
   518 
       
   519     if ( !CertificateOkL() )
   505     if ( !CertificateOkL() )
   520         {
   506         {
   521         User::Leave( KErrCancel );
   507         User::Leave( KErrCancel );
   522         }
   508         }
   523  
   509 
   524     HBufC* message = HBufC::NewLC( KMaxLengthTextMeassageBody );
   510     HBufC* message = HBufC::NewLC( KMaxLengthTextMeassageBody );
   525     TPtr msgPtr = message->Des();
   511     TPtr msgPtr = message->Des();
   526     ConstructMessageL( msgPtr );
   512     ConstructMessageL( msgPtr );
   527     CHbDeviceMessageBoxSymbian::TButtonId selection = 
   513     CAknMessageQueryDialog* dlg = CAknMessageQueryDialog::NewL( *message );
   528         CHbDeviceMessageBoxSymbian::QuestionL(msgPtr, KNullDesC, KNullDesC);
   514     CleanupStack::PopAndDestroy( message );
   529     TBool doSave= (selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
   515     CleanupStack::PushL( dlg );
   530     CleanupStack::PopAndDestroy(message);
   516 
   531 /*
   517 
       
   518     dlg->PrepareLC( R_MESSAGE_QUERY_DOSAVE );
       
   519 
       
   520     HBufC* header = StringLoader::LoadLC( R_CERTSAVER_DETAILS_HEADING );
       
   521     dlg->QueryHeading()->SetTextL( header->Des() );
       
   522     CleanupStack::PopAndDestroy( header );
       
   523     CleanupStack::Pop( dlg );
       
   524     TBool doSave = dlg->RunLD();
       
   525 
   532     if ( doSave && iCertOwnerType == ECACertificate )
   526     if ( doSave && iCertOwnerType == ECACertificate )
   533         {
   527         {
   534         // warn user about security risk
   528         // warn user about security risk
   535         HBufC* stringHolder = StringLoader::LoadLC( R_CERTSAVER_WARNING_NOTE );
   529         CAknQueryDialog* warningDialog = CAknQueryDialog::NewL();
   536         CHbDeviceMessageBoxSymbian::TButtonId selection = 
   530         doSave = warningDialog->ExecuteLD( R_CERTSAVER_WARNING_NOTE );
   537             CHbDeviceMessageBoxSymbian::QuestionL(stringHolder->Des(),KNullDesC, KNullDesC);
   531         }
   538         CleanupStack::PopAndDestroy(stringHolder);
   532 
   539 	    doSave=(selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
       
   540         }
       
   541 */
       
   542     if ( doSave )
   533     if ( doSave )
   543         {
   534         {
   544         //Check that there still is enough space to store the certificate.
   535         //Check that there still is enough space to store the
       
   536         //certificate.
   545         CheckFSSpaceL( iNewCert->Encoding() );
   537         CheckFSSpaceL( iNewCert->Encoding() );
   546         DoSaveCertL();
   538         DoSaveCertL();
   547         }
   539         }
   548     else
   540     else
   549         {
   541         {
   550         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
   542         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
   551         User::Leave( KErrCancel );
   543         User::Leave( KErrCancel );
   552         }
   544         }
   553   }
   545   }
   554 
   546 
   555 
       
   556 // ----------------------------------------------------------
   547 // ----------------------------------------------------------
   557 // CCertSaverModel::InitCertStoreL()
   548 // CCertSaverModel::InitCertStoreL()
   558 //
   549 //
   559 // ----------------------------------------------------------
   550 // ----------------------------------------------------------
   560 //
   551 //
   561 void CCertSaverModel::InitCertStoreL()
   552 void CCertSaverModel::InitCertStoreL()
   562     {
   553     {
   563 
       
   564     if ( !iUnifiedCertStore )
   554     if ( !iUnifiedCertStore )
   565         {
   555         {
   566         TRAPD( status, iUnifiedCertStore = CUnifiedCertStore::NewL( iFs, ETrue ) );
   556         TRAPD( status, iUnifiedCertStore = CUnifiedCertStore::NewL( iFs, ETrue ) );
   567         if ( status != KErrNone )
   557         if ( status != KErrNone )
   568             {
   558             {
   569             ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   559             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   570             User::Leave( KErrExitApp );
   560             User::Leave( KErrExitApp );
   571             }
   561             }
   572         // initialize unified cert store
   562         // initialize unified cert store
   573         status = iWrapper->InitializeCertStore( *iUnifiedCertStore );
   563         status = iWrapper->InitializeCertStore( *iUnifiedCertStore );
   574         if ( status )
   564         if ( status )
   584 // Tries to save the certificate to phone memory.
   574 // Tries to save the certificate to phone memory.
   585 // ----------------------------------------------------------
   575 // ----------------------------------------------------------
   586 //
   576 //
   587 void CCertSaverModel::DoSaveCertL()
   577 void CCertSaverModel::DoSaveCertL()
   588     {
   578     {
   589 
       
   590     TInt status = KErrNone;
   579     TInt status = KErrNone;
   591     CCertAttributeFilter* filter = NULL;
   580     CCertAttributeFilter* filter = NULL;
   592     TCertificateFormat certFormat = EX509Certificate;
   581     TCertificateFormat certFormat = EX509Certificate;
   593     HBufC* secondaryName = NULL;
   582     HBufC* secondaryName = NULL;
   594     TCertLabel labelBuf;
   583     TCertLabel labelBuf;
   687             MCTWritableCertStore& writableCertStore =
   676             MCTWritableCertStore& writableCertStore =
   688                     iUnifiedCertStore->WritableCertStore( i );
   677                     iUnifiedCertStore->WritableCertStore( i );
   689 
   678 
   690             MCTToken& token = writableCertStore.Token();
   679             MCTToken& token = writableCertStore.Token();
   691             TUid tokenuid = token.Handle().iTokenTypeUid;
   680             TUid tokenuid = token.Handle().iTokenTypeUid;
   692             if ( ( tokenuid == KTrustedServerTokenUid ) && 
   681             if ( ( tokenuid == KTrustedServerTokenUid ) && ( iCertOwnerType == EPeerCertificate ) ||
   693                  ( iCertOwnerType == EPeerCertificate ) ||
       
   694                  ( tokenuid == KFileTokensUid ) && ( iCertOwnerType == ECACertificate ) ||
   682                  ( tokenuid == KFileTokensUid ) && ( iCertOwnerType == ECACertificate ) ||
   695                  ( tokenuid == KFileTokensUid ) && ( iCertOwnerType == EUserCertificate ) )
   683                  ( tokenuid == KFileTokensUid ) && ( iCertOwnerType == EUserCertificate ) )
   696                 {
   684                 {
   697                 certstoreIndex = i;
   685                 certstoreIndex = i;
   698                 break;
   686                 break;
   700             }
   688             }
   701 
   689 
   702         if ( certstoreIndex < 0 )
   690         if ( certstoreIndex < 0 )
   703             {
   691             {
   704             // Couldn't find certificate storage
   692             // Couldn't find certificate storage
   705             ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   693             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   706             User::Leave( KErrExitApp );
   694             User::Leave( KErrExitApp );
   707             }
   695             }
   708 
   696 
   709         status = iWrapper->AddCert( iUnifiedCertStore->WritableCertStore( certstoreIndex ),
   697         status = iWrapper->AddCert( iUnifiedCertStore->WritableCertStore( certstoreIndex ),
   710                         labelBuf, iCertFormat, iCertOwnerType, 0, 0, iNewCert->Encoding() );
   698                         labelBuf, iCertFormat, iCertOwnerType, 0, 0, iNewCert->Encoding() );
   739         }
   727         }
   740     else
   728     else
   741         {
   729         {
   742         // If there is none WritableCertStore,
   730         // If there is none WritableCertStore,
   743         // then at least cacerts.dat is corrupted.
   731         // then at least cacerts.dat is corrupted.
   744         ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   732         ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   745         User::Leave( KErrExitApp );
   733         User::Leave( KErrExitApp );
   746         }
   734         }
   747 
   735 
   748     if ( ECACertificate == iCertOwnerType )
   736     if ( ECACertificate == iCertOwnerType )
   749         {
   737         {
   796 // already exist.
   784 // already exist.
   797 // ----------------------------------------------------------
   785 // ----------------------------------------------------------
   798 //
   786 //
   799 TInt CCertSaverModel::QueryLabelL( TCertLabel& aLabel, CUnifiedCertStore& aStore )
   787 TInt CCertSaverModel::QueryLabelL( TCertLabel& aLabel, CUnifiedCertStore& aStore )
   800     {
   788     {
   801 
       
   802     CCertAttributeFilter* filter = NULL;
   789     CCertAttributeFilter* filter = NULL;
   803     TInt status = KErrNone;
   790     TInt status = KErrNone;
   804     RMPointerArray<CCTCertInfo> entries;
   791     RMPointerArray<CCTCertInfo> entries;
   805     TBool loop = ETrue;
   792     TBool loop = ETrue;
   806 
   793 
   807     while ( loop )
   794     while ( loop )
   808         {
   795         {
   809     HBufC* labelprompt =  CEikonEnv::Static()->AllocReadResourceLC( R_CERTSAVER_ENTER_LABEL );
   796         CAknTextQueryDialog* dialog = CAknTextQueryDialog::NewL( aLabel );
   810     CSecQueryUi* SecQueryUi = CSecQueryUi::NewL();                                       
   797       if ( !dialog->ExecuteLD( R_CERTSAVER_LABEL_QUERY ) )
   811     TInt queryAccepted = SecQueryUi->SecQueryDialog(labelprompt->Des(), aLabel,
       
   812                                                 1,KMaxCertLabelLength,          
       
   813                                                 ESecUiAlphaSupported |          
       
   814                                                 ESecUiCancelSupported |         
       
   815                                                 ESecUiEmergencyNotSupported);   
       
   816     CleanupStack::PopAndDestroy( labelprompt );
       
   817     delete SecQueryUi;  
       
   818     SecQueryUi=NULL;
       
   819 
       
   820       if ( queryAccepted!=KErrNone )
       
   821             {
   798             {
   822             // cancel
   799             // cancel
   823             ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
   800             ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
   824             return KErrCancel;
   801             return KErrCancel;
   825             }
   802             }
   826         // Create filter to confirm that label doesn't already exist.
   803         // Create filter to confirm that label doesn't already exist.
   827         filter = CCertAttributeFilter::NewL();
   804         filter = CCertAttributeFilter::NewL();
   828         filter->SetLabel( aLabel );
   805         filter->SetLabel( aLabel );
   861 // of the save operation.
   838 // of the save operation.
   862 // ----------------------------------------------------------
   839 // ----------------------------------------------------------
   863 //
   840 //
   864 void CCertSaverModel::HandleSaveErrorL( TInt aStatus ) const
   841 void CCertSaverModel::HandleSaveErrorL( TInt aStatus ) const
   865     {
   842     {
   866 
       
   867     switch ( aStatus )
   843     switch ( aStatus )
   868         {
   844         {
   869         case KErrNone:
   845         case KErrNone:
   870             {
   846             {
   871             ShowInformationNoteL(R_CERTSAVER_ERROR_SAVEOK);
   847             ShowConfirmationNoteL( R_CERTSAVER_ERROR_SAVEOK );
   872             break;
   848             break;
   873             }
   849             }
   874         case KErrNotSupported:
   850         case KErrNotSupported:
   875             {
   851             {
   876             ShowInformationNoteL(R_CERTSAVER_ERROR_UNSUPPORTED_CERT);
   852             ShowErrorNoteL( R_CERTSAVER_ERROR_UNSUPPORTED_CERT );
   877             break;
   853             break;
   878             }
   854             }
   879         case KErrBadName:
   855         case KErrBadName:
   880             {
   856             {
   881             ShowInformationNoteL(R_CERTSAVER_ERROR_LABEL_ALREADY_EXISTS);
   857             ShowErrorNoteL( R_CERTSAVER_ERROR_LABEL_ALREADY_EXISTS );
   882             break;
   858             break;
   883             }
   859             }
   884         case KErrAlreadyExists:
   860         case KErrAlreadyExists:
   885             {
   861             {
   886             ShowInformationNoteL(R_CERTSAVER_ERROR_ALREADY_EXISTS);
   862             ShowErrorNoteL( R_CERTSAVER_ERROR_ALREADY_EXISTS );
   887             break;
   863             break;
   888             }
   864             }
   889         case KErrArgument:
   865         case KErrArgument:
   890             {
   866             {
   891             ShowInformationNoteL(R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED);
   867             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   892             break;
   868             break;
   893             }
   869             }
   894         default:
   870         default:
   895             {
   871             {
   896             //No error note defined for unknown error.
   872             //No error note defined for unknown error.
   906 // ----------------------------------------------------------
   882 // ----------------------------------------------------------
   907 //
   883 //
   908 void CCertSaverModel::AddToMessageWithStringL(
   884 void CCertSaverModel::AddToMessageWithStringL(
   909     TDes& aMessage, TInt aStringResID, const TDesC& aString ) const
   885     TDes& aMessage, TInt aStringResID, const TDesC& aString ) const
   910     {
   886     {
   911 
       
   912     HBufC* promptPtr = NULL;
   887     HBufC* promptPtr = NULL;
   913     promptPtr = StringLoader::LoadL( aStringResID, aString );
   888     promptPtr = StringLoader::LoadL( aStringResID, aString );
   914     CleanupStack::PushL( promptPtr );
   889     CleanupStack::PushL( promptPtr );
   915     TPtrC prompt( promptPtr->Des() );
   890     TPtrC prompt( promptPtr->Des() );
   916 
   891 
   927 void CCertSaverModel::AddToMessageWithIntL(
   902 void CCertSaverModel::AddToMessageWithIntL(
   928     TDes& aMessage,
   903     TDes& aMessage,
   929     TInt aStringResID,
   904     TInt aStringResID,
   930     TInt aInt ) const
   905     TInt aInt ) const
   931     {
   906     {
   932 
       
   933     HBufC* promptPtr = NULL;
   907     HBufC* promptPtr = NULL;
   934     promptPtr = StringLoader::LoadL( aStringResID, aInt );
   908     promptPtr = StringLoader::LoadL( aStringResID, aInt );
   935     CleanupStack::PushL( promptPtr );
   909     CleanupStack::PushL( promptPtr );
   936     TPtrC prompt( promptPtr->Des() );
   910     TPtrC prompt( promptPtr->Des() );
   937 
   911 
   946 // String loaded from resources with StringLoader.
   920 // String loaded from resources with StringLoader.
   947 // ----------------------------------------------------------
   921 // ----------------------------------------------------------
   948 //
   922 //
   949 void CCertSaverModel::AddToMessageL( TDes& aMessage, TInt aStringResID ) const
   923 void CCertSaverModel::AddToMessageL( TDes& aMessage, TInt aStringResID ) const
   950     {
   924     {
   951 
       
   952     HBufC* promptPtr = NULL;
   925     HBufC* promptPtr = NULL;
   953     promptPtr = StringLoader::LoadL( aStringResID );
   926     promptPtr = StringLoader::LoadL( aStringResID );
   954     CleanupStack::PushL( promptPtr );
   927     CleanupStack::PushL( promptPtr );
   955     TPtrC prompt( promptPtr->Des() );
   928     TPtrC prompt( promptPtr->Des() );
   956 
   929 
   965 // Creates the certificate details message shown to the user.
   938 // Creates the certificate details message shown to the user.
   966 // ----------------------------------------------------------
   939 // ----------------------------------------------------------
   967 //
   940 //
   968 void CCertSaverModel::ConstructMessageL( TDes& aMessage ) const
   941 void CCertSaverModel::ConstructMessageL( TDes& aMessage ) const
   969     {
   942     {
   970 
       
   971     HBufC16* issuerName = NULL;
   943     HBufC16* issuerName = NULL;
   972     HBufC16* subjectName = NULL;
   944     HBufC16* subjectName = NULL;
   973 
   945 
   974 
   946 
   975     X509CertNameParser::SubjectFullNameL( *((CX509Certificate*)iNewCert),
   947     X509CertNameParser::SubjectFullNameL( *((CX509Certificate*)iNewCert),
  1022 // CCTSecurityDialogsAO::AddKeyUsageL(...)
   994 // CCTSecurityDialogsAO::AddKeyUsageL(...)
  1023 // -----------------------------------------------------------------------------
   995 // -----------------------------------------------------------------------------
  1024 //
   996 //
  1025 void CCertSaverModel::AddKeyUsageL( TDes& aMessage, const CX509Certificate& aCert ) const
   997 void CCertSaverModel::AddKeyUsageL( TDes& aMessage, const CX509Certificate& aCert ) const
  1026     {
   998     {
  1027 
       
  1028     TKeyUsageX509 x509Usage = EX509UsageNone;
   999     TKeyUsageX509 x509Usage = EX509UsageNone;
  1029     TKeyUsagePKCS15 pkcs15KeyUsage = EPKCS15UsageNone;
  1000     TKeyUsagePKCS15 pkcs15KeyUsage = EPKCS15UsageNone;
  1030     const CX509CertExtension* ext = aCert.Extension( KKeyUsage );
  1001     const CX509CertExtension* ext = aCert.Extension( KKeyUsage );
  1031     if (ext)
  1002     if (ext)
  1032         {
  1003         {
  1106 // -----------------------------------------------------------------------------
  1077 // -----------------------------------------------------------------------------
  1107 //
  1078 //
  1108 void CCertSaverModel::AddValidityPeriodL(
  1079 void CCertSaverModel::AddValidityPeriodL(
  1109     TDes& aMessage, const CX509Certificate& aCert ) const
  1080     TDes& aMessage, const CX509Certificate& aCert ) const
  1110     {
  1081     {
  1111 
       
  1112     // Hometime's offset to UTC
  1082     // Hometime's offset to UTC
  1113     TLocale locale;
  1083     TLocale locale;
  1114     TTimeIntervalSeconds offSet = locale.UniversalTimeOffset();
  1084     TTimeIntervalSeconds offSet = locale.UniversalTimeOffset();
  1115     AddToMessageL( aMessage, R_CERTSAVER_QTN_CM_VALID_FROM );
  1085     AddToMessageL( aMessage, R_CERTSAVER_QTN_CM_VALID_FROM );
  1116 
  1086 
  1136     dateFormatString = iAppUi->CoeEnv()->AllocReadResourceLC(
  1106     dateFormatString = iAppUi->CoeEnv()->AllocReadResourceLC(
  1137         R_QTN_DATE_USUAL_WITH_ZERO );
  1107         R_QTN_DATE_USUAL_WITH_ZERO );
  1138     // format the date to user readable format. The format is locale dependent
  1108     // format the date to user readable format. The format is locale dependent
  1139     finishValue.FormatL( finishString, *dateFormatString );
  1109     finishValue.FormatL( finishString, *dateFormatString );
  1140     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( finishString );
  1110     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( finishString );
  1141     CleanupStack::PopAndDestroy(dateFormatString); // dateFormatString
  1111     CleanupStack::PopAndDestroy(); // dateFormatString
  1142     aMessage.Append( finishString );
  1112     aMessage.Append( finishString );
  1143     AddNewlinesToMessage( aMessage );
  1113     AddNewlinesToMessage( aMessage );
  1144     }
  1114     }
  1145 
  1115 
  1146 // ----------------------------------------------------------
  1116 // ----------------------------------------------------------
  1148 // Adds two new lines to message.
  1118 // Adds two new lines to message.
  1149 // ----------------------------------------------------------
  1119 // ----------------------------------------------------------
  1150 //
  1120 //
  1151 void CCertSaverModel::AddNewlinesToMessage( TDes& aMessage ) const
  1121 void CCertSaverModel::AddNewlinesToMessage( TDes& aMessage ) const
  1152     {
  1122     {
  1153 
       
  1154     aMessage.Append( KDoubleEnter );
  1123     aMessage.Append( KDoubleEnter );
  1155     }
  1124     }
  1156 
  1125 
  1157 // ----------------------------------------------------------
  1126 // ----------------------------------------------------------
  1158 // CCertSaverModel::CertificateSupported() const
  1127 // CCertSaverModel::CertificateSupported() const
  1159 // Checks that cert is of supported type.
  1128 // Checks that cert is of supported type.
  1160 // ----------------------------------------------------------
  1129 // ----------------------------------------------------------
  1161 //
  1130 //
  1162 TBool CCertSaverModel::CertificateSupported() const
  1131 TBool CCertSaverModel::CertificateSupported() const
  1163     {
  1132     {
  1164 
       
  1165     if ( iCertFormat == EX509Certificate &&
  1133     if ( iCertFormat == EX509Certificate &&
  1166        ( iCertOwnerType == ECACertificate ||
  1134        ( iCertOwnerType == ECACertificate ||
  1167          iCertOwnerType == EPeerCertificate ||
  1135          iCertOwnerType == EPeerCertificate ||
  1168          iCertOwnerType == EUserCertificate))
  1136          iCertOwnerType == EUserCertificate))
  1169         {
  1137         {
  1181 // certificate.
  1149 // certificate.
  1182 // ----------------------------------------------------------
  1150 // ----------------------------------------------------------
  1183 //
  1151 //
  1184 TBool CCertSaverModel::CertificateOkL() const
  1152 TBool CCertSaverModel::CertificateOkL() const
  1185     {
  1153     {
  1186 
  1154     if ( !CertificateSupported() )
  1187     if ( !CertificateSupported() ) 
  1155         {
  1188         {
  1156         ShowErrorNoteL( R_CERTSAVER_ERROR_UNSUPPORTED_CERT );
  1189         ShowInformationNoteL(R_CERTSAVER_ERROR_UNSUPPORTED_CERT);
       
  1190         return EFalse;
  1157         return EFalse;
  1191         }
  1158         }
  1192     if ( CertNotValidAnymore() )
  1159     if ( CertNotValidAnymore() )
  1193         {
  1160         {
  1194         ShowInformationNoteL(R_CERTSAVER_ERROR_CERT_NOT_VALID);
  1161         ShowErrorNoteL( R_CERTSAVER_ERROR_CERT_NOT_VALID );
  1195         return ETrue;
  1162         return ETrue;
  1196         }
  1163         }
  1197     else if ( CertNotValidYet() )
  1164     else if ( CertNotValidYet() )
  1198         {
  1165         {
  1199         ShowInformationNoteL(R_CERTSAVER_ERROR_CERT_NOT_VALID_YET);
  1166         ShowErrorNoteL( R_CERTSAVER_ERROR_CERT_NOT_VALID_YET );
  1200         }
  1167         }
  1201     return ETrue;
  1168     return ETrue;
  1202     }
  1169     }
  1203 
  1170 
  1204 // ----------------------------------------------------------
  1171 // ----------------------------------------------------------
  1206 // Checks if cert isn't valid anymore.
  1173 // Checks if cert isn't valid anymore.
  1207 // ----------------------------------------------------------
  1174 // ----------------------------------------------------------
  1208 //
  1175 //
  1209 TBool CCertSaverModel::CertNotValidAnymore() const
  1176 TBool CCertSaverModel::CertNotValidAnymore() const
  1210     {
  1177     {
  1211 
       
  1212     TTime homeTime;
  1178     TTime homeTime;
  1213     homeTime.HomeTime();
  1179     homeTime.HomeTime();
  1214     if ( iNewCert->ValidityPeriod().Finish() < homeTime )
  1180     if ( iNewCert->ValidityPeriod().Finish() < homeTime )
  1215         {
  1181         {
  1216         return ETrue;
  1182         return ETrue;
  1223 // Checks if cert isn't valid yet.
  1189 // Checks if cert isn't valid yet.
  1224 // ----------------------------------------------------------
  1190 // ----------------------------------------------------------
  1225 //
  1191 //
  1226 TBool CCertSaverModel::CertNotValidYet() const
  1192 TBool CCertSaverModel::CertNotValidYet() const
  1227     {
  1193     {
  1228 
       
  1229     TTime homeTime;
  1194     TTime homeTime;
  1230     homeTime.HomeTime();
  1195     homeTime.HomeTime();
  1231     if ( iNewCert->ValidityPeriod().Start() > homeTime )
  1196     if ( iNewCert->ValidityPeriod().Start() > homeTime )
  1232         {
  1197         {
  1233         return ETrue;
  1198         return ETrue;
  1240 // Creates and shows a confirmation note.
  1205 // Creates and shows a confirmation note.
  1241 // ----------------------------------------------------------
  1206 // ----------------------------------------------------------
  1242 //
  1207 //
  1243 void CCertSaverModel::ShowInformationNoteL( TInt aResourceID ) const
  1208 void CCertSaverModel::ShowInformationNoteL( TInt aResourceID ) const
  1244     {
  1209     {
  1245 
       
  1246     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
  1210     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
  1247     CHbDeviceMessageBoxSymbian* iMessageBox = CHbDeviceMessageBoxSymbian::NewL(CHbDeviceMessageBoxSymbian::EInformation);
  1211     CAknInformationNote* note = new (ELeave) CAknInformationNote( ETrue );
  1248     CleanupStack::PushL(iMessageBox);                                                                                    
  1212     note->ExecuteLD( buffer->Des() );
  1249     iMessageBox->SetTextL(buffer->Des());                                                                                
  1213     CleanupStack::PopAndDestroy( buffer );
  1250     iMessageBox->SetTimeout(6000);                                                                                      
  1214     }
  1251     iMessageBox->ExecL();                                                                                                
  1215 // ----------------------------------------------------------
  1252     CleanupStack::PopAndDestroy(iMessageBox);                                                                            
  1216 // CCertSaverModel::ShowConfirmationNoteL() const
  1253     CleanupStack::PopAndDestroy( buffer );      
  1217 // Creates and shows a confirmation note.
       
  1218 // ----------------------------------------------------------
       
  1219 //
       
  1220 void CCertSaverModel::ShowConfirmationNoteL( TInt aResourceID ) const
       
  1221     {
       
  1222     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
       
  1223     CAknConfirmationNote* note = new (ELeave) CAknConfirmationNote( ETrue );
       
  1224     note->ExecuteLD( buffer->Des() );
       
  1225     CleanupStack::PopAndDestroy( buffer );
       
  1226     }
       
  1227 
       
  1228 // ----------------------------------------------------------
       
  1229 // CCertSaverModel::ShowErrorNoteL() const
       
  1230 // Creates and shows an error note.
       
  1231 // ----------------------------------------------------------
       
  1232 //
       
  1233 void CCertSaverModel::ShowErrorNoteL( TInt aResourceID ) const
       
  1234     {
       
  1235     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
       
  1236     CAknErrorNote* note = new (ELeave) CAknErrorNote( ETrue );
       
  1237     note->ExecuteLD(buffer->Des());
       
  1238     CleanupStack::PopAndDestroy( buffer );
  1254     }
  1239     }
  1255 
  1240 
  1256 // ----------------------------------------------------------
  1241 // ----------------------------------------------------------
  1257 // CCertSaverModel::TrimCertificateFields() const
  1242 // CCertSaverModel::TrimCertificateFields() const
  1258 // Trims given descriptor so that everything after and
  1243 // Trims given descriptor so that everything after and
  1260 // Returns the trimmed certificate field.
  1245 // Returns the trimmed certificate field.
  1261 // ----------------------------------------------------------
  1246 // ----------------------------------------------------------
  1262 //
  1247 //
  1263 TPtrC CCertSaverModel::TrimCertificateFields( TPtrC aField ) const
  1248 TPtrC CCertSaverModel::TrimCertificateFields( TPtrC aField ) const
  1264     {
  1249     {
  1265 
       
  1266     TPtrC cutField = CutCertificateField( aField );
  1250     TPtrC cutField = CutCertificateField( aField );
  1267     // Find one semicolon at a time and crop the
  1251     // Find one semicolon at a time and crop the
  1268     // helpField from the left to search for the next semicolon
  1252     // helpField from the left to search for the next semicolon
  1269     TInt position = cutField.Locate( ';' ); // 1st semicolon
  1253     TInt position = cutField.Locate( ';' ); // 1st semicolon
  1270     TInt fieldLength = cutField.Length();
  1254     TInt fieldLength = cutField.Length();
  1312 // this function cuts it.
  1296 // this function cuts it.
  1313 // ----------------------------------------------------------
  1297 // ----------------------------------------------------------
  1314 //
  1298 //
  1315 TPtrC CCertSaverModel::CutCertificateField( TPtrC aField ) const
  1299 TPtrC CCertSaverModel::CutCertificateField( TPtrC aField ) const
  1316     {
  1300     {
  1317 
       
  1318     TInt fieldLength = aField.Length();
  1301     TInt fieldLength = aField.Length();
  1319     if ( fieldLength >= KMaxLengthTextCertIdentifierVisible )
  1302     if ( fieldLength >= KMaxLengthTextCertIdentifierVisible )
  1320         {
  1303         {
  1321         TPtrC cutCertLabel = aField.Mid( 0, KMaxLengthTextCertIdentifierVisible );
  1304         TPtrC cutCertLabel = aField.Mid( 0, KMaxLengthTextCertIdentifierVisible );
  1322         return cutCertLabel;
  1305         return cutCertLabel;
  1329 // Does needed tasks to exit.
  1312 // Does needed tasks to exit.
  1330 // ----------------------------------------------------------
  1313 // ----------------------------------------------------------
  1331 //
  1314 //
  1332 TInt CCertSaverModel::QueryTrusterUidsL( RArray<TUid>& aUids )
  1315 TInt CCertSaverModel::QueryTrusterUidsL( RArray<TUid>& aUids )
  1333     {
  1316     {
  1334 
       
  1335     TInt ret = KErrCancel;
  1317     TInt ret = KErrCancel;
       
  1318     CArrayFixFlat<TInt>* selectionArray =
       
  1319         new (ELeave)  CArrayFixFlat<TInt>( KTrusterArrayInitSize );
       
  1320     CleanupStack::PushL( selectionArray );
  1336     CDesCArray* itemsArray = new (ELeave) CDesCArrayFlat( KTrusterArrayInitSize );
  1321     CDesCArray* itemsArray = new (ELeave) CDesCArrayFlat( KTrusterArrayInitSize );
  1337     CleanupStack::PushL( itemsArray );
  1322     CleanupStack::PushL( itemsArray );
  1338 
  1323 
  1339     CCertificateAppInfoManager* appInfoManager =
  1324     CCertificateAppInfoManager* appInfoManager =
  1340         CCertificateAppInfoManager::NewL( iFs, EFalse );
  1325         CCertificateAppInfoManager::NewL( iFs, EFalse );
  1343     const RArray<TCertificateAppInfo>& apps = appInfoManager->Applications();
  1328     const RArray<TCertificateAppInfo>& apps = appInfoManager->Applications();
  1344     RArray<TCertificateAppInfo> appsInItemArray( KTrusterArrayInitSize );
  1329     RArray<TCertificateAppInfo> appsInItemArray( KTrusterArrayInitSize );
  1345     CleanupClosePushL( appsInItemArray );
  1330     CleanupClosePushL( appsInItemArray );
  1346     UpdateTrustListboxItemL( apps, appsInItemArray, *itemsArray );
  1331     UpdateTrustListboxItemL( apps, appsInItemArray, *itemsArray );
  1347 
  1332 
  1348     HBufC* title = CEikonEnv::Static()->AllocReadResourceLC( R_CERTSAVER_SELECT_TRUSTED_APPS);
  1333     CAknListQueryDialog* dlg = new (ELeave) CAknListQueryDialog( selectionArray );
  1349 
  1334     CleanupStack::PushL( dlg );
  1350     RBuf rBuf;     // buffer for items with big enough space
  1335     dlg->PrepareLC( R_CERTSAVER_TRUST_SETTINGS_QUERY );
  1351     rBuf.CreateL(KMaxLengthTextMeassageBody);
  1336     dlg->SetItemTextArray( itemsArray );
  1352     CleanupClosePushL(rBuf);
  1337     dlg->SetOwnershipType( ELbmDoesNotOwnItemArray );
  1353     for(TInt i = 0; i<itemsArray->Count(); i++) 
  1338     if ( dlg->RunLD() )
  1354         rBuf.Append( (*itemsArray)[i] ); 
  1339         {
  1355 
  1340         aUids.Reset();
  1356     CSecQueryUi* SecQueryUi = CSecQueryUi::NewL();     
  1341         for ( TInt i = 0; i < selectionArray->Count(); ++i )
  1357     TInt saved = SecQueryUi->SecQueryDialog(*title, rBuf,1,KTokenLength,              
  1342             {
  1358                                              ESecUiAlphaSupported | ESecUiCancelSupported |
  1343             TInt ii = (*selectionArray)[ i ];
  1359                                              ESecUiBasicTypeMultiCheck |
  1344             aUids.Append( appsInItemArray[ ii ].Id() );
  1360                                              ESecUiEmergencyNotSupported);   
  1345             }
  1361     delete SecQueryUi;
       
  1362     SecQueryUi=NULL;
       
  1363 
       
  1364     if (saved==KErrNone)
       
  1365         {
       
  1366         TLex16 lex(rBuf);
       
  1367         TChar ch;                                        
       
  1368         TBuf16<KTokenLength> token;          
       
  1369         TInt  val;
       
  1370         while((ch = lex.Get()) != 0 ){                   
       
  1371            while ((ch = lex.Peek()) != '|' && ch!=0) lex.Inc();
       
  1372            token.Copy(lex.MarkedToken());                
       
  1373            TLex lexc(token);
       
  1374            if(lexc.Val(val)!=KErrNone) val=0;
       
  1375            if(val<=appsInItemArray.Count()-1)
       
  1376                aUids.Append( appsInItemArray[ val ].Id() );
       
  1377            lex.Inc();                                    
       
  1378            lex.Mark();                                   
       
  1379          }  
       
  1380         ret = KErrNone;
  1346         ret = KErrNone;
  1381         }
  1347         }
  1382     else
  1348     else
  1383         {
  1349         {
  1384         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
  1350         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
  1385         ret = KErrCancel;
  1351         ret = KErrCancel;
  1386         }
  1352         }
  1387 
  1353     CleanupStack::Pop( dlg );
  1388     rBuf.Close();
  1354     CleanupStack::PopAndDestroy( 4, selectionArray );
  1389     CleanupStack::PopAndDestroy(&rBuf);
       
  1390     CleanupStack::PopAndDestroy(title);
       
  1391     CleanupStack::PopAndDestroy(&appsInItemArray);
       
  1392     CleanupStack::PopAndDestroy(appInfoManager);
       
  1393     CleanupStack::PopAndDestroy(itemsArray);
       
  1394     return ret;
  1355     return ret;
  1395     }
  1356     }
  1396 
  1357 
  1397 // ----------------------------------------------------------
  1358 // ----------------------------------------------------------
  1398 // CCertSaverModel::QueryTrustedSiteL()
  1359 // CCertSaverModel::QueryTrustedSiteL()
  1399 // Shows query for trusted site certificate
  1360 // Shows query for trusted site certificate
  1400 // ----------------------------------------------------------
  1361 // ----------------------------------------------------------
  1401 //
  1362 //
  1402 TInt CCertSaverModel::QueryTrustedSiteL()
  1363 TInt CCertSaverModel::QueryTrustedSiteL()
  1403     {
  1364     {
  1404 
       
  1405     TInt ret = KErrCancel;
  1365     TInt ret = KErrCancel;
  1406     HBufC* label = NULL;
  1366     HBufC* label = NULL;
  1407     HBufC* secondaryName = NULL;
  1367     HBufC* secondaryName = NULL;
  1408     TCertLabel labelBuf;
  1368     TCertLabel labelBuf;
  1409 
  1369 
  1411         (*(CX509Certificate*)iNewCert), label, secondaryName );
  1371         (*(CX509Certificate*)iNewCert), label, secondaryName );
  1412     CleanupStack::PushL( label );
  1372     CleanupStack::PushL( label );
  1413     delete secondaryName;
  1373     delete secondaryName;
  1414     secondaryName = NULL;
  1374     secondaryName = NULL;
  1415     labelBuf = label->Des().Left( CERTSAVER_MAX_LABEL_LEN );
  1375     labelBuf = label->Des().Left( CERTSAVER_MAX_LABEL_LEN );
       
  1376 
       
  1377 
  1416     HBufC* prompt = StringLoader::LoadLC( R_CERTSAVER_TRUSTEDSITE_WARNING, labelBuf );
  1378     HBufC* prompt = StringLoader::LoadLC( R_CERTSAVER_TRUSTEDSITE_WARNING, labelBuf );
  1417     CHbDeviceMessageBoxSymbian::TButtonId selection =
  1379 
  1418         CHbDeviceMessageBoxSymbian::QuestionL(prompt->Des(), KNullDesC, KNullDesC);
  1380     CAknMessageQueryDialog* note = CAknMessageQueryDialog::NewL( *prompt );
  1419     if ( selection == CHbDeviceMessageBoxSymbian::EAcceptButton)
  1381 
       
  1382     note->PrepareLC( R_CERTSAVER_TRUSTED_SITE_QUERY );
       
  1383     note->SetPromptL( *prompt );
       
  1384 
       
  1385     if ( note->RunLD() )
  1420         {
  1386         {
  1421         ret = KErrNone;
  1387         ret = KErrNone;
  1422         }
  1388         }
  1423     else
  1389     else
  1424         {
  1390         {
  1425         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
  1391         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
  1426         ret = KErrCancel;
  1392         ret = KErrCancel;
  1427         }
  1393         }
       
  1394 
  1428     CleanupStack::PopAndDestroy( prompt );
  1395     CleanupStack::PopAndDestroy( prompt );
       
  1396 
  1429     CleanupStack::PopAndDestroy( label );
  1397     CleanupStack::PopAndDestroy( label );
  1430     return ret;
  1398     return ret;
  1431     }
  1399     }
  1432 
  1400 
  1433 // ----------------------------------------------------------
  1401 // ----------------------------------------------------------
  1438 void CCertSaverModel::UpdateTrustListboxItemL(
  1406 void CCertSaverModel::UpdateTrustListboxItemL(
  1439     const RArray<TCertificateAppInfo>& aApps,
  1407     const RArray<TCertificateAppInfo>& aApps,
  1440     RArray<TCertificateAppInfo>& aAppsInItemArray,
  1408     RArray<TCertificateAppInfo>& aAppsInItemArray,
  1441     CDesCArray& aItemsArray ) const
  1409     CDesCArray& aItemsArray ) const
  1442   {
  1410   {
  1443 
       
  1444     for ( TInt i = 0; i < aApps.Count(); i++ )
  1411     for ( TInt i = 0; i < aApps.Count(); i++ )
  1445         {
  1412         {
  1446         TCertificateAppInfo appInfo = aApps[ i ];
  1413         TCertificateAppInfo appInfo = aApps[ i ];
  1447         TBuf<KItemBufLen> item;
  1414         TBuf<KItemBufLen> item;
  1448         item.Append( KCertSaverListBoxItemPrefix );
  1415         item.Append( KCertSaverListBoxItemPrefix );
  1501 // CCertManUIViewAuthority::DevideToBlocks
  1468 // CCertManUIViewAuthority::DevideToBlocks
  1502 // ---------------------------------------------------------
  1469 // ---------------------------------------------------------
  1503 //
  1470 //
  1504 void CCertSaverModel::DivideToBlocks( const TDesC8& aInput, TDes& aOutput ) const
  1471 void CCertSaverModel::DivideToBlocks( const TDesC8& aInput, TDes& aOutput ) const
  1505     {
  1472     {
  1506 
       
  1507     _LIT( KBlockSeparator, " " );
  1473     _LIT( KBlockSeparator, " " );
  1508     const TInt KBlockLength = 2;
  1474     const TInt KBlockLength = 2;
  1509     TInt blockIndex = 0;
  1475     TInt blockIndex = 0;
  1510     for ( TInt j = 0 ; j < aInput.Length() ; j++ )
  1476     for ( TInt j = 0 ; j < aInput.Length() ; j++ )
  1511         {
  1477         {
  1524 // Saves content of the PKCS#12 file
  1490 // Saves content of the PKCS#12 file
  1525 // ----------------------------------------------------
  1491 // ----------------------------------------------------
  1526 //
  1492 //
  1527 void CCertSaverModel::SavePKCS12L()
  1493 void CCertSaverModel::SavePKCS12L()
  1528     {
  1494     {
  1529 
       
  1530     HBufC* message = HBufC::NewLC( KMaxLengthTextMeassageBody );
  1495     HBufC* message = HBufC::NewLC( KMaxLengthTextMeassageBody );
  1531     TPtr msgPtr = message->Des();
  1496     TPtr msgPtr = message->Des();
  1532     ConstructPKCS12QueryMsgL(msgPtr, iParser.Keys().Count(),
  1497     ConstructPKCS12QueryMsgL(msgPtr, iParser.Keys().Count(),
  1533                             iParser.UserCertificates().Count(),
  1498                             iParser.UserCertificates().Count(),
  1534                             iParser.CACertificates().Count() );
  1499                             iParser.CACertificates().Count() );
  1535 
  1500     TBool save = DoMessageQueryL(
  1536     //TODO: Should be in loc file but hardcoded now
  1501         R_MESSAGE_QUERY_DOSAVE, R_CERTSAVER_HEADER_PKCS12_FILE_CONTAINS,
  1537     _LIT(KCheckBoxCaption,"Protect with Password|1");
  1502         *message );
  1538     TBuf<KMaxLengthTextCheckBoxData> CheckeBoxData(KCheckBoxCaption);
  1503     CleanupStack::PopAndDestroy( message );
  1539 
       
  1540     CSecQueryUi* SecQueryUi = CSecQueryUi::NewL();    
       
  1541     TInt save = SecQueryUi->SecQueryDialog(*message, CheckeBoxData,1,1,
       
  1542                                             ESecUiAlphaSupported | ESecUiCancelSupported |
       
  1543                                             ESecUiBasicTypeCheck | ESecUiEmergencyNotSupported);
       
  1544     CleanupStack::Pop(message);
       
  1545     message = NULL;
  1504     message = NULL;
  1546     delete SecQueryUi;
  1505     if ( !save )
  1547     SecQueryUi=NULL;
       
  1548 
       
  1549     if ( save!=KErrNone )
       
  1550         {
  1506         {
  1551         ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
  1507         ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
  1552         User::Leave( KErrExitApp );
  1508         User::Leave( KErrExitApp );
  1553         }
  1509         }
  1554     TInt status = KErrNone;
  1510     TInt status = KErrNone;
  1555     // save private keys
  1511     // save private keys
  1556    if(CheckeBoxData.Compare(_L("1"))==0)
  1512     TRAP( status, SavePrivateKeyL() );
  1557    {
  1513 
  1558        TRAP( status, SavePrivateKeyL() );
       
  1559    }
       
  1560     // save user certificates if private key was saved.
  1514     // save user certificates if private key was saved.
  1561     if ( ( iSavedKeysCount > 0 || iKeyAlreadyExists ) && iParser.UserCertificates().Count() > 0 )
  1515     if ( ( iSavedKeysCount > 0 || iKeyAlreadyExists ) && iParser.UserCertificates().Count() > 0 )
  1562         {
  1516         {
  1563         for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
  1517         for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
  1564             {
  1518             {
  1587                 {
  1541                 {
  1588                 User::Leave( KErrExitApp );
  1542                 User::Leave( KErrExitApp );
  1589                 }
  1543                 }
  1590             }
  1544             }
  1591         }
  1545         }
  1592 /*
       
  1593     if ( iSavedCACertsCount != 0 || iSavedKeysCount != 0
  1546     if ( iSavedCACertsCount != 0 || iSavedKeysCount != 0
  1594         || iSavedUserCertsCount != 0 )
  1547         || iSavedUserCertsCount != 0 )
  1595         {
  1548         {
  1596 // show how many have been saved
  1549         message = HBufC::NewLC( KMaxLengthTextMeassageBody );
  1597         HBufC* p = StringLoader::LoadLC( R_CERTSAVER_HEADER_SAVED );             
  1550         TPtr msgPtr2 = message->Des();
  1598         message = HBufC::NewLC( KMaxLengthTextMeassageBody );                    
  1551         ConstructPKCS12QueryMsgL(
  1599         message->Des().Append(p->Des());                                         
       
  1600         TPtr msgPtr2 = message->Des();                                           
       
  1601         ConstructPKCS12QueryMsgL(                                                
       
  1602             msgPtr2, iSavedKeysCount, iSavedUserCertsCount, iSavedCACertsCount );
  1552             msgPtr2, iSavedKeysCount, iSavedUserCertsCount, iSavedCACertsCount );
  1603         CHbDeviceMessageBoxSymbian::InformationL(message->Des());                
  1553         DoMessageQueryL(
  1604         CleanupStack::PopAndDestroy( message );                                  
  1554             R_MESSAGE_QUERY_SAVED, R_CERTSAVER_HEADER_SAVED, *message );
  1605         CleanupStack::PopAndDestroy( p );                                        
  1555         CleanupStack::PopAndDestroy( message );
  1606         }
  1556         }
  1607     else
  1557     else
  1608         {
  1558         {
  1609         // the contents could not be saved is dropped
       
  1610         ShowInformationNoteL( R_QTN_CM_PKCS12_SAVING_FAILED );
  1559         ShowInformationNoteL( R_QTN_CM_PKCS12_SAVING_FAILED );
  1611         CleanupStack::PopAndDestroy( message );
  1560         }
  1612         }
  1561     }
  1613 */
  1562 
       
  1563 // ----------------------------------------------------------
       
  1564 // CCertSaverModel::DoMessageQuery()
       
  1565 // Displays message query dialog for user.
       
  1566 // ----------------------------------------------------------
       
  1567 //
       
  1568 TBool CCertSaverModel::DoMessageQueryL(
       
  1569     TInt aDialogResId,
       
  1570     TInt aHeadingResId,
       
  1571     TDesC& aMessage )
       
  1572     {
       
  1573     CAknMessageQueryDialog* dlg = CAknMessageQueryDialog::NewL( aMessage );
       
  1574     CleanupStack::PushL( dlg );
       
  1575     dlg->PrepareLC( aDialogResId );
       
  1576 
       
  1577     HBufC* header = StringLoader::LoadLC( aHeadingResId );
       
  1578     dlg->QueryHeading()->SetTextL( header->Des() );
       
  1579     CleanupStack::PopAndDestroy( header );
       
  1580     CleanupStack::Pop( dlg );
       
  1581     return dlg->RunLD();
  1614     }
  1582     }
  1615 
  1583 
  1616 // ----------------------------------------------------------
  1584 // ----------------------------------------------------------
  1617 // CCertSaverModel::ConstructPKCS12QueryMsgL() const
  1585 // CCertSaverModel::ConstructPKCS12QueryMsgL() const
  1618 // Creates the certificate details message shown to the user.
  1586 // Creates the certificate details message shown to the user.
  1622     TDes& aMessage,
  1590     TDes& aMessage,
  1623     TInt aPrivateKeys,
  1591     TInt aPrivateKeys,
  1624     TInt aUserCerts,
  1592     TInt aUserCerts,
  1625     TInt aCACerts ) const
  1593     TInt aCACerts ) const
  1626     {
  1594     {
  1627 
       
  1628     if ( aPrivateKeys > 0 )
  1595     if ( aPrivateKeys > 0 )
  1629         {
  1596         {
  1630         if ( aPrivateKeys == 1 )
  1597         if ( aPrivateKeys == 1 )
  1631             {
  1598             {
  1632             AddToMessageL( aMessage, R_CERTSAVER_ONE_PRIVATE_KEY );
  1599             AddToMessageL( aMessage, R_CERTSAVER_ONE_PRIVATE_KEY );