os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_testcertfordeletable.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 "t_testcertfordeletable.h"
    20 #include "t_certstoredefs.h"
    21 #include "t_input.h"
    22 #include "t_certstoreout.h"
    23 
    24 #include "t_WritableCSDummies.h"
    25 
    26 
    27 CTestAction* CTestCertForDeletable::NewL(RFs& aFs,
    28 								   CConsoleBase& aConsole, 
    29 								   Output& aOut, 
    30 								   const TTestActionSpec& aTestActionSpec)
    31 	{
    32 	CTestCertForDeletable* self = new(ELeave) CTestCertForDeletable(aFs, aConsole, aOut);
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL(aTestActionSpec);
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	}
    38 
    39 CTestCertForDeletable::~CTestCertForDeletable()
    40 	{
    41 	delete iCertificate;
    42 	delete iCertificateURL;
    43 	delete iCertificateContent;
    44 	delete iCertificateLabel;
    45 	}
    46 
    47 void CTestCertForDeletable::TestSerializationL(	MCTToken& aToken,
    48 												TKeyIdentifier* aIssuerKeyId,
    49 												TKeyIdentifier* aSubjectKeyId, 
    50 												const TInt aCertificateId,
    51 												const TDesC8* aIssuerHash )
    52 	{
    53 	CCTCertInfo* cert = CCTCertInfo::NewLC( *iCertificateLabel,	// const TDesC& 
    54 											iCertificateFormat,	// TCertificateFormat
    55 											iOwnerType, 		// TCertificateOwnerType
    56 											999, 				// TInt aSize | aCert.Length()
    57 											aSubjectKeyId,		// const TKeyIdentifier*
    58 											aIssuerKeyId,		// const TKeyIdentifier* 
    59 											aToken,	 			// MCTToken&
    60 											aCertificateId,		// TInt aCertificateId
    61 											iDeletable,			// TBool
    62 											aIssuerHash );		// const TDesC8* aIssuerHash = NULL	
    63 																
    64 													// cert -> stack
    65 	RFs fs;
    66 	User::LeaveIfError(fs.Connect());
    67 	CleanupClosePushL(fs);							// . fs -> stack
    68 
    69 	// this is a name of the file that will be opened as 
    70 	// a stream to test externalize/internalize
    71 	TDriveUnit sysDrive (fs.GetSystemDrive());
    72 	TBuf<24> fileName (sysDrive.Name());
    73 	fileName.Append(_L("\\certinfo.dat"));
    74 
    75 	RFileWriteStream write_stream;
    76 	User::LeaveIfError( write_stream.Replace(fs, fileName, EFileWrite) );
    77 	CleanupClosePushL(write_stream);				// . . write_stream -> stack
    78 
    79 	cert->ExternalizeL(write_stream);						
    80 	CleanupStack::PopAndDestroy();					// . . write_stream <- pop
    81 		
    82 	// now internalize back from stream
    83 	RFileReadStream read_stream;
    84 	User::LeaveIfError( read_stream.Open(fs, fileName, EFileRead) );
    85 	CleanupClosePushL(read_stream);					// . . read_stream -> stack
    86 
    87 	CCTCertInfo* cert_read =
    88 					CCTCertInfo::NewLC(read_stream, aToken); // does InternalizeL()
    89 													// . . . cert_read -> stack
    90 	// log certinfo params just internalized
    91 	iOut.writeString(_L("Cert attributes after internalize:"));
    92 	iOut.writeNewLine();
    93 	WriteFormat( cert_read->CertificateFormat() );
    94 	iOut.writeString(_L("\tiDeletable  = "));
    95 	iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
    96 	iOut.writeNewLine();
    97 	iOut.writeNewLine();
    98 			
    99 	// set result
   100 	iResultGood = ETrue; 
   101 	// check deletable flag is persistent
   102 	if ( cert_read->IsDeletable() != iDeletable )
   103 		{
   104 		iResultGood = EFalse; 
   105 		}
   106 	// check format is persistent
   107 	if ( cert_read->CertificateFormat() != iCertificateFormat )
   108 		{
   109 		iResultGood = EFalse; 
   110 		}			
   111 	
   112 	// cleanup
   113 	CleanupStack::PopAndDestroy(cert_read);			// . . . cert_read <- pop
   114 	CleanupStack::PopAndDestroy();					// . . read_stream <- pop
   115 	CleanupStack::PopAndDestroy(&fs); 				// . fs <- pop
   116 	CleanupStack::PopAndDestroy(cert);				// initial certinfo <- pop
   117 	}
   118 
   119 void CTestCertForDeletable::PerformAction(TRequestStatus& aStatus)
   120 	{
   121 // 	COMMON PART
   122 	MCTWritableCertStore& ustore = 
   123 			UnifiedCertStore().WritableCertStore(iStoreIndex);
   124 	MCTToken& token = ustore.Token();
   125 
   126 	TKeyIdentifier* issuerKeyId = NULL;
   127 	TKeyIdentifier* subjectKeyId = NULL;
   128 
   129 	if (iOwnerType != ECACertificate)
   130 		{
   131 		issuerKeyId = & iIssuerKeyId;
   132 		subjectKeyId = & iSubjectKeyId;
   133 		}
   134 
   135 	const TInt KCertificateId = 0x00000213;
   136 	_LIT8(KSomeHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d");
   137 
   138 	// create some certinfo object
   139 	ASSERT(iCertificateLabel);
   140 		
   141 	switch (iState)
   142 		{
   143 		case ETestSerialization:
   144 			{
   145 			iOut.writeString(_L("Checking serialization for a certificate..."));
   146 			iOut.writeNewLine();			
   147 
   148 			TRAPD( err, TestSerializationL( token, issuerKeyId, subjectKeyId,
   149 												 KCertificateId, &KSomeHash) );
   150 
   151 			TRequestStatus* status = &aStatus;
   152 			if (err != KErrNone )
   153 				{
   154 				// TestSerializationL() did leave
   155 				iResult = EFalse;
   156 				iResultGood = EFalse;
   157 				User::RequestComplete(status, err);
   158 				}
   159 			else
   160 				{
   161 				// TestSerializationL() passed ok
   162 				iResult = ETrue;
   163 				User::RequestComplete(status, aStatus.Int());
   164 				}					
   165 			// set next state
   166 			iState = EFinished;
   167 			}
   168 			break;
   169 		
   170 			
   171 		case ETestNewCSClasswDeletable:			// check mctwritablecertstore
   172 			{
   173 			iOut.writeString(_L("Testing MCTWritableCertStore::Add() w deletable..."));
   174 			iOut.writeNewLine();
   175 
   176 			// create a writablestore supporting new Add()
   177 			TNewCSClasswDeletable storeNew;
   178 			
   179 			// call new Add()
   180 			storeNew.Add( *iCertificateLabel, 
   181 						  iCertificateFormat, 
   182 						  iOwnerType,
   183 						  subjectKeyId, 
   184 						  issuerKeyId, 
   185 						  *iCertificateContent,	// this is probably unset 
   186 						  iDeletable,
   187 						  aStatus);
   188 						  
   189 			iResultGood = ETrue; 						  						
   190 			iState = EFinished;
   191 			}
   192 			break;
   193 
   194 
   195 		case ETestOldCSClasswoDeletable:		// check mctwritablecertstore
   196 			{
   197 
   198 			iOut.writeString(_L("Testing new MCTWritableCertStore::Add() on old class..."));
   199 			iOut.writeNewLine();
   200  
   201 			// create a writablestore that does not have new Add()
   202 			TOldCSClasswoDeletable storeOld;
   203 			
   204 			// call new Add(), expect to get KErrNotSupported
   205 			storeOld.Add( *iCertificateLabel, 
   206 						  iCertificateFormat, 
   207 						  iOwnerType,
   208 						  subjectKeyId, 
   209 						  issuerKeyId, 
   210 						  *iCertificateContent,	// this is probably unset 
   211 						  iDeletable,
   212 						  aStatus);
   213 			iResultGood = ETrue; 						  						
   214 			iState = EFinished;
   215 			
   216 			TRequestStatus* status = &aStatus;
   217 			User::RequestComplete(status, aStatus.Int());
   218 			}
   219 			break;
   220 
   221 
   222 		case EFinished:
   223 			{
   224 			
   225 			if (aStatus == KErrNone)
   226 				{
   227 				iResult = ETrue;
   228 				}
   229 			else
   230 			 	if (aStatus == iExpectedResult)
   231 					{
   232 					iResult = ETrue;
   233 					iResultGood = ETrue;
   234 					}
   235 				else
   236 					{
   237 					iResult = EFalse;
   238 					iResultGood = ETrue;
   239 					}
   240 
   241             if (aStatus != KErrNoMemory)
   242                 {
   243                 iFinished = ETrue;
   244                 }
   245             
   246 			TRequestStatus* status = &aStatus;
   247 			User::RequestComplete(status, aStatus.Int());
   248 			}
   249 			break;
   250 
   251 		default:
   252 			break;
   253 		
   254 		} 	// switch ()
   255 
   256 	}  // CTestCertForDeletable::PerformAction()
   257 
   258 void CTestCertForDeletable::PerformCancel()
   259 	{
   260 	iState = EFinished;
   261 	MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
   262 	store.CancelAdd();
   263 	}
   264 
   265 void CTestCertForDeletable::AfterOOMFailure()
   266 	{
   267 	}
   268 
   269 void CTestCertForDeletable::Reset()
   270 	{
   271 	iState = EFinished;
   272 	}
   273 
   274 void CTestCertForDeletable::DoReportAction()
   275 	{
   276 	iOut.writeString(_L("\tLabel = "));
   277 	iOut.writeString(*iCertificateLabel);
   278 	iOut.writeNewLine();
   279 	iOut.writeString(_L("\tOwner type = "));
   280 	WriteOwnerType();
   281 	WriteFormat(iCertificateFormat);
   282 	
   283 	iOut.writeString(_L("\tSubjectKeyId:  "));
   284 	iOut.writeOctetString(iSubjectKeyId);
   285 	iOut.writeNewLine();
   286 
   287 	iOut.writeString(_L("\tiDeletable  = "));
   288 	iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
   289 	iOut.writeNewLine();
   290 	iOut.writeNewLine();
   291 	}
   292 
   293 void CTestCertForDeletable::WriteFormat(TCertificateFormat aFormat)
   294 	{
   295 	iOut.writeString(_L("\tFormat = "));
   296 	switch (aFormat)
   297 		{
   298 		case EX509Certificate:
   299 			iOut.writeString(_L("X.509\n"));
   300 			break;
   301 			
   302 		case EWTLSCertificate:
   303 			iOut.writeString(_L("WTLS\n"));
   304 			break;
   305 			
   306 		case EX968Certificate:
   307 			iOut.writeString(_L("X968\n"));
   308 			break;
   309 			
   310 		case EX509CertificateUrl:
   311 			iOut.writeString(_L("X.509 URL\n"));
   312 			break;
   313 			
   314 		case EWTLSCertificateUrl:
   315 			iOut.writeString(_L("WTLS URL\n"));
   316 			break;
   317 			
   318 		case EX968CertificateUrl:
   319 			iOut.writeString(_L("X968 URL\n"));
   320 			break;
   321 			
   322 		default:
   323 			iOut.writeString(_L("Unknown format\n"));
   324 			break;
   325 		}
   326 	}
   327 
   328 void CTestCertForDeletable::WriteOwnerType()
   329 	{
   330 	switch (iOwnerType)
   331 		{
   332 		case ECACertificate:
   333 			iOut.writeString(_L("CA\n"));
   334 			break;
   335 			
   336 		case EUserCertificate:
   337 			iOut.writeString(_L("User"));
   338 			break;
   339 			
   340 		case EPeerCertificate:
   341 			iOut.writeString(_L("Peer"));
   342 			break;
   343 
   344 		default:
   345 			iOut.writeString(_L("Unknown"));
   346 			break;
   347 		}
   348 	}
   349 
   350 CTestCertForDeletable::CTestCertForDeletable(RFs& aFs, CConsoleBase& aConsole,
   351 								 Output& aOut)
   352 : CCertStoreTestAction(aFs, aConsole, aOut), iState(EFinished)
   353 	{
   354 	}
   355 
   356 void CTestCertForDeletable::ConstructL(const TTestActionSpec& aTestActionSpec)
   357 	{
   358 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   359 
   360 	SetCertFormatL(Input::ParseElement(aTestActionSpec.iActionBody, KCertFormatStart));
   361 	SetCertOwnerTypeL(Input::ParseElement(aTestActionSpec.iActionBody, KCertOwnerTypeStart));
   362 	SetCertLabelL(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
   363 	SetKeyId(iIssuerKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KIssuerKeyStart));
   364 	SetKeyId(iSubjectKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KSubjectKeyStart));
   365 	SetStoreToUse(Input::ParseElement(aTestActionSpec.iActionBody, KStoreToUseStart));
   366 	SetDeletable(Input::ParseElement(aTestActionSpec.iActionBody, KDeletableStart));
   367 	SetSubActionL(Input::ParseElement(aTestActionSpec.iActionBody, KSubActionStart));
   368 
   369 	// Setting the expected result
   370 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   371 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   372 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   373 	CleanupStack::PopAndDestroy(result);
   374 	}
   375 
   376 void CTestCertForDeletable::SetKeyId(TKeyIdentifier& aKeyIdentifier, const TDesC8& aKeyInfo)
   377 	{
   378 	TInt size = aKeyInfo.Length();
   379 	for (TInt i = 0; i < size; i += 2)
   380 		{
   381 		TInt a = (aKeyInfo[i+1] >= 'a') ? (aKeyInfo[i+1] - 'a' + 10) : (aKeyInfo[i+1] - '0');
   382 		TInt b = (aKeyInfo[i] >= 'a') ? (aKeyInfo[i] - 'a' + 10) : (aKeyInfo[i] - '0');
   383 		aKeyIdentifier.Append(a  + b * 16);
   384 		}
   385 	}
   386 
   387 void CTestCertForDeletable::SetCertFormatL(const TDesC8& aFormat)
   388 	{
   389 	if (aFormat == KX509)
   390 		{
   391 		iCertificateFormat = EX509Certificate;
   392 		}
   393 	else if (aFormat == KWTLS)
   394 		{
   395 		iCertificateFormat = EWTLSCertificate;
   396 		}
   397 	else if (aFormat == KX968)
   398 		{
   399 		iCertificateFormat = EX968Certificate;
   400 		}
   401 	else if (aFormat == KX509URL)
   402 		{
   403 		iCertificateFormat = EX509CertificateUrl;
   404 		}
   405 	else if (aFormat == KWTLSURL)
   406 		{
   407 		iCertificateFormat = EWTLSCertificateUrl;
   408 		}
   409 	else if (aFormat == KX968URL)
   410 		{
   411 		iCertificateFormat = EX968CertificateUrl;
   412 		}	
   413 	else if (aFormat == KUnknown)
   414 		{
   415 		iCertificateFormat = EUnknownCertificate;
   416 		}
   417 	else
   418 		{
   419 		iOut.write(_L("Unrecognized cert format: "));
   420 		iOut.writeString(aFormat);
   421 		iOut.writeNewLine();		   
   422 		User::Leave(KErrArgument);
   423 		}
   424 	}
   425 
   426 void CTestCertForDeletable::SetCertOwnerTypeL(const TDesC8& aOwnerType)
   427 	{
   428 	if (aOwnerType == KCACert)
   429 		{
   430 		iOwnerType = ECACertificate;
   431 		}
   432 	else if (aOwnerType == KUserCert)
   433 		{
   434 		iOwnerType = EUserCertificate;
   435 		}
   436 	else if (aOwnerType == KPeerCert)
   437 		{
   438 		iOwnerType = EPeerCertificate;
   439 		}
   440 	else if (aOwnerType == KUnknown)
   441 		{
   442 		// set dummy bogus owner type
   443 		iOwnerType = static_cast<TCertificateOwnerType>(EPeerCertificate + 1);
   444 		}
   445 	else
   446 		{
   447 		iOut.write(_L("Unknown cert owner type: "));
   448 		iOut.writeString(aOwnerType);
   449 		iOut.writeNewLine();		   
   450 		User::Leave(KErrArgument);
   451 		}
   452 	}
   453 
   454 void CTestCertForDeletable::SetSubActionL(const TDesC8& aStringVal)
   455 	{
   456 	if (aStringVal == KTestSerialization)
   457 		{
   458 		iState = ETestSerialization;
   459 		}
   460 	else if (aStringVal == KTestNewCSClasswDeletable)
   461 		{
   462 		iState = ETestNewCSClasswDeletable;
   463 		}
   464 	else if (aStringVal == KTestOldCSClasswoDeletable)
   465 		{
   466 		iState = ETestOldCSClasswoDeletable;
   467 		}
   468 
   469 	else
   470 		{
   471 		iOut.write(_L("Unknown subaction type: "));
   472 		iOut.writeString(aStringVal);
   473 		iOut.writeNewLine();		   
   474 		User::Leave(KErrArgument);
   475 		}
   476 	}
   477 
   478 
   479 void CTestCertForDeletable::SetCertLabelL(const TDesC8& aLabel)
   480 	{
   481 	delete iCertificateLabel;
   482 	iCertificateLabel = NULL;
   483 	iCertificateLabel = HBufC::NewL(aLabel.Length());
   484 	TPtr ptr = iCertificateLabel->Des();
   485 	ptr.Copy(aLabel);
   486 	}
   487 	
   488 
   489 
   490 void CTestCertForDeletable::SetStoreToUse(const TDesC8& aStoreToUse)
   491 	{
   492 	TLex8 lex(aStoreToUse);
   493 	lex.Val(iStoreIndex);
   494 	}
   495 	
   496 void CTestCertForDeletable::SetDeletable(const TDesC8& aStringVal)
   497 	{
   498 	TLex8 lex(aStringVal);
   499 	lex.Val(iDeletable);
   500 	}
   501 
   502 void CTestCertForDeletable::SetCertificateContentL(const TDesC8& aFileName)
   503 	{
   504 	TFileName fileName;
   505 	fileName.Copy(aFileName);
   506 	RFs fs; 
   507 	User::LeaveIfError(fs.Connect());
   508 	CleanupClosePushL(fs);
   509 	__ASSERT_DEBUG(!iCertificateContent, User::Panic(_L("CTestCertForDeletable"), 1));
   510 	TRAPD(err, iCertificateContent = Input::ReadFileL(fileName, fs));
   511 	if (err != KErrNone)
   512 		{
   513 		iConsole.Printf(_L("Error reading file : "));
   514 		iConsole.Printf(fileName);
   515 		iConsole.Printf(_L("\n"));
   516 		User::Leave(err);
   517 		}
   518 	CleanupStack::PopAndDestroy();	// fs
   519 	}
   520 
   521 void CTestCertForDeletable::ConstructCertL(const TDesC8& aCert)
   522 	{
   523 	TFileName filename;
   524 	filename.Copy(aCert);
   525 	RFs fs; 
   526 	User::LeaveIfError(fs.Connect());
   527 	CleanupClosePushL(fs);
   528 	HBufC8* certBuf = 0;
   529 	TRAPD(err, certBuf = Input::ReadFileL(filename, fs));
   530 	if (err != KErrNone)
   531 		{
   532 		iConsole.Printf(_L("Error reading file : "));
   533 		iConsole.Printf(filename);
   534 		iConsole.Printf(_L("\n"));
   535 		User::Leave(err);
   536 		}
   537 	CleanupStack::PushL(certBuf);
   538 	switch (iCertificateFormat)
   539 		{
   540 		case EX509Certificate:
   541 			iCertificate = CX509Certificate::NewL(*certBuf);
   542 			break;
   543 			
   544 		case EWTLSCertificate:
   545 			iCertificate = CWTLSCertificate::NewL(*certBuf);
   546 			break;
   547 			
   548 		default:
   549 			// Unknown format - do nothing
   550 			break;
   551 		}
   552 	CleanupStack::PopAndDestroy(2); // fs and certBuf
   553 	}
   554 
   555 void CTestCertForDeletable::DoCheckResult(TInt aError)
   556 	{
   557 	if (iFinished)
   558 		{
   559 		if (iResult && !iResultGood)
   560 			{
   561 			iConsole.Write(_L("\ttestcertfordeletable failed\n"));
   562 			iOut.writeString(_L("\ttestcertfordeletable failed"));
   563 			iOut.writeNewLine();
   564 			iOut.writeNewLine();
   565 			}
   566 		else if (iResult)
   567 			{
   568 			iConsole.Write(_L("\ttestcertfordeletable passed ok\n"));
   569 			iOut.writeString(_L("\ttestcertfordeletable passed ok"));
   570 			iOut.writeNewLine();
   571 			iOut.writeNewLine();
   572 			}
   573 		else 
   574 			{
   575 			iConsole.Write(_L("\tcertinfo couldn't be tested\n"));
   576 			iOut.writeString(_L("\tcertinfo couldn't be tested"));
   577 			iOut.writeNewLine();
   578 			iOut.writeString(_L("\t"));
   579 			iOut.writeError(aError);
   580 			if (aError == KErrBadName)
   581 				{
   582 				iOut.writeString(_L("   - Check that the label is unique"));
   583 				}
   584 			if (aError == KErrAccessDenied)
   585 				{
   586 				iOut.writeString(_L("   - Can't open filestream. Access denied."));
   587 				}
   588 			iOut.writeNewLine();
   589 			iOut.writeNewLine();
   590 			}
   591 		}	
   592 	}