os/security/securityanddataprivacytools/securitytools/certapp/encdec/certinfo.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "certinfo.h"
    20 #include "stringconv.h"
    21 #define KReadOnlyFlagMask 128
    22 
    23 static const EnumEntry enumDetailsForTBool[] =
    24 {
    25     { "false", false},
    26     { "true", true},
    27     { "EFalse", false},
    28     { "ETrue", true},
    29 	{ 0,0 }
    30 };
    31 
    32 
    33 // Enum values for TCertificateFormat
    34 static const EnumEntry enumDetailsForTCertificateFormat[] =
    35 {
    36     { "EX509Certificate", EX509Certificate},
    37     { "EWTLSCertificate", EWTLSCertificate},
    38 	{ "EX968Certificate", EX968Certificate},
    39 	{ "EUnknownCertificate", EUnknownCertificate},
    40 	{ "EX509CertificateUrl", EX509CertificateUrl},
    41 	{ "EWTLSCertificateUrl", EWTLSCertificateUrl},
    42 	{ "EX968CertificateUrl", EX968CertificateUrl},
    43 	{ 0,0 }
    44 };
    45 
    46 
    47 static const EnumEntry enumDetailsForTCertificateOwnerType[] =
    48 {
    49 	{ "ECACertificate", ECACertificate},
    50 	{ "EUserCertificate", EUserCertificate},
    51 	{ "EPeerCertificate", EPeerCertificate},
    52 	{ 0,0 }
    53 };
    54 
    55 
    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
    61 	  iLabel("Label"),
    62 	  iReadCertificateId("CertId(read)", true),
    63 	  iWriteCertificateId("CertId(write)", false),
    64 	  iCertificateOwnerType("CertOwnerType", enumDetailsForTCertificateOwnerType),
    65 	  iSubjectKeyId("SubjectKeyId"), iIssuerKeyId("IssuerKeyId"),
    66 	  iSwiMode(aSwiMode)
    67 	
    68 {
    69 	// We only need to initialise EncDecObject members which wrap non-class types
    70 	iTmpCombinedDeletableAndFormat.Value() = 0;
    71 	iSize.Value() = 0;
    72 	iReadCertificateId.Value() = 0;
    73 	iWriteCertificateId.Value() = 0;
    74 	iCertificateOwnerType.Value() = 0;
    75 }
    76 
    77 
    78 void CertInfo::Encode(REncodeWriteStream &aWriteStream)
    79 {
    80 	if(aWriteStream.HumanReadable())
    81 		{
    82 		aWriteStream << iDeletable;
    83 		aWriteStream << iFormat;
    84 		}
    85 	else
    86 		{
    87 		// Write the binary field containing both format and deletable
    88 		// flag.
    89 		//
    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;
    95 		}
    96 	
    97 
    98 	aWriteStream << iSize;
    99 	if(aWriteStream.HumanReadable())
   100 		{
   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;
   104 		}
   105 	else
   106 		{
   107 		aWriteStream << iLabel;
   108 		aWriteStream << iWriteCertificateId;
   109 		}
   110 	
   111 	
   112 	aWriteStream << iCertificateOwnerType;
   113 	aWriteStream <<  iSubjectKeyId;
   114 	aWriteStream <<  iIssuerKeyId;
   115 }
   116 
   117 
   118 void CertInfo::Decode(RDecodeReadStream &aReadStream)
   119 {
   120 	if(aReadStream.HumanReadable())
   121 		{
   122 		// Read the Deletable and Format fields
   123 		aReadStream >> iDeletable;
   124 		aReadStream >> iFormat;
   125 		}
   126 	else
   127 		{
   128 		// Read the binary field containing both format and deletable
   129 		// flag.  
   130 		//
   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;
   135 
   136 		iDeletable.SetValue((iTmpCombinedDeletableAndFormat.Value() & KReadOnlyFlagMask) == 0);
   137 		iFormat.SetValue((iTmpCombinedDeletableAndFormat.Value() & ~KReadOnlyFlagMask));
   138 		}
   139 	
   140 	aReadStream >> iSize;
   141 	if(!aReadStream.HumanReadable())
   142 		{
   143 		aReadStream >> iLabel;
   144 		}
   145 	aReadStream >> iReadCertificateId;
   146 	aReadStream >> iCertificateOwnerType;
   147 
   148 
   149 	if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iSubjectKeyId.Name()))
   150 		{
   151 		// Either in binary mode, or the next token is SubjectKeyId, so read the field
   152 		aReadStream >> iSubjectKeyId;
   153 		}
   154 	else
   155 		{
   156 		// In human mode and field not present, so set it to auto
   157 		iSubjectKeyId.Value().iAutoKey = true;
   158 		iSubjectKeyId.Value().iHash.SetLength(0);
   159 		}
   160 
   161 	if(!aReadStream.HumanReadable() || (aReadStream.PeakToken() == iIssuerKeyId.Name()))
   162 		{
   163 		// Either in binary mode, or the next token is IssuerKeyId, so read the field
   164 		aReadStream >> iIssuerKeyId;
   165 		}
   166 	else
   167 		{
   168 		// In human mode and field not present, so set it to auto
   169 		iIssuerKeyId.Value().iAutoKey = true;
   170 		iIssuerKeyId.Value().iHash.SetLength(0);
   171 		}
   172 }
   173 
   174 
   175 TUint32 CertInfo::CertSize() const
   176 {
   177 	return iSize.Value();
   178 }
   179 
   180 void CertInfo::SetCertSize(TUint32 aSize)
   181 {
   182 	iSize.Value() = aSize;
   183 }
   184 
   185 const TCertLabel &CertInfo::Label() const
   186 {
   187 	return iLabel.Value();
   188 }
   189 
   190 TCertLabel &CertInfo::Label()
   191 {
   192 	return iLabel.Value();
   193 }
   194 
   195 TCertificateFormat CertInfo::CertificateFormat() const
   196 {
   197 	return (TCertificateFormat)iFormat.Value();
   198 }
   199 
   200 TCertificateOwnerType CertInfo::CertificateOwnerType() const
   201 {
   202 	return (TCertificateOwnerType)iCertificateOwnerType.Value();
   203 }
   204 
   205 
   206 KeyIdentifierObject &CertInfo::SubjectKeyId()
   207 {
   208 	return iSubjectKeyId.Value();
   209 }
   210 
   211 const KeyIdentifierObject &CertInfo::SubjectKeyId() const
   212 {
   213 	return iSubjectKeyId.Value();
   214 }
   215 
   216 KeyIdentifierObject &CertInfo::IssuerKeyId()
   217 {
   218 	return iIssuerKeyId.Value();
   219 }
   220 
   221 #ifdef _BullseyeCoverage
   222 #pragma BullseyeCoverage off
   223 #endif
   224 const KeyIdentifierObject &CertInfo::IssuerKeyId() const
   225 {
   226 	return iIssuerKeyId.Value();
   227 }
   228 #ifdef _BullseyeCoverage
   229 #pragma BullseyeCoverage restore
   230 #endif
   231 
   232 TUint32 CertInfo::OutputCertificateId() const
   233 {
   234 	return iWriteCertificateId.Value();
   235 }
   236 
   237 
   238 void CertInfo::SetOutputCertificateId(TUint32 aId)
   239 {
   240 	iWriteCertificateId.Value() = aId;
   241 }
   242 
   243 
   244 
   245 
   246 //
   247 // TCertLabel
   248 //
   249 void EncodeHuman(REncodeWriteStream& aStream,const TCertLabel &aLabel)
   250 {
   251 	// Compress the internal UTF-16 to human readable UTF-8
   252 	TInt outputBytes = 0;
   253 	TUint8 *outBuf = cstrFromUtf16(aLabel.Ptr(), aLabel.Length(), outputBytes);
   254 	
   255 	aStream.WriteByte('"');
   256 	aStream.WriteQuotedUtf8(outBuf, outputBytes);
   257 	aStream.WriteByte('"');
   258 
   259 	delete [] outBuf;
   260 }
   261 void DecodeHuman(RDecodeReadStream& aStream,TCertLabel &aLabel)
   262 {
   263 	aStream.ReadNextToken();
   264 
   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())
   269 		{
   270 		dbg << Log::Indent() << "String too long" << Log::Endl();
   271 		FatalError();
   272 		}
   273 	
   274 	memcpy((void *)aLabel.Ptr(), outputBuf, outputWords*2);
   275 	aLabel.SetLength(outputWords);
   276 	delete [] outputBuf;
   277 }
   278 
   279 
   280 
   281 
   282 // End of file