Applied latest source code
[apps/native/preloaded/Settings.git] / src / StCertificatePresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                StCertificatePresentationModel.cpp
19  * @brief               This is the implementation file for Certificate Presentation Model class.
20  */
21
22 #include <cstdlib>
23 #include <FIo.h>
24 #include <FSystem.h>
25 #include "StCertificatePresentationModel.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Utility;
30 using namespace Tizen::Io;
31 using namespace Tizen::Security::Cert;
32 using namespace Tizen::System;
33
34 static const int PUBLIC_KEY_HEX_RIGHT_SHIFT_4 = 4;
35 static const int DEFAULT_COUNT = 1;
36
37 CertificatePresentationModel* CertificatePresentationModel::__pStCertificatePresentationModelInstance = null;
38
39 CertificatePresentationModel::CertificatePresentationModel(void)
40         : __pX509CertificateStore(null)
41         , __pICertificatePresentationModelSelector(null)
42         , __pPendingCertificate(null)
43         , __pCertificateList(null)
44         , __pFoundUserCertificateToInstallList(null)
45         , __pPendingUserCertificateToInstallList(null)
46 {
47 }
48
49 CertificatePresentationModel::~CertificatePresentationModel(void)
50 {
51         ClearPendingCertificate();
52         ClearPendingUserCertificateFileNameToInstall();
53         RemoveCertificateList();
54         RemoveCertificateSelector();
55         RemoveFoundUserCertifiacteToInstallList();
56
57         delete __pX509CertificateStore;
58         __pX509CertificateStore = null;
59 }
60
61 void
62 CertificatePresentationModel::CreateInstance(void)
63 {
64         __pStCertificatePresentationModelInstance = new (std::nothrow) CertificatePresentationModel();
65         result r = __pStCertificatePresentationModelInstance->Construct();
66         if (IsFailed(r))
67         {
68                 delete __pStCertificatePresentationModelInstance;
69                 __pStCertificatePresentationModelInstance = null;
70                 return;
71         }
72
73         std::atexit(DestroyInstance);
74 }
75
76 void
77 CertificatePresentationModel::DestroyInstance(void)
78 {
79         delete __pStCertificatePresentationModelInstance;
80         __pStCertificatePresentationModelInstance = null;
81 }
82
83 CertificatePresentationModel*
84 CertificatePresentationModel::GetInstance(void)
85 {
86         if (__pStCertificatePresentationModelInstance == null)
87         {
88                 CreateInstance();
89         }
90
91         return __pStCertificatePresentationModelInstance;
92 }
93
94 result
95 CertificatePresentationModel::Construct(void)
96 {
97         __pX509CertificateStore = new (std::nothrow) X509CertificateStore();
98
99         if (__pX509CertificateStore == null)
100         {
101                 AppLogDebug("__pX509CertificateStore is null");
102                 delete __pX509CertificateStore;
103                 __pX509CertificateStore = null;
104                 return E_FAILURE;
105         }
106
107         return E_SUCCESS;
108 }
109
110 result
111 CertificatePresentationModel::SetCertificateSelector(Tizen::Security::Cert::CertificateType certificateType)
112 {
113         if (__pICertificatePresentationModelSelector == null)
114         {
115                 __pICertificatePresentationModelSelector = new (std::nothrow) ICertificatePresentationModelSelector();
116                 if (__pICertificatePresentationModelSelector == null)
117                 {
118                         AppLogDebug("__pICertificatePresentationModelSelector is null");
119                         return E_FAILURE;
120                 }
121         }
122
123         __pICertificatePresentationModelSelector->SetType(certificateType);
124
125         result r = __pX509CertificateStore->SetCertificateSelector(*__pICertificatePresentationModelSelector);
126         if (IsFailed(r))
127         {
128                 AppLogDebug("SetCertificateSelector failed(%s)", GetErrorMessage(r));
129                 RemoveCertificateSelector();
130         }
131
132         return r;
133 }
134
135 void
136 CertificatePresentationModel::RemoveCertificateSelector(void)
137 {
138         delete __pICertificatePresentationModelSelector;
139         __pICertificatePresentationModelSelector = null;
140 }
141
142 result
143 CertificatePresentationModel::UpdateCertificateListFromCertificateStore(void)
144 {
145         RemoveCertificateList();
146         SetCertificateSelector(__pICertificatePresentationModelSelector->GetType());
147
148         __pCertificateList = new (std::nothrow) ArrayListT<Tizen::Security::Cert::ICertificate*>();
149         result r = __pCertificateList->Construct();
150         if (IsFailed(r))
151         {
152                 AppLogDebug("__pCertificateList->Construct failed(%s)", GetErrorMessage(r));
153                 delete __pCertificateList;
154                 __pCertificateList = null;
155
156                 return E_FAILURE;
157         }
158
159         int storedCertificateCount = 0;
160         r = __pX509CertificateStore->GetCertificateCount(storedCertificateCount);
161         if (IsFailed(r))
162         {
163                 AppLogDebug("GetCertificateCount failed(%s) or GetCertificateCount is zero", GetErrorMessage(r));
164                 RemoveCertificateList();
165                 delete __pCertificateList;
166                 __pCertificateList = null;
167         }
168
169         for (int i = 0; i < storedCertificateCount; i++)
170         {
171                 ICertificate* pCertificate = __pX509CertificateStore->GetNextCertificateN();
172                 if (pCertificate == null)
173                 {
174                         AppLogDebug("GetNextCertificateN is null");
175                         break;
176                 }
177
178                 __pCertificateList->Add(pCertificate);
179         }
180
181         if (__pCertificateList->GetCount() != storedCertificateCount)
182         {
183                 AppLogDebug("different between __pCertificateList->GetCount() and storedCertificateCount");
184                 RemoveCertificateList();
185                 return E_FAILURE;
186         }
187
188         return r;
189 }
190
191 void
192 CertificatePresentationModel::RemoveCertificateList(void)
193 {
194         if (__pCertificateList != null)
195         {
196                 __pCertificateList->RemoveAll();
197                 delete __pCertificateList;
198                 __pCertificateList = null;
199         }
200 }
201
202 Tizen::Security::Cert::CertificateType
203 CertificatePresentationModel::GetCertificateSelectorType(void) const
204 {
205         return __pICertificatePresentationModelSelector->GetType();
206 }
207
208 Tizen::Base::String
209 CertificatePresentationModel::GetSubjectNameFromCertifcateStore(int i)
210 {
211         ICertificate* pICertificate;
212         result r = __pCertificateList->GetAt(i, pICertificate);
213
214         if (IsFailed(r) || pICertificate == null)
215         {
216                 AppLogDebug("__pCertificateList->GetAt(%d) failed(%s) or pICertificate is null",i , GetErrorMessage(r));
217                 SetLastResult(E_FAILURE);
218                 return String(L"");
219         }
220
221         return pICertificate->GetSubject();
222 }
223
224 Tizen::Security::Cert::ICertificate*
225 CertificatePresentationModel::GetCertificateFromCertificateStoreN(int i)
226 {
227         ICertificate* pICertificate;
228         result r = __pCertificateList->GetAt(i, pICertificate);
229
230         if (IsFailed(r) || pICertificate == null)
231         {
232                 AppLogDebug("__pCertificateList->GetAt(%d) failed(%s) or pICertificate is null", i, GetErrorMessage(r));
233                 return null;
234         }
235
236         ByteBuffer* pBuffer = pICertificate->GetEncodedDataN();
237         X509Certificate* pX509Certificate = new (std::nothrow) X509Certificate();
238
239         r = pX509Certificate->Construct(*pBuffer);
240         delete pBuffer;
241
242         if (IsFailed(r))
243         {
244                 AppLogDebug("pCertificate->Construct failed(%s)", GetErrorMessage(r));
245                 delete pX509Certificate;
246                 return null;
247         }
248
249         return pX509Certificate;
250 }
251
252 result
253 CertificatePresentationModel::InitFoundUserCertificateToInstallList(void)
254 {
255         if (__pFoundUserCertificateToInstallList != null)
256         {
257                 AppLogDebug("__pFoundUserCertificateToInstallList is not null");
258                 return E_FAILURE;
259         }
260         __pFoundUserCertificateToInstallList = new (std::nothrow) ArrayList(SingleObjectDeleter);
261         return __pFoundUserCertificateToInstallList->Construct();
262 }
263
264 void
265 CertificatePresentationModel::RemoveFoundUserCertifiacteToInstallList(void)
266 {
267         if (__pFoundUserCertificateToInstallList != null)
268         {
269                 delete __pFoundUserCertificateToInstallList;
270                 __pFoundUserCertificateToInstallList = null;
271         }
272 }
273
274 result
275 CertificatePresentationModel::FindUserCertificateToInstallFromStorage(void)
276 {
277         if (__pICertificatePresentationModelSelector == null
278                 || __pICertificatePresentationModelSelector->GetType() != USER_CERT)
279         {
280                 AppLogDebug("selector is null or must be set USER_CERT");
281                 return E_FAILURE;
282         }
283
284         RemoveFoundUserCertifiacteToInstallList();
285         InitFoundUserCertificateToInstallList();
286         Directory dir;
287         DirEnumerator *pDirEnum = null;
288         String extrnalStoragePath = Environment::GetExternalStoragePath();
289         result r = dir.Construct(extrnalStoragePath);
290         if (IsFailed(r))
291         {
292                 AppLogDebug("dir.Construct failed(%s)", GetErrorMessage(r));
293                 return r;
294         }
295
296         pDirEnum = dir.ReadN();
297         if (pDirEnum == null)
298         {
299                 AppLogDebug("dir.ReadN is null");
300                 return E_FAILURE;
301         }
302         while (pDirEnum->MoveNext() == E_SUCCESS)
303         {
304                 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
305                 if (dirEntry.IsDirectory() == true)
306                 {
307                         continue;
308                 }
309                 String filePath = extrnalStoragePath + dirEntry.GetName();
310                 if (File::GetFileExtension(filePath).Equals(L"p12", false)
311                         || File::GetFileExtension(filePath).Equals(L"pfx", false))
312                 {
313                         __pFoundUserCertificateToInstallList->Add(new (std::nothrow) String(dirEntry.GetName()));
314                 }
315         }
316
317         delete pDirEnum;
318         return E_SUCCESS;
319 }
320
321 Tizen::Base::String
322 CertificatePresentationModel::GetFileNameInFoundUserCertificateToInstallListAt(int index)
323 {
324         if (__pFoundUserCertificateToInstallList == null)
325         {
326                 AppLogDebug("__pFoundUserCertificateToInstallList is null");
327                 SetLastResult(E_FAILURE);
328                 return L"";
329         }
330         String* certificateFileName = static_cast<String*>(__pFoundUserCertificateToInstallList->GetAt(index));
331         if (certificateFileName == null)
332         {
333                 AppLogDebug("__pFoundUserCertificateToInstallList->GetAt is null");
334                 SetLastResult(E_FAILURE);
335                 return String();
336         }
337
338         return String(*certificateFileName);
339 }
340
341 int
342 CertificatePresentationModel::GetFoundUserCertificateToInstallListCount(void)
343 {
344         if (__pFoundUserCertificateToInstallList == null)
345         {
346                 AppLogDebug("__pFoundUserCertificateToInstallList is null");
347                 SetLastResult(E_FAILURE);
348                 return 0;
349         }
350
351         return __pFoundUserCertificateToInstallList->GetCount();
352 }
353
354 int
355 CertificatePresentationModel::GetCertificateStoreCount(void)
356 {
357         if (__pCertificateList == null)
358         {
359                 return 0;
360         }
361
362         return  __pCertificateList->GetCount();
363 }
364
365 result
366 CertificatePresentationModel::RemoveFromCertificateStore(const Tizen::Security::Cert::ICertificate& certificate)
367 {
368         result r = __pX509CertificateStore->Remove(__pICertificatePresentationModelSelector->GetType(), certificate);
369         if (!IsFailed(r))
370         {
371                 UpdateCertificateListFromCertificateStore();
372         }
373
374         return r;
375 }
376
377 result
378 CertificatePresentationModel::InsertPkcs12FormatIntoCertificateStore(const Tizen::Base::String& filePath, const Tizen::Base::String& password)
379 {
380         result r = __pX509CertificateStore->InsertPkcs12(filePath, password);
381         if (!IsFailed(r))
382         {
383                 UpdateCertificateListFromCertificateStore();
384         }
385
386         return r;
387 }
388
389 bool
390 CertificatePresentationModel::PushPendingCertificate(Tizen::Security::Cert::ICertificate& certificate)
391 {
392         ByteBuffer* pBuffer = certificate.GetEncodedDataN();
393         X509Certificate* pCertificate = new (std::nothrow) X509Certificate();
394
395         result r = pCertificate->Construct(*pBuffer);
396         delete pBuffer;
397
398         if (IsFailed(r) || IsPendingCertificate() == true)
399         {
400                 AppLogDebug("pCertificate->Construct failed(%s)", GetErrorMessage(r));
401                 delete pCertificate;
402                 return false;
403         }
404
405         __pPendingCertificate = pCertificate;
406         return true;
407 }
408
409 Tizen::Security::Cert::ICertificate*
410 CertificatePresentationModel::PopPendingCertificateN(void)
411 {
412         if (IsPendingCertificate() == true)
413         {
414                 ICertificate* pICertificate = __pPendingCertificate;
415                 __pPendingCertificate = null;
416
417                 return pICertificate;
418         }
419
420         return null;
421 }
422
423 bool
424 CertificatePresentationModel::IsPendingCertificate(void)
425 {
426         if (__pPendingCertificate == null)
427         {
428                 return false;
429         }
430         return true;
431 }
432
433 void
434 CertificatePresentationModel::ClearPendingCertificate(void)
435 {
436         if (IsPendingCertificate() == true)
437         {
438                 delete __pPendingCertificate;
439                 __pPendingCertificate = null;
440         }
441 }
442
443 bool
444 CertificatePresentationModel::IsPendingUserCertificateFileNameToInstall(void)
445 {
446         if (__pPendingUserCertificateToInstallList == null)
447         {
448                 return false;
449         }
450         return true;
451 }
452
453 bool
454 CertificatePresentationModel::PushPendingUserCertificateFileNameToInstall(Tizen::Base::String& filePath)
455 {
456         if (IsPendingUserCertificateFileNameToInstall() == true)
457         {
458                 AppLogDebug("IsPendingUserCertificateToInstall is true");
459                 return false;
460         }
461         __pPendingUserCertificateToInstallList = new (std::nothrow) String(filePath);
462         return true;
463 }
464
465 Tizen::Base::String*
466 CertificatePresentationModel::PopPendingUserCertificateFileNameToInstallN(void)
467 {
468         if (IsPendingUserCertificateFileNameToInstall() == true)
469         {
470                 String* pUserCertificateToInstallList = __pPendingUserCertificateToInstallList;
471                 __pPendingUserCertificateToInstallList = null;
472                 return pUserCertificateToInstallList;
473         }
474         return null;
475 }
476
477 void
478 CertificatePresentationModel::ClearPendingUserCertificateFileNameToInstall(void)
479 {
480         if (IsPendingUserCertificateFileNameToInstall() == true)
481         {
482                 delete __pPendingUserCertificateToInstallList;
483                 __pPendingUserCertificateToInstallList = null;
484         }
485 }
486
487 ICertificatePresentationModelSelector::ICertificatePresentationModelSelector(void)
488         : __currentCertificateType(Tizen::Security::Cert::ROOT_CA)
489 {
490 }
491
492 Tizen::Security::Cert::CertificateType
493 ICertificatePresentationModelSelector::GetType(void) const
494 {
495         return __currentCertificateType;
496 }
497
498 void
499 ICertificatePresentationModelSelector::SetType(const Tizen::Security::Cert::CertificateType certificateType)
500 {
501         __currentCertificateType = certificateType;
502 }
503
504 Tizen::Base::String
505 CertificatePresentationModelUtility::GetCommonName(const Tizen::Base::String& source)
506 {
507         String isContan(L"CN=");
508         StringTokenizer strTokenizer(source, L"/");
509         while (strTokenizer.HasMoreTokens())
510         {
511                 String recipient;
512                 result r = strTokenizer.GetNextToken(recipient);
513                 if (IsFailed(r))
514                 {
515                         AppLogDebug("GetNextToken failed(%s)", GetErrorMessage(r));
516                         continue;
517                 }
518
519                 if (recipient.Contains(isContan))
520                 {
521                         recipient.Remove(0, isContan.GetLength());
522                         return recipient;
523                 }
524         }
525
526         SetLastResult(E_FAILURE);
527         return String();
528 }
529
530 Tizen::Base::String
531 CertificatePresentationModelUtility::GetOrganisation(const Tizen::Base::String& source)
532 {
533         String isContan(L"O=");
534         StringTokenizer strTokenizer(source, L"/");
535
536         while (strTokenizer.HasMoreTokens())
537         {
538                 String recipient;
539                 result r = strTokenizer.GetNextToken(recipient);
540                 if (IsFailed(r))
541                 {
542                         AppLogDebug("GetNextToken failed(%s)", GetErrorMessage(r));
543                         continue;
544                 }
545
546                 if (recipient.Contains(isContan))
547                 {
548                         recipient.Remove(0, isContan.GetLength());
549                         return recipient;
550                 }
551         }
552
553         SetLastResult(E_FAILURE);
554         return String();
555 }
556
557 Tizen::Base::String
558 CertificatePresentationModelUtility::GetHexPublicKeyString(Tizen::Security::IPublicKey& publicKey)
559 {
560         ByteBuffer* pBuffer = publicKey.GetEncodedN();
561         if (pBuffer == null)
562         {
563                 AppLogDebug("GetEncodedN is null");
564                 SetLastResult(E_FAILURE);
565                 return String();
566         }
567
568         byte hex = 0;
569         byte value = 0;
570         char hexChar[] = "0123456789abcdef";
571         String hexString;
572
573         for (int bufferCurrentPos = 0; bufferCurrentPos < pBuffer->GetCapacity(); bufferCurrentPos++)
574         {
575                 pBuffer->GetByte(bufferCurrentPos, value);
576                 hex = (value & 0xF0) >> PUBLIC_KEY_HEX_RIGHT_SHIFT_4;
577                 hexString.Append(hexChar[hex]);
578
579                 hex = value & 0x0F;
580                 hexString.Append(hexChar[hex]);
581                 hexString.Append(L":");
582         }
583         hexString.Remove(hexString.GetLength() - DEFAULT_COUNT, DEFAULT_COUNT);
584
585         delete pBuffer;
586         return hexString;
587 }