|
1 /* |
|
2 * Copyright (c) 2002-2004 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: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <e32std.h> |
|
21 #include <f32file.h> |
|
22 #include <s32file.h> |
|
23 #include <x509cert.h> |
|
24 #include <x509keys.h> |
|
25 #include <asn1dec.h> |
|
26 #include <hash.h> |
|
27 |
|
28 #ifdef RD_MULTIPLE_DRIVE |
|
29 #include <DriveInfo.h> |
|
30 #endif |
|
31 |
|
32 #include "OmaCrypto.h" |
|
33 #include "RoapStorageClient.h" |
|
34 |
|
35 using namespace Roap; |
|
36 |
|
37 // EXTERNAL DATA STRUCTURES |
|
38 //extern ?external_data; |
|
39 |
|
40 // EXTERNAL FUNCTION PROTOTYPES |
|
41 //extern ?external_function( ?arg_type,?arg_type ); |
|
42 |
|
43 // CONSTANTS |
|
44 //const ?type ?constant_var = ?constant; |
|
45 |
|
46 // MACROS |
|
47 //#define ?macro ?macro_def |
|
48 |
|
49 // LOCAL CONSTANTS AND MACROS |
|
50 //const ?type ?constant_var = ?constant; |
|
51 //#define ?macro_name ?macro_def |
|
52 |
|
53 #ifdef RD_MULTIPLE_DRIVE |
|
54 _LIT(KCertChainDir, "%c:\\drm\\PKI\\"); |
|
55 _LIT(KSigningCertPrefix, "SigningCert"); |
|
56 _LIT(KDeviceCertFileName, "%c:\\drm\\PKI\\DeviceCert.der"); |
|
57 _LIT(KDevicePrivateKeyFileName, "%c:\\drm\\PKI\\DevicePrivateKey.der"); |
|
58 #else |
|
59 _LIT(KCertChainDir, "c:\\drm\\PKI\\"); |
|
60 _LIT(KSigningCertPrefix, "SigningCert"); |
|
61 _LIT(KDeviceCertFileName, "c:\\drm\\PKI\\DeviceCert.der"); |
|
62 _LIT(KDevicePrivateKeyFileName, "c:\\drm\\PKI\\DevicePrivateKey.der"); |
|
63 #endif |
|
64 |
|
65 |
|
66 // MODULE DATA STRUCTURES |
|
67 //enum ?declaration |
|
68 //typedef ?declaration |
|
69 |
|
70 // LOCAL FUNCTION PROTOTYPES |
|
71 LOCAL_C void ReadFileL( |
|
72 HBufC8*& aContent, |
|
73 const TDesC& aName); |
|
74 |
|
75 // FORWARD DECLARATIONS |
|
76 //class ?FORWARD_CLASSNAME; |
|
77 |
|
78 // ============================= LOCAL FUNCTIONS =============================== |
|
79 |
|
80 // ----------------------------------------------------------------------------- |
|
81 // ?function_name ?description. |
|
82 // ?description |
|
83 // Returns: ?value_1: ?description |
|
84 // ?value_n: ?description_line1 |
|
85 // ?description_line2 |
|
86 // ----------------------------------------------------------------------------- |
|
87 // |
|
88 LOCAL_C void ReadFileL( |
|
89 HBufC8*& aContent, |
|
90 const TDesC& aName) |
|
91 { |
|
92 TInt size = 0; |
|
93 RFile file; |
|
94 RFs fs; |
|
95 |
|
96 User::LeaveIfError(fs.Connect()); |
|
97 CleanupClosePushL(fs); |
|
98 User::LeaveIfError(file.Open(fs, aName, EFileRead)); |
|
99 CleanupClosePushL(file); |
|
100 User::LeaveIfError(file.Size(size)); |
|
101 aContent = HBufC8::NewLC(size); |
|
102 TPtr8 ptr(aContent->Des()); |
|
103 User::LeaveIfError(file.Read(ptr, size)); |
|
104 CleanupStack::Pop(); //aContent |
|
105 CleanupStack::PopAndDestroy(2); // file, fs |
|
106 } |
|
107 |
|
108 // ============================ MEMBER FUNCTIONS =============================== |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // RoapStorageClient::RoapStorageClient |
|
112 // C++ default constructor can NOT contain any code, that |
|
113 // might leave. |
|
114 // ----------------------------------------------------------------------------- |
|
115 // |
|
116 RRoapStorageClient::RRoapStorageClient() |
|
117 { |
|
118 } |
|
119 |
|
120 // Destructor |
|
121 RRoapStorageClient::~RRoapStorageClient() |
|
122 { |
|
123 |
|
124 } |
|
125 |
|
126 // ----------------------------------------------------------------------------- |
|
127 // RoapStorageClient::?member_function |
|
128 // ?implementation_description |
|
129 // (other items were commented in a header). |
|
130 // ----------------------------------------------------------------------------- |
|
131 // |
|
132 TInt RRoapStorageClient::Connect(void) |
|
133 { |
|
134 return KErrNone; |
|
135 } |
|
136 |
|
137 TInt RRoapStorageClient::GetDeviceCertificateL( |
|
138 HBufC8*& aCert) |
|
139 { |
|
140 |
|
141 #ifndef RD_MULTIPLE_DRIVE |
|
142 |
|
143 ReadFileL(aCert, KDeviceCertFileName); |
|
144 |
|
145 #else //RD_MULTIPLE_DRIVE |
|
146 |
|
147 RFs fs; |
|
148 TInt driveNumber( -1 ); |
|
149 TChar driveLetter; |
|
150 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
151 |
|
152 User::LeaveIfError( fs.Connect() ); |
|
153 fs.DriveToChar( driveNumber, driveLetter ); |
|
154 fs.Close(); |
|
155 |
|
156 TFileName deviceCertFileName; |
|
157 deviceCertFileName.Format( KDeviceCertFileName, (TUint)driveLetter ); |
|
158 |
|
159 ReadFileL(aCert, deviceCertFileName); |
|
160 |
|
161 #endif |
|
162 |
|
163 return KErrNone; |
|
164 } |
|
165 |
|
166 TInt RRoapStorageClient::GetDevicePublicKeyL( |
|
167 CRSAPublicKey*& aPublicKey) |
|
168 { |
|
169 HBufC8* certData; |
|
170 CX509Certificate* cert; |
|
171 TX509KeyFactory factory; |
|
172 |
|
173 #ifndef RD_MULTIPLE_DRIVE |
|
174 |
|
175 ReadFileL(certData, KDeviceCertFileName); |
|
176 |
|
177 #else //RD_MULTIPLE_DRIVE |
|
178 |
|
179 RFs fs; |
|
180 TInt driveNumber( -1 ); |
|
181 TChar driveLetter; |
|
182 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
183 |
|
184 User::LeaveIfError( fs.Connect() ); |
|
185 fs.DriveToChar( driveNumber, driveLetter ); |
|
186 fs.Close(); |
|
187 |
|
188 TFileName deviceCertFileName; |
|
189 deviceCertFileName.Format( KDeviceCertFileName, (TUint)driveLetter ); |
|
190 |
|
191 ReadFileL(certData, deviceCertFileName); |
|
192 |
|
193 #endif |
|
194 |
|
195 CleanupStack::PushL(certData); |
|
196 cert = CX509Certificate::NewLC(*certData); |
|
197 aPublicKey = factory.RSAPublicKeyL(cert->PublicKey().KeyData()); |
|
198 CleanupStack::PopAndDestroy(2); // cert, certData |
|
199 return KErrNone; |
|
200 } |
|
201 |
|
202 TInt RRoapStorageClient::GetDevicePublicKeyDerL( |
|
203 HBufC8*& aPublicKey) |
|
204 { |
|
205 HBufC8* certData; |
|
206 CX509Certificate* cert; |
|
207 |
|
208 #ifndef RD_MULTIPLE_DRIVE |
|
209 |
|
210 ReadFileL(certData, KDeviceCertFileName); |
|
211 |
|
212 #else //RD_MULTIPLE_DRIVE |
|
213 |
|
214 RFs fs; |
|
215 TInt driveNumber( -1 ); |
|
216 TChar driveLetter; |
|
217 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
218 |
|
219 User::LeaveIfError( fs.Connect() ); |
|
220 fs.DriveToChar( driveNumber, driveLetter ); |
|
221 fs.Close(); |
|
222 |
|
223 TFileName deviceCertFileName; |
|
224 deviceCertFileName.Format( KDeviceCertFileName, (TUint)driveLetter ); |
|
225 |
|
226 ReadFileL(certData, deviceCertFileName); |
|
227 |
|
228 #endif |
|
229 |
|
230 CleanupStack::PushL(certData); |
|
231 cert = CX509Certificate::NewLC(*certData); |
|
232 aPublicKey = cert->PublicKey().KeyData().AllocL(); |
|
233 CleanupStack::PopAndDestroy(2); // cert, certData |
|
234 return KErrNone; |
|
235 } |
|
236 |
|
237 TInt RRoapStorageClient::GetDevicePublicKeyHashL( |
|
238 TDes8& aHash) |
|
239 { |
|
240 CSHA1* hash = NULL; |
|
241 HBufC8* key = NULL; |
|
242 |
|
243 GetDevicePublicKeyDerL(key); |
|
244 CleanupStack::PushL(key); |
|
245 hash = CSHA1::NewL(); |
|
246 CleanupStack::PushL(hash); |
|
247 hash->Hash(*key); |
|
248 aHash.Copy(hash->Final()); |
|
249 CleanupStack::PopAndDestroy(2); // hash, key |
|
250 return KErrNone; |
|
251 } |
|
252 |
|
253 TInt RRoapStorageClient::GetDevicePrivateKeyL( |
|
254 CRSAPrivateKey*& aKey) |
|
255 { |
|
256 HBufC8* raw = NULL; |
|
257 TASN1DecInteger encInt; |
|
258 TInt pos = 0; |
|
259 |
|
260 #ifndef RD_MULTIPLE_DRIVE |
|
261 |
|
262 ReadFileL(raw, KDevicePrivateKeyFileName); |
|
263 |
|
264 #else //RD_MULTIPLE_DRIVE |
|
265 |
|
266 RFs fs; |
|
267 TInt driveNumber( -1 ); |
|
268 TChar driveLetter; |
|
269 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
270 |
|
271 User::LeaveIfError( fs.Connect() ); |
|
272 fs.DriveToChar( driveNumber, driveLetter ); |
|
273 fs.Close(); |
|
274 |
|
275 TFileName devicePrivateKeyFileName; |
|
276 devicePrivateKeyFileName.Format( |
|
277 KDevicePrivateKeyFileName, (TUint)driveLetter ); |
|
278 |
|
279 ReadFileL(raw, devicePrivateKeyFileName); |
|
280 |
|
281 #endif |
|
282 |
|
283 CleanupStack::PushL(raw); |
|
284 |
|
285 TASN1DecGeneric gen(*raw); |
|
286 gen.InitL(); |
|
287 pos += gen.LengthDERHeader(); |
|
288 if (gen.Tag() != EASN1Sequence) |
|
289 { |
|
290 User::Leave(KErrArgument); |
|
291 } |
|
292 |
|
293 encInt.DecodeDERShortL(*raw, pos); // version |
|
294 RInteger modulus = encInt.DecodeDERLongL(*raw, pos); |
|
295 CleanupStack::PushL(modulus); |
|
296 RInteger publicExponent = encInt.DecodeDERLongL(*raw, pos); |
|
297 CleanupStack::PushL(publicExponent); |
|
298 RInteger privateExponent = encInt.DecodeDERLongL(*raw, pos); |
|
299 CleanupStack::PushL(privateExponent); |
|
300 |
|
301 aKey = CRSAPrivateKeyStandard::NewL(modulus, privateExponent); |
|
302 |
|
303 CleanupStack::Pop(); // privateExponent |
|
304 CleanupStack::PopAndDestroy();// publicExponent |
|
305 CleanupStack::Pop(); // modulus |
|
306 CleanupStack::PopAndDestroy(); // raw |
|
307 |
|
308 return KErrNone; |
|
309 } |
|
310 |
|
311 TInt RRoapStorageClient::GetDeviceCertificateChainL( |
|
312 RPointerArray<HBufC8>& aCertChain) |
|
313 { |
|
314 RFs fs; |
|
315 RFile file; |
|
316 TFileName fileName; |
|
317 CDir* dir; |
|
318 HBufC8* cert; |
|
319 TInt size; |
|
320 TPtr8 ptr(0, 0); |
|
321 TInt itemsToPop = 0; |
|
322 TInt i; |
|
323 TEntry entry; |
|
324 |
|
325 CleanupClosePushL(aCertChain); |
|
326 User::LeaveIfError(fs.Connect()); |
|
327 CleanupClosePushL(fs); |
|
328 |
|
329 #ifndef RD_MULTIPLE_DRIVE |
|
330 |
|
331 User::LeaveIfError(fs.GetDir(KCertChainDir, KEntryAttMatchMask, |
|
332 ESortByName, dir)); |
|
333 User::LeaveIfError(fs.SetSessionPath(KCertChainDir)); |
|
334 |
|
335 #else //RD_MULTIPLE_DRIVE |
|
336 |
|
337 TInt driveNumber( -1 ); |
|
338 TChar driveLetter; |
|
339 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
340 fs.DriveToChar( driveNumber, driveLetter ); |
|
341 |
|
342 TFileName certChainDir; |
|
343 certChainDir.Format( KCertChainDir, (TUint)driveLetter ); |
|
344 |
|
345 User::LeaveIfError(fs.GetDir(certChainDir, KEntryAttMatchMask, |
|
346 ESortByName, dir)); |
|
347 User::LeaveIfError(fs.SetSessionPath(certChainDir)); |
|
348 |
|
349 #endif |
|
350 |
|
351 CleanupStack::PushL(dir); |
|
352 GetDeviceCertificateL(cert); |
|
353 CleanupStack::PushL(cert); |
|
354 User::LeaveIfError(aCertChain.Append(cert)); |
|
355 itemsToPop++; |
|
356 for (i = 0; i < dir->Count(); i++) |
|
357 { |
|
358 entry = (*dir)[i]; |
|
359 if (entry.iName.Left(KSigningCertPrefix().Length()).CompareF( |
|
360 KSigningCertPrefix) == 0) |
|
361 { |
|
362 User::LeaveIfError(file.Open(fs, entry.iName, EFileRead)); |
|
363 CleanupClosePushL(file); |
|
364 User::LeaveIfError(file.Size(size)); |
|
365 cert = HBufC8::NewMax(size); |
|
366 ptr.Set(cert->Des()); |
|
367 CleanupStack::PushL(cert); |
|
368 User::LeaveIfError(aCertChain.Append(cert)); |
|
369 itemsToPop++; |
|
370 User::LeaveIfError(file.Read(ptr)); |
|
371 CleanupStack::Pop(); // cert |
|
372 CleanupStack::PopAndDestroy(); // file |
|
373 CleanupStack::PushL(cert); |
|
374 } |
|
375 } |
|
376 CleanupStack::Pop(itemsToPop); // cert |
|
377 CleanupStack::PopAndDestroy(2); // fs, dir |
|
378 CleanupStack::Pop(); // aCertChain |
|
379 return KErrNone; |
|
380 } |
|
381 |
|
382 TInt RRoapStorageClient::SignL( |
|
383 const TDesC8& aHash, |
|
384 HBufC8*& aSignature) |
|
385 { |
|
386 CRSAPrivateKey* privateKey = NULL; |
|
387 |
|
388 GetDevicePrivateKeyL(privateKey); |
|
389 CleanupStack::PushL(privateKey); |
|
390 aSignature = OmaCrypto::RsaPssSignHashL(privateKey, aHash); |
|
391 CleanupStack::PopAndDestroy(); // privateKey |
|
392 return KErrNone; |
|
393 } |
|
394 |
|
395 TBool RRoapStorageClient::VerifyL( |
|
396 const TDesC8& aSignature, |
|
397 const TDesC8& aHash) |
|
398 { |
|
399 CRSAPublicKey* publicKey = NULL; |
|
400 TBool r = ETrue; |
|
401 |
|
402 GetDevicePublicKeyL(publicKey); |
|
403 CleanupStack::PushL(publicKey); |
|
404 r = OmaCrypto::RsaPssVerifyHashL(publicKey, aSignature, aHash); |
|
405 CleanupStack::PopAndDestroy(); // privateKey |
|
406 return r; |
|
407 } |
|
408 |
|
409 // ----------------------------------------------------------------------------- |
|
410 // RoapStorageClient::GetRIContextL |
|
411 // ----------------------------------------------------------------------------- |
|
412 // |
|
413 CDRMRIContext* RRoapStorageClient::GetRIContextL( const TDesC8& aRiID ) |
|
414 { |
|
415 return NULL; |
|
416 } |
|
417 |
|
418 |
|
419 // ----------------------------------------------------------------------------- |
|
420 // RoapStorageClient::GetDomainContextL |
|
421 // ----------------------------------------------------------------------------- |
|
422 // |
|
423 CDRMDomainContext* RRoapStorageClient::GetDomainContextL( const TDesC8& aDomainID ) |
|
424 { |
|
425 return NULL; |
|
426 } |
|
427 |
|
428 // ----------------------------------------------------------------------------- |
|
429 // RRoapStorageClient::AddRIContextL |
|
430 // Add a new RI Context to the ROAP storage |
|
431 // ----------------------------------------------------------------------------- |
|
432 // |
|
433 void RRoapStorageClient::AddRIContextL( const CDRMRIContext& aRiContext ) |
|
434 { |
|
435 } |
|
436 |
|
437 // ----------------------------------------------------------------------------- |
|
438 // RRoapStorageClient::AddDomainContextL |
|
439 // Add a new RI Context to the ROAP storage |
|
440 // ----------------------------------------------------------------------------- |
|
441 // |
|
442 void RRoapStorageClient::AddDomainContextL( const CDRMDomainContext& aDomainContext, |
|
443 TKeyTransportScheme& aTransportScheme ) |
|
444 { |
|
445 } |
|
446 |
|
447 // ----------------------------------------------------------------------------- |
|
448 // RRoapStorageClient::DeleteRiContextL |
|
449 // ----------------------------------------------------------------------------- |
|
450 // |
|
451 void RRoapStorageClient::DeleteRiContextL( const TDesC8& aRiID ) |
|
452 { |
|
453 } |
|
454 |
|
455 // ----------------------------------------------------------------------------- |
|
456 // RRoapStorageClient::DeleteDomainContextL |
|
457 // ----------------------------------------------------------------------------- |
|
458 // |
|
459 void RRoapStorageClient::DeleteDomainContextL( const TDesC8& aDomainID ) |
|
460 { |
|
461 } |
|
462 |
|
463 // ----------------------------------------------------------------------------- |
|
464 // RRoapStorageClient::DeleteExpiredRIsL |
|
465 // ----------------------------------------------------------------------------- |
|
466 // |
|
467 void RRoapStorageClient::DeleteExpiredRIsL( const TTime& aTime ) |
|
468 { |
|
469 } |
|
470 |
|
471 |
|
472 // ----------------------------------------------------------------------------- |
|
473 // RRoapStorageClient::DeleteExpiredDomainsL |
|
474 // ----------------------------------------------------------------------------- |
|
475 // |
|
476 void RRoapStorageClient::DeleteExpiredDomainsL( const TTime& aTime ) |
|
477 { |
|
478 } |
|
479 |
|
480 // ----------------------------------------------------------------------------- |
|
481 // RRoapStorageClient::WhiteListURLExistsL |
|
482 // ----------------------------------------------------------------------------- |
|
483 // |
|
484 TBool RRoapStorageClient::WhiteListURLExistsL( const TDesC8& aURL ) |
|
485 { |
|
486 return EFalse; |
|
487 } |
|
488 |
|
489 // End of File |