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