os/security/cryptoservices/certificateandkeymgmt/twtlscert/CertWriter.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
* TWTLSOUT.CPP
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "CertWriter.h"
sl@0
    21
sl@0
    22
WTLSCertWriter::WTLSCertWriter(Output* aOut)
sl@0
    23
	:iOut(aOut)
sl@0
    24
	{
sl@0
    25
	}
sl@0
    26
sl@0
    27
void WTLSCertWriter::SetOut(Output* aOut)
sl@0
    28
	{
sl@0
    29
	iOut = aOut;
sl@0
    30
	}
sl@0
    31
sl@0
    32
void WTLSCertWriter::WriteCert(const CWTLSCertificate& aCertificate)
sl@0
    33
	{
sl@0
    34
	CWTLSCertificate* cert = CWTLSCertificate::NewL(aCertificate);
sl@0
    35
	CleanupStack::PushL(cert);
sl@0
    36
	WriteEncodings(*cert);
sl@0
    37
	iOut->writeNewLine();
sl@0
    38
	iOut->writeString(_L("Version = "));
sl@0
    39
	iOut->writeNum(cert->Version());	
sl@0
    40
	iOut->writeNewLine();	
sl@0
    41
	ShowValidityPeriod(*cert);
sl@0
    42
	iOut->writeString(_L("Issuer Name = "));
sl@0
    43
	iOut->writeNewLine();	
sl@0
    44
	ShowName(cert->IssuerName());	
sl@0
    45
	iOut->writeNewLine();	
sl@0
    46
	iOut->writeString(_L("Subject Name = "));
sl@0
    47
	iOut->writeNewLine();	
sl@0
    48
	ShowName(cert->SubjectName());
sl@0
    49
	iOut->writeNewLine();	
sl@0
    50
	ShowSigningAlgorithm(cert->SigningAlgorithm());
sl@0
    51
	ShowPublicKey(aCertificate);
sl@0
    52
sl@0
    53
	iOut->writeString(_L("Short Issuer Name = "));
sl@0
    54
	HBufC* issuer = cert->IssuerL();
sl@0
    55
	iOut->writeString(*issuer);
sl@0
    56
	iOut->writeNewLine();
sl@0
    57
	delete issuer;
sl@0
    58
sl@0
    59
	iOut->writeString(_L("Short Subject Name = "));
sl@0
    60
	HBufC* subject = cert->SubjectL();
sl@0
    61
	iOut->writeString(*subject);
sl@0
    62
	iOut->writeNewLine();
sl@0
    63
	delete subject;
sl@0
    64
sl@0
    65
	iOut->writeString(_L("Full structured issuer & subject names"));
sl@0
    66
	iOut->writeNewLine();
sl@0
    67
	CWTLSStructuredText* sIssuer = NULL;
sl@0
    68
	TRAPD(err, sIssuer = CWTLSStructuredText::NewL(cert->IssuerName().NameData()));
sl@0
    69
	if (err != KErrNone)
sl@0
    70
		{
sl@0
    71
		if (err == KErrNoMemory)
sl@0
    72
			{
sl@0
    73
			User::Leave(err);
sl@0
    74
			}
sl@0
    75
		}
sl@0
    76
	else
sl@0
    77
		{
sl@0
    78
		const TWTLSStructuredTextField* field = sIssuer->FieldByName(KWTLSCountryName);
sl@0
    79
		if (field)
sl@0
    80
			{
sl@0
    81
			iOut->writeString(field->Type());
sl@0
    82
			iOut->writeString(_L(" = "));
sl@0
    83
			iOut->writeString(field->Value());
sl@0
    84
			iOut->writeNewLine();
sl@0
    85
			__ASSERT_ALWAYS((field->Value() == sIssuer->Country()),User::Panic(_L("CERT"),0));
sl@0
    86
			}
sl@0
    87
sl@0
    88
		field = sIssuer->FieldByName(KWTLSOrganizationName);
sl@0
    89
		if (field)
sl@0
    90
			{
sl@0
    91
			iOut->writeString(field->Type());
sl@0
    92
			iOut->writeString(_L(" = "));
sl@0
    93
			iOut->writeString(field->Value());
sl@0
    94
			iOut->writeNewLine();
sl@0
    95
			__ASSERT_ALWAYS((field->Value() == sIssuer->Organization()),User::Panic(_L("CERT"),0));
sl@0
    96
			}
sl@0
    97
		field = sIssuer->FieldByName(KWTLSServiceName);
sl@0
    98
		if (field)
sl@0
    99
			{
sl@0
   100
			iOut->writeString(field->Type());
sl@0
   101
			iOut->writeString(_L(" = "));
sl@0
   102
			iOut->writeString(field->Value());
sl@0
   103
			iOut->writeNewLine();
sl@0
   104
			__ASSERT_ALWAYS((field->Value() == sIssuer->ServiceName()),User::Panic(_L("CERT"),0));
sl@0
   105
			}
sl@0
   106
		field = sIssuer->FieldByName(KWTLSTitle);
sl@0
   107
		if (field)
sl@0
   108
			{
sl@0
   109
			iOut->writeString(field->Type());
sl@0
   110
			iOut->writeString(_L(" = "));
sl@0
   111
			iOut->writeString(field->Value());
sl@0
   112
			iOut->writeNewLine();
sl@0
   113
			}
sl@0
   114
		field = sIssuer->FieldByName(KWTLSCommonName);
sl@0
   115
		if (field)
sl@0
   116
			{
sl@0
   117
			iOut->writeString(field->Type());
sl@0
   118
			iOut->writeString(_L(" = "));
sl@0
   119
			iOut->writeString(field->Value());
sl@0
   120
			iOut->writeNewLine();
sl@0
   121
			}
sl@0
   122
		delete sIssuer;
sl@0
   123
		}
sl@0
   124
sl@0
   125
	CWTLSStructuredText* sSubject = NULL;
sl@0
   126
	TRAP(err, sSubject = CWTLSStructuredText::NewL(cert->SubjectName().NameData()));
sl@0
   127
	if (err != KErrNone)
sl@0
   128
		{
sl@0
   129
		if (err == KErrNoMemory)
sl@0
   130
			{
sl@0
   131
			User::Leave(err);
sl@0
   132
			}
sl@0
   133
		}
sl@0
   134
	else
sl@0
   135
		{
sl@0
   136
		TInt count = sSubject->Count();
sl@0
   137
		for (TInt i = 0; i < count; i++)
sl@0
   138
			{
sl@0
   139
			const TWTLSStructuredTextField& field = sSubject->FieldByIndex(i);
sl@0
   140
			iOut->writeString(field.Type());
sl@0
   141
			iOut->writeString(_L(" = "));
sl@0
   142
			iOut->writeString(field.Value());
sl@0
   143
			iOut->writeNewLine();
sl@0
   144
			}
sl@0
   145
		delete sSubject;
sl@0
   146
		}
sl@0
   147
sl@0
   148
	if (cert->IsSelfSignedL())
sl@0
   149
		{
sl@0
   150
		const CWTLSName& subject = cert->SubjectName();
sl@0
   151
		const CWTLSName& issuer = cert->IssuerName();
sl@0
   152
		if ((subject.NameType() == EWTLSText) && (issuer.NameType() == EWTLSText))
sl@0
   153
			{
sl@0
   154
			CWTLSText* sText = CWTLSText::NewLC(subject.NameData());
sl@0
   155
			CWTLSText* iText = CWTLSText::NewLC(issuer.NameData());
sl@0
   156
			__ASSERT_ALWAYS((sText->ExactMatchL(*iText)),User::Panic(_L("CERT"),0));
sl@0
   157
			CleanupStack::PopAndDestroy(2);
sl@0
   158
			}
sl@0
   159
sl@0
   160
		}
sl@0
   161
sl@0
   162
sl@0
   163
	CleanupStack::PopAndDestroy();
sl@0
   164
	}
