|
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 |
|
290 // nb. If processing a swicertstore we ignore any SubjectKeyId in the extension. |
|
291 if(X509SubjectKeyId((iSwiMode)?(KIgnoreCertificateExtension) : (KUseCertificateExtension), |
|
292 false, |
|
293 iCertData, |
|
294 iCertSubject, subjectKeyId)) |
|
295 { |
|
296 prog << Log::Indent() << "Subject = '" << iCertSubject << "'" << Log::Endl(); |
|
297 |
|
298 prog << Log::Indent() << "Calculated SubjectKeyId is "; |
|
299 const TUint8 *p = subjectKeyId.Ptr(); |
|
300 for(int i=0; i<subjectKeyId.Length(); ++i) |
|
301 { |
|
302 if(i) prog << ":"; |
|
303 prog.Stream() << std::setfill('0') << std::setw(2) << int(p[i]); |
|
304 } |
|
305 prog.Stream() << std::setw(0); |
|
306 prog << Log::Endl(); |
|
307 |
|
308 if(aReadStream.HumanReadable() && iCertInfo.SubjectKeyId().iAutoKey) |
|
309 { |
|
310 // Reading config file and auto set so copy generated |
|
311 // SubjectKeyId to value. |
|
312 prog << Log::Indent() << "Field set to auto so using calculated SubjectKeyId" << Log::Endl();; |
|
313 iCertInfo.SubjectKeyId().iHash = subjectKeyId; |
|
314 } |
|
315 else |
|
316 { |
|
317 // If the read value matches the calculated value then |
|
318 // set iAutoKey so we dump it as auto (with the value |
|
319 // as a comment). |
|
320 if(iCertInfo.SubjectKeyId().iHash == subjectKeyId) |
|
321 { |
|
322 prog << Log::Indent() << "Calculated SubjectKeyId matches value read from input so setting to auto" << Log::Endl();; |
|
323 iCertInfo.SubjectKeyId().iAutoKey = true; |
|
324 } |
|
325 else |
|
326 { |
|
327 prog << Log::Indent() << "Calculated SubjectKeyId does NOT match value read from input so setting to value read" << Log::Endl();; |
|
328 } |
|
329 |
|
330 } |
|
331 } |
|
332 } |
|
333 } |
|
334 |
|
335 CertStoreEntry& CertStoreEntry::operator= (const CertStoreEntry& aRhs) |
|
336 { |
|
337 if(this == &aRhs) return *this; // handle self assignment |
|
338 |
|
339 EncDecContainerItem::operator=(*static_cast<const EncDecContainerItem *>(&aRhs)); |
|
340 |
|
341 iCertInfo = aRhs.iCertInfo; |
|
342 |
|
343 iCertApps.reset(); |
|
344 for(TUint32 i=0; i<aRhs.iCertApps.size(); ++i) |
|
345 { |
|
346 AppUidListEntry *newApp = new AppUidListEntry(AppUidMap::EnumEntries()); |
|
347 const AppUidListEntry *oldApp = static_cast<const AppUidListEntry *>(&aRhs.iCertApps[i]); |
|
348 *newApp = *oldApp; |
|
349 iCertApps.push_back(newApp); |
|
350 } |
|
351 |
|
352 iTrusted = aRhs.iTrusted; |
|
353 iReadDataStreamId = aRhs.iReadDataStreamId; |
|
354 iWriteDataStreamId = aRhs.iWriteDataStreamId; |
|
355 iDataFileName = aRhs.iDataFileName; |
|
356 iCertData = aRhs.iCertData; |
|
357 |
|
358 iCertSubject = aRhs.iCertSubject; |
|
359 |
|
360 iSwiMode = aRhs.iSwiMode; |
|
361 |
|
362 return *this; |
|
363 } |
|
364 |
|
365 const TUint8 * CertStoreEntry::CertData() const |
|
366 { |
|
367 return (const TUint8 *)iCertData.data(); |
|
368 } |
|
369 |
|
370 |
|
371 const std::string &CertStoreEntry::CertSubject() const |
|
372 { |
|
373 return iCertSubject; |
|
374 } |
|
375 |
|
376 |
|
377 |
|
378 // End of file |