os/security/cryptoservices/certificateandkeymgmt/tpkixcert_tef/src/validateteststep.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-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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "validateteststep.h"
sl@0
    20
sl@0
    21
sl@0
    22
CValidateCertChainStep::CValidateCertChainStep()
sl@0
    23
	{
sl@0
    24
	SetTestStepName(KValidateCertChainStep);
sl@0
    25
	}
sl@0
    26
sl@0
    27
CValidateCertChainStep::~CValidateCertChainStep()
sl@0
    28
	{
sl@0
    29
	delete iActiveObject;
sl@0
    30
	}
sl@0
    31
sl@0
    32
TVerdict CValidateCertChainStep::doTestStepPreambleL()
sl@0
    33
	{
sl@0
    34
	CPkixCertStepBase::doTestStepPreambleL();
sl@0
    35
	GetIntFromConfig(ConfigSection(), KNumberOfCerts, iNumberOfCerts);
sl@0
    36
	return EPass;
sl@0
    37
	}
sl@0
    38
sl@0
    39
void CValidateCertChainStep::PerformTestL()
sl@0
    40
	{
sl@0
    41
	CPkixCertStepBase::PerformTestL();
sl@0
    42
	
sl@0
    43
	iCertChain->SetSupportedCriticalExtensionsL(iProcessedOids);
sl@0
    44
	
sl@0
    45
	TTime time;
sl@0
    46
	time.UniversalTime();
sl@0
    47
	
sl@0
    48
	ValidateL(time, ConfigSection());
sl@0
    49
	
sl@0
    50
	CleanupStack::PopAndDestroy(iCertChain);
sl@0
    51
sl@0
    52
	}
sl@0
    53
sl@0
    54
sl@0
    55
void CValidateCertChainStep::ValidateL(TTime aValidateTime, TDes& aConfigSection)
sl@0
    56
	{
sl@0
    57
	
sl@0
    58
	CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
sl@0
    59
	
sl@0
    60
	iActiveObject = new (ELeave) CValidateCertChainHelper(Logger());
sl@0
    61
	CleanupStack::PushL(iActiveObject);
sl@0
    62
sl@0
    63
	User::LeaveIfError(iActiveObject->PerformTestL(iCertChain, *result, aValidateTime));
sl@0
    64
sl@0
    65
	CleanupStack::PopAndDestroy(iActiveObject);
sl@0
    66
	iActiveObject = NULL;
sl@0
    67
sl@0
    68
	TInt expectedError;
sl@0
    69
	GetIntFromConfig(aConfigSection, KError, expectedError);
sl@0
    70
		
sl@0
    71
	if ((result->Error()).iReason != expectedError)
sl@0
    72
		{
sl@0
    73
		ERR_PRINTF3(_L("ERROR: Error: %d.  Expected error: %d"), (result->Error()).iReason, expectedError);
sl@0
    74
		User::Leave(KErrGeneral);
sl@0
    75
		}
sl@0
    76
			
sl@0
    77
	if (expectedError != EValidatedOK)
sl@0
    78
		{
sl@0
    79
		//we are expected to error
sl@0
    80
		TInt certIndex;
sl@0
    81
		GetIntFromConfig(aConfigSection, KErrorIndex, certIndex);	
sl@0
    82
		if ((result->Error()).iCert != certIndex)
sl@0
    83
			{			
sl@0
    84
			ERR_PRINTF3(_L("ERROR: Error certificate index: %d.  Expected index: %d"), (result->Error()).iCert, certIndex);
sl@0
    85
			User::Leave(KErrGeneral);			
sl@0
    86
			}
sl@0
    87
		else{
sl@0
    88
			CleanupStack::PopAndDestroy(result);
sl@0
    89
			return; //we have passed
sl@0
    90
			}
sl@0
    91
		}
sl@0
    92
		
sl@0
    93
	
sl@0
    94
	const CArrayFixFlat<TValidationStatus>& warns = result->Warnings();
sl@0
    95
	RArray<TValidationStatus> resultOldWarnings;
sl@0
    96
	CleanupClosePushL(resultOldWarnings);	
sl@0
    97
	TInt i;
sl@0
    98
	for (i=0; i < warns.Count(); ++i)
sl@0
    99
		{
sl@0
   100
		resultOldWarnings.AppendL(warns[i]);
sl@0
   101
		}
sl@0
   102
sl@0
   103
	const RPointerArray<CCertificateValidationWarnings>& certificateWarningsArray = result->ValidationWarnings();	
sl@0
   104
	
sl@0
   105
	HBufC* key;
sl@0
   106
sl@0
   107
	RArray<TValidationStatus> processedPolicyQualifiers;
sl@0
   108
	CleanupClosePushL(processedPolicyQualifiers);		
sl@0
   109
	
sl@0
   110
	TInt qualifierWarning = 18;
sl@0
   111
sl@0
   112
	for (i=0; i < iNumberOfCerts; ++i)
sl@0
   113
		{
sl@0
   114
		key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KPolicyIndex().Length()); 
sl@0
   115
		TPtr ptr(key->Des());
sl@0
   116
		ptr = KCert();
sl@0
   117
		ptr.AppendFormat(KKeyFormat(), i);
sl@0
   118
		ptr.Append(KPolicyIndex());	
sl@0
   119
		
sl@0
   120
		TInt policyIndex = 0;
sl@0
   121
		GetIntFromConfig(aConfigSection, *key, policyIndex);
sl@0
   122
		
sl@0
   123
		if (policyIndex > 0)
sl@0
   124
			{
sl@0
   125
			TValidationError error = (TValidationError) qualifierWarning;
sl@0
   126
			processedPolicyQualifiers.AppendL(TValidationStatus(error, policyIndex));
sl@0
   127
			}
sl@0
   128
sl@0
   129
		CleanupStack::PopAndDestroy(key);
sl@0
   130
		}
