sl@0: /* sl@0: * Copyright (c) 1998-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 "test1certstore.h" sl@0: #include "tadditionalstoremappings.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: _LIT(KName1, "Test store 1"); sl@0: _LIT(KName2, "Test store 2"); sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////////////////// sl@0: //CFileCertStore sl@0: ///////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTest1CertStore* CTest1CertStore::NewL(RFs& aFs, sl@0: CTest1CertStoreToken& aToken, sl@0: const TDesC& aFileName, sl@0: TFileMode aMode) sl@0: { sl@0: CTest1CertStore* self = new(ELeave) CTest1CertStore(aToken, aFs); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aFileName, aMode); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: MCTToken& CTest1CertStore::Token() sl@0: { sl@0: return iToken; sl@0: } sl@0: sl@0: void CTest1CertStore::DoRelease() sl@0: { sl@0: if (iToken.Label() == KName1) sl@0: { sl@0: iToken.iRefCountInterface--; sl@0: if (!iToken.iRefCountInterface) sl@0: { sl@0: delete this; sl@0: } sl@0: } sl@0: else if (iToken.Label() == KName2) sl@0: { sl@0: iToken.iRefCountInterface2--; sl@0: if (!iToken.iRefCountInterface2) sl@0: { sl@0: delete this; sl@0: } sl@0: } sl@0: } sl@0: sl@0: void CTest1CertStore::List(RMPointerArray& aCertInfos, sl@0: const CCertAttributeFilter& aFilter, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: iOriginalRequestStatus = &aStatus; sl@0: aStatus = KRequestPending; sl@0: sl@0: iCertInfos = &aCertInfos; sl@0: iFilter = &aFilter; sl@0: sl@0: //Iinitialize the ketstore and then list the key info sl@0: if (iFilter->iKeyUsage != EX509UsageAll) sl@0: { sl@0: // We have to initialize the unified key store sl@0: TRAPD(err, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs)); sl@0: if (err != KErrNone) sl@0: { sl@0: User::RequestComplete(iOriginalRequestStatus, err); sl@0: iOriginalRequestStatus = 0; sl@0: } sl@0: else sl@0: { sl@0: iUnifiedKeyStore->Initialize(iStatus); sl@0: iState = EGetKeyInfos; sl@0: SetActive(); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: iState = EList; sl@0: TRequestStatus* status = &iStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: SetActive(); sl@0: } sl@0: } sl@0: sl@0: void CTest1CertStore::CancelList() sl@0: { sl@0: Cancel(); sl@0: } sl@0: sl@0: void CTest1CertStore::GetCert(CCTCertInfo*& aCertInfo, sl@0: const TCTTokenObjectHandle& aHandle, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TInt err = KErrNotFound; sl@0: if ((aHandle.iTokenHandle == iToken.Handle()) && sl@0: (aHandle.iObjectId < iCerts->Count())) sl@0: { sl@0: TRAP(err, aCertInfo = CCTCertInfo::NewL(iCerts->Entry(aHandle.iObjectId))); sl@0: } sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelGetCert() sl@0: { sl@0: // Nothing to do because GetCert is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::Applications(const CCTCertInfo& aCertInfo, sl@0: RArray& aApplications, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TInt err = KErrNone; sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index != KErrNotFound) sl@0: { sl@0: const RArray& apps = iCerts->Mapping(index)->CertificateApps(); sl@0: TInt end = apps.Count(); sl@0: for (TInt i = 0; (i < end) && (err == KErrNone); i++) sl@0: { sl@0: err = aApplications.Append(apps[i]); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: err = index; sl@0: } sl@0: if (err != KErrNone) sl@0: { sl@0: aApplications.Reset(); sl@0: } sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelApplications() sl@0: { sl@0: } sl@0: sl@0: void CTest1CertStore::IsApplicable(const CCTCertInfo& aCertInfo, sl@0: TUid aApplication, sl@0: TBool& aIsApplicable, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index != KErrNotFound) sl@0: { sl@0: const RArray& apps = iCerts->Mapping(index)->CertificateApps(); sl@0: TInt end = apps.Count(); sl@0: TInt i; sl@0: for (i = 0; i < end; i++) sl@0: { sl@0: if (apps[i] == aApplication) sl@0: { sl@0: break; sl@0: } sl@0: } sl@0: if (i == end) sl@0: { sl@0: aIsApplicable = EFalse; sl@0: } sl@0: else sl@0: { sl@0: aIsApplicable = ETrue; sl@0: } sl@0: index = KErrNone; sl@0: } sl@0: sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, index); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelIsApplicable() sl@0: { sl@0: // Nothing to do because IsApplicable is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::Trusted(const CCTCertInfo& aCertInfo, sl@0: TBool& aTrusted, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index != KErrNotFound) sl@0: { sl@0: aTrusted = iCerts->Mapping(index)->Trusted(); sl@0: index = KErrNone; sl@0: } sl@0: sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, index); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelTrusted() sl@0: { sl@0: // Nothing to do because Trusted is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::Retrieve(const CCTCertInfo& aCertInfo, sl@0: TDes8& aCertificate, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: #ifdef CERTSTORE_SOFTWARE_ASYNCH sl@0: // perform an asynchronous retrieval of the certificate sl@0: iOriginalRequestStatus = &aStatus; sl@0: aStatus = KRequestPending; sl@0: sl@0: iAsynchCertInfo = &aCertInfo; sl@0: iAsynchCertificate = &aCertificate; sl@0: sl@0: iState = ERetrieve; sl@0: TRequestStatus* status = &iStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: SetActive(); sl@0: #else sl@0: RetrieveNow(aCertInfo,aCertificate,aStatus); sl@0: #endif sl@0: } sl@0: sl@0: void CTest1CertStore::RetrieveNow(const CCTCertInfo& aCertInfo, sl@0: TDes8& aCertificate, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: TInt err; sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index == KErrNotFound) sl@0: { sl@0: err = KErrNotFound; sl@0: } sl@0: else sl@0: { sl@0: CFileCertStoreMapping* mapping = NULL; sl@0: mapping = iCerts->Mapping(index); sl@0: err = index; sl@0: if (mapping) sl@0: { sl@0: TRAP(err, DoLoadL(aCertificate, *mapping)); sl@0: } sl@0: } sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelRetrieve() sl@0: { sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::Capabilities(const CCTCertInfo& /*aCertInfo*/, TCapabilitySet& /*aCapbilitiesOut*/, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: // currently not supported sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNotSupported); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelCapabilities() sl@0: { sl@0: // Nothing to do because Capabilities is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::IsMandatory(const CCTCertInfo& /*aCertInfo*/, TBool& /*aMandatoryOut*/, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: // currently not supported sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNotSupported); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelIsMandatory() sl@0: { sl@0: // Nothing to do because IsMandatory is not asynchronous. sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::Remove(const CCTCertInfo& aCertInfo, TRequestStatus& aStatus) sl@0: { sl@0: // This removes the certificate from the file store. sl@0: TRAPD(err, DoRemoveL(aCertInfo)); sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelRemove() sl@0: { sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::SetApplicability(const CCTCertInfo& aCertInfo, sl@0: const RArray& aTrusters, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TRAPD(err, DoSetApplicabilityL(aCertInfo, aTrusters)); sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::CancelSetApplicability() sl@0: { sl@0: } sl@0: sl@0: void CTest1CertStore::SetTrust(const CCTCertInfo& aCertInfo, sl@0: TBool aTrusted, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TRAPD(err, DoSetTrustL(aCertInfo, aTrusted)); sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelSetTrust() sl@0: { sl@0: // Nothing to do because SetTrust is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::SetCapabilities(const CCTCertInfo& /*aCertInfo*/, const TCapabilitySet& /*aCapabilities*/, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: // currently not supported sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNotSupported); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelSetCapabilities() sl@0: { sl@0: // Nothing to do because SetCapabilities is not asynchronous. sl@0: } sl@0: sl@0: void CTest1CertStore::SetMandatory(const CCTCertInfo& /*aCertInfo*/, TBool /*aMandatory*/, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: // currently not supported sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNotSupported); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelSetMandatory() sl@0: { sl@0: // Nothing to do because SetMandatory is not asynchronous. sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::RevertStore(TAny* aStore) sl@0: { sl@0: //this is a CleanupItem sl@0: CPermanentFileStore* store = REINTERPRET_CAST(CPermanentFileStore*, aStore); sl@0: store->Revert(); sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::DeleteFile(TAny* aThis) sl@0: { sl@0: CTest1CertStore* self = REINTERPRET_CAST(CTest1CertStore*, aThis); sl@0: TDriveUnit sysDrive (RFs::GetSystemDrive()); sl@0: TDriveName driveName(sysDrive.Name()); sl@0: TBuf<128> certStoreDefaultFullPath (driveName); sl@0: certStoreDefaultFullPath.Append(_L("\\system\\data\\cacerts.dat")); sl@0: self->iFile.Close(); sl@0: self->iFs.Delete(certStoreDefaultFullPath); sl@0: } sl@0: sl@0: sl@0: sl@0: CTest1CertStore::~CTest1CertStore() sl@0: { sl@0: Cancel(); sl@0: sl@0: if (iStore != NULL) sl@0: { sl@0: #ifdef _DEBUG sl@0: #else sl@0: TInt err; sl@0: TRAP(err, iStore->ReclaimL()); sl@0: TRAP(err, iStore->CompactL()); sl@0: if (err == KErrNone) sl@0: { sl@0: TRAP(err, iStore->CommitL()); sl@0: } sl@0: #endif sl@0: delete iStore; sl@0: } sl@0: sl@0: iFile.Close(); sl@0: delete iCerts; sl@0: sl@0: iKeyInfos.Close(); sl@0: sl@0: delete iUnifiedKeyStore; sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::DoSetApplicabilityL(const CCTCertInfo& aCertInfo, sl@0: const RArray& aTrusters) sl@0: { sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index == KErrNotFound) sl@0: { sl@0: User::Leave(index); sl@0: } sl@0: sl@0: CFileCertStoreMapping* mapping = iCerts->Mapping(index); sl@0: const RArray& trusters = mapping->CertificateApps(); //oldEntry->Trusters(); sl@0: RArray* oldTrusters = new(ELeave) RArray(); sl@0: CleanupStack::PushL(oldTrusters); sl@0: CleanupClosePushL(*oldTrusters); sl@0: TInt iEnd = trusters.Count(); sl@0: TInt i; sl@0: for (i = 0; i < iEnd; i++) sl@0: { sl@0: User::LeaveIfError(oldTrusters->Append(trusters[i])); sl@0: } sl@0: sl@0: RArray* newTrusters = new (ELeave) RArray; sl@0: CleanupStack::PushL(newTrusters); sl@0: CleanupClosePushL(*newTrusters); sl@0: for (i = 0 ; i < aTrusters.Count() ; ++i) sl@0: { sl@0: User::LeaveIfError(newTrusters->Append(aTrusters[i])); sl@0: } sl@0: sl@0: mapping->SetCertificateApps(newTrusters); sl@0: CleanupStack::Pop(2, newTrusters); sl@0: sl@0: TRAPD(err, UpdateStoreL()); sl@0: CleanupStack::Pop(2); // *oldTrusters, oldTrusters sl@0: if (err != KErrNone) sl@0: { sl@0: // If there is an error, we undo the change in oldEntry sl@0: mapping->SetCertificateApps(oldTrusters); sl@0: } sl@0: else sl@0: { sl@0: oldTrusters->Close(); sl@0: delete oldTrusters; sl@0: } sl@0: } sl@0: sl@0: sl@0: void CTest1CertStore::DoSetTrustL(const CCTCertInfo& aCertInfo, sl@0: TBool aTrusted) sl@0: { sl@0: TInt index = iCerts->Index(aCertInfo); sl@0: if (index == KErrNotFound) sl@0: { sl@0: User::Leave(index); sl@0: } sl@0: CFileCertStoreMapping* mapping = iCerts->Mapping(index); sl@0: TBool oldValue = mapping->Trusted(); sl@0: mapping->SetTrusted(aTrusted); sl@0: TRAPD(err, UpdateStoreL()); sl@0: if (err != KErrNone) sl@0: { sl@0: // If there is an error, we undo the change in oldEntry sl@0: mapping->SetTrusted(oldValue); sl@0: } sl@0: } sl@0: sl@0: void CTest1CertStore::Add(const TDesC& aLabel, sl@0: TCertificateFormat aFormat, sl@0: TCertificateOwnerType aCertificateOwnerType, sl@0: const TKeyIdentifier* aSubjectKeyId, sl@0: const TKeyIdentifier* aIssuerKeyId, sl@0: const TDesC8& aCert, sl@0: TRequestStatus& aStatus) sl@0: { sl@0: TRAPD(err, DoAddL(aLabel, aFormat, aCertificateOwnerType, aSubjectKeyId, sl@0: aIssuerKeyId, aCert)); sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, err); sl@0: } sl@0: sl@0: void CTest1CertStore::CancelAdd() sl@0: { sl@0: Cancel(); sl@0: } sl@0: sl@0: void CTest1CertStore::DoAddL(const TDesC& aLabel, sl@0: TCertificateFormat aFormat, sl@0: TCertificateOwnerType aCertificateOwnerType, sl@0: const TKeyIdentifier* aSubjectKeyId, sl@0: const TKeyIdentifier* aIssuerKeyId, sl@0: const TDesC8& aCert) sl@0: { sl@0: TKeyIdentifier subjectKeyId; sl@0: switch (aFormat) sl@0: { sl@0: case EX509Certificate: sl@0: if (!aSubjectKeyId) sl@0: { sl@0: CCertificate* cert = CX509Certificate::NewLC(aCert); sl@0: subjectKeyId = cert->KeyIdentifierL(); sl@0: aSubjectKeyId = &subjectKeyId; sl@0: CleanupStack::PopAndDestroy(cert); sl@0: } sl@0: break; sl@0: sl@0: case EWTLSCertificate: sl@0: if (!aSubjectKeyId) sl@0: { sl@0: CCertificate* cert = CWTLSCertificate::NewLC(aCert); sl@0: subjectKeyId = cert->KeyIdentifierL(); sl@0: aSubjectKeyId = &subjectKeyId; sl@0: CleanupStack::PopAndDestroy(cert); sl@0: } sl@0: break; sl@0: sl@0: case EX509CertificateUrl: sl@0: if (!aSubjectKeyId) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: User::Leave(KErrNotSupported); sl@0: break; sl@0: } sl@0: sl@0: sl@0: TInt iend = iCerts->Count(); sl@0: for (TInt i = 0; i < iend; i++) sl@0: { sl@0: if (iCerts->Entry(i).Label() == aLabel) sl@0: { sl@0: User::Leave(KErrBadName); sl@0: } sl@0: } sl@0: sl@0: CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, aFormat, sl@0: aCertificateOwnerType, aCert.Length(), aSubjectKeyId, aIssuerKeyId, iToken, iCerts->Count()); sl@0: // AddCertL takes ownership of entry no matter what happens. sl@0: AddCertL(entry, aCert, *iCerts); sl@0: } sl@0: sl@0: /*void CFileCertStore::AddUserCertL(const CCertificate& aCert, sl@0: const TDesC& aLabel, sl@0: TCertificateFormat aFormat, sl@0: const TKeyIdentifier& aIssuerKeyHash, sl@0: const TKeyIdentifier& aSubjectKeyHash) sl@0: { sl@0: if (aFormat != EX509Certificate) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: TInt iend = iUserCerts->Count(); sl@0: for (TInt i = 0; i < iend; i++) sl@0: { sl@0: if (iUserCerts->Entry(i).Label() == aLabel) sl@0: { sl@0: User::Leave(KErrBadName); sl@0: } sl@0: } sl@0: sl@0: // We compute the subject key hash using the information contained in the certificate sl@0: const CSubjectPublicKeyInfo& key = aCert.PublicKey(); sl@0: CX509RSAPublicKey* rsaKey = CX509RSAPublicKey::NewLC(key.KeyData()); sl@0: #ifdef SYMBIAN_CRYPTO sl@0: const TInteger& modulus = rsaKey->Modulus(); sl@0: #else sl@0: const CInteger& modulus = rsaKey->Modulus(); sl@0: #endif sl@0: HBufC8* modulusBuffer = modulus.BufferLC(); sl@0: sl@0: CSHA1* sha1 = CSHA1::NewL(); sl@0: CleanupStack::PushL(sha1); sl@0: sl@0: TPtrC8 hash = sha1->Hash(*modulusBuffer); sl@0: sl@0: sl@0: TKeyIdentifier keyId; sl@0: keyId = hash; sl@0: sl@0: CleanupStack::PopAndDestroy(3); // rsaKey, modulusBuffer, sha1 sl@0: sl@0: // If the caller supplied a aSubjectKeyHash, we must compare it with the computed sl@0: // value and ensure they are the same sl@0: if ((aSubjectKeyHash != KNullDesC8) && (aSubjectKeyHash != keyId)) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, sl@0: *this, EX509Certificate, aCert.Encoding().Length(), keyId, aIssuerKeyHash); sl@0: AddCertL(entry, aCert, *iUserCerts); sl@0: }*/ sl@0: sl@0: /*void CFileCertStore::AddUserCertURLL(const TDesC8& aCert, sl@0: const TDesC& aLabel, sl@0: const TKeyIdentifier& aIssuerKeyHash, sl@0: const TKeyIdentifier& aSubjectKeyHash) sl@0: { sl@0: TInt iEnd = iUserCerts->Count(); sl@0: for (TInt i = 0; i < iEnd; i++) sl@0: { sl@0: if (iUserCerts->Entry(i).Label() == aLabel) sl@0: { sl@0: User::Leave(KErrBadName); sl@0: } sl@0: } sl@0: sl@0: CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, sl@0: *this, EX509CertificateUrl, aCert.Length(), aSubjectKeyHash, aIssuerKeyHash); sl@0: AddCertL(entry, aCert, *iUserCerts); sl@0: }*/ sl@0: sl@0: void CTest1CertStore::AddCertL(CCTCertInfo* aCertInfo, sl@0: const TDesC8& aCert, sl@0: CFileCertStoreMappings& aMappings) sl@0: { sl@0: CleanupReleasePushL(*aCertInfo); sl@0: //create the mapping object sl@0: CFileCertStoreMapping* mapping = CFileCertStoreMapping::NewL(); sl@0: mapping->SetEntry(aCertInfo); sl@0: CleanupStack::Pop();//aEntry, mapping has taken ownership sl@0: CleanupStack::PushL(mapping); sl@0: sl@0: TCleanupItem cleanupStore(CTest1CertStore::RevertStore, iStore);//store will revert() if a leave occurs sl@0: CleanupStack::PushL(cleanupStore); sl@0: sl@0: //store cert sl@0: RStoreWriteStream stream; sl@0: TStreamId certId = stream.CreateLC(*iStore);//stream for cert sl@0: stream.WriteL(aCert); sl@0: stream.CommitL(); sl@0: CleanupStack::PopAndDestroy();//stream sl@0: mapping->SetId(certId); sl@0: sl@0: //add mapping to mappings, & store mappings sl@0: aMappings.AddL(mapping); //takes ownership sl@0: CleanupStack::Pop();//mapping; mappings has taken ownership sl@0: sl@0: //Update the mapping. if error, remove the entry. sl@0: TRAPD(err, aMappings.ReplaceL()); sl@0: if (err == KErrNone) sl@0: { sl@0: TRAP(err, iStore->CommitL()); sl@0: if (err != KErrNone) sl@0: { sl@0: aMappings.Remove(*mapping->Entry()); sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: //oom tests pass currently sl@0: aMappings.Remove(*mapping->Entry()); sl@0: User::Leave(err); sl@0: } sl@0: CleanupStack::Pop();//revert store sl@0: } sl@0: sl@0: void CTest1CertStore::UpdateStoreL() sl@0: { sl@0: //tries to write out the new cacerts to the file sl@0: TCleanupItem cleanupStore(RevertStore, iStore);//store will revert() if a leave occurs sl@0: CleanupStack::PushL(cleanupStore); sl@0: iCerts->ReplaceL(); sl@0: iStore->CommitL(); sl@0: CleanupStack::Pop();//revert store sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: void CTest1CertStore::DoLoadL(TDes8& aCertificate, CFileCertStoreMapping& aMapping) const sl@0: { sl@0: RStoreReadStream stream; sl@0: stream.OpenLC(*iStore, aMapping.Id()); sl@0: CCTCertInfo* entry = aMapping.Entry(); sl@0: stream.ReadL(aCertificate, entry->Size()); sl@0: CleanupStack::PopAndDestroy();//stream sl@0: } sl@0: sl@0: void CTest1CertStore::DoRemoveL(const CCTCertInfo& aCertInfo) sl@0: { sl@0: switch(aCertInfo.CertificateFormat()) sl@0: { sl@0: case EWTLSCertificate://must be a CA cert sl@0: case EX509CertificateUrl: sl@0: case EX509Certificate: sl@0: User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, ETrue)); sl@0: break; sl@0: sl@0: default: sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: TRAPD(err, UpdateStoreL()); sl@0: if (err != KErrNone) sl@0: { sl@0: User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, EFalse)); sl@0: } sl@0: else sl@0: { sl@0: User::LeaveIfError(iCerts->Remove(aCertInfo)); sl@0: } sl@0: } sl@0: sl@0: //private functions sl@0: CTest1CertStore::CTest1CertStore(CTest1CertStoreToken& aToken, RFs& aFs) sl@0: : CActive(EPriorityNormal), iToken(aToken), iFs(aFs) sl@0: { sl@0: CActiveScheduler::Add(this); sl@0: } sl@0: sl@0: void CTest1CertStore::ConstructL(const TDesC& aFileName, TFileMode aMode) sl@0: { sl@0: iStore = OpenStoreL(aFileName, aMode); sl@0: RestoreL(); sl@0: } sl@0: sl@0: void CTest1CertStore::RestoreL() sl@0: { sl@0: TStreamId caCertEntryStreamId; sl@0: RStoreReadStream stream; sl@0: stream.OpenLC(*iStore, iStore->Root()); sl@0: sl@0: stream >> caCertEntryStreamId; sl@0: CleanupStack::PopAndDestroy();//stream sl@0: sl@0: iCerts = CFileCertStoreMappings::NewL(caCertEntryStreamId, *iStore); sl@0: RStoreReadStream caCertEntryStream; sl@0: caCertEntryStream.OpenLC(*iStore, caCertEntryStreamId); sl@0: sl@0: TInt caCount = caCertEntryStream.ReadInt32L(); sl@0: for (TInt i = 0; i < caCount; i++) sl@0: { sl@0: CFileCertStoreMapping* caCertMapping = CFileCertStoreMapping::NewL(); sl@0: CleanupStack::PushL(caCertMapping); sl@0: CCTCertInfo* caCertEntry = CCTCertInfo::NewLC(caCertEntryStream, iToken); sl@0: caCertMapping->SetEntry(caCertEntry); sl@0: CleanupStack::Pop();//caCertEntry sl@0: // Read the CertificateApps uids sl@0: RArray* certificateApps = new(ELeave) RArray(); sl@0: CleanupStack::PushL(certificateApps); sl@0: CleanupClosePushL(*certificateApps); sl@0: TInt count = caCertEntryStream.ReadInt32L(); sl@0: for (TInt j = 0; j < count; j++) sl@0: { sl@0: TUid id; sl@0: caCertEntryStream >> id; sl@0: User::LeaveIfError(certificateApps->Append(id)); sl@0: } sl@0: CleanupStack::Pop(2); // *certificateApps, certificateApps sl@0: caCertMapping->SetCertificateApps(certificateApps); sl@0: TBool trusted = caCertEntryStream.ReadUint8L(); sl@0: caCertMapping->SetTrusted(trusted); sl@0: TStreamId caCertStreamId; sl@0: caCertEntryStream >> caCertStreamId; sl@0: caCertMapping->SetId(caCertStreamId); sl@0: iCerts->AddL(caCertMapping); sl@0: CleanupStack::Pop();//caCertMapping sl@0: } sl@0: CleanupStack::PopAndDestroy();//caCertStream sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: CPermanentFileStore* CTest1CertStore::OpenStoreLC(const TDesC& aFileName, TFileMode aMode) sl@0: { sl@0: //this function creates, opens and returns a permanent file store in KCertStorePath, sl@0: //on the drive letter passed in, leaving it on the cleanup stack. sl@0: //if the store isn't found it returns NULL sl@0: TInt err = iFile.Open(iFs, aFileName, aMode); sl@0: if (err == KErrNone) sl@0: { sl@0: CPermanentFileStore* store = CPermanentFileStore::FromLC(iFile); sl@0: return store; sl@0: } sl@0: else sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: return NULL; sl@0: } sl@0: sl@0: CPermanentFileStore* CTest1CertStore::OpenStoreL(const TDesC& aFileName, TFileMode aMode) sl@0: { sl@0: CPermanentFileStore* store = NULL; sl@0: store = OpenStoreLC(aFileName, aMode); sl@0: CleanupStack::Pop(store); sl@0: return store; sl@0: } sl@0: sl@0: void CTest1CertStore::RunL() sl@0: { sl@0: switch (iState) sl@0: { sl@0: case EGetKeyInfos: sl@0: iKeyFilter.iUsage = KeyUsageX509ToPKCS15Private(iFilter->iKeyUsage); sl@0: iUnifiedKeyStore->List(iKeyInfos, iKeyFilter, iStatus); sl@0: iState = EList; sl@0: SetActive(); sl@0: break; sl@0: sl@0: case ERetrieve: sl@0: // Asynch Retrieve sl@0: RetrieveNow(*iAsynchCertInfo,*iAsynchCertificate,*iOriginalRequestStatus); sl@0: break; sl@0: sl@0: case EList: sl@0: { sl@0: TInt count = iCerts->Count(); sl@0: for (TInt index = 0; index < count; index++) sl@0: { sl@0: const CCTCertInfo& certInfo = iCerts->Entry(index); sl@0: sl@0: TBool accept = ETrue; sl@0: if (iFilter->iUidIsSet) sl@0: { sl@0: accept = iCerts->Mapping(index)->IsApplicable(iFilter->iUid); sl@0: } sl@0: if (iFilter->iFormatIsSet && accept) sl@0: { sl@0: accept = (iFilter->iFormat == certInfo.CertificateFormat()); sl@0: } sl@0: if (iFilter->iOwnerTypeIsSet && accept) sl@0: { sl@0: accept = (iFilter->iOwnerType == certInfo.CertificateOwnerType()); sl@0: } sl@0: if ((iFilter->iSubjectKeyId != KNullDesC8) && accept) sl@0: { sl@0: accept = (iFilter->iSubjectKeyId == certInfo.SubjectKeyId()); sl@0: } sl@0: sl@0: if (accept) sl@0: { sl@0: // Fill in the cert hash. sl@0: // (This returns an incorrect hard-coded value, which allows sl@0: // the test code to check that the store is being treated sl@0: // correctly as a hardware store, as it'll behave differently sl@0: // to if it returned the correct hashes.) sl@0: _LIT8(KHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d"); sl@0: CCTCertInfo* copy = CCTCertInfo::NewLC( sl@0: certInfo.Label(), certInfo.CertificateFormat(), sl@0: certInfo.CertificateOwnerType(), certInfo.Size(), sl@0: &certInfo.SubjectKeyId(), &certInfo.IssuerKeyId(), sl@0: certInfo.Token(), certInfo.Handle().iObjectId, sl@0: ETrue, &KHash); sl@0: User::LeaveIfError(iCertInfos->Append(copy)); sl@0: CleanupStack::Pop(); sl@0: } sl@0: } sl@0: iKeyInfos.Close(); sl@0: delete iUnifiedKeyStore; sl@0: iUnifiedKeyStore = 0; sl@0: User::RequestComplete(iOriginalRequestStatus, KErrNone); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: // ignore the undefined operations sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CTest1CertStore::DoCancel() sl@0: { sl@0: if (iUnifiedKeyStore) sl@0: { sl@0: if (iState == EGetKeyInfos) sl@0: { sl@0: iUnifiedKeyStore->CancelInitialize(); sl@0: } sl@0: else if (iState == EList) sl@0: { sl@0: iUnifiedKeyStore->CancelList(); sl@0: } sl@0: iState = EList; sl@0: iKeyInfos.Close(); sl@0: delete iUnifiedKeyStore; sl@0: iUnifiedKeyStore = 0; sl@0: } sl@0: User::RequestComplete(iOriginalRequestStatus, KErrCancel); sl@0: } sl@0: sl@0: const TImplementationProxy ImplementationTable[] = sl@0: { sl@0: #ifdef CERTSTORE_SOFTWARE_ASYNCH sl@0: IMPLEMENTATION_PROXY_ENTRY(0x101FF738, CTest1CertStoreTokenType::NewL) sl@0: #else sl@0: #ifdef CERTSTORE_HARDWARE_SIM sl@0: IMPLEMENTATION_PROXY_ENTRY(0x10206846, CTest1CertStoreTokenType::NewL) sl@0: #else sl@0: #ifdef CERTSTORE_HARDWARE_WIM sl@0: IMPLEMENTATION_PROXY_ENTRY(0x10206847, CTest1CertStoreTokenType::NewL) sl@0: #else sl@0: #ifdef CERTSTORE_HARDWARE_UICC sl@0: IMPLEMENTATION_PROXY_ENTRY(0x10206848, CTest1CertStoreTokenType::NewL) sl@0: #else sl@0: #ifdef CERTSTORE_DEVICE_IMMUTABLE sl@0: IMPLEMENTATION_PROXY_ENTRY(0x102077C3, CTest1CertStoreTokenType::NewL) sl@0: #else sl@0: IMPLEMENTATION_PROXY_ENTRY(0x101F5279, CTest1CertStoreTokenType::NewL) sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: }; sl@0: sl@0: EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount) sl@0: { sl@0: aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy); sl@0: sl@0: return ImplementationTable; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: