os/security/cryptoservices/certificateandkeymgmt/tcertcommon/Tcertwriter.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) 1998-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 "tcertwriter.h"
sl@0
    20
#include <x509keys.h>
sl@0
    21
#include <asn1dec.h>
sl@0
    22
sl@0
    23
EXPORT_C CertWriter::CertWriter(Output* aOut)
sl@0
    24
	:iOut(aOut)
sl@0
    25
	{
sl@0
    26
	}
sl@0
    27
sl@0
    28
EXPORT_C void CertWriter::SetOut(Output* aOut)
sl@0
    29
	{
sl@0
    30
	iOut = aOut;
sl@0
    31
	}
sl@0
    32
sl@0
    33
EXPORT_C void CertWriter::WriteCert(const CX509Certificate& aCertificate)
sl@0
    34
	{
sl@0
    35
sl@0
    36
	CX509Certificate* cert = CX509Certificate::NewLC(aCertificate);
sl@0
    37
//	iOut->writeString(_L("\n Certificate Version = ")):
sl@0
    38
//	iOut->writeNum(cert->Version());
sl@0
    39
//	iOut.writeNewLine();
sl@0
    40
	WriteEncodings(*cert);
sl@0
    41
	iOut->writeNewLine();	
sl@0
    42
	ShowSerialNumber(cert->SerialNumber());
sl@0
    43
	ShowValidityPeriod(*cert);
sl@0
    44
	iOut->writeString(_L("Issuer Name = "));
sl@0
    45
	iOut->writeNewLine();	
sl@0
    46
	ShowDN(cert->IssuerName());	
sl@0
    47
	iOut->writeString(_L("Subject Name = "));
sl@0
    48
	iOut->writeNewLine();	
sl@0
    49
	ShowDN(cert->SubjectName());
sl@0
    50
	ShowSigningAlgorithm(cert->SigningAlgorithm());
sl@0
    51
	ShowExtensions(*cert);
sl@0
    52
	ShowPublicKey(*cert);
sl@0
    53
	iOut->writeNewLine();	
sl@0
    54
	ShowFingerprint(*cert);
sl@0
    55
	ShowSignature(*cert);
sl@0
    56
sl@0
    57
/*	CalculateSignature(*cert);
sl@0
    58
	if (IsSelfSigned(*cert))
sl@0
    59
		{
sl@0
    60
		iOut->writeString(_L("\nSelf-signed\n"));
sl@0
    61
		}
sl@0
    62
	else
sl@0
    63
		{
sl@0
    64
		iOut->writeString(_L("\nNot self-signed\n"));
sl@0
    65
		}
sl@0
    66
*/
sl@0
    67
	
sl@0
    68
	iOut->writeString(_L("Short Issuer Name = "));
sl@0
    69
	HBufC* issuer = cert->IssuerL();
sl@0
    70
	iOut->writeString(*issuer);
sl@0
    71
	iOut->writeNewLine();
sl@0
    72
	delete issuer;
sl@0
    73
sl@0
    74
	iOut->writeString(_L("Short Subject Name = "));
sl@0
    75
	HBufC* subject = cert->SubjectL();
sl@0
    76
	iOut->writeString(*subject);
sl@0
    77
	iOut->writeNewLine();
sl@0
    78
	delete subject;
sl@0
    79
sl@0
    80
	iOut->writeNewLine();
sl@0
    81
	CleanupStack::PopAndDestroy();//cert
sl@0
    82
	}
sl@0
    83
sl@0
    84
EXPORT_C void CertWriter::ShowPublicKey(const CX509Certificate& aCertificate)
sl@0
    85
	{
sl@0
    86
	const CSubjectPublicKeyInfo& spki = aCertificate.PublicKey();
sl@0
    87
	switch(spki.AlgorithmId())
sl@0
    88
		{
sl@0
    89
		case ERSA:
sl@0
    90
			ShowRSAKey(spki);
sl@0
    91
			break;
sl@0
    92
			
sl@0
    93
		case EDSA:
sl@0
    94
			ShowDSAKey(spki);
sl@0
    95
			break;
sl@0
    96
		
sl@0
    97
		case EDH:
sl@0
    98
			ShowDHKey(spki);
sl@0
    99
			break;
sl@0
   100
			
sl@0
   101
		default:
sl@0
   102
			User::Panic(_L("CertWriter"), 1);
sl@0
   103
			break;
sl@0
   104
		}
sl@0
   105
	}
sl@0
   106
sl@0
   107
sl@0
   108
void CertWriter::ShowRSAKey(const CSubjectPublicKeyInfo& aSpki)
sl@0
   109
	{
sl@0
   110
	CX509RSAPublicKey* key = CX509RSAPublicKey::NewLC(aSpki.KeyData());
sl@0
   111
	iOut->writeString(_L("RSA Key:"));
sl@0
   112
	iOut->writeNewLine();
sl@0
   113
	iOut->writeSpaces(4);
sl@0
   114
	iOut->writeString(_L("Modulus = "));
sl@0
   115
	iOut->writeNewLine();
sl@0
   116
	const TInteger& mod = key->N();
sl@0
   117
	HBufC8* mBuf = mod.BufferLC();
sl@0
   118
	iOut->writeOctetStringL(mBuf->Des());
sl@0
   119
	iOut->writeNewLine();	
sl@0
   120
sl@0
   121
	iOut->writeSpaces(4);
sl@0
   122
	iOut->writeString(_L("Exponent = "));
sl@0
   123
	iOut->writeNewLine();
sl@0
   124
	const TInteger& exp = key->E();
sl@0
   125
	HBufC8* eBuf = exp.BufferLC();
sl@0
   126
	iOut->writeOctetStringL(eBuf->Des());
sl@0
   127
	iOut->writeNewLine();	
sl@0
   128
	CleanupStack::PopAndDestroy(eBuf);
sl@0
   129
	CleanupStack::PopAndDestroy(mBuf);
sl@0
   130
	CleanupStack::PopAndDestroy(key);
sl@0
   131
	}
sl@0
   132
sl@0
   133
void CertWriter::ShowDSAKey(const CSubjectPublicKeyInfo& aSpki)
sl@0
   134
	{
sl@0
   135
	const TDesC8& encParams = aSpki.EncodedParams();
sl@0
   136
	if (encParams.Length() != 0)
sl@0
   137
		{
sl@0
   138
		CDSAPublicKey* key = CX509DSAPublicKey::NewLC(encParams, aSpki.KeyData());
sl@0
   139
		iOut->writeString(_L("DSA Key:"));
sl@0
   140
		iOut->writeNewLine();
sl@0
   141
		iOut->writeSpaces(4);
sl@0
   142
		iOut->writeString(_L("Y = "));
sl@0
   143
		iOut->writeNewLine();
sl@0
   144
		const TInteger& y = key->Y();
sl@0
   145
		HBufC8* yBuf = y.BufferLC();
sl@0
   146
		iOut->writeOctetStringL(yBuf->Des());
sl@0
   147
		iOut->writeNewLine();	
sl@0
   148
sl@0
   149
		iOut->writeSpaces(4);
sl@0
   150
		iOut->writeString(_L("Params = "));
sl@0
   151
		iOut->writeNewLine();
sl@0
   152
sl@0
   153
		iOut->writeSpaces(8);
sl@0
   154
		iOut->writeString(_L("P = "));
sl@0
   155
		iOut->writeNewLine();
sl@0
   156
		const TInteger& p = key->P();
sl@0
   157
		HBufC8* pBuf = p.BufferLC();
sl@0
   158
		iOut->writeOctetStringL(pBuf->Des());
sl@0
   159
		iOut->writeNewLine();
sl@0
   160
sl@0
   161
		iOut->writeSpaces(8);
sl@0
   162
		iOut->writeString(_L("Q = "));
sl@0
   163
		iOut->writeNewLine();
sl@0
   164
		const TInteger& q = key->Q();
sl@0
   165
		HBufC8* qBuf = q.BufferLC();
sl@0
   166
		iOut->writeOctetStringL(qBuf->Des());
sl@0
   167
		iOut->writeNewLine();
sl@0
   168
sl@0
   169
		iOut->writeSpaces(8);
sl@0
   170
		iOut->writeString(_L("G = "));
sl@0
   171
		iOut->writeNewLine();
sl@0
   172
		const TInteger& g = key->G();
sl@0
   173
		HBufC8* gBuf = g.BufferLC();
sl@0
   174
		iOut->writeOctetStringL(gBuf->Des());
sl@0
   175
		iOut->writeNewLine();
sl@0
   176
sl@0
   177
		CleanupStack::PopAndDestroy(5);
sl@0
   178
		}
sl@0
   179
	}
sl@0
   180
sl@0
   181
void CertWriter::ShowDHKey(const CSubjectPublicKeyInfo& aSpki)
sl@0
   182
	{
sl@0
   183
	TASN1DecInteger encInt;
sl@0
   184
	TInt pos = 0;
sl@0
   185
	RInteger keyVal = encInt.DecodeDERLongL(aSpki.KeyData(), pos);
sl@0
   186
	CleanupStack::PushL(keyVal);
sl@0
   187
	HBufC8* keyValBuf = keyVal.BufferLC();
sl@0
   188
	iOut->writeString(_L("Y = "));
sl@0
   189
	iOut->writeNewLine();
sl@0
   190
	iOut->writeOctetStringL(keyValBuf->Des());
sl@0
   191
	CleanupStack::PopAndDestroy(2, &keyVal);
sl@0
   192
sl@0
   193
	CX509DHDomainParams* params = CX509DHDomainParams::NewLC(aSpki.EncodedParams());
sl@0
   194
	const TInteger& p = params->P();
sl@0
   195
	const TInteger& g = params->G();
sl@0
   196
	const TInteger& q = params->Q();
sl@0
   197
	const TInteger& j = params->J();
sl@0
   198
	const CX509DHValidationParams* valParams = params->ValidationParams();
sl@0
   199
sl@0
   200
	iOut->writeNewLine();
sl@0
   201
	iOut->writeString(_L("Params = "));
sl@0
   202
	iOut->writeNewLine();
sl@0
   203
sl@0
   204
	iOut->writeString(_L("P = "));
sl@0
   205
	iOut->writeNewLine();
sl@0
   206
	HBufC8* pBuf = p.BufferLC();
sl@0
   207
	iOut->writeOctetStringL(pBuf->Des());
sl@0
   208
	iOut->writeNewLine();
sl@0
   209
	CleanupStack::PopAndDestroy();
sl@0
   210
sl@0
   211
	iOut->writeString(_L("G = "));
sl@0
   212
	iOut->writeNewLine();
sl@0
   213
	HBufC8* gBuf = g.BufferLC();
sl@0
   214
	iOut->writeOctetStringL(gBuf->Des());
sl@0
   215
	iOut->writeNewLine();
sl@0
   216
	CleanupStack::PopAndDestroy();
sl@0
   217
sl@0
   218
	iOut->writeString(_L("Q = "));
sl@0
   219
	iOut->writeNewLine();
sl@0
   220
	HBufC8* qBuf = q.BufferLC();
sl@0
   221
	iOut->writeOctetStringL(qBuf->Des());
sl@0
   222
	iOut->writeNewLine();
sl@0
   223
	CleanupStack::PopAndDestroy();
sl@0
   224
sl@0
   225
	iOut->writeString(_L("J = "));
sl@0
   226
	iOut->writeNewLine();
sl@0
   227
	HBufC8* jBuf = j.BufferLC();
sl@0
   228
	iOut->writeOctetStringL(jBuf->Des());
sl@0
   229
	iOut->writeNewLine();
sl@0
   230
	CleanupStack::PopAndDestroy();
sl@0
   231
		
sl@0
   232
	if (valParams)
sl@0
   233
		{
sl@0
   234
		iOut->writeString(_L("Seed = "));
sl@0
   235
		iOut->writeNewLine();
sl@0
   236
		iOut->writeOctetStringL(valParams->Seed());
sl@0
   237
		iOut->writeNewLine();
sl@0
   238
		const TInteger& pGC = valParams->PGenCounter();
sl@0
   239
		HBufC8* pgBuf = pGC.BufferLC();
sl@0
   240
		iOut->writeString(_L("pGenCounter = "));
sl@0
   241
		iOut->writeNewLine();
sl@0
   242
		iOut->writeOctetStringL(pgBuf->Des());
sl@0
   243
		iOut->writeNewLine();
sl@0
   244
		CleanupStack::PopAndDestroy();
sl@0
   245
		}
sl@0
   246
	CleanupStack::PopAndDestroy();
sl@0
   247
	}