sl@0
   131
	
sl@0
   132
	//handle 1st part old warnings first - check any warnings we expect are present (later we check no additionals) 
sl@0
   133
	CheckWarningsPresentAndRemoveL(processedPolicyQualifiers, resultOldWarnings);
sl@0
   134
	
sl@0
   135
	RArray<TInt> configOldWarnings;
sl@0
   136
	CleanupClosePushL(configOldWarnings);
sl@0
   137
	RArray<TValidationStatus> processedConfigOldWarnings;
sl@0
   138
	CleanupClosePushL(processedConfigOldWarnings);		
sl@0
   139
	
sl@0
   140
	RArray<TValidationStatus> configNewWarnings;
sl@0
   141
	CleanupClosePushL(configNewWarnings);
sl@0
   142
	RArray<TPtrC> configCritOids;
sl@0
   143
	CleanupClosePushL(configCritOids);	
sl@0
   144
	
sl@0
   145
	for (TInt cert_index=0; cert_index < iNumberOfCerts; ++cert_index)
sl@0
   146
		{
sl@0
   147
		INFO_PRINTF2(_L("Certificate index: %d"), cert_index);
sl@0
   148
		key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KOldWarnings().Length()); 
sl@0
   149
		TPtr ptr(key->Des());
sl@0
   150
		ptr = KCert();
sl@0
   151
		ptr.AppendFormat(KKeyFormat(), cert_index);
sl@0
   152
		ptr.Append(KOldWarnings());
sl@0
   153
		
sl@0
   154
		GetIntArrayFromConfigL(aConfigSection, *key, configOldWarnings);
sl@0
   155
		
sl@0
   156
		processedConfigOldWarnings.Reset();
sl@0
   157
		for (TInt i=0; i < configOldWarnings.Count(); ++i)
sl@0
   158
			{
sl@0
   159
			TValidationError error = (TValidationError) configOldWarnings[i];
sl@0
   160
			processedConfigOldWarnings.AppendL(TValidationStatus(error, cert_index));
sl@0
   161
			}
sl@0
   162
		
sl@0
   163
		TransferToNewWarningsL(configOldWarnings, configNewWarnings, cert_index);
sl@0
   164
		
sl@0
   165
		ptr.Zero();	
sl@0
   166
		ptr = KCert();
sl@0
   167
		ptr.AppendFormat(KKeyFormat(), cert_index);
