pkiutilities/CertSaver/src/certparser.cpp
branchRCL_3
changeset 49 09b1ac925e3f
parent 0 164170e6151a
child 50 03674e5abf46
equal deleted inserted replaced
47:63339781d179 49:09b1ac925e3f
    23 #include <x509certext.h>
    23 #include <x509certext.h>
    24 #include <hash.h>
    24 #include <hash.h>
    25 #include <X509CertNameParser.h>
    25 #include <X509CertNameParser.h>
    26 #include <featmgr.h>
    26 #include <featmgr.h>
    27 #include <data_caging_path_literals.hrh>
    27 #include <data_caging_path_literals.hrh>
    28 #include <AknQueryDialog.h>
       
    29 #include <CertSaver.rsg>
    28 #include <CertSaver.rsg>
    30 #include <StringLoader.h>
    29 #include <StringLoader.h>
    31 #include <securityerr.h>
    30 #include <securityerr.h>
    32 #include <aknnotewrappers.h>              // Note dialogs
       
    33 #include <apmrec.h>
    31 #include <apmrec.h>
       
    32 #include <eikenv.h>                       // CEikonEnv
    34 #include "certparser.h"
    33 #include "certparser.h"
       
    34 
       
    35 #include "SecQueryUi.h"                   // needed for password dialog
       
    36 
       
    37 #include <hbdevicemessageboxsymbian.h>    // needed for Note dialogs    
    35 
    38 
    36 // CONSTANTS
    39 // CONSTANTS
    37 _LIT( KPKCS12DllName, "crpkcs12.dll" );   // PKCS12 DLL name
    40 _LIT( KPKCS12DllName, "crpkcs12.dll" );   // PKCS12 DLL name
    38 const TInt KX509Version3 = 3;
    41 const TInt KX509Version3 = 3;
    39 const TInt KPwMaxLength = 32;
    42 const TInt KPwMaxLength = 32;
    92 // sets member data accordingly.
    95 // sets member data accordingly.
    93 // ----------------------------------------------------------
    96 // ----------------------------------------------------------
    94 //
    97 //
    95 void CCertParser::SetContentL( RFile& aFile )
    98 void CCertParser::SetContentL( RFile& aFile )
    96     {
    99     {
       
   100 
    97     delete iCert;
   101     delete iCert;
    98     iCert = NULL;
   102     iCert = NULL;
    99     TInt fileSize = 0;
   103     TInt fileSize = 0;
   100     CleanupClosePushL( aFile );
   104     CleanupClosePushL( aFile );
   101     User::LeaveIfError( aFile.Size( fileSize ) );
   105     User::LeaveIfError( aFile.Size( fileSize ) );
   124 // Creates PKCS#12 DLL
   128 // Creates PKCS#12 DLL
   125 // ----------------------------------------------------------
   129 // ----------------------------------------------------------
   126 //
   130 //
   127 void CCertParser::CreatePKCS12L()
   131 void CCertParser::CreatePKCS12L()
   128     {
   132     {
   129     // Load PKCS#12 dll
   133 
       
   134         // Load PKCS#12 dll
   130 #ifdef __WINS__
   135 #ifdef __WINS__
   131     User::LeaveIfError ( iLibrary.Load( KPKCS12DllName ) );
   136     User::LeaveIfError ( iLibrary.Load( KPKCS12DllName ) );
   132 #else
   137 #else
   133     User::LeaveIfError ( iLibrary.Load( KPKCS12DllName, KDC_SHARED_LIB_DIR ) );
   138     User::LeaveIfError ( iLibrary.Load( KPKCS12DllName, KDC_SHARED_LIB_DIR ) );
   134 #endif
   139 #endif
   149 //
   154 //
   150 TBool CCertParser::CheckIfPKCS12L(
   155 TBool CCertParser::CheckIfPKCS12L(
   151     const TDesC8& aPKCS12,
   156     const TDesC8& aPKCS12,
   152     const TDesC& aFileName )
   157     const TDesC& aFileName )
   153     {
   158     {
       
   159 
   154     CreatePKCS12L();
   160     CreatePKCS12L();
   155 
   161 
   156     if ( !iPKCS12->IsPKCS12Data( aPKCS12 ) )
   162     if ( !iPKCS12->IsPKCS12Data( aPKCS12 ) )
   157         {
   163         {
   158         ShowErrorNoteL( R_CERTSAVER_PKCS12_FILE_CORRUPTED );
   164         ShowErrorNoteL( R_CERTSAVER_PKCS12_FILE_CORRUPTED );
   159         return EFalse;
   165         return EFalse;
   160         }
   166         }
   161 
   167 
   162     TBool done = EFalse;
   168     TBool done = EFalse;
   163     TBuf<KPwMaxLength> password;
   169     TBuf<KPwMaxLength> password;
   164     HBufC* buffer = NULL;
       
   165 
       
   166     if ( !iEikEnv )
       
   167         {
       
   168         iEikEnv = CEikonEnv::Static();
       
   169         }
       
   170 
   170 
   171     while ( !done )
   171     while ( !done )
   172         {
   172         {
   173         if ( !GetPasswordL( password, aFileName ) )
   173         if ( !GetPasswordL( password, aFileName ) )
   174             {
   174             {
   175             buffer = iEikEnv->AllocReadResourceLC( R_CERTSAVER_PKCS12_DISCARDED );
   175             ShowErrorNoteL(R_CERTSAVER_PKCS12_DISCARDED);
   176             CAknInformationNote* note = new (ELeave) CAknInformationNote( ETrue );
       
   177             note->ExecuteLD(buffer->Des());
       
   178             CleanupStack::PopAndDestroy( buffer );
       
   179             User::Leave( KErrExitApp );
   176             User::Leave( KErrExitApp );
   180             }
   177             }
   181         TRAPD( err, iPKCS12->ParseL( aPKCS12, password ) );
   178         TRAPD( err, iPKCS12->ParseL( aPKCS12, password ) );
   182         if ( err != KErrBadPassphrase )
   179         if ( err != KErrBadPassphrase )
   183             {
   180             {
   224 //
   221 //
   225 // ----------------------------------------------------------
   222 // ----------------------------------------------------------
   226 //
   223 //
   227 TInt CCertParser::GetPasswordL( TDes& aPassword, const TDesC& aFileName )
   224 TInt CCertParser::GetPasswordL( TDes& aPassword, const TDesC& aFileName )
   228     {
   225     {
   229     CAknTextQueryDialog* query = CAknTextQueryDialog::NewL( aPassword );
   226     CSecQueryUi* SecQueryUi = CSecQueryUi::NewL();                                       
   230     CleanupStack::PushL( query );
   227     HBufC* prompt =StringLoader::LoadLC( R_QTN_CM_TITLE_P12_PASSWORD, aFileName );        
   231     HBufC* prompt =
   228     TInt queryAccepted = SecQueryUi->SecQueryDialog(prompt->Des(), aPassword,                
   232         StringLoader::LoadLC( R_QTN_CM_TITLE_P12_PASSWORD, aFileName );
   229                                                 1,KPwMaxLength,                               
   233     query->SetPromptL( *prompt );
   230                                                 ESecUiAlphaSupported |                        
   234     CleanupStack::PopAndDestroy( prompt );
   231                                                 ESecUiCancelSupported |                       
   235     CleanupStack::Pop( query );
   232                                                 ESecUiSecretSupported |                
   236     return query->ExecuteLD( R_PKCS12_PW_QUERY_DIALOG );
   233                                                 ESecUiEmergencyNotSupported); 
   237     }
   234     CleanupStack::PopAndDestroy( prompt );                                                
       
   235     delete SecQueryUi;                                                                   
       
   236     SecQueryUi=NULL;
       
   237     return (queryAccepted==KErrNone);
       
   238   }
   238 
   239 
   239 // ----------------------------------------------------------
   240 // ----------------------------------------------------------
   240 // CCertParser::CheckIfX509CertificateL()
   241 // CCertParser::CheckIfX509CertificateL()
   241 // Returns the type of the certificate.
   242 // Returns the type of the certificate.
   242 // ----------------------------------------------------------
   243 // ----------------------------------------------------------
   243 //
   244 //
   244 TBool CCertParser::CheckIfX509CertificateL(const TDesC8& aCert)
   245 TBool CCertParser::CheckIfX509CertificateL(const TDesC8& aCert)
   245     {
   246     {
       
   247 
   246     TRAPD( err,
   248     TRAPD( err,
   247            iCert = CX509Certificate::NewL(aCert);
   249            iCert = CX509Certificate::NewL(aCert);
   248          );
   250          );
   249 
   251 
   250     if ( err != KErrNone )
   252     if ( err != KErrNone )
   300 // CCertParser::CACertificates()
   302 // CCertParser::CACertificates()
   301 // ----------------------------------------------------------
   303 // ----------------------------------------------------------
   302 //
   304 //
   303 const CArrayPtr<CX509Certificate>&  CCertParser::CACertificates() const
   305 const CArrayPtr<CX509Certificate>&  CCertParser::CACertificates() const
   304     {
   306     {
       
   307 
   305     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   308     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   306     return iPKCS12->CACertificates();
   309     return iPKCS12->CACertificates();
   307     }
   310     }
   308 
   311 
   309 // ----------------------------------------------------------
   312 // ----------------------------------------------------------
   310 // CCertParser::UserCertificates()
   313 // CCertParser::UserCertificates()
   311 // ----------------------------------------------------------
   314 // ----------------------------------------------------------
   312 //
   315 //
   313 const CArrayPtr<CX509Certificate>&  CCertParser::UserCertificates() const
   316 const CArrayPtr<CX509Certificate>&  CCertParser::UserCertificates() const
   314     {
   317     {
       
   318 
   315     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   319     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   316     return iPKCS12->UserCertificates();
   320     return iPKCS12->UserCertificates();
   317     }
   321     }
   318 
   322 
   319 // ----------------------------------------------------------
   323 // ----------------------------------------------------------
   320 // CCertParser::Keys()
   324 // CCertParser::Keys()
   321 // ----------------------------------------------------------
   325 // ----------------------------------------------------------
   322 //
   326 //
   323 const CArrayPtr<HBufC8>& CCertParser::Keys() const
   327 const CArrayPtr<HBufC8>& CCertParser::Keys() const
   324     {
   328     {
       
   329 
   325     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   330     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   326     return iPKCS12->PrivateKeys();
   331     return iPKCS12->PrivateKeys();
   327     }
   332     }
   328 
   333 
   329 // ----------------------------------------------------------
   334 // ----------------------------------------------------------
   330 // CCertParser::CertType()
   335 // CCertParser::CertType()
   331 // ----------------------------------------------------------
   336 // ----------------------------------------------------------
   332 //
   337 //
   333  CCertParser::TCertType CCertParser::CertType() const
   338  CCertParser::TCertType CCertParser::CertType() const
   334     {
   339     {
       
   340 
   335     return iCertType;
   341     return iCertType;
   336     }
   342     }
   337 
   343 
   338 // ----------------------------------------------------------
   344 // ----------------------------------------------------------
   339 // CCertParser::CertificateBuf()
   345 // CCertParser::CertificateBuf()
   340 // Returns the content of the certificate.
   346 // Returns the content of the certificate.
   341 // ----------------------------------------------------------
   347 // ----------------------------------------------------------
   342 //
   348 //
   343 const TPtrC8 CCertParser::CertificateBuf() const
   349 const TPtrC8 CCertParser::CertificateBuf() const
   344     {
   350     {
       
   351 
   345     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   352     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   346     return iCert->Encoding();
   353     return iCert->Encoding();
   347     }
   354     }
   348 
   355 
   349 // ----------------------------------------------------------
   356 // ----------------------------------------------------------
   351 // Returns the X.509 certificate object.
   358 // Returns the X.509 certificate object.
   352 // ----------------------------------------------------------
   359 // ----------------------------------------------------------
   353 //
   360 //
   354 const CX509Certificate& CCertParser::Certificate() const
   361 const CX509Certificate& CCertParser::Certificate() const
   355     {
   362     {
       
   363 
   356     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   364     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   357     return *((CX509Certificate*)iCert);
   365     return *((CX509Certificate*)iCert);
   358     }
   366     }
   359 
   367 
   360 // ----------------------------------------------------------
   368 // ----------------------------------------------------------
   367      if ( !iEikEnv )
   375      if ( !iEikEnv )
   368         {
   376         {
   369         iEikEnv = CEikonEnv::Static();
   377         iEikEnv = CEikonEnv::Static();
   370         }
   378         }
   371     HBufC* buffer = iEikEnv->AllocReadResourceLC( aResourceID );
   379     HBufC* buffer = iEikEnv->AllocReadResourceLC( aResourceID );
   372     CAknErrorNote* note = new (ELeave) CAknErrorNote( ETrue );
   380     CHbDeviceMessageBoxSymbian* iMessageBox = CHbDeviceMessageBoxSymbian::NewL(CHbDeviceMessageBoxSymbian::EWarning);
   373     note->ExecuteLD(buffer->Des());
   381     CleanupStack::PushL(iMessageBox);                                                                                    
   374     CleanupStack::PopAndDestroy( buffer );
   382     iMessageBox->SetTextL(buffer->Des());                                                                                
       
   383     iMessageBox->SetTimeout(6000);                                                                                      
       
   384     iMessageBox->ExecL();                                                                                                
       
   385     CleanupStack::PopAndDestroy(iMessageBox);                                                                            
       
   386     CleanupStack::PopAndDestroy( buffer );                                                                               
   375     }
   387     }
   376 
   388 
   377 //  End of File
   389 //  End of File
   378 
   390