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