|
1 /** |
|
2 * Copyright (c) 2010 Sasken Communication Technologies Ltd. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "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 * Lasse Laasonen, Sasken Communication Technologies Ltd - Initial contribution |
|
11 * |
|
12 * Description: |
|
13 * This source contains routines handling RSA signing using Unified Key Store |
|
14 */ |
|
15 |
|
16 #ifdef SYMBIAN_V3 |
|
17 #include <unifiedkeystore.h> |
|
18 #include <asymmetric.h> |
|
19 #include <mctkeystore.h> |
|
20 #endif |
|
21 |
|
22 #include <smfcredmgrclientdatastruct.h> |
|
23 #include <smfcredmgrcommon.h> |
|
24 #include <hash.h> |
|
25 #include "smfkeystoremanager.h" |
|
26 |
|
27 CSmfKeyStoreManager* CSmfKeyStoreManager::NewL() { |
|
28 CSmfKeyStoreManager* self = CSmfKeyStoreManager::NewLC(); |
|
29 CleanupStack::Pop( self ); |
|
30 return self; |
|
31 } |
|
32 |
|
33 CSmfKeyStoreManager* CSmfKeyStoreManager::NewLC() { |
|
34 CSmfKeyStoreManager* self = new( ELeave )CSmfKeyStoreManager(); |
|
35 CleanupStack::PushL( self ); |
|
36 self->ConstructL(); |
|
37 return self; |
|
38 } |
|
39 |
|
40 CSmfKeyStoreManager::~CSmfKeyStoreManager() { |
|
41 iFs.Close(); |
|
42 |
|
43 delete iSignParameters; |
|
44 iSignParameters = NULL; |
|
45 |
|
46 delete iRsaKeyParameters; |
|
47 iRsaKeyParameters = NULL; |
|
48 |
|
49 } |
|
50 |
|
51 void CSmfKeyStoreManager::HandleMessageL( const RMessage2& aMessage ) |
|
52 { |
|
53 RDebug::Printf("SMF: CSmfKeyStoreManager::HandleMessageL"); |
|
54 iMessages.AppendL( &aMessage ); |
|
55 if ( !IsActive() ) |
|
56 { |
|
57 ContinueMessageHandlingL(); |
|
58 } |
|
59 } |
|
60 |
|
61 void CSmfKeyStoreManager::RunL() |
|
62 { |
|
63 RDebug::Printf("SMF: CSmfKeyStoreManager::RunL, iStatus=%d", iStatus.Int() ); |
|
64 |
|
65 switch( iState ) |
|
66 { |
|
67 case EInitializingKeystore: |
|
68 { |
|
69 SetPassphraseTimeout(); |
|
70 break; |
|
71 } |
|
72 case ESettingPassphraseTimeout: |
|
73 { |
|
74 RDebug::Printf("SMF: Key store manager initialized"); |
|
75 iState = EInitialized; |
|
76 break; |
|
77 } |
|
78 #ifdef SYMBIAN_V3 |
|
79 case EGeneratingKey: |
|
80 { |
|
81 iState = EInitialized; |
|
82 RDebug::Printf("SMF: Completing generate key message"); |
|
83 iMessages[0]->Complete( iStatus.Int() ); |
|
84 iMessages.Remove(0); |
|
85 break; |
|
86 } |
|
87 case EImportingKey: |
|
88 { |
|
89 iState = EInitialized; |
|
90 TInt num = iStatus.Int(); |
|
91 if( iStatus.Int() == KErrNone ) |
|
92 { |
|
93 iMessages[0]->WriteL( 1, iImportedKey->ID() ); |
|
94 } |
|
95 |
|
96 RDebug::Printf("SMF: Completing import key message"); |
|
97 iMessages[0]->Complete( iStatus.Int() ); |
|
98 iMessages.Remove(0); |
|
99 break; |
|
100 } |
|
101 case EGettingKeyList: |
|
102 case EGettingRSASigner: |
|
103 { |
|
104 RSA_SHA1_SignMessageL(); |
|
105 return; |
|
106 } |
|
107 case ERSASigningMessage: |
|
108 { |
|
109 iState = EInitialized; |
|
110 |
|
111 if ( iStatus.Int() == KErrNone ) |
|
112 { |
|
113 HBufC8* signature = iRSASignature->S().BufferLC(); |
|
114 RDebug::Printf("SMF: Signed message=%S", signature); |
|
115 iMessages[0]->WriteL(1, *signature); |
|
116 CleanupStack::PopAndDestroy(); |
|
117 } |
|
118 iMessages[0]->Complete( iStatus.Int() ); |
|
119 iMessages.Remove(0); |
|
120 break; |
|
121 } |
|
122 case EListingKeys: |
|
123 { |
|
124 iState = EInitialized; |
|
125 RDebug::Printf("Key count: %d", iKeys.Count() ); |
|
126 for (TInt i = 0; i < iKeys.Count(); i++) |
|
127 { |
|
128 CCTKeyInfo* info = iKeys[i]; |
|
129 RDebug::Printf("key label: %S", &info->Label()); |
|
130 } |
|
131 iMessages[0]->Complete( iStatus.Int() ); |
|
132 iMessages.Remove(0); |
|
133 break; |
|
134 } |
|
135 case EKeyDeleted: |
|
136 { |
|
137 iState = EInitialized; |
|
138 iMessages[0]->Complete(iStatus.Int()); |
|
139 iMessages.Remove( 0 ); |
|
140 break; |
|
141 } |
|
142 case EDeletingKey: |
|
143 { |
|
144 DeleteKeys(); |
|
145 break; |
|
146 } |
|
147 #endif |
|
148 default: |
|
149 { |
|
150 break; |
|
151 } |
|
152 } |
|
153 |
|
154 if ( iMessages.Count() ) |
|
155 { |
|
156 ContinueMessageHandlingL(); |
|
157 } |
|
158 } |
|
159 |
|
160 void CSmfKeyStoreManager::DoCancel() { |
|
161 } |
|
162 |
|
163 TInt CSmfKeyStoreManager::RunError( TInt aError ) { |
|
164 RDebug::Printf("SMF: CSmfKeyStoreManager::RunError error=%d", aError); |
|
165 |
|
166 if ( iMessages.Count() ) |
|
167 { |
|
168 iMessages[0]->Complete( aError ); |
|
169 iMessages.Remove( 0 ); |
|
170 } |
|
171 |
|
172 return KErrNone; |
|
173 } |
|
174 |
|
175 CSmfKeyStoreManager::CSmfKeyStoreManager() |
|
176 :CActive(EPriorityStandard), iState(EInitializingKeystore) |
|
177 { |
|
178 } |
|
179 |
|
180 void CSmfKeyStoreManager::ConstructL() { |
|
181 RDebug::Printf("SMF: CSmfKeyStoreManager::ConstructL"); |
|
182 CActiveScheduler::Add( this ); |
|
183 #ifdef SYMBIAN_V3 |
|
184 User::LeaveIfError( iFs.Connect() ); |
|
185 |
|
186 iKeyStore = CUnifiedKeyStore::NewL(iFs); |
|
187 iKeyStore->Initialize( iStatus ); |
|
188 iState = EInitializingKeystore; |
|
189 SetActive(); |
|
190 #endif |
|
191 } |
|
192 |
|
193 void CSmfKeyStoreManager::ContinueMessageHandlingL() |
|
194 { |
|
195 RDebug::Printf("SMF: CSmfKeyStoreManager::ContinueMessageHandling"); |
|
196 |
|
197 if ( IsActive() ) |
|
198 { |
|
199 return; |
|
200 } |
|
201 |
|
202 RMessage2* message = iMessages[0]; |
|
203 TInt function = message->Function(); |
|
204 |
|
205 switch( function ) |
|
206 { |
|
207 case ESmfStoreRSAKey: |
|
208 { |
|
209 StoreRSAKeyL(); |
|
210 break; |
|
211 } |
|
212 case ESmfRSASignMessage: |
|
213 { |
|
214 RSA_SHA1_SignMessageL(); |
|
215 break; |
|
216 } |
|
217 case ESmfHMACSHA1SignMessage: |
|
218 { |
|
219 HMAC_SHA1_SignMessageL(); |
|
220 break; |
|
221 } |
|
222 case ESmfDeleteKeys: |
|
223 { |
|
224 DeleteKeys(); |
|
225 break; |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 void CSmfKeyStoreManager::StoreRSAKeyL() |
|
231 { |
|
232 #ifdef SYMBIAN_V3 |
|
233 RDebug::Printf("SMF: CSmfKeyStoreManager::StoreRSAKeyL"); |
|
234 |
|
235 ReadRsaKeyParametersL(); |
|
236 |
|
237 RDebug::Printf("SMF: Parameters read"); |
|
238 |
|
239 iKeyStore->ImportKey( |
|
240 0, iRsaKeyParameters->KeyData(), EPKCS15UsageSign, iRsaKeyParameters->KeyName(), |
|
241 CCTKeyInfo::EExtractable, iRsaKeyParameters->StartDate(), iRsaKeyParameters->EndDate(), |
|
242 iImportedKey, iStatus ); |
|
243 SetActive(); |
|
244 iState = EImportingKey; |
|
245 #endif |
|
246 } |
|
247 |
|
248 void CSmfKeyStoreManager::RSA_SHA1_SignMessageL() |
|
249 { |
|
250 |
|
251 RDebug::Printf("SMF: CSmfKeyStoreManager::RSA_SHA1_SignMessage"); |
|
252 #ifdef SYMBIAN_V3 |
|
253 switch( iState ) |
|
254 { |
|
255 case EInitialized: |
|
256 { |
|
257 // Get the key |
|
258 ReadSignParametersL(); |
|
259 TCTKeyAttributeFilter filter; |
|
260 filter.iKeyAlgorithm = CCTKeyInfo::ERSA; |
|
261 |
|
262 iKeyStore->List( iKeys, filter, iStatus ); |
|
263 iState = EGettingKeyList; |
|
264 SetActive(); |
|
265 break; |
|
266 } |
|
267 case EGettingKeyList: |
|
268 { |
|
269 RDebug::Printf("SMF: key count=%d", iKeys.Count()); |
|
270 // Find the correct key |
|
271 bool keyFound = EFalse; |
|
272 for( TInt i = 0; i < iKeys.Count(); i++ ) |
|
273 { |
|
274 |
|
275 if ( iKeys[i]->ID() == iSignParameters->Key() ) |
|
276 { |
|
277 RDebug::Printf("SMF: Correct key found"); |
|
278 iKeyStore->Open( *iKeys[i], iRSASigner, iStatus ); |
|
279 iState = EGettingRSASigner; |
|
280 keyFound = ETrue; |
|
281 SetActive(); |
|
282 break; |
|
283 } |
|
284 } |
|
285 if ( !keyFound ) |
|
286 { |
|
287 iMessages[0]->Complete( KErrNotFound ); |
|
288 iMessages.Remove( 0 ); |
|
289 } |
|
290 break; |
|
291 } |
|
292 case EGettingRSASigner: |
|
293 { |
|
294 ASSERT( iRSASigner != NULL ); |
|
295 iRSASigner->SignMessage(iSignParameters->Message(), iRSASignature, iStatus); |
|
296 iState = ERSASigningMessage; |
|
297 SetActive(); |
|
298 break; |
|
299 } |
|
300 default: |
|
301 { |
|
302 break; |
|
303 } |
|
304 } |
|
305 #endif |
|
306 } |
|
307 |
|
308 void CSmfKeyStoreManager::HMAC_SHA1_SignMessageL() |
|
309 { |
|
310 ReadSignParametersL(); |
|
311 |
|
312 RDebug::Printf("SMF: CSmfKeyStoreManager::HMAC_SHA1_SignMessage"); |
|
313 |
|
314 CSHA1* sha = CSHA1::NewL(); |
|
315 CHMAC* hmac = CHMAC::NewL( iSignParameters->Key(), sha ); |
|
316 TPtrC8 hashedSig( hmac->Hash( iSignParameters->Message() ) ); |
|
317 |
|
318 RDebug::Printf("SMF: hashed message length=%d", hashedSig.Length()); |
|
319 RDebug::Printf("SMF: hashed message=%S", &hashedSig); |
|
320 |
|
321 iMessages[0]->WriteL( 1, hashedSig ); |
|
322 |
|
323 RDebug::Printf("SMF: result written"); |
|
324 |
|
325 iMessages[0]->Complete( KErrNone ); |
|
326 iMessages.Remove(0); |
|
327 |
|
328 RDebug::Printf("SMF: message completed"); |
|
329 |
|
330 delete iSignParameters; |
|
331 iSignParameters = NULL; |
|
332 } |
|
333 |
|
334 void CSmfKeyStoreManager::DeleteKeys() |
|
335 { |
|
336 RDebug::Printf("SMF: CSmfKeyStoreManager::DeleteKeys"); |
|
337 #ifdef SYMBIAN_V3 |
|
338 switch ( iState ) |
|
339 { |
|
340 case EInitialized: |
|
341 { |
|
342 iKeys.Reset(); |
|
343 TCTKeyAttributeFilter filter; |
|
344 filter.iKeyAlgorithm = CCTKeyInfo::ERSA; |
|
345 iKeyStore->List( iKeys, filter, iStatus ); |
|
346 iState = EDeletingKey; |
|
347 SetActive(); |
|
348 break; |
|
349 } |
|
350 case EDeletingKey: |
|
351 { |
|
352 RDebug::Printf("SMF: key count=%d", iKeys.Count()); |
|
353 //read the key-label to delete from message |
|
354 HBufC* KeyBuf = HBufC::NewL(KMaxSignedMsgLength); |
|
355 TPtr keyPtr(KeyBuf->Des()); |
|
356 iMessages[0]->Read(0, keyPtr); |
|
357 |
|
358 // Find the correct key |
|
359 bool keyFound = EFalse; |
|
360 for (TInt i = 0; i < iKeys.Count(); i++) |
|
361 { |
|
362 if (iKeys[i]->ID() == (keyPtr.Collapse())) |
|
363 { |
|
364 RDebug::Printf("SMF: Correct key found"); |
|
365 iKeyStore->DeleteKey(*iKeys[i], iStatus); |
|
366 keyFound = ETrue; |
|
367 iState = EKeyDeleted; |
|
368 SetActive(); |
|
369 } |
|
370 } |
|
371 if (!keyFound) |
|
372 { |
|
373 iMessages[0]->Complete(KErrNotFound); |
|
374 iMessages.Remove( 0 ); |
|
375 } |
|
376 |
|
377 |
|
378 break; |
|
379 } |
|
380 } |
|
381 #endif |
|
382 } |
|
383 |
|
384 void CSmfKeyStoreManager::SetPassphraseTimeout() |
|
385 { |
|
386 #ifdef SYMBIAN_V3 |
|
387 iKeyStore->SetPassphraseTimeout( -1, iStatus ); |
|
388 iState = ESettingPassphraseTimeout; |
|
389 SetActive(); |
|
390 #endif |
|
391 } |
|
392 |
|
393 void CSmfKeyStoreManager::ReadSignParametersL() |
|
394 { |
|
395 RMessage2* message = iMessages[0]; |
|
396 RBuf8 dataBuf; |
|
397 CleanupClosePushL( dataBuf ); |
|
398 dataBuf.CreateL( message->GetDesLength( 0 ) ); |
|
399 message->ReadL( 0, dataBuf, 0 ); |
|
400 |
|
401 delete iSignParameters; |
|
402 iSignParameters = NULL; |
|
403 |
|
404 iSignParameters = CSmfSignParameters::NewL( dataBuf ); |
|
405 |
|
406 CleanupStack::PopAndDestroy( &dataBuf ); |
|
407 } |
|
408 |
|
409 void CSmfKeyStoreManager::ReadRsaKeyParametersL() |
|
410 { |
|
411 RMessage2* message = iMessages[0]; |
|
412 |
|
413 RBuf8 dataBuf; |
|
414 CleanupClosePushL( dataBuf ); |
|
415 dataBuf.CreateL( message->GetDesLength( 0 ) ); |
|
416 message->ReadL( 0, dataBuf, 0 ); |
|
417 |
|
418 delete iRsaKeyParameters; |
|
419 iRsaKeyParameters = NULL; |
|
420 |
|
421 iRsaKeyParameters = CSmfRsaKeyParameters::NewL( dataBuf ); |
|
422 |
|
423 CleanupStack::PopAndDestroy( &dataBuf ); |
|
424 } |
|
425 |
|
426 |
|
427 |