os/security/securityanddataprivacytools/securitytools/certapp/encdec/certinfo.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/securityanddataprivacytools/securitytools/certapp/encdec/certinfo.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,282 @@
1.4 +/*
1.5 +* Copyright (c) 2008-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 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "certinfo.h"
1.23 +#include "stringconv.h"
1.24 +#define KReadOnlyFlagMask 128
1.25 +
1.26 +static const EnumEntry enumDetailsForTBool[] =
1.27 +{
1.28 + { "false", false},
1.29 + { "true", true},
1.30 + { "EFalse", false},
1.31 + { "ETrue", true},
1.32 + { 0,0 }
1.33 +};
1.34 +
1.35 +
1.36 +// Enum values for TCertificateFormat
1.37 +static const EnumEntry enumDetailsForTCertificateFormat[] =
1.38 +{
1.39 + { "EX509Certificate", EX509Certificate},
1.40 + { "EWTLSCertificate", EWTLSCertificate},
1.41 + { "EX968Certificate", EX968Certificate},
1.42 + { "EUnknownCertificate", EUnknownCertificate},
1.43 + { "EX509CertificateUrl", EX509CertificateUrl},
1.44 + { "EWTLSCertificateUrl", EWTLSCertificateUrl},
1.45 + { "EX968CertificateUrl", EX968CertificateUrl},
1.46 + { 0,0 }
1.47 +};
1.48 +
1.49 +
1.50 +static const EnumEntry enumDetailsForTCertificateOwnerType[] =
1.51 +{
1.52 + { "ECACertificate", ECACertificate},
1.53 + { "EUserCertificate", EUserCertificate},
1.54 + { "EPeerCertificate", EPeerCertificate},
1.55 + { 0,0 }
1.56 +};
1.57 +
1.58 +
1.59 +CertInfo::CertInfo(bool aSwiMode)
1.60 + : iTmpCombinedDeletableAndFormat("Deletable/Format"),
1.61 + iDeletable("Deletable", enumDetailsForTBool, aSwiMode),
1.62 + iFormat("Format", enumDetailsForTCertificateFormat),
1.63 + iSize("Size", true), // Only supported as a comment in human mode
1.64 + iLabel("Label"),
1.65 + iReadCertificateId("CertId(read)", true),
1.66 + iWriteCertificateId("CertId(write)", false),
1.67 + iCertificateOwnerType("CertOwnerType", enumDetailsForTCertificateOwnerType),
1.68 + iSubjectKeyId("SubjectKeyId"), iIssuerKeyId("IssuerKeyId"),
1.69 + iSwiMode(aSwiMode)
1.70 +
1.71 +{
1.72 + // We only need to initialise EncDecObject members which wrap non-class types
1.73 + iTmpCombinedDeletableAndFormat.Value() = 0;
1.74 + iSize.Value() = 0;
1.75 + iReadCertificateId.Value() = 0;
1.76 + iWriteCertificateId.Value() = 0;
1.77 + iCertificateOwnerType.Value() = 0;
1.78 +}
1.79 +
1.80 +
1.81 +void CertInfo::Encode(REncodeWriteStream &aWriteStream)
1.82 +{
1.83 + if(aWriteStream.HumanReadable())
1.84 + {
1.85 + aWriteStream << iDeletable;
1.86 + aWriteStream << iFormat;
1.87 + }
1.88 + else
1.89 + {
1.90 + // Write the binary field containing both format and deletable
1.91 + // flag.
1.92 + //
1.93 + // iDeletable flag is the significant digit in order to store
1.94 + // the flag without changing the externalized record
1.95 + // format. The value is OPPOSITE for backward compatibility
1.96 + iTmpCombinedDeletableAndFormat.Value() = static_cast <TUint8>(iFormat.Value() | (iDeletable.Value() ? 0 : KReadOnlyFlagMask));
1.97 + aWriteStream << iTmpCombinedDeletableAndFormat;
1.98 + }
1.99 +
1.100 +
1.101 + aWriteStream << iSize;
1.102 + if(aWriteStream.HumanReadable())
1.103 + {
1.104 + // In human readable form the label has already been written as part of the item header
1.105 + // Write out certificate ID we read in
1.106 + aWriteStream << iReadCertificateId;
1.107 + }
1.108 + else
1.109 + {
1.110 + aWriteStream << iLabel;
1.111 + aWriteStream << iWriteCertificateId;
1.112 + }
1.113 +
1.114 +
1.115 + aWriteStream << iCertificateOwnerType;
1.116 + aWriteStream << iSubjectKeyId;
1.117 + aWriteStream << iIssuerKeyId;
1.118 +}
1.119 +
1.120 +
1.121 +void CertInfo::Decode(RDecodeReadStream &aReadStream)
1.122 +{
1.123 + if(aReadStream.HumanReadable())
1.124 + {
1.125 + // Read the Deletable and Format fields
1.126 + aReadStream >> iDeletable;
1.127 + aReadStream >> iFormat;
1.128 + }
1.129 + else
1.130 + {
1.131 + // Read the binary field containing both format and deletable
1.132 + // flag.
1.133 + //
1.134 + // iDeletable flag is the significant digit in order to store
1.135 + // the flag without changing the externalized record
1.136 + // format. The value is OPPOSITE for backward compatibility
1.137 + aReadStream >> iTmpCombinedDeletableAndFormat;
1.138 +
1.139 + iDeletable.SetValue((iTmpCombinedDeletableAndFormat.Value() & KReadOnlyFlagMask) == 0);
1.140 + iFormat.SetValue((iTmpCombinedDeletableAndFormat.Value() & ~KReadOnlyFlagMask));
1.141 + }
1.142 +
1.143 + aReadStream >> iSize;
1.144 + if(!aReadStream.HumanReadable())
1.145 + {
1.146 + aReadStream >> iLabel;
1.147 + }
1.148 + aReadStream >> iReadCertificateId;
1.149 + aReadStream >> iCertificateOwnerType;
1.150 +
1.151 +
1.152 + if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iSubjectKeyId.Name()))
1.153 + {
1.154 + // Either in binary mode, or the next token is SubjectKeyId, so read the field
1.155 + aReadStream >> iSubjectKeyId;
1.156 + }
1.157 + else
1.158 + {
1.159 + // In human mode and field not present, so set it to auto
1.160 + iSubjectKeyId.Value().iAutoKey = true;
1.161 + iSubjectKeyId.Value().iHash.SetLength(0);
1.162 + }
1.163 +
1.164 + if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iIssuerKeyId.Name()))
1.165 + {
1.166 + // Either in binary mode, or the next token is IssuerKeyId, so read the field
1.167 + aReadStream >> iIssuerKeyId;
1.168 + }
1.169 + else
1.170 + {
1.171 + // In human mode and field not present, so set it to auto
1.172 + iIssuerKeyId.Value().iAutoKey = true;
1.173 + iIssuerKeyId.Value().iHash.SetLength(0);
1.174 + }
1.175 +}
1.176 +
1.177 +
1.178 +TUint32 CertInfo::CertSize() const
1.179 +{
1.180 + return iSize.Value();
1.181 +}
1.182 +
1.183 +void CertInfo::SetCertSize(TUint32 aSize)
1.184 +{
1.185 + iSize.Value() = aSize;
1.186 +}
1.187 +
1.188 +const TCertLabel &CertInfo::Label() const
1.189 +{
1.190 + return iLabel.Value();
1.191 +}
1.192 +
1.193 +TCertLabel &CertInfo::Label()
1.194 +{
1.195 + return iLabel.Value();
1.196 +}
1.197 +
1.198 +TCertificateFormat CertInfo::CertificateFormat() const
1.199 +{
1.200 + return (TCertificateFormat)iFormat.Value();
1.201 +}
1.202 +
1.203 +TCertificateOwnerType CertInfo::CertificateOwnerType() const
1.204 +{
1.205 + return (TCertificateOwnerType)iCertificateOwnerType.Value();
1.206 +}
1.207 +
1.208 +
1.209 +KeyIdentifierObject &CertInfo::SubjectKeyId()
1.210 +{
1.211 + return iSubjectKeyId.Value();
1.212 +}
1.213 +
1.214 +const KeyIdentifierObject &CertInfo::SubjectKeyId() const
1.215 +{
1.216 + return iSubjectKeyId.Value();
1.217 +}
1.218 +
1.219 +KeyIdentifierObject &CertInfo::IssuerKeyId()
1.220 +{
1.221 + return iIssuerKeyId.Value();
1.222 +}
1.223 +
1.224 +#ifdef _BullseyeCoverage
1.225 +#pragma BullseyeCoverage off
1.226 +#endif
1.227 +const KeyIdentifierObject &CertInfo::IssuerKeyId() const
1.228 +{
1.229 + return iIssuerKeyId.Value();
1.230 +}
1.231 +#ifdef _BullseyeCoverage
1.232 +#pragma BullseyeCoverage restore
1.233 +#endif
1.234 +
1.235 +TUint32 CertInfo::OutputCertificateId() const
1.236 +{
1.237 + return iWriteCertificateId.Value();
1.238 +}
1.239 +
1.240 +
1.241 +void CertInfo::SetOutputCertificateId(TUint32 aId)
1.242 +{
1.243 + iWriteCertificateId.Value() = aId;
1.244 +}
1.245 +
1.246 +
1.247 +
1.248 +
1.249 +//
1.250 +// TCertLabel
1.251 +//
1.252 +void EncodeHuman(REncodeWriteStream& aStream,const TCertLabel &aLabel)
1.253 +{
1.254 + // Compress the internal UTF-16 to human readable UTF-8
1.255 + TInt outputBytes = 0;
1.256 + TUint8 *outBuf = cstrFromUtf16(aLabel.Ptr(), aLabel.Length(), outputBytes);
1.257 +
1.258 + aStream.WriteByte('"');
1.259 + aStream.WriteQuotedUtf8(outBuf, outputBytes);
1.260 + aStream.WriteByte('"');
1.261 +
1.262 + delete [] outBuf;
1.263 +}
1.264 +void DecodeHuman(RDecodeReadStream& aStream,TCertLabel &aLabel)
1.265 +{
1.266 + aStream.ReadNextToken();
1.267 +
1.268 + // Expand UTF-8 into internal UTF-16LE representation
1.269 + TInt outputWords = 0;
1.270 + TText *outputBuf = utf16FromUtf8((const TUint8 *)aStream.Token().data(), aStream.Token().size(), outputWords);
1.271 + if(outputWords > aLabel.MaxLength())
1.272 + {
1.273 + dbg << Log::Indent() << "String too long" << Log::Endl();
1.274 + FatalError();
1.275 + }
1.276 +
1.277 + memcpy((void *)aLabel.Ptr(), outputBuf, outputWords*2);
1.278 + aLabel.SetLength(outputWords);
1.279 + delete [] outputBuf;
1.280 +}
1.281 +
1.282 +
1.283 +
1.284 +
1.285 +// End of file