os/security/securityanddataprivacytools/securitytools/certapp/encdec/filecertstore.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 "filecertstore.h"
    20 #include "appuidmap.h"
    21 #include "logger.h"
    22 #include "stringconv.h"
    23 #include "utils.h"
    24 #include <iomanip>
    25 
    26 EncDecContainerItem *AppUidListEntry::Factory()
    27 {
    28 	return new AppUidListEntry(AppUidMap::EnumEntries());
    29 }
    30 
    31 AppUidListEntry::AppUidListEntry(const EnumEntry *aEnumEntries)
    32 	: EncDecContainerItem(), iUid("Application", aEnumEntries)
    33 {
    34 }
    35 
    36 AppUidListEntry::~AppUidListEntry()
    37 {
    38 }
    39 
    40 const char *AppUidListEntry::ItemType() const
    41 {
    42 	return 0; // n/a
    43 }
    44 
    45 
    46 void AppUidListEntry::Encode(REncodeWriteStream &aWriteStream)
    47 {
    48 	aWriteStream << iUid;
    49 }
    50 
    51 void AppUidListEntry::Decode(RDecodeReadStream &aReadStream)
    52 {
    53 	aReadStream >> iUid;
    54 }
    55 
    56 
    57 EncDecContainerItem *CertStoreEntry::Factory()
    58 {
    59 	return new CertStoreEntry;
    60 }
    61 
    62 
    63 static const EnumEntry enumDetailsForTBool[] =
    64 {
    65     { "false", 0x00},
    66     { "true", 0x01},
    67     { "EFalse", false},
    68     { "ETrue", true},
    69 	{ 0,0 }
    70 };
    71 
    72 
    73 CertStoreEntry::CertStoreEntry()
    74 	: EncDecContainerItem(), 
    75 	  iCertInfo(false),
    76 	  iCertApps("ApplicationList", AppUidListEntry::Factory),
    77 	  iTrusted("Trusted", enumDetailsForTBool), 
    78 	  iReadDataStreamId("DataStreamId(read)", true),
    79 	  iWriteDataStreamId("DataStreamId(write)", false),
    80 	  iDataFileName("DataFileName"),
    81 	  iCertData(),
    82 	  iSwiMode(false)
    83 {
    84 	// We only need to initialise EncDecObject members which wrap non-class types
    85 	iReadDataStreamId.Value() = 0;
    86 	iWriteDataStreamId.Value() = 0;
    87 }
    88 
    89 CertStoreEntry::CertStoreEntry(bool aSwiMode)
    90 	: EncDecContainerItem(), 
    91 	  iCertInfo(aSwiMode),
    92 	  iCertApps("ApplicationList", AppUidListEntry::Factory),
    93 	  iTrusted("Trusted", enumDetailsForTBool), 
    94 	  iReadDataStreamId("DataStreamId(read)", true),
    95 	  iWriteDataStreamId("DataStreamId(write)", false),
    96 	  iDataFileName("DataFileName"),
    97 	  iCertData(),
    98 	  iSwiMode(aSwiMode)
    99 {
   100 	// We only need to initialise EncDecObject members which wrap non-class types
   101 	iReadDataStreamId.Value() = 0;
   102 	iWriteDataStreamId.Value() = 0;
   103 }
   104 
   105 CertStoreEntry::~CertStoreEntry()
   106 {
   107 }
   108 
   109 const TCertLabel &CertStoreEntry::Label() const
   110 {
   111 	return iCertInfo.Label();
   112 }
   113 
   114 CertInfo &CertStoreEntry::Info()
   115 {
   116 	return iCertInfo;
   117 }
   118 
   119 const CertInfo &CertStoreEntry::Info() const
   120 {
   121 	return iCertInfo;
   122 }
   123 
   124 
   125 
   126 const char *CertStoreEntry::ItemType() const
   127 {
   128 	return "Entry";
   129 }
   130 
   131 std::string CertStoreEntry::ItemName() const
   132 {
   133 	return stringFromUtf16(Label());
   134 }
   135 
   136 
   137 void CertStoreEntry::SetItemName(const std::string &aName)
   138 {
   139 	TInt outputWords;
   140 	TText *outputBuf = utf16FromUtf8((const TUint8 *)aName.data(), aName.size(), outputWords);
   141 	iCertInfo.Label() = TPtrC16(outputBuf, outputWords);
   142 	delete [] outputBuf;
   143 }
   144 
   145 
   146 void CertStoreEntry::Encode(REncodeWriteStream &aWriteStream)
   147 {
   148 	iCertInfo.Encode(aWriteStream);
   149 	aWriteStream << iCertApps;
   150 	aWriteStream << iTrusted;
   151 	if(aWriteStream.HumanReadable())
   152 		{
   153 		// Write data to a file
   154 
   155 		// Generate a file name
   156 		std::string certFileName = aWriteStream.CertFileName(iCertInfo.CertificateFormat(), iCertInfo.OutputCertificateId());
   157 		iDataFileName.Value().Copy(TPtrC8((const TUint8*)certFileName.data(), certFileName.size()));
   158 		
   159 		// Write file name
   160 		aWriteStream << iDataFileName;
   161 		
   162 		std::fstream certDataFile;
   163 		OpenUtf8FStreamForWrite(certDataFile, certFileName.c_str());
   164 		if(certDataFile.fail())
   165 			{
   166 			dbg << Log::Indent() << "Failed to open '" << certDataFile << "' for output!" << Log::Endl();
   167 			FatalError();
   168 			}
   169 		if((iCertInfo.CertificateFormat() == EX509Certificate) && aWriteStream.PemOut())
   170 			{
   171 			std::string pemCert;
   172 			Der2Pem(iCertData, pemCert);
   173 			certDataFile.write(pemCert.data(), pemCert.size());
   174 			}
   175 		else
   176 			{
   177 			certDataFile.write(iCertData.data(), iCertData.size());
   178 			}
   179 		
   180 		certDataFile.close();
   181 		if(certDataFile.fail())
   182 			{
   183 			dbg << Log::Indent() << "Failed to write cert data to '" << certDataFile <<  Log::Endl();
   184 			FatalError();
   185 			}
   186 		aWriteStream << iReadDataStreamId;
   187 		}
   188 	else
   189 		{
   190 		// Write to the store
   191 		if(iCertData.size() != iCertInfo.CertSize())
   192 			{
   193 			dbg << Log::Indent() << "Internal error - cert data size does not match meta data" << Log::Endl();
   194 			FatalError();
   195 			}
   196 
   197 		RStoreWriteStream dataStream;
   198 		TStreamId dataStreamId = dataStream.CreateLC(*aWriteStream.StoreObject());
   199 		prog << Log::Indent() << "Created store stream " << dataStreamId << " for certificate data" << Log::Endl();
   200 		iWriteDataStreamId.Value() = dataStreamId;
   201 
   202 		prog << Log::Indent() << "Writing " << iCertData.size() << " bytes of binary data" << Log::Endl();
   203 		dataStream.WriteL((const TUint8 *)iCertData.data(), iCertData.size());
   204 
   205 		CleanupStack::PopAndDestroy(&dataStream);
   206 		aWriteStream << iWriteDataStreamId;
   207 		}
   208 }
   209 
   210 void CertStoreEntry::Decode(RDecodeReadStream &aReadStream)
   211 {
   212 	iCertInfo.Decode(aReadStream);
   213 	aReadStream >> iCertApps;
   214 	if((!aReadStream.HumanReadable()) ||
   215 	   (aReadStream.PeakToken() == iTrusted.Name()))
   216 		{
   217 		aReadStream >> iTrusted;
   218 		}
   219 	else
   220 		{
   221 		iTrusted.SetValue(true);
   222 		}
   223 	aReadStream >> iReadDataStreamId;
   224 	if(aReadStream.HumanReadable())
   225 		{
   226 		aReadStream >> iDataFileName;
   227 		// Read data from the specified file
   228 		std::string nFileName = stringFromUtf16(iDataFileName.Value());
   229 		
   230 		std::fstream certDataFile;
   231 		OpenUtf8FStreamForRead(certDataFile, nFileName.c_str());
   232 		if(certDataFile.fail())
   233 			{
   234 			dbg << Log::Indent() << "Failed to open '" << nFileName << "' for input!" << Log::Endl();
   235 			FatalError();
   236 			}
   237 		
   238 		certDataFile.seekg(0, std::ios_base::end);
   239 		TUint32 certSize = certDataFile.tellg();
   240 		
   241 		char *rawCertData = new char[certSize];
   242 		
   243 		certDataFile.seekg(0, std::ios_base::beg);
   244 		certDataFile.read(rawCertData, certSize);
   245 		
   246 		certDataFile.close();
   247 		if(certDataFile.fail())
   248 			{
   249 			dbg << Log::Indent() << "Failed to read cert data from '" << certDataFile << Log::Endl();
   250 			FatalError();
   251 			}
   252 		iCertData.assign(rawCertData, certSize);
   253 		delete [] rawCertData;
   254 		
   255 		if(iCertInfo.CertificateFormat() == EX509Certificate)
   256 			{
   257 			// It might be a PEM cert
   258 			std::string derFromPem;
   259 			if(Pem2Der(iCertData, derFromPem))
   260 				{
   261 				prog << Log::Indent() << "Converted PEM cert to DER" << Log::Endl();
   262 				iCertData = derFromPem;
   263 				certSize = iCertData.size();
   264 				}
   265 			}
   266 		iCertInfo.SetCertSize(certSize);
   267 		}
   268 	else 
   269 		{
   270 		// Read data from the store
   271 		RStoreReadStream dataStream;
   272 		dataStream.OpenLC(*aReadStream.iStore, iReadDataStreamId.Value());
   273 
   274 		TUint32 certSize = iCertInfo.CertSize();
   275 		TUint8 * certData = new TUint8[certSize];
   276 
   277 		prog << Log::Indent() << "Reading " << certSize << " byte certificate from store stream " << iReadDataStreamId.Value() << Log::Endl();
   278 		
   279 		dataStream.ReadL(certData, certSize);
   280 
   281 		iCertData.assign((const char *)certData, certSize);
   282 		
   283 		CleanupStack::PopAndDestroy(&dataStream);
   284 		}
   285 
   286 	if(iCertInfo.CertificateFormat() == EX509Certificate)
   287 		{
   288 		TKeyIdentifier subjectKeyId;
   289 		bool isCA = ( iCertInfo.CertificateOwnerType() != EUserCertificate );
   290 
   291 		// nb. If processing a swicertstore we ignore any SubjectKeyId in the extension.
   292 		if(X509SubjectKeyId((iSwiMode)?(KIgnoreCertificateExtension) : (KUseCertificateExtension), 
   293 							false, isCA,
   294 							iCertData,
   295 							iCertSubject, subjectKeyId))
   296 			{
   297 			prog << Log::Indent() << "Subject = '" << iCertSubject << "'" << Log::Endl();
   298 
   299 			prog << Log::Indent() << "Calculated SubjectKeyId is ";
   300 			const TUint8 *p = subjectKeyId.Ptr();
   301 			for(int i=0; i<subjectKeyId.Length(); ++i)
   302 				{
   303 				if(i) prog << ":";
   304 				prog.Stream() << std::setfill('0') << std::setw(2) << int(p[i]);
   305 				}
   306 			prog.Stream() << std::setw(0);
   307 			prog << Log::Endl();
   308 			
   309 			if(aReadStream.HumanReadable() && iCertInfo.SubjectKeyId().iAutoKey)
   310 				{
   311 				// Reading config file and auto set so copy generated
   312 				// SubjectKeyId to value.
   313 				prog << Log::Indent() << "Field set to auto so using calculated SubjectKeyId" << Log::Endl();;
   314 				iCertInfo.SubjectKeyId().iHash = subjectKeyId;
   315 				}
   316 			else
   317 				{
   318 				// If the read value matches the calculated value then
   319 				// set iAutoKey so we dump it as auto (with the value
   320 				// as a comment).
   321 				if(iCertInfo.SubjectKeyId().iHash == subjectKeyId)
   322 					{
   323 					prog << Log::Indent() << "Calculated SubjectKeyId matches value read from input so setting to auto" << Log::Endl();;
   324 					iCertInfo.SubjectKeyId().iAutoKey = true;
   325 					}
   326 				else
   327 					{
   328 					prog << Log::Indent() << "Calculated SubjectKeyId does NOT match value read from input so setting to value read" << Log::Endl();;
   329 					}
   330 				
   331 				}
   332 			}
   333 		}
   334 }
   335 	
   336 CertStoreEntry& CertStoreEntry::operator= (const CertStoreEntry& aRhs)
   337 {
   338 	if(this == &aRhs) return *this; // handle self assignment
   339 
   340 	EncDecContainerItem::operator=(*static_cast<const EncDecContainerItem *>(&aRhs));
   341 
   342 	iCertInfo = aRhs.iCertInfo;
   343 
   344 	iCertApps.reset();
   345 	for(TUint32 i=0; i<aRhs.iCertApps.size(); ++i)
   346 		{
   347 		AppUidListEntry *newApp = new AppUidListEntry(AppUidMap::EnumEntries());
   348 		const AppUidListEntry *oldApp = static_cast<const AppUidListEntry *>(&aRhs.iCertApps[i]);
   349 		*newApp = *oldApp;
   350 		iCertApps.push_back(newApp);
   351 		}
   352 
   353 	iTrusted = aRhs.iTrusted;
   354 	iReadDataStreamId = aRhs.iReadDataStreamId;
   355 	iWriteDataStreamId = aRhs.iWriteDataStreamId;
   356 	iDataFileName = aRhs.iDataFileName;
   357 	iCertData = aRhs.iCertData;
   358 
   359 	iCertSubject = aRhs.iCertSubject;
   360 
   361 	iSwiMode = aRhs.iSwiMode;
   362 		
   363 	return *this;
   364 }
   365 
   366 const TUint8 * CertStoreEntry::CertData() const
   367 {
   368 	return (const TUint8 *)iCertData.data();
   369 }
   370 
   371 
   372 const std::string &CertStoreEntry::CertSubject() const
   373 {
   374 	return iCertSubject;
   375 }
   376 
   377 
   378 
   379 // End of file