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