sl@0
   165
sl@0
   166
void WTLSCertWriter::ShowName(const CWTLSName& aName)
sl@0
   167
	{
sl@0
   168
	if (aName.NameType() == EWTLSText)
sl@0
   169
		{
sl@0
   170
		CWTLSText* text = CWTLSText::NewL(aName.NameData());
sl@0
   171
		CleanupStack::PushL(text);
sl@0
   172
		iOut->writeString(_L("Char set = "));
sl@0
   173
		iOut->writeNum(text->CharacterSet());
sl@0
   174
		iOut->writeNewLine();
sl@0
   175
		iOut->writeString(_L("Text = "));
sl@0
   176
		iOut->writeString(text->Name());
sl@0
   177
		iOut->writeNewLine();	
sl@0
   178
		CleanupStack::PopAndDestroy();
sl@0
   179
		}
sl@0
   180
//	iOut->writeString(aName.Name());
sl@0
   181
//	iOut->writeNewLine();
sl@0
   182
	iOut->writeOctetStringL(aName.NameData());
sl@0
   183
	}
sl@0
   184
sl@0
   185
void WTLSCertWriter::ShowPublicKey(const CWTLSCertificate& aCertificate)
sl@0
   186
	{
sl@0
   187
	const CSubjectPublicKeyInfo& spki = aCertificate.PublicKey();
sl@0
   188
	switch(spki.AlgorithmId())
sl@0
   189
		{
sl@0
   190
		case ERSA:
sl@0
   191
			{
sl@0
   192
			ShowRSAKey(spki);
sl@0
   193
			break;
sl@0
   194
			}
sl@0
   195
		default:
sl@0
   196
			//ignore any other algorithm
sl@0
   197
			break;
sl@0
   198
		}
sl@0
   199
	}