sl@0
   248
sl@0
   249
sl@0
   250
EXPORT_C void CertWriter::ShowSigningAlgorithm(const CSigningAlgorithmIdentifier& aSigningAlgorithm)
sl@0
   251
	{
sl@0
   252
	iOut->writeString(_L("Signed using: "));
sl@0
   253
	iOut->writeNewLine();
sl@0
   254
	iOut->writeSpaces(4);
sl@0
   255
	iOut->writeString(_L("Asymmetric algorithm = "));
sl@0
   256
	const CAlgorithmIdentifier& algId = aSigningAlgorithm.AsymmetricAlgorithm();
sl@0
   257
	switch(algId.Algorithm())
sl@0
   258
		{
sl@0
   259
		case ERSA:
sl@0
   260
			iOut->writeString(_L("RSA"));
sl@0
   261
			break;
sl@0
   262
		
sl@0
   263
		case EDSA:
sl@0
   264
			iOut->writeString(_L("DSA"));
sl@0
   265
			break;
sl@0
   266
		
sl@0
   267
		case EDH:
sl@0
   268
			iOut->writeString(_L("DH"));
sl@0
   269
			break;
sl@0
   270
						
sl@0
   271
		default:
sl@0
   272
			User::Panic(_L("CertWriter"), 1);
sl@0
   273
			break;
sl@0
   274
		}
sl@0
   275
	iOut->writeNewLine();
sl@0
   276
	iOut->writeSpaces(4);
sl@0
   277
	iOut->writeString(_L("Digest algorithm = "));
sl@0
   278
	const CAlgorithmIdentifier& digestId = aSigningAlgorithm.DigestAlgorithm();
sl@0
   279
	switch(digestId.Algorithm())
sl@0
   280
		{
sl@0
   281
		case EMD2:
sl@0
   282
			iOut->writeString(_L("MD2"));
sl@0
   283
			break;
sl@0
   284
		
sl@0
   285
		case EMD5:
sl@0
   286
			iOut->writeString(_L("MD5"));
sl@0
   287
			break;
sl@0
   288
		
sl@0
   289
		case ESHA1:
sl@0
   290
			iOut->writeString(_L("SHA1"));
sl@0
   291
			break;
sl@0
   292
		
sl@0
   293
		default:
sl@0
   294
			User::Panic(_L("CertWriter"), 1);
sl@0
   295
			break;
sl@0
   296
		}
sl@0
   297
	iOut->writeNewLine();
sl@0
   298
	}
sl@0
   299
sl@0
   300
EXPORT_C void CertWriter::ShowSerialNumber(const TPtrC8& aSerialNumber)
sl@0
   301
	{
sl@0
   302
	iOut->writeString(_L("Serial Number = "));
sl@0
   303
	iOut->writeOctetStringL(aSerialNumber);
sl@0
   304
	iOut->writeNewLine();
sl@0
   305
	}
sl@0
   306
sl@0
   307
//dn display code
sl@0
   308
EXPORT_C void CertWriter::ShowAVA(const CX520AttributeTypeAndValue& aAva)
sl@0
   309
	{
sl@0
   310
	iOut->writeString(aAva.Type());
sl@0
   311
	HBufC* val = aAva.ValueL();
sl@0
   312
	CleanupStack::PushL(val);
sl@0
   313
	iOut->writeString(_L(" = "));
sl@0
   314
	iOut->writeString(val->Des());
sl@0
   315
	CleanupStack::PopAndDestroy();
sl@0
   316
	}
sl@0
   317
sl@0
   318
EXPORT_C void CertWriter::ShowDN(const CX500DistinguishedName& aName)
sl@0
   319
	{
sl@0
   320
	iOut->writeNewLine();
sl@0
   321
	TInt count = aName.Count();
sl@0
   322
	for (TInt i = 0; i < count; i++)
sl@0
   323
		{
sl@0
   324
  		const CX520AttributeTypeAndValue& ava = aName.Element(i);
sl@0
   325
		iOut->writeSpaces(4);
sl@0
   326
		ShowAVA(ava);
sl@0
   327
		iOut->writeNewLine();
sl@0
   328
		}
sl@0
   329
	}
sl@0
   330
sl@0
   331
