|
1 /* |
|
2 * Copyright (c) 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <e32std.h> |
|
22 |
|
23 #ifdef RD_MULTIPLE_DRIVE |
|
24 #include <DriveInfo.h> |
|
25 #endif |
|
26 |
|
27 #include "RoapStorageServer.h" |
|
28 #include "DRMContextDB.h" |
|
29 #include "RoapLog.h" |
|
30 #include "drmroapclientserver.h" |
|
31 #include "drmcommonclientserver.h" |
|
32 #include "drmkeystorage.h" |
|
33 |
|
34 // EXTERNAL DATA STRUCTURES |
|
35 // EXTERNAL FUNCTION PROTOTYPES |
|
36 // CONSTANTS |
|
37 // MACROS |
|
38 |
|
39 |
|
40 // LOCAL CONSTANTS AND MACROS |
|
41 const TUint8 KMaxStartTries = 5; |
|
42 const TInt KWaitingTime = 1000000; // 10 secs |
|
43 |
|
44 _LIT( KRoapStorageThread, "RoapStorageServer" ); |
|
45 |
|
46 #ifdef RD_MULTIPLE_DRIVE |
|
47 _LIT( KRIContextFileName, "%c:\\private\\101F51F2\\ricontexts.dat" ); |
|
48 _LIT( KDomainContextFileName, "%c:\\private\\101F51F2\\domaincontexts.dat" ); |
|
49 // File names and paths for importing keys |
|
50 _LIT( KImportDir, "%c:\\private\\101F51F2\\import\\" ); |
|
51 _LIT( KInputFilePattern, "%c:\\private\\101F51F2\\import\\SigningCert*" ); |
|
52 _LIT( KDeviceKeyFileName, "%c:\\private\\101F51F2\\import\\DevicePrivateKey.der" ); |
|
53 _LIT( KDeviceCertFileName, "%c:\\private\\101F51F2\\import\\DeviceCert.der" ); |
|
54 #else |
|
55 // File names and paths for importing keys |
|
56 _LIT( KImportDir, "c:\\private\\101F51F2\\import\\" ); |
|
57 _LIT( KInputFilePattern, "c:\\private\\101F51F2\\import\\SigningCert*" ); |
|
58 _LIT( KDeviceKeyFileName, "c:\\private\\101F51F2\\import\\DevicePrivateKey.der" ); |
|
59 _LIT( KDeviceCertFileName, "c:\\private\\101F51F2\\import\\DeviceCert.der" ); |
|
60 #endif |
|
61 |
|
62 // MODULE DATA STRUCTURES |
|
63 using Roap::KServerMajorVersion; |
|
64 using Roap::KServerMinorVersion; |
|
65 using Roap::KServerBuildVersion; |
|
66 |
|
67 // LOCAL FUNCTION PROTOTYPES |
|
68 LOCAL_C TInt StartRoapServer( RSemaphore& aClientSem ); |
|
69 |
|
70 // FORWARD DECLARATIONS |
|
71 |
|
72 // ============================= LOCAL FUNCTIONS =============================== |
|
73 |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // Function StartRoapServer(). |
|
77 // This function starts the actual server under TRAP harness and starts |
|
78 // waiting for connections. This function returns only if there has been |
|
79 // errors during server startup or the server is stopped for some reason. |
|
80 // |
|
81 // Returns: TInt: Symbian OS error code. |
|
82 // ----------------------------------------------------------------------------- |
|
83 LOCAL_C TInt StartRoapServer( RSemaphore& aClientSem ) |
|
84 |
|
85 { |
|
86 TInt error = KErrNone; |
|
87 CRoapStorageServer* server = NULL; |
|
88 TUint8 count = 0; |
|
89 |
|
90 do |
|
91 { |
|
92 ++count; |
|
93 TRAP( error, ( server = CRoapStorageServer::NewL() ) ); |
|
94 if ( error ) |
|
95 { |
|
96 User::After( TTimeIntervalMicroSeconds32(KWaitingTime) ); |
|
97 } |
|
98 |
|
99 } while( error && ( count <= KMaxStartTries ) ); |
|
100 |
|
101 if( error ) |
|
102 { |
|
103 return error; |
|
104 } |
|
105 |
|
106 // Release the semaphore... |
|
107 aClientSem.Signal(); |
|
108 aClientSem.Close(); |
|
109 |
|
110 // Start waiting for connections |
|
111 CActiveScheduler::Start(); |
|
112 |
|
113 // Delete CRoapStorageServer |
|
114 delete server; |
|
115 |
|
116 return KErrNone; |
|
117 } |
|
118 |
|
119 // ----------------------------------------------------------------------------- |
|
120 // Function ReadFileL(). |
|
121 // Read a file into a buffer |
|
122 // ----------------------------------------------------------------------------- |
|
123 LOCAL_C void ReadFileL( RFs& aFs, HBufC8*& aContent, const TDesC& aName ) |
|
124 { |
|
125 TInt size = 0; |
|
126 RFile file; |
|
127 |
|
128 User::LeaveIfError( file.Open( aFs, aName, EFileRead ) ); |
|
129 CleanupClosePushL( file ); |
|
130 User::LeaveIfError( file.Size( size ) ); |
|
131 aContent = HBufC8::NewLC( size ); |
|
132 TPtr8 ptr( aContent->Des() ); |
|
133 User::LeaveIfError( file.Read( ptr, size) ); |
|
134 CleanupStack::Pop(); // aContent |
|
135 CleanupStack::PopAndDestroy(); // file |
|
136 } |
|
137 |
|
138 // ============================ MEMBER FUNCTIONS =============================== |
|
139 |
|
140 // ----------------------------------------------------------------------------- |
|
141 // CRoapStorageServer::NewLC |
|
142 // Two-phased constructor. |
|
143 // ----------------------------------------------------------------------------- |
|
144 // |
|
145 CRoapStorageServer* CRoapStorageServer::NewL() |
|
146 { |
|
147 CRoapStorageServer* self = new( ELeave ) CRoapStorageServer(); |
|
148 CleanupStack::PushL( self ); |
|
149 self->ConstructL(); |
|
150 CleanupStack::Pop( self ); |
|
151 return self; |
|
152 } |
|
153 |
|
154 // ----------------------------------------------------------------------------- |
|
155 // Destructor |
|
156 // ----------------------------------------------------------------------------- |
|
157 CRoapStorageServer::~CRoapStorageServer() |
|
158 { |
|
159 iRFs.Close(); |
|
160 delete iRoapStorage; |
|
161 } |
|
162 |
|
163 // ----------------------------------------------------------------------------- |
|
164 // CRoapStorageServer::RunErrorL |
|
165 // From CActive. Complete the request and restart the scheduler. |
|
166 // ----------------------------------------------------------------------------- |
|
167 // |
|
168 TInt CRoapStorageServer::RunError( TInt aError ) |
|
169 { |
|
170 // Inform the client. |
|
171 Message().Complete( aError ); |
|
172 // Restart the scheduler. |
|
173 ReStart(); |
|
174 // Error handled. |
|
175 return KErrNone; |
|
176 } |
|
177 |
|
178 // ----------------------------------------------------------------------------- |
|
179 // CRoapStorageServer::NewSessionL |
|
180 // Called when a client requires a new instance. |
|
181 // ----------------------------------------------------------------------------- |
|
182 CSession2* CRoapStorageServer::NewSessionL( |
|
183 const TVersion& aVersion, |
|
184 const RMessage2& /*aMessage*/) const |
|
185 { |
|
186 RThread client; |
|
187 // Check that the versions are compatible. |
|
188 if ( ! User::QueryVersionSupported( TVersion( KServerMajorVersion, |
|
189 KServerMinorVersion, |
|
190 KServerBuildVersion ), |
|
191 aVersion ) ) |
|
192 { |
|
193 // Sorry, no can do. |
|
194 User::Leave( KErrNotSupported ); |
|
195 } |
|
196 return CRoapStorageSession::NewL(); |
|
197 } |
|
198 |
|
199 // ----------------------------------------------------------------------------- |
|
200 // CRoapStorageServer::CRoapStorageServer |
|
201 // C++ default constructor can NOT contain any code, that |
|
202 // might leave. |
|
203 // ----------------------------------------------------------------------------- |
|
204 // |
|
205 CRoapStorageServer::CRoapStorageServer() : |
|
206 CServer2( EPriorityStandard ), |
|
207 iRoapStorage( NULL ) |
|
208 { |
|
209 // Nothing |
|
210 } |
|
211 |
|
212 // ----------------------------------------------------------------------------- |
|
213 // CRoapStorageServer::ConstructL |
|
214 // Symbian 2nd phase constructor can leave. |
|
215 // ----------------------------------------------------------------------------- |
|
216 // |
|
217 void CRoapStorageServer::ConstructL() |
|
218 { |
|
219 TInt err = KErrNone; |
|
220 |
|
221 User::RenameThread( KRoapStorageThread ); |
|
222 StartL( Roap::KServerName ); |
|
223 User::LeaveIfError(iRFs.Connect()); |
|
224 |
|
225 #ifndef RD_MULTIPLE_DRIVE |
|
226 |
|
227 iRoapStorage = CDRMContextDB::NewL( KRIContextFile(), |
|
228 KDomainContextFile(), |
|
229 iRFs ); |
|
230 |
|
231 #else //RD_MULTIPLE_DRIVE |
|
232 |
|
233 TInt driveNumber( -1 ); |
|
234 TChar driveLetter; |
|
235 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
236 iRFs.DriveToChar( driveNumber, driveLetter ); |
|
237 |
|
238 TFileName riContextFile; |
|
239 riContextFile.Format( KRIContextFileName, (TUint)driveLetter ); |
|
240 |
|
241 TFileName domainContextFile; |
|
242 domainContextFile.Format( KDomainContextFileName, (TUint)driveLetter ); |
|
243 |
|
244 iRoapStorage = CDRMContextDB::NewL( riContextFile, |
|
245 domainContextFile, |
|
246 iRFs ); |
|
247 |
|
248 #endif |
|
249 |
|
250 TRAP( err, ImportKeysL() ); |
|
251 } |
|
252 |
|
253 // ----------------------------------------------------------------------------- |
|
254 // CRoapStorageServer::ContextDB |
|
255 // Return the internal ROAP storage object |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 CDRMContextDB* CRoapStorageServer::ContextDB() |
|
259 { |
|
260 return iRoapStorage; |
|
261 } |
|
262 |
|
263 // ----------------------------------------------------------------------------- |
|
264 // CRoapStorageServer::ImportKeys |
|
265 // Import keys which are stored in the import directory into the private |
|
266 // directory. Only one key pair plus assocated certificates can be imported |
|
267 // at a time. The file names are DevicePrivateKey.der, DeviceCert.der and |
|
268 // SigningCertXX.der. |
|
269 // ----------------------------------------------------------------------------- |
|
270 // |
|
271 void CRoapStorageServer::ImportKeysL() |
|
272 { |
|
273 MDrmKeyStorage* storage = NULL; |
|
274 HBufC8* privateKey = NULL; |
|
275 HBufC8* cert = NULL; |
|
276 RArray<TPtrC8> certChain; |
|
277 RPointerArray<HBufC8> buffers; |
|
278 TFileName fileName; |
|
279 RFile file; |
|
280 TInt i; |
|
281 CDir* dir = NULL; |
|
282 TInt err = KErrNone; |
|
283 |
|
284 __UHEAP_MARK; |
|
285 LOG( _L( "CRoapStorageServer::ImportKeysL" ) ); |
|
286 CleanupClosePushL( buffers ); |
|
287 CleanupClosePushL( certChain ); |
|
288 |
|
289 #ifndef RD_MULTIPLE_DRIVE |
|
290 |
|
291 ReadFileL( iRFs, privateKey, KDeviceKeyFileName ); |
|
292 |
|
293 #else //RD_MULTIPLE_DRIVE |
|
294 |
|
295 TFileName tempPath; |
|
296 TFileName tempPath2; |
|
297 TInt driveNumber( -1 ); |
|
298 TChar driveLetter; |
|
299 DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber ); |
|
300 iRFs.DriveToChar( driveNumber, driveLetter ); |
|
301 |
|
302 tempPath.Format( KDeviceKeyFileName, (TUint)driveLetter ); |
|
303 |
|
304 ReadFileL( iRFs, privateKey, tempPath ); |
|
305 |
|
306 #endif |
|
307 |
|
308 CleanupStack::PushL( privateKey ); |
|
309 |
|
310 #ifndef RD_MULTIPLE_DRIVE |
|
311 |
|
312 ReadFileL( iRFs, cert, KDeviceCertFileName ); |
|
313 |
|
314 #else //RD_MULTIPLE_DRIVE |
|
315 |
|
316 tempPath2.Format( KDeviceCertFileName, (TUint)driveLetter ); |
|
317 |
|
318 ReadFileL( iRFs, cert, tempPath2 ); |
|
319 |
|
320 #endif |
|
321 |
|
322 CleanupStack::PushL( cert ); |
|
323 buffers.AppendL( cert ); |
|
324 |
|
325 #ifndef RD_MULTIPLE_DRIVE |
|
326 |
|
327 iRFs.Delete( KDeviceKeyFileName ); |
|
328 iRFs.Delete( KDeviceCertFileName ); |
|
329 |
|
330 #else //RD_MULTIPLE_DRIVE |
|
331 |
|
332 iRFs.Delete( tempPath ); |
|
333 iRFs.Delete( tempPath2 ); |
|
334 |
|
335 #endif |
|
336 |
|
337 #ifndef RD_MULTIPLE_DRIVE |
|
338 |
|
339 User::LeaveIfError( iRFs.GetDir( KInputFilePattern, KEntryAttNormal, |
|
340 ESortByName, dir ) ); |
|
341 |
|
342 #else //RD_MULTIPLE_DRIVE |
|
343 |
|
344 tempPath.Format( KInputFilePattern, (TUint)driveLetter ); |
|
345 |
|
346 User::LeaveIfError( iRFs.GetDir( tempPath, KEntryAttNormal, |
|
347 ESortByName, dir ) ); |
|
348 |
|
349 #endif |
|
350 |
|
351 CleanupStack::PushL( dir ); |
|
352 |
|
353 for ( i = 0; i < dir->Count(); i++ ) |
|
354 { |
|
355 |
|
356 #ifndef RD_MULTIPLE_DRIVE |
|
357 |
|
358 fileName.Copy( KImportDir ); |
|
359 |
|
360 #else //RD_MULTIPLE_DRIVE |
|
361 |
|
362 tempPath.Format( KImportDir, (TUint)driveLetter ); |
|
363 |
|
364 fileName.Copy( tempPath ); |
|
365 |
|
366 #endif |
|
367 |
|
368 fileName.Append( (*dir)[i].iName ); |
|
369 ReadFileL( iRFs, cert, fileName ); |
|
370 CleanupStack::PushL( cert ); |
|
371 buffers.AppendL( cert ); |
|
372 |
|
373 iRFs.Delete( fileName ); |
|
374 } |
|
375 for ( i = 0; i < buffers.Count(); i++ ) |
|
376 { |
|
377 certChain.AppendL( *( buffers[i] ) ); |
|
378 } |
|
379 storage = DrmKeyStorageNewL(); |
|
380 TRAP( err, storage->ImportDataL( *privateKey, certChain ) ); |
|
381 delete storage; |
|
382 CleanupStack::PopAndDestroy( i + 1 ); // certs & dir |
|
383 CleanupStack::PopAndDestroy( 3 ); // privateKey, certChain, buffers |
|
384 LOG( _L( "CRoapStorageServer::ImportKeysL done" ) ); |
|
385 __UHEAP_MARKEND; |
|
386 } |
|
387 |
|
388 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
389 // ----------------------------------------------------------------------------- |
|
390 // Function StartupRoapStorage(). |
|
391 // This function starts the actual Roap Storage |
|
392 // the cleanup stack and active scheduler. |
|
393 // Returns: TInt: Symbian OS error code. |
|
394 // ----------------------------------------------------------------------------- |
|
395 // |
|
396 |
|
397 TInt StartupRoapStorage( TAny* ) |
|
398 { |
|
399 TInt error = KErrNone; |
|
400 CTrapCleanup* trap = CTrapCleanup::New(); |
|
401 __ASSERT_ALWAYS( trap, User::Invariant() ); |
|
402 |
|
403 CActiveScheduler* scheduler = new CActiveScheduler(); |
|
404 __ASSERT_ALWAYS( scheduler, User::Invariant() ); |
|
405 |
|
406 CActiveScheduler::Install( scheduler ); |
|
407 RSemaphore clientSem; |
|
408 __ASSERT_ALWAYS( clientSem.OpenGlobal( KDRMEngCommonSemaphore ) == 0, User::Invariant() ); |
|
409 |
|
410 error = StartRoapServer( clientSem ); |
|
411 |
|
412 if ( error ) { |
|
413 // Server creation failed. Release the semaphore. |
|
414 // In case of successful startup, CRoapStorageServer |
|
415 // releases the semaphore. |
|
416 clientSem.Signal(); |
|
417 clientSem.Close(); |
|
418 } |
|
419 |
|
420 delete scheduler; |
|
421 scheduler = NULL; |
|
422 |
|
423 delete trap; |
|
424 trap = NULL; |
|
425 |
|
426 // __ASSERT_ALWAYS( !error, User::Invariant() ); |
|
427 |
|
428 return KErrNone; |
|
429 } |
|
430 |
|
431 // End of File |