Merge from tizen_2.1
[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 <FUiLayout.h>
35 #include <FUiVerticalBoxLayout.h>
36 #include <FIo_DatabaseImpl.h>
37 #include <FSys_SystemResource.h>
38 #include <FUi_ControlManager.h>
39 #include <FUi_ResourceManager.h>
40 #include "FWebCtrl_CertificateConfirmPopup.h"
41 #include "FWebCtrl_EflWebkit.h"
42 #include "FWebCtrl_Utility.h"
43
44
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Base::Utility;
48 using namespace Tizen::Graphics;
49 using namespace Tizen::Io;
50 using namespace Tizen::Security::Cert;
51 using namespace Tizen::System;
52 using namespace Tizen::Ui;
53 using namespace Tizen::Ui::Controls;
54
55
56 namespace Tizen { namespace Web { namespace Controls
57 {
58
59
60 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
61                                                 : __certPopupMode(CERTIFICATE_POPUP_MODE_USER_CONFIRM)
62                                                 , __confirm(false)
63                                                 , __pCertificatePolicyData(null)
64 {
65 }
66
67
68 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
69 {
70 }
71
72
73 result
74 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy)
75 {
76         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
77         result r = E_SUCCESS;
78
79         _SystemResource* pSysResource = _SystemResource::GetInstance();
80         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
81
82         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
83         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
84
85         __pCertificatePolicyData = pPolicy;
86         __certPopupMode = certPopupMode;
87         String titleText = L"";
88         int popupMaxHeight = 0;
89         Rectangle rect(0, 0, 0, 0);
90
91         ArrayList idList;
92         r = idList.Construct();
93         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
94
95         ArrayList titleList;
96         r = titleList.Construct();
97         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
98
99         if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
100         {
101                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATES");
102                 popupMaxHeight = pPopupData->popupDim.height;
103
104                 rect.height = pPopupData->popupDim.height - 4*pPopupData->sideMargin - 2*pPopupData->btnDim.height;
105                 rect.width = pPopupData->labelDim.width;
106
107                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
108                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_COM_SK_DONE"))));
109         }
110         else    // CERTIFICATE_POPUP_MODE_CONFIRM
111         {
112                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_SECURITY_WARNING");
113                 popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
114
115                 rect.height = 2*pPopupData->labelDim.height;
116                 rect.width = pPopupData->labelDim.width;
117
118                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
119                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
120                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
121
122                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_ALLOW"))));
123                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_VIEW"))));
124                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_CANCEL"))));
125         }
126
127         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
128         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
129
130         SetTitleText(titleText);
131
132         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
133         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
134
135         if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
136         {
137                 String certString;
138                 result r = GenerateCertifiate(certString);
139                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
140
141                 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
142                 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
143
144                 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
145                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
146
147                 r = pTextBox->SetTextSize(30);
148                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
149
150                 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
151                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
152
153                 r = pTextBox->SetText(certString);
154                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
155
156                 r = AddControl(*pTextBox);
157                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
158
159                 TextBox* pCertBox = pTextBox.release();
160                 pLayout->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
161         }
162         else    // CERTIFICATE_POPUP_MODE_CONFIRM
163         {
164                 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
165                 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
166
167                 String message = L"";
168                 message = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SECURITY_CERTIFICATE_PROBLEM_MSG");
169                 message.Append(L"\n");
170                 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
171
172                 r = pLabel->Construct(rect, message);
173                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
174
175                 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
176
177                 r = AddControl(*pLabel);
178                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
179
180                 Label* pInfoLabel = pLabel.release();
181                 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
182         }
183         Panel* pButtonPanel = CreateAndAddPanel();
184         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
185
186         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
187         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
190         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
191
192         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
193
194         return E_SUCCESS;
195 }
196
197
198 void
199 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
200 {
201         result r = E_SUCCESS;
202
203         switch (actionId)
204         {
205         case ID_BUTTON_CERTIFICATE_ALLOW:
206                 HandleUserAction(true);
207                 break;
208
209         case ID_BUTTON_CERTIFICATE_VIEW:
210         {
211                 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
212                 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
213
214                 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData);
215                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
216
217                 pCertificatePopup->SetOwner(this);
218                 
219                 r = pCertificatePopup->ShowPopup();
220                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
221
222                 pCertificatePopup.release();
223                 return;
224         }
225
226         case ID_BUTTON_CERTIFICATE_CANCEL:
227                 HandleUserAction(false);
228                 break;
229
230         case ID_BUTTON_CERTIFICATE_CLOSE:
231                 break;
232
233         default:
234                 SysAssert(false);
235                 break;
236         }
237
238         r = HidePopup();
239         if (IsFailed(r))
240         {
241                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
242         }
243         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
244         {
245                 delete this;
246         }
247 }
248
249
250 bool
251 _CertificateConfirmPopup::GetConfirmResult() const
252 {
253         return __confirm;
254 }
255
256
257 void
258 _CertificateConfirmPopup::HandleUserAction(bool allow)
259 {
260         __confirm = allow;
261
262         String pem(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
263         ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, static_cast< Eina_Bool >(allow));
264         AddCertificateDb(pem, allow);
265 }
266
267
268 void
269 _CertificateConfirmPopup::AddCertificateDb(const String& pem, bool allow)
270 {
271         _DatabaseImpl db;
272         String certificatePath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
273         String table(CERTIFICATE_TABLE_NAME);
274
275         result r = db.Construct(certificatePath, "r+", null);
276         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
277
278         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (pem, allow) Values (?, ?)"));
279         SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
280
281         SysLog(NID_WEB_CTRL, "The current value of pem is %ls, allow is %d", pem.GetPointer(), allow);
282
283         pStmt->BindString(0, pem);
284         pStmt->BindInt(1, static_cast < int >(allow));
285
286         db.BeginTransaction();
287
288         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
289
290         db.CommitTransaction();
291 }
292
293 result
294 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
295 {
296         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
297
298         _SystemResource* pSysResource = _SystemResource::GetInstance();
299         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
300
301         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
302         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
303         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
304
305         X509Certificate certificate;
306         result r = certificate.Construct(*pByteBuf);
307         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
308
309         //Issued to
310         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_TO_C"));
311         certString.Append(L"\n\n");
312         String subject = certificate.GetSubject();
313
314         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
315         certString.Append(L"\n");
316         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
317
318         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
319         certString.Append(L"\n");
320         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
321
322         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP ,"IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
323         certString.Append(L"\n");
324         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
325
326         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_SERIAL_NUMBER"));
327         certString.Append(L":\n");
328         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
329
330         //Issued by
331         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_BY_C"));
332         certString.Append(L"\n\n");
333         String issuer = certificate.GetIssuer();
334
335         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
336         certString.Append(L"\n");
337         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
338
339         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
340         certString.Append(L"\n");
341         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
342
343         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
344         certString.Append(L"\n");
345         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
346
347         //Validity
348         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_VALIDITY_C"));
349         certString.Append(L"\n\n");
350         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_POP_CERTI_VALID_FROM_C"));
351         certString.Append(L"\n");
352         certString.Append(certificate.GetNotBefore() + L"\n\n");
353
354         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_VALID_TILL"));
355         certString.Append(L":\n");
356         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
357
358         //FingerPrints
359         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_ROOTCERTIFICATES_FINGER_PRINT"));
360         certString.Append(L":\n\n");
361         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SIGNATURE_ALGORITHM_VODA"));
362         certString.Append(L":\n");
363         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
364
365         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
366         if (pFingerPrint.get() && pFingerPrint->GetPointer())
367         {
368                 String fingerPrint;
369                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
370                 certString.Append(fingerPrint + L"\n\n");
371         }
372
373         return E_SUCCESS;
374 }
375
376
377 String
378 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
379 {
380         String inString(parseString);
381         int index = 0;
382
383         String outString;
384         outString.Append(L"\n");
385
386         result r = inString.IndexOf(parseToken, 0, index);
387         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
388
389         int prsTokLen = parseToken.GetLength();
390         r = inString.Remove(0, index + prsTokLen);
391         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
392
393         while (true)
394         {
395                 r = inString.IndexOf(parseToken, 0, index);
396
397                 switch (r)
398                 {
399                 case E_SUCCESS:
400                         r = inString.Remove(index, prsTokLen);
401                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
402
403                         inString.Insert(L"\n", index);
404                         continue;
405
406                 case E_OBJ_NOT_FOUND:
407                         r = inString.IndexOf('=', 0, index);
408                         if (r == E_SUCCESS)
409                         {
410                                 int slashIndex = 0;
411                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
412
413                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
414                                 {
415                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
416                                 }
417                         }
418                         inString.Append(L"\n");
419                         outString = inString;
420                         return outString;
421
422                 default:
423                         return outString;
424                 }
425         }
426 }
427
428
429 }}} // Tizen::Web::Controls