sl@0: /* sl@0: * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "t_testcertfordeletable.h" sl@0: #include "t_certstoredefs.h" sl@0: #include "t_input.h" sl@0: #include "t_certstoreout.h" sl@0: sl@0: #include "t_WritableCSDummies.h" sl@0: sl@0: sl@0: CTestAction* CTestCertForDeletable::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestCertForDeletable* self = new(ELeave) CTestCertForDeletable(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CTestCertForDeletable::~CTestCertForDeletable() sl@0: { sl@0: delete iCertificate; sl@0: delete iCertificateURL; sl@0: delete iCertificateContent; sl@0: delete iCertificateLabel; sl@0: } sl@0: sl@0: void CTestCertForDeletable::TestSerializationL( MCTToken& aToken, sl@0: TKeyIdentifier* aIssuerKeyId, sl@0: TKeyIdentifier* aSubjectKeyId, sl@0: const TInt aCertificateId, sl@0: const TDesC8* aIssuerHash ) sl@0: { sl@0: CCTCertInfo* cert = CCTCertInfo::NewLC( *iCertificateLabel, // const TDesC& sl@0: iCertificateFormat, // TCertificateFormat sl@0: iOwnerType, // TCertificateOwnerType sl@0: 999, // TInt aSize | aCert.Length() sl@0: aSubjectKeyId, // const TKeyIdentifier* sl@0: aIssuerKeyId, // const TKeyIdentifier* sl@0: aToken, // MCTToken& sl@0: aCertificateId, // TInt aCertificateId sl@0: iDeletable, // TBool sl@0: aIssuerHash ); // const TDesC8* aIssuerHash = NULL sl@0: sl@0: // cert -> stack sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); // . fs -> stack sl@0: sl@0: // this is a name of the file that will be opened as sl@0: // a stream to test externalize/internalize sl@0: TDriveUnit sysDrive (fs.GetSystemDrive()); sl@0: TBuf<24> fileName (sysDrive.Name()); sl@0: fileName.Append(_L("\\certinfo.dat")); sl@0: sl@0: RFileWriteStream write_stream; sl@0: User::LeaveIfError( write_stream.Replace(fs, fileName, EFileWrite) ); sl@0: CleanupClosePushL(write_stream); // . . write_stream -> stack sl@0: sl@0: cert->ExternalizeL(write_stream); sl@0: CleanupStack::PopAndDestroy(); // . . write_stream <- pop sl@0: sl@0: // now internalize back from stream sl@0: RFileReadStream read_stream; sl@0: User::LeaveIfError( read_stream.Open(fs, fileName, EFileRead) ); sl@0: CleanupClosePushL(read_stream); // . . read_stream -> stack sl@0: sl@0: CCTCertInfo* cert_read = sl@0: CCTCertInfo::NewLC(read_stream, aToken); // does InternalizeL() sl@0: // . . . cert_read -> stack sl@0: // log certinfo params just internalized sl@0: iOut.writeString(_L("Cert attributes after internalize:")); sl@0: iOut.writeNewLine(); sl@0: WriteFormat( cert_read->CertificateFormat() ); sl@0: iOut.writeString(_L("\tiDeletable = ")); sl@0: iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: sl@0: // set result sl@0: iResultGood = ETrue; sl@0: // check deletable flag is persistent sl@0: if ( cert_read->IsDeletable() != iDeletable ) sl@0: { sl@0: iResultGood = EFalse; sl@0: } sl@0: // check format is persistent sl@0: if ( cert_read->CertificateFormat() != iCertificateFormat ) sl@0: { sl@0: iResultGood = EFalse; sl@0: } sl@0: sl@0: // cleanup sl@0: CleanupStack::PopAndDestroy(cert_read); // . . . cert_read <- pop sl@0: CleanupStack::PopAndDestroy(); // . . read_stream <- pop sl@0: CleanupStack::PopAndDestroy(&fs); // . fs <- pop sl@0: CleanupStack::PopAndDestroy(cert); // initial certinfo <- pop sl@0: } sl@0: sl@0: void CTestCertForDeletable::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: // COMMON PART sl@0: MCTWritableCertStore& ustore = sl@0: UnifiedCertStore().WritableCertStore(iStoreIndex); sl@0: MCTToken& token = ustore.Token(); sl@0: sl@0: TKeyIdentifier* issuerKeyId = NULL; sl@0: TKeyIdentifier* subjectKeyId = NULL; sl@0: sl@0: if (iOwnerType != ECACertificate) sl@0: { sl@0: issuerKeyId = & iIssuerKeyId; sl@0: subjectKeyId = & iSubjectKeyId; sl@0: } sl@0: sl@0: const TInt KCertificateId = 0x00000213; sl@0: _LIT8(KSomeHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d"); sl@0: sl@0: // create some certinfo object sl@0: ASSERT(iCertificateLabel); sl@0: sl@0: switch (iState) sl@0: { sl@0: case ETestSerialization: sl@0: { sl@0: iOut.writeString(_L("Checking serialization for a certificate...")); sl@0: iOut.writeNewLine(); sl@0: sl@0: TRAPD( err, TestSerializationL( token, issuerKeyId, subjectKeyId, sl@0: KCertificateId, &KSomeHash) ); sl@0: sl@0: TRequestStatus* status = &aStatus; sl@0: if (err != KErrNone ) sl@0: { sl@0: // TestSerializationL() did leave sl@0: iResult = EFalse; sl@0: iResultGood = EFalse; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: else sl@0: { sl@0: // TestSerializationL() passed ok sl@0: iResult = ETrue; sl@0: User::RequestComplete(status, aStatus.Int()); sl@0: } sl@0: // set next state sl@0: iState = EFinished; sl@0: } sl@0: break; sl@0: sl@0: sl@0: case ETestNewCSClasswDeletable: // check mctwritablecertstore sl@0: { sl@0: iOut.writeString(_L("Testing MCTWritableCertStore::Add() w deletable...")); sl@0: iOut.writeNewLine(); sl@0: sl@0: // create a writablestore supporting new Add() sl@0: TNewCSClasswDeletable storeNew; sl@0: sl@0: // call new Add() sl@0: storeNew.Add( *iCertificateLabel, sl@0: iCertificateFormat, sl@0: iOwnerType, sl@0: subjectKeyId, sl@0: issuerKeyId, sl@0: *iCertificateContent, // this is probably unset sl@0: iDeletable, sl@0: aStatus); sl@0: sl@0: iResultGood = ETrue; sl@0: iState = EFinished; sl@0: } sl@0: break; sl@0: sl@0: sl@0: case ETestOldCSClasswoDeletable: // check mctwritablecertstore sl@0: { sl@0: sl@0: iOut.writeString(_L("Testing new MCTWritableCertStore::Add() on old class...")); sl@0: iOut.writeNewLine(); sl@0: sl@0: // create a writablestore that does not have new Add() sl@0: TOldCSClasswoDeletable storeOld; sl@0: sl@0: // call new Add(), expect to get KErrNotSupported sl@0: storeOld.Add( *iCertificateLabel, sl@0: iCertificateFormat, sl@0: iOwnerType, sl@0: subjectKeyId, sl@0: issuerKeyId, sl@0: *iCertificateContent, // this is probably unset sl@0: iDeletable, sl@0: aStatus); sl@0: iResultGood = ETrue; sl@0: iState = EFinished; sl@0: sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, aStatus.Int()); sl@0: } sl@0: break; sl@0: sl@0: sl@0: case EFinished: sl@0: { sl@0: sl@0: if (aStatus == KErrNone) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: else sl@0: if (aStatus == iExpectedResult) sl@0: { sl@0: iResult = ETrue; sl@0: iResultGood = ETrue; sl@0: } sl@0: else sl@0: { sl@0: iResult = EFalse; sl@0: iResultGood = ETrue; sl@0: } sl@0: sl@0: if (aStatus != KErrNoMemory) sl@0: { sl@0: iFinished = ETrue; sl@0: } sl@0: sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, aStatus.Int()); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: break; sl@0: sl@0: } // switch () sl@0: sl@0: } // CTestCertForDeletable::PerformAction() sl@0: sl@0: void CTestCertForDeletable::PerformCancel() sl@0: { sl@0: iState = EFinished; sl@0: MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex); sl@0: store.CancelAdd(); sl@0: } sl@0: sl@0: void CTestCertForDeletable::AfterOOMFailure() sl@0: { sl@0: } sl@0: sl@0: void CTestCertForDeletable::Reset() sl@0: { sl@0: iState = EFinished; sl@0: } sl@0: sl@0: void CTestCertForDeletable::DoReportAction() sl@0: { sl@0: iOut.writeString(_L("\tLabel = ")); sl@0: iOut.writeString(*iCertificateLabel); sl@0: iOut.writeNewLine(); sl@0: iOut.writeString(_L("\tOwner type = ")); sl@0: WriteOwnerType(); sl@0: WriteFormat(iCertificateFormat); sl@0: sl@0: iOut.writeString(_L("\tSubjectKeyId: ")); sl@0: iOut.writeOctetString(iSubjectKeyId); sl@0: iOut.writeNewLine(); sl@0: sl@0: iOut.writeString(_L("\tiDeletable = ")); sl@0: iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: sl@0: void CTestCertForDeletable::WriteFormat(TCertificateFormat aFormat) sl@0: { sl@0: iOut.writeString(_L("\tFormat = ")); sl@0: switch (aFormat) sl@0: { sl@0: case EX509Certificate: sl@0: iOut.writeString(_L("X.509\n")); sl@0: break; sl@0: sl@0: case EWTLSCertificate: sl@0: iOut.writeString(_L("WTLS\n")); sl@0: break; sl@0: sl@0: case EX968Certificate: sl@0: iOut.writeString(_L("X968\n")); sl@0: break; sl@0: sl@0: case EX509CertificateUrl: sl@0: iOut.writeString(_L("X.509 URL\n")); sl@0: break; sl@0: sl@0: case EWTLSCertificateUrl: sl@0: iOut.writeString(_L("WTLS URL\n")); sl@0: break; sl@0: sl@0: case EX968CertificateUrl: sl@0: iOut.writeString(_L("X968 URL\n")); sl@0: break; sl@0: sl@0: default: sl@0: iOut.writeString(_L("Unknown format\n")); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CTestCertForDeletable::WriteOwnerType() sl@0: { sl@0: switch (iOwnerType) sl@0: { sl@0: case ECACertificate: sl@0: iOut.writeString(_L("CA\n")); sl@0: break; sl@0: sl@0: case EUserCertificate: sl@0: iOut.writeString(_L("User")); sl@0: break; sl@0: sl@0: case EPeerCertificate: sl@0: iOut.writeString(_L("Peer")); sl@0: break; sl@0: sl@0: default: sl@0: iOut.writeString(_L("Unknown")); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: CTestCertForDeletable::CTestCertForDeletable(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CCertStoreTestAction(aFs, aConsole, aOut), iState(EFinished) sl@0: { sl@0: } sl@0: sl@0: void CTestCertForDeletable::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CCertStoreTestAction::ConstructL(aTestActionSpec); sl@0: sl@0: SetCertFormatL(Input::ParseElement(aTestActionSpec.iActionBody, KCertFormatStart)); sl@0: SetCertOwnerTypeL(Input::ParseElement(aTestActionSpec.iActionBody, KCertOwnerTypeStart)); sl@0: SetCertLabelL(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart)); sl@0: SetKeyId(iIssuerKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KIssuerKeyStart)); sl@0: SetKeyId(iSubjectKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KSubjectKeyStart)); sl@0: SetStoreToUse(Input::ParseElement(aTestActionSpec.iActionBody, KStoreToUseStart)); sl@0: SetDeletable(Input::ParseElement(aTestActionSpec.iActionBody, KDeletableStart)); sl@0: SetSubActionL(Input::ParseElement(aTestActionSpec.iActionBody, KSubActionStart)); sl@0: sl@0: // Setting the expected result sl@0: HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length()); sl@0: TPtr(result->Des()).Copy(aTestActionSpec.iActionResult); sl@0: Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult); sl@0: CleanupStack::PopAndDestroy(result); sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetKeyId(TKeyIdentifier& aKeyIdentifier, const TDesC8& aKeyInfo) sl@0: { sl@0: TInt size = aKeyInfo.Length(); sl@0: for (TInt i = 0; i < size; i += 2) sl@0: { sl@0: TInt a = (aKeyInfo[i+1] >= 'a') ? (aKeyInfo[i+1] - 'a' + 10) : (aKeyInfo[i+1] - '0'); sl@0: TInt b = (aKeyInfo[i] >= 'a') ? (aKeyInfo[i] - 'a' + 10) : (aKeyInfo[i] - '0'); sl@0: aKeyIdentifier.Append(a + b * 16); sl@0: } sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetCertFormatL(const TDesC8& aFormat) sl@0: { sl@0: if (aFormat == KX509) sl@0: { sl@0: iCertificateFormat = EX509Certificate; sl@0: } sl@0: else if (aFormat == KWTLS) sl@0: { sl@0: iCertificateFormat = EWTLSCertificate; sl@0: } sl@0: else if (aFormat == KX968) sl@0: { sl@0: iCertificateFormat = EX968Certificate; sl@0: } sl@0: else if (aFormat == KX509URL) sl@0: { sl@0: iCertificateFormat = EX509CertificateUrl; sl@0: } sl@0: else if (aFormat == KWTLSURL) sl@0: { sl@0: iCertificateFormat = EWTLSCertificateUrl; sl@0: } sl@0: else if (aFormat == KX968URL) sl@0: { sl@0: iCertificateFormat = EX968CertificateUrl; sl@0: } sl@0: else if (aFormat == KUnknown) sl@0: { sl@0: iCertificateFormat = EUnknownCertificate; sl@0: } sl@0: else sl@0: { sl@0: iOut.write(_L("Unrecognized cert format: ")); sl@0: iOut.writeString(aFormat); sl@0: iOut.writeNewLine(); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetCertOwnerTypeL(const TDesC8& aOwnerType) sl@0: { sl@0: if (aOwnerType == KCACert) sl@0: { sl@0: iOwnerType = ECACertificate; sl@0: } sl@0: else if (aOwnerType == KUserCert) sl@0: { sl@0: iOwnerType = EUserCertificate; sl@0: } sl@0: else if (aOwnerType == KPeerCert) sl@0: { sl@0: iOwnerType = EPeerCertificate; sl@0: } sl@0: else if (aOwnerType == KUnknown) sl@0: { sl@0: // set dummy bogus owner type sl@0: iOwnerType = static_cast(EPeerCertificate + 1); sl@0: } sl@0: else sl@0: { sl@0: iOut.write(_L("Unknown cert owner type: ")); sl@0: iOut.writeString(aOwnerType); sl@0: iOut.writeNewLine(); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetSubActionL(const TDesC8& aStringVal) sl@0: { sl@0: if (aStringVal == KTestSerialization) sl@0: { sl@0: iState = ETestSerialization; sl@0: } sl@0: else if (aStringVal == KTestNewCSClasswDeletable) sl@0: { sl@0: iState = ETestNewCSClasswDeletable; sl@0: } sl@0: else if (aStringVal == KTestOldCSClasswoDeletable) sl@0: { sl@0: iState = ETestOldCSClasswoDeletable; sl@0: } sl@0: sl@0: else sl@0: { sl@0: iOut.write(_L("Unknown subaction type: ")); sl@0: iOut.writeString(aStringVal); sl@0: iOut.writeNewLine(); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CTestCertForDeletable::SetCertLabelL(const TDesC8& aLabel) sl@0: { sl@0: delete iCertificateLabel; sl@0: iCertificateLabel = NULL; sl@0: iCertificateLabel = HBufC::NewL(aLabel.Length()); sl@0: TPtr ptr = iCertificateLabel->Des(); sl@0: ptr.Copy(aLabel); sl@0: } sl@0: sl@0: sl@0: sl@0: void CTestCertForDeletable::SetStoreToUse(const TDesC8& aStoreToUse) sl@0: { sl@0: TLex8 lex(aStoreToUse); sl@0: lex.Val(iStoreIndex); sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetDeletable(const TDesC8& aStringVal) sl@0: { sl@0: TLex8 lex(aStringVal); sl@0: lex.Val(iDeletable); sl@0: } sl@0: sl@0: void CTestCertForDeletable::SetCertificateContentL(const TDesC8& aFileName) sl@0: { sl@0: TFileName fileName; sl@0: fileName.Copy(aFileName); sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: __ASSERT_DEBUG(!iCertificateContent, User::Panic(_L("CTestCertForDeletable"), 1)); sl@0: TRAPD(err, iCertificateContent = Input::ReadFileL(fileName, fs)); sl@0: if (err != KErrNone) sl@0: { sl@0: iConsole.Printf(_L("Error reading file : ")); sl@0: iConsole.Printf(fileName); sl@0: iConsole.Printf(_L("\n")); sl@0: User::Leave(err); sl@0: } sl@0: CleanupStack::PopAndDestroy(); // fs sl@0: } sl@0: sl@0: void CTestCertForDeletable::ConstructCertL(const TDesC8& aCert) sl@0: { sl@0: TFileName filename; sl@0: filename.Copy(aCert); sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: HBufC8* certBuf = 0; sl@0: TRAPD(err, certBuf = Input::ReadFileL(filename, fs)); sl@0: if (err != KErrNone) sl@0: { sl@0: iConsole.Printf(_L("Error reading file : ")); sl@0: iConsole.Printf(filename); sl@0: iConsole.Printf(_L("\n")); sl@0: User::Leave(err); sl@0: } sl@0: CleanupStack::PushL(certBuf); sl@0: switch (iCertificateFormat) sl@0: { sl@0: case EX509Certificate: sl@0: iCertificate = CX509Certificate::NewL(*certBuf); sl@0: break; sl@0: sl@0: case EWTLSCertificate: sl@0: iCertificate = CWTLSCertificate::NewL(*certBuf); sl@0: break; sl@0: sl@0: default: sl@0: // Unknown format - do nothing sl@0: break; sl@0: } sl@0: CleanupStack::PopAndDestroy(2); // fs and certBuf sl@0: } sl@0: sl@0: void CTestCertForDeletable::DoCheckResult(TInt aError) sl@0: { sl@0: if (iFinished) sl@0: { sl@0: if (iResult && !iResultGood) sl@0: { sl@0: iConsole.Write(_L("\ttestcertfordeletable failed\n")); sl@0: iOut.writeString(_L("\ttestcertfordeletable failed")); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: else if (iResult) sl@0: { sl@0: iConsole.Write(_L("\ttestcertfordeletable passed ok\n")); sl@0: iOut.writeString(_L("\ttestcertfordeletable passed ok")); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: else sl@0: { sl@0: iConsole.Write(_L("\tcertinfo couldn't be tested\n")); sl@0: iOut.writeString(_L("\tcertinfo couldn't be tested")); sl@0: iOut.writeNewLine(); sl@0: iOut.writeString(_L("\t")); sl@0: iOut.writeError(aError); sl@0: if (aError == KErrBadName) sl@0: { sl@0: iOut.writeString(_L(" - Check that the label is unique")); sl@0: } sl@0: if (aError == KErrAccessDenied) sl@0: { sl@0: iOut.writeString(_L(" - Can't open filestream. Access denied.")); sl@0: } sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: } sl@0: }