os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_open.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) 2003-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 <e32base.h>
    20 #include "t_keystore_actions.h"
    21 #include "t_keystore_defs.h"
    22 #include "t_input.h"
    23 #include <asn1enc.h>
    24 #include <asn1dec.h>
    25 #include <x509cert.h>
    26 #include <x509keys.h>
    27 #include <asymmetrickeys.h>
    28 
    29 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
    30 #include <cryptospidef.h>
    31 #include "cryptoasymmetriccipherapi.h"
    32 #include "cryptosignatureapi.h"
    33 #include <cryptospi/cryptoparams.h>
    34 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC && SYMBIAN_ENABLE_SDP_ECC
    35 
    36 /*static*/ CTestAction* COpenKey::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
    37 {
    38 	CTestAction* self = COpenKey::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    39 	CleanupStack::Pop(self);
    40 	return self;
    41 }
    42 
    43 /*static*/ CTestAction* COpenKey::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
    44 {
    45 	COpenKey* self = new (ELeave) COpenKey(aFs, aConsole, aOut);
    46 	CleanupStack::PushL(self);
    47 	self->ConstructL(aTestActionSpec);
    48 	return self;
    49 }
    50 
    51 COpenKey::~COpenKey()
    52 {
    53 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
    54     delete iSigner;
    55     delete iDecrypt;
    56 #endif
    57 	iKeys.Close();
    58 }
    59 
    60 void COpenKey::PerformAction(TRequestStatus& aStatus)
    61 {
    62 	switch (iState)
    63 	{	
    64 		case EListKeysPreOpen:
    65 		{//	Currently uses the first store, change to check the script for a specific store
    66 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    67 			ASSERT(keyStore);	//	Flag it up
    68 			if (keyStore)
    69 				keyStore->List(iKeys, iFilter, aStatus);
    70 			
    71 			iState = EOpenKey;
    72 		}
    73 		break;
    74 		
    75 		case EOpenKey:
    76 			{
    77 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    78 			ASSERT(keyStore);	//	Flag it up
    79 			
    80 			TInt keyCount = iKeys.Count();
    81 			TInt i;
    82 			
    83 			for (i = 0; i < keyCount; i++)
    84 				{
    85 				CCTKeyInfo* keyInfo = iKeys[i];
    86 
    87 				if (keyInfo->Label() == *iLabel)
    88 					{
    89 					switch (iType)
    90 						{
    91 					case ERSASign:
    92 						keyStore->Open(*keyInfo, iRSASigner, aStatus);
    93 						break;
    94 					case EDSASign:
    95 						keyStore->Open(*keyInfo, iDSASigner, aStatus);
    96 						break;
    97 					case EDecrypt:
    98 						keyStore->Open(*keyInfo, iDecryptor, aStatus);
    99 						break;
   100 					case EDH:
   101 						keyStore->Open(*keyInfo, iDH, aStatus);
   102 						break;
   103 					#if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   104 					case EECC:
   105 						{
   106 						ASSERT(iHardwareType);
   107 						if(iOperationType == ESigning)
   108 							{
   109 							keyStore->Open(keyInfo->Handle(),iSigner,aStatus);
   110 							}
   111 						if(iOperationType == EDecryption)
   112 							{
   113 							keyStore->Open(keyInfo->Handle(),iDecrypt,aStatus);
   114 							}
   115 						break;
   116 						}
   117 					#endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   118 						}
   119 					break;
   120 					}
   121 				}
   122 			if (i == keyCount)
   123 				{
   124 				TRequestStatus* status = &aStatus;
   125 				User::RequestComplete(status, KErrNotFound);
   126 				}
   127 				
   128 			iState = EFinished;
   129 			
   130 		}
   131 		break;
   132 
   133 		case EFinished:
   134 			{
   135 			TInt completionCode = aStatus.Int();
   136 			HBufC* label = 0;
   137 			iKeys.Close();
   138 			if (aStatus.Int() == KErrNone)
   139 				{
   140 				switch (iType)
   141 					{
   142 					case ERSASign:
   143 						if (iRSASigner)
   144 							{
   145 							label = iRSASigner->Label().AllocLC();
   146 							iRSASigner->Release();
   147 							}
   148 						break;
   149 					case EDSASign:
   150 						if (iDSASigner)
   151 							{
   152 							label = iDSASigner->Label().AllocLC();
   153 							iDSASigner->Release();
   154 							}
   155 						break;
   156 					case EDecrypt:
   157 						if (iDecryptor)
   158 							{
   159 							label = iDecryptor->Label().AllocLC();
   160 							iDecryptor->Release();
   161 							}
   162 					case EDH:
   163 						if (iDH)
   164 							{
   165 							label = iDH->Label().AllocLC();
   166 							iDH->Release();
   167 							}
   168 						break;
   169 	#if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   170 					case EECC:
   171 						{
   172 						ASSERT(iHardwareType);
   173 						if(iOperationType == ESigning && iSigner == NULL
   174 								|| iOperationType == EDecryption && iDecrypt == NULL)
   175 							{
   176 							completionCode = KErrGeneral;
   177 							}
   178 						break;
   179 						}
   180 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   181 				} // switch
   182 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   183 				if(iHardwareType == 0)
   184 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   185 					{
   186 					if (*label != *iLabel)
   187 						aStatus = KErrBadName;
   188 					CleanupStack::PopAndDestroy(label);
   189 					}
   190 				}
   191 			TRequestStatus* status = &aStatus;
   192 			User::RequestComplete(status, completionCode);
   193 
   194 			if (aStatus.Int()==iExpectedResult)
   195 				{
   196 				iResult = ETrue;
   197 				}
   198 			else
   199 			{
   200 				iResult = EFalse;
   201 			}
   202 			
   203 			iActionState = EPostrequisite;
   204 		}
   205 		break;
   206 		default:
   207 			ASSERT(EFalse);	
   208 	}
   209 
   210 }
   211 
   212 void COpenKey::PerformCancel()
   213     {
   214     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   215     ASSERT(keystore);
   216     
   217     switch (iState)
   218         {
   219         case EOpenKey:
   220             keystore->CancelList();
   221             break;
   222 
   223         case EFinished:
   224             keystore->CancelOpen();
   225             break;
   226 
   227         default:
   228             break;
   229         }
   230     }
   231 
   232 void COpenKey::Reset()
   233 	{
   234   	iState = EListKeysPreOpen;
   235     iKeys.Close();
   236 	if (iRSASigner)
   237 		{
   238 		iRSASigner->Release();
   239 		iRSASigner = NULL;
   240 		}
   241 	if (iDSASigner)
   242 		{
   243 		iDSASigner->Release();
   244 		iDSASigner = NULL;
   245 		}
   246 	if (iDecryptor)
   247 		{
   248 		iDecryptor->Release();
   249 		iDecryptor = NULL;
   250 		}
   251 	if (iDH)
   252 		{
   253 		iDH->Release();
   254 		iDH = NULL;
   255 		}
   256 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   257     delete iSigner;
   258     iSigner = NULL;
   259     delete iDecrypt;
   260     iDecrypt = NULL;
   261 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   262 	}
   263 
   264 void COpenKey::DoReportAction()
   265 {
   266 	_LIT(KDeleting, "Opening...");
   267 	iOut.writeString(KDeleting);
   268 	iOut.writeNewLine();
   269 }
   270 
   271 void COpenKey::DoCheckResult(TInt aError)
   272 {
   273 	if (iFinished)
   274 	{
   275 		TBuf<256> buf;
   276 		if (aError == KErrNone)
   277 		{
   278 			_LIT(KSuccessful, "Key open success\n");			
   279 			buf.Format(KSuccessful);
   280 			iConsole.Write(buf);
   281 			iOut.writeString(buf);
   282 			iOut.writeNewLine();
   283 		}
   284 		else
   285 		{
   286 			if (aError!=iExpectedResult)
   287 			{
   288 				_LIT(KFailed, "!!!Key open failure %d!!!\n");
   289 				buf.Format(KFailed, aError);
   290 				iConsole.Write(buf);
   291 				iOut.writeString(buf);
   292 			}
   293 			else
   294 			{
   295 				_LIT(KFailed, "Key open failed, but expected\n");
   296 				iConsole.Write(KFailed);
   297 				iOut.writeString(KFailed);
   298 			}
   299 
   300 			iOut.writeNewLine();
   301 		}
   302 	}
   303 }
   304 
   305 COpenKey::COpenKey(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   306 :	CKeyStoreTestAction(aFs, aConsole, aOut)
   307 {}
   308 
   309 void COpenKey::ConstructL(const TTestActionSpec& aTestActionSpec)
   310 	{
   311 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
   312 	
   313 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   314 	TInt pos = 0, err = 0;
   315 	TPtrC8 operationType = Input::ParseElement(aTestActionSpec.iActionBody, KOperationTypeStart, KOperationTypeEnd, pos, err);
   316 	if(operationType.Compare(_L8("sign")) == 0)
   317 		{
   318 		iOperationType = ESigning;
   319 		}
   320 	else if (operationType.Compare(_L8("decrypt")) == 0)
   321 		{
   322 		iOperationType = EDecryption;
   323 		}
   324 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   325 	
   326 	iState = EListKeysPreOpen;
   327 
   328 	}
   329 
   330 ////////////////////////////////////
   331 // CSign
   332 ////////////////////////////////////
   333 /*static*/ CTestAction* CSign::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
   334 {
   335 	CTestAction* self = CSign::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   336 	CleanupStack::Pop(self);
   337 	return self;
   338 }
   339 
   340 /*static*/ CTestAction* CSign::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
   341 {
   342 	CSign* self = new (ELeave) CSign(aFs, aConsole, aOut);
   343 	CleanupStack::PushL(self);
   344 	self->ConstructL(aTestActionSpec);
   345 	return self;
   346 }
   347 
   348 CSign::~CSign()
   349 {
   350 	iKeys.Close();
   351 	delete iReadText;
   352 	delete iExportedPublicKey;
   353 	delete iRSASignature;
   354 	delete iDSASignature;
   355 	delete iHash;
   356 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   357     delete iSigner;
   358     delete iSpiSignature;
   359 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   360 }
   361 
   362 void CSign::PerformAction(TRequestStatus& aStatus)
   363 {
   364 	// Jump straight to final state if an error occured 
   365 	if (aStatus.Int()!=KErrNone)
   366 		{
   367 		iState=EFinished;
   368 		}
   369 		
   370 	switch (iState)
   371 		{	
   372 		case EListKeysPreOpen:
   373 			{            
   374 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   375 			ASSERT(keyStore);	//	Flag it up
   376 			if (keyStore)
   377 				keyStore->List(iKeys, iFilter, aStatus);
   378 			iState = EOpenKey;
   379 
   380 			}
   381 			break;
   382 		
   383 		case EOpenKey:
   384 			{
   385 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   386 			ASSERT(keyStore);	//	Flag it up
   387             TInt keyCount = iKeys.Count();
   388 
   389             TInt i;
   390             for (i = 0; i < keyCount; i++)
   391                 {
   392                 CCTKeyInfo* keyInfo = iKeys[i];
   393 
   394                 if (keyInfo->Label() == *iLabel)
   395                     {
   396                     switch (iType)
   397                         {
   398 						case ERSASign:
   399 							keyStore->Open(*keyInfo, iRSASigner, aStatus);
   400 							break;
   401 						case EDSASign:
   402 							keyStore->Open(*keyInfo, iDSASigner, aStatus);
   403 							break;
   404 						case EDH:
   405 						case EDecrypt:
   406 							break;	//	Nothing to do, for the compiler
   407 						
   408 						#if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   409 						case EECC:
   410 							{
   411 							iTokenHandle = keyInfo->Handle();
   412 							if(iHardwareType == 0)
   413 								{
   414 								keyStore->Open(iTokenHandle, iSigner, aStatus);
   415 								}
   416 							else
   417 								{
   418 								TRequestStatus* status = &aStatus;
   419 								User::RequestComplete(status, KErrNone);
   420 								}
   421 							}
   422 							break;
   423 						#endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   424                         }
   425                     } // if
   426                 if (i == keyCount)
   427 					{
   428 					TRequestStatus* status = &aStatus;
   429 					User::RequestComplete(status, KErrNotFound);
   430 					}		
   431                 }// for
   432 			iState = ESign;
   433 						
   434 		}
   435 		break;
   436 
   437 		case ESign:
   438 			{
   439 			switch (iType)
   440 				{
   441 				case ERSASign:
   442 				{
   443 					if (iHash)
   444 					{
   445 					    if (iFailHashDigest)	//	Don't hash it, fail deliberately
   446 							iRSASigner->Sign(*iReadText,iRSASignature,aStatus);
   447 						else  // message gets signed by the keystore
   448 							iRSASigner->SignMessage(*iReadText,iRSASignature,aStatus);
   449 					}
   450 					else
   451 					{
   452 						iRSASigner->Sign(*iReadText,iRSASignature,aStatus);
   453 					}
   454 				}
   455 				break;
   456 				case EDSASign:
   457 				{
   458 					if (iHash)
   459 					{
   460 					    if (iFailHashDigest)	//	Don't hash it, deliberately fail it
   461 							iDSASigner->Sign(*iReadText,iDSASignature,aStatus);
   462 						else  // message gets signed by the keystore
   463 							iDSASigner->SignMessage(*iReadText,iDSASignature,aStatus);
   464 					}
   465 					else
   466 					{
   467 						iDSASigner->Sign(*iReadText,iDSASignature,aStatus);
   468 					}
   469 				}
   470 				break;
   471 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   472                 case EECC:
   473                     {
   474                     TRAPD(err, iSpiSignature = CryptoSpi::CCryptoParams::NewL());
   475                     if(err == KErrNone)
   476                         {
   477                         if(iHardwareType)
   478                             {
   479                             CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   480                             ASSERT(keyStore);   //  Flag it up
   481                             keyStore->Sign(iTokenHandle, *iReadText, iSpiSignature, aStatus);
   482                             }
   483                         else
   484                             {
   485                             if(iSigner)
   486                                 {
   487                                 TRAP(err, iSigner->SignL(*iReadText, *iSpiSignature));
   488                                 aStatus = err;
   489                                 }
   490                             TRequestStatus* status = &aStatus;
   491                             User::RequestComplete(status, aStatus.Int());
   492                             }
   493                         }
   494                     else
   495                         {
   496                         aStatus = err;
   497                         TRequestStatus* status = &aStatus;
   498                         User::RequestComplete(status, aStatus.Int());
   499                         }
   500                     }
   501                 break;
   502 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   503 
   504 				default:
   505 					ASSERT(EFalse);
   506 			}					
   507 			iState = EExportPublic;					
   508 		}
   509 		break;
   510 
   511 	
   512 		case EExportPublic:
   513 			{
   514 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   515 			ASSERT(keyStore);   //  Flag it up              
   516 
   517 			TInt keyCount = iKeys.Count();
   518 			TInt i;
   519 			for (i = 0; i < keyCount; i++)
   520 				{
   521 				CCTKeyInfo* keyInfo = iKeys[i];
   522 
   523 				if (keyInfo->Label() == *iLabel)
   524 					{
   525 					iExportHandle = keyInfo->Handle();
   526 
   527 					switch (iType)
   528 						{
   529 						case ERSASign:
   530 							keyStore->ExportPublic(iExportHandle, iExportedPublicKey,  aStatus);
   531 							break;
   532 						case EDSASign:
   533 							keyStore->ExportPublic(iExportHandle, iExportedPublicKey,  aStatus); 
   534 							break;
   535 						case EDH:
   536 						case EDecrypt:
   537 							break;	//	Nothing to do, for the compiler
   538 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   539 						case EECC:
   540 							{
   541 							keyStore->ExportPublic(iExportHandle, iExportedPublicKey, aStatus);
   542 							iState = EVerify;
   543 							}
   544 							break;
   545 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   546 						}
   547 					break;
   548 					}
   549 				}
   550 			iState = EVerify;
   551 			if (i == keyCount)
   552 				{
   553 				TRequestStatus* status = &aStatus;
   554 				User::RequestComplete(status, KErrNotFound);
   555 				}
   556 			}
   557 			break;
   558 
   559 		case EVerify:
   560 			{
   561 			TInt keyCount = iKeys.Count();
   562 			TInt i;
   563 			for (i = 0; i < keyCount; i++)
   564 				{
   565 				CCTKeyInfo* keyInfo = iKeys[i];
   566 
   567 				if (keyInfo->Label() == *iLabel)
   568 					{
   569 					iExportHandle = keyInfo->Handle();
   570 					CX509SubjectPublicKeyInfo* ki = NULL;
   571 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   572 					if(iType != EECC)
   573 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   574 						ki = CX509SubjectPublicKeyInfo::NewLC(*iExportedPublicKey);
   575 
   576 					switch (iType)
   577 						{
   578 					case ERSASign:
   579 						{
   580 						TX509KeyFactory factory; 
   581 						CRSAPublicKey* key = factory.RSAPublicKeyL(ki->KeyData());
   582 						CleanupStack::PushL(key);
   583 						
   584 						CRSAPKCS1v15Verifier* verifier = NULL;
   585 						
   586 						if (iHash)	//	Must compare with hash of original data
   587 						{
   588 							verifier = CRSAPKCS1v15Verifier::NewLC(*key);
   589 							iHash->Reset();
   590 							iVerifyResult = verifier->VerifyL(iHash->Final(*iReadText), *iRSASignature);
   591 						}
   592 						else
   593 						{
   594 							verifier = CRSAPKCS1v15Verifier::NewLC(*key);
   595 							iVerifyResult = verifier->VerifyL(*iReadText, *iRSASignature);
   596 						}
   597 												
   598 						_LIT(KReturned, "Returned... ");
   599 						iOut.writeString(KReturned);
   600 						iOut.writeNewLine();
   601 
   602 						CleanupStack::PopAndDestroy(2, key);
   603 						}
   604 						break;
   605 					case EDSASign:
   606 						{
   607 						TX509KeyFactory factory;
   608 						CDSAPublicKey* key = factory.DSAPublicKeyL(ki->EncodedParams(), ki->KeyData());
   609 						CleanupStack::PushL(key);
   610 						
   611 						CDSAVerifier* verifier = CDSAVerifier::NewLC(*key);
   612 						if (iHash)	//	Must compare with hash of original value
   613 						{
   614 							iHash->Reset();
   615 							iVerifyResult = verifier->VerifyL(iHash->Final(*iReadText),*iDSASignature);
   616 						}
   617 						else
   618 						{
   619 							iVerifyResult = verifier->VerifyL(*iReadText,*iDSASignature);
   620 						}
   621 						_LIT(KReturned, "Returned... ");
   622 						iOut.writeString(KReturned);
   623 						iOut.writeNewLine();
   624 						CleanupStack::PopAndDestroy(verifier);
   625 						CleanupStack::PopAndDestroy(key);
   626 						}
   627 						break;
   628 				
   629 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   630 					case EECC:
   631 						{
   632 						iVerifyResult = EFalse;
   633 						if (iSpiSignature && iSpiSignature->IsPresent(CryptoSpi::KEccKeyTypeUid))
   634 							{
   635 							TDesC8* actualSignature = NULL;
   636 							TRAPD(err, actualSignature = const_cast<TDesC8*>(&(iSpiSignature->GetTDesC8L(CryptoSpi::KEccKeyTypeUid))));
   637 							if(err == KErrNone)
   638 							    {
   639 							    if(iExportedPublicKey->Des() == *actualSignature)
   640 							        {
   641 							        iVerifyResult = ETrue;
   642 							        }
   643 							    }
   644 							else
   645 							    {
   646 							    aStatus = err;
   647 							    }
   648 							}
   649 						_LIT(KReturned, "Returned... ");
   650 						iOut.writeString(KReturned);
   651 						iOut.writeNewLine();
   652 						}
   653 					break;
   654 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC	
   655 					default:
   656 						ASSERT(EFalse);
   657 
   658 						}
   659 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   660 					if(iType != EECC)
   661 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   662 						CleanupStack::PopAndDestroy(ki);
   663 					}
   664 				}
   665 
   666 			iState = EFinished;
   667 			TRequestStatus* status = &aStatus;
   668 			if (!iVerifyResult)		
   669 				{
   670 				_LIT(KVerifyFail, "**Verify failed**");
   671 				iOut.writeString(KVerifyFail);
   672 				iOut.writeNewLine();
   673 
   674 				// Flat verify failed as KErrGeneral
   675 				if (aStatus.Int() == KErrNone)
   676 					{
   677 					aStatus = KErrGeneral;
   678 					}
   679 				}
   680 			User::RequestComplete(status, aStatus.Int());
   681 			}
   682 			break;
   683 		
   684 		case EFinished:
   685 		{
   686 		iActionState = EPostrequisite;
   687 		iResult = (aStatus.Int() == iExpectedResult);
   688 			
   689 			if ((aStatus.Int() != KErrNone)&&(aStatus!=KErrNoMemory))
   690 				{
   691 				_LIT(KExportFail," !Failed when exporting public key! ");
   692 				iOut.writeString(KExportFail);
   693 				}
   694 			
   695 			iKeys.Close();
   696 			switch (iType)
   697 				{
   698 			case ERSASign:
   699 				if (iRSASigner)
   700 					iRSASigner->Release();
   701 				break;
   702 			case EDSASign:
   703 				if (iDSASigner)
   704 					iDSASigner->Release();
   705 				break;
   706 			case EDecrypt:
   707 				if (iDecryptor)
   708 					iDecryptor->Release();
   709 			case EDH:
   710 				if (iDH)
   711 					iDH->Release();
   712 				break;
   713 				}
   714 			TRequestStatus* status = &aStatus;
   715 			User::RequestComplete(status, aStatus.Int());
   716 		}
   717 		break;
   718 		default:
   719 			ASSERT(EFalse);	
   720 	}
   721 }
   722 
   723 void CSign::PerformCancel()
   724     {
   725     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   726     ASSERT(keystore);
   727     
   728     switch (iState)
   729         {
   730         case EOpenKey:
   731             keystore->CancelList();
   732             break;
   733 
   734         case ESign:
   735             keystore->CancelOpen();
   736             break;
   737 
   738         case EExportPublic:
   739 			switch (iType)
   740                 {
   741 				case ERSASign:
   742                     ASSERT(iRSASigner);
   743                     iRSASigner->CancelSign();
   744                     break;
   745 
   746                 case EDSASign:
   747                     ASSERT(iDSASigner);
   748                     iDSASigner->CancelSign();
   749                     break;
   750 
   751                 default:
   752                     break;
   753                 }
   754             break;
   755 
   756         case EVerify:            
   757             keystore->CancelExportPublic();
   758             break;
   759 
   760         default:
   761             break;
   762         }
   763     }
   764 
   765 void CSign::Reset()
   766 	{
   767 	iState = EListKeysPreOpen;
   768 	iKeys.Close();
   769     if (iRSASigner)
   770         {
   771         iRSASigner->Release();
   772         iRSASigner = NULL;        
   773         }
   774     if (iDSASigner)
   775         {
   776         iDSASigner->Release();
   777         iDSASigner = NULL;        
   778         }
   779 	delete iExportedPublicKey;
   780 	iExportedPublicKey = NULL;
   781 	delete iRSASignature;
   782 	iRSASignature = NULL;
   783 	delete iDSASignature;
   784 	iDSASignature = NULL;
   785 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   786 	delete iSigner;
   787 	iSigner = NULL;
   788 	delete iSpiSignature;
   789 	iSpiSignature = NULL;
   790 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   791 	}
   792 
   793 void CSign::DoReportAction()
   794 {
   795 	_LIT(KSigning, "Signing... ");
   796 	iOut.writeString(KSigning);
   797 	iOut.writeNewLine();
   798 }
   799 
   800 void CSign::DoCheckResult(TInt aError)
   801 {
   802 	if (iFinished)
   803 	{
   804 		TBuf<256> buf;
   805 		if (aError == KErrNone)
   806 		{
   807 			iOut.writeNewLine();
   808 			_LIT(KSuccessful, "Sign success\n");			
   809 			buf.Format(KSuccessful);
   810 			iConsole.Write(buf);
   811 			iOut.writeString(buf);
   812 			iOut.writeNewLine();
   813 		}
   814 		else
   815 		{
   816 			if (aError!=iExpectedResult)
   817 			{
   818 				_LIT(KFailed, "!!!Sign failure %d!!!\n");
   819 				buf.Format(KFailed, aError);
   820 				iConsole.Write(buf);
   821 				iOut.writeString(buf);
   822 			}
   823 			else
   824 			{
   825 				_LIT(KFailed, "Sign failed, but expected\n");
   826 				iConsole.Write(KFailed);
   827 				iOut.writeString(KFailed);
   828 			}
   829 
   830 			iOut.writeNewLine();
   831 		}
   832 	}
   833 }
   834 
   835 CSign::CSign(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   836 :	CKeyStoreTestAction(aFs, aConsole, aOut)
   837 {}
   838 
   839 void CSign::ConstructL(const TTestActionSpec& aTestActionSpec)
   840 {
   841 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
   842 
   843 	SetDigestSignL(Input::ParseElement(aTestActionSpec.iActionBody, KSignDigestStart));
   844 	SetSignText(Input::ParseElement(aTestActionSpec.iActionBody, KTextStart, KTextEnd));
   845 
   846 	iFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
   847 	
   848 	iState = EListKeysPreOpen;
   849 		
   850 }
   851 
   852 void CSign::SetDigestSignL(const TDesC8& aSignDigestDesc)
   853 {
   854 	TLex8 lexer(aSignDigestDesc);
   855 	TInt digest = 0;
   856 	lexer.Val(digest);
   857 
   858 	if (digest > 0)
   859 		iHash = CMessageDigestFactory::NewDigestL(CMessageDigest::ESHA1);
   860 
   861 	if (digest==2)
   862 		iFailHashDigest = ETrue;
   863 }
   864 
   865 void CSign::SetSignText(const TDesC8& aText)
   866 {
   867 	iReadText = HBufC8::NewMax(aText.Size());
   868 	if (iReadText)
   869 	{
   870 		TPtr8 theText(iReadText->Des());
   871 		theText.FillZ();
   872 		theText.Copy(aText);
   873 	}
   874 }
   875 
   876 ////////////////////////////////////
   877 // CDecrypt
   878 ////////////////////////////////////
   879 /*static*/ CTestAction* CDecrypt::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
   880 {
   881 	CTestAction* self = CDecrypt::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   882 	CleanupStack::Pop(self);
   883 	return self;
   884 }
   885 
   886 /*static*/ CTestAction* CDecrypt::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
   887 {
   888 	CDecrypt* self = new (ELeave) CDecrypt(aFs, aConsole, aOut);
   889 	CleanupStack::PushL(self);
   890 	self->ConstructL(aTestActionSpec);
   891 	return self;
   892 }
   893 
   894 CDecrypt::~CDecrypt()
   895 {
   896 	iKeys.Close();
   897 	delete iReadText;
   898 	delete iPlainText;
   899 	delete iPublic;
   900 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   901     delete iDecrypt;
   902 #endif
   903 }
   904 
   905 void CDecrypt::PerformAction(TRequestStatus& aStatus)
   906 {
   907 	// Jump straight to final state if an error occured 
   908 	if (aStatus.Int()!=KErrNone)
   909 		{
   910 		iState=EFinished;
   911 		}
   912 
   913 	switch (iState)
   914 	{	
   915 		case EListKeysPreOpen:
   916 			{//	Currently uses the first store, change to check the script for a specific store
   917 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   918 			ASSERT(keyStore);	//	Flag it up
   919 			if (keyStore)
   920 				keyStore->List(iKeys, iFilter, aStatus);
   921 				
   922 			iState = EOpenKey;
   923 			}
   924 		break;
   925 		
   926 		case EOpenKey:
   927 		{
   928 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   929 			ASSERT(keyStore);	//	Flag it up
   930 
   931 			TInt keyCount = iKeys.Count();
   932 			TInt i;
   933 			for (i = 0; i < keyCount; i++)
   934 				{
   935 				CCTKeyInfo* keyInfo = iKeys[i];
   936 
   937 				if (keyInfo->Label() == *iLabel)
   938 					{
   939 					switch(iType)
   940 						{
   941 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
   942 						case EECC:
   943 						if(iHardwareType == 0)
   944 							{
   945 							keyStore->Open(keyInfo->Handle(), iDecrypt, aStatus);
   946 							}
   947 						else if(iHardwareType == 1 )
   948 							{
   949 							/**
   950 							 * Call the decrypt of hardware directly. iPlainText 
   951 							 * would be populated.
   952 							 */
   953 							keyStore->Decrypt(keyInfo->Handle(), *iReadText, iPlainText, aStatus);
   954 							}
   955 						break;
   956 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
   957 					default:
   958 						{
   959 						keyStore->Open(*keyInfo, iDecryptor, aStatus);
   960 						}
   961 					} // switch
   962 					break;
   963 					} // if
   964 				} // for
   965 			
   966 			iState = EExportPublic;
   967 			if (i == keyCount)
   968 				{
   969 				TRequestStatus* status = &aStatus;
   970 				User::RequestComplete(status, KErrNotFound);
   971 				}
   972 		}
   973 		break;
   974 
   975 		case EExportPublic:
   976 		{
   977 			if (aStatus.Int()!=KErrNone)
   978 				{
   979 				_LIT(KSignFail," !Failed when opening! ");
   980 				iOut.writeString(KSignFail);
   981 				iState=EFinished;
   982 				// need to set it to true so that test is true if some error was expected
   983 				iVerifyResult=ETrue;
   984 				TRequestStatus* status = &aStatus;
   985 				User::RequestComplete(status, aStatus.Int());
   986 				break;
   987 				}
   988 
   989 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   990 			ASSERT(keyStore);	//	Flag it up
   991 
   992 			TInt keyCount = iKeys.Count();
   993 			TInt i;
   994 			
   995 			for (i = 0; i < keyCount; i++)
   996 				{
   997 				CCTKeyInfo* keyInfo = iKeys[i];
   998 
   999 				if (keyInfo->Label() == *iLabel)
  1000 					{
  1001 					iExportHandle = keyInfo->Handle();
  1002 					
  1003 					keyStore->ExportPublic(iExportHandle, iPublic,  aStatus);
  1004 					break;
  1005 					}
  1006 				}
  1007 			iState = EDecrypt;
  1008 			if (i == keyCount)
  1009 				{
  1010 				TRequestStatus* status = &aStatus;
  1011 				User::RequestComplete(status, KErrNotFound);
  1012 				}
  1013 		}
  1014 		break;
  1015 
  1016 		case EDecrypt:
  1017 			{
  1018 			
  1019 			if (aStatus.Int()!=KErrNone)
  1020 				{
  1021 				iState=EFinished;
  1022 				TRequestStatus* status = &aStatus;
  1023 				User::RequestComplete(status, aStatus.Int());
  1024 				break;
  1025 				}
  1026 			switch(iType)
  1027 				{
  1028 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
  1029 				case EECC:
  1030 					{
  1031 					if(iHardwareType)
  1032 						{
  1033 						if(*iPlainText == *iPublic)
  1034 							{
  1035 							aStatus = KErrNone;
  1036 							}
  1037 						else
  1038 							{
  1039 							aStatus = KErrGeneral;
  1040 							}
  1041 						}
  1042 					else
  1043 						{
  1044                         aStatus = KErrGeneral;
  1045                         if (iDecrypt)
  1046                             {
  1047                             HBufC8* decryptedText = NULL;
  1048                             TRAPD(err, decryptedText = HBufC8::NewL(iDecrypt->GetMaximumOutputLengthL()));
  1049                             
  1050                             if (err == KErrNone)
  1051                                 {
  1052                                 CleanupStack::PushL(decryptedText);
  1053                                 TPtr8 decryptedTextPtr = decryptedText->Des();
  1054 
  1055                                 TRAP(err, (iDecrypt->ProcessL(*iReadText, decryptedTextPtr)));
  1056                                 if(err != KErrNone)
  1057                                     {
  1058                                     aStatus = err;
  1059                                     }
  1060                                 if ((err == KErrNone) && (decryptedTextPtr
  1061                                         == iPublic->Des()))
  1062                                     {
  1063                                     aStatus = KErrNone;
  1064                                     }
  1065                                 CleanupStack::PopAndDestroy(decryptedText);
  1066                                 }
  1067                             else
  1068                                 {
  1069                                 aStatus = err;
  1070                                 }
  1071                             }
  1072                         }
  1073 					iState = EFinished;
  1074 					TRequestStatus* status = &aStatus;
  1075 					User::RequestComplete(status, aStatus.Int());
  1076 					}
  1077 					break;
  1078 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
  1079 				default:
  1080 					{
  1081 					CX509SubjectPublicKeyInfo* ki = 
  1082 						CX509SubjectPublicKeyInfo::NewLC(*iPublic);
  1083 		
  1084 					TX509KeyFactory factory; 
  1085 					CRSAPublicKey* key = factory.RSAPublicKeyL(ki->KeyData());
  1086 					CleanupStack::PushL(key);
  1087 								
  1088 				//	Encrypt with public key
  1089 					CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewLC(*key);
  1090 					HBufC8* cipherText = HBufC8::NewLC(encryptor->MaxOutputLength());
  1091 					TPtr8 cipherTextPtr = cipherText->Des();
  1092 		
  1093 					encryptor->EncryptL(*iReadText, cipherTextPtr);
  1094 					
  1095 				//	Now decrypt again
  1096 					iPlainText = HBufC8::NewMaxL(100);
  1097 					iPlainTextPtr.Set(iPlainText->Des());
  1098 					iDecryptor->Decrypt(*cipherText, iPlainTextPtr, aStatus);
  1099 					
  1100 					CleanupStack::PopAndDestroy(cipherText);
  1101 					CleanupStack::PopAndDestroy(encryptor);
  1102 					CleanupStack::PopAndDestroy(key);
  1103 					CleanupStack::PopAndDestroy(ki);
  1104 					
  1105 					iState = EFinished;
  1106 					}
  1107 				} // switch
  1108 		}
  1109 		break;
  1110 
  1111 		case EFinished:
  1112 			{
  1113 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
  1114 			if(iType == EECC)
  1115 				{
  1116 				iActionState = EPostrequisite;
  1117 				iResult = (aStatus.Int() == iExpectedResult);
  1118 					
  1119 				TRequestStatus* status = &aStatus;
  1120 				User::RequestComplete(status, aStatus.Int());
  1121 				}
  1122 			else
  1123 #endif // SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT && SYMBIAN_ENABLE_SDP_ECC
  1124 				{
  1125 				if (aStatus == KErrNone && (!iPlainText || 
  1126 											*iPlainText != *iReadText))
  1127 					{
  1128 					aStatus = KErrGeneral;	//	Decryption failed
  1129 					}
  1130 				
  1131 				iActionState = EPostrequisite;
  1132 				iResult = (aStatus.Int() == iExpectedResult);
  1133 				
  1134 				if (iDecryptor)
  1135 					{
  1136 					iDecryptor->Release();
  1137 					}
  1138 				TRequestStatus* status = &aStatus;
  1139 				User::RequestComplete(status, aStatus.Int());
  1140 				}
  1141 		}
  1142 		break;
  1143 		default:
  1144 			ASSERT(EFalse);	
  1145 	}
  1146 
  1147 }
  1148 
  1149 void CDecrypt::PerformCancel()
  1150     {
  1151     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
  1152     ASSERT(keystore);
  1153     
  1154     switch (iState)
  1155         {
  1156         case EOpenKey:
  1157             keystore->CancelList();
  1158             break;
  1159 
  1160         case EExportPublic:
  1161             keystore->CancelOpen();
  1162             break;
  1163             
  1164         case EDecrypt:
  1165             keystore->CancelExportPublic();
  1166             break;
  1167 
  1168         case EFinished:
  1169             ASSERT(iDecryptor);
  1170             iDecryptor->CancelDecrypt();
  1171             break;
  1172             
  1173         default:
  1174             break;
  1175         }
  1176     }
  1177 
  1178 void CDecrypt::Reset()
  1179 	{
  1180 	iState = EListKeysPreOpen;
  1181 	iKeys.Close();
  1182     if (iDecryptor)
  1183         {
  1184         iDecryptor->Release();
  1185         iDecryptor = NULL;
  1186         }
  1187 	delete iPlainText;
  1188 	iPlainText = NULL;
  1189 	delete iPublic;
  1190 	iPublic = NULL;
  1191 #if (defined(SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT) && defined(SYMBIAN_ENABLE_SDP_ECC))
  1192     delete iDecrypt;
  1193     iDecrypt = NULL;
  1194 #endif
  1195 	}
  1196 
  1197 void CDecrypt::DoReportAction()
  1198 	{
  1199 	_LIT(KSigning, "Decrypting... ");
  1200 	iOut.writeString(KSigning);
  1201 	iOut.writeNewLine();
  1202 	}
  1203 
  1204 void CDecrypt::DoCheckResult(TInt aError)
  1205 {
  1206 	if (iFinished)
  1207 	{
  1208 		TBuf<256> buf;
  1209 		if (aError == KErrNone)
  1210 		{
  1211 			iOut.writeNewLine();
  1212 			_LIT(KSuccessful, "Decrypt success\n");			
  1213 			buf.Format(KSuccessful);
  1214 			iConsole.Write(buf);
  1215 			iOut.writeString(buf);
  1216 			iOut.writeNewLine();
  1217 		}
  1218 		else
  1219 		{
  1220 			if (aError!=iExpectedResult)
  1221 			{
  1222 				_LIT(KFailed, "!!!Decrypt failure %d!!!\n");
  1223 				buf.Format(KFailed, aError);
  1224 				iConsole.Write(buf);
  1225 				iOut.writeString(buf);
  1226 			}
  1227 			else
  1228 			{
  1229 				_LIT(KFailed, "Decrypt failed, but expected\n");
  1230 				iConsole.Write(KFailed);
  1231 				iOut.writeString(KFailed);
  1232 			}
  1233 
  1234 			iOut.writeNewLine();
  1235 		}
  1236 	}
  1237 }
  1238 
  1239 CDecrypt::CDecrypt(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
  1240 		:	CKeyStoreTestAction(aFs, aConsole, aOut), iPlainTextPtr(0,0)
  1241 {}
  1242 
  1243 void CDecrypt::ConstructL(const TTestActionSpec& aTestActionSpec)
  1244 {
  1245 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
  1246 
  1247 	SetSignText(Input::ParseElement(aTestActionSpec.iActionBody, KTextStart));
  1248 	iFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
  1249 
  1250 	iState = EListKeysPreOpen;
  1251 }
  1252 
  1253 void CDecrypt::SetSignText(const TDesC8& aText)
  1254 {
  1255 	iReadText = HBufC8::NewMax(aText.Size());
  1256 	if (iReadText)
  1257 	{
  1258 		TPtr8 theText(iReadText->Des());
  1259 		theText.FillZ();
  1260 		theText.Copy(aText);
  1261 	}
  1262 }
  1263 
  1264 ////////////////////////////////////
  1265 // CDerive
  1266 ////////////////////////////////////
  1267 
  1268 /*static*/ CTestAction* CDerive::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
  1269 {
  1270 	CTestAction* self = CDerive::NewLC(aFs, aConsole, aOut, aTestActionSpec);
  1271 	CleanupStack::Pop(self);
  1272 	return self;
  1273 }
  1274 
  1275 /*static*/ CTestAction* CDerive::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
  1276 {
  1277 	CDerive* self = new (ELeave) CDerive(aFs, aConsole, aOut);
  1278 	CleanupStack::PushL(self);
  1279 	self->ConstructL(aTestActionSpec);
  1280 	return self;
  1281 }
  1282 
  1283 CDerive::~CDerive()
  1284 	{
  1285 	iKeys.Close();
  1286 	iN.Close();
  1287 	iG.Close();
  1288 	if (iDH)
  1289 		{
  1290 		iDH->Release();
  1291 		}
  1292 	delete iPublicKey;
  1293 	delete iOutput;
  1294 	delete iRemote;
  1295 	}
  1296 
  1297 void CDerive::PerformAction(TRequestStatus& aStatus)
  1298 	{
  1299 	// Jump straight to final state if an error occured 
  1300 	if (aStatus.Int()!=KErrNone)
  1301 		{
  1302 		iState=EFinished;
  1303 		}
  1304  
  1305 	switch (iState)
  1306 		{	
  1307 		case EListKeysPreOpen:
  1308 			{
  1309 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
  1310 			ASSERT(keyStore);	//	Flag it up
  1311 			if (keyStore)
  1312 				keyStore->List(iKeys, iFilter, aStatus);
  1313 			
  1314 			iState = EOpenKey;
  1315 			}
  1316 			break;
  1317 		
  1318 		case EOpenKey:
  1319 			{
  1320 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
  1321 			ASSERT(keyStore);	//	Flag it up
  1322 			
  1323 			TInt keyCount = iKeys.Count();
  1324 
  1325 			TInt i;
  1326 			for (i = 0; i < keyCount; i++)
  1327 				{
  1328 				CCTKeyInfo* keyInfo = iKeys[i];
  1329 
  1330 				if (keyInfo->Label() == *iLabel)
  1331 					{
  1332 					keyStore->Open(*keyInfo, iDH, aStatus);
  1333 					break;
  1334 					}
  1335 				}
  1336 			iState = EExportPublic;
  1337 			if (i == keyCount)
  1338 				{
  1339 				TRequestStatus* status = &aStatus;
  1340 				User::RequestComplete(status, KErrNotFound);
  1341 				}
  1342 			}
  1343 			break;
  1344 
  1345 		case EExportPublic:
  1346 			{
  1347 			ASSERT(iDH);
  1348 			iDH->PublicKey(iN, iG, iPublicKey, aStatus);
  1349 			iState = EDerive;
  1350 			}
  1351 			break;
  1352 
  1353 		case EDerive:
  1354 			{
  1355 			if (aStatus.Int()!=KErrNone)
  1356 				{
  1357 				iState=EFinished;
  1358 				TRequestStatus* status = &aStatus;
  1359 				User::RequestComplete(status, aStatus.Int());
  1360 				break;
  1361 				}
  1362 
  1363 			ASSERT(iPublicKey);
  1364 			iDH->Agree(iRemote->PublicKey(), iOutput, aStatus);
  1365 			
  1366 			iState = EFinished;
  1367 			}
  1368 			break;
  1369 
  1370 		case EFinished:
  1371 			{
  1372 			if (aStatus.Int()==KErrNone)
  1373 				{
  1374 				CDH* dh = CDH::NewLC(iRemote->PrivateKey());
  1375 				const HBufC8* output = dh->AgreeL(*iPublicKey);
  1376 				CleanupStack::PopAndDestroy(dh);
  1377 			
  1378 				if (aStatus == KErrNone && (!iOutput || 
  1379 											*iOutput != *output))
  1380 					{
  1381 					aStatus = KErrGeneral;	//	Agree failed
  1382 					}
  1383 
  1384 				delete const_cast<HBufC8*>(output);
  1385 				}
  1386 			
  1387 			iActionState = EPostrequisite;
  1388 			iResult = (aStatus.Int() == iExpectedResult);
  1389 
  1390 			if (aStatus != KErrNone)
  1391 				{
  1392 				_LIT(KSignFail," !Failed when agreeing key!");
  1393 				iOut.writeString(KSignFail);
  1394 				}
  1395 
  1396 			TRequestStatus* status = &aStatus;
  1397 			User::RequestComplete(status, aStatus.Int());
  1398 			}
  1399 			break;
  1400 		default:
  1401 			ASSERT(EFalse);	
  1402 		}
  1403 	}
  1404 
  1405 void CDerive::PerformCancel()
  1406     {
  1407     CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
  1408     ASSERT(keystore);
  1409     
  1410     switch (iState)
  1411         {
  1412         case EOpenKey:
  1413             keystore->CancelList();
  1414             break;
  1415 
  1416         case EExportPublic:
  1417             keystore->CancelOpen();
  1418             break;
  1419             
  1420         case EDerive:
  1421             keystore->CancelExportPublic();
  1422             break;
  1423 
  1424         case EFinished:
  1425             ASSERT(iDH);
  1426             iDH->CancelAgreement();
  1427             break;
  1428             
  1429         default:
  1430             break;
  1431         }
  1432     }
  1433 
  1434 void CDerive::Reset()
  1435 	{
  1436 	iState = EListKeysPreOpen;
  1437 	iKeys.Close();
  1438 	if (iDH)
  1439 		{
  1440 		iDH->Release();
  1441 		iDH = NULL;
  1442 		}
  1443 	delete iPublicKey;
  1444 	iPublicKey = NULL;
  1445 	delete iOutput;
  1446 	iOutput = NULL;
  1447 	}
  1448 
  1449 void CDerive::DoReportAction()
  1450 {
  1451 	_LIT(KSigning, "Deriving key... ");
  1452 	iOut.writeString(KSigning);
  1453 	iOut.writeNewLine();
  1454 }
  1455 
  1456 void CDerive::DoCheckResult(TInt aError)
  1457 {
  1458 	if (iFinished)
  1459 	{
  1460 		TBuf<256> buf;
  1461 		if (aError == KErrNone)
  1462 		{
  1463 			iOut.writeNewLine();
  1464 			_LIT(KSuccessful, "Derive success\n");			
  1465 			buf.Format(KSuccessful);
  1466 			iConsole.Write(buf);
  1467 			iOut.writeString(buf);
  1468 			iOut.writeNewLine();
  1469 		}
  1470 		else
  1471 		{
  1472 			if (aError!=iExpectedResult)
  1473 			{
  1474 				_LIT(KFailed, "!!!Derive failure %d!!!\n");
  1475 				buf.Format(KFailed, aError);
  1476 				iConsole.Write(buf);
  1477 				iOut.writeString(buf);
  1478 			}
  1479 			else
  1480 			{
  1481 				_LIT(KFailed, "Derive failed, but expected\n");
  1482 				iConsole.Write(KFailed);
  1483 				iOut.writeString(KFailed);
  1484 			}
  1485 
  1486 			iOut.writeNewLine();
  1487 		}
  1488 	}
  1489 }
  1490 
  1491 CDerive::CDerive(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
  1492 		:	CKeyStoreTestAction(aFs, aConsole, aOut)
  1493 {}
  1494 
  1495 void CDerive::ConstructL(const TTestActionSpec& aTestActionSpec)
  1496 {
  1497 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
  1498 
  1499 	// Set parameters
  1500 	HBufC8* nData = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<n>"));
  1501 	CleanupStack::PushL(nData);
  1502 	iN = RInteger::NewL(*nData);
  1503 	CleanupStack::PopAndDestroy(nData);
  1504 	HBufC8* gData = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<g>"));
  1505 	CleanupStack::PushL(gData);
  1506 	iG = RInteger::NewL(*gData);
  1507 	CleanupStack::PopAndDestroy(gData);
  1508 	
  1509 	// Generate 'remote' key pair
  1510 	RInteger n = RInteger::NewL(iN);
  1511 	CleanupStack::PushL(n);
  1512 	RInteger g = RInteger::NewL(iG);
  1513 	CleanupStack::PushL(g);
  1514 	iRemote = CDHKeyPair::NewL(n, g);
  1515 	CleanupStack::Pop(2); // n, g
  1516 	
  1517 	iFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
  1518 	
  1519 	iState = EListKeysPreOpen;
  1520 }