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