cryptomgmtlibs/cryptotokenfw/tsecdlg/Tsecdlg.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2001-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 * secdlgImpl.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "Tsecdlg.h"
       
    21 #include <secdlgimpldefs.h>
       
    22 #include <certificateapps.h>
       
    23 #include <x509cert.h>
       
    24 
       
    25 
       
    26 // These are not really allocated to us, but this is only for finding errors
       
    27 // while debugging, so it doesn't really matter
       
    28 const TInt KErrTooManyDialogs			= -12000;
       
    29 const TInt KErrLabelMismatch 			= -12001;
       
    30 const TInt KErrOperationMismatch		= -12002;
       
    31 const TInt KErrOperationNotSupported	= -12003;
       
    32 
       
    33 _LIT(KpinValue,"pinkcloud");
       
    34 _LIT(KYes,"Yes");
       
    35 	
       
    36 // ----------------------------------------------------------------------------
       
    37 // Lib main entry point.
       
    38 // This can leave and should leave (if failure occurs) despite the lack of trailing L.
       
    39 //
       
    40 
       
    41 #ifdef _T_SECDLG_TEXTSHELL
       
    42 EXPORT_C CArrayPtr<MNotifierBase2>* NotifierArray()
       
    43 #else
       
    44 CArrayPtr<MNotifierBase2>* NotifierArray()
       
    45 #endif
       
    46 	{
       
    47 	//The notifierArray function CAN leave, despite no trailing L
       
    48 	CArrayPtrFlat<MNotifierBase2>* subjects = new (ELeave) CArrayPtrFlat<MNotifierBase2>( 1 );
       
    49 	CleanupStack::PushL(subjects);
       
    50 	CTestSecDlgNotifier* notifier = CTestSecDlgNotifier::NewL();
       
    51 	CleanupStack::PushL( notifier );
       
    52 	subjects->AppendL( notifier );
       
    53 	CleanupStack::Pop( 2,subjects);	//notifier, subjects
       
    54 	return subjects;
       
    55 	}
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // Ecom plugin implementation for UI notifier
       
    59 //
       
    60 
       
    61 #ifndef _T_SECDLG_TEXTSHELL
       
    62 
       
    63 const TImplementationProxy ImplementationTable[] =
       
    64 	{
       
    65 		IMPLEMENTATION_PROXY_ENTRY(KTSecDlgNotiferUid, NotifierArray)
       
    66 	};
       
    67 
       
    68 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
    69 	{
       
    70 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
    71 	return (ImplementationTable);
       
    72 	}
       
    73 
       
    74 #endif
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // CInputSpec
       
    78 //
       
    79 
       
    80 CInputSpec::CInputSpec(TSecurityDialogOperation aOp, HBufC* aLabelSpec, HBufC* aResponse1, HBufC* aResponse2) :
       
    81 	iOp(aOp), iLabelSpec(aLabelSpec), iResponse1(aResponse1), iResponse2(aResponse2)
       
    82 	{
       
    83 	}
       
    84 
       
    85 CInputSpec::~CInputSpec()
       
    86 	{
       
    87 	delete iLabelSpec;
       
    88 	delete iResponse1;
       
    89 	delete iResponse2;
       
    90 	}
       
    91 
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // CTestSecDlgNotifier
       
    95 //
       
    96 
       
    97 _LIT(KInputFile, "\\t_secdlg_in.dat");
       
    98 _LIT(KOutputFile, "\\t_secdlg_out.dat");
       
    99 
       
   100 CTestSecDlgNotifier* CTestSecDlgNotifier::NewL()
       
   101 	{
       
   102 	CTestSecDlgNotifier* self=new (ELeave) CTestSecDlgNotifier();
       
   103 	CleanupStack::PushL(self);
       
   104 	self->ConstructL();
       
   105 	CleanupStack::Pop(self);
       
   106 	return self;
       
   107 	}
       
   108 
       
   109 CTestSecDlgNotifier::CTestSecDlgNotifier()
       
   110 	{
       
   111 	iInfo.iUid = KUidSecurityDialogNotifier;
       
   112 	iInfo.iChannel = TUid::Uid(0x00001234); // dummy
       
   113 	iInfo.iPriority = ENotifierPriorityHigh;
       
   114 	}
       
   115 
       
   116 void CTestSecDlgNotifier::ConstructL()
       
   117 	{
       
   118 	User::LeaveIfError(iFs.Connect());
       
   119 	}
       
   120 
       
   121 
       
   122 TInt CTestSecDlgNotifier::GetInputIndexL()
       
   123 	{
       
   124 	RFileReadStream stream;
       
   125 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   126 	TDriveName driveName(sysDrive.Name());
       
   127 	TBuf<128> outputFile (driveName);
       
   128 	outputFile.Append(KOutputFile);
       
   129 	
       
   130 	TInt err = stream.Open(iFs, outputFile, EFileRead | EFileShareExclusive);
       
   131 	// If the file doesn't exist yet just return zero
       
   132 	if (err == KErrNotFound)
       
   133 		{
       
   134 		return 0;
       
   135 		}
       
   136 	User::LeaveIfError(err);
       
   137 	stream.PushL();
       
   138 	TInt index = stream.ReadInt32L();
       
   139 	CleanupStack::PopAndDestroy(); // stream
       
   140 	return index;
       
   141 	}
       
   142 
       
   143 void CTestSecDlgNotifier::WriteDialogCountL(TInt aCount)
       
   144 	{
       
   145 	RFileWriteStream stream;
       
   146 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   147 	TDriveName driveName(sysDrive.Name());
       
   148 	TBuf<128> outputFile (driveName);
       
   149 	outputFile.Append(KOutputFile);
       
   150 	
       
   151 	TInt err = stream.Replace(iFs, outputFile, EFileWrite | EFileShareExclusive);
       
   152 	if (err == KErrNotFound)
       
   153 		{
       
   154 		err = stream.Create(iFs, outputFile, EFileWrite | EFileShareExclusive);
       
   155 		}
       
   156 	User::LeaveIfError(err);
       
   157 	stream.PushL();
       
   158 	stream.WriteInt32L(aCount);
       
   159 	stream.CommitL();
       
   160 	CleanupStack::PopAndDestroy(); // stream	
       
   161 	}
       
   162 
       
   163 CInputSpec* CTestSecDlgNotifier::ReadInputSpecL(TInt aIndex)
       
   164 	{
       
   165 	RFileReadStream stream;
       
   166 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   167 	TDriveName driveName(sysDrive.Name());
       
   168 	TBuf<128> inputFile (driveName);
       
   169 	inputFile.Append(KInputFile);
       
   170 	User::LeaveIfError(stream.Open(iFs, inputFile, EFileRead | EFileShareExclusive));
       
   171 	stream.PushL();
       
   172 
       
   173 	// Update dialog count here so test code can see how many dialogs were
       
   174 	// requested if there were more than expected
       
   175 	WriteDialogCountL(aIndex + 1);
       
   176 	
       
   177 	MStreamBuf* streamBuf = stream.Source();
       
   178 	TInt labelSize, response1Size, response2Size;
       
   179 
       
   180 	// Skip records until we reach the one we want
       
   181 	for (TInt i = 0 ; i < aIndex ; ++i)
       
   182 		{
       
   183 		stream.ReadInt32L();
       
   184 		labelSize = stream.ReadInt32L();
       
   185 		streamBuf->SeekL(MStreamBuf::ERead, EStreamMark, labelSize * 2);
       
   186 		response1Size = stream.ReadInt32L();
       
   187 		streamBuf->SeekL(MStreamBuf::ERead, EStreamMark, response1Size * 2);
       
   188 		response2Size = stream.ReadInt32L();
       
   189 		streamBuf->SeekL(MStreamBuf::ERead, EStreamMark, response2Size * 2);
       
   190 		}
       
   191 
       
   192 	TSecurityDialogOperation op = static_cast<TSecurityDialogOperation>(stream.ReadInt32L());
       
   193 
       
   194 	labelSize = stream.ReadInt32L();
       
   195 	HBufC* labelSpec = HBufC::NewMaxLC(labelSize);
       
   196 	TPtr labelPtr(labelSpec->Des());
       
   197 	stream.ReadL(labelPtr, labelSize);
       
   198 	
       
   199 	response1Size = stream.ReadInt32L();
       
   200 	HBufC* response1 = HBufC::NewMaxLC(response1Size);
       
   201 	TPtr response1Ptr(response1->Des());
       
   202 	stream.ReadL(response1Ptr, response1Size);
       
   203 	
       
   204 	response2Size = stream.ReadInt32L();
       
   205 	HBufC* response2 = HBufC::NewMaxLC(response2Size);
       
   206 	TPtr response2Ptr(response2->Des());
       
   207 	stream.ReadL(response2Ptr, response2Size);
       
   208 
       
   209 	CInputSpec* inputSpec = new (ELeave) CInputSpec(op, labelSpec, response1, response2);	
       
   210 	CleanupStack::Pop(3, labelSpec);	
       
   211 	CleanupStack::PopAndDestroy(); // stream
       
   212 
       
   213 	return inputSpec;
       
   214 	}
       
   215 
       
   216 
       
   217 void CTestSecDlgNotifier::DoEnterPINL(const CInputSpec& aSpec, const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   218 	{
       
   219 	const TPINInput& pinInput = reinterpret_cast<const TPINInput&>(*aBuffer.Ptr());
       
   220 
       
   221 	if (pinInput.iPIN.iPINLabel.Find(aSpec.LabelSpec()) == KErrNotFound)
       
   222 		{
       
   223 		User::Leave(KErrLabelMismatch);
       
   224 		}
       
   225 	
       
   226 	TPINValue pinValue = aSpec.Response1();
       
   227 	TPckg<TPINValue> pinValueBufPtr(pinValue);
       
   228 	aMessage.WriteL(aReplySlot, pinValueBufPtr);
       
   229 	}
       
   230 
       
   231 void CTestSecDlgNotifier::DoChangePINL(const CInputSpec& aSpec, const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   232 	{
       
   233 	const TPINInput& input = reinterpret_cast<const TPINInput&>(*aBuffer.Ptr());
       
   234 
       
   235 	if (input.iPIN.iPINLabel.Find(aSpec.LabelSpec()) == KErrNotFound)
       
   236 		{
       
   237 		User::Leave(KErrLabelMismatch);
       
   238 		}
       
   239 
       
   240 	TTwoPINOutput output;
       
   241 	output.iPINValueToCheck = aSpec.Response1();
       
   242 	output.iNewPINValue = aSpec.Response2();
       
   243 	TPckg<TTwoPINOutput> outputPckg(output);
       
   244 	aMessage.WriteL(aReplySlot, outputPckg);
       
   245 	}
       
   246 
       
   247 
       
   248 void CTestSecDlgNotifier::Release()
       
   249 	{
       
   250 	delete this;
       
   251 	}
       
   252 
       
   253 
       
   254 
       
   255 CTestSecDlgNotifier::TNotifierInfo CTestSecDlgNotifier::RegisterL()
       
   256 	{
       
   257 	return iInfo;
       
   258 	}
       
   259 
       
   260 
       
   261 
       
   262 CTestSecDlgNotifier::TNotifierInfo CTestSecDlgNotifier::Info() const
       
   263 	{
       
   264 	return iInfo;
       
   265 	}
       
   266 
       
   267 
       
   268 
       
   269 void CTestSecDlgNotifier::StartL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   270 	{
       
   271 	TRAPD(err, DoStartL(aBuffer, aReplySlot, aMessage));
       
   272 	aMessage.Complete(err);
       
   273 	}
       
   274 	
       
   275 	
       
   276 void CTestSecDlgNotifier::DoStartL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   277 	{
       
   278 	// Minimum length is 4
       
   279 	__ASSERT_DEBUG( aBuffer.Length() >= 4, User::Panic(_L("CTestSecDlgNotifier"), 0));
       
   280 
       
   281 	TUint operation = *reinterpret_cast<const TInt *>(aBuffer.Ptr()) & KSecurityDialogOperationMask;
       
   282 	
       
   283 	TInt index = GetInputIndexL();
       
   284 	CInputSpec* spec = NULL;
       
   285 
       
   286 	TRAPD(err, spec = ReadInputSpecL(index));
       
   287 	
       
   288 	// If the input file doesn't exist then we will answer PIN requests with the
       
   289 	// "pinkcloud" passphrase - this is so the certstore tests work independantly
       
   290 	// from keystore
       
   291 	if (err == KErrNotFound)
       
   292 		{
       
   293 		switch(operation)
       
   294 			{
       
   295 			case EEnterPIN:
       
   296 				{
       
   297 				TPINValue pinValue(KpinValue);
       
   298 				TPckg<TPINValue> pinValueBufPtr(pinValue);
       
   299 				aMessage.WriteL(aReplySlot, pinValueBufPtr);
       
   300 				break;
       
   301 				}
       
   302 			case EServerAuthenticationFailure:
       
   303 				{
       
   304 				TServerAuthenticationFailureDialogResult output;
       
   305 				output = EStop;				
       
   306 	    		TServerAuthenticationFailureOutputBuf outputPckg(output);	
       
   307 				aMessage.WriteL(aReplySlot, outputPckg);
       
   308 				break;
       
   309 				}
       
   310 			default:
       
   311 				{
       
   312 				User::Leave(KErrOperationMismatch);
       
   313 				break;
       
   314 				}			
       
   315 			}
       
   316 		return;	
       
   317 		}
       
   318 	
       
   319 	if (err == KErrEof)
       
   320 		{
       
   321 		User::Leave(KErrTooManyDialogs);
       
   322 		}
       
   323 
       
   324 	User::LeaveIfError(err);
       
   325 
       
   326 	CleanupStack::PushL(spec);
       
   327 	
       
   328 	if (operation != spec->Operation())
       
   329 		{
       
   330 		User::Leave(KErrOperationMismatch);
       
   331 		}
       
   332 		
       
   333 	switch (operation)
       
   334 		{
       
   335 		case EEnterPIN:
       
   336 			DoEnterPINL(*spec, aBuffer, aReplySlot, aMessage);
       
   337 			break;
       
   338 
       
   339 		case EChangePIN:
       
   340 			DoChangePINL(*spec, aBuffer, aReplySlot, aMessage);
       
   341 			break;
       
   342 
       
   343 		case ESecureConnection:
       
   344  			DoSecureConnectionL(*spec, aBuffer, aReplySlot, aMessage);
       
   345  			break;
       
   346 
       
   347 		case ESignText:
       
   348 		case EEnablePIN:
       
   349 		case EDisablePIN:
       
   350 		case EUnblockPIN:		
       
   351 		case EUnblockPINInClear:
       
   352 		case EPINBlocked:
       
   353 			// these operations are not yet implemented in this test harness
       
   354 			User::Leave(KErrOperationNotSupported);
       
   355 			break;
       
   356 
       
   357 		case EServerAuthenticationFailure:
       
   358 			DoServerAuthenticationFailureL(*spec, aBuffer, aReplySlot, aMessage);
       
   359 			break;
       
   360 
       
   361 		default:
       
   362 			User::Panic(_L("CTestSecDlgNotifier"), 0);
       
   363 		}
       
   364 	CleanupStack::PopAndDestroy(spec);
       
   365 	}
       
   366 
       
   367 void CTestSecDlgNotifier::DoServerAuthenticationFailureL(const CInputSpec& aSpec, const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage )
       
   368 	{
       
   369 	// Test for valid packing of dialog data by extracting the encoded certificate
       
   370 	// and attempting to construct an X509 certificate from it.
       
   371 	CServerAuthenticationFailureInput* srvAuthFail = CServerAuthenticationFailureInput::NewLC(aBuffer);
       
   372 	TPtrC8 encodedCert;
       
   373 	srvAuthFail->GetEncodedCert(encodedCert);
       
   374 	
       
   375 	// If CX509Certificate::NewL leaves the encoded cert buffer must not be valid.
       
   376 	const CX509Certificate* cert = CX509Certificate::NewL(encodedCert);
       
   377 
       
   378 	// There is no further need for the cert, so it can be deleted immediately.
       
   379 	delete cert;
       
   380 	
       
   381 	CleanupStack::PopAndDestroy(srvAuthFail);
       
   382 	
       
   383 	TServerAuthenticationFailureDialogResult output;
       
   384 	output = EStop;
       
   385 	if( (aSpec.Response1()).CompareF(KYes) == KErrNone )
       
   386 		{
       
   387 		output = EContinue;			
       
   388 		}
       
   389 	TServerAuthenticationFailureOutputBuf outputPckg(output);	
       
   390 	aMessage.WriteL(aReplySlot, outputPckg);
       
   391 	}
       
   392 
       
   393 void CTestSecDlgNotifier::DoSecureConnectionL(const CInputSpec& aSpec, const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage )
       
   394 	{
       
   395 	// If the client does not want to continue
       
   396 	if( (aSpec.Response1()).CompareF(KYes) != KErrNone )
       
   397 		{
       
   398 		User::Leave(KErrCancel);	
       
   399 		}
       
   400 	else
       
   401 		{
       
   402 		const TSignInput* secureConnectionInput =
       
   403 								reinterpret_cast<const TSignInput*>( aBuffer.Ptr() );
       
   404 		// if the client certificate is requested
       
   405 		if (secureConnectionInput->iDoClientAuthentication)
       
   406 			{			
       
   407 			TLex lexi(aSpec.Response2());
       
   408 			TInt32 selectionId=0;
       
   409 			TInt err=lexi.Val(selectionId);
       
   410 
       
   411 			TInt certHandleTotal = secureConnectionInput->iCertHandleArrayTotal;
       
   412 			if (selectionId>certHandleTotal)
       
   413 				{
       
   414 				User::Leave(KErrNotFound);	
       
   415 				}
       
   416 												
       
   417 			// Get index at start of list of TCTTokenObjectHandle objects
       
   418 			TInt bufferIndex = sizeof( TSignInput );
       
   419 			TInt certHandleSize = sizeof( TCTTokenObjectHandle );
       
   420 			TPckgBuf<TCTTokenObjectHandle> certHandleBuf;
       
   421 			TPtrC8 certHandleData( aBuffer.Mid( bufferIndex+(selectionId-1)*certHandleSize, certHandleSize ) );
       
   422 			certHandleBuf.Copy( certHandleData );
       
   423 			aMessage.WriteL( aReplySlot, certHandleBuf );			
       
   424 			}			
       
   425 		}	
       
   426 	}	
       
   427 
       
   428 	
       
   429 TPtrC8 CTestSecDlgNotifier::StartL( const TDesC8& /*aBuffer*/ )
       
   430 	{
       
   431 	User::Panic(_L("CTestSecDlgNotifier"), 0);
       
   432 	return TPtrC8(KNullDesC8);
       
   433 	}
       
   434 
       
   435 
       
   436 void CTestSecDlgNotifier::Cancel()
       
   437 	{
       
   438 	// Don't think there is much we can do here. If a client deletes the
       
   439 	// client-side security dialog instance, after calling a method that 
       
   440 	// displays a dialog, this will not get called until the user dismisses
       
   441 	// the dialog. We can't do anything then.
       
   442 	}
       
   443 
       
   444 
       
   445 TPtrC8 CTestSecDlgNotifier::UpdateL( const TDesC8& /*aBuffer*/ )
       
   446 	{
       
   447 	User::Panic(_L("CTestSecDlgNotifier"), 0);
       
   448 	return NULL;
       
   449 	}