os/security/cryptoservices/certificateandkeymgmt/tpkixcert_tef/src/pkixcertstepbase.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2008-2009 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 *
    16 */
    17 
    18 
    19 #include "pkixcertstepbase.h"
    20 #include "cleanuputils.h"
    21 #include <s32file.h>
    22 
    23 CPkixCertStepBase::~CPkixCertStepBase()
    24 	{
    25 	delete iConcatenatedChain;	
    26 	iConcatenatedChain = NULL;
    27 	iFileServer.Close();
    28 	iProcessedOids.Reset();
    29 	iOids.Reset();
    30 	iRootCerts.ResetAndDestroy();
    31 	CActiveScheduler::Install(NULL);
    32 	delete iScheduler;
    33 	}
    34 
    35 TVerdict CPkixCertStepBase::doTestStepPreambleL()
    36 	{
    37 	//read in stuff from config
    38 	
    39 	GetBoolFromConfig(ConfigSection(), KPerformOom, iPerformOom);
    40 
    41 	iFileServer.Connect();	
    42 	TPtrC configString;
    43 
    44 	//temporary storage of the DER encoded certs so we can calculate the combined size for iConcatenatedChain
    45 	RPointerArray<HBufC8> rawCerts;
    46 	CleanupResetAndDestroy<RPointerArray<HBufC8> >::PushL(rawCerts);
    47 	TInt chainSize = 0;
    48 
    49 	GetStringFromConfig(ConfigSection(), KEndEntity, configString);
    50 	HBufC8* certBuf = ReadFileLC(configString);
    51 	rawCerts.AppendL(certBuf);
    52 	CleanupStack::Pop(certBuf);
    53 	chainSize += certBuf->Size();
    54 	
    55 
    56 	RArray<TPtrC> certFileNames;
    57 	CleanupClosePushL(certFileNames);
    58 	
    59 	GetStringArrayFromConfigL(ConfigSection(), KIntermediateCert, certFileNames);
    60 
    61 	TInt i;
    62 	for (i = 0; i < certFileNames.Count(); ++i)
    63 		{
    64 		certBuf = ReadFileLC(certFileNames[i]);
    65 		rawCerts.AppendL(certBuf);
    66 		CleanupStack::Pop(certBuf);
    67 		chainSize += certBuf->Size();
    68 		}
    69 	
    70 	CleanupStack::PopAndDestroy(&certFileNames);	
    71 		
    72 	iConcatenatedChain = HBufC8::NewL(chainSize);
    73 	TPtr8 modPtr = iConcatenatedChain->Des();
    74 	for (i = 0; i < rawCerts.Count(); i++)
    75 		{
    76 		modPtr.Append(*rawCerts[i]);
    77 		}	
    78 	
    79 	CleanupStack::PopAndDestroy(&rawCerts);
    80 	
    81 	///////////
    82 	
    83 	//read in oid values.  the usage of these is dependant on the test step
    84 	GetStringArrayFromConfigL(ConfigSection(), KOid, iOids);
    85 	//convert into argument format used by APIs
    86 	for (i=0; i < iOids.Count(); ++i)
    87 		{
    88 		iProcessedOids.AppendL(&(iOids[i]));
    89 		}
    90 		
    91 		
    92 	RArray<TPtrC> certFileNames2;
    93 	CleanupClosePushL(certFileNames2);
    94 	
    95 	GetStringArrayFromConfigL(ConfigSection(), KRootCert, certFileNames2);
    96 	CX509Certificate* cert;
    97 	for (i = 0; i < certFileNames2.Count(); ++i)
    98 		{
    99 		certBuf = ReadFileLC(certFileNames2[i]);
   100 		cert = CX509Certificate::NewLC(*certBuf);
   101 		iRootCerts.AppendL(cert);
   102 		CleanupStack::Pop(cert);
   103 		CleanupStack::PopAndDestroy(certBuf);
   104 		}
   105 	CleanupStack::PopAndDestroy(&certFileNames2);	
   106 	
   107 	TInt uid;
   108 	iUseUidOverload = GetIntFromConfig(ConfigSection(), KUid, uid);
   109 	iUid.iUid = uid;
   110 	
   111     iScheduler=new(ELeave) CActiveScheduler(); 
   112 	CActiveScheduler::Install(iScheduler);
   113 	
   114 	return EPass;	
   115 	}
   116 
   117 
   118 //N.B. iCertChain must be popped and destroyed at the end of any deriving class's PerformTestL() method
   119 void CPkixCertStepBase::PerformTestL()
   120 	{
   121 	iPtr.Set(*iConcatenatedChain);
   122 	
   123 	//split on which of the NewLs to use
   124 	if (iUseUidOverload)
   125 		{
   126 		iCertChain = CPKIXCertChain::NewLC(iFileServer, iPtr, iUid);
   127 		}
   128 	else
   129 		{		
   130 		iCertChain = CPKIXCertChain::NewLC(iFileServer, iPtr, iRootCerts);			
   131 		}
   132 	}
   133 	
   134 	
   135 HBufC8* CPkixCertStepBase::ReadFileLC(const TDesC& aFileName)
   136 	{
   137 	RFile file;
   138 	User::LeaveIfError(file.Open(iFileServer, aFileName, EFileRead));
   139 	CleanupClosePushL(file);
   140     TInt size;
   141     file.Size(size);
   142     CleanupStack::PopAndDestroy();//fileClose
   143 
   144     HBufC8* result = HBufC8::NewLC(size);
   145     TPtr8 ptr(result->Des());
   146     ptr.SetLength(size);
   147 
   148     RFileReadStream stream;
   149     User::LeaveIfError(stream.Open(iFileServer, aFileName, EFileStream));
   150     CleanupClosePushL(stream);
   151     stream.ReadL(ptr, size);
   152     CleanupStack::PopAndDestroy();//streamClose
   153     return result;	
   154 	}
   155 
   156 
   157 void CPkixCertStepBase::GetStringArrayFromConfigL(const TDesC& aSectName, const TDesC& aKeyName, RArray<TPtrC>& aArray)
   158 	{
   159     HBufC* buf = HBufC::NewLC(aKeyName.Length() + KKeyFormat().Length());
   160     TPtr ptr(buf->Des());
   161     INFO_PRINTF2(_L("Parsing attribute: %S"), &aKeyName);
   162 
   163     TInt i = 0;
   164     TBool cont = ETrue;
   165     do
   166         {
   167         ++i;
   168         ptr = aKeyName;
   169         ptr.AppendFormat(KKeyFormat(), i);
   170         TPtrC val;
   171 
   172         cont = GetStringFromConfig(aSectName, ptr, val);
   173         if (cont)
   174             {
   175             User::LeaveIfError(aArray.Append(val));
   176             }
   177         } while (cont);
   178 
   179     INFO_PRINTF2(_L("Element count: %d"), i-1);
   180     CleanupStack::PopAndDestroy(buf);
   181 	}
   182 	
   183 	
   184 void CPkixCertStepBase::GetIntArrayFromConfigL(const TDesC& aSectName, const TDesC& aKeyName, RArray<TInt>& aArray)
   185 	{
   186 	HBufC* buf = HBufC::NewLC(aKeyName.Length() + KKeyFormat().Length());
   187     TPtr ptr(buf->Des());
   188     INFO_PRINTF2(_L("Parsing attribute: %S"), &aKeyName);
   189 
   190     TInt i = 0;
   191     TBool cont = ETrue;
   192     do
   193         {
   194         ++i;
   195         ptr = aKeyName;
   196         ptr.AppendFormat(KKeyFormat(), i);
   197         TInt val;
   198 
   199         cont = GetIntFromConfig(aSectName, ptr, val);
   200         if (cont)
   201             {
   202             User::LeaveIfError(aArray.Append(val));
   203             }
   204         } while (cont);
   205 
   206     INFO_PRINTF2(_L("Element count: %d"), i-1);
   207     CleanupStack::PopAndDestroy(buf);
   208 	}
   209 
   210 TVerdict CPkixCertStepBase::doTestStepL()
   211 	{
   212 	if (!iPerformOom)	
   213 		{
   214 		TRAPD(err, PerformTestL());
   215 		if (err == KErrNone)
   216 			{
   217 			SetTestStepResult(EPass);
   218 			}
   219 		else{
   220 			SetTestStepResult(EFail);
   221 			}
   222 		}
   223 	else
   224 		{
   225  		PerformOomTestL();
   226 	    }	
   227 
   228    	return TestStepResult();	
   229 	}
   230 	
   231 		
   232 
   233 void CPkixCertStepBase::PerformOomTestL()
   234 	{
   235  	for (TInt oomCount = 0; ; oomCount++)
   236  		{
   237  		__UHEAP_RESET;
   238  		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
   239  		TInt countBefore = User::CountAllocCells();
   240  		TRAPD(error, PerformTestL());// ----> This is the actual test that runs under OOM conditions.
   241  		TInt countAfter = User::CountAllocCells();
   242  		
   243  		if (countBefore != countAfter)
   244  			{
   245  			INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
   246  			SetTestStepResult(EFail);  
   247  			break;
   248  			}
   249  		INFO_PRINTF2(_L("Result: %d"), error); 			
   250  		if (error == KErrNone)
   251  			{
   252 			INFO_PRINTF1(_L("Test outcome : Passed"));
   253 			SetTestStepResult(EPass); 
   254  			break;
   255  			}
   256 		}	 	
   257 	}