EXPORT_C void CertWriter::ShowAKI(const CX509Certificate& aCert)
sl@0
   332
	{
sl@0
   333
	const CX509CertExtension* akiExt = aCert.Extension(KAuthorityKeyId);
sl@0
   334
	if (akiExt)
sl@0
   335
		{
sl@0
   336
		iOut->writeString(_L("Authority Key ID = "));
sl@0
   337
		iOut->writeNewLine();
sl@0
   338
		const CX509AuthorityKeyIdExt* ext = CX509AuthorityKeyIdExt::NewLC(akiExt->Data());
sl@0
   339
		const CArrayPtrFlat<CX509GeneralName>& authorityName = ext->AuthorityName();
sl@0
   340
		TInt count = authorityName.Count();
sl@0
   341
		if (count > 0)
sl@0
   342
			{
sl@0
   343
			iOut->writeSpaces(4);
sl@0
   344
			iOut->writeString(_L("Authority name = "));
sl@0
   345
			iOut->writeNewLine();
sl@0
   346
			for (TInt i = 0; i < count; i++)
sl@0
   347
				{
sl@0
   348
				ShowGN(*(authorityName.At(i)));
sl@0
   349
				}
sl@0
   350
			}
sl@0
   351
		if (ext->AuthorityCertSerialNumber().Length() > 0)
sl@0
   352
			{
sl@0
   353
			iOut->writeSpaces(4);
sl@0
   354
			iOut->writeString(_L("Authority cert serial no = "));
sl@0
   355
			iOut->writeOctetStringL(ext->AuthorityCertSerialNumber());
sl@0
   356
			iOut->writeNewLine();
sl@0
   357
			}
sl@0
   358
		iOut->writeSpaces(4);
sl@0
   359
		iOut->writeString(_L("Key Id = "));
sl@0
   360
		iOut->writeOctetStringL(ext->KeyId());
sl@0
   361
		iOut->writeNewLine();
sl@0
   362
		CleanupStack::PopAndDestroy();
sl@0
   363
		}
sl@0
   364
	}
sl@0
   365
sl@0
   366
EXPORT_C void CertWriter::ShowSKI(const CX509Certificate& aCert)
sl@0
   367
	{
sl@0
   368
	const CX509CertExtension* skiExt = aCert.Extension(KSubjectKeyId);
sl@0
   369
	if (skiExt)
sl@0
   370
		{
sl@0
   371
		iOut->writeString(_L("Subject Key ID = "));
sl@0
   372
		iOut->writeNewLine();
sl@0
   373
		const CX509SubjectKeyIdExt* ext = CX509SubjectKeyIdExt::NewLC(skiExt->Data());
sl@0
   374
		iOut->writeSpaces(4);
sl@0
   375
		iOut->writeString(_L("Key Id = "));
sl@0
   376
		iOut->writeOctetStringL(ext->KeyId());
sl@0
   377
		iOut->writeNewLine();
sl@0
   378
		CleanupStack::PopAndDestroy();
sl@0
   379
		}
sl@0
   380
	}
sl@0
   381
sl@0
   382
EXPORT_C void CertWriter::ShowGN(const CX509GeneralName& aName)
sl@0
   383
	{
sl@0
   384
	iOut->writeSpaces(4);
sl@0
   385
	switch(aName.Tag())
sl@0
   386
		{
sl@0
   387
		case 1:
sl@0
   388
			{
sl@0
   389
			//rfc822
sl@0
   390
			CX509RFC822NameSubtree* email = CX509RFC822NameSubtree::NewLC(aName.Data());
sl@0
   391
			iOut->writeString(email->Name());
sl@0
   392
			iOut->writeNewLine();
sl@0
   393
			CleanupStack::PopAndDestroy();//email
sl@0
   394
			break;
sl@0
   395
			}
sl@0
   396
		case 2:
sl@0
   397
			{
sl@0
   398
			//dns name
sl@0
   399
			CX509DNSNameSubtree* dns = CX509DNSNameSubtree::NewLC(aName.Data());
sl@0
   400
			iOut->writeString(dns->Name());
sl@0
   401
			CleanupStack::PopAndDestroy();//dns
sl@0
   402
			break;
sl@0
   403
			}
sl@0
   404
		case 4:
sl@0
   405
			{
sl@0
   406
			//DN
sl@0
   407
			CX500DistinguishedName* dN = CX500DistinguishedName::NewLC(aName.Data());
sl@0
   408
			ShowDN(*dN);
sl@0
   409
			CleanupStack::PopAndDestroy();
sl@0
   410
			break;
sl@0
   411
			}
sl@0
   412
		case 6:
sl@0
   413
			{
sl@0
   414
			//uri
sl@0
   415
			CX509IPBasedURI* uri = CX509IPBasedURI::NewLC(aName.Data());
sl@0
   416
			iOut->writeString(uri->Name());
sl@0
   417
			iOut->writeNewLine();
sl@0
   418
			iOut->writeString(_L("Host="));
sl@0
   419
			iOut->writeString(uri->Host().Name());
sl@0
   420
			iOut->writeNewLine();
sl@0
   421
			CleanupStack::PopAndDestroy();//uri
sl@0
   422
			break;
sl@0
   423
			}
sl@0
   424
		case 7:
sl@0
   425
			{
sl@0
   426
			CX509IPAddress* ip = CX509IPAddress::NewLC(aName.Data());
sl@0
   427
			TPtrC8 addressStr(ip->Address());
sl@0
   428
			// IPv6 output not implemented yet
sl@0
   429
			iOut->write(_L("IP=%d.%d.%d.%d"), addressStr[0], addressStr[1], addressStr[2], addressStr[3]);
sl@0
   430
			iOut->writeNewLine();			
sl@0
   431
			CleanupStack::PopAndDestroy();
sl@0
   432
			break;
sl@0
   433
			}
sl@0
   434
		}
sl@0
   435
	}
sl@0
   436
sl@0
   437
sl@0
   438
EXPORT_C void CertWriter::ShowFingerprint(const CX509Certificate& aCertificate)
sl@0
   439
	{
sl@0
   440
	iOut->writeString(_L("Fingerprint:"));
sl@0
   441
	iOut->writeNewLine();
sl@0
   442
	iOut->writeOctetString(aCertificate.Fingerprint());
sl@0
   443
	iOut->writeNewLine();
sl@0
   444
	}
sl@0
   445
sl@0
   446
