cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_utils.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     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 <e32cons.h>
       
    21 
       
    22 #include "keytool_utils.h"
       
    23 #include "securityerr.h"
       
    24 #include "compiledsecuritypolicy.h"
       
    25 
       
    26 _LIT(space, " ");
       
    27 
       
    28 _LIT(newline, "\n");
       
    29  
       
    30 _LIT(KExtractable, "Extractable");
       
    31 _LIT(KSensitive, "Sensitive");
       
    32 _LIT(KAlwaysSensitive, "AlwaysSensitive");
       
    33 _LIT(KNeverExtractable, "NeverExtractable");
       
    34 _LIT(KLocal, "Local");
       
    35 _LIT(KEllipsis, "...");
       
    36  
       
    37 _LIT(x509, "X509");
       
    38 _LIT(allusage, "AllUsage");
       
    39 _LIT(nousage, "NoUsage");
       
    40 _LIT(encrypt, "Encrypt");
       
    41 _LIT(decrypt, "Decrypt");
       
    42 _LIT(sign, "Sign");
       
    43 _LIT(signrecover, "SignRecover");
       
    44 _LIT(wrap, "Wrap");
       
    45 _LIT(unwrap, "Unwrap");
       
    46 _LIT(verify, "Verify");
       
    47 _LIT(verifyrecover, "VerifyRecover");
       
    48 _LIT(derive, "Derive");
       
    49 _LIT(nonrepudiation, "NonRepudiation");
       
    50 
       
    51 const TInt KCursorOffset		= 2;
       
    52 static const TInt KMaxLabel = 32;
       
    53 CConsoleBase* KeyToolUtils::iConsole = NULL;       
       
    54 RFile* KeyToolUtils::iFile = NULL;
       
    55 
       
    56 /*static*/ void KeyToolUtils::WriteErrorL(TInt aError)
       
    57 	{
       
    58 	
       
    59 	if ((aError <= -12000) && (aError >= -12100))
       
    60 		{
       
    61 		PrintInfoL(_L("You have tsecdlg.dll in z:\\system\\notifiers."));		
       
    62 		PrintInfoL(_L("This notifier is for *testing only* please remove it and try again!"));
       
    63 		return;		
       
    64 		}
       
    65 	
       
    66 	switch (aError)
       
    67 		{
       
    68 	case KErrNone:	// 0
       
    69 		PrintInfoL(_L("KErrNone"));
       
    70 		break;
       
    71 
       
    72 	case KErrNotFound:	// -1
       
    73 		PrintInfoL(_L("KErrNotFound"));
       
    74 		break;
       
    75 
       
    76 	case KErrNotSupported:	// -5
       
    77 		PrintInfoL(_L("KErrNotSupported"));
       
    78 		break;
       
    79 
       
    80 	case KErrInUse:	// -14
       
    81 		PrintInfoL(_L("KErrInUse"));
       
    82 		break;
       
    83 
       
    84 	case KErrNotReady:	// -18
       
    85 		PrintInfoL(_L("KErrNotReady"));
       
    86 		break;
       
    87 
       
    88 	case KErrKeyAlgorithm:  //-11006
       
    89 		PrintInfoL(_L("KErrKeyAlgorithm"));
       
    90 		break;		
       
    91 
       
    92 	case KErrKeyUsage:  //-11007
       
    93 		PrintInfoL(_L("KErrKeyUsage"));
       
    94 		break;
       
    95 		
       
    96 	case KErrKeyValidity:  //-11008
       
    97 		PrintInfoL(_L("KErrKeyValidity"));
       
    98 		break;		
       
    99 
       
   100 	case KErrKeySize:  //-11009
       
   101 		PrintInfoL(_L("KErrKeySize"));
       
   102 		break;
       
   103 
       
   104 	case KErrKeyAccess:  //-11010
       
   105 		PrintInfoL(_L("KErrKeyAccess"));
       
   106 		break;
       
   107 
       
   108 	case KRequestPending:
       
   109 		PrintInfoL(_L("KRequestPending"));
       
   110 		break;
       
   111 
       
   112 	case KErrAlreadyExists:
       
   113 		PrintInfoL(_L("KErrAlreadyExists"));
       
   114 		break;
       
   115 
       
   116 	case KErrArgument:
       
   117 		PrintInfoL(_L("KErrArgument"));
       
   118 		break;
       
   119 
       
   120 	case KErrBadName: // -28
       
   121 		PrintInfoL(_L("KErrBadName"));
       
   122 		break;
       
   123 
       
   124 	case KErrCorrupt: // -20
       
   125 		PrintInfoL(_L("KErrCorrupt"));
       
   126 		break;		
       
   127 	case KErrPrivateKeyNotFound: //-11011				
       
   128 		PrintInfoL(_L("KErrPrivateKeyNotFound"));
       
   129 		break;	
       
   130 
       
   131 	default:
       
   132 		{
       
   133 		TBuf<64> msg;
       
   134 		msg.Format(_L("Unknown (Code %d)"), aError);
       
   135 		PrintInfoL(msg);		
       
   136 		}
       
   137 		break;
       
   138 		}
       
   139 	}
       
   140         
       
   141 /*static*/ void KeyToolUtils::WriteOctetStringL(const TDesC8& aString)
       
   142 	{
       
   143 	TInt index;
       
   144    	TInt counter;
       
   145  	counter = aString.Length();
       
   146 	HBufC* buf = HBufC::NewLC(counter*6);
       
   147 	TPtr pBuf = buf->Des();
       
   148    	 for (index = 0; index < counter; index++)
       
   149 		{
       
   150 		pBuf.AppendFormat(_L("%01x"),aString[index]>>4);
       
   151 		pBuf.AppendFormat(_L("%01x"),aString[index]&0xf);
       
   152 		pBuf.Append(_L(" "));
       
   153 		}
       
   154 	PrintInfoL(pBuf);
       
   155 	CleanupStack::PopAndDestroy();//buf
       
   156 	}
       
   157 	       
       
   158 	        
       
   159 /*static*/ void KeyToolUtils::PrintUsageL(TUint aUsage)
       
   160 	{
       
   161 	PrintInfoL(_L("\t\tUsage : "));
       
   162 	if (aUsage & 0x0000ffff)  // In this range we are talking PKCS15
       
   163 		{
       
   164 		PrintInfoL(_L("PKCS15 "));
       
   165 		}
       
   166 	if (aUsage & 0xffff0000)  // In this range we are talking X509
       
   167 		{
       
   168 		PrintInfoL(x509);
       
   169 		}
       
   170 	if (aUsage == (TUint)EPKCS15UsageAll)
       
   171 		{
       
   172 		PrintInfoL(allusage);		
       
   173 		return;
       
   174 		}	
       
   175 	if (aUsage == EPKCS15UsageNone)
       
   176 		{
       
   177 		PrintInfoL(nousage);		
       
   178 		return;
       
   179 		}	
       
   180 	if (aUsage & EPKCS15UsageEncrypt)
       
   181 		{
       
   182 		PrintInfoL(encrypt);		
       
   183 		}
       
   184 	if (aUsage & EPKCS15UsageDecrypt)
       
   185 		{
       
   186 		PrintInfoL(decrypt);		
       
   187 		}
       
   188 	if (aUsage & EPKCS15UsageSign)
       
   189 		{
       
   190 		PrintInfoL(sign);		
       
   191 		}
       
   192 	if (aUsage & EPKCS15UsageSignRecover)
       
   193 		{
       
   194 		PrintInfoL(signrecover);		
       
   195 		}
       
   196 	if (aUsage & EPKCS15UsageWrap)
       
   197 		{
       
   198 		PrintInfoL(wrap);		
       
   199 		}
       
   200 	if (aUsage & EPKCS15UsageUnwrap)
       
   201 		{
       
   202 		PrintInfoL(unwrap);		
       
   203 		}
       
   204 	if (aUsage & EPKCS15UsageVerify)
       
   205 		{
       
   206 		PrintInfoL(verify);		
       
   207 		}
       
   208 	if (aUsage & EPKCS15UsageVerifyRecover)
       
   209 		{
       
   210 		PrintInfoL(verifyrecover);		
       
   211 		}
       
   212 	if (aUsage & EPKCS15UsageDerive)
       
   213 		{
       
   214 		PrintInfoL(derive);		
       
   215 		}
       
   216 	if (aUsage & EPKCS15UsageNonRepudiation)
       
   217 		{
       
   218 		PrintInfoL(nonrepudiation);		
       
   219 		}
       
   220 	if (aUsage & (TUint)EX509UsageDigitalSignature)
       
   221 		{
       
   222 		PrintInfoL(_L("DigitalSignature "));		
       
   223 		}
       
   224 	if (aUsage & EX509UsageNonRepudiation)
       
   225 		{
       
   226 		PrintInfoL(_L("NonRepudiation "));		
       
   227 		}
       
   228 	if (aUsage & EX509UsageKeyEncipherment)
       
   229 		{
       
   230 		PrintInfoL(_L("KeyEncipherment "));		
       
   231 		}
       
   232 	if (aUsage & EX509UsageDataEncipherment )
       
   233 		{
       
   234 		PrintInfoL(_L("DataEncipherment "));		
       
   235 		}
       
   236 	if (aUsage & EX509UsageKeyAgreement )
       
   237 		{
       
   238 		PrintInfoL(_L("KeyAgreement "));		
       
   239 		}
       
   240 	if (aUsage & EX509UsageKeyCertSign )
       
   241 		{
       
   242 		PrintInfoL(_L("KeyCertSign "));		
       
   243 		}
       
   244 	if (aUsage & EX509UsageCRLSign )
       
   245 		{
       
   246 		PrintInfoL(_L("CRLSign "));		
       
   247 		}
       
   248 	if (aUsage & EX509UsageEncipherOnly )
       
   249 		{
       
   250 		PrintInfoL(_L("EncipherOnly  "));		
       
   251 		}
       
   252 	if (aUsage & EX509UsageDecipherOnly)
       
   253 		{
       
   254 		PrintInfoL(_L("DecipherOnly  "));		
       
   255 		}
       
   256 
       
   257 	TBuf<32> buf;	
       
   258 	_LIT(KKeyUsage, "\tCode: 0x%x ");
       
   259 	buf.Format(KKeyUsage, aUsage);
       
   260 	PrintInfoL(buf);
       
   261 	PrintInfoL(newline);	
       
   262 	}
       
   263 	
       
   264 	
       
   265 /*static*/ HBufC* KeyToolUtils::KeyAccessDesLC(TInt aAccess)
       
   266 	{
       
   267 	TBuf<128> access;
       
   268 	
       
   269 	if (aAccess & CCTKeyInfo::ESensitive)
       
   270 		{
       
   271 		access.Append(KSensitive);
       
   272 		access.Append(space);
       
   273 		}
       
   274 
       
   275 	if (aAccess & CCTKeyInfo::EExtractable)
       
   276 		{ 
       
   277 		access.Append(KExtractable);
       
   278 		access.Append(space);		
       
   279 		}
       
   280 
       
   281 	if (aAccess & CCTKeyInfo::EAlwaysSensitive)
       
   282 		{
       
   283 		access.Append(KAlwaysSensitive);
       
   284 		access.Append(space);
       
   285 		}
       
   286 
       
   287 	if (aAccess & CCTKeyInfo::ENeverExtractable)
       
   288 		{ 
       
   289 		access.Append(KNeverExtractable);
       
   290 		access.Append(space);
       
   291 		}
       
   292 
       
   293 	if (aAccess & CCTKeyInfo::ELocal)	
       
   294 		{ 
       
   295 		access.Append(KLocal);
       
   296 		access.Append(space);
       
   297 		}
       
   298 	return access.AllocLC();	
       
   299 	}
       
   300 	
       
   301 	
       
   302 /*static*/ HBufC* KeyToolUtils::AlgorithmDesLC(CCTKeyInfo::EKeyAlgorithm aAlgorithm)
       
   303 	{
       
   304 	TBuf<32> alg;
       
   305 
       
   306 	switch (aAlgorithm)
       
   307 		{
       
   308 		case CCTKeyInfo::EInvalidAlgorithm:
       
   309 			{
       
   310 			alg = _L("Invalid Algorithm");
       
   311 			}
       
   312 			break;
       
   313 		case CCTKeyInfo::ERSA:
       
   314 			{
       
   315 			alg = _L("RSA");
       
   316 			}
       
   317 			break;
       
   318 		case CCTKeyInfo::EDSA:
       
   319 			{
       
   320 			alg = _L("DSA");
       
   321 			}
       
   322 			break;
       
   323 		case CCTKeyInfo::EDH :
       
   324 			{
       
   325 			alg = _L("Diffie Hellman");
       
   326 			}
       
   327 			break;
       
   328 		default:
       
   329 			{
       
   330 			alg = _L("Unsupported algorithm!");			
       
   331 			}
       
   332 		}	
       
   333 	return alg.AllocLC();
       
   334 	}
       
   335 
       
   336 
       
   337         
       
   338 /*static*/ void KeyToolUtils::PrintKeyInfoL(const CCTKeyInfo& aKey, TBool aIsDetailed,TBool aPageWise)
       
   339 	{
       
   340 	HBufC* alg = KeyToolUtils::AlgorithmDesLC(aKey.Algorithm());
       
   341 	TFileName buf;
       
   342 
       
   343 	MCTToken& token = aKey.Token();
       
   344 	TFileName msg,msg1;
       
   345 	const TDesC& tmp = token.Label();
       
   346 	msg.Format(_L("Store Label: %S"), &tmp);	
       
   347 	
       
   348 	const TDesC& tmp1 = token.Information(MCTToken::EVersion);
       
   349 	const TDesC& tmp2 = token.Information(MCTToken::ESerialNo);
       
   350 	const TDesC& tmp3 = token.Information(MCTToken::EManufacturer);
       
   351 
       
   352 	msg1.Format(_L("Version: %S \t Serial Number: %S \t Manufacturer: %S Store Type: Read only"), &tmp1, &tmp2, &tmp3);	
       
   353 	
       
   354 	if (!aIsDetailed)
       
   355 		{
       
   356 		PrintInfoL(msg, aPageWise);
       
   357 		PrintInfoL(newline, aPageWise);
       
   358 		PrintInfoL(aKey.Label().Left(KMaxLabel));
       
   359 		if (aKey.Label().Length() > KMaxLabel)
       
   360 			{
       
   361 			PrintInfoL(KEllipsis);
       
   362 			}
       
   363 		PrintInfoL(_L(" is a "));			
       
   364 		_LIT(KKeySizeShort, "%d");
       
   365 		buf.Format(KKeySizeShort,  aKey.Size());
       
   366 		PrintInfoL(buf);
       
   367 		PrintInfoL(_L(" bits "));
       
   368 		PrintInfoL(*alg);
       
   369 		PrintInfoL(_L(" key "));	
       
   370 		CleanupStack::PopAndDestroy(alg);	
       
   371 		PrintInfoL(newline, aPageWise);	
       
   372 		return;
       
   373 		}
       
   374 	
       
   375 	PrintInfoL(msg, aPageWise);
       
   376 	PrintInfoL(newline, aPageWise);
       
   377 	PrintInfoL(msg1, aPageWise);
       
   378 	PrintInfoL(newline, aPageWise);
       
   379 
       
   380 	PrintInfoL(_L("\t\tAlgorithm: "));	
       
   381 	PrintInfoL(*alg);
       
   382 	CleanupStack::PopAndDestroy(alg);			
       
   383 
       
   384 	_LIT(KKeySize, "\tSize: %d ");
       
   385 	buf.Format(KKeySize,  aKey.Size());
       
   386 	PrintInfoL(buf);
       
   387 	PrintInfoL(_L(" bits"));
       
   388 	PrintInfoL(newline, aPageWise);
       
   389 
       
   390 	KeyToolUtils::PrintUsageL(aKey.Usage());
       
   391 
       
   392 	_LIT(KKeyOwner, "\t\tOwner : 0x%x ");
       
   393 	_LIT(KKeyUser, "\t\tUser : 0x%x ");
       
   394 	TCompiledSecurityPolicy managementPolicy = aKey.ManagementPolicy();
       
   395 	TCompiledSecurityPolicy usePolicy = aKey.UsePolicy();
       
   396 	
       
   397 	TUint32 mSecureid = managementPolicy.SecureId();
       
   398 	if (mSecureid == 0xffffffff)
       
   399 		{
       
   400 		TCapability cab = managementPolicy.Capability(0);
       
   401 		_LIT(KKeyOwnerCab, "\t\tOwner : %S ");
       
   402 		// currently only possible for keyowner other than secure id.
       
   403 		if (cab == ECapabilityWriteDeviceData)
       
   404 			{
       
   405 			_LIT(KCab, "WriteDeviceData");
       
   406 			buf.Format(KKeyOwnerCab, &KCab);
       
   407 			PrintInfoL(buf);	
       
   408 			}
       
   409 		else
       
   410 			{
       
   411 			_LIT(KCab, "Unknown");
       
   412 			buf.Format(KKeyOwnerCab, &KCab);
       
   413 			PrintInfoL(buf);
       
   414 			}
       
   415 		}
       
   416 	else
       
   417 		{
       
   418 		buf.Format(KKeyOwner, mSecureid);
       
   419 		PrintInfoL(buf);		
       
   420 		}
       
   421 	PrintInfoL(newline, aPageWise);
       
   422 
       
   423 	TUint32 uSecureid = usePolicy.SecureId();
       
   424 	if (uSecureid == 0xffffffff)
       
   425 		{
       
   426 		TSecurityPolicy::TType utype = usePolicy.Type();
       
   427 		_LIT(KKeyUserType, "\t\tUser : %S ");
       
   428 		// currently only possible for keyuser other than secure id
       
   429 		if (utype == TSecurityPolicy::ETypePass)
       
   430 			{
       
   431 			_LIT(KType, "ALL");
       
   432 			buf.Format(KKeyUserType, &KType);
       
   433 			PrintInfoL(buf);	
       
   434 			}
       
   435 		else
       
   436 			{
       
   437 			_LIT(KType, "Unknown");
       
   438 			buf.Format(KKeyUserType, &KType);
       
   439 			PrintInfoL(buf);
       
   440 			}
       
   441 		}
       
   442 	else
       
   443 		{
       
   444 		buf.Format(KKeyUser, uSecureid);
       
   445 		PrintInfoL(buf);
       
   446 		if (uSecureid == KUidSecurityKeytool)
       
   447 			{
       
   448 			_LIT(KUser, " (KeyTool)");
       
   449 			PrintInfoL(KUser);
       
   450 			}
       
   451 		}
       
   452 	PrintInfoL(newline, aPageWise);
       
   453 
       
   454 
       
   455 	_LIT(KKeySens, "\t\tAccess flags: ");
       
   456 	PrintInfoL(KKeySens);
       
   457 	
       
   458 	HBufC* access = KeyToolUtils::KeyAccessDesLC(aKey.AccessType());
       
   459 	PrintInfoL(*access);
       
   460 	CleanupStack::PopAndDestroy(access);
       
   461 	
       
   462 	PrintInfoL(newline, aPageWise);
       
   463 
       
   464 	_LIT(KKeyID, "\t\tID: ");
       
   465 	PrintInfoL(KKeyID);
       
   466 	KeyToolUtils::WriteOctetStringL(aKey.ID());
       
   467 	PrintInfoL(newline, aPageWise);
       
   468 				
       
   469 	_LIT(KKeyLabel, "\t\tLabel: ");
       
   470 	PrintInfoL(KKeyLabel);
       
   471 	PrintInfoL(aKey.Label().Left(KMaxLabel));
       
   472 	if (aKey.Label().Length() > KMaxLabel)
       
   473 		{
       
   474 		PrintInfoL(KEllipsis);
       
   475 		}
       
   476 	PrintInfoL(newline, aPageWise);
       
   477 
       
   478 	_LIT(KKeyNative, "\t\tNative: Yes ");
       
   479 	_LIT(KKeyNotNative, "\t\tNative: No ");
       
   480 	if ( aKey.Native())
       
   481 		{
       
   482 		PrintInfoL(KKeyNative);	
       
   483 		}
       
   484 	else
       
   485 		{
       
   486 		PrintInfoL(KKeyNotNative);			
       
   487 		}
       
   488 	PrintInfoL(newline, aPageWise);
       
   489 
       
   490 	_LIT(KTimeFormat, "%1/%2/%3 %H:%T:%S");
       
   491 	 
       
   492 	_LIT(KKeyStartDate, "\t\tStart date: ");
       
   493 	PrintInfoL(KKeyStartDate);
       
   494 	if (aKey.StartDate().Int64() == 0)
       
   495 		{
       
   496 		PrintInfoL(_L("not set"));		
       
   497 		}
       
   498 	else
       
   499 		{
       
   500 		aKey.StartDate().FormatL(buf, KTimeFormat);
       
   501 		PrintInfoL(buf);
       
   502 		}
       
   503 
       
   504 	_LIT(KKeyEndDate, "\tEnd date: ");
       
   505 	PrintInfoL(KKeyEndDate);
       
   506 	
       
   507 	if (aKey.EndDate().Int64() == 0)
       
   508 		{
       
   509 		PrintInfoL(_L("not set"));		
       
   510 		}
       
   511 	else
       
   512 		{
       
   513 		aKey.EndDate().FormatL(buf, KTimeFormat);
       
   514 		PrintInfoL(buf);
       
   515 		}
       
   516 	PrintInfoL(newline, aPageWise);
       
   517 	}
       
   518 	
       
   519 /*static*/ TKeyUsagePKCS15 KeyToolUtils::ParseKeyUsage(TPtrC aUsage)
       
   520 	{
       
   521 	TKeyUsagePKCS15 usage = EPKCS15UsageNone;
       
   522 
       
   523 	if (aUsage.Compare(allusage)==0)
       
   524 		{
       
   525 		return EPKCS15UsageAll;
       
   526 		}
       
   527 	if (aUsage.Compare(nousage)==0)
       
   528 		{
       
   529 		return EPKCS15UsageNone;
       
   530 		}
       
   531 	if (aUsage.Compare(encrypt)==0)
       
   532 		{
       
   533 		return EPKCS15UsageEncrypt;
       
   534 		}				
       
   535 	if (aUsage.Compare(decrypt)==0)
       
   536 		{
       
   537 		return EPKCS15UsageDecrypt;
       
   538 		}
       
   539 	if (aUsage.Compare(sign)==0)
       
   540 		{
       
   541 		return EPKCS15UsageSign;
       
   542 		}
       
   543 	if (aUsage.Compare(signrecover)==0)
       
   544 		{
       
   545 		return EPKCS15UsageSignRecover;
       
   546 		}
       
   547 	if (aUsage.Compare(wrap)==0)
       
   548 		{
       
   549 		return EPKCS15UsageWrap;
       
   550 		}							
       
   551 	if (aUsage.Compare(unwrap)==0)
       
   552 		{
       
   553 		return EPKCS15UsageUnwrap;
       
   554 		}							
       
   555 	if (aUsage.Compare(verify)==0)
       
   556 		{
       
   557 		return EPKCS15UsageVerify;
       
   558 		}	
       
   559 	if (aUsage.Compare(verifyrecover)==0)
       
   560 		{
       
   561 		return EPKCS15UsageVerifyRecover;
       
   562 		}	
       
   563 	if (aUsage.Compare(derive)==0)
       
   564 		{
       
   565 		return EPKCS15UsageDerive;
       
   566 		}	
       
   567 	if (aUsage.Compare(nonrepudiation)==0)
       
   568 		{
       
   569 		return EPKCS15UsageNonRepudiation;
       
   570 		}															
       
   571 	return usage;
       
   572 	}
       
   573 	
       
   574 /*static*/ CCTKeyInfo::EKeyAccess KeyToolUtils::ParseKeyAccess(TPtrC aAccess)
       
   575 	{
       
   576 	CKeyInfoBase::EKeyAccess access = CKeyInfoBase::EInvalidAccess;
       
   577 
       
   578 	if (aAccess.Compare(KSensitive)==0)
       
   579 		{
       
   580 		access = CCTKeyInfo::ESensitive;
       
   581 		}
       
   582 
       
   583 	if (aAccess.Compare(KExtractable)==0)
       
   584 		{ 
       
   585 		access = CCTKeyInfo::EExtractable;
       
   586 		}
       
   587 
       
   588 	if (aAccess.Compare(KAlwaysSensitive)==0)
       
   589 		{
       
   590 		access = CCTKeyInfo::EAlwaysSensitive;
       
   591 		}
       
   592 
       
   593 	if (aAccess.Compare(KNeverExtractable)==0)
       
   594 		{ 
       
   595 		access = CCTKeyInfo::ENeverExtractable;
       
   596 		}
       
   597 
       
   598 	if (aAccess.Compare(KLocal)==0)	
       
   599 		{ 
       
   600 		access = CCTKeyInfo::ELocal;
       
   601 		}
       
   602 	
       
   603 	return access;
       
   604 	}
       
   605 
       
   606 
       
   607 /*static*/ RArray<CCTKeyInfo*> KeyToolUtils::MatchKey(RMPointerArray<CCTKeyInfo>& aKeyList, TDesC& aLabel)
       
   608 	{
       
   609 	RArray<CCTKeyInfo*>	ret;
       
   610 	
       
   611 	for (TInt j = 0; j < aKeyList.Count(); j++)
       
   612 		{
       
   613 		if (aKeyList[j]->Label().Match(aLabel) != KErrNotFound) 
       
   614 			{
       
   615 			ret.Append(aKeyList[j]);
       
   616 			}
       
   617 		}
       
   618 	return ret;
       
   619 	}
       
   620 
       
   621 /*static*/ CCTKeyInfo* KeyToolUtils::findKey(RMPointerArray<CCTKeyInfo>& aKeyList, TDesC& aLabel)
       
   622 	{
       
   623 	TInt keyIndex = -1;
       
   624 	// Select the key with the label we want!
       
   625 	for (TInt j = 0; j < aKeyList.Count(); j++)
       
   626 		{
       
   627 		if (aKeyList[j]->Label() == aLabel) 
       
   628 			{
       
   629 			keyIndex = j;
       
   630 			break;
       
   631 			}
       
   632 		}
       
   633 
       
   634 	if (keyIndex == -1)
       
   635 		{
       
   636 		return NULL;
       
   637 		}
       
   638 	return aKeyList[keyIndex];
       
   639 	}
       
   640 
       
   641 /*static*/ const TDesC& KeyToolUtils::Uid2Des(TUid aUid)
       
   642 	{
       
   643 	if (aUid.iUid == swinstalluid)
       
   644 		{
       
   645 		return KSWInstall; 
       
   646 		}
       
   647 	else if (aUid.iUid == swinstallocspuid) 
       
   648 		{
       
   649 		return KSWInstallOCSP;
       
   650 		}
       
   651 	else if (aUid.iUid == midletinstalluid)
       
   652 		{
       
   653 		return KMidletInstall;
       
   654 		}
       
   655 	else if (aUid.iUid ==  tlsuid) 
       
   656 		{
       
   657 		return KTls;
       
   658 		}
       
   659   	else if (aUid.iUid ==  tocspuid) 
       
   660   		{
       
   661   		return KTOcsp;
       
   662   		}
       
   663   
       
   664  	return KUnknown;
       
   665 	}	
       
   666 
       
   667 
       
   668 /*static*/ void KeyToolUtils::FilterCertsL(RMPointerArray<CCTCertInfo>& aCertList, TDesC& aLabel)
       
   669   	{
       
   670   	TInt j = 0;
       
   671   	while (j < aCertList.Count())
       
   672   		{
       
   673   		if ( aCertList[j]->Label().Match(aLabel) == KErrNotFound) 
       
   674   			{
       
   675   			CCTCertInfo* tmp = aCertList[j];
       
   676   			tmp->Release();
       
   677   			aCertList.Remove(j);
       
   678   			}
       
   679   		else
       
   680   			{
       
   681   			j++;
       
   682   			}	
       
   683   		}
       
   684   	if (j ==0)	
       
   685   		{
       
   686   		PrintInfoL(_L("Cannot find the specified Certificate"));
       
   687   		PrintInfoL(newline);	
       
   688   		}
       
   689   	}
       
   690 
       
   691 /*static*/ void KeyToolUtils::FilterCertsL(RMPointerArray<CCTCertInfo>& aCertList, TCertificateOwnerType& aOwnerType)
       
   692 	{
       
   693 	TInt j = 0;
       
   694   	while (j < aCertList.Count())
       
   695   		{
       
   696   		if ( aCertList[j]->CertificateOwnerType() != aOwnerType) 
       
   697   			{
       
   698   			CCTCertInfo* tmp = aCertList[j];
       
   699   			tmp->Release();
       
   700   			aCertList.Remove(j);
       
   701   			}
       
   702   		else
       
   703   			{
       
   704   			j++;
       
   705   			}	
       
   706   		}
       
   707   	if (j ==0)	
       
   708   		{
       
   709   		PrintInfoL(_L("No Certificate Exist"));	
       
   710   		PrintInfoL(newline);
       
   711   		}
       
   712 	}
       
   713 
       
   714 /*static*/ void KeyToolUtils::PrintCertInfoL(CCTCertInfo& aCertInfo, CCertificate& aCertificate,RArray<TUid> aApps, TBool aIsDetailed,TBool aPageWise)
       
   715 	{
       
   716 	TFileName certFormat,ownerType;
       
   717 
       
   718 	switch (aCertInfo.CertificateFormat())
       
   719 		{
       
   720 		case EX509Certificate :
       
   721 			{
       
   722 			certFormat.Copy(_L("X509"));
       
   723 			}
       
   724 			break;
       
   725 		case EWTLSCertificate :
       
   726 			{
       
   727 			certFormat.Copy(_L("WTLS"));
       
   728 			}
       
   729 			break;
       
   730 		case EX968Certificate :
       
   731 			{
       
   732 			certFormat.Copy(_L("X968"));
       
   733 			}
       
   734 			break;		
       
   735 		default :
       
   736 			{
       
   737 			certFormat.Copy(_L("Unknown certificate format!"));
       
   738 			}
       
   739 		}	
       
   740 
       
   741 	switch (aCertInfo.CertificateOwnerType())
       
   742 		{
       
   743 		case EPeerCertificate :
       
   744 			{
       
   745 			ownerType.Copy(_L("Peer"));	
       
   746 			}
       
   747 			break;
       
   748 		case EUserCertificate :
       
   749 			{
       
   750 			ownerType.Copy(_L("User"));	
       
   751 			}
       
   752 			break;
       
   753 		case ECACertificate :
       
   754 			{
       
   755 			ownerType.Copy(_L("Root (CA)"));	
       
   756 			}
       
   757 			break;
       
   758 		default :
       
   759 			{
       
   760 			ownerType.Copy(_L("Unknown"));			
       
   761 			}
       
   762 		}
       
   763 	
       
   764 	MCTToken& token = aCertInfo.Token();
       
   765 	TFileName msg,msg1;
       
   766 	const TDesC& tmp = token.Label();
       
   767 	msg.Format(_L("Store Label: %S"), &tmp);	
       
   768 	
       
   769 	const TDesC& tmp1 = token.Information(MCTToken::EVersion);
       
   770 	const TDesC& tmp2 = token.Information(MCTToken::ESerialNo);
       
   771 	const TDesC& tmp3 = token.Information(MCTToken::EManufacturer);
       
   772 
       
   773 	msg1.Format(_L("Version: %S \t Serial Number: %S \t Manufacturer: %S "), &tmp1, &tmp2, &tmp3);	
       
   774 						
       
   775 	if (!aIsDetailed)
       
   776 		{
       
   777 		PrintInfoL(msg, aPageWise);
       
   778 		PrintInfoL(newline, aPageWise);
       
   779 		PrintInfoL(aCertInfo.Label());
       
   780 		PrintInfoL(_L(" is a "));
       
   781 		PrintInfoL(certFormat);
       
   782 		PrintInfoL(_L(" format which is a "));
       
   783 		PrintInfoL(ownerType);
       
   784 		PrintInfoL(_L(" type "));
       
   785 		PrintInfoL(newline, aPageWise);
       
   786 		return;
       
   787 		}
       
   788 
       
   789 	PrintInfoL(msg, aPageWise);
       
   790 	PrintInfoL(newline, aPageWise);		
       
   791 	PrintInfoL(msg1, aPageWise);
       
   792 	PrintInfoL(newline, aPageWise);		
       
   793 	PrintInfoL(_L("\tLabel: "));
       
   794 	PrintInfoL(aCertInfo.Label());
       
   795 
       
   796 	PrintInfoL(_L("\t\t"));
       
   797 	
       
   798 	PrintInfoL(_L("Format: "));
       
   799 	PrintInfoL(certFormat);
       
   800 	PrintInfoL(_L("\t"));
       
   801 	PrintInfoL(_L("Owner Type: "));	
       
   802 	PrintInfoL(ownerType);
       
   803 	PrintInfoL(newline, aPageWise);
       
   804 	PrintInfoL(_L("\tIssuer Name: "));
       
   805 	HBufC* issuer = aCertificate.IssuerL();
       
   806 	PrintInfoL(*issuer);	
       
   807 	delete issuer;
       
   808 	HBufC* subj = aCertificate.SubjectL();
       
   809 	PrintInfoL(_L("\t\tSubject Name: "));
       
   810 	PrintInfoL(*subj);
       
   811 	delete subj;
       
   812 
       
   813 	PrintInfoL(newline, aPageWise);
       
   814 	CValidityPeriod vp = aCertificate.ValidityPeriod();
       
   815 
       
   816 	const TTime& start = vp.Start();
       
   817 	const TTime& finish = vp.Finish();
       
   818 	TBuf<30> dateString1;
       
   819 	start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
       
   820 
       
   821 	PrintInfoL(_L("\tValid From  "));
       
   822 	PrintInfoL(dateString1, aPageWise);
       
   823 
       
   824 	TBuf<30> dateString2;
       
   825 	finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
       
   826 
       
   827 	PrintInfoL(_L("\tValid Until "));
       
   828 	PrintInfoL(dateString2, aPageWise);
       
   829 	PrintInfoL(newline, aPageWise);	
       
   830 	PrintInfoL(_L("\tTrusted for Applications: "));
       
   831 	for (TInt k = 0; k < aApps.Count(); k++)
       
   832 		{
       
   833 		PrintInfoL(Uid2Des(aApps[k]), aPageWise);
       
   834 		PrintInfoL(_L("  "));
       
   835 		}
       
   836 	PrintInfoL(newline, aPageWise);
       
   837 	}
       
   838 	
       
   839 void KeyToolUtils::SetConsole(CConsoleBase *aConsole )
       
   840 	{
       
   841 	iConsole = aConsole;
       
   842 	}
       
   843 
       
   844 void KeyToolUtils::SetFile(RFile* aFile)
       
   845 	{
       
   846 	iFile = aFile;
       
   847 	}
       
   848 
       
   849 TBool KeyToolUtils::DoesFileExistsL(const RFs& aFs, const TDesC& aFileName)
       
   850 	{
       
   851 	TBool exists = EFalse;
       
   852 	TEntry* e = new(ELeave) TEntry();		
       
   853 	TInt err = aFs.Entry(aFileName, *e);
       
   854 	if (err == KErrNone && ! e->IsDir())
       
   855 		{
       
   856 		exists = ETrue;			
       
   857 		}
       
   858 	delete e;
       
   859 	return exists;		
       
   860 	}
       
   861 	
       
   862 /*static*/ void KeyToolUtils::PrintInfoL(const TDesC& aValue, TBool aPageWise)
       
   863 	{
       
   864 	if (iConsole)
       
   865 		{
       
   866 		TSize screenSize = iConsole->ScreenSize();
       
   867 		if(aPageWise && (iConsole->WhereY() >= screenSize.iHeight - KCursorOffset))
       
   868 			{
       
   869  			iConsole->Printf(_L("\nPress a key to continue..."));
       
   870 			iConsole->Getch();
       
   871 			iConsole->ClearScreen();
       
   872 			iConsole->SetPos(0);
       
   873 			}
       
   874 		iConsole->Printf(aValue);	
       
   875 		}
       
   876 	else
       
   877 		{
       
   878 		TBuf8<256> buf8;
       
   879 		buf8.Copy(aValue);
       
   880 		TInt seekPos = 0;
       
   881 		iFile->Seek(ESeekEnd, seekPos);
       
   882 		User::LeaveIfError(iFile->Write(buf8));
       
   883 		}
       
   884 	}
       
   885