sl@0: /* sl@0: * Copyright (c) 2008-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: * sl@0: */ sl@0: sl@0: sl@0: #include "certinfo.h" sl@0: #include "stringconv.h" sl@0: #define KReadOnlyFlagMask 128 sl@0: sl@0: static const EnumEntry enumDetailsForTBool[] = sl@0: { sl@0: { "false", false}, sl@0: { "true", true}, sl@0: { "EFalse", false}, sl@0: { "ETrue", true}, sl@0: { 0,0 } sl@0: }; sl@0: sl@0: sl@0: // Enum values for TCertificateFormat sl@0: static const EnumEntry enumDetailsForTCertificateFormat[] = sl@0: { sl@0: { "EX509Certificate", EX509Certificate}, sl@0: { "EWTLSCertificate", EWTLSCertificate}, sl@0: { "EX968Certificate", EX968Certificate}, sl@0: { "EUnknownCertificate", EUnknownCertificate}, sl@0: { "EX509CertificateUrl", EX509CertificateUrl}, sl@0: { "EWTLSCertificateUrl", EWTLSCertificateUrl}, sl@0: { "EX968CertificateUrl", EX968CertificateUrl}, sl@0: { 0,0 } sl@0: }; sl@0: sl@0: sl@0: static const EnumEntry enumDetailsForTCertificateOwnerType[] = sl@0: { sl@0: { "ECACertificate", ECACertificate}, sl@0: { "EUserCertificate", EUserCertificate}, sl@0: { "EPeerCertificate", EPeerCertificate}, sl@0: { 0,0 } sl@0: }; sl@0: sl@0: sl@0: CertInfo::CertInfo(bool aSwiMode) sl@0: : iTmpCombinedDeletableAndFormat("Deletable/Format"), sl@0: iDeletable("Deletable", enumDetailsForTBool, aSwiMode), sl@0: iFormat("Format", enumDetailsForTCertificateFormat), sl@0: iSize("Size", true), // Only supported as a comment in human mode sl@0: iLabel("Label"), sl@0: iReadCertificateId("CertId(read)", true), sl@0: iWriteCertificateId("CertId(write)", false), sl@0: iCertificateOwnerType("CertOwnerType", enumDetailsForTCertificateOwnerType), sl@0: iSubjectKeyId("SubjectKeyId"), iIssuerKeyId("IssuerKeyId"), sl@0: iSwiMode(aSwiMode) sl@0: sl@0: { sl@0: // We only need to initialise EncDecObject members which wrap non-class types sl@0: iTmpCombinedDeletableAndFormat.Value() = 0; sl@0: iSize.Value() = 0; sl@0: iReadCertificateId.Value() = 0; sl@0: iWriteCertificateId.Value() = 0; sl@0: iCertificateOwnerType.Value() = 0; sl@0: } sl@0: sl@0: sl@0: void CertInfo::Encode(REncodeWriteStream &aWriteStream) sl@0: { sl@0: if(aWriteStream.HumanReadable()) sl@0: { sl@0: aWriteStream << iDeletable; sl@0: aWriteStream << iFormat; sl@0: } sl@0: else sl@0: { sl@0: // Write the binary field containing both format and deletable sl@0: // flag. sl@0: // sl@0: // iDeletable flag is the significant digit in order to store sl@0: // the flag without changing the externalized record sl@0: // format. The value is OPPOSITE for backward compatibility sl@0: iTmpCombinedDeletableAndFormat.Value() = static_cast (iFormat.Value() | (iDeletable.Value() ? 0 : KReadOnlyFlagMask)); sl@0: aWriteStream << iTmpCombinedDeletableAndFormat; sl@0: } sl@0: sl@0: sl@0: aWriteStream << iSize; sl@0: if(aWriteStream.HumanReadable()) sl@0: { sl@0: // In human readable form the label has already been written as part of the item header sl@0: // Write out certificate ID we read in sl@0: aWriteStream << iReadCertificateId; sl@0: } sl@0: else sl@0: { sl@0: aWriteStream << iLabel; sl@0: aWriteStream << iWriteCertificateId; sl@0: } sl@0: sl@0: sl@0: aWriteStream << iCertificateOwnerType; sl@0: aWriteStream << iSubjectKeyId; sl@0: aWriteStream << iIssuerKeyId; sl@0: } sl@0: sl@0: sl@0: void CertInfo::Decode(RDecodeReadStream &aReadStream) sl@0: { sl@0: if(aReadStream.HumanReadable()) sl@0: { sl@0: // Read the Deletable and Format fields sl@0: aReadStream >> iDeletable; sl@0: aReadStream >> iFormat; sl@0: } sl@0: else sl@0: { sl@0: // Read the binary field containing both format and deletable sl@0: // flag. sl@0: // sl@0: // iDeletable flag is the significant digit in order to store sl@0: // the flag without changing the externalized record sl@0: // format. The value is OPPOSITE for backward compatibility sl@0: aReadStream >> iTmpCombinedDeletableAndFormat; sl@0: sl@0: iDeletable.SetValue((iTmpCombinedDeletableAndFormat.Value() & KReadOnlyFlagMask) == 0); sl@0: iFormat.SetValue((iTmpCombinedDeletableAndFormat.Value() & ~KReadOnlyFlagMask)); sl@0: } sl@0: sl@0: aReadStream >> iSize; sl@0: if(!aReadStream.HumanReadable()) sl@0: { sl@0: aReadStream >> iLabel; sl@0: } sl@0: aReadStream >> iReadCertificateId; sl@0: aReadStream >> iCertificateOwnerType; sl@0: sl@0: sl@0: if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iSubjectKeyId.Name())) sl@0: { sl@0: // Either in binary mode, or the next token is SubjectKeyId, so read the field sl@0: aReadStream >> iSubjectKeyId; sl@0: } sl@0: else sl@0: { sl@0: // In human mode and field not present, so set it to auto sl@0: iSubjectKeyId.Value().iAutoKey = true; sl@0: iSubjectKeyId.Value().iHash.SetLength(0); sl@0: } sl@0: sl@0: if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iIssuerKeyId.Name())) sl@0: { sl@0: // Either in binary mode, or the next token is IssuerKeyId, so read the field sl@0: aReadStream >> iIssuerKeyId; sl@0: } sl@0: else sl@0: { sl@0: // In human mode and field not present, so set it to auto sl@0: iIssuerKeyId.Value().iAutoKey = true; sl@0: iIssuerKeyId.Value().iHash.SetLength(0); sl@0: } sl@0: } sl@0: sl@0: sl@0: TUint32 CertInfo::CertSize() const sl@0: { sl@0: return iSize.Value(); sl@0: } sl@0: sl@0: void CertInfo::SetCertSize(TUint32 aSize) sl@0: { sl@0: iSize.Value() = aSize; sl@0: } sl@0: sl@0: const TCertLabel &CertInfo::Label() const sl@0: { sl@0: return iLabel.Value(); sl@0: } sl@0: sl@0: TCertLabel &CertInfo::Label() sl@0: { sl@0: return iLabel.Value(); sl@0: } sl@0: sl@0: TCertificateFormat CertInfo::CertificateFormat() const sl@0: { sl@0: return (TCertificateFormat)iFormat.Value(); sl@0: } sl@0: sl@0: TCertificateOwnerType CertInfo::CertificateOwnerType() const sl@0: { sl@0: return (TCertificateOwnerType)iCertificateOwnerType.Value(); sl@0: } sl@0: sl@0: sl@0: KeyIdentifierObject &CertInfo::SubjectKeyId() sl@0: { sl@0: return iSubjectKeyId.Value(); sl@0: } sl@0: sl@0: const KeyIdentifierObject &CertInfo::SubjectKeyId() const sl@0: { sl@0: return iSubjectKeyId.Value(); sl@0: } sl@0: sl@0: KeyIdentifierObject &CertInfo::IssuerKeyId() sl@0: { sl@0: return iIssuerKeyId.Value(); sl@0: } sl@0: sl@0: #ifdef _BullseyeCoverage sl@0: #pragma BullseyeCoverage off sl@0: #endif sl@0: const KeyIdentifierObject &CertInfo::IssuerKeyId() const sl@0: { sl@0: return iIssuerKeyId.Value(); sl@0: } sl@0: #ifdef _BullseyeCoverage sl@0: #pragma BullseyeCoverage restore sl@0: #endif sl@0: sl@0: TUint32 CertInfo::OutputCertificateId() const sl@0: { sl@0: return iWriteCertificateId.Value(); sl@0: } sl@0: sl@0: sl@0: void CertInfo::SetOutputCertificateId(TUint32 aId) sl@0: { sl@0: iWriteCertificateId.Value() = aId; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: // sl@0: // TCertLabel sl@0: // sl@0: void EncodeHuman(REncodeWriteStream& aStream,const TCertLabel &aLabel) sl@0: { sl@0: // Compress the internal UTF-16 to human readable UTF-8 sl@0: TInt outputBytes = 0; sl@0: TUint8 *outBuf = cstrFromUtf16(aLabel.Ptr(), aLabel.Length(), outputBytes); sl@0: sl@0: aStream.WriteByte('"'); sl@0: aStream.WriteQuotedUtf8(outBuf, outputBytes); sl@0: aStream.WriteByte('"'); sl@0: sl@0: delete [] outBuf; sl@0: } sl@0: void DecodeHuman(RDecodeReadStream& aStream,TCertLabel &aLabel) sl@0: { sl@0: aStream.ReadNextToken(); sl@0: sl@0: // Expand UTF-8 into internal UTF-16LE representation sl@0: TInt outputWords = 0; sl@0: TText *outputBuf = utf16FromUtf8((const TUint8 *)aStream.Token().data(), aStream.Token().size(), outputWords); sl@0: if(outputWords > aLabel.MaxLength()) sl@0: { sl@0: dbg << Log::Indent() << "String too long" << Log::Endl(); sl@0: FatalError(); sl@0: } sl@0: sl@0: memcpy((void *)aLabel.Ptr(), outputBuf, outputWords*2); sl@0: aLabel.SetLength(outputWords); sl@0: delete [] outputBuf; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: // End of file