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