sl@0
   200
sl@0
   201
void WTLSCertWriter::ShowRSAKey(const CSubjectPublicKeyInfo& aSpki)
sl@0
   202
	{
sl@0
   203
	CRSAPublicKey* key = CWTLSRSAPublicKey::NewLC(aSpki.KeyData());
sl@0
   204
	iOut->writeString(_L("RSA Key:"));
sl@0
   205
	iOut->writeNewLine();
sl@0
   206
	iOut->writeSpaces(4);
sl@0
   207
	iOut->writeString(_L("Modulus = "));
sl@0
   208
	iOut->writeNewLine();
sl@0
   209
	const TInteger& mod = key->N();
sl@0
   210
	HBufC8* mBuf = mod.BufferLC();
sl@0
   211
	iOut->writeOctetStringL(mBuf->Des());
sl@0
   212
	iOut->writeNewLine();	
sl@0
   213
sl@0
   214
	iOut->writeSpaces(4);
sl@0
   215
	iOut->writeString(_L("Exponent = "));
sl@0
   216
	iOut->writeNewLine();
sl@0
   217
	const TInteger& exp = key->E();
sl@0
   218
	HBufC8* eBuf = exp.BufferLC();
sl@0
   219
	iOut->writeOctetString(eBuf->Des());
sl@0
   220
	iOut->writeNewLine();	
sl@0
   221
	CleanupStack::PopAndDestroy(3);
sl@0
   222
	}
sl@0
   223
sl@0
   224
void WTLSCertWriter::ShowSigningAlgorithm(const CSigningAlgorithmIdentifier& aSigningAlgorithm)
sl@0
   225
	{
sl@0
   226
	iOut->writeString(_L("Signed using: "));
sl@0
   227
	iOut->writeNewLine();
sl@0
   228
	iOut->writeSpaces(4);
sl@0
   229
	iOut->writeString(_L("Asymmetric algorithm = "));
sl@0
   230
	const CAlgorithmIdentifier& algId = aSigningAlgorithm.AsymmetricAlgorithm();
sl@0
   231
	switch(algId.Algorithm())
sl@0
   232
	{
sl@0
   233
	case ERSA:
sl@0
   234
		{
sl@0
   235
		iOut->writeString(_L("RSA"));
sl@0
   236
		break;
sl@0
   237
		}
sl@0
   238
	case EDSA:
sl@0
   239
		{
sl@0
   240
		iOut->writeString(_L("DSA"));
sl@0
   241
		break;
sl@0
   242
		}
sl@0
   243
	case EDH:
sl@0
   244
		{
sl@0
   245
		iOut->writeString(_L("DH"));
sl@0
   246
		break;
sl@0
   247
		}
sl@0
   248
	default:
sl@0
   249
		{
sl@0
   250
		iOut->writeString(_L("Unknown"));
sl@0
   251
		break;
sl@0
   252
		}
sl@0
   253
	}
sl@0
   254
	iOut->writeNewLine();
sl@0
   255
	iOut->writeSpaces(4);
sl@0
   256
	iOut->writeString(_L("Digest algorithm = "));
sl@0
   257
	const CAlgorithmIdentifier& digestId = aSigningAlgorithm.DigestAlgorithm();
sl@0
   258
	switch(digestId.Algorithm())
sl@0
   259
	{
sl@0
   260
	case EMD2:
sl@0
   261
		{
sl@0
   262
		iOut->writeString(_L("MD2"));
sl@0
   263
		break;
sl@0
   264
		}
sl@0
   265
	case EMD5:
sl@0
   266
		{
sl@0
   267
		iOut->writeString(_L("MD5"));
sl@0
   268
		break;
sl@0
   269
		}
sl@0
   270
	case ESHA1:
sl@0
   271
		{
sl@0
   272
		iOut->writeString(_L("SHA1"));
sl@0
   273
		break;
sl@0
   274
		}
sl@0
   275
	default:
sl@0
   276
		{
sl@0
   277
		iOut->writeString(_L("Unknown"));
sl@0
   278
		break;
sl@0
   279
		}
sl@0
   280
	}
sl@0
   281
	iOut->writeNewLine();
sl@0
   282
	}
