First public contribution.
2 * Copyright (c) 2002-2009 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 "tpkcs7step.h"
20 #include <test/testexecutelog.h>
21 #include "pkcs7signedobject.h"
22 #include <pkcs7contentinfo_v2.h>
23 #include <pkcs7digestinfo.h>
24 #include <pkcs7encrypteddataobject.h>
25 #include <pkixcertchain.h>
32 CTPKCS7CorruptTest::CTPKCS7CorruptTest()
34 SetTestStepName(KTPKCS7CorruptTest);
37 CTPKCS7ValidTest::CTPKCS7ValidTest()
39 SetTestStepName(KTPKCS7ValidTest);
42 CTPKCS7ContentTest::CTPKCS7ContentTest()
44 SetTestStepName(KTPKCS7ContentTest);
47 CTPKCS7CertificateTest::CTPKCS7CertificateTest()
49 SetTestStepName(KTPKCS7CertificateTest);
52 CTPKCS7SignerTest::CTPKCS7SignerTest()
54 SetTestStepName(KTPKCS7SignerTest);
56 CTPKCS7EncryptedDataTest::CTPKCS7EncryptedDataTest()
58 SetTestStepName(KTPKCS7EncryptedDataTest);
61 CTPKCS7DigestInfoTest::CTPKCS7DigestInfoTest()
63 SetTestStepName(KTPKCS7DigestInfoTest);
66 CTPKCS7EncryptedDataCorruptTest::CTPKCS7EncryptedDataCorruptTest()
68 SetTestStepName(KTPKCS7EncryptedDataCorruptTest);
71 CTPKCS7DigestInfoCorruptTest::CTPKCS7DigestInfoCorruptTest()
73 SetTestStepName(KTPKCS7DigestInfoCorruptTest);
76 CTPKCS7OOMTest::CTPKCS7OOMTest()
78 SetTestStepName(KTPKCS7OOMTest);
81 TVerdict CTPKCS7OOMTest::doTestStepL()
83 TVerdict verdict = EFail;
86 for (TInt oomCount = 0; ; oomCount++)
89 __UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
90 countBefore = User::CountAllocCells();
91 TRAPD(error, doTestOOML());
92 countAfter = User::CountAllocCells();
94 if (error != KErrNoMemory)
97 INFO_PRINTF2(_L("OOM Status %d"),error);
98 INFO_PRINTF1(_L("Test outcome : Passed"));
103 if (countBefore != countAfter)
105 INFO_PRINTF2(_L("OOM Status %d"),error);
106 INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
111 INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
113 INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
114 SetTestStepResult(verdict);
118 void CTPKCS7OOMTest::doTestOOML()
121 GetIntFromConfig(ConfigSection(),_L("OOMTestNo"),OOMTest);
126 CPKCS7ContentInfo* theTest = NULL;
127 theTest = CPKCS7ContentInfo::NewL(iRawData->Des());
131 else if (OOMTest == 2)
133 CPKCS7DigestInfo* theTest0 = NULL;
134 theTest0 = CPKCS7DigestInfo::NewL(iRawData->Des());
138 else if (OOMTest == 3)
140 CPKCS7ContentInfo* theTest1 = NULL;
141 theTest1 = CPKCS7ContentInfo::NewL(iRawData->Des());
143 CleanupStack::PushL(theTest1);
144 CPKCS7EncryptedDataObject* theTest2 = NULL;
145 theTest2 = CPKCS7EncryptedDataObject::NewL(*theTest1);
147 CleanupStack::PopAndDestroy(theTest1);
155 TVerdict CTPKCS7CorruptTest::doTestStepL()
157 if (TestStepResult() != EPass)
159 return TestStepResult();
163 if (GetIntFromConfig(ConfigSection(),_L("Reason"),reason) == EFalse)
170 //Use CPKCS7ContentInfo class in order to obtain the ContentType and ContentData.
171 //Depending on the ContentType the corresponding Object of that ContentType is created.
172 CPKCS7ContentInfo * contentInfo = NULL;
173 TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
175 //Checks if the file is empty, Corrupt
179 SetTestStepResult(EPass);
180 INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
181 return TestStepResult();
186 //ContentType is SignedData
187 if( contentInfo->ContentType() == KPkcs7SignedData)
189 CPKCS7SignedObject * p7 = NULL;
190 TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
198 SetTestStepResult(EFail);
199 INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
201 //Not corrupt , repeat files
202 return TestStepResult();
206 TVerdict CTPKCS7ValidTest::doTestStepL()
208 if (TestStepResult() != EPass)
210 return TestStepResult();
215 if (GetBoolFromConfig(ConfigSection(),_L("IsValid"), expectedValid) == EFalse)
217 expectedValid = ETrue;
222 CPKCS7ContentInfo * contentInfo = NULL;
223 TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
227 CPKCS7SignedObject * p7 = NULL;
228 if( contentInfo->ContentType() == KPkcs7SignedData)
230 TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
231 //expired, and the case where certificate chain root is not on the device
236 SetTestStepResult(EPass);
237 INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
238 return TestStepResult();
244 SetTestStepResult(EFail);
245 INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
249 CleanupStack::PushL (p7);
251 const RPointerArray<CPKCS7SignerInfo>& signers = p7->SignerInfo();
252 TBool isValid = EFalse;
253 HBufC8* certificateEncoding = NULL;
254 if(!p7->ValidateSignerL(*signers[0], certificateEncoding))
256 INFO_PRINTF1(_L("Couldn't validate signer"));
260 CActiveScheduler* sched = NULL;
261 if (CActiveScheduler::Current() == NULL)
263 INFO_PRINTF1(_L("Installing scheduler"));
264 sched = new (ELeave) CActiveScheduler();
265 CleanupStack::PushL (sched);
267 CActiveScheduler::Install (sched);
269 RPointerArray<CX509Certificate> roots (&iRootCertificate, 1);
270 CPKIXCertChain * chain = CPKIXCertChain::NewLC(iFs, *certificateEncoding, roots);
272 _LIT(KDateCorrect1,"20040801:");
273 TBuf <24> theDate(KDateCorrect1);
274 TInt err=tm.Set(theDate);
279 CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
280 CTPKCS7Validator* validator = new (ELeave) CTPKCS7Validator (chain, result, &tm);
281 CleanupStack::PushL (validator);
282 validator->doValidate ();
284 if (result->Error().iReason == EValidatedOK)
287 INFO_PRINTF1(_L("Validation success"));
291 INFO_PRINTF2(_L("Validation failed: %d"), result->Error().iReason);
294 CleanupStack::PopAndDestroy(validator);
295 CleanupStack::PopAndDestroy(result);
296 CleanupStack::PopAndDestroy(chain);
299 CActiveScheduler::Install (NULL);
300 CleanupStack::PopAndDestroy (sched);
303 if (certificateEncoding)
305 CleanupStack::PopAndDestroy(certificateEncoding);
307 CleanupStack::PopAndDestroy (p7);
308 if (isValid != expectedValid)
310 SetTestStepResult(EFail);
313 INFO_PRINTF1(_L("Expected valid, got invalid"));
317 INFO_PRINTF1(_L("Expected invalid, got valid"));
326 SetTestStepResult(EFail);
331 return TestStepResult();
336 TVerdict CTPKCS7ContentTest::doTestStepL()
338 if (TestStepResult() != EPass)
340 return TestStepResult();
348 TPtrC expectedString;
349 HBufC8 * expectedContent = NULL;
351 checkType = GetIntFromConfig (ConfigSection(),_L("ContentType"), expectedType);
352 checkLength = GetIntFromConfig (ConfigSection(),_L("ContentLength"), expectedLength);
353 checkContent = GetStringFromConfig(ConfigSection(),_L("ContentString"), expectedString);
357 if (expectedString.Length() & 1)
359 SetTestStepResult(ETestSuiteError);
360 INFO_PRINTF1(_L("ContentString is not a multiple of 2 long"));
361 checkContent = EFalse;
365 // workaround to parse the expectedstring
366 expectedContent = HBufC8::NewLC (expectedString.Length()/2);
367 TPtr8 des = expectedContent->Des ();
368 for (TInt i = 0; i < expectedString.Length()/2; i++)
370 TInt top = expectedString[2*i];
371 TInt low = expectedString[2*i+1];
372 if (top >= 0x61) top -= (0x61-10);
374 if (low >= 0x61) low -= (0x61-10);
376 des.Append (top*16+low);
382 CPKCS7ContentInfo * contentInfo = NULL;
383 TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
386 CPKCS7SignedObject * p7 = NULL;
387 if( contentInfo->ContentType() == 2)
389 TRAPD (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
393 SetTestStepResult(EFail);
394 INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
398 CleanupStack::PushL (p7);
399 const CPKCS7ContentInfo& p7info = p7->ContentInfo ();
402 if (p7info.ContentType() != expectedType)
404 SetTestStepResult(EFail);
405 INFO_PRINTF3(_L("Expected ContentType %d, got %d"), expectedType, p7info.ContentType());
408 const TDesC8& content = p7info.ContentData();
412 if (content.Length() != expectedLength)
414 SetTestStepResult(EFail);
415 INFO_PRINTF3(_L("Expected ContentLength %d, got %d"), expectedLength, content.Length());
420 if (content.Length() != expectedContent->Length())
422 SetTestStepResult(EFail);
423 INFO_PRINTF3(_L("Expected ContentString length %d does not correspond to PKCS7 data length %d"),
424 expectedContent->Length(), content.Length());
428 if (expectedContent->Compare(content) != 0)
430 SetTestStepResult(EFail);
431 INFO_PRINTF1(_L("Expected ContentString does not match PKCS7 content"));
435 CleanupStack::PopAndDestroy (p7);
442 SetTestStepResult(EFail);
447 CleanupStack::PopAndDestroy (expectedContent);
450 if (!checkType && !checkLength && !checkContent && (TestStepResult()==EPass))
452 SetTestStepResult(EInconclusive);
453 INFO_PRINTF1(_L("No test performed in this step"));
456 return TestStepResult();
459 // Step 4 Certificate
460 TVerdict CTPKCS7CertificateTest::doTestStepL()
462 if (TestStepResult() != EPass)
464 return TestStepResult();
470 checkCount = GetIntFromConfig (ConfigSection(),_L("CertificateCount"), expectedCount);
475 CPKCS7ContentInfo * contentInfo = NULL;
476 TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
480 CPKCS7SignedObject * p7 = NULL;
481 if( contentInfo->ContentType() == 2)
483 TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
487 SetTestStepResult(EFail);
488 INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
492 CleanupStack::PushL (p7);
493 const RPointerArray<CPKCS7ExtendedCertificateOrCertificate>& certificates = p7->Certificates();
496 if (certificates.Count() != expectedCount)
498 SetTestStepResult(EFail);
499 INFO_PRINTF3(_L("Expected %d certificates, got %d"), expectedCount, certificates.Count());
502 CleanupStack::PopAndDestroy (p7);
509 SetTestStepResult(EFail);
512 if (!checkCount && (TestStepResult()==EPass))
514 SetTestStepResult(EInconclusive);
515 INFO_PRINTF1(_L("No test performed in this step"));
518 return TestStepResult();
522 TVerdict CTPKCS7SignerTest::doTestStepL()
524 if (TestStepResult() != EPass)
526 return TestStepResult();
532 checkCount = GetIntFromConfig (ConfigSection(),_L("SignerCount"), expectedCount);
536 CPKCS7ContentInfo * contentInfo = NULL;
537 TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
540 CPKCS7SignedObject * p7 = NULL;
541 if( contentInfo->ContentType() == 2)
543 TRAPD (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
547 SetTestStepResult(EFail);
548 INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
552 CleanupStack::PushL (p7);
553 const RPointerArray<CPKCS7SignerInfo>& signers = p7->SignerInfo();
556 if (signers.Count() != expectedCount)
558 SetTestStepResult(EFail);
559 INFO_PRINTF3(_L("Expected %d signers, got %d"), expectedCount, signers.Count());
562 CleanupStack::PopAndDestroy (p7);
569 if (!checkCount && (TestStepResult()==EPass))
571 SetTestStepResult(EInconclusive);
572 INFO_PRINTF1(_L("No test performed in this step"));
575 return TestStepResult();
581 TVerdict CTPKCS7EncryptedDataTest::doTestStepL()
584 if (TestStepResult() != EPass)
586 return TestStepResult();
590 // Read from INI file.
591 GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
597 checkValid = GetBoolFromConfig(ConfigSection(),_L("IsValid"), expectedValid);
600 CPKCS7ContentInfo *contentInfo = NULL;
601 TRAPD (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
602 CleanupStack::PushL(contentInfo);
606 CPKCS7EncryptedDataObject* p7 = NULL;
607 TInt type = contentInfo->ContentType();
608 if( contentInfo->ContentType() == CPKCS7ContentInfo::EContentTypeEncryptedData)
610 TRAPD (err, p7 = CPKCS7EncryptedDataObject::NewL(*contentInfo));
611 CleanupStack::PushL(p7);
616 if(!expectedValid && err == reason)
618 CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
619 return TestStepResult();
621 if(!expectedValid && contentInfo->ContentType() == 1)
623 CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
624 return TestStepResult();
630 SetTestStepResult(EFail);
631 ERR_PRINTF2(_L("Got %d building PKCS7 object"), err);
635 CPKCS7EncryptedDataObject::TContentType contentType = p7->ContentType();
636 if(contentType != CPKCS7EncryptedDataObject::EPkcs7Data)
638 SetTestStepResult(EFail);
639 ERR_PRINTF1(_L("ContentType does not match"));
640 CleanupStack::PopAndDestroy(3,contentInfo);//expectedContentType, p7, contentInfo
641 return TestStepResult();
645 INFO_PRINTF1(_L("ContentType matches"));
648 //ENCRYPTEDCONTENT DATA
649 const TDesC8& contentData = p7->EncryptedContentInfoData();
650 if(contentData.Compare(*iContentData) != 0)
652 SetTestStepResult(EFail);
653 ERR_PRINTF1(_L("EncryptedContentData does not match"));
654 CleanupStack::PopAndDestroy(2,contentInfo); //p7, contentInfo
655 return TestStepResult();
659 INFO_PRINTF1(_L("EncryptedContentData matches"));
664 const CPBEncryptParms& encryptParams = p7->EncryptParams();
665 if((encryptParams.Salt()).Compare(*iSaltValue) != 0)
667 SetTestStepResult(EFail);
668 ERR_PRINTF1(_L("Salt Value does not match"));
669 CleanupStack::PopAndDestroy(2, contentInfo);//p7,contentInfo
670 return TestStepResult();
674 INFO_PRINTF1(_L("Salt Value matches"));
679 TBool checkIteration;
680 TInt expectedIterationCount;
682 checkIteration = GetIntFromConfig(ConfigSection(),_L("Iteration"), expectedIterationCount);
685 if(encryptParams.Iterations() != expectedIterationCount)
687 SetTestStepResult(EFail);
688 ERR_PRINTF1(_L("Iterations does not match"));
689 CleanupStack::PopAndDestroy(2, contentInfo);//p7, contentInfo
690 return TestStepResult();
694 INFO_PRINTF1(_L("Iteration matches"));
700 TPtrC expectedCipher;
701 TPBECipher cipherValue = (TPBECipher) -1;//assign to impossible value
703 checkCipher = GetStringFromConfig(ConfigSection(),_L("Algorithm"), expectedCipher);
707 if(expectedCipher.Compare(_L("pbeWithSHA1And40BitRC2_CBC")) == 0)
709 cipherValue = ECipherRC2_CBC_40_5;
711 else if(expectedCipher.Compare(_L("pbeWithSHA1And128BitRC4")) == 0)
713 cipherValue = ECipherARC4_128;
715 else if(expectedCipher.Compare(_L("pbeWithSHA1AndDES-CBC")) == 0)
717 cipherValue = ECipherDES_CBC;
719 else if(expectedCipher.Compare(_L("pbeWithSHAAnd3_KeyTripleDES_CBC")) == 0)
721 cipherValue = ECipher3DES_CBC;
723 else if(expectedCipher.Compare(_L("pbeWithSHAAnd2-KeyTripleDES-CBC"))==0)
725 cipherValue = ECipher2Key3DES_CBC;
727 else if(expectedCipher.Compare(_L("pbeWithSHA1And40BitRC4")) == 0)
729 cipherValue = ECipherARC4_40;
731 else if(expectedCipher.Compare(_L("pbewithSHAAnd128BitRC2CBC")) == 0)
733 cipherValue = ECipherRC2_CBC_128_16;
735 else if(expectedCipher.Compare(_L("pbeWithSHA1AndRC2_CBC")) == 0)
737 cipherValue = ECipherRC2_CBC_40;
740 if(encryptParams.Cipher() != cipherValue)
742 SetTestStepResult(EFail);
743 ERR_PRINTF1(_L("Cipher does not match"));
744 CleanupStack::PopAndDestroy(3, contentInfo);//expectedCipher,p7,contentInfo
745 return TestStepResult();
749 INFO_PRINTF1(_L("Cipher matches"));
753 if(contentData.Length() != 0)
759 checkPassword = GetStringFromConfig(ConfigSection(),_L("Password"), password);
763 HBufC8* plainText = p7->DecryptDataL(password);
765 SetTestStepResult(EPass);
768 CleanupStack::PopAndDestroy(p7);
770 //If ContentType is not Encrypted Data
776 if(!expectedValid && err == reason)
778 CleanupStack::PopAndDestroy(contentInfo);
779 return TestStepResult();
783 SetTestStepResult(EFail);
789 else if(err != reason)
791 SetTestStepResult(EFail);
793 CleanupStack::PopAndDestroy(contentInfo);
796 return TestStepResult();
801 TVerdict CTPKCS7DigestInfoTest::doTestStepL()
803 if (TestStepResult() != EPass)
805 return TestStepResult();
810 // Read from INI file.
811 GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
813 CPKCS7DigestInfo* p7 = NULL;
814 TRAPD (err, p7 = CPKCS7DigestInfo::NewL(iRawData->Des()));
815 CleanupStack::PushL(p7);
821 CleanupStack::PopAndDestroy(p7);
822 SetTestStepResult(EPass);
823 return TestStepResult();
827 SetTestStepResult(EFail);
828 ERR_PRINTF2(_L("Got %d building PKCS7 object"), err);
833 const TDesC8& digest = p7->Digest();
834 if(digest.Compare(*iDigest) != 0)
836 SetTestStepResult(EFail);
837 ERR_PRINTF1(_L("Digest does not match"));
841 INFO_PRINTF1(_L("Digest matches"));
845 TBool checkAlgorithm;
846 TPtrC expectedAlgorithm;
847 TAlgorithmId algorithmId = (TAlgorithmId) -1;
849 checkAlgorithm = GetStringFromConfig(ConfigSection(),_L("AlgorithmId"), expectedAlgorithm);
853 if(expectedAlgorithm.Compare(_L("SHA1")) == 0)
857 else if(expectedAlgorithm.Compare(_L("MD5")) == 0)
862 if(algorithmId != p7->Algorithm())
864 SetTestStepResult(EFail);
865 ERR_PRINTF1(_L("Algorithm Id does not match"));
869 INFO_PRINTF1(_L("Algorithm Id matches"));
874 const TDesC8& encodedParams= p7->EncodedParams();
876 if(encodedParams.Length() != 0)
878 if(encodedParams.Compare(*iEncodedParams))
880 SetTestStepResult(EFail);
881 ERR_PRINTF1(_L("EncodedParams does not match"));
885 INFO_PRINTF1(_L("EncodedParams match"));
888 else if(!iEncodedParams)
890 INFO_PRINTF1(_L("EncodedParams not present"));
894 CleanupStack::PopAndDestroy(p7);
896 return TestStepResult();
900 //EncryptedDataCorrupt
902 TVerdict CTPKCS7EncryptedDataCorruptTest::doTestStepL()
904 if (TestStepResult() != EPass)
906 return TestStepResult();
910 GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
912 CPKCS7ContentInfo *contentInfo = NULL;
913 TRAPD (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
914 CleanupStack::PushL(contentInfo);
920 SetTestStepResult(EFail);
921 INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
928 CleanupStack::PopAndDestroy(contentInfo);
929 return TestStepResult();
932 CPKCS7EncryptedDataObject* p7 = NULL;
933 TRAPD (err1, p7 = CPKCS7EncryptedDataObject::NewL(*contentInfo));
934 CleanupStack::PushL(p7);
938 SetTestStepResult(EFail);
939 INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
941 CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
943 return TestStepResult();
948 TVerdict CTPKCS7DigestInfoCorruptTest::doTestStepL()
950 if (TestStepResult() != EPass)
952 return TestStepResult();
956 GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
958 CPKCS7DigestInfo* p7 = NULL;
959 TRAPD (err, p7 = CPKCS7DigestInfo::NewL(iRawData->Des()));
964 SetTestStepResult(EFail);
965 INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
968 return TestStepResult();