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