EXPORT_C void CertWriter::ShowValidityPeriod(const CX509Certificate& aCertificate)
sl@0
   447
	{
sl@0
   448
	const CValidityPeriod& vp = aCertificate.ValidityPeriod();
sl@0
   449
	iOut->writeString(_L("Validity Period = "));
sl@0
   450
	iOut->writeNewLine();
sl@0
   451
	const TTime& start = vp.Start();
sl@0
   452
	const TTime& finish = vp.Finish();
sl@0
   453
	TBuf<30> dateString1;
sl@0
   454
	start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
sl@0
   455
sl@0
   456
	iOut->writeSpaces(4);
sl@0
   457
	iOut->writeString(_L("Valid From = "));
sl@0
   458
	iOut->writeString(dateString1);
sl@0
   459
	iOut->writeNewLine();
sl@0
   460
sl@0
   461
	TBuf<30> dateString2;
sl@0
   462
	finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
sl@0
   463
sl@0
   464
	iOut->writeSpaces(4);
sl@0
   465
	iOut->writeString(_L("Valid Until = "));
sl@0
   466
sl@0
   467
	iOut->writeString(dateString2);
sl@0
   468
	iOut->writeNewLine();
sl@0
   469
	}
sl@0
   470
sl@0
   471
//extensions
sl@0
   472
EXPORT_C void CertWriter::ShowExtensions(const CX509Certificate& aCertificate)
sl@0
   473
	{
sl@0
   474
	const CArrayPtrFlat<CX509CertExtension>& exts = aCertificate.Extensions();
sl@0
   475
	TInt count = exts.Count();
sl@0
   476
	for (TInt i = 0; i < count; i++)
sl@0
   477
		{
sl@0
   478
		const CX509CertExtension* ext = exts.At(i);
sl@0
   479
		iOut->writeString(_L("extension"));
sl@0
   480
		iOut->writeNum(i);
sl@0
   481
		if (!(ext->Critical()))
sl@0
   482
			{
sl@0
   483
			iOut->writeString(_L(" not"));
sl@0
   484
			}
sl@0
   485
		iOut->writeString(_L(" critical"));
sl@0
   486
		iOut->writeString(_L(" ID = "));
sl@0
   487
		TPtrC id(ext->Id());
sl@0
   488
		iOut->writeString(id);
sl@0
   489
		iOut->writeNewLine();
sl@0
   490
		if (id == KBasicConstraints)
sl@0
   491
			{
sl@0
   492
			ShowBC(*ext);
sl@0
   493
			}
sl@0
   494
		if (id == KSubjectAltName)
sl@0
   495
			{
sl@0
   496
			iOut->writeString(_L("SubjectAltName: "));
sl@0
   497
			iOut->writeNewLine();
sl@0
   498
			ShowAltName(*ext);
sl@0
   499
			}
sl@0
   500
		if (id == KIssuerAltName)
sl@0
   501
			{
sl@0
   502
			iOut->writeString(_L("IssuerAltName: "));
sl@0
   503
			iOut->writeNewLine();
sl@0
   504
			ShowAltName(*ext);
sl@0
   505
			}
sl@0
   506
		if (id == KKeyUsage)
sl@0
   507
			{
sl@0
   508
			ShowKU(*ext);
sl@0
   509
			}
sl@0
   510
		if (id == KNameConstraints)
sl@0
   511
			{
sl@0
   512
			ShowNC(*ext);
sl@0
   513
			}
sl@0
   514
		if (id == KPolicyConstraints)
sl@0
   515
			{
sl@0
   516
			ShowPC(*ext);
sl@0
   517
			}
sl@0
   518
		if (id == KCertPolicies)
sl@0
   519
			{
sl@0
   520
			ShowCP(*ext);
sl@0
   521
			}
sl@0
   522
		if (id == KPolicyMapping)
sl@0
   523
			{
sl@0
   524
			ShowPM(*ext);
sl@0
   525
			}
sl@0
   526
		if (id == KAuthorityKeyId)
sl@0
   527
			{
sl@0
   528
			ShowAKI(aCertificate);
sl@0
   529
			}
sl@0
   530
		if (id == KSubjectKeyId)
sl@0
   531
			{
sl@0
   532
			ShowSKI(aCertificate);
sl@0
   533
			}
sl@0
   534
		if (id == KExtendedKeyUsage)
sl@0
   535
			{
sl@0
   536
			ShowEKU(*ext);
sl@0
   537
			}
sl@0
   538
		}
sl@0
   539
	}
sl@0
   540
sl@0
   541
EXPORT_C void CertWriter::ShowBC(const CX509CertExtension& aExt)
sl@0
   542
	{
sl@0
   543
	iOut->writeString(_L("Basic Constraints:"));
sl@0
   544
	iOut->writeNewLine();
sl@0
   545
	CX509BasicConstraintsExt* ext = CX509BasicConstraintsExt::NewLC(aExt.Data());
sl@0
   546
	if (ext->IsCA())
sl@0
   547
		{
sl@0
   548
		iOut->writeSpaces(4);
sl@0
   549
		iOut->writeString(_L("CA cert"));
sl@0
   550
		iOut->writeNewLine();
sl@0
   551
		if (ext->MaxChainLength() < KMaxTInt)
sl@0
   552
			{
sl@0
   553
			iOut->writeSpaces(4);
sl@0
   554
			iOut->writeString(_L("Max Chain Length = "));
sl@0
   555
			iOut->writeNum(ext->MaxChainLength());
sl@0
   556
			iOut->writeNewLine();
sl@0
   557
			}
sl@0
   558
		}
sl@0
   559
	else
sl@0
   560
		{
sl@0
   561
		iOut->writeSpaces(4);
sl@0
   562
		iOut->writeString(_L("EE cert"));
sl@0
   563
		iOut->writeNewLine();
sl@0
   564
		}
sl@0
   565
	CleanupStack::PopAndDestroy();
sl@0
   566
	}
sl@0
   567
sl@0
   568
EXPORT_C void CertWriter::ShowAltName(const CX509CertExtension& aExt)
sl@0
   569
	{
sl@0
   570
	CX509AltNameExt* ext = CX509AltNameExt::NewLC(aExt.Data());
sl@0
   571
	const CArrayPtrFlat<CX509GeneralName>& names = ext->AltName();
sl@0
   572
	TInt count = names.Count();
sl@0
   573
	for (TInt i = 0; i < count; i++)
sl@0
   574
		{
sl@0
   575
		ShowGN(*(names.At(i)));
sl@0
   576
		}
sl@0
   577
	CleanupStack::PopAndDestroy();
sl@0
   578
	}