sl@0
   283
sl@0
   284
void WTLSCertWriter::ShowSerialNumber(const TPtrC8& aSerialNumber)
sl@0
   285
	{
sl@0
   286
	iOut->writeString(_L("Serial Number = "));
sl@0
   287
	iOut->writeOctetString(aSerialNumber);
sl@0
   288
	iOut->writeNewLine();
sl@0
   289
	}
sl@0
   290
sl@0
   291
void WTLSCertWriter::ShowFingerprint(const CWTLSCertificate& aCertificate)
sl@0
   292
	{
sl@0
   293
	iOut->writeOctetString(aCertificate.Fingerprint());
sl@0
   294
	iOut->writeNewLine();
sl@0
   295
	}
sl@0
   296
sl@0
   297
void WTLSCertWriter::ShowValidityPeriod(const CWTLSCertificate& aCertificate)
sl@0
   298
	{
sl@0
   299
	const CValidityPeriod& vp = aCertificate.ValidityPeriod();
sl@0
   300
	iOut->writeString(_L("Validity Period = "));
sl@0
   301
	iOut->writeNewLine();
sl@0
   302
	const TTime& start = vp.Start();
sl@0
   303
	const TTime& finish = vp.Finish();
sl@0
   304
	TBuf<30> dateString1;
sl@0
   305
	start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
sl@0
   306
sl@0
   307
	iOut->writeSpaces(4);
sl@0
   308
	iOut->writeString(_L("Valid From = "));
sl@0
   309
	iOut->writeString(dateString1);
sl@0
   310
	iOut->writeNewLine();
sl@0
   311
sl@0
   312
	TBuf<30> dateString2;
sl@0
   313
	finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
sl@0
   314
sl@0
   315
	iOut->writeSpaces(4);
sl@0
   316
	iOut->writeString(_L("Valid Until = "));
sl@0
   317
sl@0
   318
	iOut->writeString(dateString2);
sl@0
   319
	iOut->writeNewLine();
sl@0
   320
	}
sl@0
   321
sl@0
   322
