securityanddataprivacytools/securitytools/certapp/encdec/certinfo.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     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 
       
   201 KeyIdentifierObject &CertInfo::SubjectKeyId()
       
   202 {
       
   203 	return iSubjectKeyId.Value();
       
   204 }
       
   205 
       
   206 const KeyIdentifierObject &CertInfo::SubjectKeyId() const
       
   207 {
       
   208 	return iSubjectKeyId.Value();
       
   209 }
       
   210 
       
   211 KeyIdentifierObject &CertInfo::IssuerKeyId()
       
   212 {
       
   213 	return iIssuerKeyId.Value();
       
   214 }
       
   215 
       
   216 #ifdef _BullseyeCoverage
       
   217 #pragma BullseyeCoverage off
       
   218 #endif
       
   219 const KeyIdentifierObject &CertInfo::IssuerKeyId() const
       
   220 {
       
   221 	return iIssuerKeyId.Value();
       
   222 }
       
   223 #ifdef _BullseyeCoverage
       
   224 #pragma BullseyeCoverage restore
       
   225 #endif
       
   226 
       
   227 TUint32 CertInfo::OutputCertificateId() const
       
   228 {
       
   229 	return iWriteCertificateId.Value();
       
   230 }
       
   231 
       
   232 
       
   233 void CertInfo::SetOutputCertificateId(TUint32 aId)
       
   234 {
       
   235 	iWriteCertificateId.Value() = aId;
       
   236 }
       
   237 
       
   238 
       
   239 
       
   240 
       
   241 //
       
   242 // TCertLabel
       
   243 //
       
   244 void EncodeHuman(REncodeWriteStream& aStream,const TCertLabel &aLabel)
       
   245 {
       
   246 	// Compress the internal UTF-16 to human readable UTF-8
       
   247 	TInt outputBytes = 0;
       
   248 	TUint8 *outBuf = cstrFromUtf16(aLabel.Ptr(), aLabel.Length(), outputBytes);
       
   249 	
       
   250 	aStream.WriteByte('"');
       
   251 	aStream.WriteQuotedUtf8(outBuf, outputBytes);
       
   252 	aStream.WriteByte('"');
       
   253 
       
   254 	delete [] outBuf;
       
   255 }
       
   256 void DecodeHuman(RDecodeReadStream& aStream,TCertLabel &aLabel)
       
   257 {
       
   258 	aStream.ReadNextToken();
       
   259 
       
   260 	// Expand UTF-8 into internal UTF-16LE representation
       
   261 	TInt outputWords = 0;
       
   262 	TText *outputBuf = utf16FromUtf8((const TUint8 *)aStream.Token().data(), aStream.Token().size(), outputWords);
       
   263 	if(outputWords > aLabel.MaxLength())
       
   264 		{
       
   265 		dbg << Log::Indent() << "String too long" << Log::Endl();
       
   266 		FatalError();
       
   267 		}
       
   268 	
       
   269 	memcpy((void *)aLabel.Ptr(), outputBuf, outputWords*2);
       
   270 	aLabel.SetLength(outputWords);
       
   271 	delete [] outputBuf;
       
   272 }
       
   273 
       
   274 
       
   275 
       
   276 
       
   277 // End of file