sl@0
   579
sl@0
   580
sl@0
   581
_LIT(KDigSig, "digital signature");
sl@0
   582
_LIT(KNonRep, "non-repudiation");
sl@0
   583
_LIT(KKeyEnc, "key encipherment");
sl@0
   584
_LIT(KDataEnc, "data encipherment");
sl@0
   585
_LIT(KKeyAgree, "key agreement");
sl@0
   586
_LIT(KCertSign, "key cert sign");
sl@0
   587
_LIT(KCRLSign, "crl sign");
sl@0
   588
_LIT(KEnciph, "encipher only");
sl@0
   589
_LIT(KDeciph, "decipher only");
sl@0
   590
sl@0
   591
EXPORT_C void CertWriter::ShowKU(const CX509CertExtension& aExt)
sl@0
   592
	{
sl@0
   593
	iOut->writeString(_L("Key Usage:"));
sl@0
   594
	iOut->writeNewLine();
sl@0
   595
	CX509KeyUsageExt* ext = CX509KeyUsageExt::NewLC(aExt.Data());
sl@0
   596
		if (ext->IsSet(EX509DigitalSignature))
sl@0
   597
			{
sl@0
   598
			iOut->writeSpaces(4);
sl@0
   599
			iOut->writeString(KDigSig);
sl@0
   600
			iOut->writeNewLine();
sl@0
   601
			}
sl@0
   602
		if (ext->IsSet(EX509NonRepudiation))
sl@0
   603
			{
sl@0
   604
			iOut->writeSpaces(4);
sl@0
   605
			iOut->writeString(KNonRep);
sl@0
   606
			iOut->writeNewLine();
sl@0
   607
			}
sl@0
   608
		if (ext->IsSet(EX509KeyEncipherment))
sl@0
   609
			{
sl@0
   610
			iOut->writeSpaces(4);
sl@0
   611
			iOut->writeString(KKeyEnc);
sl@0
   612
			iOut->writeNewLine();
sl@0
   613
			}
sl@0
   614
		if (ext->IsSet(EX509DataEncipherment))
sl@0
   615
			{
sl@0
   616
			iOut->writeSpaces(4);
sl@0
   617
			iOut->writeString(KDataEnc);
sl@0
   618
			iOut->writeNewLine();
sl@0
   619
			}
sl@0
   620
		if (ext->IsSet(EX509KeyAgreement))
sl@0
   621
			{
sl@0
   622
			iOut->writeSpaces(4);
sl@0
   623
			iOut->writeString(KKeyAgree);
sl@0
   624
			iOut->writeNewLine();
sl@0
   625
			}
sl@0
   626
		if (ext->IsSet(EX509KeyCertSign))
sl@0
   627
			{
sl@0
   628
			iOut->writeSpaces(4);
sl@0
   629
			iOut->writeString(KCertSign);
sl@0
   630
			iOut->writeNewLine();
sl@0
   631
			}
sl@0
   632
		if (ext->IsSet(EX509CRLSign))
sl@0
   633
			{
sl@0
   634
			iOut->writeSpaces(4);
sl@0
   635
			iOut->writeString(KCRLSign);
sl@0
   636
			iOut->writeNewLine();
sl@0
   637
			}
sl@0
   638
		if (ext->IsSet(EX509EncipherOnly))
sl@0
   639
			{
sl@0
   640
			iOut->writeSpaces(4);
sl@0
   641
			iOut->writeString(KEnciph);
sl@0
   642
			iOut->writeNewLine();
sl@0
   643
			}
sl@0
   644
		if (ext->IsSet(EX509DecipherOnly))
sl@0
   645
			{
sl@0
   646
			iOut->writeSpaces(4);
sl@0
   647
			iOut->writeString(KDeciph);
sl@0
   648
			iOut->writeNewLine();
sl@0
   649
			}		
sl@0
   650
	CleanupStack::PopAndDestroy();//ext
sl@0
   651
	}
sl@0
   652
sl@0
   653
EXPORT_C void CertWriter::ShowSubtrees(const CArrayPtrFlat<CX509GeneralSubtree>& aSubtrees)
sl@0
   654
	{
sl@0
   655
	TInt count = aSubtrees.Count();
sl@0
   656
	for (TInt i = 0; i < count; i++)
sl@0
   657
		{
sl@0
   658
		const CX509GeneralSubtree* subtree = aSubtrees.At(i);
sl@0
   659
		ShowGN(subtree->Name());
sl@0
   660
		///!!!!!don't write these outFile either!!
sl@0
   661
		}
sl@0
   662
	}
sl@0
   663
sl@0
   664
EXPORT_C void CertWriter::ShowNC(const CX509CertExtension& aExt)
sl@0
   665
	{//!!!!don't write these outFile
sl@0
   666
sl@0
   667
	CX509NameConstraintsExt* ext = CX509NameConstraintsExt::NewLC(aExt.Data());
sl@0
   668
	ShowSubtrees(ext->ExcludedSubtrees());
sl@0
   669
	ShowSubtrees(ext->PermittedSubtrees());
sl@0
   670
	CleanupStack::PopAndDestroy();
sl@0
   671
	}
sl@0
   672
sl@0
   673
EXPORT_C void CertWriter::ShowPC(const CX509CertExtension& /*aExt*/)
sl@0
   674
	{//!!!!don't write these outFile
sl@0
   675
//	CX509PolicyConstraintsExt* ext = CX509PolicyConstraintsExt::NewLC(aExt.Data());
sl@0
   676
//	TX509PolicyConstraint required = ext->ExplicitPolicyRequired();
sl@0
   677
//	TX509PolicyConstraint mapping = ext->InhibitPolicyMapping();
sl@0
   678
//	CleanupStack::PopAndDestroy();
sl@0
   679
	}
sl@0
   680
sl@0
   681
EXPORT_C void CertWriter::ShowCP(const CX509CertExtension& aExt)
sl@0
   682
	{
sl@0
   683
	iOut->writeString(_L("Cert Policies = "));
sl@0
   684
	iOut->writeNewLine();
sl@0
   685
sl@0
   686
	CX509CertPoliciesExt* ext = CX509CertPoliciesExt::NewLC(aExt.Data());
sl@0
   687
	const CArrayPtrFlat<CX509CertPolicyInfo>& policies = ext->Policies();
sl@0
   688
	TInt count = policies.Count();
sl@0
   689
	for (TInt i = 0; i < count; i++)
sl@0
   690
		{
sl@0
   691
		const CX509CertPolicyInfo* policy = policies.At(i);
sl@0
   692
		
sl@0
   693
		iOut->writeSpaces(4);
sl@0
   694
		iOut->writeString(_L("Policy ID = "));
sl@0
   695
		iOut->writeString(policy->Id());
sl@0
   696
		iOut->writeNewLine();
sl@0
   697
		const CArrayPtrFlat<CX509PolicyQualifierInfo>& qualifiers = policy->Qualifiers();
sl@0
   698
		TInt qCount = qualifiers.Count();
sl@0
   699
sl@0
   700
		if (qCount > 0)
sl@0
   701
			{
sl@0
   702
sl@0
   703
			iOut->writeSpaces(4);
sl@0
   704
			iOut->writeString(_L("Qualified by: "));
sl@0
   705
			iOut->writeNewLine();
sl@0
   706
			}
sl@0
   707
sl@0
   708
		for (TInt i = 0; i < qCount; i++)
sl@0
   709
			{
sl@0
   710
			iOut->writeSpaces(8);
sl@0
   711
			iOut->writeString(_L("Qualifier ID = "));
sl@0
   712
			iOut->writeString(policy->Id());
sl@0
   713
			iOut->writeNewLine();
sl@0
   714
			}
sl@0
   715
		}
sl@0
   716
	CleanupStack::PopAndDestroy();//ext
sl@0
   717
	}
sl@0
   718
sl@0
   719
EXPORT_C void CertWriter::ShowPM(const CX509CertExtension& aExt)
sl@0
   720
	{
sl@0
   721
	iOut->writeString(_L("Policy Mappings = "));
sl@0
   722
	CX509PolicyMappingExt* ext = CX509PolicyMappingExt::NewLC(aExt.Data());
sl@0
   723
	const CArrayPtrFlat<CX509PolicyMapping>& mappings = ext->Mappings();
sl@0
   724
	TInt count = mappings.Count();
sl@0
   725
	for (TInt i = 0; i < count; i++)
sl@0
   726
		{
sl@0
   727
		const CX509PolicyMapping* mapping = mappings.At(i);
sl@0
   728
		iOut->writeString(_L("Issuer Policy = "));
sl@0
   729
		iOut->writeString(mapping->IssuerPolicy());
sl@0
   730
		iOut->writeNewLine();
sl@0
   731
		iOut->writeString(_L("Subject Policy = "));
sl@0
   732
		iOut->writeString(mapping->SubjectPolicy());
sl@0
   733
		iOut->writeNewLine();
sl@0
   734
		}
sl@0
   735
	iOut->writeNewLine();
sl@0
   736
	CleanupStack::PopAndDestroy();
sl@0
   737
	}
sl@0
   738
sl@0
   739
sl@0
   740
EXPORT_C void CertWriter::ShowEKU(const CX509CertExtension& aExt)
sl@0
   741
	{
sl@0
   742
	iOut->writeString(_L("Extended Key Usage = "));
sl@0
   743
	iOut->writeNewLine();
sl@0
   744
sl@0
   745
	CX509ExtendedKeyUsageExt* ext = CX509ExtendedKeyUsageExt::NewLC(aExt.Data());
sl@0
   746
	const CArrayPtrFlat<HBufC>& usages = ext->KeyUsages();
sl@0
   747
	TInt count = usages.Count();
sl@0
   748
	for (TInt i = 0; i < count; i++)
sl@0
   749
		{
sl@0
   750
		iOut->writeSpaces(4);
sl@0
   751
		iOut->writeString(usages.At(i)->Des());
sl@0
   752
		iOut->writeNewLine();
sl@0
   753
		}
sl@0
   754
	CleanupStack::PopAndDestroy();
sl@0
   755
	}
sl@0
   756
sl@0
   757
EXPORT_C void CertWriter::ShowSignature(const CX509Certificate& aCert)
sl@0
   758
	{
sl@0
   759
	iOut->writeString(_L("Signature:"));
sl@0
   760
	iOut->writeNewLine();
sl@0
   761
	const TPtrC8 sig = aCert.Signature();
sl@0
   762
	iOut->writeOctetStringL(sig);
sl@0
   763
	iOut->writeNewLine();
sl@0
   764
	}
sl@0
   765
sl@0
   766
void CertWriter::CalculateSignature(const CX509Certificate& aCert)
sl@0
   767
	{
sl@0
   768
	TBool res = EFalse;;
sl@0
   769
	TRAPD(err, res = aCert.VerifySignatureL(aCert.PublicKey().KeyData()));
sl@0
   770
	if (err != KErrNone)
sl@0
   771
		{
sl@0
   772
		iOut->writeString(_L("\nsignature verification could not be performed\n"));
sl@0
   773
		if (err != KErrArgument)
sl@0
   774
			{
sl@0
   775
			User::Leave(err);
sl@0
   776
			}
sl@0
   777
		}
sl@0
   778
	else
sl@0
   779
		{
sl@0
   780
		if (res)
sl@0
   781
			{
sl@0
   782
			iOut->writeString(_L("\nself-signed\n"));
sl@0
   783
			}
sl@0
   784
		else
sl@0
   785
			{
sl@0
   786
			iOut->writeString(_L("\n not self-signed\n"));
sl@0
   787
			}
sl@0
   788
		}
sl@0
   789
	}
