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