eapol/eapol_framework/wapi_symbian/wapi_core/symbian/certificate_store_db_symbian.cpp
changeset 17 8840d3e38314
equal deleted inserted replaced
2:1c7bc153c08e 17:8840d3e38314
       
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : ./accesssec/eapol/eapol_framework/wapi_symbian/wapi_core/symbian/certificate_store_db_symbian.cpp
       
     4 *  Part of     : WAPI / WAPI       *** Info from the SWAD
       
     5 *  Description : WAPI authentication
       
     6 *  Version     : %version: 61 % << Don't touch! Updated by Synergy at check-out.
       
     7 *
       
     8 *  Copyright © 2001-2009 Nokia.  All rights reserved.
       
     9 *  This material, including documentation and any related computer
       
    10 *  programs, is protected by copyright controlled by Nokia.  All
       
    11 *  rights are reserved.  Copying, including reproducing, storing,
       
    12 *  adapting or translating, any or all of this material requires the
       
    13 *  prior written consent of Nokia.  This material also contains
       
    14 *  confidential information which may not be disclosed to others
       
    15 *  without the prior written consent of Nokia.
       
    16 * ============================================================================
       
    17 * Template version: 4.1.1
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <mmtsy_names.h>
       
    23 #include <utf.h>                           // for CnvUtfConverter
       
    24 #include <s32strm.h> 	                   // For RReadStream
       
    25 #include <d32dbms.h>                       // for RDbColReadStream
       
    26 #include "certificate_store_db_symbian.h"
       
    27 #include "eap_am_trace_symbian.h"
       
    28 #include "eap_variable_data.h"
       
    29 #include "abs_eap_am_tools.h"
       
    30 #include "eap_am_tools_symbian.h"
       
    31 #include "eap_am_types.h"
       
    32 #include "wapi_asn1_der_parser.h"
       
    33 #include "wapi_am_core_symbian.h"
       
    34 // ================= public:  Constructors and destructor =======================
       
    35 
       
    36 // ---------------------------------------------------------
       
    37 // CCertificateStoreDatabase::NewL()
       
    38 // ---------------------------------------------------------
       
    39 //
       
    40 CCertificateStoreDatabase* CCertificateStoreDatabase::NewL(
       
    41     abs_eap_am_tools_c* aAmTools )
       
    42     {    
       
    43 	CCertificateStoreDatabase* self = new(ELeave)
       
    44 	    CCertificateStoreDatabase( aAmTools );
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL();
       
    47     CleanupStack::Pop( self );
       
    48 
       
    49     return self;
       
    50 
       
    51     } // CCertificateStoreDatabase::NewL()
       
    52 
       
    53 
       
    54 // ---------------------------------------------------------
       
    55 // CCertificateStoreDatabase::~CCertificateStoreDatabase()
       
    56 // ---------------------------------------------------------
       
    57 //
       
    58 CCertificateStoreDatabase::~CCertificateStoreDatabase()
       
    59     {
       
    60     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
    61         "CCertificateStoreDatabase::~CCertificateStoreDatabase() IN\n" ) ) );
       
    62 
       
    63     Close();
       
    64     
       
    65     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
    66         "CCertificateStoreDatabase::~CCertificateStoreDatabase() OUT\n" ) ) );
       
    67 
       
    68     } // CCertificateStoreDatabase::~CCertificateStoreDatabase()
       
    69 
       
    70 
       
    71 // ================= public: New, open/close/destroy functionality =======================
       
    72 
       
    73 
       
    74 // ---------------------------------------------------------
       
    75 // CCertificateStoreDatabase::OpenCertificateStoreL()
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 void CCertificateStoreDatabase::OpenCertificateStoreL()
       
    79 	{
       
    80     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
    81         "CCertificateStoreDatabase::OpenCertificateStoreL() IN\n" ) ) );
       
    82 	
       
    83     if ( iCsDbCreated == EFalse || iCsSessionOpened == EFalse )
       
    84 		{
       
    85 		// Certificate store DB and tables are not created.
       
    86 		CreateCertificateStoreL();
       
    87 		}
       
    88 
       
    89     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
    90         "CCertificateStoreDatabase::OpenCertificateStoreL() OUT\n" ) ) );
       
    91 
       
    92 	} // CCertificateStoreDatabase::OpenCertificateStoreL()
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------
       
    96 // CCertificateStoreDatabase::Close()
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CCertificateStoreDatabase::Close()
       
   100 	{
       
   101     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   102         "CCertificateStoreDatabase::Close() IN\n" ) ) );
       
   103 
       
   104 	if ( iCsDbCreated )
       
   105 		{
       
   106 		iCsDb.Close();
       
   107 		iCsDbCreated = EFalse;
       
   108 		}
       
   109 	if ( iCsSessionOpened )
       
   110 	    {
       
   111 		iCsDbSession.Close();
       
   112 		iCsSessionOpened = EFalse;
       
   113 	    }	
       
   114 
       
   115     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   116         "CCertificateStoreDatabase::Close() OUT\n" ) ) );
       
   117 	
       
   118 	} // CCertificateStoreDatabase::Close()
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CCertificateStoreDatabase::SetCorePartner()
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 void CCertificateStoreDatabase::SetCorePartner(wapi_am_core_symbian_c *partner)
       
   125     {
       
   126     iPartner = partner; 
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CCertificateStoreDatabase::DestroyCertificateStore()
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 TInt CCertificateStoreDatabase::DestroyCertificateStore()
       
   134 	{
       
   135     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   136         "CCertificateStoreDatabase::DestroyCertificateStore() IN, \
       
   137         iCsSessionOpened=%d, iCsDbCreated=%d\n" ),
       
   138         iCsSessionOpened, iCsDbCreated ) );
       
   139 		
       
   140     // There could be a case where certificate store DB is destroyed and UI
       
   141     // calls this function. We return KErrNone in that case.        
       
   142     if ( iCsDbCreated == EFalse )
       
   143         {
       
   144         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   145             "WARNING: CCertificateStoreDatabase::DestroyCertificateStore() \
       
   146             Certificate store DB doesn't exist. Returning KErrNone.\n" ) ) );
       
   147         return KErrNone;
       
   148         }
       
   149 
       
   150     if ( iCsSessionOpened == EFalse )
       
   151 		{
       
   152 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   153             "ERROR: CCertificateStoreDatabase::DestroyCertificateStore() \
       
   154 			Certificate store not opened!\n" ) ) );
       
   155 		return KErrSessionClosed;
       
   156 		}	
       
   157 		
       
   158     if (iPartner != NULL)
       
   159         {
       
   160         iPartner->reset();
       
   161         }
       
   162     
       
   163 	TInt error = iCsDb.Destroy();
       
   164 		
       
   165 	if ( error != KErrNone )
       
   166 		{
       
   167 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   168             "ERROR: CCertificateStoreDatabase::DestroyCertificateStore() \
       
   169 			iCsDb.Destroy() failed, error=%d.\n" ), error ) );
       
   170 		}
       
   171 		else
       
   172 		{
       
   173 		iCsDbCreated = EFalse;
       
   174 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   175             "CCertificateStoreDatabase::DestroyCertificateStore() \
       
   176 			CS DB destroyed successfully.\n" ) ) );	
       
   177 		}
       
   178 	
       
   179     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   180         "CCertificateStoreDatabase::DestroyCertificateStore() OUT,\
       
   181         error=%d.\n" ), error ) );
       
   182 	
       
   183     return error;
       
   184 	} // CCertificateStoreDatabase::DestroyCertificateStore()
       
   185 
       
   186 
       
   187 // ================= public:  New =======================
       
   188 
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // CCertificateStoreDatabase::InitializeCertificateStoreL()
       
   192 // ---------------------------------------------------------
       
   193 //
       
   194 void CCertificateStoreDatabase::InitializeCertificateStoreL()
       
   195 	{
       
   196     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   197         "CCertificateStoreDatabase::InitializeCertificateStoreL() IN\n" ) ) );
       
   198 
       
   199     WriteCertificateStoreStateL( ECertificateStoreInitialized );
       
   200     
       
   201     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   202         "CCertificateStoreDatabase::InitializeCertificateStoreL() OUT\n" ) ) );
       
   203 
       
   204 	} // CCertificateStoreDatabase::InitializeCertificateStoreL()
       
   205 
       
   206 
       
   207 // ================= public: New, get/set/remove data in database =======================
       
   208 
       
   209 
       
   210 // ---------------------------------------------------------
       
   211 // CCertificateStoreDatabase::GetCsDataByReferenceL()
       
   212 // ---------------------------------------------------------
       
   213 //
       
   214 void CCertificateStoreDatabase::GetCsDataByReferenceL(
       
   215     ec_cs_data_type_e aDataType,
       
   216     const TDesC8& aDataReference,
       
   217    	HBufC8** aOutColumnValue )
       
   218 	{
       
   219     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   220         "CCertificateStoreDatabase::GetCsDataByReferenceL1() IN\n" ) ) );
       
   221     
       
   222         // Convert the received reference id into integer.. 
       
   223     TUint intRef;
       
   224     if (aDataType != ec_cs_data_type_selected_ca_id && aDataType != ec_cs_data_type_selected_client_id)
       
   225         {
       
   226         // Convert the received reference id into integer.. 
       
   227         intRef = eap_read_u32_t_network_order(
       
   228             aDataReference.Ptr(),
       
   229             aDataReference.Size());
       
   230         }
       
   231     else
       
   232         {
       
   233         intRef = static_cast<TUint>(*aDataReference.Ptr());
       
   234         }
       
   235     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
   236             ( "CCertificateStoreDatabase::GetCsDataByReference1L() \
       
   237             New entry: reference set to DB(TEXT)=%d\n" ), intRef ) );
       
   238     GetCsDataByReferenceL ( aDataType, intRef, aOutColumnValue );
       
   239 
       
   240     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   241         "CCertificateStoreDatabase::GetCsDataByReferenceL1() OUT\n" ) ) );
       
   242 
       
   243 	} // CCertificateStoreDatabase::GetCsDataByReferenceL()
       
   244 
       
   245 
       
   246 // ---------------------------------------------------------
       
   247 // CCertificateStoreDatabase::GetCsDataByReferenceL()
       
   248 // ---------------------------------------------------------
       
   249 //
       
   250 void CCertificateStoreDatabase::GetCsDataByReferenceL(
       
   251     ec_cs_data_type_e aDataType,
       
   252     const TUint aDataReference,
       
   253    	HBufC8** aOutColumnValue )
       
   254     
       
   255     {
       
   256     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   257         "CCertificateStoreDatabase::GetCsDataByReferenceL() IN\n" ) ) );
       
   258     	
       
   259     // There could be a case where CS DB is destroyed and UI
       
   260     // calls this function. We just return in that case.
       
   261     if ( !iCsDbCreated )
       
   262         {
       
   263         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   264                 "ERROR: CCertificateStoreDatabase::GetCsDataByReferenceL() \
       
   265                 CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   266         return;
       
   267         }
       
   268 
       
   269     if ( !iCsSessionOpened )
       
   270         {
       
   271         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   272                 "ERROR: CCertificateStoreDatabase::GetCsDataByReferenceL() - CS not opened.\n" ) ) );
       
   273         User::Leave( KErrSessionClosed );
       
   274         }			
       
   275 		    
       
   276     // DB names
       
   277     TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   278     TBuf<KDbMaxColName> referenceColumnName;  // const from d32dbms.h
       
   279     TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   280 
       
   281     GetDbNamesFromDataTypeL( aDataType, tableName, referenceColumnName,
       
   282 		                        dataColumnName );
       
   283 
       
   284     // create SQL query statement
       
   285     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
   286     TPtr sqlStatement = buf->Des();
       
   287 
       
   288     _LIT( KSqlQuery, "SELECT %S FROM %S WHERE %S=%d" );
       
   289     sqlStatement.Format( KSqlQuery, &dataColumnName, &tableName, &referenceColumnName, aDataReference );
       
   290 
       
   291     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   292             "wapi_am_core_symbian_c::GetCsDataByReferenceL() sqlStatement",
       
   293             sqlStatement.Ptr(), sqlStatement.Size() ) );	
       
   294 
       
   295     RDbView view;
       
   296     User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
   297             TDbWindow::EUnlimited, RDbView::EReadOnly ) );
       
   298     
       
   299 
       
   300     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   301         "CCertificateStoreDatabase::GetCsDataByReferenceL() View prepared OK.\n" ) ) );
       
   302 	CleanupClosePushL( view );
       
   303 	User::LeaveIfError( view.EvaluateAll() );
       
   304     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   305         "CCertificateStoreDatabase::GetCsDataByReferenceL() View evaluated OK.\n" ) ) );    
       
   306     
       
   307 	if ( view.FirstL() )
       
   308 	    {
       
   309 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   310             "CCertificateStoreDatabase::GetCsDataByReferenceL() view.FirstL() OK.\n" ) ) );    
       
   311 		
       
   312 		view.GetL();		
       
   313 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   314             "CCertificateStoreDatabase::GetCsDataByReferenceL() view.GetL() OK.\n" ) ) );    
       
   315 
       
   316 		switch ( view.ColType( KDefaultColumnNumberOne ) )
       
   317 		    {
       
   318 		    case EDbColText:				
       
   319 			    {
       
   320                 // Buffer for unicode parameter
       
   321                 HBufC* unicodebuf = HBufC::NewLC(view.ColLength( KDefaultColumnNumberOne ));
       
   322                 TPtr unicodeString = unicodebuf->Des();
       
   323                 unicodeString = view.ColDes(KDefaultColumnNumberOne);
       
   324                 // Convert to 8-bit
       
   325                 if (unicodeString.Size() > 0)
       
   326                     {
       
   327                     *aOutColumnValue = HBufC8::NewLC(
       
   328                         view.ColLength( KDefaultColumnNumberOne ) ); // Buffer for the data.
       
   329                     TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
   330                     outColumnValuePtr8.Copy(unicodeString);
       
   331                     if (outColumnValuePtr8.Size() == 0)
       
   332 	                    {
       
   333 	                        User::Leave(KErrNoMemory);
       
   334 	                    }
       
   335                     CleanupStack::Pop( *aOutColumnValue );
       
   336                     } 
       
   337                 else 
       
   338                     {
       
   339                         // Empty field. Do nothing...data remains invalid
       
   340                     }
       
   341                 CleanupStack::PopAndDestroy(unicodebuf);
       
   342 			    break;
       
   343 			    }
       
   344 			    
       
   345 		    case EDbColUint32:
       
   346 	            {
       
   347 	                TUint value;
       
   348 	                value = view.ColUint32(KDefaultColumnNumberOne);
       
   349                     *aOutColumnValue = HBufC8::NewLC(
       
   350                         view.ColLength( KDefaultColumnNumberOne ) ); // Buffer for the data.
       
   351                     TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
   352                     outColumnValuePtr8.Copy((const unsigned char *)&value, sizeof(TUint));
       
   353                     if (outColumnValuePtr8.Size() == 0)
       
   354                     {
       
   355                         User::Leave(KErrNoMemory);
       
   356                     }
       
   357                     CleanupStack::Pop( *aOutColumnValue );
       
   358 	            }
       
   359 	            break;
       
   360 		        
       
   361 		    case EDbColBinary:
       
   362 			    {
       
   363 		        EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   364 	                "ERROR: CCertificateStoreDatabase::GetCsDataByReferenceL() \
       
   365 	                Unsupported DB field EDbColBinary.\n" ) ) );
       
   366 				User::Leave( KErrNotSupported );
       
   367                 break;
       
   368 			    }
       
   369 		    case EDbColLongBinary:				
       
   370 			    {
       
   371 		        EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   372 					"CCertificateStoreDatabase::GetCsDataByReferenceL() \
       
   373 					Long binary column.\n" ) ) );			
       
   374 				GetLongBinaryDataL( view, aOutColumnValue );
       
   375 				break;
       
   376 			    }			
       
   377 		    default:
       
   378 		    	{
       
   379 		        EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   380 		            "ERROR: CCertificateStoreDatabase::GetCsDataByReferenceL() \
       
   381 		            Unsupported DB field:%d\n" ), 
       
   382 		            view.ColType( KDefaultColumnNumberOne ) ) );
       
   383 			    User::Leave( KErrNotSupported );
       
   384 			    break;
       
   385 		    	}
       
   386 		    } // switch ( view.ColType( KDefaultColumnNumberOne ) )
       
   387 	    } // if ( view.FirstL() )
       
   388 	
       
   389 	// clean
       
   390 	CleanupStack::PopAndDestroy( &view ); // Close view.	
       
   391     CleanupStack::PopAndDestroy( buf );
       
   392 
       
   393     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   394         "CCertificateStoreDatabase::GetCsDataByReferenceL() OUT\n" ) ) );
       
   395 
       
   396 	} // CCertificateStoreDatabase::GetCsDataByReferenceL()
       
   397 	
       
   398 
       
   399 // ---------------------------------------------------------
       
   400 // CCertificateStoreDatabase::GetCsDataL()
       
   401 // ---------------------------------------------------------
       
   402 //
       
   403 void CCertificateStoreDatabase::GetCsDataL(
       
   404 	ec_cs_data_type_e aDataType,
       
   405     HBufC8** aOutColumnValue,
       
   406 	RArray<SWapiCertEntry>& aArray,
       
   407 	TBool aGetAll)
       
   408 
       
   409 	{
       
   410     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   411         "CCertificateStoreDatabase::GetCsDataL() IN\n" ) ) );
       
   412 
       
   413     aArray.Reset();
       
   414     
       
   415     // There could be a case where CS DB is destroyed and UI
       
   416     // calls this function. We just return in that case.
       
   417     if ( !iCsDbCreated )
       
   418         {
       
   419         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   420             "ERROR: CCertificateStoreDatabase::GetCsDataL() \
       
   421             CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   422         return;
       
   423         }
       
   424 
       
   425     if ( !iCsSessionOpened )
       
   426 		{
       
   427 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   428             "ERROR: CCertificateStoreDatabase::GetCsDataL() CS not opened.\n" ) ) );
       
   429 		User::Leave( KErrSessionClosed );
       
   430 		}			
       
   431 
       
   432     // DB names
       
   433 	TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   434 	TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   435 
       
   436 	GetDbNamesFromDataTypeL( aDataType, tableName, dataColumnName );
       
   437 
       
   438   	// create SQL query statement
       
   439 	HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
   440 	TPtr sqlStatement = buf->Des();
       
   441 
       
   442 	_LIT( KSqlQuery, "SELECT %S FROM %S" );
       
   443     _LIT( KSqlQueryAll, "SELECT * FROM %S" );
       
   444 	if (aGetAll)
       
   445 	    sqlStatement.Format( KSqlQueryAll, &tableName );
       
   446 	else
       
   447 	    sqlStatement.Format( KSqlQuery, &dataColumnName, &tableName );
       
   448 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   449 		"wapi_am_core_symbian_c::GetCsDataL() sqlStatement",
       
   450 		sqlStatement.Ptr(), 
       
   451 		sqlStatement.Size() ) );	
       
   452 
       
   453 		
       
   454  	RDbView view;
       
   455 	User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
   456 		TDbWindow::EUnlimited, RDbView::EReadOnly ) );
       
   457     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   458         "CCertificateStoreDatabase::GetCsDataL() View prepared OK.\n" ) ) );
       
   459 	CleanupClosePushL( view );
       
   460 	User::LeaveIfError( view.EvaluateAll() );
       
   461     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   462         "CCertificateStoreDatabase::GetCsDataL() View evaluated OK.\n" ) ) );    
       
   463     
       
   464  	if ( view.FirstL() && aGetAll == EFalse)
       
   465 	    {
       
   466 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   467 			"CCertificateStoreDatabase::GetCsDataL() view.FirstL() OK.\n" ) ) );
       
   468 		
       
   469 		view.GetL();		
       
   470 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   471 			"CCertificateStoreDatabase::GetCsDataL() view.GetL() OK.\n" ) ) );
       
   472 
       
   473 		switch ( view.ColType( KDefaultColumnNumberOne ) )
       
   474 		    {
       
   475 		    case EDbColText:				
       
   476 			    {
       
   477                 EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   478                     "CCertificateStoreDatabase::GetCsDataL() \
       
   479                     EDbColText.\n" ) ) );            
       
   480 			    // Buffer for unicode parameter
       
   481                 HBufC* unicodebuf = HBufC::NewLC(view.ColLength( KDefaultColumnNumberOne ));
       
   482 			    TPtr unicodeString = unicodebuf->Des();
       
   483                 unicodeString = view.ColDes(KDefaultColumnNumberOne);
       
   484                 // Convert to 8-bit
       
   485                 if (unicodeString.Size() > 0)
       
   486                     {
       
   487                     *aOutColumnValue = HBufC8::NewLC(
       
   488                         view.ColLength( KDefaultColumnNumberOne ) ); // Buffer for the data.
       
   489                     TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
   490                     outColumnValuePtr8.Copy(unicodeString);
       
   491                      if (outColumnValuePtr8.Size() == 0)
       
   492                     {
       
   493                         User::Leave(KErrNoMemory);
       
   494                     }
       
   495                      CleanupStack::Pop( *aOutColumnValue );
       
   496 
       
   497                 } 
       
   498                 else 
       
   499                     {
       
   500                         // Empty field. Do nothing...data remains invalid
       
   501                      }
       
   502                 CleanupStack::PopAndDestroy(unicodebuf);
       
   503 
       
   504                 break;
       
   505 			    }
       
   506 	         case EDbColUint32:
       
   507 	                {
       
   508 	                EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   509 	                    "CCertificateStoreDatabase::GetCsDataL() \
       
   510 	                    EDbColUint32.\n" ) ) );            
       
   511 	                    TUint value;
       
   512 	                    value = view.ColUint32(KDefaultColumnNumberOne);
       
   513 	                    *aOutColumnValue = HBufC8::NewLC(
       
   514 	                        view.ColLength( KDefaultColumnNumberOne ) ); // Buffer for the data.
       
   515 	                    TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
   516 	                    outColumnValuePtr8.Copy((const unsigned char *)&value, sizeof(TUint));
       
   517 	                    if (outColumnValuePtr8.Size() == 0)
       
   518 	                    {
       
   519 	                        User::Leave(KErrNoMemory);
       
   520 	                    }
       
   521 	                    CleanupStack::Pop( *aOutColumnValue );
       
   522 	                    if (outColumnValuePtr8.Size() == 0)
       
   523 	                    {
       
   524 	                        User::Leave(KErrNoMemory);
       
   525 	                    }
       
   526 	                }
       
   527 	                break;
       
   528 
       
   529 		    case EDbColBinary:
       
   530 			    {
       
   531 		        EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   532 					"CCertificateStoreDatabase::GetCsDataL() \
       
   533 					Binary column.\n" ) ) );			
       
   534 				GetBinaryDataL( view, aOutColumnValue );
       
   535                 break;
       
   536 			    }
       
   537 		    case EDbColLongBinary:				
       
   538 			    {
       
   539                 EAP_TRACE_DEBUG_SYMBIAN(
       
   540                     (_L("CCertificateStoreDatabase::GetCsDataL - Long Binary column\n")));
       
   541             
       
   542                 GetLongBinaryDataL( view, aOutColumnValue );
       
   543                 break;
       
   544 			    }			
       
   545 		    default:
       
   546 		    	{
       
   547 		        EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   548 			    	"ERROR: CCertificateStoreDatabase::GetCsDataL() \
       
   549 			    	Unsupported DB field:%d\n" ),
       
   550 				    view.ColType( KDefaultColumnNumberOne ) ) );	
       
   551 			    User::Leave( KErrNotSupported );
       
   552 			    break;
       
   553 		    	}
       
   554 		    } // switch ( view.ColType( KDefaultColumnNumberOne ) )
       
   555 	    } // if ( view.FirstL() )
       
   556  	else
       
   557         {
       
   558         if (view.FirstL())
       
   559             {
       
   560             GetTableDataL(view, aArray);       
       
   561             }
       
   562         }
       
   563 	// clean memory
       
   564 	CleanupStack::PopAndDestroy( &view );
       
   565     CleanupStack::PopAndDestroy( buf );
       
   566 
       
   567     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   568         "CCertificateStoreDatabase::GetCsDataL() OUT\n" ) ) );
       
   569 
       
   570 	} // CCertificateStoreDatabase::GetCsDataL()
       
   571 
       
   572 
       
   573 // ---------------------------------------------------------
       
   574 // CCertificateStoreDatabase::SetCsDataByReferenceL()
       
   575 // ---------------------------------------------------------
       
   576 //
       
   577 void CCertificateStoreDatabase::SetCsDataByReferenceL(
       
   578     ec_cs_data_type_e aDataType,
       
   579 	const TDesC8& aColumnValue,
       
   580 	const TDesC8& aDataReference,
       
   581 	const TBool aIsNewEntry )
       
   582 	{
       
   583     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   584         "CCertificateStoreDatabase::SetCsDataByReferenceL1() IN\n" ) ) );
       
   585 	
       
   586     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   587         "wapi_am_core_symbian_c::SetCsDataByReferenceL1() Reference",
       
   588         aDataReference.Ptr(), 
       
   589         aDataReference.Size() ) );
       
   590     
       
   591 	  // Convert the received reference id into integer.. 
       
   592     TUint intRef = eap_read_u32_t_network_order(
       
   593             aDataReference.Ptr(),
       
   594             aDataReference.Size());
       
   595     
       
   596     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
   597             ( "CCertificateStoreDatabase::SetCsDataByReferenceL1() \
       
   598             New entry: reference set to DB(TEXT)=%d\n" ), intRef ) );
       
   599     SetCsDataByReferenceL ( aDataType, aColumnValue, intRef, aIsNewEntry );
       
   600 		
       
   601     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   602         "CCertificateStoreDatabase::SetCsDataByReferenceL1() OUT\n" ) ) );
       
   603 
       
   604 	} // CCertificateStoreDatabase::SetCsDataByReferenceL()
       
   605 
       
   606 
       
   607 // ---------------------------------------------------------
       
   608 // CCertificateStoreDatabase::SetCsDataByReferenceL()
       
   609 // ---------------------------------------------------------
       
   610 //
       
   611 void CCertificateStoreDatabase::SetCsDataByReferenceL(
       
   612     ec_cs_data_type_e aDataType,
       
   613     const TDesC8& aColumnValue,
       
   614     const TUint aDataReference,
       
   615     const TBool aIsNewEntry )
       
   616     {
       
   617     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   618         "CCertificateStoreDatabase::SetCsDataByReferenceL() IN\n" ) ) );
       
   619     
       
   620     // There could be a case where CS DB is destroyed and UI
       
   621     // calls this function. We just return in that case.
       
   622     if ( !iCsDbCreated )
       
   623         {
       
   624         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   625             "ERROR: CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   626             CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   627         return;
       
   628         }
       
   629     
       
   630     if ( !iCsSessionOpened )
       
   631         {
       
   632         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   633             "ERROR: CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   634             CS not opened.\n" ) ) );
       
   635         User::Leave( KErrSessionClosed );
       
   636         }           
       
   637     
       
   638     if ( aColumnValue.Size() <= 0 )
       
   639         {
       
   640         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   641             "ERROR: CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   642             Column value is negative!\n" ) ) );
       
   643         User::Leave( KErrArgument );
       
   644         }
       
   645     
       
   646     // DB names
       
   647     TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   648     TBuf<KDbMaxColName> referenceColumnName;  // const from d32dbms.h
       
   649     TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   650     RDbView::TAccess dbMode = RDbView::EUpdatable;       
       
   651     
       
   652     GetDbNamesFromDataTypeL( aDataType, tableName, referenceColumnName, 
       
   653                             dataColumnName );
       
   654 
       
   655     // create SQL query statement
       
   656     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
   657     TPtr sqlStatement = buf->Des();
       
   658     
       
   659     _LIT( KSqlQueryInsert, "SELECT * FROM %S" );
       
   660     _LIT( KSqlQueryWithRef, "SELECT %S FROM %S WHERE %S=%d" );
       
   661 
       
   662     if( aIsNewEntry )
       
   663         {
       
   664         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   665              "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   666              New Entry.\n" ) ) );
       
   667        dbMode = RDbView::EInsertOnly;         
       
   668         sqlStatement.Format( KSqlQueryInsert, &tableName );         
       
   669         }
       
   670     else
       
   671         {
       
   672         sqlStatement.Format( KSqlQueryWithRef,
       
   673                              &dataColumnName,
       
   674                              &tableName,
       
   675                              &referenceColumnName,
       
   676                              &aDataReference );
       
   677         }
       
   678 
       
   679     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   680         "wapi_am_core_symbian_c::SetCsDataByReferenceL() sqlStatement",
       
   681         sqlStatement.Ptr(), 
       
   682         sqlStatement.Size() ) );        
       
   683     
       
   684      RDbView view;   
       
   685     User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
   686         TDbWindow::EUnlimited, dbMode ) );   
       
   687     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   688         "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   689         View prepared OK.\n" ) ) );
       
   690     
       
   691     CleanupClosePushL( view );
       
   692     User::LeaveIfError( view.EvaluateAll() );
       
   693     
       
   694     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   695         "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   696         View evaluated OK.\n" ) ) );
       
   697 
       
   698     if ( aIsNewEntry && ( aDataReference > 0 ) )
       
   699         {       
       
   700         InsertDataAndReferenceL( view, referenceColumnName,
       
   701             dataColumnName, aDataReference, aColumnValue );     
       
   702         } // if ( aIsNewEntry && ...
       
   703     else
       
   704         {
       
   705         UpdateColOneRowOneL( view, aColumnValue );      
       
   706         }
       
   707 
       
   708     view.PutL();    
       
   709     
       
   710     // clean
       
   711     CleanupStack::PopAndDestroy( &view );   
       
   712     CleanupStack::PopAndDestroy( buf );
       
   713         
       
   714     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   715         "CCertificateStoreDatabase::SetCsDataByReferenceL() OUT\n" ) ) );
       
   716 
       
   717     } // CCertificateStoreDatabase::SetCsDataByReferenceL()
       
   718     
       
   719     
       
   720 
       
   721 // ---------------------------------------------------------
       
   722 // CCertificateStoreDatabase::SetCsDataL()
       
   723 // ---------------------------------------------------------
       
   724 //
       
   725 void CCertificateStoreDatabase::SetCsDataL(
       
   726     ec_cs_data_type_e aDataType,
       
   727 	const TDesC8& aColumnValue,
       
   728 	const TBool aIsNewEntry )
       
   729 	{
       
   730     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   731         "CCertificateStoreDatabase::SetCsDataL() IN\n" ) ) );
       
   732     
       
   733     // There could be a case where CS DB is destroyed and UI
       
   734     // calls this function. We just return in that case.
       
   735     if ( !iCsDbCreated )
       
   736         {
       
   737         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   738             "ERROR: CCertificateStoreDatabase::SetCsDataL() \
       
   739             CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   740         return;
       
   741         }
       
   742 
       
   743     if ( !iCsSessionOpened )
       
   744 		{
       
   745 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   746             "ERROR: CCertificateStoreDatabase::SetCsDataL() \
       
   747             CS not opened.\n" ) ) );
       
   748 		User::Leave( KErrSessionClosed );
       
   749 		}			
       
   750 
       
   751 	if ( aColumnValue.Size() <= 0 )
       
   752 		{
       
   753 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   754 			"ERROR: CCertificateStoreDatabase::SetCsDataL() \
       
   755 			Column value is empty!\n" ) ) );
       
   756 		User::Leave( KErrArgument );
       
   757 		}
       
   758 
       
   759     // DB names
       
   760 	TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   761 	TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   762 	RDbView::TAccess dbMode = RDbView::EUpdatable;		
       
   763 	
       
   764 	GetDbNamesFromDataTypeL( aDataType, tableName, dataColumnName );
       
   765 
       
   766 	// create SQL query statement
       
   767 	HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
   768 	TPtr sqlStatement = buf->Des();
       
   769 	
       
   770 	_LIT( KSqlQuery, "SELECT %S FROM %S" );
       
   771 	_LIT( KSqlQueryInsert, "SELECT * FROM %S" );
       
   772 
       
   773 	if( aIsNewEntry )
       
   774 		{
       
   775 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   776 	        "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   777 	        New Entry.\n" ) ) );
       
   778 		dbMode = RDbView::EInsertOnly;			
       
   779 		sqlStatement.Format( KSqlQueryInsert, &tableName );			
       
   780 		}
       
   781 	else
       
   782 		{
       
   783 		sqlStatement.Format( KSqlQuery,
       
   784 			                 &dataColumnName,
       
   785 			                 &tableName );
       
   786 		}
       
   787 
       
   788 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   789 		"wapi_am_core_symbian_c::SetCsDataL() sqlStatement",
       
   790 		sqlStatement.Ptr(), 
       
   791 		sqlStatement.Size() ) );	
       
   792 	
       
   793 	RDbView view;	
       
   794 	User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
   795 		TDbWindow::EUnlimited, dbMode ) );   
       
   796     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   797         "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   798         View prepared OK.\n" ) ) );
       
   799 	CleanupClosePushL( view );
       
   800 	User::LeaveIfError( view.EvaluateAll() );
       
   801     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   802         "CCertificateStoreDatabase::SetCsDataByReferenceL() \
       
   803         View evaluated OK.\n" ) ) );
       
   804 	
       
   805 	if ( aIsNewEntry )
       
   806 		{
       
   807 		InsertDataL( view, dataColumnName, aColumnValue );		
       
   808 	    }
       
   809 	else
       
   810 	    {
       
   811 	    UpdateColOneRowOneL( view, aColumnValue );		
       
   812 	    }
       
   813 	
       
   814 	view.PutL();	
       
   815 	
       
   816 	// clean
       
   817 	CleanupStack::PopAndDestroy( &view );	
       
   818 	CleanupStack::PopAndDestroy( buf );
       
   819     
       
   820     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   821         "CCertificateStoreDatabase::SetCsDataL() OUT\n" ) ) );
       
   822 
       
   823 	} // CCertificateStoreDatabase::SetCsDataL()
       
   824 
       
   825 
       
   826 // ---------------------------------------------------------
       
   827 // CCertificateStoreDatabase::RemoveCsDataByReferenceL()
       
   828 // ---------------------------------------------------------
       
   829 //
       
   830 void CCertificateStoreDatabase::RemoveCsDataByReferenceL(
       
   831 	ec_cs_data_type_e aDataType,
       
   832     const TDesC8& aColumnValue,
       
   833     const TDesC8& aDataReference )
       
   834 	{
       
   835     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   836         "CCertificateStoreDatabase::RemoveCsDataByReferenceL1() IN\n" ) ) );
       
   837 
       
   838     // Convert the received reference id into integer.. 
       
   839     TUint intRef = eap_read_u32_t_network_order(
       
   840             aDataReference.Ptr(),
       
   841             aDataReference.Size());
       
   842     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
   843             ( "CCertificateStoreDatabase::RemoveCsDataByReferenceL1() \
       
   844             New entry: reference set to DB(TEXT)=%d\n" ), intRef ) );
       
   845 
       
   846     RemoveCsDataByReferenceL ( aDataType, aColumnValue, intRef );
       
   847 		
       
   848 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   849 	    "CCertificateStoreDatabase::RemoveCsDataByReferenceL1() OUT\n" ) ) );
       
   850 
       
   851 	} // CCertificateStoreDatabase::RemoveCsDataByReferenceL()
       
   852 
       
   853 
       
   854 // ---------------------------------------------------------
       
   855 // CCertificateStoreDatabase::RemoveCsDataByReferenceL()
       
   856 // ---------------------------------------------------------
       
   857 //
       
   858 void CCertificateStoreDatabase::RemoveCsDataByReferenceL(
       
   859     ec_cs_data_type_e aDataType,
       
   860     const TDesC8& aColumnValue,
       
   861     const TUint aDataReference )
       
   862     {
       
   863     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   864         "CCertificateStoreDatabase::RemoveCsDataByReferenceL() IN\n" ) ) );
       
   865     
       
   866     // There could be a case where CS DB is destroyed and UI
       
   867     // calls this function. We just return in that case.
       
   868     if ( !iCsDbCreated )
       
   869         {
       
   870         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   871             "ERROR: CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   872             CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   873         return;
       
   874         }
       
   875 
       
   876     if ( !iCsSessionOpened )
       
   877         {
       
   878         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   879             "ERROR: CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   880             CS not opened.\n" ) ) );
       
   881         User::Leave( KErrSessionClosed );
       
   882         }           
       
   883 
       
   884     if ( aColumnValue.Size() <= 0 )
       
   885         {
       
   886         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   887             "ERROR: CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   888             Column value is empty!\n" ) ) );
       
   889         User::Leave( KErrArgument );
       
   890         }
       
   891         
       
   892     // DB names
       
   893     TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   894     TBuf<KDbMaxColName> referenceColumnName;  // const from d32dbms.h
       
   895     TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   896     RDbView::TAccess dbMode = RDbView::EUpdatable;      
       
   897     
       
   898     GetDbNamesFromDataTypeL( aDataType, tableName, referenceColumnName,
       
   899         dataColumnName );
       
   900     
       
   901     // create SQL query statement
       
   902     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
   903     TPtr sqlStatement = buf->Des();
       
   904     
       
   905     _LIT( KSqlQueryWithRef, "SELECT %S FROM %S WHERE %S=%d" );  
       
   906     sqlStatement.Format( KSqlQueryWithRef,
       
   907                          &dataColumnName,
       
   908                          &tableName,
       
   909                          &referenceColumnName,
       
   910                          &aDataReference );
       
   911     
       
   912     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
   913         "wapi_am_core_symbian_c::RemoveCsDataByReferenceL() sqlStatement",
       
   914         sqlStatement.Ptr(), 
       
   915         sqlStatement.Size() ) );        
       
   916     
       
   917     RDbView view;   
       
   918     User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
   919         TDbWindow::EUnlimited, dbMode ) );   
       
   920     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   921         "CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   922         View prepared OK.\n" ) ) );
       
   923     CleanupClosePushL( view );
       
   924     User::LeaveIfError( view.EvaluateAll() );
       
   925     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   926         "CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   927         View evaluated OK.\n" ) ) );
       
   928         
       
   929     if ( view.FirstL() )
       
   930         {
       
   931         view.DeleteL();
       
   932         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   933             "CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   934             View deleted OK.\n" ) ) );
       
   935         }
       
   936     else
       
   937         {
       
   938         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   939             "CCertificateStoreDatabase::RemoveCsDataByReferenceL() \
       
   940             No data found.\n" ) ) );
       
   941         }
       
   942     
       
   943     // clean
       
   944     CleanupStack::PopAndDestroy( &view );
       
   945     CleanupStack::PopAndDestroy( buf );
       
   946         
       
   947     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   948         "CCertificateStoreDatabase::RemoveCsDataByReferenceL() OUT\n" ) ) );
       
   949 
       
   950     } // CCertificateStoreDatabase::RemoveCsDataByReferenceL()
       
   951 
       
   952 
       
   953 // ---------------------------------------------------------
       
   954 // CCertificateStoreDatabase::RemoveCsDataL()
       
   955 // ---------------------------------------------------------
       
   956 //
       
   957 void CCertificateStoreDatabase::RemoveCsDataL(
       
   958 	ec_cs_data_type_e aDataType,
       
   959     const TDesC8& aColumnValue )
       
   960 	{
       
   961     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   962         "CCertificateStoreDatabase::RemoveCsDataL() IN\n" ) ) );
       
   963 
       
   964     // There could be a case where CS DB is destroyed and UI
       
   965     // calls this function. We just return in that case.
       
   966     if ( !iCsDbCreated )
       
   967         {
       
   968         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   969             "ERROR: CCertificateStoreDatabase::RemoveCsDataL() \
       
   970             CS DB doesn't exist. Don't do anything.\n" ) ) );
       
   971         return;
       
   972         }
       
   973 
       
   974     if ( !iCsSessionOpened )
       
   975 	    {
       
   976         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   977             "ERROR: CCertificateStoreDatabase::RemoveCsDataL() \
       
   978             CS not opened.\n" ) ) );
       
   979 	    User::Leave( KErrSessionClosed );
       
   980 	    }			
       
   981 
       
   982     if ( aColumnValue.Size() <= 0 )
       
   983 	    {
       
   984         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
   985 		    "ERROR: CCertificateStoreDatabase::RemoveCsDataL() \
       
   986 		    Column value is empty!\n" ) ) );
       
   987 	    User::Leave( KErrArgument );
       
   988 	    }
       
   989 	    
       
   990 	// DB names
       
   991 	TBuf<KDbMaxName> tableName;               // const from d32dbms.h
       
   992 	TBuf<KDbMaxColName> referenceColumnName;  // const from d32dbms.h
       
   993 	TBuf<KDbMaxColName> dataColumnName;       // const from d32dbms.h
       
   994 	RDbView::TAccess dbMode = RDbView::EUpdatable;	
       
   995 	
       
   996 	GetDbNamesFromDataTypeL( aDataType, tableName, referenceColumnName,
       
   997 		dataColumnName );
       
   998 	
       
   999 	// create SQL query statement
       
  1000 	HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1001 	TPtr sqlStatement = buf->Des();
       
  1002 	
       
  1003 	_LIT( KSqlQuery, "SELECT %S FROM %S" );
       
  1004 	sqlStatement.Format( KSqlQuery,
       
  1005 		                 &dataColumnName,
       
  1006 		                 &tableName );
       
  1007 	
       
  1008 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1009 		"wapi_am_core_symbian_c::RemoveCsDataL() sqlStatement",
       
  1010 		sqlStatement.Ptr(), 
       
  1011 		sqlStatement.Size() ) );		
       
  1012 	
       
  1013 	RDbView view;	
       
  1014 	User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
  1015 		TDbWindow::EUnlimited, dbMode ) );   
       
  1016 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1017 	    "CCertificateStoreDatabase::RemoveCsDataL() \
       
  1018 	    View prepared OK.\n" ) ) );
       
  1019 	CleanupClosePushL( view );
       
  1020 	User::LeaveIfError( view.EvaluateAll() );
       
  1021 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1022 	    "CCertificateStoreDatabase::RemoveCsDataL() \
       
  1023 	    View evaluated OK.\n" ) ) );
       
  1024 		
       
  1025 	if ( view.FirstL() )
       
  1026 		{
       
  1027 		view.DeleteL();
       
  1028 		EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1029 	        "CCertificateStoreDatabase::RemoveCsDataL() \
       
  1030 			View deleted OK.\n" ) ) );
       
  1031 		}
       
  1032 	else
       
  1033 		{
       
  1034 		EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1035 	        "CCertificateStoreDatabase::RemoveCsDataL() \
       
  1036             No data found.\n" ) ) );
       
  1037 		}
       
  1038 	
       
  1039 	// clean
       
  1040 	CleanupStack::PopAndDestroy( &view );	
       
  1041 	CleanupStack::PopAndDestroy( buf );
       
  1042 		
       
  1043 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1044 	    "CCertificateStoreDatabase::RemoveCsDataL() OUT\n" ) ) );
       
  1045 
       
  1046 	} // CCertificateStoreDatabase::RemoveCsDataL()
       
  1047 
       
  1048 
       
  1049 // ================= public: New, boolean conditions =======================
       
  1050 
       
  1051 
       
  1052 // ---------------------------------------------------------
       
  1053 // CCertificateStoreDatabase::IsInitializedL()
       
  1054 // ---------------------------------------------------------
       
  1055 //
       
  1056 TBool CCertificateStoreDatabase::IsInitializedL()
       
  1057 	{
       
  1058     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1059         "CCertificateStoreDatabase::IsInitializedL() IN\n" ) ) );
       
  1060 		
       
  1061     // There could be a case where CS DB is destroyed and UI
       
  1062     // calls this function. We return EFalse in that case.      
       
  1063     if ( !iCsDbCreated )
       
  1064         {
       
  1065         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1066             "CCertificateStoreDatabase::IsInitializedL() \
       
  1067             Certificate store DB doesn't exist. Returning EFalse." ) ) );   
       
  1068         return EFalse;
       
  1069         }
       
  1070 
       
  1071     if ( !iCsSessionOpened )
       
  1072 	    {
       
  1073 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1074 			"CCertificateStoreDatabase::IsInitializedL() \
       
  1075 			ERROR: certificate store not opened!" ) ) );
       
  1076 		return EFalse;
       
  1077 		}	
       
  1078 		
       
  1079 	TBool IsInitializedL( EFalse );		
       
  1080 				
       
  1081 	HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1082 	TPtr sqlStatement = buf->Des();
       
  1083 
       
  1084 	// Query only initialization flag field from general settings table.
       
  1085 	_LIT( KSqlQuery, "SELECT %S FROM %S" );
       
  1086 	sqlStatement.Format( KSqlQuery,
       
  1087 						 &KCsInitialized,
       
  1088 						 &KCsGeneralSettingsTableName );
       
  1089 
       
  1090 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1091 		"wapi_am_core_symbian_c::IsInitializedL() sqlStatement",
       
  1092 		sqlStatement.Ptr(), 
       
  1093 		sqlStatement.Size() ) );	
       
  1094 
       
  1095 	RDbView view;		
       
  1096 	User::LeaveIfError( view.Prepare( iCsDb, TDbQuery( sqlStatement ),
       
  1097 		TDbWindow::EUnlimited, RDbView::EReadOnly ) );
       
  1098     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1099         "CCertificateStoreDatabase::IsInitializedL() View prepared OK.\n" ) ) );
       
  1100 	
       
  1101 	CleanupStack::PopAndDestroy( buf );
       
  1102 	
       
  1103 	CleanupClosePushL( view );
       
  1104 		
       
  1105 	User::LeaveIfError( view.EvaluateAll() );
       
  1106     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1107         "CCertificateStoreDatabase::IsInitializedL() View evaluated OK.\n" ) ) );    
       
  1108 	
       
  1109 	if ( view.FirstL() )
       
  1110 	    {
       
  1111 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1112 		    "CCertificateStoreDatabase::IsInitializedL() view.FirstL() OK.\n" ) ) );
       
  1113 		view.GetL();
       
  1114 		
       
  1115 		if ( view.IsColNull( KDefaultColumnNumberOne ) )
       
  1116 			{
       
  1117 			IsInitializedL = EFalse;
       
  1118 			}
       
  1119 		else
       
  1120 			{
       
  1121 			// Store the line
       
  1122 			TUint initValue = view.ColUint( KDefaultColumnNumberOne );
       
  1123 			
       
  1124 			if ( initValue == ECertificateStoreInitialized )
       
  1125 				{
       
  1126 				IsInitializedL = ETrue;
       
  1127 				}
       
  1128 			else
       
  1129 				{
       
  1130 				IsInitializedL = EFalse;
       
  1131 				}
       
  1132 			
       
  1133 			}		
       
  1134 		}
       
  1135 	else
       
  1136 	    {
       
  1137 		// Nothing in the view means there is no entry at all.
       
  1138 		IsInitializedL = EFalse;
       
  1139 		}
       
  1140 	
       
  1141 	CleanupStack::PopAndDestroy( &view ); // Close view.		
       
  1142 
       
  1143 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1144 		"CCertificateStoreDatabase::IsInitializedL() \
       
  1145 		OUT, IsInitializedL=%d.\n" ), IsInitializedL ) );
       
  1146 	
       
  1147 	return IsInitializedL;	
       
  1148 	} // CCertificateStoreDatabase::IsInitializedL()
       
  1149 
       
  1150 
       
  1151 // ================= private: Access =======================
       
  1152 
       
  1153 
       
  1154 // ---------------------------------------------------------
       
  1155 // CCertificateStoreDatabase::GetCertificateStoreDb()
       
  1156 // ---------------------------------------------------------
       
  1157 //
       
  1158 RDbNamedDatabase& CCertificateStoreDatabase::GetCertificateStoreDb()
       
  1159     {
       
  1160 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1161         "CCertificateStoreDatabase::GetCertificateStoreDb() IN\n" ) ) );
       
  1162 
       
  1163 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1164         "CCertificateStoreDatabase::GetCertificateStoreDb() OUT\n" ) ) );
       
  1165 	return iCsDb; 
       
  1166     }
       
  1167 
       
  1168 
       
  1169 // ================= private: New, database, tables =======================
       
  1170 
       
  1171 
       
  1172 // ---------------------------------------------------------
       
  1173 // CCertificateStoreDatabase::CreateCertificateStoreL()
       
  1174 // ---------------------------------------------------------
       
  1175 //
       
  1176 void CCertificateStoreDatabase::CreateCertificateStoreL()
       
  1177     {
       
  1178 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1179         "CCertificateStoreDatabase::CreateCertificateStoreL() IN\n" ) ) );
       
  1180 
       
  1181     // 1. Open/create a database	
       
  1182     CreateDatabaseL();
       
  1183 
       
  1184     // 2. Create CS tables to database (ignore error if tables exist)	
       
  1185 
       
  1186     // Table 1: Create table for general settings. 
       
  1187     CreateGeneralSettingsTableL();
       
  1188 
       
  1189     // Table 2: Create table for client ASU-ID list
       
  1190     CreateClientAsuIdListTableL();
       
  1191 
       
  1192     // Table 3: Create table for CA ASU-ID list
       
  1193     CreateCaAsuIdListTableL();
       
  1194 
       
  1195     // Table 4: Create table for client certificates
       
  1196     CreateClientCertificateTableL();
       
  1197 
       
  1198     // Table 5: Create table for CA certificates
       
  1199     CreateCaCertificateTableL();
       
  1200 
       
  1201     // Table 6: Create table for private keys
       
  1202     CreatePrivateKeyTableL();
       
  1203 
       
  1204     // Table 7: Create table for WAPI certificate labels
       
  1205     CreateWapiCertLabeltableL();
       
  1206 
       
  1207     // Table 8: Create table for WAPI certificate files
       
  1208     CreateWapiCertFiletableL();
       
  1209 
       
  1210     
       
  1211     iCsDbCreated = ETrue;
       
  1212     
       
  1213     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1214         "CCertificateStoreDatabase::CreateCertificateStoreL() OUT\n" ) ) );    
       
  1215     
       
  1216     } // CCertificateStoreDatabase::CreateCertificateStoreL()
       
  1217 
       
  1218 
       
  1219 // ---------------------------------------------------------
       
  1220 // CCertificateStoreDatabase::CreateDatabaseL()
       
  1221 // ---------------------------------------------------------
       
  1222 //
       
  1223 void CCertificateStoreDatabase::CreateDatabaseL()
       
  1224 	{
       
  1225     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1226         "CCertificateStoreDatabase::CreateDatabaseL() IN\n" ) ) );    
       
  1227 
       
  1228 	// Connect to the DBMS server, if not connected already.
       
  1229 	if ( !iCsSessionOpened )
       
  1230 	    {
       
  1231 		User::LeaveIfError( iCsDbSession.Connect() );
       
  1232 		iCsSessionOpened = ETrue;
       
  1233 	    }		
       
  1234 	
       
  1235 	// Create the secure shared database with the specified secure policy.
       
  1236 	// Database will be created in the data caging path for DBMS (C:\private\100012a5).
       
  1237 	TInt err = iCsDb.Create( iCsDbSession, KCsDatabaseName,
       
  1238 		KSecureUidFormatCertificate );
       
  1239 	DEBUG1( "CCertificateStoreDatabase::CreateDatabaseL() Created secure DB for \
       
  1240 		certificatestore.dat, err=%d (-11=DB already exist).", err );	
       
  1241 	if ( err == KErrNone )
       
  1242 	    {
       
  1243 		iCsDb.Close();
       
  1244 	    }
       
  1245 	else if ( err != KErrAlreadyExists ) 
       
  1246 	    {
       
  1247 		User::LeaveIfError( err );
       
  1248 	    }
       
  1249 	User::LeaveIfError( iCsDb.Open( iCsDbSession, KCsDatabaseName,
       
  1250 		KSecureUidFormatCertificate ) );
       
  1251 
       
  1252     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1253         "CCertificateStoreDatabase::CreateDatabaseL() OUT\n" ) ) );    
       
  1254 	
       
  1255 	} // CCertificateStoreDatabase::CreateDatabaseL()
       
  1256 
       
  1257 
       
  1258 // ---------------------------------------------------------
       
  1259 // CCertificateStoreDatabase::CreateGeneralSettingsTableL()
       
  1260 // ---------------------------------------------------------
       
  1261 //
       
  1262 void CCertificateStoreDatabase::CreateGeneralSettingsTableL()
       
  1263 	{
       
  1264     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1265         "CCertificateStoreDatabase::CreateGeneralSettingsTableL() IN\n" ) ) );
       
  1266 
       
  1267     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1268     TPtr sqlStatement = buf->Des();
       
  1269 
       
  1270     // Table columns:
       
  1271     ////// NAME //////////////////////////// TYPE ////////////// Constant ////////////////////////
       
  1272     //| CS_password						     | VARBINARY(255)	| KCsPassword                  |//
       
  1273     //| CS_reference_counter			     | VARBINARY(255)	| KCsReferenceCounter	   	   |//
       
  1274     //| CS_master_key					     | VARBINARY(255)	| KCsMasterKey	   	           |//
       
  1275     //| CS_initialized					     | UNSIGNED INTEGER | KCsInitialized	   	       |//
       
  1276     //| CS_password_max_validity_time        | BIGINT	   	    | KCsPasswordMaxValidityTime   |//
       
  1277     //| CS_password_last_identity_time	     | BIGINT	   		| KCsLastPasswordIdentityTime  |//	    
       
  1278     //////////////////////////////////////////////////////////////////////////////////////////////
       
  1279 
       
  1280     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1281 	    %S VARBINARY(%d),     \
       
  1282 	    %S VARBINARY(%d),	  \
       
  1283         %S VARBINARY(%d),	  \
       
  1284 	    %S UNSIGNED INTEGER,  \
       
  1285 	    %S BIGINT,            \
       
  1286 	    %S BIGINT)" );
       
  1287 									 
       
  1288     sqlStatement.Format(
       
  1289         KSqlCreateTable, &KCsGeneralSettingsTableName,
       
  1290         &KCsPassword, KCsMaxPasswordLengthInDb,
       
  1291         &KCsReferenceCounter, KCsMaxRefCounterLengthInDb,
       
  1292         &KCsMasterKey, KCsMaxMasterKeyLengthInDb,
       
  1293         &KCsInitialized,
       
  1294         &KCsPasswordMaxValidityTime,
       
  1295         &KCsLastPasswordIdentityTime,
       
  1296         &KCsPrivateKeyAsuIdReference);
       
  1297     
       
  1298 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1299 		"wapi_am_core_symbian_c::CreateGeneralSettingsTableL() sqlStatement",
       
  1300 		sqlStatement.Ptr(), 
       
  1301 		sqlStatement.Size() ) );
       
  1302 
       
  1303     TInt err = iCsDb.Execute( sqlStatement );
       
  1304     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1305         {
       
  1306         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1307             "ERROR: CCertificateStoreDatabase::CreateGeneralSettingsTableL() \
       
  1308             iCsDb.Execute(), err=%d.\n" ), err ) );
       
  1309         CleanupStack::PopAndDestroy( buf );
       
  1310         User::Leave( err );
       
  1311         }
       
  1312     CleanupStack::PopAndDestroy( buf );
       
  1313     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1314         "CCertificateStoreDatabase::CreateGeneralSettingsTableL() OUT\n" ) ) );
       
  1315     
       
  1316 	} // CCertificateStoreDatabase::CreateGeneralSettingsTableL()
       
  1317 
       
  1318 
       
  1319 // ---------------------------------------------------------
       
  1320 // CCertificateStoreDatabase::CreateClientAsuIdListTableL()
       
  1321 // ---------------------------------------------------------
       
  1322 //
       
  1323 void CCertificateStoreDatabase::CreateClientAsuIdListTableL()
       
  1324 	{
       
  1325     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1326         "CCertificateStoreDatabase::CreateClientAsuIdListTableL() IN\n" ) ) );
       
  1327 
       
  1328     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1329     TPtr sqlStatement = buf->Des();
       
  1330 
       
  1331     // Table columns:
       
  1332     ////// NAME /////////////////////// TYPE ///////////// Constant ////////////////////
       
  1333     //| CS_client_ASU_ID_reference    | UNSIGNED INTEGER   | KCsClientAsuIdReference |//		
       
  1334     //| CS_client_ASU_ID_data         | LONG VARBINARY	   | KCsClientAsuIdData      |//	
       
  1335     ////////////////////////////////////////////////////////////////////////////////////
       
  1336     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1337     	%S UNSIGNED INTEGER, \
       
  1338         %S LONG VARBINARY)" );
       
  1339 
       
  1340     sqlStatement.Format( KSqlCreateTable, &KCsClientAsuIdListTableName, 
       
  1341     	&KCsClientAsuIdReference, &KCsClientAsuIdData );
       
  1342     
       
  1343 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1344 		"wapi_am_core_symbian_c::CreateClientAsuIdListTableL() sqlStatement",
       
  1345 		sqlStatement.Ptr(), 
       
  1346 		sqlStatement.Size() ) );
       
  1347     
       
  1348     TInt err = iCsDb.Execute( sqlStatement );
       
  1349     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1350         {
       
  1351         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1352             "ERROR: CCertificateStoreDatabase::CreateClientAsuIdListTableL() \
       
  1353             iCsDb.Execute(), err=%d" ), err ) );
       
  1354         CleanupStack::PopAndDestroy( buf );
       
  1355 	    User::Leave( err );
       
  1356         }
       
  1357     CleanupStack::PopAndDestroy( buf );
       
  1358 
       
  1359     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1360         "CCertificateStoreDatabase::CreateClientAsuIdListTableL() OUT\n" ) ) );
       
  1361 
       
  1362 	} // CCertificateStoreDatabase::CreateClientAsuIdListTableL()
       
  1363 
       
  1364 
       
  1365 // ---------------------------------------------------------
       
  1366 // CCertificateStoreDatabase::CreateCaAsuIdListTableL()
       
  1367 // ---------------------------------------------------------
       
  1368 //
       
  1369 void CCertificateStoreDatabase::CreateCaAsuIdListTableL()
       
  1370     {
       
  1371     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1372         "CCertificateStoreDatabase::CreateCaAsuIdListTableL() IN\n" ) ) );
       
  1373     
       
  1374     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1375     TPtr sqlStatement = buf->Des();
       
  1376 
       
  1377     // Table columns:
       
  1378     ////// NAME //////////// TYPE ///////////// Constant ///////////////////
       
  1379     //| CS_CA_ASU_ID_reference  | UNSIGNED INTEGER)| KCsCaAsuIdReference |//	
       
  1380     //| CS_CA_ASU_ID_data       | LONG VARBINARY   | KCsCaAsuIdData      |//		
       
  1381     ////////////////////////////////////////////////////////////////////////
       
  1382     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1383     	%S UNSIGNED INTEGER, \
       
  1384     	%S LONG VARBINARY)" );
       
  1385 
       
  1386     sqlStatement.Format(
       
  1387     	KSqlCreateTable, &KCsCaAsuIdListTableName, 
       
  1388     	&KCsCaAsuIdReference,&KCsCaAsuIdData );
       
  1389 
       
  1390 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1391 		"wapi_am_core_symbian_c::CreateCaAsuIdListTableL() sqlStatement",
       
  1392 		sqlStatement.Ptr(), 
       
  1393 		sqlStatement.Size() ) );	
       
  1394 
       
  1395     TInt err = iCsDb.Execute( sqlStatement );
       
  1396     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1397         {
       
  1398         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1399             "ERROR: CCertificateStoreDatabase::CreateCaAsuIdListTableL() \
       
  1400         	iCsDb.Execute(), err=%d" ), err ) );
       
  1401         CleanupStack::PopAndDestroy( buf );
       
  1402 	    User::Leave( err );
       
  1403         }
       
  1404     CleanupStack::PopAndDestroy( buf );
       
  1405 
       
  1406     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1407         "CCertificateStoreDatabase::CreateCaAsuIdListTableL() OUT\n" ) ) );
       
  1408 
       
  1409     } // CCertificateStoreDatabase::CreateCaAsuIdListTableL()
       
  1410 
       
  1411 
       
  1412 // ---------------------------------------------------------
       
  1413 // CCertificateStoreDatabase::CreateClientCertificateTableL()
       
  1414 // ---------------------------------------------------------
       
  1415 //
       
  1416 void CCertificateStoreDatabase::CreateClientCertificateTableL()
       
  1417     {
       
  1418     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1419         "CCertificateStoreDatabase::CreateClientCertificateTableL() IN\n" ) ) );    
       
  1420     
       
  1421     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1422     TPtr sqlStatement = buf->Des();
       
  1423 
       
  1424     // Table columns:
       
  1425     ////// NAME //////////// TYPE ///////////// Constant ////////////////////////////////////
       
  1426     //| CS_client_cert_ASU_ID_reference   | UNSIGNED INTEGER| KCsClientCertAsuIdReference |//	
       
  1427     //| CS_client_cert_data               | LONG VARBINARY  | KCsClientCertData           |//		
       
  1428     /////////////////////////////////////////////////////////////////////////////////////////
       
  1429     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1430     	%S UNSIGNED INTEGER, \
       
  1431     	%S LONG VARBINARY)" );
       
  1432 
       
  1433     sqlStatement.Format(
       
  1434     	KSqlCreateTable, &KCsClientCertificateTable, 
       
  1435     	&KCsClientCertAsuIdReference, &KCsClientCertData );
       
  1436     
       
  1437 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1438 		"wapi_am_core_symbian_c::CreateClientCertificateTableL() sqlStatement",
       
  1439 		sqlStatement.Ptr(), 
       
  1440 		sqlStatement.Size() ) );	
       
  1441 
       
  1442     TInt err = iCsDb.Execute( sqlStatement );
       
  1443     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1444         {
       
  1445         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1446             "ERROR: CCertificateStoreDatabase::CreateClientCertificateTableL() \
       
  1447         	iCsDb.Execute(), err=%d" ), err ) );
       
  1448         CleanupStack::PopAndDestroy( buf );
       
  1449 	    User::Leave(err);
       
  1450         }
       
  1451     CleanupStack::PopAndDestroy( buf );
       
  1452 
       
  1453     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1454         "CCertificateStoreDatabase::CreateClientCertificateTableL() OUT\n" ) ) );
       
  1455 
       
  1456     } // CCertificateStoreDatabase::CreateClientCertificateTableL()
       
  1457 
       
  1458 
       
  1459 // ---------------------------------------------------------
       
  1460 // CCertificateStoreDatabase::CreateCaCertificateTableL()
       
  1461 // ---------------------------------------------------------
       
  1462 //
       
  1463 void CCertificateStoreDatabase::CreateCaCertificateTableL()
       
  1464     {
       
  1465     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1466         "CCertificateStoreDatabase::CreateCaCertificateTableL() IN\n" ) ) );    
       
  1467     
       
  1468     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1469     TPtr sqlStatement = buf->Des();
       
  1470 
       
  1471     // Table columns:
       
  1472     ////// NAME //////////// TYPE ///////////// Constant ////////////////////////////
       
  1473     //| CS_client_cert_ASU_ID_reference   | UNSIGNED INTEGER| KCsClientCertAsuIdReference |//   
       
  1474     //| CS_CA_cert_data               | LONG VARBINARY  | KCsCaCertData           |//		
       
  1475     /////////////////////////////////////////////////////////////////////////////////
       
  1476     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1477         %S UNSIGNED INTEGER, \
       
  1478     	%S LONG VARBINARY)" );
       
  1479 
       
  1480     sqlStatement.Format(
       
  1481     	KSqlCreateTable, &KCsCaCertificateTable, 
       
  1482         &KCsCaCertAsuIdReference, &KCsCaCertData );
       
  1483     
       
  1484 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1485 		"wapi_am_core_symbian_c::CreateCaCertificateTableL() sqlStatement",
       
  1486 		sqlStatement.Ptr(), 
       
  1487 		sqlStatement.Size() ) );	
       
  1488 
       
  1489     TInt err = iCsDb.Execute( sqlStatement );
       
  1490     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1491         {
       
  1492         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1493             "ERROR: CCertificateStoreDatabase::CreateCaCertificateTableL() \
       
  1494         	iCsDb.Execute(), err=%d" ), err ) );
       
  1495         CleanupStack::PopAndDestroy( buf );
       
  1496 	    User::Leave( err );
       
  1497         }
       
  1498     CleanupStack::PopAndDestroy( buf );
       
  1499     
       
  1500     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1501         "CCertificateStoreDatabase::CreateCaCertificateTableL() OUT\n" ) ) );
       
  1502 
       
  1503     } // CCertificateStoreDatabase::CreateCaCertificateTableL()
       
  1504 
       
  1505 
       
  1506 // ---------------------------------------------------------
       
  1507 // CCertificateStoreDatabase::CreatePrivateKeyTableL()
       
  1508 // ---------------------------------------------------------
       
  1509 //
       
  1510 void CCertificateStoreDatabase::CreatePrivateKeyTableL()
       
  1511     {
       
  1512     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1513         "CCertificateStoreDatabase::CreatePrivateKeyTableL() IN\n" ) ) );
       
  1514     
       
  1515     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1516     TPtr sqlStatement = buf->Des();
       
  1517 
       
  1518     // Table columns:
       
  1519     ////// NAME ///////////////////////// TYPE ///////////// Constant /////////////////////
       
  1520     //| CS_private_key_ASU_ID_reference | UNSIGNED INTEGER| KCsPrivateKeyAsuIdReference |//	
       
  1521     //| CS_private_key_data             | LONG VARBINARY  | KCsPrivateKeyData           |//		
       
  1522     ///////////////////////////////////////////////////////////////////////////////////////
       
  1523     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1524     	%S UNSIGNED INTEGER, \
       
  1525     	%S LONG VARBINARY)" );
       
  1526 
       
  1527     sqlStatement.Format(
       
  1528     	KSqlCreateTable, &KCsPrivateKeyTable, 
       
  1529     	&KCsPrivateKeyAsuIdReference, &KCsPrivateKeyData );
       
  1530     
       
  1531 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1532 		"wapi_am_core_symbian_c::CreatePrivateKeyTableL() sqlStatement",
       
  1533 		sqlStatement.Ptr(), 
       
  1534 		sqlStatement.Size() ) );	
       
  1535 
       
  1536     TInt err = iCsDb.Execute( sqlStatement );
       
  1537     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1538         {
       
  1539         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1540             "ERROR: CCertificateStoreDatabase::CreatePrivateKeyTableL() \
       
  1541         	iCsDb.Execute(), err=%d" ), err ) );
       
  1542         CleanupStack::PopAndDestroy( buf );
       
  1543 	    User::Leave( err );
       
  1544         }
       
  1545     CleanupStack::PopAndDestroy( buf );
       
  1546         
       
  1547     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1548         "CCertificateStoreDatabase::CreatePrivateKeyTableL() OUT\n" ) ) );
       
  1549     }
       
  1550 // ---------------------------------------------------------
       
  1551 // CCertificateStoreDatabase::CreateWapiCertLabeltableL()
       
  1552 // ---------------------------------------------------------
       
  1553 //
       
  1554 void CCertificateStoreDatabase::CreateWapiCertLabeltableL()
       
  1555     {
       
  1556     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1557         "CCertificateStoreDatabase::CreateWapiCertLabeltableL() IN\n" ) ) );
       
  1558     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1559     TPtr sqlStatement = buf->Des();
       
  1560 
       
  1561     // Table columns:
       
  1562     ////// NAME ///////////////////////// TYPE ///////////// Constant //////////////////
       
  1563     //| wapi_cs_cert_ASU_ID_reference | UNSIGNED INTEGER  |KCsCertLabelAsuIdReference|//	
       
  1564     //| CS_CA_cert_label              | LONG VARBINARY    | KCsCACertLabel           |//		
       
  1565     //| CS_user_cert_label            | LONG VARBINARY 	  | KCsUserCertLabel         |//
       
  1566     ////////////////////////////////////////////////////////////////////////////////////
       
  1567 
       
  1568     _LIT( KSqlCreateTable, "CREATE TABLE %S (\
       
  1569         %S UNSIGNED INTEGER, \
       
  1570     	%S LONG VARBINARY,	\
       
  1571 		%S LONG VARBINARY)");
       
  1572 
       
  1573     sqlStatement.Format( KSqlCreateTable, &KCsWapiCertLabelTable, 
       
  1574                         &KCsCertLabelAsuIdReference, 
       
  1575     					&KCsCACertLabel,
       
  1576     					&KCsUserCertLabel );
       
  1577 	 
       
  1578     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1579         "CCertificateStoreDatabase::CreateWapiCertLabeltableL() sqlStatement",
       
  1580         sqlStatement.Ptr(), 
       
  1581         sqlStatement.Size() ) );    
       
  1582  
       
  1583     TInt err = iCsDb.Execute( sqlStatement );
       
  1584     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1585         {
       
  1586         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1587             "ERROR: CCertificateStoreDatabase::CreateWapiCertLabeltableL() \
       
  1588         	iCsDb.Execute(), err=%d" ), err ) );
       
  1589             CleanupStack::PopAndDestroy( buf );
       
  1590 	    	User::Leave( err );
       
  1591         }
       
  1592     CleanupStack::PopAndDestroy( buf );
       
  1593         
       
  1594     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1595         "CCertificateStoreDatabase::CreateWapiCertLabeltableL() OUT\n" ) ) );
       
  1596 
       
  1597     } // CCertificateStoreDatabase::CreateWapiCertLabeltableL()
       
  1598 
       
  1599 
       
  1600 
       
  1601 // ---------------------------------------------------------
       
  1602 // CCertificateStoreDatabase::CreateWapiCertFiletableL()
       
  1603 // ---------------------------------------------------------
       
  1604 //
       
  1605 void CCertificateStoreDatabase::CreateWapiCertFiletableL()
       
  1606     {
       
  1607     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1608         "CCertificateStoreDatabase::CreateWapiCertFiletableL() IN\n" ) ) );
       
  1609     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  1610     TPtr sqlStatement = buf->Des();
       
  1611 
       
  1612     // Table columns:
       
  1613     ////// NAME ///////////////////////// TYPE ///////////// Constant /////////////////
       
  1614     //| CS_filename                   | VARBINARY    | KCsFileName                  |//    
       
  1615     ///////////////////////////////////////////////////////////////////////////////////
       
  1616 
       
  1617     _LIT( KSqlCreateTable, "CREATE TABLE %S (%S VARBINARY)");
       
  1618 
       
  1619     sqlStatement.Format( KSqlCreateTable, &KCsWapiCertFileTable, 
       
  1620                         &KCsFileName );
       
  1621      
       
  1622     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1623         "CCertificateStoreDatabase::CreateWapiCertFiletableL() sqlStatement",
       
  1624         sqlStatement.Ptr(), 
       
  1625         sqlStatement.Size() ) );    
       
  1626 
       
  1627     TInt err = iCsDb.Execute( sqlStatement );
       
  1628     if ( err != KErrNone && err != KErrAlreadyExists )
       
  1629         {
       
  1630         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1631             "ERROR: CCertificateStoreDatabase::CreateWapiCertFiletableL() \
       
  1632             iCsDb.Execute(), err=%d" ), err ) );
       
  1633             CleanupStack::PopAndDestroy( buf );
       
  1634             User::Leave( err );
       
  1635         }
       
  1636     CleanupStack::PopAndDestroy( buf );
       
  1637         
       
  1638     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1639         "CCertificateStoreDatabase::CreateWapiCertFiletableL() OUT\n" ) ) );
       
  1640 
       
  1641     } // CCertificateStoreDatabase::CreateWapiCertFiletableL()
       
  1642 
       
  1643 // ================= private:  Operations with view =======================
       
  1644 
       
  1645 
       
  1646 // ---------------------------------------------------------
       
  1647 // CCertificateStoreDatabase::GetLongBinaryDataL()
       
  1648 // ---------------------------------------------------------
       
  1649 //
       
  1650 void CCertificateStoreDatabase::GetLongBinaryDataL(
       
  1651 	RDbView& aView,	HBufC8** aOutColumnValue )
       
  1652 	{
       
  1653     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1654         "CCertificateStoreDatabase::GetLongBinaryDataL() IN\n" ) ) );
       
  1655 
       
  1656 	// Get the value from DB.
       
  1657 	*aOutColumnValue = HBufC8::NewLC(
       
  1658 		aView.ColLength( KDefaultColumnNumberOne ) ); // Buffer for the data.
       
  1659 	TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
  1660 	
       
  1661     RDbColReadStream readStream;
       
  1662 	readStream.OpenLC( aView, KDefaultColumnNumberOne );
       
  1663 	readStream.ReadL( outColumnValuePtr8, aView.ColLength( KDefaultColumnNumberOne ) );
       
  1664 	readStream.Close();
       
  1665 	CleanupStack::Pop( &readStream );
       
  1666 	
       
  1667 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1668 		"CCertificateStoreDatabase::GetLongBinaryDataL() LONG BINARY value from DB",
       
  1669 		outColumnValuePtr8.Ptr(), outColumnValuePtr8.Size() ) );
       
  1670 		
       
  1671 	CleanupStack::Pop( *aOutColumnValue );
       
  1672  
       
  1673     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1674         "CCertificateStoreDatabase::GetLongBinaryDataL() OUT\n" ) ) );
       
  1675     
       
  1676 	} // CCertificateStoreDatabase::GetLongBinaryDataL()
       
  1677 
       
  1678 
       
  1679 // ---------------------------------------------------------
       
  1680 // CCertificateStoreDatabase::GetBinaryDataL()
       
  1681 // ---------------------------------------------------------
       
  1682 //
       
  1683 void CCertificateStoreDatabase::GetBinaryDataL(
       
  1684 	RDbView& aView,	HBufC8** aOutColumnValue )
       
  1685 	{
       
  1686     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1687         "CCertificateStoreDatabase::GetBinaryDataL() IN\n" ) ) );
       
  1688 
       
  1689 	TPtrC8 dbValuePtrC8 = aView.ColDes8( KDefaultColumnNumberOne );
       
  1690 
       
  1691 	*aOutColumnValue = HBufC8::NewLC( dbValuePtrC8.Size() ); // Buffer for the data.
       
  1692 	TPtr8 outColumnValuePtr8 = ( *aOutColumnValue )->Des();
       
  1693 		
       
  1694 	outColumnValuePtr8.Copy( dbValuePtrC8 );
       
  1695 
       
  1696 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1697 		"CCertificateStoreDatabase::GetBinaryDataL() BINARY value from DB",
       
  1698 		outColumnValuePtr8.Ptr(), outColumnValuePtr8.Size() ) );
       
  1699 		
       
  1700 	CleanupStack::Pop( *aOutColumnValue );
       
  1701 				
       
  1702     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1703         "CCertificateStoreDatabase::GetBinaryDataL() OUT\n" ) ) );
       
  1704 
       
  1705 	} // CCertificateStoreDatabase::GetBinaryDataL()
       
  1706 
       
  1707 
       
  1708 // ---------------------------------------------------------
       
  1709 // CCertificateStoreDatabase::GetTableDataL()
       
  1710 // ---------------------------------------------------------
       
  1711 //
       
  1712 void CCertificateStoreDatabase::GetTableDataL( RDbView& aView, RArray<SWapiCertEntry>& aArray )
       
  1713 	{
       
  1714     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1715         "CCertificateStoreDatabase::GetTableDataL() IN\n" ) ) );
       
  1716 
       
  1717 	HBufC8* aOutColumnValue = NULL;
       
  1718 	
       
  1719 	if ( aView.FirstL())
       
  1720 	        {
       
  1721 	        do
       
  1722 	            {
       
  1723 	        
       
  1724 	            SWapiCertEntry aEntry;
       
  1725 	        
       
  1726                 aView.GetL();        
       
  1727                 EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1728                     "CCertificateStoreDatabase::GetTableDataL() aView.GetL() OK.\n" ) ) );
       
  1729             
       
  1730                 for (TInt aColNumber = 1; aColNumber<3 ; aColNumber++ )
       
  1731                     {
       
  1732                     switch ( aView.ColType( aColNumber ) )
       
  1733                         {
       
  1734                         case EDbColText:                
       
  1735                             {
       
  1736                             // Buffer for unicode parameter
       
  1737                             HBufC* unicodebuf = HBufC::NewLC(aView.ColLength( aColNumber ));
       
  1738                             TPtr unicodeString = unicodebuf->Des();
       
  1739                             unicodeString = aView.ColDes(aColNumber);
       
  1740                             // Convert to 8-bit
       
  1741                             if (unicodeString.Size() > 0)
       
  1742                                 {
       
  1743                                 aOutColumnValue = HBufC8::NewLC(
       
  1744                                     aView.ColLength( aColNumber ) ); // Buffer for the data.
       
  1745                                 TPtr8 outColumnValuePtr8 = ( aOutColumnValue )->Des();
       
  1746                                 outColumnValuePtr8.Copy(unicodeString);
       
  1747                                  if (outColumnValuePtr8.Size() == 0)
       
  1748                                 {
       
  1749                                     User::Leave(KErrNoMemory);
       
  1750                                 }
       
  1751                                  CleanupStack::Pop( aOutColumnValue );
       
  1752                 
       
  1753                             } 
       
  1754                             else 
       
  1755                                 {
       
  1756                                     // Empty field. Do nothing...data remains invalid
       
  1757                                 }
       
  1758                             CleanupStack::PopAndDestroy(unicodebuf);
       
  1759                             if(aColNumber == 1)
       
  1760                                 aEntry.iReference = aOutColumnValue;
       
  1761                             else
       
  1762                                 aEntry.iData = aOutColumnValue;
       
  1763                
       
  1764                             break;
       
  1765                             }
       
  1766                          case EDbColUint32:
       
  1767                                 {
       
  1768                                     TUint value;
       
  1769                                     value = eap_htonl(aView.ColUint32(aColNumber));
       
  1770                                     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1771                                         "CCertificateStoreDatabase::GetTableDataL() \
       
  1772                                         uint32 value=%d" ), value ) );
       
  1773 
       
  1774                                     aOutColumnValue = HBufC8::NewLC(
       
  1775                                         aView.ColLength( aColNumber ) ); // Buffer for the data.
       
  1776                                     TPtr8 outColumnValuePtr8 = ( aOutColumnValue )->Des();
       
  1777                                     outColumnValuePtr8.Copy((const unsigned char *)&value, sizeof(TUint));
       
  1778                                     if (outColumnValuePtr8.Size() == 0)
       
  1779                                     {
       
  1780                                         User::Leave(KErrNoMemory);
       
  1781                                     }
       
  1782                                     CleanupStack::Pop( aOutColumnValue );
       
  1783                                     if (outColumnValuePtr8.Size() == 0)
       
  1784                                     {
       
  1785                                         User::Leave(KErrNoMemory);
       
  1786                                     }
       
  1787                                     if(aColNumber == 1)
       
  1788                                         aEntry.iReference = aOutColumnValue;
       
  1789                                     else
       
  1790                                         aEntry.iData = aOutColumnValue;
       
  1791                                }
       
  1792                                 
       
  1793                                 break;
       
  1794                 
       
  1795                         case EDbColBinary:
       
  1796                             {
       
  1797                             EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1798                                 "CCertificateStoreDatabase::GetTableDataL() \
       
  1799                                 Binary column.\n" ) ) );            
       
  1800                             GetBinaryDataL( aView, &aOutColumnValue );
       
  1801                             break;
       
  1802                             }
       
  1803                         case EDbColLongBinary:              
       
  1804                             {
       
  1805                             EAP_TRACE_DEBUG_SYMBIAN(
       
  1806                                 (_L("CCertificateStoreDatabase::GetTableDataL - Long Binary column\n")));
       
  1807                         
       
  1808                             RDbColReadStream readStream;
       
  1809                         
       
  1810                             // Get the value from DB.
       
  1811                             HBufC8* valueBuf = HBufC8::NewLC(aView.ColLength(aColNumber)); // Buffer for the data.
       
  1812                             TPtr8 value8 = valueBuf->Des();
       
  1813                             
       
  1814                             readStream.OpenLC(aView, aColNumber);
       
  1815                             readStream.ReadL(value8, aView.ColLength(aColNumber));
       
  1816                             readStream.Close();
       
  1817                             CleanupStack::Pop(&readStream);
       
  1818                             
       
  1819                             EAP_TRACE_DATA_DEBUG_SYMBIAN(
       
  1820                                 ("CCertificateStoreDatabase::GetTableDataL: LONG BINARY value from DB",
       
  1821                                 value8.Ptr(), 
       
  1822                                 value8.Size()));
       
  1823                             
       
  1824                             HBufC8 *aDbBinaryColumnValue = HBufC8::NewLC(value8.Size());
       
  1825                             TPtr8 aDbBinaryColumnValuePtr = (aDbBinaryColumnValue)->Des();         
       
  1826         
       
  1827                             aDbBinaryColumnValuePtr.Copy(value8);
       
  1828                             EAP_TRACE_DATA_DEBUG_SYMBIAN(
       
  1829                                 ("CCertificateStoreDatabase::GetTableDataL: LONG BINARY value to caller",
       
  1830                                     aDbBinaryColumnValuePtr.Ptr(), 
       
  1831                                     aDbBinaryColumnValuePtr.Size()));
       
  1832                             
       
  1833                             CleanupStack::Pop(aDbBinaryColumnValue);
       
  1834                             CleanupStack::PopAndDestroy(valueBuf);
       
  1835                             
       
  1836                             if(aColNumber == 1)
       
  1837                                 aEntry.iReference = aDbBinaryColumnValue;
       
  1838                             else
       
  1839                                 aEntry.iData = aDbBinaryColumnValue;
       
  1840                            break;
       
  1841                             }       
       
  1842                         default:
       
  1843                             {
       
  1844                             EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1845                                 "ERROR: CCertificateStoreDatabase::GetTableDataL() \
       
  1846                                 Unsupported DB field:%d\n" ),
       
  1847                                 aView.ColType( aColNumber ) ) );    
       
  1848                             User::Leave( KErrNotSupported );
       
  1849                             break;
       
  1850                             }
       
  1851                         } // switch ( aView.ColType( KDefaultColumnNumberOne ) )
       
  1852                
       
  1853                     } // for
       
  1854                 aArray.Append(aEntry);
       
  1855 	            }while (aView.NextL() != EFalse);
       
  1856 	        } // if ( aView.FirstL() )
       
  1857 			
       
  1858     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1859         "CCertificateStoreDatabase::GetTableDataL() OUT\n" ) ) );
       
  1860 
       
  1861 	} // CCertificateStoreDatabase::GetBinaryDataL()
       
  1862 
       
  1863 		
       
  1864 
       
  1865 // ---------------------------------------------------------
       
  1866 // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1867 // ---------------------------------------------------------
       
  1868 //
       
  1869 void CCertificateStoreDatabase::InsertDataAndReferenceL(
       
  1870 	RDbView& aView,
       
  1871 	const TDesC& aReferenceColumnName,
       
  1872 	const TDesC& aDataColumnName,
       
  1873 	const TDesC16& aDataReference16,
       
  1874 	const TDesC8& aColumnValue )
       
  1875 	{
       
  1876     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1877         "CCertificateStoreDatabase::InsertCsDataByReferenceL IN1\n" ) ) );
       
  1878 	
       
  1879     // Convert the received reference id into integer.. 
       
  1880     TUint intRef = eap_read_u32_t_network_order(
       
  1881             aDataReference16.Ptr(),
       
  1882             aDataReference16.Size());
       
  1883     
       
  1884     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  1885             ( "CCertificateStoreDatabase::InsertCsDataByReferenceL1() \
       
  1886             New entry: reference set to DB(TEXT)=%d\n" ), intRef ) );
       
  1887     
       
  1888    InsertDataAndReferenceL ( aView, aReferenceColumnName, aDataColumnName, intRef, aColumnValue );
       
  1889 
       
  1890     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1891         "CCertificateStoreDatabase::InsertDataAndReferenceL OUT1\n" ) ) );
       
  1892 
       
  1893 	} // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1894 
       
  1895 
       
  1896 // ---------------------------------------------------------
       
  1897 // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1898 // ---------------------------------------------------------
       
  1899 //
       
  1900 void CCertificateStoreDatabase::InsertDataAndReferenceL(
       
  1901     RDbView& aView,
       
  1902     const TDesC& aReferenceColumnName,
       
  1903     const TDesC& aDataColumnName,
       
  1904     const TUint aDataRef,
       
  1905     const TDesC8& aColumnValue )
       
  1906     {
       
  1907     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1908         "CCertificateStoreDatabase::InsertDataAndReferenceL IN1\n" ) ) );
       
  1909      
       
  1910     aView.InsertL();
       
  1911 
       
  1912     // There are two columns here to set. Value and reference.  
       
  1913     // Get column set so we get the correct column numbers
       
  1914     CDbColSet* colSet = aView.ColSetL();
       
  1915     CleanupStack::PushL( colSet );
       
  1916 
       
  1917     TDbColNo colNoReference = colSet->ColNo( aReferenceColumnName );
       
  1918     TDbColNo colNoValue = colSet->ColNo( aDataColumnName );
       
  1919             
       
  1920     aView.SetColL( colNoReference, aDataRef );
       
  1921             
       
  1922     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  1923             ( "CCertificateStoreDatabase::InsertDataAndReferenceL1() \
       
  1924             New entry: reference set to DB(TEXT)=%d\n" ), aDataRef ) );
       
  1925     
       
  1926     // Set the value.
       
  1927     HBufC8* valueBuf = HBufC8::NewLC( aColumnValue.Size() );
       
  1928     TPtr8 valuePtr8 = valueBuf->Des();      
       
  1929     valuePtr8.Copy( aColumnValue);      
       
  1930     aView.SetColL( colNoValue, valuePtr8 );
       
  1931             
       
  1932      EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  1933         "CCertificateStoreDatabase::InsertDataAndReferenceL1() \
       
  1934         New entry:Value set to DB",
       
  1935         valuePtr8.Ptr(), valuePtr8.Size() ) );
       
  1936     
       
  1937     CleanupStack::PopAndDestroy( valueBuf );        
       
  1938     CleanupStack::PopAndDestroy( colSet );
       
  1939 
       
  1940     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1941         "CCertificateStoreDatabase::InsertDataAndReferenceL1 OUT\n" ) ) );
       
  1942 
       
  1943     } // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1944     
       
  1945 
       
  1946 // ---------------------------------------------------------
       
  1947 // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1948 // ---------------------------------------------------------
       
  1949 //
       
  1950 void CCertificateStoreDatabase::InsertDataAndReferenceL(
       
  1951     RDbView& aView,
       
  1952     const TDesC& aReferenceColumnName,
       
  1953     const TDesC& aDataColumnName,
       
  1954     const TUint aDataRef,
       
  1955     const TDesC& aColumnValue )
       
  1956     {
       
  1957     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1958         "CCertificateStoreDatabase::InsertDataAndReferenceL IN\n" ) ) );
       
  1959     
       
  1960     aView.InsertL();
       
  1961 
       
  1962     // There are two columns here to set. Value and reference.  
       
  1963     // Get column set so we get the correct column numbers
       
  1964     CDbColSet* colSet = aView.ColSetL();
       
  1965     CleanupStack::PushL( colSet );
       
  1966 
       
  1967     TDbColNo colNoReference = colSet->ColNo( aReferenceColumnName );
       
  1968     TDbColNo colNoValue = colSet->ColNo( aDataColumnName );
       
  1969             
       
  1970     aView.SetColL( colNoReference, aDataRef );
       
  1971             
       
  1972     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  1973             ( "CCertificateStoreDatabase::InsertDataAndReferenceL() \
       
  1974             New entry: reference set to DB(TEXT)=%d\n" ), &aDataRef ) );
       
  1975     
       
  1976     // Set the value.
       
  1977     aView.SetColL( colNoValue, aColumnValue );
       
  1978             
       
  1979     CleanupStack::PopAndDestroy( colSet );
       
  1980 
       
  1981     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1982         "CCertificateStoreDatabase::InsertDataAndReferenceL OUT\n" ) ) );
       
  1983 
       
  1984     } // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  1985     
       
  1986 
       
  1987 // CCertificateStoreDatabase::InsertDataL()
       
  1988 // ---------------------------------------------------------
       
  1989 //
       
  1990 void CCertificateStoreDatabase::InsertDataL(
       
  1991 	RDbView& aView,
       
  1992 	const TDesC& aDataColumnName,
       
  1993 	const TDesC8& aColumnValue )
       
  1994 	{
       
  1995     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  1996         "CCertificateStoreDatabase::InsertDataL() IN\n" ) ) );
       
  1997 	
       
  1998 	aView.InsertL();
       
  1999 
       
  2000 	// There is only one column here to set: data value.	
       
  2001 	// Get column set so we get the correct column number.
       
  2002 	CDbColSet* colSet = aView.ColSetL();
       
  2003 	CleanupStack::PushL( colSet );
       
  2004 	TDbColNo colNoValue = colSet->ColNo( aDataColumnName );
       
  2005 				
       
  2006 	// Set the value.
       
  2007 	HBufC8* valueBuf = HBufC8::NewLC( aColumnValue.Size() );
       
  2008 	TPtr8 valuePtr8 = valueBuf->Des();		
       
  2009 	valuePtr8.Copy( aColumnValue);		
       
  2010 	aView.SetColL( colNoValue, valuePtr8 );
       
  2011 			
       
  2012 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  2013 		"CCertificateStoreDatabase::InsertDataL() \
       
  2014 		New entry: value set to DB",
       
  2015 	    valuePtr8.Ptr(), valuePtr8.Size() ) );
       
  2016 	
       
  2017 	// clean
       
  2018 	CleanupStack::PopAndDestroy( valueBuf );		
       
  2019 	CleanupStack::PopAndDestroy( colSet );
       
  2020 
       
  2021     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2022         "CCertificateStoreDatabase::InsertDataL() OUT\n" ) ) );
       
  2023 
       
  2024 	} // CCertificateStoreDatabase::InsertDataAndReferenceL()
       
  2025 
       
  2026 
       
  2027 // ---------------------------------------------------------
       
  2028 // CCertificateStoreDatabase::UpdateColOneRowOneL()
       
  2029 // ---------------------------------------------------------
       
  2030 //
       
  2031 void CCertificateStoreDatabase::UpdateColOneRowOneL(
       
  2032 	RDbView& aView, const TDesC8& aColumnValue )
       
  2033 	{
       
  2034     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2035         "CCertificateStoreDatabase::UpdateColOneRowOneL() IN\n" ) ) );
       
  2036 	
       
  2037 	if ( aView.IsEmptyL() ||
       
  2038 		 aView.CountL() > KDefaultColumnNumberOne  ||
       
  2039 		 aView.ColCount() == 0 ||
       
  2040 		 aView.ColCount() > KDefaultColumnNumberOne  )
       
  2041 		{
       
  2042 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2043 			"ERROR: CCertificateStoreDatabase::UpdateColOneRowOneL() \
       
  2044 			Problem with rows or columns in DB view, row count=%d, col count=%d \n" ),
       
  2045 			aView.CountL(), aView.ColCount() ) );
       
  2046 			User::Leave( KErrNotFound );				
       
  2047 		}	
       
  2048 	if ( aView.FirstL() )
       
  2049 		{			
       
  2050 		aView.UpdateL(); // Here it is update.							
       
  2051 		switch ( aView.ColType( KDefaultColumnNumberOne ) )
       
  2052 			{
       
  2053 			case EDbColText:				
       
  2054 				{
       
  2055 				// This value can be set as it is. The column is default 1 here.
       
  2056 				aView.SetColL( KDefaultColumnNumberOne, aColumnValue );
       
  2057 				break;
       
  2058 				}
       
  2059             case EDbColUint32:
       
  2060                 {
       
  2061                 TUint aIntVal = eap_read_u32_t_network_order(
       
  2062                         aColumnValue.Ptr(),
       
  2063                         aColumnValue.Size());
       
  2064                 aView.SetColL( KDefaultColumnNumberOne, aIntVal );
       
  2065                 }
       
  2066                 break;
       
  2067 
       
  2068 			case EDbColBinary:
       
  2069 				{
       
  2070 				aView.SetColL( KDefaultColumnNumberOne, aColumnValue );					
       
  2071 				break;
       
  2072 				}
       
  2073 			case EDbColLongBinary:				
       
  2074 				{
       
  2075 			    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2076 					"CCertificateStoreDatabase::UpdateColOneRowOneL() \
       
  2077 					Long binary column.\n" ) ) );
       
  2078 				// A stream is needed for LONG columns in DB.
       
  2079 				RDbColWriteStream writeStream;					
       
  2080 				writeStream.OpenLC( aView, KDefaultColumnNumberOne );
       
  2081 				writeStream.WriteL( aColumnValue );
       
  2082 				writeStream.Close();
       
  2083 				CleanupStack::Pop( &writeStream );
       
  2084 				break;
       
  2085 				}
       
  2086 			default:
       
  2087 				{
       
  2088 			    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2089 					"ERROR: CCertificateStoreDatabase::UpdateColOneRowOneL() \
       
  2090 					Unsupported DB field! \n" ) ) );	
       
  2091 				User::Leave( KErrNotSupported );
       
  2092 				}
       
  2093 			} // switch ( aView.ColType( KDefaultColumnNumberOne ) )
       
  2094 		} // if ( aView.FirstL() )
       
  2095 	else
       
  2096 		{
       
  2097 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2098 			"ERROR: CCertificateStoreDatabase::UpdateColOneRowOneL() \
       
  2099 			There are no rows in view.\n" ) ) );
       
  2100 		User::Leave( KErrNotFound );
       
  2101 		}
       
  2102 
       
  2103     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2104         "CCertificateStoreDatabase::UpdateColOneRowOneL() OUT\n" ) ) );
       
  2105 
       
  2106 	} // CCertificateStoreDatabase::UpdateColOneRowOneL()
       
  2107 
       
  2108 
       
  2109 // ================= private:  Other =======================
       
  2110 
       
  2111 
       
  2112 // ---------------------------------------------------------
       
  2113 // CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC()
       
  2114 // ---------------------------------------------------------
       
  2115 //
       
  2116 void CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC(
       
  2117 	const TDesC8& aInBuf8, HBufC16** aOutBuf16 )
       
  2118 	{
       
  2119     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2120         "CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC() IN\n" ) ) );
       
  2121     
       
  2122 	// convert utf8->unicode,
       
  2123 	// aInBuf8 is UTF8 string, unicode max length is
       
  2124 	// then the length of UTF8 string.
       
  2125 	// NOTE, HBufC16 length means count of 16-bit objects.
       
  2126 	*aOutBuf16 = HBufC16::NewLC( aInBuf8.Size() );
       
  2127 	TPtr16 outBufPtr16 = ( *aOutBuf16 )->Des();
       
  2128 
       
  2129 	const TPtrC8 inBufPtrC8( aInBuf8 );
       
  2130 
       
  2131 	CnvUtfConverter::ConvertToUnicodeFromUtf8( outBufPtr16, inBufPtrC8 );
       
  2132 
       
  2133 	// print data
       
  2134 	EAP_TRACE_DATA_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2135         "CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC() aInBuf8" ),
       
  2136         inBufPtrC8.Ptr(), inBufPtrC8.Size() ) );
       
  2137 	
       
  2138     EAP_TRACE_DATA_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2139 	    "CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC() aOutBuf16" ),
       
  2140 	    outBufPtr16.Ptr(), outBufPtr16.Size() ) );
       
  2141 	
       
  2142     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2143         "CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC() OUT\n" ) ) );
       
  2144 
       
  2145 	} // CCertificateStoreDatabase::ConvertFromBuf8ToBuf16LC()
       
  2146 
       
  2147 
       
  2148 // ---------------------------------------------------------
       
  2149 // CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC()
       
  2150 // ---------------------------------------------------------
       
  2151 //
       
  2152 void CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC(
       
  2153 	const TDesC16& aInBuf16, HBufC8** aOutBuf8 )
       
  2154 	{
       
  2155     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2156         "CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC() IN\n" ) ) );
       
  2157 
       
  2158     // "In UTF-8, characters are encoded using sequences of 1 to 6 octets."
       
  2159     // RFC2279 - UTF-8
       
  2160     const TUint KMaxNumberOfOctetsPerUtf8Char = 6;
       
  2161 	// Convert unicode->utf8.
       
  2162 	// Note, HBufC16 length means the number of 16-bit values or
       
  2163     // data items represented by the descriptor.
       
  2164     // Multiply number of charachters by max number of octets for char.
       
  2165 	*aOutBuf8 = HBufC8::NewLC( aInBuf16.Length() * KMaxNumberOfOctetsPerUtf8Char );
       
  2166 	TPtr8 outBufPtr8 = ( *aOutBuf8 )->Des();
       
  2167 
       
  2168 	const TPtrC16 inBufPtrC16( aInBuf16 );
       
  2169 	
       
  2170 	CnvUtfConverter::ConvertFromUnicodeToUtf8( outBufPtr8, inBufPtrC16 );
       
  2171   
       
  2172 	// print data
       
  2173 	EAP_TRACE_DATA_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2174 	    "CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC() aInBuf16" ),
       
  2175 	    inBufPtrC16.Ptr(), inBufPtrC16.Size() ) );
       
  2176    	
       
  2177 	EAP_TRACE_DATA_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2178 		"CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC() aOutBuf8" ),
       
  2179 		outBufPtr8.Ptr(), outBufPtr8.Size() ) );
       
  2180 
       
  2181     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2182         "CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC() OUT\n" ) ) );	
       
  2183 	
       
  2184 	} // CCertificateStoreDatabase::ConvertFromBuf16ToBuf8LC()
       
  2185 
       
  2186 
       
  2187 // ---------------------------------------------------------
       
  2188 // CCertificateStoreDatabase::WriteCertificateStoreStateL()
       
  2189 // ---------------------------------------------------------
       
  2190 void CCertificateStoreDatabase::WriteCertificateStoreStateL(
       
  2191 	TCertificateStoreState aState )
       
  2192 	{
       
  2193     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2194         "CCertificateStoreDatabase::WriteCertificateStoreStateL() IN, \
       
  2195         aState=%d.\n" ), aState ) );
       
  2196 	    
       
  2197     // There could be a case where CS DB is destroyed.
       
  2198     // We just return in that case. 
       
  2199     if ( !iCsDbCreated )
       
  2200         {
       
  2201         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2202                 "CCertificateStoreDatabase::SetUserCertL() CS not created.\n" ) ) );
       
  2203         OpenCertificateStoreL();
       
  2204         }  
       
  2205 
       
  2206     if ( !iCsSessionOpened )
       
  2207 		{
       
  2208 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2209 			"ERROR: CCertificateStoreDatabase::WriteCertificateStoreStateL() \
       
  2210 			CS store not opened!\n" ) ) );		
       
  2211 		User::Leave( KErrSessionClosed );
       
  2212 		}		
       
  2213 				
       
  2214 	HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  2215 	TPtr sqlStatement = buf->Des();
       
  2216 				
       
  2217 	_LIT( KSqlQuery, "SELECT %S FROM %S" );
       
  2218 	sqlStatement.Format(
       
  2219 		KSqlQuery,
       
  2220 		&KCsInitialized,
       
  2221 		&KCsGeneralSettingsTableName );
       
  2222 
       
  2223 	EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  2224 		"wapi_am_core_symbian_c::WriteCertificateStoreStateL() sqlStatement",
       
  2225 		sqlStatement.Ptr(), 
       
  2226 		sqlStatement.Size() ) );	
       
  2227 
       
  2228  	RDbView view;
       
  2229 		
       
  2230 	User::LeaveIfError( view.Prepare(
       
  2231 		iCsDb, 
       
  2232 	    TDbQuery( sqlStatement ), 
       
  2233 		TDbWindow::EUnlimited ) );
       
  2234     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2235         "CCertificateStoreDatabase::WriteCertificateStoreStateL() \
       
  2236         View prepared OK.\n" ) ) );	   
       
  2237 	CleanupClosePushL( view );
       
  2238 		
       
  2239 	User::LeaveIfError( view.EvaluateAll() );
       
  2240     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2241 		"CCertificateStoreDatabase::WriteCertificateStoreStateL() \
       
  2242 		View evaluated OK.\n" ) ) );
       
  2243 		
       
  2244 	if ( !view.FirstL() )
       
  2245 		{		
       
  2246 		view.InsertL();
       
  2247 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2248     		"CCertificateStoreDatabase::WriteCertificateStoreStateL() \
       
  2249     		View inserted OK.\n" ) ) );
       
  2250 		}
       
  2251 	else
       
  2252 		{
       
  2253 	    view.UpdateL();
       
  2254 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2255 	    	"CCertificateStoreDatabase::WriteCertificateStoreStateL() \
       
  2256 	    	View updated OK.\n" ) ) );
       
  2257 		}
       
  2258 	
       
  2259 	// Get column set so we get the correct column numbers
       
  2260 	CDbColSet* colSet = view.ColSetL();
       
  2261 	CleanupStack::PushL( colSet );
       
  2262 						
       
  2263 	view.SetColL( colSet->ColNo( KCsInitialized ), aState );
       
  2264 						
       
  2265 	CleanupStack::PopAndDestroy( colSet ); // Delete colSet
       
  2266 					
       
  2267 	// Now it should go to the DB.
       
  2268 	view.PutL();	
       
  2269 
       
  2270 	CleanupStack::PopAndDestroy( &view ); // Close view.	
       
  2271 	CleanupStack::PopAndDestroy( buf ); // Delete buf or sqlStatement.		
       
  2272 		
       
  2273     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2274         "CCertificateStoreDatabase::WriteCertificateStoreStateL() OUT\n" ) ) );
       
  2275     
       
  2276 	} // CCertificateStoreDatabase::WriteCertificateStoreStateL()
       
  2277 
       
  2278 
       
  2279 // ---------------------------------------------------------
       
  2280 // CCertificateStoreDatabase::GetDbNamesFromDataTypeL()
       
  2281 // ---------------------------------------------------------
       
  2282 //
       
  2283 void CCertificateStoreDatabase::GetDbNamesFromDataTypeL(
       
  2284 	ec_cs_data_type_e aDataType, TDes& aTableName,
       
  2285 	TDes& aReferenceColumnName, TDes& aDataColumnName )
       
  2286 	{
       
  2287     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2288         "CCertificateStoreDatabase::GetDbNamesFromDataTypeL() IN\n" ) ) );
       
  2289 
       
  2290     switch ( aDataType )
       
  2291         {
       
  2292 	    case ec_cs_data_type_ca_certificate_data:
       
  2293 		    {
       
  2294 		    aTableName = KCsCaCertificateTable;
       
  2295 		    aReferenceColumnName = KCsCaCertAsuIdReference;
       
  2296 		    aDataColumnName = KCsCaCertData;
       
  2297 		    break;
       
  2298 		    }
       
  2299 	    case ec_cs_data_type_client_certificate_data:
       
  2300 		    {
       
  2301 		    aTableName = KCsClientCertificateTable;
       
  2302 		    aReferenceColumnName = KCsClientCertAsuIdReference;
       
  2303 		    aDataColumnName = KCsClientCertData;
       
  2304 		    break;
       
  2305 		    }
       
  2306 	    case ec_cs_data_type_private_key_data:
       
  2307 		    {
       
  2308 		    aTableName = KCsPrivateKeyTable;
       
  2309 		    aReferenceColumnName = KCsPrivateKeyAsuIdReference;
       
  2310 		    aDataColumnName = KCsPrivateKeyData;
       
  2311 		    break;
       
  2312 		    }
       
  2313         case ec_cs_data_type_ca_asu_id:
       
  2314         case ec_cs_data_type_ca_asu_id_list:
       
  2315            {
       
  2316             aTableName = KCsCaAsuIdListTableName;
       
  2317             aReferenceColumnName = KCsCaAsuIdReference;
       
  2318             aDataColumnName = KCsCaAsuIdData;
       
  2319             break;
       
  2320             }
       
  2321         case ec_cs_data_type_client_asu_id:
       
  2322         case ec_cs_data_type_client_asu_id_list:
       
  2323             {
       
  2324             aTableName = KCsClientAsuIdListTableName;
       
  2325             aReferenceColumnName = KCsClientAsuIdReference;
       
  2326             aDataColumnName = KCsClientAsuIdData;
       
  2327             break;
       
  2328             }
       
  2329         case ec_cs_data_type_master_key:
       
  2330             {
       
  2331             aTableName = KCsGeneralSettingsTableName;
       
  2332             aReferenceColumnName = NULL;
       
  2333             aDataColumnName = KCsMasterKey;
       
  2334             break;
       
  2335             }
       
  2336         case ec_cs_data_type_reference_counter:
       
  2337             {
       
  2338             aTableName = KCsGeneralSettingsTableName;
       
  2339             aReferenceColumnName = NULL;
       
  2340             aDataColumnName = KCsReferenceCounter;          
       
  2341             break;
       
  2342             }
       
  2343         case ec_cs_data_type_selected_ca_id:
       
  2344             {
       
  2345             aTableName = KCsWapiCertLabelTable;
       
  2346             aReferenceColumnName = KCsCertLabelAsuIdReference;
       
  2347             aDataColumnName = KCsCACertLabel;          
       
  2348             break;
       
  2349             }
       
  2350        case ec_cs_data_type_selected_client_id:
       
  2351            {
       
  2352            aTableName = KCsWapiCertLabelTable;
       
  2353            aReferenceColumnName = KCsCertLabelAsuIdReference;
       
  2354            aDataColumnName = KCsUserCertLabel;          
       
  2355            break;
       
  2356            }
       
  2357  		// ... add other types when needed
       
  2358         default:
       
  2359         	{
       
  2360 		    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2361 	            "ERROR: wapi_am_core_symbian_c::GetDbNamesFromDataTypeL() \
       
  2362 	            unknown dataType=%d.\n" ), aDataType ) );
       
  2363 			User::Leave( KErrArgument );	    	
       
  2364     	    }
       
  2365         } // switch
       
  2366     
       
  2367     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2368         "CCertificateStoreDatabase::GetDbNamesFromDataTypeL() OUT\n" ) ) );
       
  2369 
       
  2370     } // CCertificateStoreDatabase::GetDbNamesFromDataTypeL
       
  2371 
       
  2372 
       
  2373 // ---------------------------------------------------------
       
  2374 // CCertificateStoreDatabase::GetDbNamesFromDataTypeL()
       
  2375 // ---------------------------------------------------------
       
  2376 //
       
  2377 void CCertificateStoreDatabase::GetDbNamesFromDataTypeL(
       
  2378 	ec_cs_data_type_e aDataType,
       
  2379 	TDes& aTableName,
       
  2380 	TDes& aDataColumnName )
       
  2381 	{
       
  2382     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2383         "CCertificateStoreDatabase::GetDbNamesFromDataTypeL() IN\n" ) ) );
       
  2384 
       
  2385     switch ( aDataType )
       
  2386 	    {
       
  2387 		case ec_cs_data_type_master_key:
       
  2388 			{
       
  2389 		    aTableName = KCsGeneralSettingsTableName;
       
  2390 		    aDataColumnName = KCsMasterKey;
       
  2391 			break;
       
  2392 			}
       
  2393 		case ec_cs_data_type_reference_counter:
       
  2394 			{
       
  2395 		    aTableName = KCsGeneralSettingsTableName;
       
  2396 		    aDataColumnName = KCsReferenceCounter;			
       
  2397 			break;
       
  2398 			}
       
  2399         case ec_cs_data_type_ca_certificate_data:
       
  2400             {
       
  2401             aTableName = KCsCaCertificateTable;
       
  2402             aDataColumnName = KCsCaCertData;
       
  2403             break;
       
  2404             }
       
  2405         case ec_cs_data_type_client_certificate_data:
       
  2406             {
       
  2407             aTableName = KCsClientCertificateTable;
       
  2408             aDataColumnName = KCsClientCertData;
       
  2409             break;
       
  2410             }
       
  2411         case ec_cs_data_type_ca_asu_id:
       
  2412         case ec_cs_data_type_ca_asu_id_list:
       
  2413            {
       
  2414             aTableName = KCsCaAsuIdListTableName;
       
  2415             // only table needed
       
  2416             break;
       
  2417             }
       
  2418         case ec_cs_data_type_client_asu_id:
       
  2419         case ec_cs_data_type_client_asu_id_list:
       
  2420             {
       
  2421             aTableName = KCsClientAsuIdListTableName;
       
  2422             // only table needed
       
  2423             break;
       
  2424             }
       
  2425         case ec_cs_data_type_selected_ca_id:
       
  2426             {
       
  2427             aTableName = KCsWapiCertLabelTable;
       
  2428             aDataColumnName = KCsCACertLabel;          
       
  2429             break;
       
  2430             }
       
  2431        case ec_cs_data_type_selected_client_id:
       
  2432            {
       
  2433            aTableName = KCsWapiCertLabelTable;
       
  2434            aDataColumnName = KCsUserCertLabel;          
       
  2435            break;
       
  2436             }
       
  2437 			// ... add other types when needed	
       
  2438 	    default:
       
  2439 	    	{
       
  2440 		    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2441 	            "ERROR: wapi_am_core_symbian_c::GetDbNamesFromDataTypeL() \
       
  2442 	            unknown dataType=%d.\n" ), aDataType ) );
       
  2443 			User::Leave( KErrArgument );	    	
       
  2444 		    }
       
  2445 	    } // switch
       
  2446 	
       
  2447 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2448 	    "CCertificateStoreDatabase::GetDbNamesFromDataTypeL() OUT\n" ) ) );		
       
  2449 	
       
  2450 	} // CCertificateStoreDatabase::GetDbNamesFromDataTypeL()
       
  2451 
       
  2452 
       
  2453 
       
  2454 // ---------------------------------------------------------
       
  2455 // CCertificateStoreDatabase::SetCACertL()
       
  2456 // ---------------------------------------------------------
       
  2457 //
       
  2458 void CCertificateStoreDatabase::SetCACertL( const TInt aId, const TBuf8<KMaxIdentityLength> aSelectedCert )
       
  2459     {
       
  2460     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2461             ("CCertificateStoreDatabase::SetCACertL -Start")) );
       
  2462 
       
  2463     SetCertL( aId, aSelectedCert, KCsCACertLabel );
       
  2464     
       
  2465     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2466                 ("CCertificateStoreDatabase::SetCACertL -End")) );
       
  2467     return;
       
  2468 	}
       
  2469                                   
       
  2470 
       
  2471 // ---------------------------------------------------------
       
  2472 // CCertificateStoreDatabase::SetUserCertL()
       
  2473 // ---------------------------------------------------------
       
  2474 //
       
  2475 void CCertificateStoreDatabase::SetUserCertL( const TInt aId, const TBuf8<KMaxIdentityLength> aSelectedCert )
       
  2476 	{
       
  2477 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2478 	        ("CCertificateStoreDatabase::SetUserCertL -Start")) );
       
  2479 
       
  2480 	SetCertL( aId, aSelectedCert, KCsUserCertLabel );
       
  2481 	
       
  2482 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2483 	            ("CCertificateStoreDatabase::SetUserCertL -End")) );
       
  2484     return;
       
  2485     }
       
  2486 
       
  2487 
       
  2488 void CCertificateStoreDatabase::SetCertL ( const TInt aId, 
       
  2489         const TBuf8<KMaxIdentityLength> aSelectedCert, 
       
  2490         const TDesC& aParameterName )
       
  2491     {
       
  2492     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2493             ("CCertificateStoreDatabase::SetCertL -Start")) );
       
  2494 
       
  2495     if ( !iCsDbCreated )
       
  2496         {
       
  2497         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2498                 "CCertificateStoreDatabase::SetUserCertL() CS not created.\n" ) ) );
       
  2499         OpenCertificateStoreL();
       
  2500         }  
       
  2501     
       
  2502     if ( !iCsSessionOpened )
       
  2503         {
       
  2504         EAP_TRACE_ERROR(
       
  2505             iAmTools,
       
  2506             TRACE_FLAGS_DEFAULT,
       
  2507             (EAPL("ERROR: CCertificateStoreDatabase::SetCertL() \
       
  2508                     CS store not opened!\n")));        
       
  2509         User::Leave( KErrSessionClosed );
       
  2510         } 
       
  2511         
       
  2512     EAP_TRACE_DATA_DEBUG_SYMBIAN( (
       
  2513         "CCertificateStoreDatabase::SetCertL() aSelectedCert",
       
  2514         aSelectedCert.Ptr(), 
       
  2515         aSelectedCert.Size() ) );    
       
  2516    
       
  2517     // Two SQL statements, one for addition and one for modification
       
  2518     _LIT(KSQLQuery, "SELECT %S FROM %S WHERE %S=%d");
       
  2519     _LIT(KSQLInsert, "SELECT * FROM %S");
       
  2520             
       
  2521     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  2522     TPtr sqlStatement = buf->Des();
       
  2523     sqlStatement.Format( KSQLQuery, &aParameterName, &KCsWapiCertLabelTable, &KCsCertLabelAsuIdReference, aId );
       
  2524         
       
  2525     // Prepare the view, leave if it fails
       
  2526     RDbView view;   
       
  2527     User::LeaveIfError(view.Prepare( iCsDb, TDbQuery(sqlStatement), TDbWindow::EUnlimited, RDbView::EUpdatable));
       
  2528     User::LeaveIfError(view.EvaluateAll()); 
       
  2529     CleanupClosePushL(view);
       
  2530         
       
  2531     // Update the data if the record exists
       
  2532     if (view.FirstL())
       
  2533         {
       
  2534         view.UpdateL();
       
  2535         // Get column set so we get the correct column numbers
       
  2536         CDbColSet* colSet = view.ColSetL();
       
  2537         CleanupStack::PushL(colSet);
       
  2538         view.SetColL( colSet->ColNo( aParameterName ), aSelectedCert ); 
       
  2539         view.PutL(); 
       
  2540         CleanupStack::PopAndDestroy( colSet );
       
  2541         }
       
  2542     // New row. Modify the sql statement for insertion
       
  2543     else
       
  2544         {
       
  2545         sqlStatement.Format( KSQLInsert, &KCsWapiCertLabelTable );
       
  2546         // Leave if the view preparation still fails
       
  2547         User::LeaveIfError ( view.Prepare( iCsDb, TDbQuery(sqlStatement), TDbWindow::EUnlimited, RDbView::EInsertOnly ));
       
  2548         User::LeaveIfError(view.EvaluateAll());
       
  2549         // Use the data insertion function to update data and reference
       
  2550         InsertDataAndReferenceL ( view, KCsCertLabelAsuIdReference, aParameterName, aId, aSelectedCert );  
       
  2551         view.PutL(); 
       
  2552         }
       
  2553          
       
  2554     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2555             ("CCertificateStoreDatabase::SetCertL - labels read")) );
       
  2556     CleanupStack::PopAndDestroy( &view );
       
  2557     CleanupStack::PopAndDestroy( buf );
       
  2558     return;
       
  2559     }
       
  2560         
       
  2561 // ---------------------------------------------------------
       
  2562 // CCertificateStoreDatabase::GetConfigurationL()
       
  2563 // ---------------------------------------------------------
       
  2564 //
       
  2565 void CCertificateStoreDatabase::GetConfigurationL( const TInt aId, TDes& aCACert, TDes& aUserCert )
       
  2566 	{
       
  2567 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2568 	        ("CCertificateStoreDatabase::GetConfigurationL -Start")) );
       
  2569 	
       
  2570 	_LIT(KEmpty, "None");
       
  2571 	// Initialize with not found
       
  2572     aCACert.Copy( KEmpty );
       
  2573     aUserCert.Copy( KEmpty );
       
  2574 
       
  2575     // Check whether db exists and connection is open. 
       
  2576 	// Zero values are returned if not 
       
  2577     if ( !iCsSessionOpened || !iCsDbCreated )
       
  2578         {
       
  2579         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2580                 "CCertificateStoreDatabase::GetConfigurationL() \
       
  2581                 CS not opened.\n" ) ) );
       
  2582         }
       
  2583     else
       
  2584         {  
       
  2585         HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  2586         TPtr sqlStatement = buf->Des();
       
  2587         
       
  2588 	    // Read ca certificate label value
       
  2589 	    _LIT(KSQLQuery, "SELECT %S FROM %S WHERE %S=%d");
       
  2590 	    sqlStatement.Format( KSQLQuery, &KCsCACertLabel, &KCsWapiCertLabelTable, 
       
  2591 	                        &KCsCertLabelAsuIdReference, aId );
       
  2592 	    
       
  2593 	    RDbView view;   
       
  2594 	    User::LeaveIfError(view.Prepare( iCsDb, TDbQuery(sqlStatement), 
       
  2595 	                            TDbWindow::EUnlimited, RDbView::EReadOnly));
       
  2596 	    CleanupClosePushL(view);
       
  2597 	    User::LeaveIfError(view.EvaluateAll()); 
       
  2598 	  
       
  2599 	    // Read the CA cert label
       
  2600         ReadLabelTableL( view, aCACert );
       
  2601 	        
       
  2602 	    // Read the User cert label
       
  2603 	    sqlStatement.Format( KSQLQuery, &KCsUserCertLabel, &KCsWapiCertLabelTable, 
       
  2604 	                            &KCsCertLabelAsuIdReference, aId );         
       
  2605 	    User::LeaveIfError(view.Prepare( iCsDb, TDbQuery(sqlStatement), 
       
  2606 	                                    TDbWindow::EUnlimited, RDbView::EReadOnly));
       
  2607 	    User::LeaveIfError(view.EvaluateAll()); 
       
  2608 	    
       
  2609 	    ReadLabelTableL( view, aUserCert );
       
  2610 
       
  2611 	    EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2612 	            ("CCertificateStoreDatabase::GetConfigurationL - labels read")) );
       
  2613 
       
  2614 	    // Close database
       
  2615 	    CleanupStack::PopAndDestroy( &view );
       
  2616 	    CleanupStack::PopAndDestroy( buf );
       
  2617 	    } 
       
  2618 	return;
       
  2619 	}
       
  2620 
       
  2621 
       
  2622 // ---------------------------------------------------------
       
  2623 // CCertificateStoreDatabase::ReadLabelTable()
       
  2624 // ---------------------------------------------------------
       
  2625 //
       
  2626 void CCertificateStoreDatabase::ReadLabelTableL( RDbView& aView, TDes& aCert )
       
  2627     {
       
  2628     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2629             ("CCertificateStoreDatabase::ReadLabelTableL -Start")) );
       
  2630     
       
  2631     HBufC8* value;
       
  2632     eap_variable_data_c subjectName(iAmTools);
       
  2633     eap_variable_data_c label(iAmTools); 
       
  2634     wapi_asn1_der_parser_c wapiAsn1(iAmTools);
       
  2635     // Check memory reservations and leave if failed
       
  2636     if (wapiAsn1.get_is_valid() == false || 
       
  2637         subjectName.get_is_valid() == false ||
       
  2638         label.get_is_valid() == false )
       
  2639         {
       
  2640         EAP_TRACE_ERROR(
       
  2641             iAmTools,
       
  2642             TRACE_FLAGS_DEFAULT,
       
  2643             (EAPL("ERROR: CCertificateStoreDatabase::ReadLabelTableL() \
       
  2644                     Memory allocation failed!\n")));  
       
  2645         User::Leave(KErrGeneral);
       
  2646         }
       
  2647     
       
  2648     // check if there are rows in the view
       
  2649     if (aView.FirstL())
       
  2650         {
       
  2651         aView.GetL();
       
  2652         // Store the data     
       
  2653         GetLongBinaryDataL( aView, &value );
       
  2654         CleanupStack::PushL(value);
       
  2655         
       
  2656         // If the label exists, it will be decoded
       
  2657         if ( value->Size() > 0 )
       
  2658             {
       
  2659             eap_status_e status = label.set_copy_of_buffer( value->Ptr(), value->Size() );
       
  2660             CleanupStack::PopAndDestroy( value );
       
  2661             if ( status != eap_status_ok )
       
  2662                 {
       
  2663                 User::Leave(KErrGeneral);
       
  2664                 }
       
  2665             
       
  2666             status = wapiAsn1.get_decoded_subject_name( &label, &subjectName );
       
  2667             if ( status != eap_status_ok )
       
  2668                 {
       
  2669                 EAP_TRACE_ERROR(
       
  2670                         iAmTools,
       
  2671                         TRACE_FLAGS_DEFAULT,
       
  2672                             (EAPL("ERROR: CCertificateStoreDatabase::ReadLabelTable() \
       
  2673                             decoding failed!\n")));  
       
  2674                 User::Leave(KErrGeneral);
       
  2675                 }
       
  2676             
       
  2677             // Check the lenght of the subject name part of the label,
       
  2678             if ( subjectName.get_data_length() <= KCsMaxWapiCertLabelLength )
       
  2679                 {
       
  2680                 TBuf8<KCsMaxWapiCertLabelLength> tmpLabel;
       
  2681                 tmpLabel.Append( subjectName.get_data(subjectName.get_data_length()),
       
  2682                         subjectName.get_data_length());
       
  2683                         
       
  2684                 // Copy the data into the returned parameter
       
  2685                 HBufC16* label16;
       
  2686                 ConvertFromBuf8ToBuf16LC( tmpLabel, &label16 );
       
  2687                 aCert.Copy( *label16 );
       
  2688                 CleanupStack::PopAndDestroy(label16);
       
  2689                 }
       
  2690             // Label is too long, write to log
       
  2691             else
       
  2692                 {
       
  2693                 EAP_TRACE_ERROR(
       
  2694                         iAmTools,
       
  2695                         TRACE_FLAGS_DEFAULT,
       
  2696                         (EAPL("ERROR: CCertificateStoreDatabase::ReadLabelTable() \
       
  2697                         label too long!!\n"))); 
       
  2698                 }
       
  2699             }
       
  2700         else
       
  2701             {
       
  2702             CleanupStack::PopAndDestroy(value);
       
  2703             }
       
  2704         }
       
  2705     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2706             ("CCertificateStoreDatabase::ReadLabelTableL -End")) );
       
  2707     }
       
  2708 
       
  2709 // ---------------------------------------------------------
       
  2710 // CCertificateStoreDatabase::RemoveDataFromViewL()
       
  2711 // ---------------------------------------------------------
       
  2712 //
       
  2713 void CCertificateStoreDatabase::RemoveDataFromTableL( const TDesC& aTableName, 
       
  2714                                     const TDesC& aReferenceName, TUint aRefId  )
       
  2715     {
       
  2716     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2717             ("CCertificateStoreDatabase::RemoveDataFromTable -Start")) );       
       
  2718         
       
  2719     RDbView view;
       
  2720     HBufC* buf = HBufC::NewLC( KMaxSqlQueryLength );
       
  2721     TPtr sqlStatement = buf->Des();
       
  2722     _LIT(KSQLQuery, "SELECT * FROM %S WHERE %S=%d");
       
  2723     
       
  2724     sqlStatement.Format( KSQLQuery, &aTableName, &aReferenceName, aRefId );
       
  2725     User::LeaveIfError(view.Prepare( iCsDb, TDbQuery(sqlStatement), TDbWindow::EUnlimited, RDbView::EUpdatable));
       
  2726     CleanupClosePushL(view);
       
  2727     
       
  2728     User::LeaveIfError(view.EvaluateAll());
       
  2729     
       
  2730     EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2731             ("CCertificateStoreDatabase::RemoveDataFromTable - view evaluated OK\n")));
       
  2732     
       
  2733     if (view.FirstL())
       
  2734         {
       
  2735         //Delete the row if it was found
       
  2736         view.DeleteL(); 
       
  2737         }
       
  2738     else
       
  2739         {
       
  2740         // the row was not found
       
  2741         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2742                 ("CCertificateStoreDatabase::RemoveDataFromTable - No data found\n")));
       
  2743         }
       
  2744     CleanupStack::PopAndDestroy( &view );
       
  2745     CleanupStack::PopAndDestroy( buf );
       
  2746     }
       
  2747 	
       
  2748 // ---------------------------------------------------------
       
  2749 // CCertificateStoreDatabase::DeleteAPSpecificDataL( TInt aId )
       
  2750 // ---------------------------------------------------------
       
  2751 //
       
  2752 void CCertificateStoreDatabase::DeleteAPSpecificDataL( const TInt aId )
       
  2753 	{
       
  2754 	
       
  2755 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL
       
  2756 	        ("CCertificateStoreDatabase::DeleteAPSpecificDataL -Start")) );  
       
  2757 	
       
  2758     // If DB is not created, there is nothing to delete
       
  2759     if ( !iCsDbCreated )
       
  2760         {
       
  2761         EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2762                 "ERROR: CCertificateStoreDatabase::DeleteAPSpecificDataL() \
       
  2763                 CS DB doesn't exist. Don't do anything.\n" ) ) );
       
  2764         return;
       
  2765         }     
       
  2766 
       
  2767     // Check whether db connection is open and data exists
       
  2768 	if ( !iCsSessionOpened )
       
  2769 	    {
       
  2770         EAP_TRACE_ERROR(
       
  2771             iAmTools,
       
  2772             TRACE_FLAGS_DEFAULT,
       
  2773             (EAPL("ERROR: CCertificateStoreDatabase::DeleteAPSpecificDataL() \
       
  2774                 CS not opened.\n")));
       
  2775 	    User::Leave( KErrSessionClosed );
       
  2776 	    }   
       
  2777   
       
  2778     // Delete the row from KCsWapiCertLabelTable 
       
  2779     TRAPD ( err, RemoveDataFromTableL ( KCsWapiCertLabelTable, KCsCertLabelAsuIdReference, aId ));
       
  2780     
       
  2781     // Leave if there were errors in one of the deletions
       
  2782     User::LeaveIfError ( err );
       
  2783 	}
       
  2784 
       
  2785 // ================= private:  Private constructors =======================
       
  2786 
       
  2787 
       
  2788 // ---------------------------------------------------------
       
  2789 // CCertificateStoreDatabase::CCertificateStoreDatabase()
       
  2790 // ---------------------------------------------------------
       
  2791 //
       
  2792 CCertificateStoreDatabase::CCertificateStoreDatabase(
       
  2793     abs_eap_am_tools_c* aAmTools )
       
  2794     : iState( ECertificateStoreStatesNumber )
       
  2795     , iCsDbCreated( EFalse )
       
  2796     , iCsSessionOpened( EFalse )
       
  2797     , iAmTools( aAmTools )
       
  2798     , iPartner (NULL)
       
  2799     {
       
  2800     }
       
  2801 
       
  2802 	
       
  2803 // ---------------------------------------------------------
       
  2804 // CCertificateStoreDatabase::ConstructL()
       
  2805 // ---------------------------------------------------------
       
  2806 //
       
  2807 void CCertificateStoreDatabase::ConstructL()
       
  2808     {
       
  2809 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2810         "CCertificateStoreDatabase::ConstructL() IN\n" ) ) );		
       
  2811 
       
  2812 	EAP_TRACE_DEBUG( iAmTools, TRACE_FLAGS_DEFAULT, ( EAPL(
       
  2813         "CCertificateStoreDatabase::ConstructL() OUT\n" ) ) );			
       
  2814     
       
  2815     } // CCertificateStoreDatabase::ConstructL()
       
  2816 
       
  2817 // End of file.
       
  2818