void WTLSCertWriter::WriteEncodings(const CWTLSCertificate& aCertificate)
sl@0
   323
	{
sl@0
   324
	iOut->writeString(_L("Version:"));
sl@0
   325
	iOut->writeNewLine();
sl@0
   326
	const TPtrC8* encoding = Encoding(aCertificate, CWTLSCertificate::EVersionNumber);
sl@0
   327
	if (encoding)
sl@0
   328
		{
sl@0
   329
		iOut->writeOctetString(*encoding);
sl@0
   330
		}
sl@0
   331
	iOut->writeNewLine();
sl@0
   332
sl@0
   333
	iOut->writeString(_L("Algorithm:"));
sl@0
   334
	iOut->writeNewLine();
sl@0
   335
	encoding = Encoding(aCertificate, CWTLSCertificate::EAlgorithmId);
sl@0
   336
	if (encoding)
sl@0
   337
		{
sl@0
   338
		CWTLSSigningAlgorithmIdentifier* algId = CWTLSSigningAlgorithmIdentifier::NewL(*encoding);
sl@0
   339
		CleanupStack::PushL(algId);
sl@0
   340
		__ASSERT_ALWAYS((*algId==aCertificate.SigningAlgorithm()),User::Panic(_L("CERT"),0));
sl@0
   341
		iOut->writeOctetString(*encoding);
sl@0
   342
		CleanupStack::PopAndDestroy();
sl@0
   343
		}
sl@0
   344
	iOut->writeNewLine();
sl@0
   345
sl@0
   346
	iOut->writeString(_L("Issuer:"));
sl@0
   347
	iOut->writeNewLine();
sl@0
   348
	encoding = Encoding(aCertificate, CWTLSCertificate::EIssuerName);
sl@0
   349
	if (encoding)
sl@0
   350
		{
sl@0
   351
		CWTLSName* name = CWTLSName::NewL(*encoding);
sl@0
   352
		CleanupStack::PushL(name);
sl@0
   353
		__ASSERT_ALWAYS(	(name->ExactMatchL(aCertificate.IssuerName())	),User::Panic(_L("CERT"),0));
sl@0
   354
		iOut->writeOctetStringL(*encoding);
sl@0
   355
		CleanupStack::PopAndDestroy();
sl@0
   356
		}
sl@0
   357
	iOut->writeNewLine();
sl@0
   358
sl@0
   359
	iOut->writeString(_L("Validity:"));
sl@0
   360
	iOut->writeNewLine();
sl@0
   361
	encoding = Encoding(aCertificate, CWTLSCertificate::EValidityPeriod);
sl@0
   362
	if (encoding)
sl@0
   363
		{
sl@0
   364
		CValidityPeriod* val  = CWTLSValidityPeriod::NewL(*encoding);
sl@0
   365
		CleanupStack::PushL(val);
sl@0
   366
		__ASSERT_ALWAYS(((val->Start() == aCertificate.ValidityPeriod().Start()) && (val->Finish() == aCertificate.ValidityPeriod().Finish())),User::Panic(_L("CERT"),0));
sl@0
   367
		iOut->writeOctetString(*encoding);
sl@0
   368
		CleanupStack::PopAndDestroy();
sl@0
   369
		}
sl@0
   370
	iOut->writeNewLine();
sl@0
   371
sl@0
   372
	iOut->writeString(_L("Subject:"));
sl@0
   373
	iOut->writeNewLine();
sl@0
   374
	encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectName);
sl@0
   375
	if (encoding)
sl@0
   376
		{
sl@0
   377
		CWTLSName* name = CWTLSName::NewL(*encoding);
sl@0
   378
		CleanupStack::PushL(name);
sl@0
   379
		__ASSERT_ALWAYS(	(name->ExactMatchL(aCertificate.SubjectName())	),User::Panic(_L("CERT"),0));
sl@0
   380
		iOut->writeOctetStringL(*encoding);
sl@0
   381
		CleanupStack::PopAndDestroy();
sl@0
   382
		}
sl@0
   383
	iOut->writeNewLine();
sl@0
   384
sl@0
   385
	iOut->writeString(_L("Public Key:"));
sl@0
   386
	iOut->writeNewLine();
sl@0
   387
	encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectPublicKeyInfo);
sl@0
   388
	if (encoding)
sl@0
   389
		{
sl@0
   390
		CSubjectPublicKeyInfo* info  = CWTLSSubjectPublicKeyInfo::NewL(*encoding);
sl@0
   391
		CleanupStack::PushL(info);
sl@0
   392
		__ASSERT_ALWAYS((info->KeyData() == aCertificate.PublicKey().KeyData()),User::Panic(_L("CERT"),0));
sl@0
   393
		iOut->writeOctetStringL(*encoding);
sl@0
   394
		CleanupStack::PopAndDestroy();
sl@0
   395
		}
sl@0
   396
	iOut->writeNewLine();
sl@0
   397
	}
sl@0
   398
sl@0
   399
const TPtrC8* WTLSCertWriter::Encoding(const CWTLSCertificate& aCertificate, const TUint aIndex)
sl@0
   400
	{
sl@0
   401
	if (aCertificate.DataElementEncoding(aIndex))
sl@0
   402
		{
sl@0
   403
		return aCertificate.DataElementEncoding(aIndex);
sl@0
   404
		}
sl@0
   405
	return NULL;
sl@0
   406
	}
sl@0
   407