diff -r 675a964f4eb5 -r 35751d3474b7 crypto/weakcryptospi/source/spi/cryptospisetup/cryptopluginsloader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crypto/weakcryptospi/source/spi/cryptospisetup/cryptopluginsloader.cpp Thu Sep 10 14:01:51 2009 +0300 @@ -0,0 +1,448 @@ +/* +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of the License "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* crypto plugins loader implementation +* crypto spi state implementation +* +*/ + + +/** + @file +*/ +#include "cryptopluginsloader.h" +#include +#include +#include "cryptospiproperty.h" +#include + +using namespace CryptoSpi; + +_LIT(KPluginDir, "z:\\sys\\bin\\"); + +HBufC *GetConfigFileNameL(); + +// +// Implementation of TCharacteristicsDll +// +TCharacteristicsDll::TCharacteristicsDll(const TAny* aCharacteristics, TInt aIndex) +:iCharacteristics(aCharacteristics), iDllIndex(aIndex) + { + } + +void TCharacteristicsDll::ExternalizeL(RWriteStream& aStream) + { + const TCommonCharacteristics* common=reinterpret_cast(iCharacteristics); + switch (common->iInterfaceUID) + { + case KHashInterface: + case KAsyncHashInterface: + { + const THashCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + case KRandomInterface: + case KAsyncRandomInterface: + { + const TRandomCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + + case KSymmetricCipherInterface: + case KAsyncSymmetricCipherInterface: + { + const TSymmetricCipherCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + + case KAsymmetricCipherInterface: + case KAsyncAsymmetricCipherInterface: + { + const TAsymmetricCipherCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + + case KSignerInterface: + case KVerifierInterface: + case KAsyncSignerInterface: + case KAsyncVerifierInterface: + { + const TAsymmetricSignatureCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + + case KKeyAgreementInterface: + case KAsyncKeyAgreementInterface: + { + const TKeyAgreementCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; + + case KKeypairGeneratorInterface: + case KAsyncKeypairGeneratorInterface: + { + const TAsymmetricKeypairGeneratorCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; +#ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT + case KMacInterface: + case KAsyncMacInterface: + { + const TMacCharacteristics* characteristics=reinterpret_cast(iCharacteristics); + characteristics->ExternalizeL(aStream); + } + break; +#endif + default: + User::Leave(KErrNotSupported); + } + + aStream.WriteInt8L(iDllIndex); + } + +// +// Implementation of CCharacteristicDllIndexList +// +CCharacteristicDllIndexList* CCharacteristicDllIndexList::NewL() + { + CCharacteristicDllIndexList* self=new(ELeave) CCharacteristicDllIndexList(); + return self; + } + +CCharacteristicDllIndexList* CCharacteristicDllIndexList::NewLC() + { + CCharacteristicDllIndexList* self=new(ELeave) CCharacteristicDllIndexList(); + CleanupStack::PushL(self); + return self; + } + +CCharacteristicDllIndexList::CCharacteristicDllIndexList() + { + } + +CCharacteristicDllIndexList::~CCharacteristicDllIndexList() + { + iCharacteristicList.Close(); + } + +void CCharacteristicDllIndexList::ExternalizeL(RWriteStream& aStream) + { + TInt count=iCharacteristicList.Count(); + aStream.WriteInt16L(count); + for (TInt i=0;iConstructL(); + return self; + } + +CCryptoPluginsLoader::CCryptoPluginsLoader() + { + } + +CCryptoPluginsLoader::~CCryptoPluginsLoader() + { + //Destroy the characteristic map + THashMapIter it(iInterfaceCharacteristicsMap); + it.NextValue(); + while (it.CurrentValue()) + { + delete *it.CurrentValue(); + it.NextValue(); + } + iInterfaceCharacteristicsMap.Close(); + + //Unload the plugin DLLs and release the array + TInt dllCount=iPluginDllList.Count(); + for (TInt i=0;iDes()); + + RFs fs; + User::LeaveIfError(fs.Connect()); + CleanupClosePushL(fs); + + RFile file; + User::LeaveIfError(file.Open(fs, configFile, KEntryAttNormal)); + CleanupClosePushL(file); + + TFileText ft; + ft.Set(file); + TFileName line; + + User::LeaveIfError(ft.Read(line)); + + //Load all the plugins + do + { + TFileName filename; + filename.Append(KPluginDir); + filename.Append(line); + + //Load... + RLibrary plugin; + TInt err=plugin.Load(filename); + if (err==KErrNone) + { + CleanupClosePushL(plugin); + iPluginDllList.AppendL(plugin); + CleanupStack::Pop(&plugin); + iPluginNames.AppendL(line); + } + } + while(ft.Read(line) == KErrNone); + + + CleanupStack::PopAndDestroy(&file); + CleanupStack::PopAndDestroy(&fs); + CleanupStack::PopAndDestroy(configFileBuf); + + BuildPluginCharacteristicsL(); + } + +void CCryptoPluginsLoader::BuildPluginCharacteristicsL() + { + TInt dllCount=iPluginDllList.Count(); + TInt interfaceCount=sizeof(KInterfacesUids)/sizeof(KInterfacesUids[0]); + for (TInt i=0;iiCharacteristicList.AppendL(temp); + } + p++; + } + } + } + +void CCryptoPluginsLoader::CreatePluginConfigPropertyL() + { + RProcess thisProcess; + // sanity check that feature property category in common header equals SID of this process + ASSERT(KCryptoSpiPropertyCat == thisProcess.SecureId()); + TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass); + TSecurityPolicy writePolicy(thisProcess.SecureId()); + + TInt count=iPluginNames.Count(); + TInt publishResult=KErrNone; + for (TInt i=0;iDes()); + RDesWriteStream dws(bufPtr); + dws<Des()); + RDesWriteStream dws(bufPtr); + dws.WriteInt32L(ss.Size()); + DoCreateInterfacePropertyL(dws, aInterface); + dws.CommitL(); + User::LeaveIfError(RProperty::Set(KCryptoSpiPropertyCat, aInterface, bufPtr)); + CleanupStack::PopAndDestroy(buf); + } + +void CCryptoPluginsLoader::DoCreateInterfacePropertyL(RWriteStream& aStream, TInt32 aInterface) + { + RProcess thisProcess; + // sanity check that feature property category in common header equals SID of this process + ASSERT(KCryptoSpiPropertyCat == thisProcess.SecureId()); + TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass); + TSecurityPolicy writePolicy(thisProcess.SecureId()); + + TInt publishResult = RProperty::Define(aInterface, RProperty::ELargeByteArray, readPolicy, writePolicy); + if ((publishResult == KErrNone) || (publishResult == KErrAlreadyExists)) + { + CCharacteristicDllIndexList** charsListPtr=iInterfaceCharacteristicsMap.Find(aInterface); + if (charsListPtr) + { + (*charsListPtr)->ExternalizeL(aStream); + } + } + else + { + User::Leave(publishResult); + } + } + +// +// Implementation of TSizeStream +// +void TSizeStream::DoWriteL(const TAny* /* aPtr */, TInt aLength) + { + iSize += aLength; + } + +_LIT(KPluginConfigFile, "z:\\resource\\cryptospi\\plug-ins.txt"); +HBufC *GetConfigFileNameL() + { + // Check the command line. It should be empty or a single decimal digit + TInt argsLen = User::CommandLineLength(); + + if(argsLen == 0) + { + // No arguments so return the default config file name + return KPluginConfigFile().AllocL(); + } + + // We only support a single digit argument + if(argsLen != 1) + { + User::Leave(KErrArgument); + } + + + // Read the single char command line + TBuf<1> argsBuf; + User::CommandLine(argsBuf); + + // Check if it is a digit + TChar ch(argsBuf[0]); + if(!ch.IsDigit()) + { + User::Leave(KErrArgument); + } + + // Create buffer for config file name, inc space to append the digit + HBufC *configFileBuf = HBufC::NewL(KPluginConfigFile().Length()+1); + TPtr configFile(configFileBuf->Des()); + + // Initialise it to the default file name + configFile = KPluginConfigFile; + + // Append the digit to the config file name + configFile.Append(ch); + + return configFileBuf; + } + +// End of file