os/security/cryptoservices/certificateandkeymgmt/twtlscert/CertWriter.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/twtlscert/CertWriter.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,407 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* TWTLSOUT.CPP
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include "CertWriter.h"
    1.24 +
    1.25 +WTLSCertWriter::WTLSCertWriter(Output* aOut)
    1.26 +	:iOut(aOut)
    1.27 +	{
    1.28 +	}
    1.29 +
    1.30 +void WTLSCertWriter::SetOut(Output* aOut)
    1.31 +	{
    1.32 +	iOut = aOut;
    1.33 +	}
    1.34 +
    1.35 +void WTLSCertWriter::WriteCert(const CWTLSCertificate& aCertificate)
    1.36 +	{
    1.37 +	CWTLSCertificate* cert = CWTLSCertificate::NewL(aCertificate);
    1.38 +	CleanupStack::PushL(cert);
    1.39 +	WriteEncodings(*cert);
    1.40 +	iOut->writeNewLine();
    1.41 +	iOut->writeString(_L("Version = "));
    1.42 +	iOut->writeNum(cert->Version());	
    1.43 +	iOut->writeNewLine();	
    1.44 +	ShowValidityPeriod(*cert);
    1.45 +	iOut->writeString(_L("Issuer Name = "));
    1.46 +	iOut->writeNewLine();	
    1.47 +	ShowName(cert->IssuerName());	
    1.48 +	iOut->writeNewLine();	
    1.49 +	iOut->writeString(_L("Subject Name = "));
    1.50 +	iOut->writeNewLine();	
    1.51 +	ShowName(cert->SubjectName());
    1.52 +	iOut->writeNewLine();	
    1.53 +	ShowSigningAlgorithm(cert->SigningAlgorithm());
    1.54 +	ShowPublicKey(aCertificate);
    1.55 +
    1.56 +	iOut->writeString(_L("Short Issuer Name = "));
    1.57 +	HBufC* issuer = cert->IssuerL();
    1.58 +	iOut->writeString(*issuer);
    1.59 +	iOut->writeNewLine();
    1.60 +	delete issuer;
    1.61 +
    1.62 +	iOut->writeString(_L("Short Subject Name = "));
    1.63 +	HBufC* subject = cert->SubjectL();
    1.64 +	iOut->writeString(*subject);
    1.65 +	iOut->writeNewLine();
    1.66 +	delete subject;
    1.67 +
    1.68 +	iOut->writeString(_L("Full structured issuer & subject names"));
    1.69 +	iOut->writeNewLine();
    1.70 +	CWTLSStructuredText* sIssuer = NULL;
    1.71 +	TRAPD(err, sIssuer = CWTLSStructuredText::NewL(cert->IssuerName().NameData()));
    1.72 +	if (err != KErrNone)
    1.73 +		{
    1.74 +		if (err == KErrNoMemory)
    1.75 +			{
    1.76 +			User::Leave(err);
    1.77 +			}
    1.78 +		}
    1.79 +	else
    1.80 +		{
    1.81 +		const TWTLSStructuredTextField* field = sIssuer->FieldByName(KWTLSCountryName);
    1.82 +		if (field)
    1.83 +			{
    1.84 +			iOut->writeString(field->Type());
    1.85 +			iOut->writeString(_L(" = "));
    1.86 +			iOut->writeString(field->Value());
    1.87 +			iOut->writeNewLine();
    1.88 +			__ASSERT_ALWAYS((field->Value() == sIssuer->Country()),User::Panic(_L("CERT"),0));
    1.89 +			}
    1.90 +
    1.91 +		field = sIssuer->FieldByName(KWTLSOrganizationName);
    1.92 +		if (field)
    1.93 +			{
    1.94 +			iOut->writeString(field->Type());
    1.95 +			iOut->writeString(_L(" = "));
    1.96 +			iOut->writeString(field->Value());
    1.97 +			iOut->writeNewLine();
    1.98 +			__ASSERT_ALWAYS((field->Value() == sIssuer->Organization()),User::Panic(_L("CERT"),0));
    1.99 +			}
   1.100 +		field = sIssuer->FieldByName(KWTLSServiceName);
   1.101 +		if (field)
   1.102 +			{
   1.103 +			iOut->writeString(field->Type());
   1.104 +			iOut->writeString(_L(" = "));
   1.105 +			iOut->writeString(field->Value());
   1.106 +			iOut->writeNewLine();
   1.107 +			__ASSERT_ALWAYS((field->Value() == sIssuer->ServiceName()),User::Panic(_L("CERT"),0));
   1.108 +			}
   1.109 +		field = sIssuer->FieldByName(KWTLSTitle);
   1.110 +		if (field)
   1.111 +			{
   1.112 +			iOut->writeString(field->Type());
   1.113 +			iOut->writeString(_L(" = "));
   1.114 +			iOut->writeString(field->Value());
   1.115 +			iOut->writeNewLine();
   1.116 +			}
   1.117 +		field = sIssuer->FieldByName(KWTLSCommonName);
   1.118 +		if (field)
   1.119 +			{
   1.120 +			iOut->writeString(field->Type());
   1.121 +			iOut->writeString(_L(" = "));
   1.122 +			iOut->writeString(field->Value());
   1.123 +			iOut->writeNewLine();
   1.124 +			}
   1.125 +		delete sIssuer;
   1.126 +		}
   1.127 +
   1.128 +	CWTLSStructuredText* sSubject = NULL;
   1.129 +	TRAP(err, sSubject = CWTLSStructuredText::NewL(cert->SubjectName().NameData()));
   1.130 +	if (err != KErrNone)
   1.131 +		{
   1.132 +		if (err == KErrNoMemory)
   1.133 +			{
   1.134 +			User::Leave(err);
   1.135 +			}
   1.136 +		}
   1.137 +	else
   1.138 +		{
   1.139 +		TInt count = sSubject->Count();
   1.140 +		for (TInt i = 0; i < count; i++)
   1.141 +			{
   1.142 +			const TWTLSStructuredTextField& field = sSubject->FieldByIndex(i);
   1.143 +			iOut->writeString(field.Type());
   1.144 +			iOut->writeString(_L(" = "));
   1.145 +			iOut->writeString(field.Value());
   1.146 +			iOut->writeNewLine();
   1.147 +			}
   1.148 +		delete sSubject;
   1.149 +		}
   1.150 +
   1.151 +	if (cert->IsSelfSignedL())
   1.152 +		{
   1.153 +		const CWTLSName& subject = cert->SubjectName();
   1.154 +		const CWTLSName& issuer = cert->IssuerName();
   1.155 +		if ((subject.NameType() == EWTLSText) && (issuer.NameType() == EWTLSText))
   1.156 +			{
   1.157 +			CWTLSText* sText = CWTLSText::NewLC(subject.NameData());
   1.158 +			CWTLSText* iText = CWTLSText::NewLC(issuer.NameData());
   1.159 +			__ASSERT_ALWAYS((sText->ExactMatchL(*iText)),User::Panic(_L("CERT"),0));
   1.160 +			CleanupStack::PopAndDestroy(2);
   1.161 +			}
   1.162 +
   1.163 +		}
   1.164 +
   1.165 +
   1.166 +	CleanupStack::PopAndDestroy();
   1.167 +	}
   1.168 +
   1.169 +void WTLSCertWriter::ShowName(const CWTLSName& aName)
   1.170 +	{
   1.171 +	if (aName.NameType() == EWTLSText)
   1.172 +		{
   1.173 +		CWTLSText* text = CWTLSText::NewL(aName.NameData());
   1.174 +		CleanupStack::PushL(text);
   1.175 +		iOut->writeString(_L("Char set = "));
   1.176 +		iOut->writeNum(text->CharacterSet());
   1.177 +		iOut->writeNewLine();
   1.178 +		iOut->writeString(_L("Text = "));
   1.179 +		iOut->writeString(text->Name());
   1.180 +		iOut->writeNewLine();	
   1.181 +		CleanupStack::PopAndDestroy();
   1.182 +		}
   1.183 +//	iOut->writeString(aName.Name());
   1.184 +//	iOut->writeNewLine();
   1.185 +	iOut->writeOctetStringL(aName.NameData());
   1.186 +	}
   1.187 +
   1.188 +void WTLSCertWriter::ShowPublicKey(const CWTLSCertificate& aCertificate)
   1.189 +	{
   1.190 +	const CSubjectPublicKeyInfo& spki = aCertificate.PublicKey();
   1.191 +	switch(spki.AlgorithmId())
   1.192 +		{
   1.193 +		case ERSA:
   1.194 +			{
   1.195 +			ShowRSAKey(spki);
   1.196 +			break;
   1.197 +			}
   1.198 +		default:
   1.199 +			//ignore any other algorithm
   1.200 +			break;
   1.201 +		}
   1.202 +	}
   1.203 +
   1.204 +void WTLSCertWriter::ShowRSAKey(const CSubjectPublicKeyInfo& aSpki)
   1.205 +	{
   1.206 +	CRSAPublicKey* key = CWTLSRSAPublicKey::NewLC(aSpki.KeyData());
   1.207 +	iOut->writeString(_L("RSA Key:"));
   1.208 +	iOut->writeNewLine();
   1.209 +	iOut->writeSpaces(4);
   1.210 +	iOut->writeString(_L("Modulus = "));
   1.211 +	iOut->writeNewLine();
   1.212 +	const TInteger& mod = key->N();
   1.213 +	HBufC8* mBuf = mod.BufferLC();
   1.214 +	iOut->writeOctetStringL(mBuf->Des());
   1.215 +	iOut->writeNewLine();	
   1.216 +
   1.217 +	iOut->writeSpaces(4);
   1.218 +	iOut->writeString(_L("Exponent = "));
   1.219 +	iOut->writeNewLine();
   1.220 +	const TInteger& exp = key->E();
   1.221 +	HBufC8* eBuf = exp.BufferLC();
   1.222 +	iOut->writeOctetString(eBuf->Des());
   1.223 +	iOut->writeNewLine();	
   1.224 +	CleanupStack::PopAndDestroy(3);
   1.225 +	}
   1.226 +
   1.227 +void WTLSCertWriter::ShowSigningAlgorithm(const CSigningAlgorithmIdentifier& aSigningAlgorithm)
   1.228 +	{
   1.229 +	iOut->writeString(_L("Signed using: "));
   1.230 +	iOut->writeNewLine();
   1.231 +	iOut->writeSpaces(4);
   1.232 +	iOut->writeString(_L("Asymmetric algorithm = "));
   1.233 +	const CAlgorithmIdentifier& algId = aSigningAlgorithm.AsymmetricAlgorithm();
   1.234 +	switch(algId.Algorithm())
   1.235 +	{
   1.236 +	case ERSA:
   1.237 +		{
   1.238 +		iOut->writeString(_L("RSA"));
   1.239 +		break;
   1.240 +		}
   1.241 +	case EDSA:
   1.242 +		{
   1.243 +		iOut->writeString(_L("DSA"));
   1.244 +		break;
   1.245 +		}
   1.246 +	case EDH:
   1.247 +		{
   1.248 +		iOut->writeString(_L("DH"));
   1.249 +		break;
   1.250 +		}
   1.251 +	default:
   1.252 +		{
   1.253 +		iOut->writeString(_L("Unknown"));
   1.254 +		break;
   1.255 +		}
   1.256 +	}
   1.257 +	iOut->writeNewLine();
   1.258 +	iOut->writeSpaces(4);
   1.259 +	iOut->writeString(_L("Digest algorithm = "));
   1.260 +	const CAlgorithmIdentifier& digestId = aSigningAlgorithm.DigestAlgorithm();
   1.261 +	switch(digestId.Algorithm())
   1.262 +	{
   1.263 +	case EMD2:
   1.264 +		{
   1.265 +		iOut->writeString(_L("MD2"));
   1.266 +		break;
   1.267 +		}
   1.268 +	case EMD5:
   1.269 +		{
   1.270 +		iOut->writeString(_L("MD5"));
   1.271 +		break;
   1.272 +		}
   1.273 +	case ESHA1:
   1.274 +		{
   1.275 +		iOut->writeString(_L("SHA1"));
   1.276 +		break;
   1.277 +		}
   1.278 +	default:
   1.279 +		{
   1.280 +		iOut->writeString(_L("Unknown"));
   1.281 +		break;
   1.282 +		}
   1.283 +	}
   1.284 +	iOut->writeNewLine();
   1.285 +	}
   1.286 +
   1.287 +void WTLSCertWriter::ShowSerialNumber(const TPtrC8& aSerialNumber)
   1.288 +	{
   1.289 +	iOut->writeString(_L("Serial Number = "));
   1.290 +	iOut->writeOctetString(aSerialNumber);
   1.291 +	iOut->writeNewLine();
   1.292 +	}
   1.293 +
   1.294 +void WTLSCertWriter::ShowFingerprint(const CWTLSCertificate& aCertificate)
   1.295 +	{
   1.296 +	iOut->writeOctetString(aCertificate.Fingerprint());
   1.297 +	iOut->writeNewLine();
   1.298 +	}
   1.299 +
   1.300 +void WTLSCertWriter::ShowValidityPeriod(const CWTLSCertificate& aCertificate)
   1.301 +	{
   1.302 +	const CValidityPeriod& vp = aCertificate.ValidityPeriod();
   1.303 +	iOut->writeString(_L("Validity Period = "));
   1.304 +	iOut->writeNewLine();
   1.305 +	const TTime& start = vp.Start();
   1.306 +	const TTime& finish = vp.Finish();
   1.307 +	TBuf<30> dateString1;
   1.308 +	start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
   1.309 +
   1.310 +	iOut->writeSpaces(4);
   1.311 +	iOut->writeString(_L("Valid From = "));
   1.312 +	iOut->writeString(dateString1);
   1.313 +	iOut->writeNewLine();
   1.314 +
   1.315 +	TBuf<30> dateString2;
   1.316 +	finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
   1.317 +
   1.318 +	iOut->writeSpaces(4);
   1.319 +	iOut->writeString(_L("Valid Until = "));
   1.320 +
   1.321 +	iOut->writeString(dateString2);
   1.322 +	iOut->writeNewLine();
   1.323 +	}
   1.324 +
   1.325 +void WTLSCertWriter::WriteEncodings(const CWTLSCertificate& aCertificate)
   1.326 +	{
   1.327 +	iOut->writeString(_L("Version:"));
   1.328 +	iOut->writeNewLine();
   1.329 +	const TPtrC8* encoding = Encoding(aCertificate, CWTLSCertificate::EVersionNumber);
   1.330 +	if (encoding)
   1.331 +		{
   1.332 +		iOut->writeOctetString(*encoding);
   1.333 +		}
   1.334 +	iOut->writeNewLine();
   1.335 +
   1.336 +	iOut->writeString(_L("Algorithm:"));
   1.337 +	iOut->writeNewLine();
   1.338 +	encoding = Encoding(aCertificate, CWTLSCertificate::EAlgorithmId);
   1.339 +	if (encoding)
   1.340 +		{
   1.341 +		CWTLSSigningAlgorithmIdentifier* algId = CWTLSSigningAlgorithmIdentifier::NewL(*encoding);
   1.342 +		CleanupStack::PushL(algId);
   1.343 +		__ASSERT_ALWAYS((*algId==aCertificate.SigningAlgorithm()),User::Panic(_L("CERT"),0));
   1.344 +		iOut->writeOctetString(*encoding);
   1.345 +		CleanupStack::PopAndDestroy();
   1.346 +		}
   1.347 +	iOut->writeNewLine();
   1.348 +
   1.349 +	iOut->writeString(_L("Issuer:"));
   1.350 +	iOut->writeNewLine();
   1.351 +	encoding = Encoding(aCertificate, CWTLSCertificate::EIssuerName);
   1.352 +	if (encoding)
   1.353 +		{
   1.354 +		CWTLSName* name = CWTLSName::NewL(*encoding);
   1.355 +		CleanupStack::PushL(name);
   1.356 +		__ASSERT_ALWAYS(	(name->ExactMatchL(aCertificate.IssuerName())	),User::Panic(_L("CERT"),0));
   1.357 +		iOut->writeOctetStringL(*encoding);
   1.358 +		CleanupStack::PopAndDestroy();
   1.359 +		}
   1.360 +	iOut->writeNewLine();
   1.361 +
   1.362 +	iOut->writeString(_L("Validity:"));
   1.363 +	iOut->writeNewLine();
   1.364 +	encoding = Encoding(aCertificate, CWTLSCertificate::EValidityPeriod);
   1.365 +	if (encoding)
   1.366 +		{
   1.367 +		CValidityPeriod* val  = CWTLSValidityPeriod::NewL(*encoding);
   1.368 +		CleanupStack::PushL(val);
   1.369 +		__ASSERT_ALWAYS(((val->Start() == aCertificate.ValidityPeriod().Start()) && (val->Finish() == aCertificate.ValidityPeriod().Finish())),User::Panic(_L("CERT"),0));
   1.370 +		iOut->writeOctetString(*encoding);
   1.371 +		CleanupStack::PopAndDestroy();
   1.372 +		}
   1.373 +	iOut->writeNewLine();
   1.374 +
   1.375 +	iOut->writeString(_L("Subject:"));
   1.376 +	iOut->writeNewLine();
   1.377 +	encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectName);
   1.378 +	if (encoding)
   1.379 +		{
   1.380 +		CWTLSName* name = CWTLSName::NewL(*encoding);
   1.381 +		CleanupStack::PushL(name);
   1.382 +		__ASSERT_ALWAYS(	(name->ExactMatchL(aCertificate.SubjectName())	),User::Panic(_L("CERT"),0));
   1.383 +		iOut->writeOctetStringL(*encoding);
   1.384 +		CleanupStack::PopAndDestroy();
   1.385 +		}
   1.386 +	iOut->writeNewLine();
   1.387 +
   1.388 +	iOut->writeString(_L("Public Key:"));
   1.389 +	iOut->writeNewLine();
   1.390 +	encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectPublicKeyInfo);
   1.391 +	if (encoding)
   1.392 +		{
   1.393 +		CSubjectPublicKeyInfo* info  = CWTLSSubjectPublicKeyInfo::NewL(*encoding);
   1.394 +		CleanupStack::PushL(info);
   1.395 +		__ASSERT_ALWAYS((info->KeyData() == aCertificate.PublicKey().KeyData()),User::Panic(_L("CERT"),0));
   1.396 +		iOut->writeOctetStringL(*encoding);
   1.397 +		CleanupStack::PopAndDestroy();
   1.398 +		}
   1.399 +	iOut->writeNewLine();
   1.400 +	}
   1.401 +
   1.402 +const TPtrC8* WTLSCertWriter::Encoding(const CWTLSCertificate& aCertificate, const TUint aIndex)
   1.403 +	{
   1.404 +	if (aCertificate.DataElementEncoding(aIndex))
   1.405 +		{
   1.406 +		return aCertificate.DataElementEncoding(aIndex);
   1.407 +		}
   1.408 +	return NULL;
   1.409 +	}
   1.410 +