|
1 /* |
|
2 * Copyright (c) 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 "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: An interface to the Symbian Unified Key Store |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef __XMLSECCERTMAN_SYMBIANKEYSTORE_H_ |
|
19 #define __XMLSECCERTMAN_SYMBIANKEYSTORE_H__ |
|
20 |
|
21 #include <e32base.h> |
|
22 #include <unifiedkeystore.h> |
|
23 #include <e32std.h> |
|
24 #include <signed.h> |
|
25 |
|
26 class CSymbianKeyStore : public CActive |
|
27 { |
|
28 public: |
|
29 |
|
30 /** |
|
31 * Create new object. |
|
32 * |
|
33 * @since S60 v3.2 |
|
34 * @return pointer to new object |
|
35 */ |
|
36 IMPORT_C static CSymbianKeyStore* NewL(); |
|
37 |
|
38 /** |
|
39 * Create new object and put it on clean stack. |
|
40 * |
|
41 * @since S60 v3.2 |
|
42 * @return pointer to new object |
|
43 */ |
|
44 IMPORT_C static CSymbianKeyStore* NewLC(); |
|
45 |
|
46 /** |
|
47 * Destructor. |
|
48 */ |
|
49 virtual ~CSymbianKeyStore(); |
|
50 |
|
51 /** |
|
52 * Create Unified Key Store |
|
53 * |
|
54 * @since S60 v3.2 |
|
55 */ |
|
56 void CreateUnifiedKeyStoreL(); |
|
57 |
|
58 /** |
|
59 * Find a key in the Unified Key Store |
|
60 * |
|
61 * @since S60 v3.2 |
|
62 * @param aLabel The name of the key to be found |
|
63 * @param aAlgo The algorithm of the key to be found |
|
64 */ |
|
65 IMPORT_C void FindKey(const TDesC8 &aLabel, CKeyInfoBase::EKeyAlgorithm aAlgo); |
|
66 |
|
67 /** |
|
68 * Check if a key is found in the Unified Key Store |
|
69 * |
|
70 * @since S60 v3.2 |
|
71 * @return KErrNone if the key is found |
|
72 * @return KErrNotFound if the key is not found |
|
73 */ |
|
74 IMPORT_C TInt hasKey(); |
|
75 |
|
76 /** |
|
77 * Get the size of the key stored |
|
78 * |
|
79 * @since S60 v3.2 |
|
80 * @return TUint Size of the key |
|
81 */ |
|
82 IMPORT_C TUint GetKeySize(); |
|
83 |
|
84 /** |
|
85 * Get the algorithm of the key stored |
|
86 * |
|
87 * @since S60 v3.2 |
|
88 * @return CCTKeyInfo::EKeyAlgorithm |
|
89 */ |
|
90 IMPORT_C CCTKeyInfo::EKeyAlgorithm GetKeyAlgorithm(); |
|
91 |
|
92 /** |
|
93 * Create RSA Key |
|
94 * |
|
95 * @since S60 v3.2 |
|
96 * @param aSize Bit size of the key |
|
97 * @param aKeyName Name of the key |
|
98 */ |
|
99 IMPORT_C void CreateRSAKey(TUint aSize, const TDesC8 &aKeyName); |
|
100 |
|
101 /** |
|
102 * Import key |
|
103 * |
|
104 * @since S60 v3.2 |
|
105 * @param aKeyData The key data to import, ASN.1 DER encoded PKCS#8 |
|
106 * @aKeyName Name of the key |
|
107 */ |
|
108 IMPORT_C void ImportKey(const TDesC8 &aKeyData, const TDesC8 &aKeyName); |
|
109 |
|
110 /** |
|
111 * Opens RSA key for signing the data |
|
112 * |
|
113 * @since S60 v3.2 |
|
114 * @param aDataToSign The data to be signed |
|
115 * @param aLen Length of the data |
|
116 */ |
|
117 IMPORT_C void RSASignL(const TUint8* aDataToSign, TUint aLen); |
|
118 |
|
119 /** |
|
120 * Verify a RSA signed data |
|
121 * |
|
122 * @since S60 v3.2 |
|
123 * @param aDataToVerify The signed data to be verified |
|
124 * @param aDataLen Length of the signed data |
|
125 * @param aSig A reference to the signature that signed the data |
|
126 * @param aSigLen Length of the signature |
|
127 */ |
|
128 IMPORT_C void RSAVerifyL(const TUint8* aDataToVerify, |
|
129 TUint aDataLen, |
|
130 const TUint8* aSig, |
|
131 TUint aSigLen); |
|
132 |
|
133 /** |
|
134 * Verify an RSA signed data with a public key passed from a certificate |
|
135 * |
|
136 * @since S60 v3.2 |
|
137 * @param aDataToVerify The signed data to be verified |
|
138 * @param aDataLen Length of the signed data |
|
139 * @param aSig A reference to the signature that signed the data |
|
140 * @param aSigLen Length of the signature |
|
141 * @param aSubPubKeyInfo A handle to the public key passed from a certificate |
|
142 * @return ETrue The verification is succeeded |
|
143 * @return EFalse The verification is failed |
|
144 */ |
|
145 IMPORT_C TBool RSAVerifyWithPublicKeyL(const TUint8* aDataToVerify, |
|
146 TUint aDataLen, |
|
147 const TUint8* aSig, |
|
148 TUint aSigLen, |
|
149 CSubjectPublicKeyInfo *aSubPubKeyInfo); |
|
150 |
|
151 /** |
|
152 * Gets Signed data |
|
153 * |
|
154 * @since S60 v3.2 |
|
155 * @param aLen sets length of signed data |
|
156 */ |
|
157 IMPORT_C const TUint8* GetSignedData(TUint* aLen); |
|
158 |
|
159 /** |
|
160 * Gets verification result |
|
161 * |
|
162 * @since S60 v3.2 |
|
163 * @return ETrue The verification was succeeded |
|
164 * @return EFalse The cerification was failed |
|
165 */ |
|
166 IMPORT_C TBool GetVerifyResult(); |
|
167 |
|
168 /** |
|
169 * Get the error flag |
|
170 * |
|
171 * @since S60 v3.2 |
|
172 * @return error code |
|
173 */ |
|
174 IMPORT_C TInt CSymbianKeyStore::GetError(); |
|
175 |
|
176 /** |
|
177 * Perform RSA sign operation |
|
178 * |
|
179 * @since S60 v3.2 |
|
180 */ |
|
181 void PerformRSASignOperation(); |
|
182 |
|
183 /** |
|
184 * Perform RSA verify operation |
|
185 * |
|
186 * @since S60 v3.2 |
|
187 */ |
|
188 void PerformRSAVerifyOperationL(); |
|
189 |
|
190 /** |
|
191 * Export public key if none present |
|
192 * |
|
193 * @since S60 v3.2 |
|
194 */ |
|
195 void ExportRSAPublicKeyL(); |
|
196 |
|
197 protected: |
|
198 /** |
|
199 * From CActive Callback function, invoked to handle responses from the server |
|
200 */ |
|
201 void RunL(); |
|
202 |
|
203 /** |
|
204 * This function is called as part of the active object's Cancel(). |
|
205 */ |
|
206 void DoCancel(); |
|
207 |
|
208 /** |
|
209 * Handles Leaves from RunL function. |
|
210 */ |
|
211 TInt RunError(TInt aError); |
|
212 |
|
213 private: |
|
214 /** |
|
215 * C++ default constructor. |
|
216 */ |
|
217 CSymbianKeyStore(); |
|
218 |
|
219 /** |
|
220 * By default Symbian 2nd phase constructor is private. |
|
221 */ |
|
222 void ConstructL(); |
|
223 |
|
224 /** |
|
225 * To find matched key in the phase EFindingKeys |
|
226 */ |
|
227 void FindMatchedKey(); |
|
228 |
|
229 /** |
|
230 * Sets iSignature buffer |
|
231 */ |
|
232 void GetRSASignatureL(); |
|
233 |
|
234 /** |
|
235 * Release all resources kept in iKeys array (exept for iKey |
|
236 * which is released separetly) and empty iKeys array |
|
237 */ |
|
238 void ResetAndDestroyKeysArray(); |
|
239 |
|
240 private: |
|
241 /** |
|
242 * State of active object. |
|
243 * EUnitialized standard state |
|
244 * EInitializingKeystore during initialize keystore |
|
245 * EFindingKeys during finding keys |
|
246 * ECreateKey during creating key |
|
247 * EImportKey during importing key |
|
248 * EOpenRsaKeyForSigning during preparing key for signing |
|
249 * EPerformRSASignOperation during signing |
|
250 * EExportPublic during exporting public key |
|
251 */ |
|
252 enum TState |
|
253 { |
|
254 EUnitialized, |
|
255 EInitializingKeystore, |
|
256 EFindingKeys, |
|
257 ECreateKey, |
|
258 EImportKey, |
|
259 EOpenRSAKeyForSigning, |
|
260 EPerformRSASignOperation, |
|
261 EExportPublic |
|
262 }; |
|
263 |
|
264 private: |
|
265 /** |
|
266 * A reference to the CUnifiedKeyStore class |
|
267 */ |
|
268 CUnifiedKeyStore *iKeyStore; |
|
269 |
|
270 /** |
|
271 * Store the generated key |
|
272 */ |
|
273 CCTKeyInfo *iKey; |
|
274 |
|
275 /** |
|
276 * Store the RSA Signer |
|
277 */ |
|
278 MRSASigner *iRSASigner; |
|
279 |
|
280 /** |
|
281 * Store the data to be signed |
|
282 */ |
|
283 HBufC8* iDataToSign; |
|
284 |
|
285 /** |
|
286 * Store the data to be verified |
|
287 */ |
|
288 HBufC8* iDataToVerify; |
|
289 |
|
290 /** |
|
291 * Contain the result of the RSA Signer |
|
292 */ |
|
293 CRSASignature* iRSASignature; |
|
294 |
|
295 /** |
|
296 * Store the signed signature |
|
297 */ |
|
298 HBufC8* iSignature; |
|
299 |
|
300 /** |
|
301 * Contain the exported public key |
|
302 */ |
|
303 HBufC8* iPublicKeyData; |
|
304 |
|
305 /** |
|
306 * An internal state |
|
307 */ |
|
308 TState iState; |
|
309 |
|
310 /** |
|
311 * To store the key label to find |
|
312 */ |
|
313 HBufC *iKeyLabelToFind; |
|
314 |
|
315 /** |
|
316 * Store the result of signature verification |
|
317 */ |
|
318 TBool iVerifyResult; |
|
319 |
|
320 /** |
|
321 * To check if it was out of memory during previous verify operation |
|
322 */ |
|
323 TBool iOutOfMemoryFlag; |
|
324 |
|
325 /** |
|
326 * Store the keys found |
|
327 */ |
|
328 RMPointerArray< CCTKeyInfo > iKeys; |
|
329 |
|
330 /** |
|
331 * A reference to the File Server Client |
|
332 */ |
|
333 RFs iFs; |
|
334 |
|
335 /** |
|
336 * Error flag |
|
337 */ |
|
338 TInt iError; |
|
339 }; |
|
340 |
|
341 #endif // __XMLSECCERTMAN_SYMBIANKEYSTORE_H__ |