os/security/crypto/weakcryptospi/test/tcryptospi/src/pluginloadstep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 the License "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: 
    15 * Example CTestStep derived implementation
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22  @internalTechnology
    23 */
    24 #include "pluginloadstep.h"
    25 #include "plugincharschecker.h"
    26 
    27 #include <cryptospi/cryptosymmetriccipherapi.h>
    28 #include <cryptospi/keys.h>
    29 
    30 #include <cryptospi/cryptospistateapi.h>
    31 #include <cryptospi/cryptohashapi.h>
    32 #include <cryptospi/cryptorandomapi.h>
    33 #include <cryptospi/cryptosymmetriccipherapi.h>
    34 #include <cryptospi/cryptoasymmetriccipherapi.h>
    35 #include <cryptospi/cryptosignatureapi.h>
    36 #include <cryptospi/cryptokeypairgeneratorapi.h>
    37 #include <cryptospi/cryptokeyagreementapi.h>
    38 #include <cryptospi/ruleselector.h>
    39 
    40 using namespace CryptoSpi;
    41 
    42 
    43 CPluginLoadStep::~CPluginLoadStep()
    44 	{
    45 	}
    46 
    47 
    48 CPluginLoadStep::CPluginLoadStep()
    49 	{
    50 	SetTestStepName(KPluginLoadStep);
    51 	}
    52 
    53 
    54 TVerdict CPluginLoadStep::doTestStepPreambleL()
    55 	{
    56 	SetTestStepResult(EPass);	
    57 	return TestStepResult();
    58 	}
    59 
    60 
    61 TVerdict CPluginLoadStep::doTestStepL()
    62 	{
    63 	
    64 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    65 	
    66   	if (TestStepResult()==EPass)
    67 		{
    68 		//Assume faliure, unless all is successful
    69 		SetTestStepResult(EFail);
    70 		
    71 		TPtrC encryptKey;
    72 		TVariantPtrC keyType;
    73 		TVariantPtrC algorithm;
    74 		TVariantPtrC operationMode;
    75 		TVariantPtrC paddingMode;
    76 		TBool ruleSelectorToggle = EFalse;
    77 		
    78 		
    79 		if(	!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) || 
    80 			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType) ||
    81 			!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || 
    82 			!GetStringFromConfig(ConfigSection(),KConfigOperationMode, operationMode) ||
    83 			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ) ||
    84 			!GetBoolFromConfig(ConfigSection(),KConfigRuleSelectorToggle, ruleSelectorToggle ))
    85 			{
    86 			ERR_PRINTF1(_L("** Error: Failed to Load DoStep() Configuration Parameters **"));
    87 			User::Leave(KErrNotFound);
    88 			}
    89 		else
    90 			{
    91 			//Convert encryption key to an 8 Bit Descriptor
    92 			HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
    93 			TPtr8 keyStrPtr = keyStr->Des();
    94 
    95 			keyStrPtr.Copy(encryptKey);
    96 			
    97 			//Create an new CryptoParams object to encapsulate the key type and secret key string
    98 			CCryptoParams* keyParams = CCryptoParams::NewL();
    99 			CleanupStack::PushL(keyParams);
   100 			keyParams->AddL(*keyStr,keyType);
   101 
   102 			//Create Key Object
   103 			TKeyProperty keyProperty;
   104 			CKey* key=CKey::NewL(keyProperty,*keyParams);
   105 			CleanupStack::PushL(key);
   106 			
   107 			//***** Determine whether to set the Rule Selector *****
   108 			
   109 			CRuleSelector* ruleSelector = NULL;
   110 			
   111 			if(ruleSelectorToggle)
   112 				{
   113 				//Create Rule Selection Rules Object
   114 				CSelectionRules* rules = CSelectionRules::NewL();
   115 				CleanupStack::PushL(rules);
   116 				
   117 				//Create Rule Selector Object	
   118 				ruleSelector = CRuleSelector::NewL(rules);
   119 				CleanupStack::Pop(rules);
   120 				CleanupStack::PushL(ruleSelector);
   121 
   122 				//Set the Selector Passing in a pointer to the Default Selector and SPI State	
   123 				CCryptoSpiStateApi::SetSelector(ruleSelector);	
   124 				}
   125 				
   126 			//******************************************************
   127 
   128 			// Create a Symmetric Cipher with the values from the ini file
   129 			CryptoSpi::CSymmetricCipher * impl = NULL;	
   130 	
   131 			TRAPD(err,CSymmetricCipherFactory::CreateSymmetricCipherL(impl,
   132 															algorithm,
   133 															*key,
   134 															KCryptoModeEncryptUid,
   135 															operationMode,
   136 															paddingMode,
   137 															NULL));
   138 
   139 			if(impl && (err==KErrNone))
   140 				{
   141 				INFO_PRINTF1(_L("Successful Implementation Object Load..."));
   142 				
   143 				CleanupStack::PushL(impl);
   144 				
   145 				//Define a pointer of type TCharacteristics in order to store the appropriate
   146 				//encryption object's characterisctics
   147 				const TCharacteristics* characteristics(NULL);
   148 					
   149 				//Retrieve the characteristics for the symmetric cipher implementation object
   150 				TRAP_LOG(err, impl->GetCharacteristicsL(characteristics));
   151 				
   152 				TVariantPtrC exAlgorithmUid;
   153 				TVariantPtrC exImplementationUid;
   154 						
   155 				if(!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmUid,exAlgorithmUid) ||
   156 					!GetStringFromConfig(ConfigSection(),KConfigExImplementationUid,exImplementationUid))
   157 					{
   158 					ERR_PRINTF1(_L("** .INI Error: Expected Algorithm Arguments Not Located **"));
   159 					SetTestStepResult(EFail);
   160 					}
   161 				else
   162 					{
   163 					INFO_PRINTF1(_L("Checking Plug-in Selection..."));
   164 					
   165 					CPluginCharsChecker* pluginCheck = CPluginCharsChecker::NewLC();
   166 					
   167 					TPtrC errorMessage;
   168 					
   169 					//Perform plug-in Check
   170 					if(pluginCheck->checkSelectedPlugin(characteristics,
   171 														exAlgorithmUid,
   172 														exImplementationUid,
   173 														errorMessage))
   174 						{
   175 						INFO_PRINTF1(_L("** PASS: Expected Plugin Loaded Successfully **"));
   176 						SetTestStepResult(EPass);	
   177 						}
   178 					else
   179 						{
   180 						ERR_PRINTF2(_L("** FAIL: Unexpected Plugin Implementation Loaded - %S **"),&errorMessage);
   181 						}
   182 					
   183 					CleanupStack::PopAndDestroy(pluginCheck);
   184 				
   185 					}
   186 					
   187 				CleanupStack::PopAndDestroy(impl);
   188 				
   189 				
   190 				}
   191 			else
   192 				{
   193 				ERR_PRINTF2(_L("*** FAIL: Implementation Object Load Failure ***"), err);
   194 				SetTestStepResult(EFail);
   195 				}
   196 				
   197 			if(ruleSelectorToggle)
   198 				{
   199 				//Set the Selector Passing in a pointer to the Default Selector and SPI State	
   200 				CCryptoSpiStateApi::UnsetSelector();	
   201 				
   202 				CleanupStack::PopAndDestroy();
   203 				}
   204 			
   205 			CleanupStack::PopAndDestroy(3,keyStr);	
   206 			}
   207 		
   208 		}
   209 	else
   210 		{
   211 		ERR_PRINTF1(_L("*** FAIL: Test Case Initialistion Failure ***"));	
   212 		}
   213 	
   214 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   215 	
   216 	return TestStepResult();
   217 	}
   218 
   219 
   220 TVerdict CPluginLoadStep::doTestStepPostambleL()
   221 	{
   222 	return TestStepResult();
   223 	}