sl@0
   168
		ptr.Append(KCritOid());
sl@0
   169
			
sl@0
   170
		GetStringArrayFromConfigL(aConfigSection, *key, configCritOids);
sl@0
   171
		
sl@0
   172
		CleanupStack::PopAndDestroy(key);
sl@0
   173
		
sl@0
   174
		//we now have all our arguments from the config file
sl@0
   175
		
sl@0
   176
		//handle 1st part old warnings first - check any warnings we expect are present (later we check no additionals) 
sl@0
   177
		CheckWarningsPresentAndRemoveL(processedConfigOldWarnings, resultOldWarnings);
sl@0
   178
		
sl@0
   179
		
sl@0
   180
		CCertificateValidationWarnings* certWarning = certificateWarningsArray[((iNumberOfCerts - cert_index) - 1)];	//not owned	//warnings returned in reverse order
sl@0
   181
		
sl@0
   182
		//check cert and certindex match up correctly		
sl@0
   183
		if (certWarning->CertIndex() != cert_index)
sl@0
   184
			{
sl@0
   185
			ERR_PRINTF3(_L("ERROR: Certificate indexes do not match.  Certificate index: %d.  Expected index: %d"), certWarning->CertIndex(), cert_index);
sl@0
   186
			User::Leave(KErrGeneral);
sl@0
   187
			}
sl@0
   188
		
sl@0
   189
		key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KPolicyIndex().Length()); 
sl@0
   190
		TPtr pt(key->Des());
sl@0
   191
		pt = KCert();
sl@0
   192
		pt.AppendFormat(KKeyFormat(), cert_index);
sl@0
   193
		pt.Append(KPolicyIndex());	
sl@0
   194
			
sl@0
   195
		TInt policyIndex = 0;
sl@0
   196
		GetIntFromConfig(aConfigSection, *key, policyIndex);
sl@0
   197
			
sl@0
   198
		if (policyIndex > 0)
sl@0
   199
			{
sl@0
   200
			TValidationError error = (TValidationError) qualifierWarning;
sl@0
   201
			configNewWarnings.AppendL(TValidationStatus(error, policyIndex));
sl@0
   202
			}
sl@0
   203
sl@0
   204
		CleanupStack::PopAndDestroy(key);
sl@0
   205
			
sl@0
   206
		//check new warnings behave correctly
sl@0
   207
		CheckWarningsPresentAndRemoveL(certWarning->Warnings(), configNewWarnings);
sl@0
   208
		
sl@0
   209
		if (configNewWarnings.Count() != 0)
sl@0
   210
			{
sl@0
   211
			for (TInt err=0; err < configNewWarnings.Count(); ++err)
sl@0
   212
				{
sl@0
   213
				ERR_PRINTF2(_L("ERROR: Warning(new API) not reported: %d"), (configNewWarnings[err]).iReason);
sl@0
   214
				}
sl@0
   215
			User::Leave(KErrGeneral);
sl@0
   216
			}
sl@0
   217
		
sl@0
   218
		
sl@0
   219
		//check critoids behave correctly
sl@0
   220
		CheckCriticalExtsPresentAndRemoveL(certWarning->CriticalExtensionsFound(), configCritOids);
sl@0
   221
		if (configCritOids.Count() != 0)
sl@0
   222
			{
sl@0
   223
			for (TInt err=0; err < configCritOids.Count(); ++err)
sl@0
   224
				{
sl@0
   225
				ERR_PRINTF2(_L("ERROR: Critical extension not reported: %S"), &configCritOids[err]);
sl@0
   226
				}			
sl@0
   227
			User::Leave(KErrGeneral);
sl@0
   228
			}
sl@0
   229
			
sl@0
   230
		configOldWarnings.Reset();
sl@0
   231
		configNewWarnings.Reset();
sl@0
   232
		configCritOids.Reset();		
sl@0
   233
		}
sl@0
   234
		
sl@0
   235
	CleanupStack::PopAndDestroy(5, &processedPolicyQualifiers);	
sl@0
   236
		
sl@0
   237
	//check if any warnings in old api we weren't expecting	
