pkiutilities/CertSaver/src/certparser.cpp
changeset 30 cc1cea6aabaf
parent 0 164170e6151a
child 33 3aa774c655ac
equal deleted inserted replaced
26:aad866c37519 30:cc1cea6aabaf
    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
    31 #include <aknnotewrappers.h>              // Note dialogs text, TODO: could be removed after localization is ready
    33 #include <apmrec.h>
    32 #include <apmrec.h>
    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 );
   171     while ( !done )
   177     while ( !done )
   172         {
   178         {
   173         if ( !GetPasswordL( password, aFileName ) )
   179         if ( !GetPasswordL( password, aFileName ) )
   174             {
   180             {
   175             buffer = iEikEnv->AllocReadResourceLC( R_CERTSAVER_PKCS12_DISCARDED );
   181             buffer = iEikEnv->AllocReadResourceLC( R_CERTSAVER_PKCS12_DISCARDED );
   176             CAknInformationNote* note = new (ELeave) CAknInformationNote( ETrue );
   182             CHbDeviceMessageBoxSymbian::InformationL(buffer->Des()); 
   177             note->ExecuteLD(buffer->Des());
       
   178             CleanupStack::PopAndDestroy( buffer );
   183             CleanupStack::PopAndDestroy( buffer );
   179             User::Leave( KErrExitApp );
   184             User::Leave( KErrExitApp );
   180             }
   185             }
   181         TRAPD( err, iPKCS12->ParseL( aPKCS12, password ) );
   186         TRAPD( err, iPKCS12->ParseL( aPKCS12, password ) );
   182         if ( err != KErrBadPassphrase )
   187         if ( err != KErrBadPassphrase )
   224 //
   229 //
   225 // ----------------------------------------------------------
   230 // ----------------------------------------------------------
   226 //
   231 //
   227 TInt CCertParser::GetPasswordL( TDes& aPassword, const TDesC& aFileName )
   232 TInt CCertParser::GetPasswordL( TDes& aPassword, const TDesC& aFileName )
   228     {
   233     {
   229     CAknTextQueryDialog* query = CAknTextQueryDialog::NewL( aPassword );
   234     CSecQueryUi* SecQueryUi = CSecQueryUi::NewL();                                       
   230     CleanupStack::PushL( query );
   235     HBufC* prompt =StringLoader::LoadLC( R_QTN_CM_TITLE_P12_PASSWORD, aFileName );        
   231     HBufC* prompt =
   236     TInt queryAccepted = SecQueryUi->SecQueryDialog(prompt->Des(), aPassword,                
   232         StringLoader::LoadLC( R_QTN_CM_TITLE_P12_PASSWORD, aFileName );
   237                                                 1,KPwMaxLength,                               
   233     query->SetPromptL( *prompt );
   238                                                 ESecUiAlphaSupported |                        
   234     CleanupStack::PopAndDestroy( prompt );
   239                                                 ESecUiCancelSupported |                       
   235     CleanupStack::Pop( query );
   240                                                 ESecUiSecretSupported |                
   236     return query->ExecuteLD( R_PKCS12_PW_QUERY_DIALOG );
   241                                                 ESecUiEmergencyNotSupported); 
   237     }
   242     CleanupStack::PopAndDestroy( prompt );                                                
       
   243     delete SecQueryUi;                                                                   
       
   244     SecQueryUi=NULL;
       
   245     return (queryAccepted==KErrNone);
       
   246   }
   238 
   247 
   239 // ----------------------------------------------------------
   248 // ----------------------------------------------------------
   240 // CCertParser::CheckIfX509CertificateL()
   249 // CCertParser::CheckIfX509CertificateL()
   241 // Returns the type of the certificate.
   250 // Returns the type of the certificate.
   242 // ----------------------------------------------------------
   251 // ----------------------------------------------------------
   243 //
   252 //
   244 TBool CCertParser::CheckIfX509CertificateL(const TDesC8& aCert)
   253 TBool CCertParser::CheckIfX509CertificateL(const TDesC8& aCert)
   245     {
   254     {
       
   255 
   246     TRAPD( err,
   256     TRAPD( err,
   247            iCert = CX509Certificate::NewL(aCert);
   257            iCert = CX509Certificate::NewL(aCert);
   248          );
   258          );
   249 
   259 
   250     if ( err != KErrNone )
   260     if ( err != KErrNone )
   300 // CCertParser::CACertificates()
   310 // CCertParser::CACertificates()
   301 // ----------------------------------------------------------
   311 // ----------------------------------------------------------
   302 //
   312 //
   303 const CArrayPtr<CX509Certificate>&  CCertParser::CACertificates() const
   313 const CArrayPtr<CX509Certificate>&  CCertParser::CACertificates() const
   304     {
   314     {
       
   315 
   305     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   316     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   306     return iPKCS12->CACertificates();
   317     return iPKCS12->CACertificates();
   307     }
   318     }
   308 
   319 
   309 // ----------------------------------------------------------
   320 // ----------------------------------------------------------
   310 // CCertParser::UserCertificates()
   321 // CCertParser::UserCertificates()
   311 // ----------------------------------------------------------
   322 // ----------------------------------------------------------
   312 //
   323 //
   313 const CArrayPtr<CX509Certificate>&  CCertParser::UserCertificates() const
   324 const CArrayPtr<CX509Certificate>&  CCertParser::UserCertificates() const
   314     {
   325     {
       
   326 
   315     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   327     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   316     return iPKCS12->UserCertificates();
   328     return iPKCS12->UserCertificates();
   317     }
   329     }
   318 
   330 
   319 // ----------------------------------------------------------
   331 // ----------------------------------------------------------
   320 // CCertParser::Keys()
   332 // CCertParser::Keys()
   321 // ----------------------------------------------------------
   333 // ----------------------------------------------------------
   322 //
   334 //
   323 const CArrayPtr<HBufC8>& CCertParser::Keys() const
   335 const CArrayPtr<HBufC8>& CCertParser::Keys() const
   324     {
   336     {
       
   337 
   325     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   338     __ASSERT_ALWAYS( iPKCS12, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   326     return iPKCS12->PrivateKeys();
   339     return iPKCS12->PrivateKeys();
   327     }
   340     }
   328 
   341 
   329 // ----------------------------------------------------------
   342 // ----------------------------------------------------------
   330 // CCertParser::CertType()
   343 // CCertParser::CertType()
   331 // ----------------------------------------------------------
   344 // ----------------------------------------------------------
   332 //
   345 //
   333  CCertParser::TCertType CCertParser::CertType() const
   346  CCertParser::TCertType CCertParser::CertType() const
   334     {
   347     {
       
   348 
   335     return iCertType;
   349     return iCertType;
   336     }
   350     }
   337 
   351 
   338 // ----------------------------------------------------------
   352 // ----------------------------------------------------------
   339 // CCertParser::CertificateBuf()
   353 // CCertParser::CertificateBuf()
   340 // Returns the content of the certificate.
   354 // Returns the content of the certificate.
   341 // ----------------------------------------------------------
   355 // ----------------------------------------------------------
   342 //
   356 //
   343 const TPtrC8 CCertParser::CertificateBuf() const
   357 const TPtrC8 CCertParser::CertificateBuf() const
   344     {
   358     {
       
   359 
   345     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   360     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   346     return iCert->Encoding();
   361     return iCert->Encoding();
   347     }
   362     }
   348 
   363 
   349 // ----------------------------------------------------------
   364 // ----------------------------------------------------------
   351 // Returns the X.509 certificate object.
   366 // Returns the X.509 certificate object.
   352 // ----------------------------------------------------------
   367 // ----------------------------------------------------------
   353 //
   368 //
   354 const CX509Certificate& CCertParser::Certificate() const
   369 const CX509Certificate& CCertParser::Certificate() const
   355     {
   370     {
       
   371 
   356     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   372     __ASSERT_ALWAYS( iCert, User::Panic( KCertSaverPanic, KPanicNullPointer ) );
   357     return *((CX509Certificate*)iCert);
   373     return *((CX509Certificate*)iCert);
   358     }
   374     }
   359 
   375 
   360 // ----------------------------------------------------------
   376 // ----------------------------------------------------------
   367      if ( !iEikEnv )
   383      if ( !iEikEnv )
   368         {
   384         {
   369         iEikEnv = CEikonEnv::Static();
   385         iEikEnv = CEikonEnv::Static();
   370         }
   386         }
   371     HBufC* buffer = iEikEnv->AllocReadResourceLC( aResourceID );
   387     HBufC* buffer = iEikEnv->AllocReadResourceLC( aResourceID );
   372     CAknErrorNote* note = new (ELeave) CAknErrorNote( ETrue );
   388     CHbDeviceMessageBoxSymbian::WarningL(buffer->Des()); 
   373     note->ExecuteLD(buffer->Des());
       
   374     CleanupStack::PopAndDestroy( buffer );
   389     CleanupStack::PopAndDestroy( buffer );
   375     }
   390     }
   376 
   391 
   377 //  End of File
   392 //  End of File
   378 
   393