os/security/cryptoservices/certificateandkeymgmt/tpkixcert_tef/src/validateteststep.cpp
First public contribution.
2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "validateteststep.h"
22 CValidateCertChainStep::CValidateCertChainStep()
24 SetTestStepName(KValidateCertChainStep);
27 CValidateCertChainStep::~CValidateCertChainStep()
32 TVerdict CValidateCertChainStep::doTestStepPreambleL()
34 CPkixCertStepBase::doTestStepPreambleL();
35 GetIntFromConfig(ConfigSection(), KNumberOfCerts, iNumberOfCerts);
39 void CValidateCertChainStep::PerformTestL()
41 CPkixCertStepBase::PerformTestL();
43 iCertChain->SetSupportedCriticalExtensionsL(iProcessedOids);
48 ValidateL(time, ConfigSection());
50 CleanupStack::PopAndDestroy(iCertChain);
55 void CValidateCertChainStep::ValidateL(TTime aValidateTime, TDes& aConfigSection)
58 CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
60 iActiveObject = new (ELeave) CValidateCertChainHelper(Logger());
61 CleanupStack::PushL(iActiveObject);
63 User::LeaveIfError(iActiveObject->PerformTestL(iCertChain, *result, aValidateTime));
65 CleanupStack::PopAndDestroy(iActiveObject);
69 GetIntFromConfig(aConfigSection, KError, expectedError);
71 if ((result->Error()).iReason != expectedError)
73 ERR_PRINTF3(_L("ERROR: Error: %d. Expected error: %d"), (result->Error()).iReason, expectedError);
74 User::Leave(KErrGeneral);
77 if (expectedError != EValidatedOK)
79 //we are expected to error
81 GetIntFromConfig(aConfigSection, KErrorIndex, certIndex);
82 if ((result->Error()).iCert != certIndex)
84 ERR_PRINTF3(_L("ERROR: Error certificate index: %d. Expected index: %d"), (result->Error()).iCert, certIndex);
85 User::Leave(KErrGeneral);
88 CleanupStack::PopAndDestroy(result);
89 return; //we have passed
94 const CArrayFixFlat<TValidationStatus>& warns = result->Warnings();
95 RArray<TValidationStatus> resultOldWarnings;
96 CleanupClosePushL(resultOldWarnings);
98 for (i=0; i < warns.Count(); ++i)
100 resultOldWarnings.AppendL(warns[i]);
103 const RPointerArray<CCertificateValidationWarnings>& certificateWarningsArray = result->ValidationWarnings();
107 RArray<TValidationStatus> processedPolicyQualifiers;
108 CleanupClosePushL(processedPolicyQualifiers);
110 TInt qualifierWarning = 18;
112 for (i=0; i < iNumberOfCerts; ++i)
114 key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KPolicyIndex().Length());
115 TPtr ptr(key->Des());
117 ptr.AppendFormat(KKeyFormat(), i);
118 ptr.Append(KPolicyIndex());
120 TInt policyIndex = 0;
121 GetIntFromConfig(aConfigSection, *key, policyIndex);
125 TValidationError error = (TValidationError) qualifierWarning;
126 processedPolicyQualifiers.AppendL(TValidationStatus(error, policyIndex));
129 CleanupStack::PopAndDestroy(key);
132 //handle 1st part old warnings first - check any warnings we expect are present (later we check no additionals)
133 CheckWarningsPresentAndRemoveL(processedPolicyQualifiers, resultOldWarnings);
135 RArray<TInt> configOldWarnings;
136 CleanupClosePushL(configOldWarnings);
137 RArray<TValidationStatus> processedConfigOldWarnings;
138 CleanupClosePushL(processedConfigOldWarnings);
140 RArray<TValidationStatus> configNewWarnings;
141 CleanupClosePushL(configNewWarnings);
142 RArray<TPtrC> configCritOids;
143 CleanupClosePushL(configCritOids);
145 for (TInt cert_index=0; cert_index < iNumberOfCerts; ++cert_index)
147 INFO_PRINTF2(_L("Certificate index: %d"), cert_index);
148 key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KOldWarnings().Length());
149 TPtr ptr(key->Des());
151 ptr.AppendFormat(KKeyFormat(), cert_index);
152 ptr.Append(KOldWarnings());
154 GetIntArrayFromConfigL(aConfigSection, *key, configOldWarnings);
156 processedConfigOldWarnings.Reset();
157 for (TInt i=0; i < configOldWarnings.Count(); ++i)
159 TValidationError error = (TValidationError) configOldWarnings[i];
160 processedConfigOldWarnings.AppendL(TValidationStatus(error, cert_index));
163 TransferToNewWarningsL(configOldWarnings, configNewWarnings, cert_index);
167 ptr.AppendFormat(KKeyFormat(), cert_index);
168 ptr.Append(KCritOid());
170 GetStringArrayFromConfigL(aConfigSection, *key, configCritOids);
172 CleanupStack::PopAndDestroy(key);
174 //we now have all our arguments from the config file
176 //handle 1st part old warnings first - check any warnings we expect are present (later we check no additionals)
177 CheckWarningsPresentAndRemoveL(processedConfigOldWarnings, resultOldWarnings);
180 CCertificateValidationWarnings* certWarning = certificateWarningsArray[((iNumberOfCerts - cert_index) - 1)]; //not owned //warnings returned in reverse order
182 //check cert and certindex match up correctly
183 if (certWarning->CertIndex() != cert_index)
185 ERR_PRINTF3(_L("ERROR: Certificate indexes do not match. Certificate index: %d. Expected index: %d"), certWarning->CertIndex(), cert_index);
186 User::Leave(KErrGeneral);
189 key = HBufC::NewLC(KCert().Length() + KKeyFormat().Length() + KPolicyIndex().Length());
192 pt.AppendFormat(KKeyFormat(), cert_index);
193 pt.Append(KPolicyIndex());
195 TInt policyIndex = 0;
196 GetIntFromConfig(aConfigSection, *key, policyIndex);
200 TValidationError error = (TValidationError) qualifierWarning;
201 configNewWarnings.AppendL(TValidationStatus(error, policyIndex));
204 CleanupStack::PopAndDestroy(key);
206 //check new warnings behave correctly
207 CheckWarningsPresentAndRemoveL(certWarning->Warnings(), configNewWarnings);
209 if (configNewWarnings.Count() != 0)
211 for (TInt err=0; err < configNewWarnings.Count(); ++err)
213 ERR_PRINTF2(_L("ERROR: Warning(new API) not reported: %d"), (configNewWarnings[err]).iReason);
215 User::Leave(KErrGeneral);
219 //check critoids behave correctly
220 CheckCriticalExtsPresentAndRemoveL(certWarning->CriticalExtensionsFound(), configCritOids);
221 if (configCritOids.Count() != 0)
223 for (TInt err=0; err < configCritOids.Count(); ++err)
225 ERR_PRINTF2(_L("ERROR: Critical extension not reported: %S"), &configCritOids[err]);
227 User::Leave(KErrGeneral);
230 configOldWarnings.Reset();
231 configNewWarnings.Reset();
232 configCritOids.Reset();
235 CleanupStack::PopAndDestroy(5, &processedPolicyQualifiers);
237 //check if any warnings in old api we weren't expecting
238 if (resultOldWarnings.Count() != 0)
240 for (TInt warn=0; warn < resultOldWarnings.Count(); ++warn)
242 ERR_PRINTF3(_L("ERROR: Warning(old API) not reported: %d. Index: %d"), (resultOldWarnings[warn]).iReason, (resultOldWarnings[warn]).iCert);
244 User::Leave(KErrGeneral);
247 CleanupStack::PopAndDestroy(2, result);
250 void CValidateCertChainStep::TransferToNewWarningsL(RArray<TInt>& aOldWarnings, RArray<TValidationStatus>& aNewWarnings, TInt aCertIndex)
252 for (TInt i=0; i < aOldWarnings.Count(); ++i)
254 switch (aOldWarnings[i])
256 case ECriticalExtendedKeyUsage:
258 case ECriticalPolicyMapping:
260 case ECriticalDeviceId:
266 case ECriticalCapabilities:
268 case ECriticalCertPoliciesWithQualifiers: //handled by ini parameter due to overloading by warning of iCert value
271 TValidationError error = (TValidationError) aOldWarnings[i];
272 aNewWarnings.AppendL(TValidationStatus(error, aCertIndex));
281 void CValidateCertChainStep::CheckWarningsPresentAndRemoveL(const RArray<TValidationStatus>& aWarningsToCheckFor, RArray<TValidationStatus>& aWarningsToCheckIn)
284 for (TInt warnings_index=0; warnings_index < aWarningsToCheckFor.Count(); ++warnings_index)
287 for (TInt k=0; k < aWarningsToCheckIn.Count(); ++k)
289 if (ValidationStatusEqual(aWarningsToCheckFor[warnings_index], aWarningsToCheckIn[k]))
292 aWarningsToCheckIn.Remove(k);
299 ERR_PRINTF2(_L("ERROR: Warning not present: %d"), aWarningsToCheckFor[warnings_index].iReason);
300 User::Leave(KErrGeneral);
306 TBool CValidateCertChainStep::ValidationStatusEqual(const TValidationStatus& aValidationStatus1, const TValidationStatus& aValidationStatus2)
308 if (aValidationStatus1.iReason != aValidationStatus2.iReason)
312 if (aValidationStatus1.iCert != aValidationStatus2.iCert)
320 //takes away from aOidsToCheckAgainst
321 void CValidateCertChainStep::CheckCriticalExtsPresentAndRemoveL(const RPointerArray<TDesC>& aOidsToLookFor, RArray<TPtrC>& aOidsToCheckAgainst)
324 for (TInt oid_index=0; oid_index < aOidsToLookFor.Count(); ++oid_index)
327 for (TInt k=0; k < aOidsToCheckAgainst.Count(); ++k)
329 if ((*aOidsToLookFor[oid_index])==(aOidsToCheckAgainst[k]))
332 aOidsToCheckAgainst.Remove(k);
339 ERR_PRINTF2(_L("ERROR: Critical extension OID not present: %S"), aOidsToLookFor[oid_index]);
340 User::Leave(KErrGeneral);
347 //////////////////////////************
349 CValidateCertChainHelper::CValidateCertChainHelper(CTestExecuteLogger& aLogger)
350 : CActive(EPriorityStandard), iLogger(aLogger)
352 CActiveScheduler::Add(this);
356 CValidateCertChainHelper::~CValidateCertChainHelper()
362 TInt CValidateCertChainHelper::PerformTestL(CPKIXCertChain* aCertChain, CPKIXValidationResult& aValidResult, TTime aValidateTime)
364 iCertChain = aCertChain;
367 aCertChain->ValidateL(aValidResult, aValidateTime, iStatus);
370 CActiveScheduler::Start();
376 void CValidateCertChainHelper::DoCancel()
378 iCertChain->CancelValidate();
382 void CValidateCertChainHelper::RunL()
384 iRunError = KErrNone;
385 User::LeaveIfError(iStatus.Int());
390 CActiveScheduler::Stop();
394 TInt CValidateCertChainHelper::RunError(TInt aError)
400 CActiveScheduler::Stop();