os/security/crypto/weakcryptospi/source/spi/cryptospisetup/cryptopluginsloader.cpp
Update contrib.
2 * Copyright (c) 2007-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.
15 * crypto plugins loader implementation
16 * crypto spi state implementation
24 #include "cryptopluginsloader.h"
26 #include <cryptospi/cryptospidef.h>
27 #include "cryptospiproperty.h"
28 #include <e32property.h>
30 using namespace CryptoSpi;
32 _LIT(KPluginDir, "z:\\sys\\bin\\");
34 HBufC *GetConfigFileNameL();
37 // Implementation of TCharacteristicsDll
39 TCharacteristicsDll::TCharacteristicsDll(const TAny* aCharacteristics, TInt aIndex)
40 :iCharacteristics(aCharacteristics), iDllIndex(aIndex)
44 void TCharacteristicsDll::ExternalizeL(RWriteStream& aStream)
46 const TCommonCharacteristics* common=reinterpret_cast<const TCommonCharacteristics*>(iCharacteristics);
47 switch (common->iInterfaceUID)
50 case KAsyncHashInterface:
52 const THashCharacteristics* characteristics=reinterpret_cast<const THashCharacteristics*>(iCharacteristics);
53 characteristics->ExternalizeL(aStream);
56 case KRandomInterface:
57 case KAsyncRandomInterface:
59 const TRandomCharacteristics* characteristics=reinterpret_cast<const TRandomCharacteristics*>(iCharacteristics);
60 characteristics->ExternalizeL(aStream);
64 case KSymmetricCipherInterface:
65 case KAsyncSymmetricCipherInterface:
67 const TSymmetricCipherCharacteristics* characteristics=reinterpret_cast<const TSymmetricCipherCharacteristics*>(iCharacteristics);
68 characteristics->ExternalizeL(aStream);
72 case KAsymmetricCipherInterface:
73 case KAsyncAsymmetricCipherInterface:
75 const TAsymmetricCipherCharacteristics* characteristics=reinterpret_cast<const TAsymmetricCipherCharacteristics*>(iCharacteristics);
76 characteristics->ExternalizeL(aStream);
80 case KSignerInterface:
81 case KVerifierInterface:
82 case KAsyncSignerInterface:
83 case KAsyncVerifierInterface:
85 const TAsymmetricSignatureCharacteristics* characteristics=reinterpret_cast<const TAsymmetricSignatureCharacteristics*>(iCharacteristics);
86 characteristics->ExternalizeL(aStream);
90 case KKeyAgreementInterface:
91 case KAsyncKeyAgreementInterface:
93 const TKeyAgreementCharacteristics* characteristics=reinterpret_cast<const TKeyAgreementCharacteristics*>(iCharacteristics);
94 characteristics->ExternalizeL(aStream);
98 case KKeypairGeneratorInterface:
99 case KAsyncKeypairGeneratorInterface:
101 const TAsymmetricKeypairGeneratorCharacteristics* characteristics=reinterpret_cast<const TAsymmetricKeypairGeneratorCharacteristics*>(iCharacteristics);
102 characteristics->ExternalizeL(aStream);
105 #ifdef SYMBIAN_SDP_IPSEC_VOIP_SUPPORT
107 case KAsyncMacInterface:
109 const TMacCharacteristics* characteristics=reinterpret_cast<const TMacCharacteristics*>(iCharacteristics);
110 characteristics->ExternalizeL(aStream);
115 User::Leave(KErrNotSupported);
118 aStream.WriteInt8L(iDllIndex);
122 // Implementation of CCharacteristicDllIndexList
124 CCharacteristicDllIndexList* CCharacteristicDllIndexList::NewL()
126 CCharacteristicDllIndexList* self=new(ELeave) CCharacteristicDllIndexList();
130 CCharacteristicDllIndexList* CCharacteristicDllIndexList::NewLC()
132 CCharacteristicDllIndexList* self=new(ELeave) CCharacteristicDllIndexList();
133 CleanupStack::PushL(self);
137 CCharacteristicDllIndexList::CCharacteristicDllIndexList()
141 CCharacteristicDllIndexList::~CCharacteristicDllIndexList()
143 iCharacteristicList.Close();
146 void CCharacteristicDllIndexList::ExternalizeL(RWriteStream& aStream)
148 TInt count=iCharacteristicList.Count();
149 aStream.WriteInt16L(count);
150 for (TInt i=0;i<count;i++)
152 iCharacteristicList[i].ExternalizeL(aStream);
157 // Implementation of CCryptoPluginsLoader
159 CCryptoPluginsLoader* CCryptoPluginsLoader::NewL()
161 CCryptoPluginsLoader* self=NewLC();
162 CleanupStack::Pop(self);
166 CCryptoPluginsLoader* CCryptoPluginsLoader::NewLC()
168 CCryptoPluginsLoader* self=new(ELeave)CCryptoPluginsLoader();
169 CleanupStack::PushL(self);
174 CCryptoPluginsLoader::CCryptoPluginsLoader()
178 CCryptoPluginsLoader::~CCryptoPluginsLoader()
180 //Destroy the characteristic map
181 THashMapIter<TInt32, CCharacteristicDllIndexList*> it(iInterfaceCharacteristicsMap);
183 while (it.CurrentValue())
185 delete *it.CurrentValue();
188 iInterfaceCharacteristicsMap.Close();
190 //Unload the plugin DLLs and release the array
191 TInt dllCount=iPluginDllList.Count();
192 for (TInt i=0;i<dllCount;i++)
194 iPluginDllList[i].Close();
196 iPluginDllList.Close();
197 iPluginNames.Close();
200 void CCryptoPluginsLoader::ConstructL()
205 void CCryptoPluginsLoader::LoadPluginsL()
207 HBufC *configFileBuf = GetConfigFileNameL();
208 CleanupStack::PushL(configFileBuf);
209 TPtr configFile(configFileBuf->Des());
212 User::LeaveIfError(fs.Connect());
213 CleanupClosePushL(fs);
216 User::LeaveIfError(file.Open(fs, configFile, KEntryAttNormal));
217 CleanupClosePushL(file);
223 User::LeaveIfError(ft.Read(line));
225 //Load all the plugins
229 filename.Append(KPluginDir);
230 filename.Append(line);
234 TInt err=plugin.Load(filename);
237 CleanupClosePushL(plugin);
238 iPluginDllList.AppendL(plugin);
239 CleanupStack::Pop(&plugin);
240 iPluginNames.AppendL(line);
243 while(ft.Read(line) == KErrNone);
246 CleanupStack::PopAndDestroy(&file);
247 CleanupStack::PopAndDestroy(&fs);
248 CleanupStack::PopAndDestroy(configFileBuf);
250 BuildPluginCharacteristicsL();
253 void CCryptoPluginsLoader::BuildPluginCharacteristicsL()
255 TInt dllCount=iPluginDllList.Count();
256 TInt interfaceCount=sizeof(KInterfacesUids)/sizeof(KInterfacesUids[0]);
257 for (TInt i=0;i<dllCount;i++)
260 //find the enumeration function pointer
261 EnumerateCharacteristicsFunc enumerateFunc=(EnumerateCharacteristicsFunc)iPluginDllList[i].Lookup(EEnumerateCharacteristicsOrdinal);
264 for (TInt j=0;j<interfaceCount;j++)
266 BuildInterfaceCharacteristicsL(KInterfacesUids[j], enumerateFunc, i);
272 void CCryptoPluginsLoader::BuildInterfaceCharacteristicsL(TUid aInterfaceUid,
273 EnumerateCharacteristicsFunc aEntryFunc,
276 //Get the corresponding characteristics from the plug-in module
278 const TCharacteristics** p = aEntryFunc(aInterfaceUid, numPlugins);
280 //characteristics are available
284 CCharacteristicDllIndexList** charsListPtr=iInterfaceCharacteristicsMap.Find(aInterfaceUid.iUid);
285 CCharacteristicDllIndexList* charsList=NULL;
286 //create new one if it is not in the map
289 charsList=CCharacteristicDllIndexList::NewLC();
290 iInterfaceCharacteristicsMap.InsertL(aInterfaceUid.iUid, charsList);
291 CleanupStack::Pop(charsList);
295 //Use the existing one.
296 charsList=*charsListPtr;
299 for (TInt i=0; i<numPlugins; ++i)
301 // The pointer should never be null, but try to prevent
302 // rogue plug-ins from breaking the framework.
306 TCharacteristicsDll temp(*p, aDllIndex);
307 charsList->iCharacteristicList.AppendL(temp);
314 void CCryptoPluginsLoader::CreatePluginConfigPropertyL()
316 RProcess thisProcess;
317 // sanity check that feature property category in common header equals SID of this process
318 ASSERT(KCryptoSpiPropertyCat == thisProcess.SecureId());
319 TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass);
320 TSecurityPolicy writePolicy(thisProcess.SecureId());
322 TInt count=iPluginNames.Count();
323 TInt publishResult=KErrNone;
324 for (TInt i=0;i<count;i++)
326 publishResult = RProperty::Define(KPluginsConfigurationKey+i, RProperty::EByteArray, readPolicy, writePolicy);
327 if ((publishResult == KErrNone) || (publishResult == KErrAlreadyExists))
330 RWriteStream ws(&ss);
333 HBufC8* buf = HBufC8::NewLC(ss.Size());
334 TPtr8 bufPtr(buf->Des());
335 RDesWriteStream dws(bufPtr);
336 dws<<iPluginNames[i];
338 publishResult = RProperty::Set(KCryptoSpiPropertyCat, KPluginsConfigurationKey+i, bufPtr);
339 CleanupStack::PopAndDestroy(buf);
343 User::Leave(publishResult);
346 publishResult = RProperty::Define(KPluginsConfigurationKeyCount, RProperty::EInt, readPolicy, writePolicy);
347 if ((publishResult == KErrNone) || (publishResult == KErrAlreadyExists))
349 publishResult = RProperty::Set(KCryptoSpiPropertyCat, KPluginsConfigurationKeyCount, count);
353 User::Leave(publishResult);
357 void CCryptoPluginsLoader::CreateInterfacePropertyL(TInt32 aInterface)
360 RWriteStream ws(&ss);
362 DoCreateInterfacePropertyL(ws, aInterface);
364 HBufC8* buf = HBufC8::NewLC(ss.Size());
365 TPtr8 bufPtr(buf->Des());
366 RDesWriteStream dws(bufPtr);
367 dws.WriteInt32L(ss.Size());
368 DoCreateInterfacePropertyL(dws, aInterface);
370 User::LeaveIfError(RProperty::Set(KCryptoSpiPropertyCat, aInterface, bufPtr));
371 CleanupStack::PopAndDestroy(buf);
374 void CCryptoPluginsLoader::DoCreateInterfacePropertyL(RWriteStream& aStream, TInt32 aInterface)
376 RProcess thisProcess;
377 // sanity check that feature property category in common header equals SID of this process
378 ASSERT(KCryptoSpiPropertyCat == thisProcess.SecureId());
379 TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass);
380 TSecurityPolicy writePolicy(thisProcess.SecureId());
382 TInt publishResult = RProperty::Define(aInterface, RProperty::ELargeByteArray, readPolicy, writePolicy);
383 if ((publishResult == KErrNone) || (publishResult == KErrAlreadyExists))
385 CCharacteristicDllIndexList** charsListPtr=iInterfaceCharacteristicsMap.Find(aInterface);
388 (*charsListPtr)->ExternalizeL(aStream);
393 User::Leave(publishResult);
398 // Implementation of TSizeStream
400 void TSizeStream::DoWriteL(const TAny* /* aPtr */, TInt aLength)
405 _LIT(KPluginConfigFile, "z:\\resource\\cryptospi\\plug-ins.txt");
406 HBufC *GetConfigFileNameL()
408 // Check the command line. It should be empty or a single decimal digit
409 TInt argsLen = User::CommandLineLength();
413 // No arguments so return the default config file name
414 return KPluginConfigFile().AllocL();
417 // We only support a single digit argument
420 User::Leave(KErrArgument);
424 // Read the single char command line
426 User::CommandLine(argsBuf);
428 // Check if it is a digit
429 TChar ch(argsBuf[0]);
432 User::Leave(KErrArgument);
435 // Create buffer for config file name, inc space to append the digit
436 HBufC *configFileBuf = HBufC::NewL(KPluginConfigFile().Length()+1);
437 TPtr configFile(configFileBuf->Des());
439 // Initialise it to the default file name
440 configFile = KPluginConfigFile;
442 // Append the digit to the config file name
443 configFile.Append(ch);
445 return configFileBuf;