sl@0
   790
sl@0
   791
EXPORT_C TBool CertWriter::IsSelfSigned(const CX509Certificate& aCert)
sl@0
   792
	{
sl@0
   793
	TBool res = EFalse;
sl@0
   794
	const CX500DistinguishedName& subject = aCert.SubjectName();
sl@0
   795
	if (subject.Count() > 0)
sl@0
   796
		{
sl@0
   797
		res = subject.ExactMatchL(aCert.IssuerName());
sl@0
   798
		}
sl@0
   799
	else
sl@0
   800
		{
sl@0
   801
		const CX509CertExtension* subjectExt = aCert.Extension(KIssuerAltName);
sl@0
   802
		const CX509CertExtension* issuerExt = aCert.Extension(KSubjectAltName);
sl@0
   803
		if ((!subjectExt) || (!issuerExt))
sl@0
   804
			{
sl@0
   805
			}
sl@0
   806
		else
sl@0
   807
			{
sl@0
   808
			const CX509AltNameExt* issuerAltName = CX509AltNameExt::NewLC(subjectExt->Data());
sl@0
   809
			const CX509AltNameExt* subjectAltName = CX509AltNameExt::NewLC(issuerExt->Data());
sl@0
   810
			if (subjectAltName->Match(*issuerAltName))
sl@0
   811
				{
sl@0
   812
				res = ETrue;
sl@0
   813
				}
sl@0
   814
			CleanupStack::PopAndDestroy(2);//subjectAltName, issuerAltName
sl@0
   815
			}		
sl@0
   816
		}
sl@0
   817
	return res;
sl@0
   818
	}
sl@0
   819
/*		EVersionNumber = 0,
sl@0
   820
		ESerialNumber = 1,
sl@0
   821
		EAlgorithmId = 2,
sl@0
   822
		EIssuerName = 3,
sl@0
   823
		EValidityPeriod = 4,
sl@0
   824
		ESubjectName = 5,
sl@0
   825
		ESubjectPublicKeyInfo = 6,
sl@0
   826
		EIssuerUID = 7,
sl@0
   827
		ESubjectUID = 8,
sl@0
   828
		EExtensionList = 9
sl@0
   829
*/
sl@0
   830
void CertWriter::WriteEncodings(const CX509Certificate& aCertificate)
sl@0
   831
	{
sl@0
   832
	iOut->writeString(_L("Version:"));
sl@0
   833
	iOut->writeNewLine();
sl@0
   834
	WriteEncoding(aCertificate, CX509Certificate::EVersionNumber);
sl@0
   835
	iOut->writeNewLine();
sl@0
   836
sl@0
   837
	iOut->writeString(_L("Serial Number:"));
sl@0
   838
	iOut->writeNewLine();
sl@0
   839
	WriteEncoding(aCertificate, CX509Certificate::ESerialNumber);
sl@0
   840
	iOut->writeNewLine();
sl@0
   841
sl@0
   842
	iOut->writeString(_L("Algorithm:"));
sl@0
   843
	iOut->writeNewLine();
sl@0
   844
	WriteEncoding(aCertificate, CX509Certificate::EAlgorithmId);
sl@0
   845
	iOut->writeNewLine();
sl@0
   846
sl@0
   847
	iOut->writeString(_L("Issuer:"));
sl@0
   848
	iOut->writeNewLine();
sl@0
   849
	WriteEncoding(aCertificate, CX509Certificate::EIssuerName);
sl@0
   850
	iOut->writeNewLine();
sl@0
   851
sl@0
   852
	iOut->writeString(_L("Validity:"));
sl@0
   853
	iOut->writeNewLine();
sl@0
   854
	WriteEncoding(aCertificate, CX509Certificate::EValidityPeriod);
sl@0
   855
	iOut->writeNewLine();
sl@0
   856
sl@0
   857
	iOut->writeString(_L("Subject:"));
sl@0
   858
	iOut->writeNewLine();
sl@0
   859
	WriteEncoding(aCertificate, CX509Certificate::ESubjectName);
sl@0
   860
	iOut->writeNewLine();
sl@0
   861
sl@0
   862
	iOut->writeString(_L("Public Key:"));
sl@0
   863
	iOut->writeNewLine();
sl@0
   864
	WriteEncoding(aCertificate, CX509Certificate::ESubjectPublicKeyInfo);
sl@0
   865
	iOut->writeNewLine();
sl@0
   866
sl@0
   867
	iOut->writeString(_L("Issuer ID:"));
sl@0
   868
	iOut->writeNewLine();
sl@0
   869
	WriteEncoding(aCertificate, CX509Certificate::EIssuerUID);
sl@0
   870
	iOut->writeNewLine();
sl@0
   871
	
sl@0
   872
	iOut->writeString(_L("Subject ID:"));
sl@0
   873
	iOut->writeNewLine();
sl@0
   874
	WriteEncoding(aCertificate, CX509Certificate::ESubjectUID);
sl@0
   875
	iOut->writeNewLine();
sl@0
   876
sl@0
   877
	iOut->writeString(_L("Extensions:"));
sl@0
   878
	iOut->writeNewLine();
sl@0
   879
	WriteEncoding(aCertificate, CX509Certificate::EExtensionList);
sl@0
   880
	iOut->writeNewLine();
sl@0
   881
	}
sl@0
   882
sl@0
   883
void CertWriter::WriteEncoding(const CX509Certificate& aCertificate, const TUint aIndex)
sl@0
   884
	{
sl@0
   885
	if (aCertificate.DataElementEncoding(aIndex))
sl@0
   886
		{
sl@0
   887
		iOut->writeOctetStringL(*(aCertificate.DataElementEncoding(aIndex)));
sl@0
   888
		}
sl@0
   889
	}