os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/OpenedKeys.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2004-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 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 "OpenedKeys.h"
    20 #include "cfskeystoreserver.h"
    21 #include "keystreamutils.h"
    22 #include "fsdatatypes.h"
    23 #include "keystorepassphrase.h"
    24 
    25 #include <asymmetric.h>
    26 #include <asymmetrickeys.h>
    27 #include <bigint.h>
    28 #include <ct.h>
    29 #include <securityerr.h>
    30 #include <e32base.h>
    31 #include <mctkeystoreuids.h>
    32 
    33 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    34 #include <authserver/authtypes.h>
    35 #include <authserver/auth_srv_errs.h>
    36 #include <s32mem.h>
    37 #include "keystore_errs.h"
    38 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    39 
    40 // COpenedKey //////////////////////////////////////////////////////////////////
    41 
    42 COpenedKey* COpenedKey::NewL(const CFileKeyData& aKeyData, TUid aType, const RMessage2& aMessage,
    43 							 CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan)
    44 	{
    45 	COpenedKey* self = NULL;
    46 
    47 	if (aType == KRSARepudiableSignerUID)
    48 		{
    49 		self = new (ELeave) CRSARepudiableSigner(aKeyData, aKeyDataMan, aPassMan);
    50 		}
    51 	else if (aType == KDSARepudiableSignerUID)
    52 		{
    53 		self = new (ELeave) CDSARepudiableSigner(aKeyData, aKeyDataMan, aPassMan);
    54 		}
    55 	else if (aType == KPrivateDecryptorUID)
    56 		{
    57 		self = new (ELeave) CFSRSADecryptor(aKeyData, aKeyDataMan, aPassMan);
    58 		}
    59 	else if (aType == KKeyAgreementUID)
    60 		{
    61 		self = new (ELeave) CDHAgreement(aKeyData, aKeyDataMan, aPassMan);
    62 		}
    63 	else
    64 		{
    65 		User::Invariant();
    66 		}
    67 
    68 	CleanupStack::PushL(self);
    69 	self->ConstructL(aMessage);
    70 	CleanupStack::Pop(self);
    71 	return self;
    72 	}
    73 
    74 COpenedKey::COpenedKey(const CFileKeyData& aKeyData, CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan) :
    75 	CActive(EPriorityStandard),
    76 	iKeyData(aKeyData),
    77 	iKeyDataMan(aKeyDataMan),
    78 	iPassMan(aPassMan)
    79 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    80 	,iUserIdentity(NULL)
    81 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    82 	{
    83 	}
    84 
    85 void COpenedKey::ConstructL(const RMessage2& aMessage)
    86 	{
    87 	CKeyInfo* keyInfo = iKeyDataMan.ReadKeyInfoLC(iKeyData);
    88 	CleanupStack::Pop(keyInfo);
    89 	iKeyInfo = keyInfo;
    90 	CheckKeyL(aMessage);
    91 	iLabel = iKeyInfo->Label().AllocL();
    92 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    93 	User::LeaveIfError(iAuthClient.Connect());
    94 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    95 	CActiveScheduler::Add(this);
    96 	}
    97 
    98 COpenedKey::~COpenedKey()
    99 	{
   100 	Cancel();
   101 	delete iLabel;
   102 	delete iKeyInfo;
   103 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   104 	iAuthClient.Close();
   105 	delete iExpression;
   106 	delete iUserIdentity;
   107 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   108 
   109 	}
   110 
   111 const TDesC& COpenedKey::Label() const
   112 	{
   113 	return *iLabel;
   114 	}
   115 
   116 TInt COpenedKey::Handle() const
   117 	{
   118 	return iKeyData.Handle();
   119 	}
   120 
   121 void COpenedKey::CheckKeyL(const RMessage2& aMessage)
   122 	{
   123 	// Check the client is allowed to use the key
   124 	if (!iKeyInfo->UsePolicy().CheckPolicy(aMessage))
   125 		{
   126 		User::Leave(KErrPermissionDenied);
   127 		}
   128 
   129 	// Check that the operation represented by this object is supported for this
   130 	// type of key
   131 	if (iKeyInfo->Algorithm() != Algorithm())
   132 		{
   133 		User::Leave(KErrKeyAlgorithm);
   134 		}
   135 
   136 	// Check the key usage allows the operation
   137 	if ((iKeyInfo->Usage() & RequiredUsage()) == 0)
   138 		{
   139 		User::Leave(KErrKeyUsage);
   140 		}
   141 
   142 	// Check current time is after start date (if set) and before end date (if
   143 	// set)
   144 	TTime timeNow;
   145 	timeNow.UniversalTime();
   146 	if (iKeyInfo->StartDate().Int64() != 0 && timeNow < iKeyInfo->StartDate())
   147 		{
   148 		User::Leave(KErrKeyValidity);
   149 		}
   150 	if (iKeyInfo->EndDate().Int64() != 0 && timeNow >= iKeyInfo->EndDate())
   151 		{
   152 		User::Leave(KErrKeyValidity);
   153 		}
   154 		
   155 	}
   156 
   157 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   158 void COpenedKey::GetPassphrase(TRequestStatus& aStatus)
   159 	{
   160 	ASSERT(iState == EIdle);
   161 
   162 	TInt timeout = iKeyDataMan.GetPassphraseTimeout();
   163 	TStreamId passphraseId = iKeyDataMan.DefaultPassphraseId();
   164 	ASSERT(passphraseId != KNullStreamId);
   165 	iClientStatus = &aStatus;
   166 	
   167 	iPassMan.GetPassphrase(passphraseId, timeout, iPassphrase, iStatus);
   168 	iState = EGetPassphrase;
   169 	SetActive();
   170 	}
   171 #else
   172 void COpenedKey::AuthenticateL()
   173 	{	
   174 	iExpression = iAuthClient.CreateAuthExpressionL(iKeyInfo->AuthExpression());
   175 	TUid uid = TUid::Uid(0);
   176 	iAuthClient.AuthenticateL(*iExpression,iKeyInfo->Freshness(), EFalse, uid, EFalse, KNullDesC, iUserIdentity, iStatus);
   177 	iState = EAuthenticate;
   178 	SetActive();
   179 	}
   180 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   181 
   182 void COpenedKey::RunL()
   183 	{
   184 	User::LeaveIfError(iStatus.Int());
   185 
   186 	switch (iState)
   187 		{
   188 		#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   189 		case EDoAuthenticate:
   190 			AuthenticateL();
   191 			break;
   192 			
   193 		case EAuthenticate:
   194 			if(iUserIdentity->Id() == AuthServer::KUnknownIdentity)
   195 				{
   196 				User::Leave(KErrAuthenticationFailure);
   197 				}
   198 					
   199 			if (!iKeyRead)
   200 				{
   201 				RStoreReadStream stream;
   202 				iKeyDataMan.OpenPrivateDataStreamLC(iKeyData, stream);
   203 				TPtrC8 key = iUserIdentity->Key().KeyData();
   204 				HBufC8* plaintext = DecryptFromStreamL(stream, key);
   205 				CleanupStack::PushL(plaintext);
   206 				TAny* ptr = const_cast<TAny*>(static_cast<const TAny*>(plaintext->Des().PtrZ()));
   207 							
   208 				RMemReadStream decryptedStream(ptr, plaintext->Length());
   209 				decryptedStream.PushL();
   210 				ReadPrivateKeyL(decryptedStream);
   211 				CleanupStack::PopAndDestroy(3,&stream); // plaintext, decryptedStream
   212 				iKeyRead = ETrue;
   213 				}
   214 			
   215 			delete iUserIdentity;
   216 			iUserIdentity = NULL;
   217 			delete iExpression;
   218 			iExpression = NULL;
   219 			PerformOperationL();
   220 			Complete(KErrNone);
   221 			break;
   222 		#else
   223 		case EGetPassphrase:
   224 			ASSERT(iPassphrase);
   225 			if (!iKeyRead)
   226 				{
   227 				RStoreReadStream stream;
   228 				iKeyDataMan.OpenPrivateDataStreamLC(iKeyData, *iPassphrase, stream);
   229 				ReadPrivateKeyL(stream);
   230 				CleanupStack::PopAndDestroy(&stream);
   231 				iKeyRead = ETrue;
   232 				}
   233 			PerformOperationL();
   234 			Complete(KErrNone);
   235 			break;	
   236 		#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   237 		default:
   238 			ASSERT(EFalse);
   239 		}
   240 	}
   241 
   242 TInt COpenedKey::RunError(TInt aError)
   243 	{
   244 	Complete(aError);
   245 	return KErrNone;
   246 	}
   247 
   248 void COpenedKey::DoCancel()
   249 	{
   250 	Complete(KErrCancel);
   251 	}
   252 
   253 void COpenedKey::Complete(TInt aError)
   254 	{
   255 	Cleanup();
   256 	iPassphrase = NULL;
   257 	if (iClientStatus)
   258 		{
   259 		User::RequestComplete(iClientStatus, aError);
   260 		}
   261 	iState = EIdle;
   262 	}
   263 
   264 void COpenedKey::Cleanup()
   265 	{
   266 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   267 		delete iUserIdentity;
   268 		iUserIdentity = NULL;
   269 		delete iExpression;
   270 		iExpression = NULL;
   271 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   272 	}
   273 
   274 // CRSARepudiableSigner ////////////////////////////////////////////////////////
   275 
   276 CRSARepudiableSigner::CRSARepudiableSigner(const CFileKeyData& aKeyData, CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan) :
   277 	COpenedKey(aKeyData, aKeyDataMan, aPassMan)
   278 	{
   279 	}
   280 
   281 CRSARepudiableSigner::~CRSARepudiableSigner()
   282 	{
   283 	delete iPrivateKey;
   284 	}
   285 
   286 TUid CRSARepudiableSigner::Type() const
   287 	{
   288 	return KRSARepudiableSignerUID;
   289 	}
   290 
   291 CKeyInfo::EKeyAlgorithm CRSARepudiableSigner::Algorithm() const
   292 	{
   293 	return CKeyInfo::ERSA;
   294 	}
   295  
   296 TKeyUsagePKCS15 CRSARepudiableSigner::RequiredUsage() const
   297 	{
   298 	return EPKCS15UsageSignSignRecover;
   299 	}
   300 
   301 void CRSARepudiableSigner::Sign(const TDesC8& aPlaintext,
   302 								CRSASignature*& aSignature,
   303 								TRequestStatus& aStatus)
   304 	{
   305 	ASSERT(iPlaintext.Ptr() == NULL);
   306 	ASSERT(iSignaturePtr == NULL);
   307 	iPlaintext.Set(aPlaintext);
   308 	iSignaturePtr = &aSignature;
   309 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   310 	GetPassphrase(aStatus);
   311 #else
   312 	aStatus = KRequestPending;
   313 	iClientStatus = &aStatus;
   314 	iState = EDoAuthenticate;
   315 	SetActive();
   316 	TRequestStatus* status = &iStatus;
   317 	User::RequestComplete(status, KErrNone);
   318 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   319 	}
   320 
   321 void CRSARepudiableSigner::ReadPrivateKeyL(RReadStream& aStream)
   322 	{
   323 	ASSERT(iPrivateKey == NULL);
   324 	CreateL(aStream, iPrivateKey);
   325 	}
   326 
   327 void CRSARepudiableSigner::PerformOperationL()
   328 	{
   329 	ASSERT(iPrivateKey);
   330 	
   331 	CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewLC(*iPrivateKey);
   332 	const CRSASignature* signature = signer->SignL(iPlaintext);
   333 	CleanupStack::PopAndDestroy(signer);
   334 	*iSignaturePtr = const_cast<CRSASignature*>(signature);
   335 	}
   336 
   337 void CRSARepudiableSigner::Cleanup()
   338 	{
   339 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   340 	COpenedKey::Cleanup();
   341 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   342 	iPlaintext.Set(NULL, 0);
   343 	iSignaturePtr = NULL;
   344 	}
   345 
   346 // CDSARepudiableSigner ////////////////////////////////////////////////////////
   347 
   348 CDSARepudiableSigner::CDSARepudiableSigner(const CFileKeyData& aKeyData, CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan) :
   349 	COpenedKey(aKeyData, aKeyDataMan, aPassMan)
   350 	{
   351 	}
   352 
   353 CDSARepudiableSigner::~CDSARepudiableSigner()
   354 	{
   355 	delete iPrivateKey;
   356 	}
   357 
   358 TUid CDSARepudiableSigner::Type() const
   359 	{
   360 	return KDSARepudiableSignerUID;
   361 	}
   362 
   363 CKeyInfo::EKeyAlgorithm CDSARepudiableSigner::Algorithm() const
   364 	{
   365 	return CKeyInfo::EDSA;
   366 	}
   367  
   368 TKeyUsagePKCS15 CDSARepudiableSigner::RequiredUsage() const
   369 	{
   370 	return EPKCS15UsageSignSignRecover;
   371 	}
   372 
   373 void CDSARepudiableSigner::Sign(const TDesC8& aPlaintext,
   374 								CDSASignature*& aSignature,
   375 								TRequestStatus& aStatus)
   376 	{
   377 	ASSERT(iPlaintext.Ptr() == NULL);
   378 	ASSERT(iSignaturePtr == NULL);
   379 	iPlaintext.Set(aPlaintext);
   380 	iSignaturePtr = &aSignature;
   381 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   382 	GetPassphrase(aStatus);
   383 #else
   384 	aStatus = KRequestPending;
   385 	iClientStatus = &aStatus;
   386 	iState = EDoAuthenticate;
   387 	SetActive();
   388 	TRequestStatus* status = &iStatus;
   389 	User::RequestComplete(status, KErrNone);
   390 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   391 	}
   392 
   393 void CDSARepudiableSigner::ReadPrivateKeyL(RReadStream& aStream)
   394 	{
   395 	ASSERT(iPrivateKey == NULL);
   396 	CreateL(aStream, iPrivateKey);
   397 	}
   398 
   399 void CDSARepudiableSigner::PerformOperationL()
   400 	{
   401 	ASSERT(iPrivateKey);
   402 	
   403 	CDSASigner* signer = CDSASigner::NewLC(*iPrivateKey);
   404 	const CDSASignature* signature = signer->SignL(iPlaintext);
   405 	CleanupStack::PopAndDestroy(signer);
   406 	*iSignaturePtr = const_cast<CDSASignature*>(signature);
   407 	}
   408 
   409 void CDSARepudiableSigner::Cleanup()
   410 	{
   411 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   412 	COpenedKey::Cleanup();
   413 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   414 
   415 	iPlaintext.Set(NULL, 0);
   416 	iSignaturePtr = NULL;
   417 	}
   418 
   419 // CFSRSADecryptor /////////////////////////////////////////////////////////////
   420 
   421 CFSRSADecryptor::CFSRSADecryptor(const CFileKeyData& aKeyData, CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan) :
   422 	COpenedKey(aKeyData, aKeyDataMan, aPassMan)
   423 	{
   424 	}
   425 
   426 CFSRSADecryptor::~CFSRSADecryptor()
   427 	{
   428 	delete iPrivateKey;
   429 	}
   430 
   431 TUid CFSRSADecryptor::Type() const
   432 	{
   433 	return KPrivateDecryptorUID;
   434 	}
   435 
   436 CKeyInfo::EKeyAlgorithm CFSRSADecryptor::Algorithm() const
   437 	{
   438 	return CKeyInfo::ERSA;
   439 	}
   440  
   441 TKeyUsagePKCS15 CFSRSADecryptor::RequiredUsage() const
   442 	{
   443 	return EPKCS15UsageDecryptUnwrap;
   444 	}
   445 
   446 void CFSRSADecryptor::Decrypt(const TDesC8& aCiphertext,
   447 							  HBufC8*& aPlaintext,
   448 							  TRequestStatus& aStatus)
   449 	{
   450 	ASSERT(iCiphertext.Ptr() == NULL);
   451 	ASSERT(iPlaintextPtr == NULL);
   452 	iCiphertext.Set(aCiphertext);
   453 	iPlaintextPtr = &aPlaintext;
   454 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   455 	GetPassphrase(aStatus);
   456 #else
   457 	aStatus = KRequestPending;
   458 	iClientStatus = &aStatus;
   459 	iState = EDoAuthenticate;
   460 	SetActive();
   461 	TRequestStatus* status = &iStatus;
   462 	User::RequestComplete(status, KErrNone);
   463 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   464 	}
   465 
   466 void CFSRSADecryptor::ReadPrivateKeyL(RReadStream& aStream)
   467 	{
   468 	ASSERT(iPrivateKey == NULL);
   469 	CreateL(aStream, iPrivateKey);
   470 	}
   471 
   472 void CFSRSADecryptor::PerformOperationL()
   473 	{
   474 	ASSERT(iPrivateKey);
   475 	
   476 	CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewLC(*iPrivateKey);
   477 	HBufC8* plaintext = HBufC8::NewMaxLC(decryptor->MaxOutputLength());
   478 	TPtr8 ptr = plaintext->Des();
   479 	decryptor->DecryptL(iCiphertext, ptr);
   480 
   481 	*iPlaintextPtr = plaintext;
   482 	CleanupStack::Pop(plaintext); // now owned by client
   483 	CleanupStack::PopAndDestroy(decryptor);
   484 	}
   485 
   486 void CFSRSADecryptor::Cleanup()
   487 	{
   488 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   489 	COpenedKey::Cleanup();
   490 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   491 
   492 	iCiphertext.Set(NULL, 0);
   493 	iPlaintextPtr = NULL;
   494 	}
   495 
   496 // CDHAgreement ////////////////////////////////////////////////////////////////
   497 
   498 CDHAgreement::CDHAgreement(const CFileKeyData& aKeyData, CFileKeyDataManager& aKeyDataMan, CPassphraseManager& aPassMan) :
   499 	COpenedKey(aKeyData, aKeyDataMan, aPassMan)
   500 	{
   501 	}
   502 
   503 CDHAgreement::~CDHAgreement()
   504 	{
   505 	iKey.Close();
   506 	}
   507 
   508 TUid CDHAgreement::Type() const
   509 	{
   510 	return KKeyAgreementUID;
   511 	}
   512 
   513 CKeyInfo::EKeyAlgorithm CDHAgreement::Algorithm() const
   514 	{
   515 	return CKeyInfo::EDH;
   516 	}
   517  
   518 TKeyUsagePKCS15 CDHAgreement::RequiredUsage() const
   519 	{
   520 	return EPKCS15UsageDerive;
   521 	}
   522 
   523 void CDHAgreement::PublicKey(CDHParams& aParameters, RInteger& aPublicKey, TRequestStatus& aStatus)
   524 	{
   525 	ASSERT(iPKParams == NULL);
   526 	ASSERT(iPKPublicKeyPtr == NULL);
   527 	iPKParams = &aParameters;
   528 	iPKPublicKeyPtr = &aPublicKey;
   529 	iDHState = EPublicKey;
   530 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   531 	GetPassphrase(aStatus);
   532 #else
   533 	aStatus = KRequestPending;
   534 	iClientStatus = &aStatus;
   535 	iState = EDoAuthenticate;
   536 	SetActive();
   537 	TRequestStatus* status = &iStatus;
   538 	User::RequestComplete(status, KErrNone);
   539 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   540 	}
   541 
   542 void CDHAgreement::Agree(CDHPublicKey& aY, HBufC8*& aAgreedKey, TRequestStatus& aStatus)
   543 	{
   544 	ASSERT(iAKPublicKey == NULL);
   545 	ASSERT(iAKAgreedKeyPtr == NULL);
   546 	iAKPublicKey = &aY;
   547 	iAKAgreedKeyPtr = &aAgreedKey;
   548 	iDHState = EAgree;
   549 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   550 	GetPassphrase(aStatus);
   551 #else
   552 	aStatus = KRequestPending;
   553 	iClientStatus = &aStatus;
   554 	iState = EDoAuthenticate;
   555 	SetActive();
   556 	TRequestStatus* status = &iStatus;
   557 	User::RequestComplete(status, KErrNone);
   558 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   559 	}
   560 
   561 void CDHAgreement::ReadPrivateKeyL(RReadStream& aStream)
   562 	{
   563 	CreateLC(aStream, iKey);
   564 	CleanupStack::Pop(&iKey);
   565 	}
   566 
   567 void CDHAgreement::PerformOperationL()
   568 	{
   569 	switch (iDHState)
   570 		{
   571 		case EPublicKey:
   572 			DoPublicKeyL();
   573 			break;
   574 		case EAgree:
   575 			DoAgreeL();
   576 			break;
   577 		default:
   578 			ASSERT(FALSE);
   579 		}
   580 	}
   581 
   582 void CDHAgreement::DoPublicKeyL()
   583 	{
   584 	ASSERT(iPKParams);
   585 	ASSERT(iPKPublicKeyPtr);
   586 	
   587 	RInteger n = iPKParams->TakeN();
   588 	CleanupStack::PushL(n);
   589 	RInteger g = iPKParams->TakeG();
   590 	CleanupStack::PushL(g);
   591 	RInteger x = RInteger::NewL(iKey);
   592 	CleanupStack::PushL(x);
   593 	CDHKeyPair* keyPair = CDHKeyPair::NewL(n, g, x);
   594 	CleanupStack::Pop(3); // x, g, n
   595 	CleanupStack::PushL(keyPair);
   596 	
   597 	const CDHPublicKey& pubKey = keyPair->PublicKey();
   598 	*iPKPublicKeyPtr = RInteger::NewL(pubKey.X());
   599 	CleanupStack::PopAndDestroy(keyPair);
   600 	}
   601 
   602 void CDHAgreement::DoAgreeL()
   603 	{
   604 	ASSERT(iAKPublicKey);
   605 	ASSERT(iAKAgreedKeyPtr);
   606 	
   607 	RInteger n = RInteger::NewL(iAKPublicKey->N());
   608 	CleanupStack::PushL(n);
   609 	RInteger g = RInteger::NewL(iAKPublicKey->G());
   610 	CleanupStack::PushL(g);
   611 	RInteger x = RInteger::NewL(iKey);
   612 	CleanupStack::PushL(x);
   613 	CDHPrivateKey* privKey = CDHPrivateKey::NewL(n, g, x);
   614 	CleanupStack::Pop(3); // x, g, n
   615 	CleanupStack::PushL(privKey);
   616 	CDH* dh = CDH::NewLC(*privKey);
   617 	*iAKAgreedKeyPtr = const_cast<HBufC8*>(dh->AgreeL(*iAKPublicKey));
   618 	CleanupStack::PopAndDestroy(2, privKey);
   619 	}
   620 
   621 void CDHAgreement::Cleanup()
   622 	{
   623 	#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   624 	COpenedKey::Cleanup();
   625 	#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   626 
   627 	iPKParams = NULL;
   628 	iPKPublicKeyPtr = NULL;
   629 	iAKPublicKey = NULL;
   630 	iAKAgreedKeyPtr = NULL;
   631 	iDHState = EIdle;
   632 	}