os/security/cryptoservices/certificateandkeymgmt/tpkixcert_tef/src/pkixcertstepbase.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tpkixcert_tef/src/pkixcertstepbase.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "pkixcertstepbase.h"
    1.23 +#include "cleanuputils.h"
    1.24 +#include <s32file.h>
    1.25 +
    1.26 +CPkixCertStepBase::~CPkixCertStepBase()
    1.27 +	{
    1.28 +	delete iConcatenatedChain;	
    1.29 +	iConcatenatedChain = NULL;
    1.30 +	iFileServer.Close();
    1.31 +	iProcessedOids.Reset();
    1.32 +	iOids.Reset();
    1.33 +	iRootCerts.ResetAndDestroy();
    1.34 +	CActiveScheduler::Install(NULL);
    1.35 +	delete iScheduler;
    1.36 +	}
    1.37 +
    1.38 +TVerdict CPkixCertStepBase::doTestStepPreambleL()
    1.39 +	{
    1.40 +	//read in stuff from config
    1.41 +	
    1.42 +	GetBoolFromConfig(ConfigSection(), KPerformOom, iPerformOom);
    1.43 +
    1.44 +	iFileServer.Connect();	
    1.45 +	TPtrC configString;
    1.46 +
    1.47 +	//temporary storage of the DER encoded certs so we can calculate the combined size for iConcatenatedChain
    1.48 +	RPointerArray<HBufC8> rawCerts;
    1.49 +	CleanupResetAndDestroy<RPointerArray<HBufC8> >::PushL(rawCerts);
    1.50 +	TInt chainSize = 0;
    1.51 +
    1.52 +	GetStringFromConfig(ConfigSection(), KEndEntity, configString);
    1.53 +	HBufC8* certBuf = ReadFileLC(configString);
    1.54 +	rawCerts.AppendL(certBuf);
    1.55 +	CleanupStack::Pop(certBuf);
    1.56 +	chainSize += certBuf->Size();
    1.57 +	
    1.58 +
    1.59 +	RArray<TPtrC> certFileNames;
    1.60 +	CleanupClosePushL(certFileNames);
    1.61 +	
    1.62 +	GetStringArrayFromConfigL(ConfigSection(), KIntermediateCert, certFileNames);
    1.63 +
    1.64 +	TInt i;
    1.65 +	for (i = 0; i < certFileNames.Count(); ++i)
    1.66 +		{
    1.67 +		certBuf = ReadFileLC(certFileNames[i]);
    1.68 +		rawCerts.AppendL(certBuf);
    1.69 +		CleanupStack::Pop(certBuf);
    1.70 +		chainSize += certBuf->Size();
    1.71 +		}
    1.72 +	
    1.73 +	CleanupStack::PopAndDestroy(&certFileNames);	
    1.74 +		
    1.75 +	iConcatenatedChain = HBufC8::NewL(chainSize);
    1.76 +	TPtr8 modPtr = iConcatenatedChain->Des();
    1.77 +	for (i = 0; i < rawCerts.Count(); i++)
    1.78 +		{
    1.79 +		modPtr.Append(*rawCerts[i]);
    1.80 +		}	
    1.81 +	
    1.82 +	CleanupStack::PopAndDestroy(&rawCerts);
    1.83 +	
    1.84 +	///////////
    1.85 +	
    1.86 +	//read in oid values.  the usage of these is dependant on the test step
    1.87 +	GetStringArrayFromConfigL(ConfigSection(), KOid, iOids);
    1.88 +	//convert into argument format used by APIs
    1.89 +	for (i=0; i < iOids.Count(); ++i)
    1.90 +		{
    1.91 +		iProcessedOids.AppendL(&(iOids[i]));
    1.92 +		}
    1.93 +		
    1.94 +		
    1.95 +	RArray<TPtrC> certFileNames2;
    1.96 +	CleanupClosePushL(certFileNames2);
    1.97 +	
    1.98 +	GetStringArrayFromConfigL(ConfigSection(), KRootCert, certFileNames2);
    1.99 +	CX509Certificate* cert;
   1.100 +	for (i = 0; i < certFileNames2.Count(); ++i)
   1.101 +		{
   1.102 +		certBuf = ReadFileLC(certFileNames2[i]);
   1.103 +		cert = CX509Certificate::NewLC(*certBuf);
   1.104 +		iRootCerts.AppendL(cert);
   1.105 +		CleanupStack::Pop(cert);
   1.106 +		CleanupStack::PopAndDestroy(certBuf);
   1.107 +		}
   1.108 +	CleanupStack::PopAndDestroy(&certFileNames2);	
   1.109 +	
   1.110 +	TInt uid;
   1.111 +	iUseUidOverload = GetIntFromConfig(ConfigSection(), KUid, uid);
   1.112 +	iUid.iUid = uid;
   1.113 +	
   1.114 +    iScheduler=new(ELeave) CActiveScheduler(); 
   1.115 +	CActiveScheduler::Install(iScheduler);
   1.116 +	
   1.117 +	return EPass;	
   1.118 +	}
   1.119 +
   1.120 +
   1.121 +//N.B. iCertChain must be popped and destroyed at the end of any deriving class's PerformTestL() method
   1.122 +void CPkixCertStepBase::PerformTestL()
   1.123 +	{
   1.124 +	iPtr.Set(*iConcatenatedChain);
   1.125 +	
   1.126 +	//split on which of the NewLs to use
   1.127 +	if (iUseUidOverload)
   1.128 +		{
   1.129 +		iCertChain = CPKIXCertChain::NewLC(iFileServer, iPtr, iUid);
   1.130 +		}
   1.131 +	else
   1.132 +		{		
   1.133 +		iCertChain = CPKIXCertChain::NewLC(iFileServer, iPtr, iRootCerts);			
   1.134 +		}
   1.135 +	}
   1.136 +	
   1.137 +	
   1.138 +HBufC8* CPkixCertStepBase::ReadFileLC(const TDesC& aFileName)
   1.139 +	{
   1.140 +	RFile file;
   1.141 +	User::LeaveIfError(file.Open(iFileServer, aFileName, EFileRead));
   1.142 +	CleanupClosePushL(file);
   1.143 +    TInt size;
   1.144 +    file.Size(size);
   1.145 +    CleanupStack::PopAndDestroy();//fileClose
   1.146 +
   1.147 +    HBufC8* result = HBufC8::NewLC(size);
   1.148 +    TPtr8 ptr(result->Des());
   1.149 +    ptr.SetLength(size);
   1.150 +
   1.151 +    RFileReadStream stream;
   1.152 +    User::LeaveIfError(stream.Open(iFileServer, aFileName, EFileStream));
   1.153 +    CleanupClosePushL(stream);
   1.154 +    stream.ReadL(ptr, size);
   1.155 +    CleanupStack::PopAndDestroy();//streamClose
   1.156 +    return result;	
   1.157 +	}
   1.158 +
   1.159 +
   1.160 +void CPkixCertStepBase::GetStringArrayFromConfigL(const TDesC& aSectName, const TDesC& aKeyName, RArray<TPtrC>& aArray)
   1.161 +	{
   1.162 +    HBufC* buf = HBufC::NewLC(aKeyName.Length() + KKeyFormat().Length());
   1.163 +    TPtr ptr(buf->Des());
   1.164 +    INFO_PRINTF2(_L("Parsing attribute: %S"), &aKeyName);
   1.165 +
   1.166 +    TInt i = 0;
   1.167 +    TBool cont = ETrue;
   1.168 +    do
   1.169 +        {
   1.170 +        ++i;
   1.171 +        ptr = aKeyName;
   1.172 +        ptr.AppendFormat(KKeyFormat(), i);
   1.173 +        TPtrC val;
   1.174 +
   1.175 +        cont = GetStringFromConfig(aSectName, ptr, val);
   1.176 +        if (cont)
   1.177 +            {
   1.178 +            User::LeaveIfError(aArray.Append(val));
   1.179 +            }
   1.180 +        } while (cont);
   1.181 +
   1.182 +    INFO_PRINTF2(_L("Element count: %d"), i-1);
   1.183 +    CleanupStack::PopAndDestroy(buf);
   1.184 +	}
   1.185 +	
   1.186 +	
   1.187 +void CPkixCertStepBase::GetIntArrayFromConfigL(const TDesC& aSectName, const TDesC& aKeyName, RArray<TInt>& aArray)
   1.188 +	{
   1.189 +	HBufC* buf = HBufC::NewLC(aKeyName.Length() + KKeyFormat().Length());
   1.190 +    TPtr ptr(buf->Des());
   1.191 +    INFO_PRINTF2(_L("Parsing attribute: %S"), &aKeyName);
   1.192 +
   1.193 +    TInt i = 0;
   1.194 +    TBool cont = ETrue;
   1.195 +    do
   1.196 +        {
   1.197 +        ++i;
   1.198 +        ptr = aKeyName;
   1.199 +        ptr.AppendFormat(KKeyFormat(), i);
   1.200 +        TInt val;
   1.201 +
   1.202 +        cont = GetIntFromConfig(aSectName, ptr, val);
   1.203 +        if (cont)
   1.204 +            {
   1.205 +            User::LeaveIfError(aArray.Append(val));
   1.206 +            }
   1.207 +        } while (cont);
   1.208 +
   1.209 +    INFO_PRINTF2(_L("Element count: %d"), i-1);
   1.210 +    CleanupStack::PopAndDestroy(buf);
   1.211 +	}
   1.212 +
   1.213 +TVerdict CPkixCertStepBase::doTestStepL()
   1.214 +	{
   1.215 +	if (!iPerformOom)	
   1.216 +		{
   1.217 +		TRAPD(err, PerformTestL());
   1.218 +		if (err == KErrNone)
   1.219 +			{
   1.220 +			SetTestStepResult(EPass);
   1.221 +			}
   1.222 +		else{
   1.223 +			SetTestStepResult(EFail);
   1.224 +			}
   1.225 +		}
   1.226 +	else
   1.227 +		{
   1.228 + 		PerformOomTestL();
   1.229 +	    }	
   1.230 +
   1.231 +   	return TestStepResult();	
   1.232 +	}
   1.233 +	
   1.234 +		
   1.235 +
   1.236 +void CPkixCertStepBase::PerformOomTestL()
   1.237 +	{
   1.238 + 	for (TInt oomCount = 0; ; oomCount++)
   1.239 + 		{
   1.240 + 		__UHEAP_RESET;
   1.241 + 		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
   1.242 + 		TInt countBefore = User::CountAllocCells();
   1.243 + 		TRAPD(error, PerformTestL());// ----> This is the actual test that runs under OOM conditions.
   1.244 + 		TInt countAfter = User::CountAllocCells();
   1.245 + 		
   1.246 + 		if (countBefore != countAfter)
   1.247 + 			{
   1.248 + 			INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
   1.249 + 			SetTestStepResult(EFail);  
   1.250 + 			break;
   1.251 + 			}
   1.252 + 		INFO_PRINTF2(_L("Result: %d"), error); 			
   1.253 + 		if (error == KErrNone)
   1.254 + 			{
   1.255 +			INFO_PRINTF1(_L("Test outcome : Passed"));
   1.256 +			SetTestStepResult(EPass); 
   1.257 + 			break;
   1.258 + 			}
   1.259 +		}	 	
   1.260 +	}