|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * CTrainingMgr - Auth Server helper class |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /** |
|
21 @file |
|
22 */ |
|
23 #include <e32debug.h> |
|
24 #include <s32mem.h> |
|
25 #include "authserver_impl.h" |
|
26 #include "log.h" |
|
27 #include "authrepository.h" |
|
28 |
|
29 using namespace AuthServer; |
|
30 |
|
31 |
|
32 CTrainingMgr::CTrainingMgr(CPluginMgr& aPluginMgr, |
|
33 CAuthDb2& aAuthDb, |
|
34 CAuthRepository& aAuthRepository) : |
|
35 CActive(EPriorityStandard), |
|
36 iPluginMgr(&aPluginMgr), |
|
37 iAuthDb(&aAuthDb), |
|
38 iAuthRepository(aAuthRepository) |
|
39 { |
|
40 CActiveScheduler::Add(this); |
|
41 } |
|
42 |
|
43 CTrainingMgr::~CTrainingMgr() |
|
44 { |
|
45 Cancel(); |
|
46 Cleanup(); |
|
47 } |
|
48 |
|
49 /** |
|
50 * @param aMessage |
|
51 * @param aId The id number to use for the identity |
|
52 * @param aProtKey the protection key will be returned here |
|
53 */ |
|
54 void CTrainingMgr::RegisterIdentityL(const RMessage2& aMessage, |
|
55 TIdentityId aId, |
|
56 CProtectionKey& aProtKey) |
|
57 { |
|
58 __ASSERT_ALWAYS(!IsActive(), |
|
59 User::Panic(KAuthServerShortName, |
|
60 EPanicTrainingMgrBusy)); |
|
61 |
|
62 iProtKey = &aProtKey; |
|
63 iMessage = new (ELeave) RMessage2(aMessage); |
|
64 iCurrentPluginIdx = 0; |
|
65 iIdentity = aId; |
|
66 iDescription = HBufC::NewL(aMessage.GetDesLength(1)); |
|
67 TPtr ptr = iDescription->Des(); |
|
68 aMessage.Read(1,ptr); |
|
69 |
|
70 iState = ERegistrationFirstStep; |
|
71 DoRegistrationStepL(); |
|
72 } |
|
73 |
|
74 TBool CTrainingMgr::RegisterFirstIdentityL(TIdentityId aId, |
|
75 CProtectionKey& aProtKey) |
|
76 { |
|
77 __ASSERT_ALWAYS(!IsActive(), |
|
78 User::Panic(KAuthServerShortName, |
|
79 EPanicTrainingMgrBusy)); |
|
80 |
|
81 iIdentity = aId; |
|
82 iProtKey = &aProtKey; |
|
83 iDescription = HBufC::NewL(KDefaultUserDescription().Length()); |
|
84 *iDescription = KDefaultUserDescription; |
|
85 |
|
86 TBool result = EFalse; |
|
87 |
|
88 //Get the default plugin id from the configuration file. |
|
89 TPluginId defaultPluginId = iAuthRepository.DefaultPluginL(); |
|
90 |
|
91 CAuthPluginInterface* plugin = 0; |
|
92 TRAPD(err, plugin = iPluginMgr->PluginL(defaultPluginId)); |
|
93 |
|
94 if ((err == KErrNone) && |
|
95 (plugin->IsActive()) && |
|
96 (plugin->Type() == EAuthKnowledge) && |
|
97 (plugin->DefaultData(aId, iResult) == KErrNone)) |
|
98 { |
|
99 iCurrentPlugin = plugin->Id(); |
|
100 AddIdentityL(); |
|
101 AddTrainingResultL(); |
|
102 result = ETrue; |
|
103 } |
|
104 |
|
105 Cleanup(); |
|
106 return result; |
|
107 } |
|
108 |
|
109 void CTrainingMgr::TrainPluginL(const RMessage2& aMessage, |
|
110 CProtectionKey& aProtKey) |
|
111 { |
|
112 __ASSERT_ALWAYS(!IsActive(), |
|
113 User::Panic(KAuthServerShortName, |
|
114 EPanicTrainingMgrBusy)); |
|
115 |
|
116 iProtKey = &aProtKey; |
|
117 iIdentity = aMessage.Int0(); |
|
118 iCurrentPlugin = aMessage.Int1(); |
|
119 iMessage = new (ELeave) RMessage2(aMessage); |
|
120 CAuthPluginInterface* plugin = 0; |
|
121 |
|
122 TRAPD(err, plugin = iPluginMgr->PluginL(iCurrentPlugin)); |
|
123 |
|
124 if (err != KErrNone) |
|
125 { |
|
126 iMessage->Complete(err); |
|
127 Cleanup(); |
|
128 return; |
|
129 } |
|
130 else |
|
131 { |
|
132 SetActive(); |
|
133 iState=ETrainingDone; |
|
134 if (plugin->IsActive()) |
|
135 { |
|
136 plugin->Train(iIdentity, iResult, iStatus); |
|
137 } |
|
138 else |
|
139 { |
|
140 TRequestStatus* status = &iStatus; |
|
141 User::RequestComplete(status, KErrAuthServPluginNotActive); |
|
142 } |
|
143 } |
|
144 } |
|
145 |
|
146 TInt CTrainingMgr::RunError(TInt aError) |
|
147 { |
|
148 iMessage->Complete(aError); |
|
149 return KErrNone; |
|
150 |
|
151 } |
|
152 |
|
153 void CTrainingMgr::RunL() |
|
154 { |
|
155 if (iStatus != KErrNone && |
|
156 (iStatus != KErrAuthServPluginCancelled && |
|
157 iStatus != KErrAuthServPluginNotActive)) |
|
158 { |
|
159 // error results other than plugin cancelled or inactive handled here |
|
160 Complete(); |
|
161 return; |
|
162 } |
|
163 switch (iState) |
|
164 { |
|
165 case ERegistrationFirstStep: |
|
166 if (AddIdentityL()) |
|
167 { |
|
168 iState = ERegistering; |
|
169 } |
|
170 DoRegistrationStepL(); |
|
171 break; |
|
172 case ERegistering: |
|
173 AddTrainingResultL(); |
|
174 DoRegistrationStepL(); |
|
175 break; |
|
176 case ETrainingDone: |
|
177 if (iStatus == KErrAuthServPluginCancelled) |
|
178 { |
|
179 DEBUG_PRINTF(_L8("Plugin cancelled in training")); |
|
180 } |
|
181 AddTrainingResultL(); |
|
182 Complete(); |
|
183 break; |
|
184 case ERegistrationDone: |
|
185 WriteResultToMsgL(); |
|
186 Complete(); |
|
187 break; |
|
188 } |
|
189 } |
|
190 |
|
191 void CTrainingMgr::DoCancel() |
|
192 { |
|
193 CAuthPluginInterface* plugin = 0; |
|
194 TRAPD(err, plugin = iPluginMgr->PluginL(iCurrentPlugin)); |
|
195 |
|
196 if (err == KErrNone) |
|
197 { |
|
198 iMessage->Complete(KErrCancel); |
|
199 plugin->Cancel(); |
|
200 } |
|
201 else |
|
202 { |
|
203 User::Panic(KAuthServerShortName, EPanicNoSuchAuthPlugin); |
|
204 } |
|
205 Cleanup(); |
|
206 } |
|
207 |
|
208 TBool CTrainingMgr::IsBusy() const |
|
209 { |
|
210 return iState != EIdle; |
|
211 } |
|
212 |
|
213 CTransientKeyInfo* CTrainingMgr::CreateKeyInfoLC() |
|
214 { |
|
215 __ASSERT_ALWAYS(iResult != 0 && iResult->Size() > 0, |
|
216 User::Panic(KAuthServerShortName, |
|
217 EPanicInvalidDefaultData)); |
|
218 |
|
219 CTransientKeyInfo* keyInfo = CTransientKeyInfo::NewLC(iCurrentPlugin); |
|
220 |
|
221 CTransientKey* key = keyInfo->CreateTransientKeyL(*iResult); |
|
222 CleanupStack::PushL(key); |
|
223 |
|
224 CEncryptedProtectionKey* encKey = key->EncryptL(*iProtKey); |
|
225 CleanupStack::PushL(encKey); |
|
226 |
|
227 keyInfo->SetEncryptedProtectionKeyL(encKey); |
|
228 CleanupStack::Pop(encKey); |
|
229 CleanupStack::PopAndDestroy(key); |
|
230 return keyInfo; |
|
231 } |
|
232 |
|
233 TBool CTrainingMgr::AddTrainingResultL() |
|
234 { |
|
235 TBool result = EFalse; |
|
236 if (iStatus == KErrNone) |
|
237 { |
|
238 CTransientKeyInfo* keyInfo = CreateKeyInfoLC(); |
|
239 iAuthDb->SetTrainedPluginL(iIdentity, keyInfo->PluginId(), |
|
240 *keyInfo); |
|
241 CleanupStack::PopAndDestroy(keyInfo); |
|
242 result = ETrue; |
|
243 } |
|
244 return result; |
|
245 } |
|
246 |
|
247 TBool CTrainingMgr::AddIdentityL() |
|
248 { |
|
249 TBool result = EFalse; |
|
250 if (iStatus == KErrNone) |
|
251 { |
|
252 CTransientKeyInfo* keyInfo = CreateKeyInfoLC(); |
|
253 iAuthDb->AddIdentityWithTrainedPluginL(iIdentity, *iDescription, *keyInfo); |
|
254 CleanupStack::PopAndDestroy(keyInfo); |
|
255 result = ETrue; |
|
256 } |
|
257 return result; |
|
258 } |
|
259 |
|
260 void CTrainingMgr::DoRegistrationStepL() |
|
261 { |
|
262 |
|
263 |
|
264 if (iCurrentPluginIdx == iPluginMgr->ImplementationsL().Count()) |
|
265 { |
|
266 SetActive(); |
|
267 iState = ERegistrationDone; |
|
268 TRequestStatus* status = &iStatus; |
|
269 User::RequestComplete(status, KErrNone); |
|
270 return; |
|
271 } |
|
272 |
|
273 iCurrentPlugin = |
|
274 iPluginMgr->ImplementationsL()[iCurrentPluginIdx++] |
|
275 ->ImplementationUid().iUid; |
|
276 |
|
277 CAuthPluginInterface* plugin = 0; |
|
278 TRAPD(err, plugin = iPluginMgr->PluginL(iCurrentPlugin)); |
|
279 |
|
280 SetActive(); |
|
281 switch (err) |
|
282 { |
|
283 case KErrAuthServNoSuchPlugin: |
|
284 { |
|
285 // skip this plugin |
|
286 TRequestStatus* status = &iStatus; |
|
287 User::RequestComplete(status, KErrNone); |
|
288 return; |
|
289 } |
|
290 case KErrNone: |
|
291 break; |
|
292 default: |
|
293 User::Leave(err); |
|
294 break; |
|
295 } |
|
296 delete iResult; |
|
297 iResult = 0; |
|
298 if (plugin->IsActive()) |
|
299 { |
|
300 plugin->Train(iIdentity, iResult, iStatus); |
|
301 } |
|
302 else |
|
303 { |
|
304 TRequestStatus* status = &iStatus; |
|
305 User::RequestComplete(status, KErrAuthServPluginNotActive); |
|
306 } |
|
307 } |
|
308 |
|
309 void CTrainingMgr::WriteResultToMsgL() |
|
310 { |
|
311 |
|
312 if (iAuthDb->NumTrainedPluginsL(iIdentity) > 0) |
|
313 { |
|
314 CIdentity* identity = CIdentity::NewLC(iIdentity, iProtKey, |
|
315 iDescription); |
|
316 iProtKey = 0; |
|
317 iDescription = 0; |
|
318 |
|
319 HBufC8* idBuff = HBufC8::NewLC(KDefaultBufferSize); |
|
320 TPtr8 idPtr = idBuff->Des(); |
|
321 |
|
322 RDesWriteStream writeStream(idPtr); |
|
323 CleanupClosePushL(writeStream); |
|
324 |
|
325 writeStream << *identity; |
|
326 writeStream.CommitL(); |
|
327 |
|
328 TInt clientBuffSize = iMessage->GetDesMaxLength(0); |
|
329 |
|
330 if (clientBuffSize >= idBuff->Size()) |
|
331 { |
|
332 iMessage->Write(0, *idBuff); |
|
333 } |
|
334 else |
|
335 { |
|
336 User::Leave(KErrUnderflow); |
|
337 } |
|
338 CleanupStack::PopAndDestroy(3,identity); |
|
339 } |
|
340 else |
|
341 { |
|
342 |
|
343 iStatus = KErrAuthServRegistrationFailed; |
|
344 } |
|
345 |
|
346 } |
|
347 |
|
348 void CTrainingMgr::Complete() |
|
349 { |
|
350 iMessage->Complete(iStatus.Int()); |
|
351 Cleanup(); |
|
352 } |
|
353 |
|
354 void CTrainingMgr::Cleanup() |
|
355 { |
|
356 if(iDescription) |
|
357 { |
|
358 delete iDescription; |
|
359 iDescription = 0; |
|
360 } |
|
361 |
|
362 if(iResult) |
|
363 { |
|
364 delete iResult; |
|
365 iResult = 0; |
|
366 } |
|
367 |
|
368 if(iMessage) |
|
369 { |
|
370 delete iMessage; |
|
371 iMessage = 0; |
|
372 } |
|
373 |
|
374 if(iProtKey) |
|
375 { |
|
376 delete iProtKey; |
|
377 iProtKey = 0; |
|
378 } |
|
379 |
|
380 iState = EIdle; |
|
381 } |