sl@0
   238
	if (resultOldWarnings.Count() != 0)
sl@0
   239
		{	
sl@0
   240
		for (TInt warn=0; warn < resultOldWarnings.Count(); ++warn)
sl@0
   241
			{
sl@0
   242
			ERR_PRINTF3(_L("ERROR: Warning(old API) not reported: %d.  Index: %d"), (resultOldWarnings[warn]).iReason, (resultOldWarnings[warn]).iCert);
sl@0
   243
			}		
sl@0
   244
		User::Leave(KErrGeneral);
sl@0
   245
		}
sl@0
   246
	
sl@0
   247
	CleanupStack::PopAndDestroy(2, result);
sl@0
   248
	}
sl@0
   249
sl@0
   250
void CValidateCertChainStep::TransferToNewWarningsL(RArray<TInt>& aOldWarnings, RArray<TValidationStatus>& aNewWarnings, TInt aCertIndex)
sl@0
   251
	{
sl@0
   252
	for (TInt i=0; i < aOldWarnings.Count(); ++i)
sl@0
   253
		{
sl@0
   254
		switch (aOldWarnings[i])
sl@0
   255
			{
sl@0
   256
			case ECriticalExtendedKeyUsage:
sl@0
   257
				break;
sl@0
   258
			case ECriticalPolicyMapping:
sl@0
   259
				break;
sl@0
   260
			case ECriticalDeviceId:
sl@0
   261
				break;
sl@0
   262
			case ECriticalSid:
sl@0
   263
				break;
sl@0
   264
			case ECriticalVid:
sl@0
   265
				break;
sl@0
   266
			case ECriticalCapabilities:
sl@0
   267
				break;
sl@0
   268
			case ECriticalCertPoliciesWithQualifiers:	//handled by ini parameter due to overloading by warning of iCert value
sl@0
   269
				break;
sl@0
   270
			default:
sl@0
   271
				TValidationError error = (TValidationError) aOldWarnings[i];
sl@0
   272
				aNewWarnings.AppendL(TValidationStatus(error, aCertIndex));							
sl@0
   273
			}
sl@0
   274
		}
sl@0
   275
	}
sl@0
   276
sl@0
   277
sl@0
   278
sl@0
   279
sl@0
   280
sl@0
   281
void CValidateCertChainStep::CheckWarningsPresentAndRemoveL(const RArray<TValidationStatus>& aWarningsToCheckFor, RArray<TValidationStatus>& aWarningsToCheckIn)
sl@0
   282
	{
sl@0
   283
	TBool found;
sl@0
   284
	for (TInt warnings_index=0; warnings_index < aWarningsToCheckFor.Count(); ++warnings_index)
sl@0
   285
		{
sl@0
   286
		found = EFalse;
sl@0
   287
		for (TInt k=0; k < aWarningsToCheckIn.Count(); ++k)
sl@0
   288
			{
sl@0
   289
			if (ValidationStatusEqual(aWarningsToCheckFor[warnings_index], aWarningsToCheckIn[k]))
sl@0
   290
				{
sl@0
   291
				found = ETrue;
sl@0
   292
				aWarningsToCheckIn.Remove(k);
sl@0
   293
				break;
sl@0
   294
				}
sl@0
   295
			}
sl@0
   296
				
sl@0
   297
		if (!found)	
sl@0
   298
			{	
sl@0
   299
			ERR_PRINTF2(_L("ERROR: Warning not present: %d"), aWarningsToCheckFor[warnings_index].iReason);
sl@0
   300
			User::Leave(KErrGeneral); 
sl@0
   301
			}
sl@0
   302
		}	
sl@0
   303
	}
sl@0
   304
sl@0
   305
sl@0
   306
TBool CValidateCertChainStep::ValidationStatusEqual(const TValidationStatus& aValidationStatus1, const TValidationStatus& aValidationStatus2)
sl@0
   307
	{
sl@0
   308
	if (aValidationStatus1.iReason != aValidationStatus2.iReason)
sl@0
   309
		{
sl@0
   310
		return EFalse;
sl@0
   311
		}
sl@0
   312
	if (aValidationStatus1.iCert != aValidationStatus2.iCert)
sl@0
   313
		{
sl@0
   314
		return EFalse;
sl@0
   315
		}
sl@0
   316
	return ETrue;	
sl@0
   317
	}
