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_unifiedcertstoreadd.h" sl@0: #include "t_certstoredefs.h" sl@0: #include "t_input.h" sl@0: #include "t_certstoreout.h" sl@0: #include sl@0: sl@0: CTestAction* CAddCertificate::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CAddCertificate* self = new(ELeave) CAddCertificate(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: CAddCertificate::~CAddCertificate() 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 CAddCertificate::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: switch (iState) sl@0: { sl@0: case EAdding: sl@0: { sl@0: if (iNotificationSubscribed) sl@0: { sl@0: if (!iNotifier) sl@0: { sl@0: iNotifier = CCertStoreChangeNotifier::NewL(iNotifierFlag); sl@0: iNotifier->StartNotification(); sl@0: } sl@0: iState = ECheckNotification; sl@0: } sl@0: else sl@0: { sl@0: iState = EFinished; sl@0: } sl@0: MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex); sl@0: sl@0: TKeyIdentifier* issuerKeyId = &iIssuerKeyId; sl@0: TKeyIdentifier* subjectKeyId = &iSubjectKeyId; sl@0: sl@0: ASSERT(iCertificateLabel); sl@0: sl@0: // Use the Add() with Deletable param if Deletable flag present in test data sl@0: if (iDeletableFlagPresent) sl@0: { sl@0: store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType, sl@0: subjectKeyId, issuerKeyId, *iCertificateContent, sl@0: iDeletable, aStatus); sl@0: } sl@0: // otherwise, use the original Add() sl@0: else sl@0: { sl@0: store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType, sl@0: subjectKeyId, issuerKeyId, *iCertificateContent, aStatus); sl@0: } sl@0: } sl@0: break; sl@0: case ECheckNotification: sl@0: { sl@0: iState = EFinished; sl@0: if (iNotifierFlag) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: else sl@0: { sl@0: iNotifier->SetCompleteStatus(&aStatus); sl@0: } sl@0: break; sl@0: } sl@0: case EFinished: sl@0: { sl@0: if (aStatus == iExpectedResult) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: else sl@0: { sl@0: iResult = EFalse; 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: User::Invariant(); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CAddCertificate::PerformCancel() sl@0: { sl@0: switch (iState) sl@0: { sl@0: case ECheckNotification: sl@0: case EFinished: sl@0: { sl@0: MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex); sl@0: store.CancelAdd(); sl@0: break; sl@0: } sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CAddCertificate::AfterOOMFailure() sl@0: { sl@0: } sl@0: sl@0: void CAddCertificate::Reset() sl@0: { sl@0: iState = EAdding; sl@0: } sl@0: sl@0: void CAddCertificate::DoReportAction() sl@0: { sl@0: iOut.writeString(_L("Adding certificate...")); sl@0: iOut.writeNewLine(); 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(); sl@0: iOut.writeString(_L("\tSubjectKeyId: ")); sl@0: iOut.writeOctetString(iSubjectKeyId); sl@0: iOut.writeNewLine(); sl@0: iOut.writeString(_L("\tDeletable = ")); sl@0: iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: sl@0: void CAddCertificate::WriteFormat() sl@0: { sl@0: iOut.writeString(_L("\tFormat = ")); sl@0: switch (iCertificateFormat) 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 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: default: sl@0: iOut.writeString(_L("Unknown format\n")); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CAddCertificate::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: CAddCertificate::CAddCertificate(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CSubscriberAction(aFs, aConsole, aOut), iState(EAdding), sl@0: iDeletable(ETrue), iDeletableFlagPresent(EFalse) sl@0: { sl@0: } sl@0: sl@0: void CAddCertificate::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CSubscriberAction::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: sl@0: TPtrC8 certFileOrURL = Input::ParseElement(aTestActionSpec.iActionBody, KCertFileStart); sl@0: sl@0: SetCertificateContentL(certFileOrURL); sl@0: sl@0: if (iCertificateFormat == EX509CertificateUrl || sl@0: iCertificateFormat == EWTLSCertificateUrl) sl@0: { sl@0: iCertificateURL = certFileOrURL.AllocL(); sl@0: } sl@0: else sl@0: { sl@0: ConstructCertL(certFileOrURL); sl@0: } sl@0: sl@0: // check for a possible deletable flag value for the certificate sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: const TDesC8& deletableStr = Input::ParseElement(aTestActionSpec.iActionBody, sl@0: KDeletableStart, sl@0: KDeletableEnd, sl@0: pos, sl@0: err); sl@0: sl@0: // set the deletable attribute if a value was found for the certificate sl@0: if (err == KErrNone) sl@0: { sl@0: SetDeletable(deletableStr); sl@0: } 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 CAddCertificate::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 CAddCertificate::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 == 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 == KUnknown) sl@0: { sl@0: iCertificateFormat = EUnknownCertificate; sl@0: } sl@0: else sl@0: { sl@0: iOut.write(_L("Unknown 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 CAddCertificate::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 CAddCertificate::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: void CAddCertificate::SetStoreToUse(const TDesC8& aStoreToUse) sl@0: { sl@0: TLex8 lex(aStoreToUse); sl@0: lex.Val(iStoreIndex); sl@0: } sl@0: sl@0: void CAddCertificate::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("CAddCertificate"), 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 CAddCertificate::SetDeletable(const TDesC8& aDeletable) sl@0: { sl@0: iDeletableFlagPresent = ETrue; sl@0: if (aDeletable.Compare(KTrue)==0) sl@0: { sl@0: iDeletable = ETrue; sl@0: } sl@0: else sl@0: { sl@0: iDeletable = EFalse; sl@0: } sl@0: } sl@0: sl@0: void CAddCertificate::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); sl@0: } sl@0: sl@0: void CAddCertificate::DoCheckResult(TInt aError) sl@0: { sl@0: sl@0: if (iFinished) sl@0: { sl@0: if (iResult ) sl@0: { sl@0: if (iExpectedResult == KErrNone ) sl@0: { sl@0: iConsole.Write(_L("\tcertificate added successfully\n")); sl@0: iOut.writeString(_L("\tcertificate added successfully")); sl@0: } sl@0: else sl@0: { sl@0: iConsole.Write(_L("\tcertificate not added.\n")); sl@0: iOut.writeString(_L("\tcertificate not added.")); sl@0: } sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: else if( !iResult) sl@0: { sl@0: if(iExpectedResult == KErrNone ) sl@0: { sl@0: iConsole.Write(_L("\tcertificate not added\n")); sl@0: iOut.writeString(_L("\tcertificate not added")); sl@0: } sl@0: else sl@0: { sl@0: iConsole.Write(_L("\tcertificate should not be added\n")); sl@0: iOut.writeString(_L("\tcertificate should not be added")); sl@0: } 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: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: } sl@0: } sl@0: ////////////////////////////////////////////////////////// sl@0: // Key import, from keystore for adding user certificates sl@0: ////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CImportKey::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CImportKey::NewLC(aFs, aConsole, aOut, aTestActionSpec); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CImportKey::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CImportKey* self = new (ELeave) CImportKey(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CImportKey::~CImportKey() sl@0: { sl@0: delete iLabel; sl@0: delete iKeyData; sl@0: if (iKeyInfo) sl@0: { sl@0: iKeyInfo->Release(); sl@0: } sl@0: delete iUnifiedKeyStore; sl@0: iFs.Close(); sl@0: } sl@0: sl@0: CImportKey::CImportKey(RFs& aFs, CConsoleBase& aConsole, Output& aOut) sl@0: : CCertStoreTestAction(aFs, aConsole, aOut), sl@0: iState(EInitialise) sl@0: {} sl@0: sl@0: sl@0: void CImportKey::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: User::LeaveIfError(iFs.Connect()); sl@0: sl@0: CCertStoreTestAction::ConstructL(aTestActionSpec); sl@0: sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: SetKeyDataFileL(Input::ParseElement(aTestActionSpec.iActionBody, KImportDataFile, KImportDataFileEnd, pos, err)); sl@0: for (;SetKeyUsage(Input::ParseElement(aTestActionSpec.iActionBody, KKeyUsageStart, KKeyUsageEnd, pos, err));) sl@0: ; sl@0: sl@0: SetKeyLabel(Input::ParseElement(aTestActionSpec.iActionBody, KKeyLabelStart, KKeyLabelEnd, pos, err)); sl@0: for (;SetKeyAccessType(Input::ParseElement(aTestActionSpec.iActionBody, KKeyAccessTypeStart, KKeyAccessTypeEnd, pos, err));) sl@0: ; sl@0: sl@0: SetKeyPassphrase(Input::ParseElement(aTestActionSpec.iActionBody, KKeyPassphraseStart, KKeyPassphraseEnd, pos, err)); sl@0: sl@0: pos = 0; sl@0: 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: TBool CImportKey::SetKeyUsage(const TDesC8& aKeyUsage) sl@0: { sl@0: TBool ret = ETrue; sl@0: if (aKeyUsage.Compare(KAllKeyUsages)==0) sl@0: iUsage = EPKCS15UsageAll; sl@0: else if (aKeyUsage.Compare(KAllKeyUsagesButNR)==0) sl@0: iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) | sl@0: (TKeyUsagePKCS15)(EPKCS15UsageSignRecover) | sl@0: (TKeyUsagePKCS15)(EPKCS15UsageDecrypt); sl@0: else if (aKeyUsage.Compare(KDSAUsage)==0) sl@0: iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) | sl@0: (TKeyUsagePKCS15)(EPKCS15UsageSignRecover); sl@0: else if (aKeyUsage.Compare(KDerive)==0) sl@0: iUsage |= EPKCS15UsageDerive; sl@0: else if (aKeyUsage.Compare(KSign)==0) sl@0: iUsage |= EPKCS15UsageSign; sl@0: else if (aKeyUsage.Compare(KSignRecover)==0) sl@0: iUsage |= EPKCS15UsageSignRecover; sl@0: else if (aKeyUsage.Compare(KDecrypt)==0) sl@0: iUsage |= EPKCS15UsageDecrypt; sl@0: else if (aKeyUsage.Compare(KNR)==0) sl@0: iUsage |= EPKCS15UsageNonRepudiation; sl@0: else if (aKeyUsage.Compare(KEncipherAndSign)==0) sl@0: iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) | sl@0: (TKeyUsagePKCS15)(EPKCS15UsageSignRecover) | sl@0: (TKeyUsagePKCS15)(EPKCS15UsageUnwrap); sl@0: else sl@0: ret = EFalse; sl@0: return ret; sl@0: } sl@0: sl@0: void CImportKey::SetKeyLabel(const TDesC8& aKeyLabel) sl@0: { sl@0: iLabel = HBufC::NewMax(aKeyLabel.Size()); sl@0: if (iLabel) sl@0: { sl@0: TPtr theLabel(iLabel->Des()); sl@0: theLabel.FillZ(); sl@0: theLabel.Copy(aKeyLabel); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CImportKey::SetKeyPassphrase(const TDesC8& aPassphrase) sl@0: { sl@0: // If the passphrase is empty, then use "clanger" by default. sl@0: _LIT8(KDefaultPassphrase, "clanger"); sl@0: TPtrC8 phrase(KDefaultPassphrase()); sl@0: if (aPassphrase.Length()) sl@0: { sl@0: phrase.Set(aPassphrase); sl@0: } sl@0: sl@0: sl@0: RFs fs; sl@0: RFile file; sl@0: fs.Connect(); sl@0: sl@0: // Write the passphrase straight to the file. sl@0: TDriveUnit sysDrive (fs.GetSystemDrive()); sl@0: TBuf<24> fileName (sysDrive.Name()); sl@0: fileName.Append(_L("\\password.txt")); sl@0: sl@0: file.Replace(fs, fileName, EFileWrite); sl@0: file.Write(phrase); sl@0: file.Close(); sl@0: fs.Close(); sl@0: } sl@0: sl@0: void CImportKey::SetKeyDataFileL(const TDesC8& aDes) sl@0: { sl@0: // Now the filename itself sl@0: TFileName fileName; sl@0: fileName.FillZ(); sl@0: fileName.Copy(aDes); sl@0: sl@0: TDriveUnit sysDrive (RFs::GetSystemDrive()); sl@0: TBuf<64> buf(sysDrive.Name()); sl@0: buf.Append(_L("\\tcertstore\\data\\")); sl@0: buf.Append(fileName); sl@0: sl@0: RFile file; sl@0: TInt r = file.Open(iFs, buf, EFileRead); sl@0: if ( (r==KErrNotFound) || (r==KErrPathNotFound) ) sl@0: {// Not on c:, try z: sl@0: buf[0] = 'z'; sl@0: r = file.Open(iFs, buf, EFileRead); sl@0: } sl@0: sl@0: User::LeaveIfError(r); sl@0: sl@0: CleanupClosePushL(file); sl@0: sl@0: TInt fileSize = 0; sl@0: User::LeaveIfError(file.Size(fileSize)); sl@0: sl@0: if (fileSize > 0) sl@0: { sl@0: iKeyData = HBufC8::NewMaxL(fileSize); sl@0: TPtr8 data(iKeyData->Des()); sl@0: data.FillZ(); sl@0: User::LeaveIfError(file.Read(data, fileSize)); sl@0: CleanupStack::Pop(1); sl@0: } sl@0: sl@0: file.Close(); sl@0: } sl@0: sl@0: TBool CImportKey::SetKeyAccessType(const TDesC8& aKeyAccessType) sl@0: { sl@0: TBool ret = ETrue; sl@0: if (aKeyAccessType.Compare(KExtractable)==0) sl@0: { sl@0: iAccessType |= CCTKeyInfo::EExtractable; sl@0: } sl@0: else if (aKeyAccessType.Compare(KSensitive)==0) sl@0: { sl@0: iAccessType |= CCTKeyInfo::ESensitive; sl@0: } sl@0: else if (aKeyAccessType.Compare(KAlwaysSensitive)==0) sl@0: { sl@0: iAccessType |= CCTKeyInfo::EAlwaysSensitive; sl@0: } sl@0: else if (aKeyAccessType.Compare(KNeverExtractable)==0) sl@0: { sl@0: iAccessType |= CCTKeyInfo::ENeverExtractable; sl@0: } sl@0: else if (aKeyAccessType.Compare(KLocal)==0) sl@0: { sl@0: iAccessType |= CCTKeyInfo::ELocal; sl@0: } sl@0: else sl@0: ret = EFalse; sl@0: return ret; sl@0: } sl@0: sl@0: void CImportKey::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: TDriveUnit sysDrive (RFs::GetSystemDrive()); sl@0: switch (iState) sl@0: { sl@0: case EInitialise: sl@0: { sl@0: if (iKeyInfo != NULL) sl@0: { sl@0: iKeyInfo->Release(); sl@0: iKeyInfo = NULL; sl@0: } sl@0: sl@0: // Delete t_secdlg files - this will then always answer "clanger" for the passphrase sl@0: sl@0: TInt result; sl@0: TBuf<24> datFile(sysDrive.Name()); sl@0: datFile.Append(_L("\\t_secdlg_in.dat")); sl@0: result = iFs.Delete(datFile); sl@0: sl@0: if (result != KErrNone && result != KErrNotFound) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, result); sl@0: return; sl@0: } sl@0: sl@0: datFile.Copy(sysDrive.Name()); sl@0: datFile.Append(_L("\\t_secdlg_out.dat")); sl@0: result = iFs.Delete(datFile); sl@0: sl@0: if (result != KErrNone && result != KErrNotFound) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, result); sl@0: return; sl@0: } sl@0: sl@0: TRAP(result, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs)); sl@0: if ( (result==KErrNone) && (iUnifiedKeyStore) ) sl@0: { sl@0: iUnifiedKeyStore->Initialize(aStatus); sl@0: iState = EImportKey; sl@0: } sl@0: else sl@0: { sl@0: aStatus = result; sl@0: iState = EFinished; sl@0: } sl@0: } sl@0: break; sl@0: case EImportKey: sl@0: { sl@0: if (KErrNone==aStatus.Int()) sl@0: { sl@0: // Currently uses the first store, change to check the script for a specific store sl@0: iUnifiedKeyStore->ImportKey(0, iKeyData->Des(), iUsage, *iLabel, iAccessType, sl@0: TTime(0), TTime(0), iKeyInfo, aStatus); sl@0: } sl@0: else sl@0: { sl@0: // Errors get passed to next state sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, aStatus.Int()); sl@0: } sl@0: sl@0: iState = EFinished; sl@0: } sl@0: sl@0: break; sl@0: sl@0: case EFinished: sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, aStatus.Int()); sl@0: if ( (aStatus == iExpectedResult) || (aStatus==KErrAlreadyExists) ) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: else sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: iFinished = ETrue; sl@0: } sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CImportKey::PerformCancel() sl@0: {// To do when test harness cancel comes back. Currently cancel testing sl@0: // is performed in RunL with a set of flags and a separate active object sl@0: } sl@0: sl@0: void CImportKey::Reset() sl@0: {} sl@0: sl@0: void CImportKey::DoReportAction() sl@0: { sl@0: _LIT(KImporting, "Importing key from keystore..."); sl@0: iOut.writeString(KImporting); sl@0: TPtr theLabel(iLabel->Des()); sl@0: iOut.writeString(theLabel); sl@0: iOut.writeNewLine(); sl@0: } sl@0: sl@0: sl@0: void CImportKey::DoCheckResult(TInt aError) sl@0: { sl@0: sl@0: if (iFinished) sl@0: { sl@0: if (aError == KErrNone) sl@0: { sl@0: _LIT(KSuccessful, "Key imported successfully\n"); sl@0: iConsole.Write(KSuccessful); sl@0: iOut.writeString(KSuccessful); sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: else sl@0: { sl@0: if ( (aError!=iExpectedResult) && (aError!=KErrAlreadyExists) ) sl@0: { sl@0: _LIT(KFailed, "!!!Key import failure!!!\n"); sl@0: iConsole.Write(KFailed); sl@0: iOut.writeString(KFailed); sl@0: } sl@0: sl@0: iOut.writeNewLine(); sl@0: iOut.writeNewLine(); sl@0: } sl@0: } sl@0: } sl@0: