os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/CKeyDataManager.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2004-2010 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 *
    16 */
    17 
    18 
    19 #include "CKeyDataManager.h"
    20 #include "fsdatatypes.h"
    21 #include "fstokencliserv.h"
    22 #include "fstokenutil.h"
    23 #include "keystorepassphrase.h"
    24 
    25 _LIT(KKeyStoreFilename,"keys.dat");
    26 
    27 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    28 #include <e32property.h>
    29 #include <authserver/aspubsubdefs.h>
    30 #else
    31 const TInt KDefaultPassphraseTimeout = 30;
    32 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    33 
    34 //	*********************************************************************
    35 //	Key store data manager - maintains array of objects representing keys
    36 //	*********************************************************************
    37 
    38 /*static*/ CFileKeyDataManager* CFileKeyDataManager::NewL()
    39 	{
    40 	CFileKeyDataManager* self = new (ELeave) CFileKeyDataManager();
    41 	CleanupStack::PushL(self);
    42 	self->ConstructL();
    43 	CleanupStack::Pop(self);
    44 	return self;
    45 	}
    46 
    47 CFileKeyDataManager::~CFileKeyDataManager()
    48 	{
    49 	if (iFileStore)
    50 		{
    51 		CompactStore();
    52 		delete iFileStore;
    53 		}
    54 
    55 	iFile.Close(); // May already have been closed by store
    56 	iFs.Close();
    57 		
    58 	iKeys.ResetAndDestroy();
    59 	iKeys.Close();
    60 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    61 	iIdentityId.Close();
    62 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    63 	}
    64 
    65 CFileKeyDataManager::CFileKeyDataManager() :
    66 	iRootStreamId(KNullStreamId),
    67 	iInfoStreamId(KNullStreamId)
    68 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    69 	,iPassStreamId(KNullStreamId),
    70 	iTimeoutStreamId(KNullStreamId)
    71 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    72 	{
    73 	}
    74 
    75 void CFileKeyDataManager::ConstructL()
    76 	{
    77 	
    78 	User::LeaveIfError(iFs.Connect());
    79 	OpenStoreL();
    80 
    81 	RStoreReadStream lookupStream;
    82 	lookupStream.OpenLC(*iFileStore, iInfoStreamId);
    83 
    84 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    85 	User::LeaveIfError(iIdentityId.Attach(	AuthServer::KAuthServerSecureId,
    86 											AuthServer::KUidAuthServerAuthChangeEvent));
    87 	#else
    88 	iPassStreamId = (TStreamId) lookupStream.ReadUint32L();
    89 	iTimeoutStreamId = (TStreamId) lookupStream.ReadUint32L();
    90 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    91 
    92 	TInt count = lookupStream.ReadInt32L();
    93 	for (TInt index = 0; index < count; index++)
    94 		{
    95 		CFileKeyData* keyData = CFileKeyData::NewL(lookupStream);
    96 		CleanupStack::PushL(keyData);
    97 
    98 		if (keyData->Handle() > iKeyIdentifier)
    99 			iKeyIdentifier = keyData->Handle();
   100 
   101 		iKeys.AppendL(keyData);
   102 		CleanupStack::Pop(keyData);
   103 		}
   104 	
   105 	CleanupStack::PopAndDestroy(&lookupStream);
   106 
   107 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   108 	ReadPassphraseTimeoutL();
   109 #endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   110 	
   111 	}
   112 
   113 CPassphraseManager* CFileKeyDataManager::CreatePassphraseManagerLC()
   114 	{
   115 	CPassphraseManager* result = CPassphraseManager::NewL(*iFileStore);
   116 	CleanupStack::PushL(result);
   117 	return result;
   118 	}
   119 
   120 void CFileKeyDataManager::OpenStoreL()
   121 	{
   122 	//	Tries to locate a key store file on the default drive and then from ROM
   123 	//	If it cannot find one, tries to create a file with permanent file store
   124 	//	inside it In all cases, should initialise iFileStore unless it cannot
   125 	//	create the file/store/streams
   126 	
   127 	__ASSERT_DEBUG(!iFileStore, PanicServer(EPanicStoreInitialised));
   128 
   129 	TFileName fullPath;
   130 	FileUtils::MakePrivateFilenameL(iFs, KKeyStoreFilename, fullPath);
   131 
   132 	FileUtils::EnsurePathL(iFs, fullPath);
   133 	TRAPD(result, OpenStoreInFileL(fullPath));
   134 
   135 	if (result == KErrInUse) 
   136 		{		
   137 		// Cannot access the file now. Abort server startup rather than wiping the keystore.
   138 		User::Leave(result); 
   139 		}
   140 
   141 	if (result != KErrNone)
   142 		{		
   143 		// Not yet opened a valid store, either no file to be found, or no valid
   144 		// store in it. Copy the original one stored in the ROM.
   145 		delete iFileStore;
   146 		iFileStore = NULL;
   147 		
   148 		TFileName romPath;
   149 		FileUtils::MakePrivateROMFilenameL(iFs, KKeyStoreFilename, romPath);
   150 
   151 		if (result != KErrNotFound)
   152 			{
   153 			// Wipe the keystore if we can't open it (it's corrupt anyway)
   154 			User::LeaveIfError(iFs.Delete(fullPath));
   155 			}
   156 
   157 		// Copy data from rom and open it	
   158 		TRAPD(err,
   159 			  FileUtils::CopyL(iFs, romPath, fullPath);
   160 			  OpenStoreInFileL(fullPath)
   161 			);
   162 		
   163 		if (KErrNone != err)
   164 			{
   165 			// We tried to copy the keystore from ROM. For some reason this
   166 			// failed and we still cannot open the file. Create a new one from
   167 			// scratch.
   168 			CreateStoreInFileL(fullPath);
   169 			}
   170 		}
   171 
   172 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   173 	__ASSERT_DEBUG((KNullStreamId!=iRootStreamId), PanicServer(EPanicRootStreamNotReady));
   174 	__ASSERT_DEBUG((KNullStreamId!=iInfoStreamId), PanicServer(EPanicManagerStreamNotReady));
   175 	}
   176 
   177 void CFileKeyDataManager::CreateStoreInFileL(const TDesC& aFile)
   178 	{
   179 	TInt r = iFs.MkDirAll(aFile);
   180 	if ( (r!=KErrNone) && (r!=KErrAlreadyExists) )
   181 		User::Leave(r);
   182 
   183 	iFileStore = CPermanentFileStore::ReplaceL(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive);
   184 	iFileStore->SetTypeL(KPermanentFileStoreLayoutUid);
   185 
   186 	TCleanupItem cleanupStore(RevertStore, iFileStore);
   187 	CleanupStack::PushL(cleanupStore);
   188 	
   189 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   190 	// Create timeout stream with default timeout
   191 	RStoreWriteStream timeoutStream;
   192 	iTimeoutStreamId = timeoutStream.CreateLC(*iFileStore);
   193 	timeoutStream.WriteUint32L(KDefaultPassphraseTimeout);
   194 	timeoutStream.CommitL();
   195 	CleanupStack::PopAndDestroy(&timeoutStream);
   196 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   197 	
   198 	// Create info stream - Currently no passphrase created, and no keys
   199 	RStoreWriteStream infoStream;
   200 	iInfoStreamId = infoStream.CreateLC(*iFileStore);
   201 	
   202 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   203 	infoStream.WriteUint32L(KNullStreamId.Value());
   204 	infoStream.WriteUint32L(iTimeoutStreamId.Value());
   205 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   206 	
   207 	infoStream.WriteUint32L(0); // Write key count of zero
   208 	infoStream.CommitL();
   209 	CleanupStack::PopAndDestroy(&infoStream);
   210 
   211 	// Create root stream - just contains id of info stream
   212 	RStoreWriteStream rootStream;
   213 	iRootStreamId = rootStream.CreateLC(*iFileStore);
   214 	iFileStore->SetRootL(iRootStreamId);
   215 
   216 	rootStream.WriteUint32L(iInfoStreamId.Value());		
   217 	rootStream.CommitL();
   218 	CleanupStack::PopAndDestroy(&rootStream);
   219 
   220 	WriteKeysToStoreL();
   221 	
   222 	iFileStore->CommitL();
   223 	CleanupStack::Pop(); // cleanupStore
   224 	}
   225 
   226 void CFileKeyDataManager::OpenStoreInFileL(const TDesC& aFile)
   227 	{
   228 	// Make sure the file isn't write protected
   229 	User::LeaveIfError(iFs.SetAtt(aFile, 0, KEntryAttReadOnly));
   230 	
   231 	User::LeaveIfError(iFile.Open(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive));
   232 	
   233 	iFileStore = CPermanentFileStore::FromL(iFile);		
   234 
   235 	// Get the salt, root and manager TStreamIds
   236 	iRootStreamId = iFileStore->Root();
   237 	if (iRootStreamId == KNullStreamId)
   238 		{
   239 		User::Leave(KErrCorrupt);
   240 		}
   241 	
   242 	RStoreReadStream rootStream;
   243 	rootStream.OpenLC(*iFileStore, iRootStreamId);
   244 	iInfoStreamId = (TStreamId)(rootStream.ReadUint32L());
   245 	CleanupStack::PopAndDestroy(&rootStream);
   246 	}
   247 
   248 // Methods dealing with atomic updates to key data file ////////////////////////
   249 
   250 // This is a cleanup item that reverts the store
   251 void CFileKeyDataManager::RevertStore(TAny* aStore)
   252 {
   253 	CPermanentFileStore* store = reinterpret_cast<CPermanentFileStore*>(aStore);
   254 	TRAP_IGNORE(store->RevertL());
   255 	// We're ignoring the leave code from this becuase there's no way we can
   256 	// handle this sensibly.  This shouldn't be a problem in practice - this
   257 	// will leave if for example the file store is on removable which is
   258 	// unexpectedly remove, and this is never the case for us.
   259 }
   260 
   261 // Rewrites the info stream (ie the array of key data info) to the store
   262 void CFileKeyDataManager::WriteKeysToStoreL()
   263 	{
   264 	RStoreWriteStream lookupStream;
   265 	lookupStream.ReplaceLC(*iFileStore, iInfoStreamId);
   266 
   267 	#ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   268 		lookupStream.WriteUint32L(iPassStreamId.Value());
   269 		lookupStream.WriteUint32L(iTimeoutStreamId.Value());
   270 	#endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   271 	
   272 	TInt keyCount = iKeys.Count();
   273 	lookupStream.WriteInt32L(keyCount);
   274 
   275 	for (TInt index = 0; index < keyCount; index++)
   276 		{
   277 		const CFileKeyData* key = iKeys[index];
   278 		key->ExternalizeL(lookupStream);
   279 		}
   280 
   281 	lookupStream.CommitL();
   282 	CleanupStack::PopAndDestroy(&lookupStream);
   283 	}
   284 
   285 /**
   286  * Add a key to the store.  Assumes that the key data streams (info, public key
   287  * and private key) have already been written.
   288  */
   289 void CFileKeyDataManager::AddL(const CFileKeyData* aKeyData)
   290 	{
   291 	ASSERT(aKeyData);
   292 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   293 		ASSERT(aKeyData->PassphraseStreamId() != KNullStreamId);
   294 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   295 
   296 	// Add the key to to the array, rewrite the infostream and 
   297 	// ONLY THEN commit the store
   298 	User::LeaveIfError(iKeys.Append(aKeyData));
   299 
   300 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   301 	TStreamId oldDefaultPassphraseId;
   302 
   303 	// Set the default passphrase id if this is the first key
   304 	oldDefaultPassphraseId = iPassStreamId;
   305 	if (iKeys.Count() == 1)
   306 		{
   307 		iPassStreamId = aKeyData->PassphraseStreamId();
   308 		}
   309 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   310 	
   311 	TRAPD(err,UpdateStoreL());
   312 	
   313 	if (err != KErrNone)
   314 		{
   315 		iKeys.Remove(iKeys.Count() - 1);
   316 		#ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   317 			iPassStreamId = oldDefaultPassphraseId;
   318 		#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   319 		User::Leave(err);
   320 		}
   321 	}
   322 
   323 void CFileKeyDataManager::UpdateStoreL()
   324 	{
   325 	WriteKeysToStoreL();
   326 
   327 	// Release ownership of key data and reset default passphrase id if store
   328 	// can't be written
   329 	TCleanupItem cleanupStore(RevertStore, iFileStore);
   330 	CleanupStack::PushL(cleanupStore);
   331 
   332 	iFileStore->CommitL();
   333 	
   334 	CleanupStack::Pop(); // cleanupStore
   335 	}
   336 
   337 /**
   338  * "Transaction safe" key removal - only removes the key in memory and file if
   339  * all operations are successful.
   340  */
   341 void CFileKeyDataManager::RemoveL(TInt aObjectId)
   342 	{
   343 	TInt index;
   344 	const CFileKeyData* key = NULL;
   345 	for (index = 0 ; index < iKeys.Count() ; ++index)
   346 		{
   347 		if (iKeys[index]->Handle() == aObjectId)
   348 			{
   349 			key = iKeys[index];
   350 			break;
   351 			}
   352 		}
   353 	
   354 	if (!key)
   355 		{
   356 		User::Leave(KErrNotFound);
   357 		}
   358 
   359 	TCleanupItem cleanupStore(RevertStore, iFileStore);
   360 	CleanupStack::PushL(cleanupStore);	
   361 
   362 	iFileStore->DeleteL(key->PrivateDataStreamId());
   363 	iFileStore->DeleteL(key->PublicDataStreamId());
   364 	iFileStore->DeleteL(key->InfoDataStreamId());
   365 
   366 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   367 	// Remove the passphrase if it's the last key
   368 	TStreamId oldPassphraseId = iPassStreamId;
   369 	if (Count() == 1)
   370 		{
   371 		iFileStore->DeleteL(iPassStreamId);
   372 		iPassStreamId = KNullStreamId;
   373 		}
   374 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   375 	
   376 	// Remove the key
   377 	iKeys.Remove(index);
   378 	
   379 	TRAPD(res, WriteKeysToStoreL());
   380 
   381 	if (res != KErrNone)
   382 		{
   383 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER		
   384 		iPassStreamId = oldPassphraseId;
   385 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   386 		User::LeaveIfError(iKeys.Append(key)); // Put it back, shouldn't leave
   387 		User::Leave(res);
   388 		}
   389 	else 
   390 		{
   391 		delete key;   // Cannot leave from the point it's removed to here, so no cleanup stack!
   392 		}		
   393 	iFileStore->CommitL();
   394 
   395 	CleanupStack::Pop(); // cleanupStore
   396 	CompactStore();
   397 }
   398 
   399 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   400 TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel, AuthServer::TIdentityId aIdentity) const
   401 	{//	Check each key in the store to determine if aKeyLabel already exists
   402 	TInt keyCount = iKeys.Count();
   403 	TBool isInStore = EFalse;
   404 	for (TInt index = 0; index < keyCount; ++index)
   405 		{
   406 		const TDesC& keyLabel = iKeys[index]->Label();
   407 		if (keyLabel.Compare(aKeyLabel)==0 && (iKeys[index]->Identity() == aIdentity))
   408 			{
   409 			isInStore = ETrue;
   410 			break;
   411 			}
   412 		}
   413 	return (isInStore);
   414 	}
   415 
   416 #else
   417 TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel) const
   418 {//	Check each key in the store to determine if aKeyLabel already exists
   419 	TInt keyCount = iKeys.Count();
   420 	TBool isInStore = EFalse;
   421 	for (TInt index = 0; index < keyCount; index++)
   422 	{
   423 		const TDesC& keyLabel = iKeys[index]->Label();
   424 		if (keyLabel.Compare(aKeyLabel)==0)
   425 		{
   426 			isInStore = ETrue;
   427 			break;
   428 		}
   429 	}
   430 
   431 	return (isInStore);
   432 }
   433 
   434 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   435 
   436 TInt CFileKeyDataManager::Count() const
   437 	{
   438 	return iKeys.Count();
   439 	}	
   440 
   441 const CFileKeyData* CFileKeyDataManager::operator[](TInt aIndex) const
   442 	{
   443 	return iKeys[aIndex];
   444 	}
   445 
   446 const CFileKeyData* CFileKeyDataManager::Lookup(TInt aObjectId) const
   447 	{
   448 	TInt count = Count();
   449 	for (TInt i = 0; i < count; ++i)
   450 		{
   451 		if ((*this)[i]->Handle() == aObjectId)
   452 			{
   453 			return (*this)[i];
   454 			}
   455 		}
   456 	return NULL;
   457 	}
   458 
   459 //	*********************************************************************
   460 //	Management of file and store therein
   461 //	*********************************************************************
   462 
   463 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   464 
   465 const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, AuthServer::TIdentityId aIdentityId)
   466 	{
   467 	TInt objectId = ++iKeyIdentifier;
   468 	TStreamId infoData = CreateWriteStreamL();
   469 	TStreamId publicKeyData = CreateWriteStreamL();
   470 	TStreamId privateKeyData = CreateWriteStreamL();
   471 	return CFileKeyData::NewLC(objectId, aLabel, infoData, publicKeyData, privateKeyData, aIdentityId);
   472 	}
   473 
   474 #else
   475 const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassStreamId)
   476 	{
   477 	ASSERT(aPassStreamId != KNullStreamId);
   478 	TInt objectId = ++iKeyIdentifier;
   479 	TStreamId infoData = CreateWriteStreamL();
   480 	TStreamId publicKeyData = CreateWriteStreamL();
   481 	TStreamId privateKeyData = CreateWriteStreamL();
   482 	return CFileKeyData::NewLC(objectId, aLabel, infoData, aPassStreamId, publicKeyData, privateKeyData);
   483 	}
   484 
   485 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   486 
   487 //	Creates a new write stream in the store (which it then closes)
   488 //	Returns the TStreamId associated with it
   489 TStreamId CFileKeyDataManager::CreateWriteStreamL()
   490 	{
   491 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   492 	if (!iFileStore)
   493 		User::Leave(KErrNotReady);
   494 
   495 	RStoreWriteStream newStream;
   496 	TStreamId result = newStream.CreateLC(*iFileStore);
   497 	if (KNullStreamId == result)
   498 		User::Leave(KErrBadHandle);
   499 
   500 	newStream.CommitL();
   501 	CleanupStack::PopAndDestroy(&newStream);
   502 
   503 	return result;
   504 	}
   505 
   506 CKeyInfo* CFileKeyDataManager::ReadKeyInfoLC(const CFileKeyData& aKeyData) const
   507 	{
   508 	__ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
   509 	RStoreReadStream stream;
   510 	stream.OpenLC(*iFileStore, aKeyData.InfoDataStreamId());
   511 	CKeyInfo* info = CKeyInfo::NewL(stream);
   512 	
   513 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   514 	ReadAuthDetailsL(stream, *info);
   515 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   516 	CleanupStack::PopAndDestroy(&stream);
   517 	info->CleanupPushL();
   518 	if (info->Handle() != aKeyData.Handle())
   519 		{
   520 		User::Leave(KErrCorrupt); // is this appropriate?
   521 		}
   522 	return info;
   523 	}
   524 
   525 void CFileKeyDataManager::WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo)
   526 	{
   527 	RStoreWriteStream infoStream;
   528 	OpenInfoDataStreamLC(aKeyData, infoStream);
   529 	infoStream << aKeyInfo;
   530 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   531 	WriteAuthDetailsL(infoStream, aKeyInfo);
   532 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   533 	infoStream.CommitL();
   534 	CleanupStack::PopAndDestroy(&infoStream);
   535 	}
   536 
   537 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   538 void CFileKeyDataManager::WriteAuthDetailsL( RStoreWriteStream& aInfoStream, const CKeyInfo& aKeyInfo )
   539 	{
   540 	aInfoStream.WriteInt32L(aKeyInfo.Identity());
   541 	aInfoStream << aKeyInfo.AuthExpression();
   542   	aInfoStream.WriteInt32L(aKeyInfo.Freshness());
   543 	}
   544 
   545 void CFileKeyDataManager::ReadAuthDetailsL( RStoreReadStream& aInfoStream, CKeyInfo& aKeyInfo ) const
   546 	{
   547 	aKeyInfo.SetIdentity(aInfoStream.ReadInt32L());
   548 	HBufC* expression = HBufC::NewLC(aInfoStream, KMaxTInt);
   549 	aKeyInfo.SetAuthExpressionL(*expression);
   550 	aKeyInfo.SetFreshness(aInfoStream.ReadInt32L());
   551 	CleanupStack::PopAndDestroy(expression);
   552 	}
   553 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   554 
   555 void CFileKeyDataManager::SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo)
   556 	{
   557 	TCleanupItem cleanupStore(RevertStore, iFileStore);
   558 	CleanupStack::PushL(cleanupStore);	
   559 
   560 	WriteKeyInfoL(aKeyData, aKeyInfo);
   561 	iFileStore->CommitL();
   562 	
   563 	CleanupStack::Pop(); // cleanupStore	
   564 	}
   565 
   566 void CFileKeyDataManager::OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream)
   567 	{
   568 	__ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
   569 	aStream.ReplaceLC(*iFileStore, aKeyData.InfoDataStreamId());
   570 	}
   571 
   572 void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream)
   573 	{
   574 	__ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
   575 	aStream.ReplaceLC(*iFileStore, aKeyData.PublicDataStreamId());
   576 	}
   577 
   578 void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const
   579 	{
   580 	__ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
   581 	aStream.OpenLC(*iFileStore, aKeyData.PublicDataStreamId());
   582 	}
   583 
   584 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   585 
   586 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream) 
   587 	{
   588 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   589 	aStream.ReplaceLC(*iFileStore, aKeyData.PrivateDataStreamId());
   590 	}
   591 
   592 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const
   593 	{
   594 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   595 	aStream.OpenLC(*iFileStore, aKeyData.PrivateDataStreamId());
   596 	}
   597 
   598 #else
   599 
   600 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase,
   601 												  RStoreReadStream& aStream) 
   602 	{
   603 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   604 	aStream.OpenLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId());
   605 	}
   606 
   607 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase,
   608 												  RStoreWriteStream& aStream)
   609 	{
   610 	__ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
   611 	aStream.ReplaceLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId());
   612 	}
   613 
   614 
   615 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   616 
   617 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   618 TInt CFileKeyDataManager::GetPassphraseTimeout() const
   619 	{
   620 	return iTimeout;
   621 	}
   622 
   623 void CFileKeyDataManager::SetPassphraseTimeoutL(TInt aTimeout)
   624 	{
   625 	TInt oldTimeout = iTimeout;
   626 
   627 	iTimeout = aTimeout;
   628 	TRAPD(err, WritePassphraseTimeoutL(); iFileStore->CommitL());
   629 
   630 	if (err != KErrNone)
   631 		{
   632 		iTimeout = oldTimeout;
   633 		iFileStore->RevertL(); // shouldn't leave
   634 		User::Leave(err);
   635 		}
   636 	}
   637 
   638 void CFileKeyDataManager::ReadPassphraseTimeoutL()
   639 	{
   640 	ASSERT(iTimeout == 0); // Only called from ConstructL()
   641 	
   642 	RStoreReadStream stream;
   643 	stream.OpenLC(*iFileStore, iTimeoutStreamId);
   644 	iTimeout = stream.ReadInt32L();
   645 	CleanupStack::PopAndDestroy(&stream);
   646 	}
   647 
   648 void CFileKeyDataManager::WritePassphraseTimeoutL()
   649 	{
   650 	RStoreWriteStream stream;
   651 	stream.ReplaceLC(*iFileStore, iTimeoutStreamId);
   652 	stream.WriteUint32L(iTimeout);	
   653 	stream.CommitL();
   654 	CleanupStack::PopAndDestroy(&stream);
   655 	}
   656 
   657 TStreamId CFileKeyDataManager::DefaultPassphraseId() const
   658 	{
   659 	ASSERT((iPassStreamId == KNullStreamId) == (Count() == 0));
   660 	return iPassStreamId;
   661 	}
   662 
   663 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   664 
   665 /**
   666  * Attempt to compact the store - it doesn't matter if these calls leave, it
   667  * will only mean that the store takes up more space than necessary.
   668  */
   669 void CFileKeyDataManager::CompactStore()
   670 	{
   671 	ASSERT(iFileStore);
   672 	TRAP_IGNORE(iFileStore->ReclaimL(); iFileStore->CompactL());
   673 	}
   674 
   675 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   676 TUint32 CFileKeyDataManager::CachedIdentity()
   677 	{
   678 	TInt value = 0;
   679 	iIdentityId.Get(value);
   680 	return value;
   681 	}
   682 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   683 
   684 // CFileKeyData ////////////////////////////////////////////////////////////////
   685 
   686 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   687 CFileKeyData* CFileKeyData::NewLC(	TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
   688 								  TStreamId aPublicData, TStreamId aPrivateData,
   689 								  AuthServer::TIdentityId aIdentityId)
   690 	{
   691 	CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPublicData, aPrivateData, aIdentityId);
   692 	CleanupStack::PushL(self);
   693 	self->ConstructL(aLabel);
   694 	return self;
   695 	}
   696 #else
   697 CFileKeyData* CFileKeyData::NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
   698 								  TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData)
   699 	{
   700 	CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPassphraseId, aPublicData, aPrivateData);
   701 	CleanupStack::PushL(self);
   702 	self->ConstructL(aLabel);
   703 	return self;
   704 	}
   705 
   706 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   707 
   708 CFileKeyData* CFileKeyData::NewL(RStoreReadStream& aReadStream)
   709 	{
   710 	CFileKeyData* self = new (ELeave) CFileKeyData();
   711 	CleanupStack::PushL(self);
   712 	self->InternalizeL(aReadStream);
   713 	CleanupStack::Pop(self);
   714 	return (self);
   715 	}
   716 
   717 CFileKeyData::~CFileKeyData()
   718 	{
   719 	delete iLabel;
   720 	}
   721 
   722 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   723 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData,  
   724 						   TStreamId aPublicData, TStreamId aPrivateData,
   725 						   AuthServer::TIdentityId aIdentityId) :
   726 	iObjectId(aObjectId), iInfoData(aInfoData), 
   727 	iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData),
   728 	iIdentityId(aIdentityId)
   729 	{
   730 	ASSERT(iObjectId);
   731 	ASSERT(iInfoData != KNullStreamId);
   732 	ASSERT(iPublicKeyData != KNullStreamId);
   733 	ASSERT(iPrivateKeyData != KNullStreamId);
   734 	ASSERT(iIdentityId);
   735 	}
   736 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   737 #ifdef KEYTOOL
   738 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData,  
   739 						   TStreamId aPublicData, TStreamId aPrivateData,
   740 						   AuthServer::TIdentityId aIdentityId) :
   741 	iObjectId(aObjectId), iInfoData(aInfoData), 
   742 	iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData),
   743 	iIdentityId(aIdentityId)
   744 	{
   745 	ASSERT(iObjectId);
   746 	ASSERT(iInfoData != KNullStreamId);
   747 	ASSERT(iPublicKeyData != KNullStreamId);
   748 	ASSERT(iPrivateKeyData != KNullStreamId);
   749 	ASSERT(iIdentityId);
   750 	}
   751 #endif // KEYTOOL
   752 
   753 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId, 
   754 						   TStreamId aPublicData, TStreamId aPrivateData) :
   755 	iObjectId(aObjectId), iInfoData(aInfoData), iPassphraseId(aPassphraseId),
   756 	iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData)
   757 	{
   758 	ASSERT(iObjectId);
   759 	ASSERT(iInfoData != KNullStreamId);
   760 	ASSERT(iPassphraseId != KNullStreamId);
   761 	ASSERT(iPublicKeyData != KNullStreamId);
   762 	ASSERT(iPrivateKeyData != KNullStreamId);
   763 	}
   764 
   765 CFileKeyData::CFileKeyData()
   766 	{
   767 	}
   768 
   769 void CFileKeyData::ConstructL(const TDesC& aLabel)
   770 	{
   771 	TInt labelLen = aLabel.Length();
   772 	iLabel = HBufC::NewMaxL(labelLen);
   773 	TPtr theLabel(iLabel->Des());
   774 	theLabel.FillZ();
   775 	theLabel.Copy(aLabel);
   776 	}
   777 
   778 void CFileKeyData::InternalizeL(RReadStream& aReadStream)
   779 {
   780 	iObjectId = aReadStream.ReadInt32L();
   781 	iInfoData.InternalizeL(aReadStream);
   782 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   783 	iPassphraseId.InternalizeL(aReadStream);
   784 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   785 	iPublicKeyData.InternalizeL(aReadStream);
   786 	iPrivateKeyData.InternalizeL(aReadStream);
   787 	
   788 	TInt labelLen = aReadStream.ReadInt32L();
   789 	iLabel = HBufC::NewMaxL(labelLen);
   790 	TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen);
   791 	theLabel.FillZ(labelLen);
   792 	aReadStream.ReadL(theLabel);
   793 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   794 	iIdentityId = aReadStream.ReadInt32L();
   795 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   796 }
   797 
   798 void CFileKeyData::ExternalizeL(RWriteStream& aWriteStream) const
   799 {
   800 	aWriteStream.WriteInt32L(iObjectId);
   801 	iInfoData.ExternalizeL(aWriteStream);
   802 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   803 	iPassphraseId.ExternalizeL(aWriteStream);
   804 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   805 	iPublicKeyData.ExternalizeL(aWriteStream);
   806 	iPrivateKeyData.ExternalizeL(aWriteStream);
   807 
   808 	TInt labelLen = iLabel->Length();
   809 	aWriteStream.WriteInt32L(labelLen);
   810 	TPtr theLabel(iLabel->Des());
   811 	theLabel.SetLength(labelLen);
   812 	aWriteStream.WriteL(theLabel);
   813 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   814 	aWriteStream.WriteInt32L(iIdentityId);
   815 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   816 
   817 }
   818 
   819 #ifdef KEYTOOL
   820 
   821 CFileKeyData* CFileKeyData::CreateOldKeyL(RStoreReadStream& aReadStream)
   822 	{
   823 	CFileKeyData* self = new (ELeave) CFileKeyData();
   824 	CleanupStack::PushL(self);
   825 	self->InternalizeOldKeyL(aReadStream);
   826 	CleanupStack::Pop(self);
   827 	return (self);
   828 	}
   829 
   830 void CFileKeyData::InternalizeOldKeyL(RReadStream& aReadStream)
   831 	{
   832 	iObjectId = aReadStream.ReadInt32L();
   833 	iInfoData.InternalizeL(aReadStream);
   834 	iPassphraseId.InternalizeL(aReadStream);
   835 	iPublicKeyData.InternalizeL(aReadStream);
   836 	iPrivateKeyData.InternalizeL(aReadStream);
   837 	
   838 	TInt labelLen = aReadStream.ReadInt32L();
   839 	iLabel = HBufC::NewMaxL(labelLen);
   840 	TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen);
   841 	theLabel.FillZ(labelLen);
   842 	aReadStream.ReadL(theLabel);
   843 	}
   844 
   845 void CFileKeyData::ExternalizeWithAuthL(RWriteStream& aWriteStream) 
   846 {
   847 	aWriteStream.WriteInt32L(iObjectId);
   848 	iInfoData.ExternalizeL(aWriteStream);
   849 	iPublicKeyData.ExternalizeL(aWriteStream);
   850 	iPrivateKeyData.ExternalizeL(aWriteStream);
   851 
   852 	TInt labelLen = iLabel->Length();
   853 	aWriteStream.WriteInt32L(labelLen);
   854 	TPtr theLabel(iLabel->Des());
   855 	theLabel.SetLength(labelLen);
   856 	aWriteStream.WriteL(theLabel);
   857 	aWriteStream.WriteInt32L(iIdentityId);
   858 }
   859 
   860 #endif // KEYTOOL