pkiutilities/CertSaver/src/CertSaverModel.cpp
changeset 38 e0432375ea67
parent 37 7bad16cccaca
child 39 61986511a9c8
equal deleted inserted replaced
37:7bad16cccaca 38:e0432375ea67
   208             break;
   208             break;
   209             }
   209             }
   210         case KErrKeySize:
   210         case KErrKeySize:
   211         case KErrArgument:
   211         case KErrArgument:
   212             {
   212             {
   213             ShowErrorNoteL( R_CERTSAVER_KEY_TYPE_NOT_SUPPORTED );
   213             ShowInformationNoteL( R_CERTSAVER_KEY_TYPE_NOT_SUPPORTED );
   214             User::Leave( KErrCancel );
   214             User::Leave( KErrCancel );
   215             break;
   215             break;
   216             }
   216             }
   217         case KErrKeyValidity:
   217         case KErrKeyValidity:
   218             {
   218             {
   219             ShowInformationNoteL( R_CERTSAVER_QTN_CM_PKCS12_EXPIRED );
   219             ShowInformationNoteL( R_CERTSAVER_QTN_CM_PKCS12_EXPIRED );
   220             User::Leave( KErrCancel );
   220             User::Leave( KErrCancel );
   221             }
   221             }
   222         case KErrKeyUsage:
   222         case KErrKeyUsage:
   223             {
   223             {
   224             ShowErrorNoteL( R_CERTSAVER_PRIVATE_KEY_CORRUPTED );
   224             ShowInformationNoteL( R_CERTSAVER_PRIVATE_KEY_CORRUPTED );
   225             User::Leave( KErrCancel );
   225             User::Leave( KErrCancel );
   226             break;
   226             break;
   227             }
   227             }
   228         case KErrCancel:
   228         case KErrCancel:
   229         case KErrPermissionDenied:
   229         case KErrPermissionDenied:
   230             {
   230             {
   231             ShowErrorNoteL( R_CERTSAVER_PKCS12_DISCARDED );
   231             ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
   232             User::Leave( KErrCancel );
   232             User::Leave( KErrCancel );
   233             break;
   233             break;
   234             }
   234             }
   235         case KErrCorrupt:
   235         case KErrCorrupt:
   236         case KErrEof:
   236         case KErrEof:
   237             {
   237             {
   238             ShowErrorNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   238             ShowInformationNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   239             User::Leave( KErrCancel );
   239             User::Leave( KErrCancel );
   240             break;
   240             break;
   241             }
   241             }
   242         case KErrAlreadyExists:
   242         case KErrAlreadyExists:
   243             {
   243             {
   285             break;
   285             break;
   286             }
   286             }
   287         case KErrCorrupt:
   287         case KErrCorrupt:
   288         case KErrEof:
   288         case KErrEof:
   289             {
   289             {
   290             ShowErrorNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   290             ShowInformationNoteL( R_CERTSAVER_KEYSTORE_CORRUPTED );
   291             User::Leave( KErrCancel );
   291             User::Leave( KErrCancel );
   292             }
   292             }
   293         default:
   293         default:
   294             {
   294             {
   295             User::Leave( err );
   295             User::Leave( err );
   505 void CCertSaverModel::CheckFSSpaceL( const TDesC8& aDataToSave )
   505 void CCertSaverModel::CheckFSSpaceL( const TDesC8& aDataToSave )
   506     {
   506     {
   507 
   507 
   508     if (SysUtil::FFSSpaceBelowCriticalLevelL( &iFs, aDataToSave.Size() ))
   508     if (SysUtil::FFSSpaceBelowCriticalLevelL( &iFs, aDataToSave.Size() ))
   509         {
   509         {
   510         HBufC* p = StringLoader::LoadLC( R_CERTSAVER_MEMORY );
   510         ShowInformationNoteL(R_CERTSAVER_MEMORY);
   511         CHbDeviceMessageBoxSymbian::WarningL(p->Des()); 
       
   512         CleanupStack::PopAndDestroy( p );
       
   513         User::Leave( KErrExitApp );
   511         User::Leave( KErrExitApp );
   514         }
   512         }
   515     }
   513     }
   516 // ----------------------------------------------------------
   514 // ----------------------------------------------------------
   517 // CCertSaverModel::SaveCertL()
   515 // CCertSaverModel::SaveCertL()
   531     ConstructMessageL( msgPtr );
   529     ConstructMessageL( msgPtr );
   532     CHbDeviceMessageBoxSymbian::TButtonId selection = 
   530     CHbDeviceMessageBoxSymbian::TButtonId selection = 
   533         CHbDeviceMessageBoxSymbian::QuestionL(msgPtr, KNullDesC, KNullDesC);
   531         CHbDeviceMessageBoxSymbian::QuestionL(msgPtr, KNullDesC, KNullDesC);
   534     TBool doSave= (selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
   532     TBool doSave= (selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
   535     CleanupStack::PopAndDestroy(message);
   533     CleanupStack::PopAndDestroy(message);
   536 
   534 /*
   537     if ( doSave && iCertOwnerType == ECACertificate )
   535     if ( doSave && iCertOwnerType == ECACertificate )
   538         {
   536         {
   539         // warn user about security risk
   537         // warn user about security risk
   540         HBufC* stringHolder = StringLoader::LoadLC( R_CERTSAVER_WARNING_NOTE );
   538         HBufC* stringHolder = StringLoader::LoadLC( R_CERTSAVER_WARNING_NOTE );
   541         CHbDeviceMessageBoxSymbian::TButtonId selection = 
   539         CHbDeviceMessageBoxSymbian::TButtonId selection = 
   542             CHbDeviceMessageBoxSymbian::QuestionL(stringHolder->Des(),KNullDesC, KNullDesC);
   540             CHbDeviceMessageBoxSymbian::QuestionL(stringHolder->Des(),KNullDesC, KNullDesC);
   543         CleanupStack::PopAndDestroy(stringHolder);
   541         CleanupStack::PopAndDestroy(stringHolder);
   544 	    doSave=(selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
   542 	    doSave=(selection == CHbDeviceMessageBoxSymbian::EAcceptButton);
   545         }
   543         }
   546 
   544 */
   547     if ( doSave )
   545     if ( doSave )
   548         {
   546         {
   549         //Check that there still is enough space to store the certificate.
   547         //Check that there still is enough space to store the certificate.
   550         CheckFSSpaceL( iNewCert->Encoding() );
   548         CheckFSSpaceL( iNewCert->Encoding() );
   551         DoSaveCertL();
   549         DoSaveCertL();
   552         }
   550         }
   553     else
   551     else
   554         {
   552         {
   555         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
   553         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
   556         User::Leave( KErrCancel );
   554         User::Leave( KErrCancel );
   557         }
   555         }
   558   }
   556   }
   559 
   557 
   560 
   558 
   569     if ( !iUnifiedCertStore )
   567     if ( !iUnifiedCertStore )
   570         {
   568         {
   571         TRAPD( status, iUnifiedCertStore = CUnifiedCertStore::NewL( iFs, ETrue ) );
   569         TRAPD( status, iUnifiedCertStore = CUnifiedCertStore::NewL( iFs, ETrue ) );
   572         if ( status != KErrNone )
   570         if ( status != KErrNone )
   573             {
   571             {
   574             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   572             ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   575             User::Leave( KErrExitApp );
   573             User::Leave( KErrExitApp );
   576             }
   574             }
   577         // initialize unified cert store
   575         // initialize unified cert store
   578         status = iWrapper->InitializeCertStore( *iUnifiedCertStore );
   576         status = iWrapper->InitializeCertStore( *iUnifiedCertStore );
   579         if ( status )
   577         if ( status )
   705             }
   703             }
   706 
   704 
   707         if ( certstoreIndex < 0 )
   705         if ( certstoreIndex < 0 )
   708             {
   706             {
   709             // Couldn't find certificate storage
   707             // Couldn't find certificate storage
   710             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   708             ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   711             User::Leave( KErrExitApp );
   709             User::Leave( KErrExitApp );
   712             }
   710             }
   713 
   711 
   714         status = iWrapper->AddCert( iUnifiedCertStore->WritableCertStore( certstoreIndex ),
   712         status = iWrapper->AddCert( iUnifiedCertStore->WritableCertStore( certstoreIndex ),
   715                         labelBuf, iCertFormat, iCertOwnerType, 0, 0, iNewCert->Encoding() );
   713                         labelBuf, iCertFormat, iCertOwnerType, 0, 0, iNewCert->Encoding() );
   744         }
   742         }
   745     else
   743     else
   746         {
   744         {
   747         // If there is none WritableCertStore,
   745         // If there is none WritableCertStore,
   748         // then at least cacerts.dat is corrupted.
   746         // then at least cacerts.dat is corrupted.
   749         ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   747         ShowInformationNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   750         User::Leave( KErrExitApp );
   748         User::Leave( KErrExitApp );
   751         }
   749         }
   752 
   750 
   753     if ( ECACertificate == iCertOwnerType )
   751     if ( ECACertificate == iCertOwnerType )
   754         {
   752         {
   823     SecQueryUi=NULL;
   821     SecQueryUi=NULL;
   824 
   822 
   825       if ( queryAccepted!=KErrNone )
   823       if ( queryAccepted!=KErrNone )
   826             {
   824             {
   827             // cancel
   825             // cancel
   828             ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
   826             ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
   829             return KErrCancel;
   827             return KErrCancel;
   830             }
   828             }
   831         // Create filter to confirm that label doesn't already exist.
   829         // Create filter to confirm that label doesn't already exist.
   832         filter = CCertAttributeFilter::NewL();
   830         filter = CCertAttributeFilter::NewL();
   833         filter->SetLabel( aLabel );
   831         filter->SetLabel( aLabel );
   871 
   869 
   872     switch ( aStatus )
   870     switch ( aStatus )
   873         {
   871         {
   874         case KErrNone:
   872         case KErrNone:
   875             {
   873             {
   876             ShowConfirmationNoteL( R_CERTSAVER_ERROR_SAVEOK );
   874             ShowInformationNoteL(R_CERTSAVER_ERROR_SAVEOK);
   877             break;
   875             break;
   878             }
   876             }
   879         case KErrNotSupported:
   877         case KErrNotSupported:
   880             {
   878             {
   881             ShowErrorNoteL( R_CERTSAVER_ERROR_UNSUPPORTED_CERT );
   879             ShowInformationNoteL(R_CERTSAVER_ERROR_UNSUPPORTED_CERT);
   882             break;
   880             break;
   883             }
   881             }
   884         case KErrBadName:
   882         case KErrBadName:
   885             {
   883             {
   886             ShowErrorNoteL( R_CERTSAVER_ERROR_LABEL_ALREADY_EXISTS );
   884             ShowInformationNoteL(R_CERTSAVER_ERROR_LABEL_ALREADY_EXISTS);
   887             break;
   885             break;
   888             }
   886             }
   889         case KErrAlreadyExists:
   887         case KErrAlreadyExists:
   890             {
   888             {
   891             ShowErrorNoteL( R_CERTSAVER_ERROR_ALREADY_EXISTS );
   889             ShowInformationNoteL(R_CERTSAVER_ERROR_ALREADY_EXISTS);
   892             break;
   890             break;
   893             }
   891             }
   894         case KErrArgument:
   892         case KErrArgument:
   895             {
   893             {
   896             ShowErrorNoteL( R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED );
   894             ShowInformationNoteL(R_CERTSAVER_ERROR_CACERTS_DB_CORRUPTED);
   897             break;
   895             break;
   898             }
   896             }
   899         default:
   897         default:
   900             {
   898             {
   901             //No error note defined for unknown error.
   899             //No error note defined for unknown error.
  1189 TBool CCertSaverModel::CertificateOkL() const
  1187 TBool CCertSaverModel::CertificateOkL() const
  1190     {
  1188     {
  1191 
  1189 
  1192     if ( !CertificateSupported() ) 
  1190     if ( !CertificateSupported() ) 
  1193         {
  1191         {
  1194         ShowErrorNoteL( R_CERTSAVER_ERROR_UNSUPPORTED_CERT   ); 
  1192         ShowInformationNoteL(R_CERTSAVER_ERROR_UNSUPPORTED_CERT);
  1195         return EFalse;
  1193         return EFalse;
  1196         }
  1194         }
  1197     if ( CertNotValidAnymore() )
  1195     if ( CertNotValidAnymore() )
  1198         {
  1196         {
  1199         ShowErrorNoteL(R_CERTSAVER_ERROR_CERT_NOT_VALID);
  1197         ShowInformationNoteL(R_CERTSAVER_ERROR_CERT_NOT_VALID);
  1200         return ETrue;
  1198         return ETrue;
  1201         }
  1199         }
  1202     else if ( CertNotValidYet() )
  1200     else if ( CertNotValidYet() )
  1203         {
  1201         {
  1204         ShowErrorNoteL( R_CERTSAVER_ERROR_CERT_NOT_VALID_YET );
  1202         ShowInformationNoteL(R_CERTSAVER_ERROR_CERT_NOT_VALID_YET);
  1205         }
  1203         }
  1206     return ETrue;
  1204     return ETrue;
  1207     }
  1205     }
  1208 
  1206 
  1209 // ----------------------------------------------------------
  1207 // ----------------------------------------------------------
  1247 //
  1245 //
  1248 void CCertSaverModel::ShowInformationNoteL( TInt aResourceID ) const
  1246 void CCertSaverModel::ShowInformationNoteL( TInt aResourceID ) const
  1249     {
  1247     {
  1250 
  1248 
  1251     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
  1249     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
  1252     CHbDeviceMessageBoxSymbian::InformationL(buffer->Des()); 
  1250     CHbDeviceMessageBoxSymbian* iMessageBox = CHbDeviceMessageBoxSymbian::NewL(CHbDeviceMessageBoxSymbian::EInformation);
  1253     CleanupStack::PopAndDestroy( buffer );
  1251     CleanupStack::PushL(iMessageBox);                                                                                    
  1254     }
  1252     iMessageBox->SetTextL(buffer->Des());                                                                                
  1255 // ----------------------------------------------------------
  1253     iMessageBox->SetTimeout(6000);                                                                                      
  1256 // CCertSaverModel::ShowConfirmationNoteL() const
  1254     iMessageBox->ExecL();                                                                                                
  1257 // Creates and shows a confirmation note.
  1255     CleanupStack::PopAndDestroy(iMessageBox);                                                                            
  1258 // ----------------------------------------------------------
  1256     CleanupStack::PopAndDestroy( buffer );      
  1259 //
       
  1260 void CCertSaverModel::ShowConfirmationNoteL( TInt aResourceID ) const
       
  1261     {
       
  1262 
       
  1263     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
       
  1264     CHbDeviceMessageBoxSymbian::WarningL(buffer->Des()); 
       
  1265     CleanupStack::PopAndDestroy( buffer );
       
  1266     }
       
  1267 
       
  1268 // ----------------------------------------------------------
       
  1269 // CCertSaverModel::ShowErrorNoteL() const
       
  1270 // Creates and shows an error note.
       
  1271 // ----------------------------------------------------------
       
  1272 //
       
  1273 void CCertSaverModel::ShowErrorNoteL( TInt aResourceID ) const
       
  1274     {
       
  1275 
       
  1276     HBufC* buffer = iAppUi->CoeEnv()->AllocReadResourceLC( aResourceID );
       
  1277     //TODO: can be changed when hb supports ErrorL
       
  1278     CHbDeviceMessageBoxSymbian::WarningL(buffer->Des()); 
       
  1279     CleanupStack::PopAndDestroy( buffer );
       
  1280     }
  1257     }
  1281 
  1258 
  1282 // ----------------------------------------------------------
  1259 // ----------------------------------------------------------
  1283 // CCertSaverModel::TrimCertificateFields() const
  1260 // CCertSaverModel::TrimCertificateFields() const
  1284 // Trims given descriptor so that everything after and
  1261 // Trims given descriptor so that everything after and
  1405          }  
  1382          }  
  1406         ret = KErrNone;
  1383         ret = KErrNone;
  1407         }
  1384         }
  1408     else
  1385     else
  1409         {
  1386         {
  1410         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
  1387         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
  1411         ret = KErrCancel;
  1388         ret = KErrCancel;
  1412         }
  1389         }
  1413 
  1390 
  1414     rBuf.Close();
  1391     rBuf.Close();
  1415     CleanupStack::PopAndDestroy(&rBuf);
  1392     CleanupStack::PopAndDestroy(&rBuf);
  1446         {
  1423         {
  1447         ret = KErrNone;
  1424         ret = KErrNone;
  1448         }
  1425         }
  1449     else
  1426     else
  1450         {
  1427         {
  1451         ShowConfirmationNoteL( R_CERTSAVER_CERT_DISCARDED );
  1428         ShowInformationNoteL(R_CERTSAVER_CERT_DISCARDED);
  1452         ret = KErrCancel;
  1429         ret = KErrCancel;
  1453         }
  1430         }
  1454     CleanupStack::PopAndDestroy( prompt );
  1431     CleanupStack::PopAndDestroy( prompt );
  1455     CleanupStack::PopAndDestroy( label );
  1432     CleanupStack::PopAndDestroy( label );
  1456     return ret;
  1433     return ret;
  1577         ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
  1554         ShowInformationNoteL( R_CERTSAVER_PKCS12_DISCARDED );
  1578         User::Leave( KErrExitApp );
  1555         User::Leave( KErrExitApp );
  1579         }
  1556         }
  1580     TInt status = KErrNone;
  1557     TInt status = KErrNone;
  1581     // save private keys
  1558     // save private keys
  1582     TRAP( status, SavePrivateKeyL() );
  1559    if(CheckeBoxData.Compare(_L("1"))==0)
  1583 
  1560    {
       
  1561        TRAP( status, SavePrivateKeyL() );
       
  1562    }
  1584     // save user certificates if private key was saved.
  1563     // save user certificates if private key was saved.
  1585     if ( ( iSavedKeysCount > 0 || iKeyAlreadyExists ) && iParser.UserCertificates().Count() > 0 )
  1564     if ( ( iSavedKeysCount > 0 || iKeyAlreadyExists ) && iParser.UserCertificates().Count() > 0 )
  1586         {
  1565         {
  1587         for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
  1566         for ( TInt i = 0; i < iParser.UserCertificates().Count(); i++ )
  1588             {
  1567             {
  1611                 {
  1590                 {
  1612                 User::Leave( KErrExitApp );
  1591                 User::Leave( KErrExitApp );
  1613                 }
  1592                 }
  1614             }
  1593             }
  1615         }
  1594         }
       
  1595 /*
  1616     if ( iSavedCACertsCount != 0 || iSavedKeysCount != 0
  1596     if ( iSavedCACertsCount != 0 || iSavedKeysCount != 0
  1617         || iSavedUserCertsCount != 0 )
  1597         || iSavedUserCertsCount != 0 )
  1618         {
  1598         {
  1619         HBufC* p = StringLoader::LoadLC( R_CERTSAVER_HEADER_SAVED );
  1599 // show how many have been saved
  1620         message = HBufC::NewLC( KMaxLengthTextMeassageBody );
  1600         HBufC* p = StringLoader::LoadLC( R_CERTSAVER_HEADER_SAVED );             
  1621         message->Des().Append(p->Des());   
  1601         message = HBufC::NewLC( KMaxLengthTextMeassageBody );                    
  1622         TPtr msgPtr2 = message->Des();
  1602         message->Des().Append(p->Des());                                         
  1623         ConstructPKCS12QueryMsgL(
  1603         TPtr msgPtr2 = message->Des();                                           
       
  1604         ConstructPKCS12QueryMsgL(                                                
  1624             msgPtr2, iSavedKeysCount, iSavedUserCertsCount, iSavedCACertsCount );
  1605             msgPtr2, iSavedKeysCount, iSavedUserCertsCount, iSavedCACertsCount );
  1625         CHbDeviceMessageBoxSymbian::InformationL(message->Des()); 
  1606         CHbDeviceMessageBoxSymbian::InformationL(message->Des());                
  1626         CleanupStack::PopAndDestroy( message );
  1607         CleanupStack::PopAndDestroy( message );                                  
  1627         CleanupStack::PopAndDestroy( p );       
  1608         CleanupStack::PopAndDestroy( p );                                        
  1628         }
  1609         }
  1629     else
  1610     else
  1630         {
  1611         {
       
  1612         // the contents could not be saved is dropped
  1631         ShowInformationNoteL( R_QTN_CM_PKCS12_SAVING_FAILED );
  1613         ShowInformationNoteL( R_QTN_CM_PKCS12_SAVING_FAILED );
  1632         CleanupStack::PopAndDestroy( message );
  1614         CleanupStack::PopAndDestroy( message );
  1633         }
  1615         }
       
  1616 */
  1634     }
  1617     }
  1635 
  1618 
  1636 // ----------------------------------------------------------
  1619 // ----------------------------------------------------------
  1637 // CCertSaverModel::ConstructPKCS12QueryMsgL() const
  1620 // CCertSaverModel::ConstructPKCS12QueryMsgL() const
  1638 // Creates the certificate details message shown to the user.
  1621 // Creates the certificate details message shown to the user.