os/security/securityanddataprivacytools/securitytools/certapp/encdec/certinfo.cpp
First public contribution.
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #include "stringconv.h"
21 #define KReadOnlyFlagMask 128
23 static const EnumEntry enumDetailsForTBool[] =
33 // Enum values for TCertificateFormat
34 static const EnumEntry enumDetailsForTCertificateFormat[] =
36 { "EX509Certificate", EX509Certificate},
37 { "EWTLSCertificate", EWTLSCertificate},
38 { "EX968Certificate", EX968Certificate},
39 { "EUnknownCertificate", EUnknownCertificate},
40 { "EX509CertificateUrl", EX509CertificateUrl},
41 { "EWTLSCertificateUrl", EWTLSCertificateUrl},
42 { "EX968CertificateUrl", EX968CertificateUrl},
47 static const EnumEntry enumDetailsForTCertificateOwnerType[] =
49 { "ECACertificate", ECACertificate},
50 { "EUserCertificate", EUserCertificate},
51 { "EPeerCertificate", EPeerCertificate},
56 CertInfo::CertInfo(bool aSwiMode)
57 : iTmpCombinedDeletableAndFormat("Deletable/Format"),
58 iDeletable("Deletable", enumDetailsForTBool, aSwiMode),
59 iFormat("Format", enumDetailsForTCertificateFormat),
60 iSize("Size", true), // Only supported as a comment in human mode
62 iReadCertificateId("CertId(read)", true),
63 iWriteCertificateId("CertId(write)", false),
64 iCertificateOwnerType("CertOwnerType", enumDetailsForTCertificateOwnerType),
65 iSubjectKeyId("SubjectKeyId"), iIssuerKeyId("IssuerKeyId"),
69 // We only need to initialise EncDecObject members which wrap non-class types
70 iTmpCombinedDeletableAndFormat.Value() = 0;
72 iReadCertificateId.Value() = 0;
73 iWriteCertificateId.Value() = 0;
74 iCertificateOwnerType.Value() = 0;
78 void CertInfo::Encode(REncodeWriteStream &aWriteStream)
80 if(aWriteStream.HumanReadable())
82 aWriteStream << iDeletable;
83 aWriteStream << iFormat;
87 // Write the binary field containing both format and deletable
90 // iDeletable flag is the significant digit in order to store
91 // the flag without changing the externalized record
92 // format. The value is OPPOSITE for backward compatibility
93 iTmpCombinedDeletableAndFormat.Value() = static_cast <TUint8>(iFormat.Value() | (iDeletable.Value() ? 0 : KReadOnlyFlagMask));
94 aWriteStream << iTmpCombinedDeletableAndFormat;
98 aWriteStream << iSize;
99 if(aWriteStream.HumanReadable())
101 // In human readable form the label has already been written as part of the item header
102 // Write out certificate ID we read in
103 aWriteStream << iReadCertificateId;
107 aWriteStream << iLabel;
108 aWriteStream << iWriteCertificateId;
112 aWriteStream << iCertificateOwnerType;
113 aWriteStream << iSubjectKeyId;
114 aWriteStream << iIssuerKeyId;
118 void CertInfo::Decode(RDecodeReadStream &aReadStream)
120 if(aReadStream.HumanReadable())
122 // Read the Deletable and Format fields
123 aReadStream >> iDeletable;
124 aReadStream >> iFormat;
128 // Read the binary field containing both format and deletable
131 // iDeletable flag is the significant digit in order to store
132 // the flag without changing the externalized record
133 // format. The value is OPPOSITE for backward compatibility
134 aReadStream >> iTmpCombinedDeletableAndFormat;
136 iDeletable.SetValue((iTmpCombinedDeletableAndFormat.Value() & KReadOnlyFlagMask) == 0);
137 iFormat.SetValue((iTmpCombinedDeletableAndFormat.Value() & ~KReadOnlyFlagMask));
140 aReadStream >> iSize;
141 if(!aReadStream.HumanReadable())
143 aReadStream >> iLabel;
145 aReadStream >> iReadCertificateId;
146 aReadStream >> iCertificateOwnerType;
149 if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iSubjectKeyId.Name()))
151 // Either in binary mode, or the next token is SubjectKeyId, so read the field
152 aReadStream >> iSubjectKeyId;
156 // In human mode and field not present, so set it to auto
157 iSubjectKeyId.Value().iAutoKey = true;
158 iSubjectKeyId.Value().iHash.SetLength(0);
161 if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iIssuerKeyId.Name()))
163 // Either in binary mode, or the next token is IssuerKeyId, so read the field
164 aReadStream >> iIssuerKeyId;
168 // In human mode and field not present, so set it to auto
169 iIssuerKeyId.Value().iAutoKey = true;
170 iIssuerKeyId.Value().iHash.SetLength(0);
175 TUint32 CertInfo::CertSize() const
177 return iSize.Value();
180 void CertInfo::SetCertSize(TUint32 aSize)
182 iSize.Value() = aSize;
185 const TCertLabel &CertInfo::Label() const
187 return iLabel.Value();
190 TCertLabel &CertInfo::Label()
192 return iLabel.Value();
195 TCertificateFormat CertInfo::CertificateFormat() const
197 return (TCertificateFormat)iFormat.Value();
200 TCertificateOwnerType CertInfo::CertificateOwnerType() const
202 return (TCertificateOwnerType)iCertificateOwnerType.Value();
206 KeyIdentifierObject &CertInfo::SubjectKeyId()
208 return iSubjectKeyId.Value();
211 const KeyIdentifierObject &CertInfo::SubjectKeyId() const
213 return iSubjectKeyId.Value();
216 KeyIdentifierObject &CertInfo::IssuerKeyId()
218 return iIssuerKeyId.Value();
221 #ifdef _BullseyeCoverage
222 #pragma BullseyeCoverage off
224 const KeyIdentifierObject &CertInfo::IssuerKeyId() const
226 return iIssuerKeyId.Value();
228 #ifdef _BullseyeCoverage
229 #pragma BullseyeCoverage restore
232 TUint32 CertInfo::OutputCertificateId() const
234 return iWriteCertificateId.Value();
238 void CertInfo::SetOutputCertificateId(TUint32 aId)
240 iWriteCertificateId.Value() = aId;
249 void EncodeHuman(REncodeWriteStream& aStream,const TCertLabel &aLabel)
251 // Compress the internal UTF-16 to human readable UTF-8
252 TInt outputBytes = 0;
253 TUint8 *outBuf = cstrFromUtf16(aLabel.Ptr(), aLabel.Length(), outputBytes);
255 aStream.WriteByte('"');
256 aStream.WriteQuotedUtf8(outBuf, outputBytes);
257 aStream.WriteByte('"');
261 void DecodeHuman(RDecodeReadStream& aStream,TCertLabel &aLabel)
263 aStream.ReadNextToken();
265 // Expand UTF-8 into internal UTF-16LE representation
266 TInt outputWords = 0;
267 TText *outputBuf = utf16FromUtf8((const TUint8 *)aStream.Token().data(), aStream.Token().size(), outputWords);
268 if(outputWords > aLabel.MaxLength())
270 dbg << Log::Indent() << "String too long" << Log::Endl();
274 memcpy((void *)aLabel.Ptr(), outputBuf, outputWords*2);
275 aLabel.SetLength(outputWords);