terminalsecurity/SCP/SCPServer/src/SCPConfiguration.cpp
changeset 0 b497e44ab2fc
child 19 86979fe66c4c
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2000 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Implementation of terminalsecurity components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32debug.h>
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include <SCPParamObject.h>
       
    24 #include <des.h>
       
    25 
       
    26 #include "SCPConfiguration.h"
       
    27 #include "SCPServer.h"
       
    28 #include <featmgr.h>
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 	    
       
    32         
       
    33 // C++ default constructor can NOT contain any code, that
       
    34 // might leave.
       
    35 //
       
    36 TSCPConfiguration::TSCPConfiguration( RFs* aRfs )
       
    37     : iFsSession( aRfs )
       
    38 	{
       
    39 	Dprint( (_L("--> TSCPConfiguration::TSCPConfiguration()") ));
       
    40 		      	
       
    41 	Dprint( (_L("<-- TSCPConfiguration::TSCPConfiguration()") ));
       
    42 	}
       
    43 
       
    44 
       
    45 
       
    46 // ---------------------------------------------------------
       
    47 // TSCPConfiguration::Initialize()
       
    48 // Initialized the configuration path
       
    49 // 
       
    50 // Status : Approved
       
    51 // ---------------------------------------------------------
       
    52 //
       
    53 void TSCPConfiguration::Initialize()
       
    54     {
       
    55     Dprint( (_L("--> TSCPConfiguration::Initialize()") ));
       
    56     
       
    57     iConfigFileName.Zero();
       
    58 		
       
    59     // Create the server's private path, if it doesn't exist
       
    60     iFsSession->CreatePrivatePath( EDriveC );
       
    61         
       
    62     // Form the configuration file name        
       
    63     iFsSession->PrivatePath( iConfigFileName );
       
    64     iConfigFileName.Append( KSCPSettingsFileName );      
       
    65     }
       
    66 
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------
       
    70 // TSCPConfiguration::ReadSetupL()
       
    71 // Read the configuration settings from the config-file, using
       
    72 // a paramObject.
       
    73 // 
       
    74 // Status : Approved
       
    75 // ---------------------------------------------------------
       
    76 //
       
    77 TInt TSCPConfiguration::ReadSetupL()
       
    78     {
       
    79     TRAPD( errf, FeatureManager::InitializeLibL() );
       
    80 		if( errf != KErrNone )
       
    81 		{
       
    82 			User::Leave(errf);
       
    83 		}
       
    84 		TInt result = KErrNone;
       
    85 		
       
    86 		TAny* KParameters[7];
       
    87 		TInt KParamIDs[7];
       
    88 		TSCPParamType KSCPParamTypes[7];
       
    89 		TInt KNumParams;
       
    90 		
       
    91 		
       
    92 		if(FeatureManager::FeatureSupported(KFeatureIdSapDeviceLockEnhancements))
       
    93 		{
       
    94 			TAny* KParameters_WithFlag[] = SCP_PARAMETERS_WithFlag;       
       
    95 		    const TInt KParamIDs_WithFlag[] = SCP_PARAMIDS_WithFlag;
       
    96 		    const TSCPParamType KSCPParamTypes_WithFlag[] = SCP_PARAMTYPES_WithFlag;
       
    97 		    const TInt KNumParams_WithFlag = sizeof( KParameters_WithFlag ) / sizeof( TAny* );
       
    98 	    	
       
    99 	    	for (TInt index = 0; index < KNumParams_WithFlag; index++)
       
   100 	    	{
       
   101 	    		KParameters[index] = KParameters_WithFlag [index];
       
   102 	    		KParamIDs[index] = KParamIDs_WithFlag[index];
       
   103 	    		KSCPParamTypes[index] = KSCPParamTypes_WithFlag[index];
       
   104 	    	}
       
   105 	    	KNumParams = KNumParams_WithFlag;
       
   106 		}
       
   107 		
       
   108 		else
       
   109 		{
       
   110 			TAny* KParameters_WithOutFlag[] = SCP_PARAMETERS_WithOutFlag;       
       
   111 		    const TInt KParamIDs_WithOutFlag[] = SCP_PARAMIDS_WithOutFlag;
       
   112 		    const TSCPParamType KSCPParamTypes_WithOutFlag[] = SCP_PARAMTYPES_WithOutFlag;
       
   113 		    const TInt KNumParams_WithOutFlag = sizeof( KParameters_WithOutFlag ) / sizeof( TAny* );
       
   114 	    	
       
   115 	    	for (TInt index = 0; index < KNumParams_WithOutFlag; index++)
       
   116 	    	{
       
   117 	    		KParameters[index] = KParameters_WithOutFlag [index];
       
   118 	    		KParamIDs[index] = KParamIDs_WithOutFlag[index];
       
   119 	    		KSCPParamTypes[index] = KSCPParamTypes_WithOutFlag[index];
       
   120 	    	}
       
   121 	    	KNumParams = KNumParams_WithOutFlag;
       
   122 		}
       
   123 		
       
   124     
       
   125     CSCPParamObject* params = CSCPParamObject::NewL();
       
   126     CleanupStack::PushL( params );
       
   127 
       
   128     TFileName configFileName;
       
   129     configFileName.Copy( KSCPSettingsFileName );
       
   130     
       
   131     TRAPD( err, params->ReadFromFileL( iConfigFileName, iFsSession ) );
       
   132     
       
   133     if ( err != KErrNone )
       
   134         {
       
   135         FeatureManager::UnInitializeLib();
       
   136         CleanupStack::PopAndDestroy( params );
       
   137         User::Leave( err );
       
   138         }
       
   139     
       
   140     TInt ret = KErrNone;    
       
   141     for ( TInt i = 0; i < KNumParams; i++ )
       
   142         { 
       
   143         // Set the value according to the type
       
   144         switch ( KSCPParamTypes[i] )
       
   145            {   
       
   146            case ( EParTypeInt ):
       
   147                {                                
       
   148                ret = params->Get( KParamIDs[i], *(reinterpret_cast<TInt*>( KParameters[i] )) );
       
   149                break;
       
   150                }
       
   151                                
       
   152 		   case ( EParTypeDesc ):
       
   153                { 
       
   154                ret = params->Get( KParamIDs[i], *(reinterpret_cast<TDes*>( KParameters[i] )) );
       
   155                break;
       
   156                } 
       
   157                
       
   158            default:               
       
   159                 // No implementation needed           
       
   160                 break;
       
   161            } // switch                 
       
   162         
       
   163         if ( ret != KErrNone )
       
   164             {
       
   165             result = ret; // Return the last error, attempting to read all parameters
       
   166             Dprint( (_L("TSCPConfiguration::ReadSetupL(): FAILED to get value for %d"), KParamIDs[i] ));
       
   167             }
       
   168         }
       
   169     
       
   170     CleanupStack::PopAndDestroy( params ); 
       
   171     
       
   172     // Decrypt the ISA security code
       
   173     TSCPSecCode cryptBuf;
       
   174     cryptBuf.Copy( iSecCode );
       
   175     TransformStringL( EFalse, cryptBuf, iSecCode );
       
   176     iSecCode.SetLength( KSCPCodeMaxLen ); // Remove the suffix
       
   177        
       
   178     FeatureManager::UnInitializeLib();
       
   179     return result;       
       
   180     }
       
   181 
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------
       
   185 // TSCPConfiguration::WriteSetupL()
       
   186 // Fill the configuration values into a paramObject and write
       
   187 // it to disk.
       
   188 // 
       
   189 // Status : Approved
       
   190 // ---------------------------------------------------------
       
   191 //
       
   192 TInt TSCPConfiguration::WriteSetupL()
       
   193     {        
       
   194     TRAPD( errf, FeatureManager::InitializeLibL() );
       
   195 		if( errf != KErrNone )
       
   196 		{
       
   197 			User::Leave(errf);
       
   198 		}
       
   199 		TInt result = KErrNone;
       
   200 		
       
   201 		
       
   202 		
       
   203 		TAny* KParameters[7];
       
   204 		TInt KParamIDs[7];
       
   205 		TSCPParamType KSCPParamTypes[7];
       
   206 		TInt KNumParams;
       
   207 		
       
   208 		
       
   209 		if(FeatureManager::FeatureSupported(KFeatureIdSapDeviceLockEnhancements))
       
   210 		{
       
   211 			TAny* KParameters_WithFlag[] = SCP_PARAMETERS_WithFlag;       
       
   212 		    const TInt KParamIDs_WithFlag[] = SCP_PARAMIDS_WithFlag;
       
   213 		    const TSCPParamType KSCPParamTypes_WithFlag[] = SCP_PARAMTYPES_WithFlag;
       
   214 		    const TInt KNumParams_WithFlag = sizeof( KParameters_WithFlag ) / sizeof( TAny* );
       
   215 	    	
       
   216 	    	for (TInt index = 0; index < KNumParams_WithFlag; index++)
       
   217 	    	{
       
   218 	    		KParameters[index] = KParameters_WithFlag [index];
       
   219 	    		KParamIDs[index] = KParamIDs_WithFlag[index];
       
   220 	    		KSCPParamTypes[index] = KSCPParamTypes_WithFlag[index];
       
   221 	    	}
       
   222 	    	KNumParams = KNumParams_WithFlag;
       
   223 		}
       
   224 		
       
   225 		else
       
   226 		{
       
   227 			TAny* KParameters_WithOutFlag[] = SCP_PARAMETERS_WithOutFlag;       
       
   228 		    const TInt KParamIDs_WithOutFlag[] = SCP_PARAMIDS_WithOutFlag;
       
   229 		    const TSCPParamType KSCPParamTypes_WithOutFlag[] = SCP_PARAMTYPES_WithOutFlag;
       
   230 		    const TInt KNumParams_WithOutFlag = sizeof( KParameters_WithOutFlag ) / sizeof( TAny* );
       
   231 	    	
       
   232 	    	for (TInt index = 0; index < KNumParams_WithOutFlag; index++)
       
   233 	    	{
       
   234 	    		KParameters[index] = KParameters_WithOutFlag [index];
       
   235 	    		KParamIDs[index] = KParamIDs_WithOutFlag[index];
       
   236 	    		KSCPParamTypes[index] = KSCPParamTypes_WithOutFlag[index];
       
   237 	    	}
       
   238 	    	KNumParams = KNumParams_WithOutFlag;
       
   239 		}
       
   240     // Write the parameters by the configured IDs to the paramObject, and write this to disk.       
       
   241     CSCPParamObject* params = CSCPParamObject::NewL();
       
   242     CleanupStack::PushL( params );  
       
   243     
       
   244     // Encrypt the ISA security code
       
   245     TSCPSecCode cryptBuf;
       
   246     cryptBuf.Copy( iSecCode );
       
   247     cryptBuf.Append( KSCPCryptSuffix ); // 5 chars for the code + suffix
       
   248     TransformStringL( ETrue, cryptBuf, iSecCode );
       
   249     
       
   250     TInt ret = KErrNone;    
       
   251     for ( TInt i = 0; i < KNumParams; i++ )
       
   252         { 
       
   253         // Set the value according to the type
       
   254         switch ( KSCPParamTypes[i] )
       
   255            {   
       
   256            case ( EParTypeInt ):
       
   257                {                                
       
   258                ret = params->Set( KParamIDs[i], *(reinterpret_cast<TInt*>( KParameters[i] )) );
       
   259                break;
       
   260                }
       
   261                                
       
   262 		   case ( EParTypeDesc ):
       
   263                { 
       
   264                ret = params->Set( KParamIDs[i], *(reinterpret_cast<TDes*>( KParameters[i] )) );
       
   265                break;
       
   266                } 
       
   267                
       
   268            default:               
       
   269                 // No implementation needed           
       
   270                 break;
       
   271            } // switch                 
       
   272         
       
   273         if ( ret != KErrNone )
       
   274             {
       
   275             result = ret; // Return the last error, attempting to read all parameters
       
   276             Dprint( (_L("TSCPConfiguration::WriteSetupL(): FAILED to set value for %d"), KParamIDs[i] ));
       
   277             }
       
   278         }  
       
   279         
       
   280     // Reset the ISA security code
       
   281     iSecCode.Copy( cryptBuf );
       
   282     iSecCode.SetLength( KSCPCodeMaxLen );
       
   283            
       
   284     params->WriteToFileL( iConfigFileName, iFsSession );           
       
   285      
       
   286     CleanupStack::PopAndDestroy( params );  
       
   287     
       
   288     
       
   289     FeatureManager::UnInitializeLib();
       
   290     return result;    
       
   291     }
       
   292     
       
   293     
       
   294 
       
   295 // ---------------------------------------------------------
       
   296 // TSCPConfiguration::GetKey()
       
   297 // Returns the server's UID reformatted as the key.
       
   298 // 
       
   299 // Status : Approved
       
   300 // ---------------------------------------------------------
       
   301 //
       
   302 void TSCPConfiguration::GetKeyL( TDes8& aKey )
       
   303     {
       
   304     TInt64 key = KSCPServerUid.iUid;
       
   305     key = ( key << 32 ) + KSCPServerUid.iUid;
       
   306     
       
   307     TUint8* keyPtr = const_cast<TUint8*>( aKey.Ptr() );
       
   308     *( reinterpret_cast<TInt64*>( keyPtr ) ) = key;
       
   309     
       
   310     aKey.SetLength( KSCPDesBlockSize );
       
   311     
       
   312     TBuf8<KSCPDesBlockSize> modBuf;
       
   313     modBuf.Copy( KSCPEncryptionKeyMod );
       
   314     
       
   315     TInt i = 0;
       
   316     for ( i = 0; i < KSCPDesBlockSize; i++ )
       
   317         {
       
   318         aKey[i] = aKey[i] ^ modBuf[i];
       
   319         }    
       
   320     }
       
   321     
       
   322     
       
   323     
       
   324 // ---------------------------------------------------------
       
   325 // TSCPConfiguration::TransformStringL()
       
   326 // Transforms the given buffer using DES encryption/decryption
       
   327 // and stores the result to aOutput
       
   328 // 
       
   329 // Status : Approved
       
   330 // ---------------------------------------------------------
       
   331 //
       
   332 void TSCPConfiguration::TransformStringL( TBool aEncrypt, TDes& aInput, TDes& aOutput )
       
   333     {
       
   334 #ifdef UNICODE
       
   335     // Reinterpret the input and output as 8-bit data
       
   336     TUint16* inputPtr = const_cast<TUint16*>( aInput.Ptr() ); 
       
   337     TPtrC8 inputData( reinterpret_cast<TUint8*>(inputPtr), aInput.Length()*2 );
       
   338     
       
   339     TUint16* outputPtr = const_cast<TUint16*>( aOutput.Ptr() ); 
       
   340     TPtr8 outputData( reinterpret_cast<TUint8*>(outputPtr), 0, aOutput.MaxLength()*2 );
       
   341         
       
   342 #else // !UNICODE
       
   343     TPtrC8 inputData = aInput;
       
   344     TPtr8 outputData = aOutput;
       
   345 #endif  // UNICODE             
       
   346     
       
   347     TBuf8<KSCPDesBlockSize> desKey;
       
   348     GetKeyL( desKey );
       
   349     
       
   350     CBlockTransformation* transformer = NULL;
       
   351     if ( aEncrypt )
       
   352         {
       
   353         transformer = CDESEncryptor::NewL( desKey, EFalse );
       
   354         }
       
   355     else
       
   356         {
       
   357         transformer = CDESDecryptor::NewL( desKey, EFalse );
       
   358         }
       
   359     CleanupStack::PushL( transformer );
       
   360     
       
   361     aOutput.FillZ();
       
   362     aOutput.Zero();
       
   363     
       
   364     // 8 bytes of data per block
       
   365     TBuf8<KSCPDesBlockSize> desBlock;          
       
   366         
       
   367     TInt index = 0;
       
   368     TInt copySize;
       
   369     while ( index < inputData.Length() - 1 )
       
   370         {
       
   371         desBlock.FillZ(); // If the data is not an exact multiple of 8, pad with 0s
       
   372         desBlock.Zero();
       
   373         
       
   374         // Copy either the remaining data or a DES block
       
   375         if ( ( inputData.Length() - index ) < KSCPDesBlockSize )
       
   376             {
       
   377             copySize = inputData.Length() - index;
       
   378             }
       
   379         else
       
   380             {
       
   381             copySize = KSCPDesBlockSize;
       
   382             }
       
   383             
       
   384         desBlock.Copy( inputData.Mid( index, copySize ) );
       
   385         index += copySize;
       
   386         desBlock.SetLength( KSCPDesBlockSize );
       
   387         
       
   388         transformer->Transform( desBlock );
       
   389                         
       
   390         // Append the transformed block to the output
       
   391         outputData.Append( desBlock );                
       
   392         }
       
   393             
       
   394     CleanupStack::PopAndDestroy( transformer );
       
   395         
       
   396 #ifdef UNICODE
       
   397     aOutput.SetLength( outputData.Length() / 2 );
       
   398 #endif // UNICODE       
       
   399     }
       
   400 
       
   401 
       
   402 
       
   403 //  End of File  
       
   404