Fix for issue
[framework/osp/web.git] / src / controls / FWebCtrl_CertificateConfirmPopup.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_CertificateConfirmPopup.cpp
20  * @brief               The file contains the definition of _CertificateConfirmPopup class.
21  */
22 #include <FAppApp.h>
23 #include <FBaseColArrayList.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseUtilUri.h>
26 #include <FGrpDimension.h>
27 #include <FGrpRectangle.h>
28 #include <FIoDbEnumerator.h>
29 #include <FIoDbStatement.h>
30 #include <FSecCertX509Certificate.h>
31 #include <FUiCtrlButton.h>
32 #include <FUiCtrlLabel.h>
33 #include <FUiCtrlPanel.h>
34 #include <FUiKeyEventInfo.h>
35 #include <FUiLayout.h>
36 #include <FUiVerticalBoxLayout.h>
37 #include <FIo_DatabaseImpl.h>
38 #include <FSys_SystemResource.h>
39 #include <FUi_ControlManager.h>
40 #include <FUi_ResourceManager.h>
41 #include "FWebCtrl_CertificateConfirmPopup.h"
42 #include "FWebCtrl_EflWebkit.h"
43 #include "FWebCtrl_Utility.h"
44 #include "FWebCtrl_WebImpl.h"
45
46
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Utility;
50 using namespace Tizen::Graphics;
51 using namespace Tizen::Io;
52 using namespace Tizen::Security::Cert;
53 using namespace Tizen::System;
54 using namespace Tizen::Ui;
55 using namespace Tizen::Ui::Controls;
56
57
58 namespace Tizen { namespace Web { namespace Controls
59 {
60
61
62 static const int EDIT_TEXT_SIZE = 30;
63
64
65 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
66                                                 : __certPopupMode(CERTIFICATE_POPUP_MODE_USER_CONFIRM)
67                                                 , __confirm(false)
68                                                 , __pCertificatePolicyData(null)
69 {
70 }
71
72
73 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
74 {
75         if (IsModalPopup() == true)
76         {
77                 HandleUserAction(false);
78         }
79 }
80
81
82 result
83 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy)
84 {
85         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
86         result r = E_SUCCESS;
87
88         _SystemResource* pSysResource = _SystemResource::GetInstance();
89         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
90
91         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
92         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
93
94         __pCertificatePolicyData = pPolicy;
95         __certPopupMode = certPopupMode;
96         String titleText = L"";
97         int popupMaxHeight = 0;
98         Rectangle rect(0, 0, 0, 0);
99
100         ArrayList idList;
101         r = idList.Construct();
102         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         ArrayList titleList;
105         r = titleList.Construct();
106         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
107
108         if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
109         {
110                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATES");
111                 popupMaxHeight = 4*pPopupData->labelDim.height + pPopupData->btnDim.height + 2*pPopupData->sideMargin;
112
113                 rect.height = 4*pPopupData->labelDim.height;
114                 rect.width = pPopupData->labelDim.width;
115
116                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
117                 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
118         }
119         else    // CERTIFICATE_POPUP_MODE_CONFIRM
120         {
121                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_SECURITY_WARNING");
122                 popupMaxHeight = 2*pPopupData->labelDim.height + pPopupData->btnDim.height + 2*pPopupData->sideMargin;
123
124                 rect.height = 2*pPopupData->labelDim.height;
125                 rect.width = pPopupData->labelDim.width;
126
127                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
128                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
129                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
130
131                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_ALLOW"))));
132                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_VIEW"))));
133                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_CANCEL"))));
134         }
135
136         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
137         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
138
139         SetTitleText(titleText);
140
141         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
142         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
143
144         if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
145         {
146                 String certString;
147                 result r = GenerateCertifiate(certString);
148                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
149
150                 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
151                 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
152
153                 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
154                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
155
156                 r = pTextBox->SetTextSize(EDIT_TEXT_SIZE);
157                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
158
159                 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
160                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
161
162                 r = pTextBox->SetText(certString);
163                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
164
165                 r = AddControl(*pTextBox);
166                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
167
168                 TextBox* pCertBox = pTextBox.release();
169                 pLayout->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
170         }
171         else    // CERTIFICATE_POPUP_MODE_CONFIRM
172         {
173                 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
174                 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
175
176                 String message = L"";
177                 message = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SECURITY_CERTIFICATE_PROBLEM_MSG");
178                 message.Append(L"\n");
179                 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
180
181                 r = pLabel->Construct(rect, message);
182                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
183
184                 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
185
186                 r = AddControl(*pLabel);
187                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189                 Label* pInfoLabel = pLabel.release();
190                 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
191         }
192         Panel* pButtonPanel = CreateAndAddPanel();
193         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
194
195         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
196         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
197
198         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
199         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
200
201         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
202
203         SetPropagatedKeyEventListener(this);
204
205         return E_SUCCESS;
206 }
207
208
209 void
210 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
211 {
212         result r = E_SUCCESS;
213
214         switch (actionId)
215         {
216         case ID_BUTTON_CERTIFICATE_ALLOW:
217                 HandleUserAction(true);
218                 break;
219
220         case ID_BUTTON_CERTIFICATE_VIEW:
221         {
222                 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
223                 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
224
225                 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData);
226                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
227
228                 pCertificatePopup->SetOwner(this);
229                 
230                 r = pCertificatePopup->ShowPopup();
231                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
232
233                 pCertificatePopup.release();
234                 return;
235         }
236
237         case ID_BUTTON_CERTIFICATE_CANCEL:
238                 HandleUserAction(false);
239                 break;
240
241         case ID_BUTTON_CERTIFICATE_CLOSE:
242                 break;
243
244         default:
245                 SysAssert(false);
246                 break;
247         }
248
249         r = HidePopup();
250         if (IsFailed(r))
251         {
252                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
253         }
254         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
255         {
256                 GetOwner()->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
257         }
258 }
259
260 bool
261 _CertificateConfirmPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
262 {
263         return true;
264 }
265
266 bool
267 _CertificateConfirmPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
268 {
269         result r = E_SUCCESS;
270         if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
271         {
272                 HandleUserAction(false);
273                 r = HidePopup();
274                 if (IsFailed(r))
275                 {
276                         SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
277                 }
278                 if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
279                 {
280                         GetOwner()->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
281                 }
282         }
283
284         return true;
285 }
286
287 bool
288 _CertificateConfirmPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
289 {
290         return false;
291 }
292
293 bool
294 _CertificateConfirmPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
295 {
296         return false;
297 }
298
299 bool
300 _CertificateConfirmPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
301 {
302         return false;
303 }
304
305 bool
306 _CertificateConfirmPopup::GetConfirmResult() const
307 {
308         return __confirm;
309 }
310
311
312 void
313 _CertificateConfirmPopup::HandleUserAction(bool allow)
314 {
315         __confirm = allow;
316
317         String pem(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
318         ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, static_cast< Eina_Bool >(allow));
319         AddCertificateDb(pem, allow);
320 }
321
322
323 void
324 _CertificateConfirmPopup::AddCertificateDb(const String& pem, bool allow)
325 {
326         _DatabaseImpl db;
327         String certificatePath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
328         String table(CERTIFICATE_TABLE_NAME);
329
330         result r = db.Construct(certificatePath, "r+", null);
331         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
332
333         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (pem, allow) Values (?, ?)"));
334         SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
335
336         SysLog(NID_WEB_CTRL, "The current value of pem is %ls, allow is %d", pem.GetPointer(), allow);
337
338         pStmt->BindString(0, pem);
339         pStmt->BindInt(1, static_cast < int >(allow));
340
341         db.BeginTransaction();
342
343         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
344
345         db.CommitTransaction();
346 }
347
348
349 result
350 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
351 {
352         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
353
354         _SystemResource* pSysResource = _SystemResource::GetInstance();
355         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
356
357         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
358         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
359         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
360
361         X509Certificate certificate;
362         result r = certificate.Construct(*pByteBuf);
363         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
364
365         //Issued to
366         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_TO_C"));
367         certString.Append(L"\n\n");
368         String subject = certificate.GetSubject();
369
370         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
371         certString.Append(L"\n");
372         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
373
374         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
375         certString.Append(L"\n");
376         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
377
378         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP ,"IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
379         certString.Append(L"\n");
380         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
381
382         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_SERIAL_NUMBER"));
383         certString.Append(L":\n");
384         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
385
386         //Issued by
387         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_BY_C"));
388         certString.Append(L"\n\n");
389         String issuer = certificate.GetIssuer();
390
391         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
392         certString.Append(L"\n");
393         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
394
395         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
396         certString.Append(L"\n");
397         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
398
399         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
400         certString.Append(L"\n");
401         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
402
403         //Validity
404         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_VALIDITY_C"));
405         certString.Append(L"\n\n");
406         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_POP_CERTI_VALID_FROM_C"));
407         certString.Append(L"\n");
408         certString.Append(certificate.GetNotBefore() + L"\n\n");
409
410         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_VALID_TILL"));
411         certString.Append(L":\n");
412         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
413
414         //FingerPrints
415         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_ROOTCERTIFICATES_FINGER_PRINT"));
416         certString.Append(L":\n\n");
417         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SIGNATURE_ALGORITHM_VODA"));
418         certString.Append(L":\n");
419         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
420
421         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
422         if (pFingerPrint.get() && pFingerPrint->GetPointer())
423         {
424                 String fingerPrint;
425                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
426                 certString.Append(fingerPrint + L"\n\n");
427         }
428
429         return E_SUCCESS;
430 }
431
432
433 String
434 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
435 {
436         String inString(parseString);
437         int index = 0;
438
439         String outString;
440         outString.Append(L"\n");
441
442         result r = inString.IndexOf(parseToken, 0, index);
443         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
444
445         int prsTokLen = parseToken.GetLength();
446         r = inString.Remove(0, index + prsTokLen);
447         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
448
449         while (true)
450         {
451                 r = inString.IndexOf(parseToken, 0, index);
452
453                 switch (r)
454                 {
455                 case E_SUCCESS:
456                         r = inString.Remove(index, prsTokLen);
457                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
458
459                         inString.Insert(L"\n", index);
460                         continue;
461
462                 case E_OBJ_NOT_FOUND:
463                         r = inString.IndexOf('=', 0, index);
464                         if (r == E_SUCCESS)
465                         {
466                                 int slashIndex = 0;
467                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
468
469                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
470                                 {
471                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
472                                 }
473                         }
474                         inString.Append(L"\n");
475                         outString = inString;
476                         return outString;
477
478                 default:
479                         return outString;
480                 }
481         }
482 }
483
484
485 }}} // Tizen::Web::Controls