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