os/security/securityanddataprivacytools/securitytools/certapp/encdec/filecertstore.cpp
Update contrib.
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.
19 #include "filecertstore.h"
20 #include "appuidmap.h"
22 #include "stringconv.h"
26 EncDecContainerItem *AppUidListEntry::Factory()
28 return new AppUidListEntry(AppUidMap::EnumEntries());
31 AppUidListEntry::AppUidListEntry(const EnumEntry *aEnumEntries)
32 : EncDecContainerItem(), iUid("Application", aEnumEntries)
36 AppUidListEntry::~AppUidListEntry()
40 const char *AppUidListEntry::ItemType() const
46 void AppUidListEntry::Encode(REncodeWriteStream &aWriteStream)
51 void AppUidListEntry::Decode(RDecodeReadStream &aReadStream)
57 EncDecContainerItem *CertStoreEntry::Factory()
59 return new CertStoreEntry;
63 static const EnumEntry enumDetailsForTBool[] =
73 CertStoreEntry::CertStoreEntry()
74 : EncDecContainerItem(),
76 iCertApps("ApplicationList", AppUidListEntry::Factory),
77 iTrusted("Trusted", enumDetailsForTBool),
78 iReadDataStreamId("DataStreamId(read)", true),
79 iWriteDataStreamId("DataStreamId(write)", false),
80 iDataFileName("DataFileName"),
84 // We only need to initialise EncDecObject members which wrap non-class types
85 iReadDataStreamId.Value() = 0;
86 iWriteDataStreamId.Value() = 0;
89 CertStoreEntry::CertStoreEntry(bool aSwiMode)
90 : EncDecContainerItem(),
92 iCertApps("ApplicationList", AppUidListEntry::Factory),
93 iTrusted("Trusted", enumDetailsForTBool),
94 iReadDataStreamId("DataStreamId(read)", true),
95 iWriteDataStreamId("DataStreamId(write)", false),
96 iDataFileName("DataFileName"),
100 // We only need to initialise EncDecObject members which wrap non-class types
101 iReadDataStreamId.Value() = 0;
102 iWriteDataStreamId.Value() = 0;
105 CertStoreEntry::~CertStoreEntry()
109 const TCertLabel &CertStoreEntry::Label() const
111 return iCertInfo.Label();
114 CertInfo &CertStoreEntry::Info()
119 const CertInfo &CertStoreEntry::Info() const
126 const char *CertStoreEntry::ItemType() const
131 std::string CertStoreEntry::ItemName() const
133 return stringFromUtf16(Label());
137 void CertStoreEntry::SetItemName(const std::string &aName)
140 TText *outputBuf = utf16FromUtf8((const TUint8 *)aName.data(), aName.size(), outputWords);
141 iCertInfo.Label() = TPtrC16(outputBuf, outputWords);
146 void CertStoreEntry::Encode(REncodeWriteStream &aWriteStream)
148 iCertInfo.Encode(aWriteStream);
149 aWriteStream << iCertApps;
150 aWriteStream << iTrusted;
151 if(aWriteStream.HumanReadable())
153 // Write data to a file
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()));
160 aWriteStream << iDataFileName;
162 std::fstream certDataFile;
163 OpenUtf8FStreamForWrite(certDataFile, certFileName.c_str());
164 if(certDataFile.fail())
166 dbg << Log::Indent() << "Failed to open '" << certDataFile << "' for output!" << Log::Endl();
169 if((iCertInfo.CertificateFormat() == EX509Certificate) && aWriteStream.PemOut())
172 Der2Pem(iCertData, pemCert);
173 certDataFile.write(pemCert.data(), pemCert.size());
177 certDataFile.write(iCertData.data(), iCertData.size());
180 certDataFile.close();
181 if(certDataFile.fail())
183 dbg << Log::Indent() << "Failed to write cert data to '" << certDataFile << Log::Endl();
186 aWriteStream << iReadDataStreamId;
190 // Write to the store
191 if(iCertData.size() != iCertInfo.CertSize())
193 dbg << Log::Indent() << "Internal error - cert data size does not match meta data" << Log::Endl();
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;
202 prog << Log::Indent() << "Writing " << iCertData.size() << " bytes of binary data" << Log::Endl();
203 dataStream.WriteL((const TUint8 *)iCertData.data(), iCertData.size());
205 CleanupStack::PopAndDestroy(&dataStream);
206 aWriteStream << iWriteDataStreamId;
210 void CertStoreEntry::Decode(RDecodeReadStream &aReadStream)
212 iCertInfo.Decode(aReadStream);
213 aReadStream >> iCertApps;
214 if((!aReadStream.HumanReadable()) ||
215 (aReadStream.PeakToken() == iTrusted.Name()))
217 aReadStream >> iTrusted;
221 iTrusted.SetValue(true);
223 aReadStream >> iReadDataStreamId;
224 if(aReadStream.HumanReadable())
226 aReadStream >> iDataFileName;
227 // Read data from the specified file
228 std::string nFileName = stringFromUtf16(iDataFileName.Value());
230 std::fstream certDataFile;
231 OpenUtf8FStreamForRead(certDataFile, nFileName.c_str());
232 if(certDataFile.fail())
234 dbg << Log::Indent() << "Failed to open '" << nFileName << "' for input!" << Log::Endl();
238 certDataFile.seekg(0, std::ios_base::end);
239 TUint32 certSize = certDataFile.tellg();
241 char *rawCertData = new char[certSize];
243 certDataFile.seekg(0, std::ios_base::beg);
244 certDataFile.read(rawCertData, certSize);
246 certDataFile.close();
247 if(certDataFile.fail())
249 dbg << Log::Indent() << "Failed to read cert data from '" << certDataFile << Log::Endl();
252 iCertData.assign(rawCertData, certSize);
253 delete [] rawCertData;
255 if(iCertInfo.CertificateFormat() == EX509Certificate)
257 // It might be a PEM cert
258 std::string derFromPem;
259 if(Pem2Der(iCertData, derFromPem))
261 prog << Log::Indent() << "Converted PEM cert to DER" << Log::Endl();
262 iCertData = derFromPem;
263 certSize = iCertData.size();
266 iCertInfo.SetCertSize(certSize);
270 // Read data from the store
271 RStoreReadStream dataStream;
272 dataStream.OpenLC(*aReadStream.iStore, iReadDataStreamId.Value());
274 TUint32 certSize = iCertInfo.CertSize();
275 TUint8 * certData = new TUint8[certSize];
277 prog << Log::Indent() << "Reading " << certSize << " byte certificate from store stream " << iReadDataStreamId.Value() << Log::Endl();
279 dataStream.ReadL(certData, certSize);
281 iCertData.assign((const char *)certData, certSize);
283 CleanupStack::PopAndDestroy(&dataStream);
286 if(iCertInfo.CertificateFormat() == EX509Certificate)
288 TKeyIdentifier subjectKeyId;
289 bool isCA = ( iCertInfo.CertificateOwnerType() != EUserCertificate );
291 // nb. If processing a swicertstore we ignore any SubjectKeyId in the extension.
292 if(X509SubjectKeyId((iSwiMode)?(KIgnoreCertificateExtension) : (KUseCertificateExtension),
295 iCertSubject, subjectKeyId))
297 prog << Log::Indent() << "Subject = '" << iCertSubject << "'" << Log::Endl();
299 prog << Log::Indent() << "Calculated SubjectKeyId is ";
300 const TUint8 *p = subjectKeyId.Ptr();
301 for(int i=0; i<subjectKeyId.Length(); ++i)
304 prog.Stream() << std::setfill('0') << std::setw(2) << int(p[i]);
306 prog.Stream() << std::setw(0);
309 if(aReadStream.HumanReadable() && iCertInfo.SubjectKeyId().iAutoKey)
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;
318 // If the read value matches the calculated value then
319 // set iAutoKey so we dump it as auto (with the value
321 if(iCertInfo.SubjectKeyId().iHash == subjectKeyId)
323 prog << Log::Indent() << "Calculated SubjectKeyId matches value read from input so setting to auto" << Log::Endl();;
324 iCertInfo.SubjectKeyId().iAutoKey = true;
328 prog << Log::Indent() << "Calculated SubjectKeyId does NOT match value read from input so setting to value read" << Log::Endl();;
336 CertStoreEntry& CertStoreEntry::operator= (const CertStoreEntry& aRhs)
338 if(this == &aRhs) return *this; // handle self assignment
340 EncDecContainerItem::operator=(*static_cast<const EncDecContainerItem *>(&aRhs));
342 iCertInfo = aRhs.iCertInfo;
345 for(TUint32 i=0; i<aRhs.iCertApps.size(); ++i)
347 AppUidListEntry *newApp = new AppUidListEntry(AppUidMap::EnumEntries());
348 const AppUidListEntry *oldApp = static_cast<const AppUidListEntry *>(&aRhs.iCertApps[i]);
350 iCertApps.push_back(newApp);
353 iTrusted = aRhs.iTrusted;
354 iReadDataStreamId = aRhs.iReadDataStreamId;
355 iWriteDataStreamId = aRhs.iWriteDataStreamId;
356 iDataFileName = aRhs.iDataFileName;
357 iCertData = aRhs.iCertData;
359 iCertSubject = aRhs.iCertSubject;
361 iSwiMode = aRhs.iSwiMode;
366 const TUint8 * CertStoreEntry::CertData() const
368 return (const TUint8 *)iCertData.data();
372 const std::string &CertStoreEntry::CertSubject() const