sl@0
   318
sl@0
   319
sl@0
   320
//takes away from aOidsToCheckAgainst
sl@0
   321
void CValidateCertChainStep::CheckCriticalExtsPresentAndRemoveL(const RPointerArray<TDesC>& aOidsToLookFor, RArray<TPtrC>& aOidsToCheckAgainst)
sl@0
   322
	{
sl@0
   323
	TBool found;
sl@0
   324
	for (TInt oid_index=0; oid_index < aOidsToLookFor.Count(); ++oid_index)
sl@0
   325
		{
sl@0
   326
		found = EFalse;
sl@0
   327
		for (TInt k=0; k < aOidsToCheckAgainst.Count(); ++k)
sl@0
   328
			{
sl@0
   329
			if ((*aOidsToLookFor[oid_index])==(aOidsToCheckAgainst[k]))
sl@0
   330
				{
sl@0
   331
				found = ETrue;
sl@0
   332
				aOidsToCheckAgainst.Remove(k);
sl@0
   333
				break;
sl@0
   334
				}
sl@0
   335
			}
sl@0
   336
				
sl@0
   337
		if (!found)	
sl@0
   338
			{
sl@0
   339
			ERR_PRINTF2(_L("ERROR: Critical extension OID not present: %S"), aOidsToLookFor[oid_index]);
sl@0
   340
			User::Leave(KErrGeneral);	
sl@0
   341
			}
sl@0
   342
		}		
sl@0
   343
	}
sl@0
   344
sl@0
   345
sl@0
   346
sl@0
   347
//////////////////////////************
sl@0
   348
sl@0
   349
CValidateCertChainHelper::CValidateCertChainHelper(CTestExecuteLogger& aLogger)
sl@0
   350
:  CActive(EPriorityStandard), iLogger(aLogger)
sl@0
   351
	{
sl@0
   352
	CActiveScheduler::Add(this);
sl@0
   353
	}
sl@0
   354
	
sl@0
   355
	
sl@0
   356
CValidateCertChainHelper::~CValidateCertChainHelper()
sl@0
   357
	{
sl@0
   358
	Cancel();
sl@0
   359
	}
sl@0
   360
	
sl@0
   361
sl@0
   362
TInt CValidateCertChainHelper::PerformTestL(CPKIXCertChain* aCertChain, CPKIXValidationResult& aValidResult, TTime aValidateTime)
sl@0
   363
	{
sl@0
   364
	iCertChain = aCertChain;
sl@0
   365
	
sl@0
   366
	//call async
sl@0
   367
	aCertChain->ValidateL(aValidResult, aValidateTime, iStatus);
sl@0
   368
	
sl@0
   369
	SetActive();
sl@0
   370
   	CActiveScheduler::Start();
sl@0
   371
   	
sl@0
   372
    return iRunError;	
sl@0
   373
	}
sl@0
   374
	
sl@0
   375
	
sl@0
   376
void CValidateCertChainHelper::DoCancel()
sl@0
   377
	{
sl@0
   378
	iCertChain->CancelValidate();
sl@0
   379
	}
sl@0
   380
	
sl@0
   381
	
sl@0
   382
void CValidateCertChainHelper::RunL()
sl@0
   383
	{
sl@0
   384
	iRunError = KErrNone;
sl@0
   385
	User::LeaveIfError(iStatus.Int());
sl@0
   386
	
sl@0
   387
	//processing here
sl@0
   388
sl@0
   389
	
sl@0
   390
	CActiveScheduler::Stop();	
sl@0
   391
	}
sl@0
   392
	
sl@0
   393
sl@0
   394
TInt CValidateCertChainHelper::RunError(TInt aError)
sl@0
   395
	{
sl@0
   396
	iRunError = aError;
sl@0
   397
	//processing here
sl@0
   398
sl@0
   399
	
sl@0
   400
	CActiveScheduler::Stop();
sl@0
   401
	return KErrNone;	
sl@0
   402
	}
sl@0
   403