sl@0: /* sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * TWTLSOUT.CPP sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "CertWriter.h" sl@0: sl@0: WTLSCertWriter::WTLSCertWriter(Output* aOut) sl@0: :iOut(aOut) sl@0: { sl@0: } sl@0: sl@0: void WTLSCertWriter::SetOut(Output* aOut) sl@0: { sl@0: iOut = aOut; sl@0: } sl@0: sl@0: void WTLSCertWriter::WriteCert(const CWTLSCertificate& aCertificate) sl@0: { sl@0: CWTLSCertificate* cert = CWTLSCertificate::NewL(aCertificate); sl@0: CleanupStack::PushL(cert); sl@0: WriteEncodings(*cert); sl@0: iOut->writeNewLine(); sl@0: iOut->writeString(_L("Version = ")); sl@0: iOut->writeNum(cert->Version()); sl@0: iOut->writeNewLine(); sl@0: ShowValidityPeriod(*cert); sl@0: iOut->writeString(_L("Issuer Name = ")); sl@0: iOut->writeNewLine(); sl@0: ShowName(cert->IssuerName()); sl@0: iOut->writeNewLine(); sl@0: iOut->writeString(_L("Subject Name = ")); sl@0: iOut->writeNewLine(); sl@0: ShowName(cert->SubjectName()); sl@0: iOut->writeNewLine(); sl@0: ShowSigningAlgorithm(cert->SigningAlgorithm()); sl@0: ShowPublicKey(aCertificate); sl@0: sl@0: iOut->writeString(_L("Short Issuer Name = ")); sl@0: HBufC* issuer = cert->IssuerL(); sl@0: iOut->writeString(*issuer); sl@0: iOut->writeNewLine(); sl@0: delete issuer; sl@0: sl@0: iOut->writeString(_L("Short Subject Name = ")); sl@0: HBufC* subject = cert->SubjectL(); sl@0: iOut->writeString(*subject); sl@0: iOut->writeNewLine(); sl@0: delete subject; sl@0: sl@0: iOut->writeString(_L("Full structured issuer & subject names")); sl@0: iOut->writeNewLine(); sl@0: CWTLSStructuredText* sIssuer = NULL; sl@0: TRAPD(err, sIssuer = CWTLSStructuredText::NewL(cert->IssuerName().NameData())); sl@0: if (err != KErrNone) sl@0: { sl@0: if (err == KErrNoMemory) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: const TWTLSStructuredTextField* field = sIssuer->FieldByName(KWTLSCountryName); sl@0: if (field) sl@0: { sl@0: iOut->writeString(field->Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field->Value()); sl@0: iOut->writeNewLine(); sl@0: __ASSERT_ALWAYS((field->Value() == sIssuer->Country()),User::Panic(_L("CERT"),0)); sl@0: } sl@0: sl@0: field = sIssuer->FieldByName(KWTLSOrganizationName); sl@0: if (field) sl@0: { sl@0: iOut->writeString(field->Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field->Value()); sl@0: iOut->writeNewLine(); sl@0: __ASSERT_ALWAYS((field->Value() == sIssuer->Organization()),User::Panic(_L("CERT"),0)); sl@0: } sl@0: field = sIssuer->FieldByName(KWTLSServiceName); sl@0: if (field) sl@0: { sl@0: iOut->writeString(field->Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field->Value()); sl@0: iOut->writeNewLine(); sl@0: __ASSERT_ALWAYS((field->Value() == sIssuer->ServiceName()),User::Panic(_L("CERT"),0)); sl@0: } sl@0: field = sIssuer->FieldByName(KWTLSTitle); sl@0: if (field) sl@0: { sl@0: iOut->writeString(field->Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field->Value()); sl@0: iOut->writeNewLine(); sl@0: } sl@0: field = sIssuer->FieldByName(KWTLSCommonName); sl@0: if (field) sl@0: { sl@0: iOut->writeString(field->Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field->Value()); sl@0: iOut->writeNewLine(); sl@0: } sl@0: delete sIssuer; sl@0: } sl@0: sl@0: CWTLSStructuredText* sSubject = NULL; sl@0: TRAP(err, sSubject = CWTLSStructuredText::NewL(cert->SubjectName().NameData())); sl@0: if (err != KErrNone) sl@0: { sl@0: if (err == KErrNoMemory) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: TInt count = sSubject->Count(); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: const TWTLSStructuredTextField& field = sSubject->FieldByIndex(i); sl@0: iOut->writeString(field.Type()); sl@0: iOut->writeString(_L(" = ")); sl@0: iOut->writeString(field.Value()); sl@0: iOut->writeNewLine(); sl@0: } sl@0: delete sSubject; sl@0: } sl@0: sl@0: if (cert->IsSelfSignedL()) sl@0: { sl@0: const CWTLSName& subject = cert->SubjectName(); sl@0: const CWTLSName& issuer = cert->IssuerName(); sl@0: if ((subject.NameType() == EWTLSText) && (issuer.NameType() == EWTLSText)) sl@0: { sl@0: CWTLSText* sText = CWTLSText::NewLC(subject.NameData()); sl@0: CWTLSText* iText = CWTLSText::NewLC(issuer.NameData()); sl@0: __ASSERT_ALWAYS((sText->ExactMatchL(*iText)),User::Panic(_L("CERT"),0)); sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: } sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowName(const CWTLSName& aName) sl@0: { sl@0: if (aName.NameType() == EWTLSText) sl@0: { sl@0: CWTLSText* text = CWTLSText::NewL(aName.NameData()); sl@0: CleanupStack::PushL(text); sl@0: iOut->writeString(_L("Char set = ")); sl@0: iOut->writeNum(text->CharacterSet()); sl@0: iOut->writeNewLine(); sl@0: iOut->writeString(_L("Text = ")); sl@0: iOut->writeString(text->Name()); sl@0: iOut->writeNewLine(); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: // iOut->writeString(aName.Name()); sl@0: // iOut->writeNewLine(); sl@0: iOut->writeOctetStringL(aName.NameData()); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowPublicKey(const CWTLSCertificate& aCertificate) sl@0: { sl@0: const CSubjectPublicKeyInfo& spki = aCertificate.PublicKey(); sl@0: switch(spki.AlgorithmId()) sl@0: { sl@0: case ERSA: sl@0: { sl@0: ShowRSAKey(spki); sl@0: break; sl@0: } sl@0: default: sl@0: //ignore any other algorithm sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowRSAKey(const CSubjectPublicKeyInfo& aSpki) sl@0: { sl@0: CRSAPublicKey* key = CWTLSRSAPublicKey::NewLC(aSpki.KeyData()); sl@0: iOut->writeString(_L("RSA Key:")); sl@0: iOut->writeNewLine(); sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Modulus = ")); sl@0: iOut->writeNewLine(); sl@0: const TInteger& mod = key->N(); sl@0: HBufC8* mBuf = mod.BufferLC(); sl@0: iOut->writeOctetStringL(mBuf->Des()); sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Exponent = ")); sl@0: iOut->writeNewLine(); sl@0: const TInteger& exp = key->E(); sl@0: HBufC8* eBuf = exp.BufferLC(); sl@0: iOut->writeOctetString(eBuf->Des()); sl@0: iOut->writeNewLine(); sl@0: CleanupStack::PopAndDestroy(3); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowSigningAlgorithm(const CSigningAlgorithmIdentifier& aSigningAlgorithm) sl@0: { sl@0: iOut->writeString(_L("Signed using: ")); sl@0: iOut->writeNewLine(); sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Asymmetric algorithm = ")); sl@0: const CAlgorithmIdentifier& algId = aSigningAlgorithm.AsymmetricAlgorithm(); sl@0: switch(algId.Algorithm()) sl@0: { sl@0: case ERSA: sl@0: { sl@0: iOut->writeString(_L("RSA")); sl@0: break; sl@0: } sl@0: case EDSA: sl@0: { sl@0: iOut->writeString(_L("DSA")); sl@0: break; sl@0: } sl@0: case EDH: sl@0: { sl@0: iOut->writeString(_L("DH")); sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: iOut->writeString(_L("Unknown")); sl@0: break; sl@0: } sl@0: } sl@0: iOut->writeNewLine(); sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Digest algorithm = ")); sl@0: const CAlgorithmIdentifier& digestId = aSigningAlgorithm.DigestAlgorithm(); sl@0: switch(digestId.Algorithm()) sl@0: { sl@0: case EMD2: sl@0: { sl@0: iOut->writeString(_L("MD2")); sl@0: break; sl@0: } sl@0: case EMD5: sl@0: { sl@0: iOut->writeString(_L("MD5")); sl@0: break; sl@0: } sl@0: case ESHA1: sl@0: { sl@0: iOut->writeString(_L("SHA1")); sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: iOut->writeString(_L("Unknown")); sl@0: break; sl@0: } sl@0: } sl@0: iOut->writeNewLine(); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowSerialNumber(const TPtrC8& aSerialNumber) sl@0: { sl@0: iOut->writeString(_L("Serial Number = ")); sl@0: iOut->writeOctetString(aSerialNumber); sl@0: iOut->writeNewLine(); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowFingerprint(const CWTLSCertificate& aCertificate) sl@0: { sl@0: iOut->writeOctetString(aCertificate.Fingerprint()); sl@0: iOut->writeNewLine(); sl@0: } sl@0: sl@0: void WTLSCertWriter::ShowValidityPeriod(const CWTLSCertificate& aCertificate) sl@0: { sl@0: const CValidityPeriod& vp = aCertificate.ValidityPeriod(); sl@0: iOut->writeString(_L("Validity Period = ")); sl@0: iOut->writeNewLine(); sl@0: const TTime& start = vp.Start(); sl@0: const TTime& finish = vp.Finish(); sl@0: TBuf<30> dateString1; sl@0: start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3"))); sl@0: sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Valid From = ")); sl@0: iOut->writeString(dateString1); sl@0: iOut->writeNewLine(); sl@0: sl@0: TBuf<30> dateString2; sl@0: finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3"))); sl@0: sl@0: iOut->writeSpaces(4); sl@0: iOut->writeString(_L("Valid Until = ")); sl@0: sl@0: iOut->writeString(dateString2); sl@0: iOut->writeNewLine(); sl@0: } sl@0: sl@0: void WTLSCertWriter::WriteEncodings(const CWTLSCertificate& aCertificate) sl@0: { sl@0: iOut->writeString(_L("Version:")); sl@0: iOut->writeNewLine(); sl@0: const TPtrC8* encoding = Encoding(aCertificate, CWTLSCertificate::EVersionNumber); sl@0: if (encoding) sl@0: { sl@0: iOut->writeOctetString(*encoding); sl@0: } sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeString(_L("Algorithm:")); sl@0: iOut->writeNewLine(); sl@0: encoding = Encoding(aCertificate, CWTLSCertificate::EAlgorithmId); sl@0: if (encoding) sl@0: { sl@0: CWTLSSigningAlgorithmIdentifier* algId = CWTLSSigningAlgorithmIdentifier::NewL(*encoding); sl@0: CleanupStack::PushL(algId); sl@0: __ASSERT_ALWAYS((*algId==aCertificate.SigningAlgorithm()),User::Panic(_L("CERT"),0)); sl@0: iOut->writeOctetString(*encoding); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeString(_L("Issuer:")); sl@0: iOut->writeNewLine(); sl@0: encoding = Encoding(aCertificate, CWTLSCertificate::EIssuerName); sl@0: if (encoding) sl@0: { sl@0: CWTLSName* name = CWTLSName::NewL(*encoding); sl@0: CleanupStack::PushL(name); sl@0: __ASSERT_ALWAYS( (name->ExactMatchL(aCertificate.IssuerName()) ),User::Panic(_L("CERT"),0)); sl@0: iOut->writeOctetStringL(*encoding); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeString(_L("Validity:")); sl@0: iOut->writeNewLine(); sl@0: encoding = Encoding(aCertificate, CWTLSCertificate::EValidityPeriod); sl@0: if (encoding) sl@0: { sl@0: CValidityPeriod* val = CWTLSValidityPeriod::NewL(*encoding); sl@0: CleanupStack::PushL(val); sl@0: __ASSERT_ALWAYS(((val->Start() == aCertificate.ValidityPeriod().Start()) && (val->Finish() == aCertificate.ValidityPeriod().Finish())),User::Panic(_L("CERT"),0)); sl@0: iOut->writeOctetString(*encoding); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeString(_L("Subject:")); sl@0: iOut->writeNewLine(); sl@0: encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectName); sl@0: if (encoding) sl@0: { sl@0: CWTLSName* name = CWTLSName::NewL(*encoding); sl@0: CleanupStack::PushL(name); sl@0: __ASSERT_ALWAYS( (name->ExactMatchL(aCertificate.SubjectName()) ),User::Panic(_L("CERT"),0)); sl@0: iOut->writeOctetStringL(*encoding); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iOut->writeNewLine(); sl@0: sl@0: iOut->writeString(_L("Public Key:")); sl@0: iOut->writeNewLine(); sl@0: encoding = Encoding(aCertificate, CWTLSCertificate::ESubjectPublicKeyInfo); sl@0: if (encoding) sl@0: { sl@0: CSubjectPublicKeyInfo* info = CWTLSSubjectPublicKeyInfo::NewL(*encoding); sl@0: CleanupStack::PushL(info); sl@0: __ASSERT_ALWAYS((info->KeyData() == aCertificate.PublicKey().KeyData()),User::Panic(_L("CERT"),0)); sl@0: iOut->writeOctetStringL(*encoding); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iOut->writeNewLine(); sl@0: } sl@0: sl@0: const TPtrC8* WTLSCertWriter::Encoding(const CWTLSCertificate& aCertificate, const TUint aIndex) sl@0: { sl@0: if (aCertificate.DataElementEncoding(aIndex)) sl@0: { sl@0: return aCertificate.DataElementEncoding(aIndex); sl@0: } sl@0: return NULL; sl@0: } sl@0: