fixes for static analysis tool results
[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 <FBaseColArrayList.h>
23 #include <FBaseSysLog.h>
24 #include <FBaseUtilUri.h>
25 #include <FGrpDimension.h>
26 #include <FGrpRectangle.h>
27 #include <FSecCertX509Certificate.h>
28 #include <FUiCtrlButton.h>
29 #include <FUiCtrlLabel.h>
30 #include <FUiCtrlPanel.h>
31 #include <FUiLayout.h>
32 #include <FUiVerticalBoxLayout.h>
33 #include <FUi_ControlManager.h>
34 #include <FUi_ResourceManager.h>
35 #include "FWebCtrl_CertificateConfirmPopup.h"
36 #include "FWebCtrl_Utility.h"
37
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Utility;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Io;
44 using namespace Tizen::Security::Cert;
45 using namespace Tizen::Ui;
46 using namespace Tizen::Ui::Controls;
47
48
49 namespace Tizen { namespace Web { namespace Controls
50 {
51
52
53 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
54                                                 : __certPopupMode(CERTIFICATE_POPUP_MODE_USER_CONFIRM)
55                                                 , __confirm(false)
56                                                 , __pCertificatePolicyData(null)
57 {
58 }
59
60
61 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
62 {
63 }
64
65
66 result
67 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy)
68 {
69         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
70         result r = E_SUCCESS;
71
72         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
73         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
74
75         __pCertificatePolicyData = pPolicy;
76         __certPopupMode = certPopupMode;
77         String titleText = L"";
78         int popupMaxHeight = 0;
79         Rectangle rect(0, 0, 0, 0);
80
81         ArrayList idList;
82         r = idList.Construct();
83         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
84
85         ArrayList titleList;
86         r = titleList.Construct();
87         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
88
89         if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
90         {
91                 titleText = L"Certificate";
92                 popupMaxHeight = pPopupData->popupDim.height;
93
94                 rect.height = pPopupData->popupDim.height - 4*pPopupData->sideMargin - 2*pPopupData->btnDim.height;
95                 rect.width = pPopupData->labelDim.width;
96
97                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
98                 titleList.Add(*(new String(L"Close")));
99         }
100         else    // CERTIFICATE_POPUP_MODE_CONFIRM
101         {
102                 titleText = L"Security Warning";
103                 popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
104
105                 rect.height = 2*pPopupData->labelDim.height;
106                 rect.width = pPopupData->labelDim.width;
107
108                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
109                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
110                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
111
112                 titleList.Add(*(new String(L"Allow")));
113                 titleList.Add(*(new String(L"View")));
114                 titleList.Add(*(new String(L"Cancel")));
115         }
116
117         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
118         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
119
120         SetTitleText(titleText);
121
122         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
123         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
124
125         if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
126         {
127                 String certString;
128                 result r = GenerateCertifiate(certString);
129                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
130
131                 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
132                 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
133
134                 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
135                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
136
137                 r = pTextBox->SetTextSize(30);
138                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139
140                 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
141                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
142
143                 r = pTextBox->SetText(certString);
144                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
145
146                 r = AddControl(*pTextBox);
147                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
148
149                 TextBox* pCertBox = pTextBox.release();
150                 pLayout->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
151         }
152         else    // CERTIFICATE_POPUP_MODE_CONFIRM
153         {
154                 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
155                 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
156
157                 String message = L"";
158                 message = L"There are problems with the security certificate of this site.\n";
159                 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
160
161                 r = pLabel->Construct(rect, message);
162                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
163
164                 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
165
166                 r = AddControl(*pLabel);
167                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
168
169                 Label* pInfoLabel = pLabel.release();
170                 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
171         }
172         Panel* pButtonPanel = CreateAndAddPanel();
173         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
174
175         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
176         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
177
178         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
179         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
180
181         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
182
183         return E_SUCCESS;
184 }
185
186
187 void
188 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
189 {
190         result r = E_SUCCESS;
191
192         switch (actionId)
193         {
194         case ID_BUTTON_CERTIFICATE_ALLOW:
195                 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, EINA_TRUE);
196                 __confirm = static_cast< bool >(EINA_TRUE);
197                 break;
198
199         case ID_BUTTON_CERTIFICATE_VIEW:
200         {
201                 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
202                 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
203
204                 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData);
205                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
206
207                 pCertificatePopup->SetOwner(this);
208                 
209                 r = pCertificatePopup->ShowPopup();
210                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
211
212                 pCertificatePopup.release();
213                 return;
214         }
215
216         case ID_BUTTON_CERTIFICATE_CANCEL:
217                 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, EINA_FALSE);
218                 __confirm = static_cast< bool >(EINA_FALSE);
219                 break;
220
221         case ID_BUTTON_CERTIFICATE_CLOSE:
222                 break;
223
224         default:
225                 SysAssert(false);
226                 break;
227         }
228
229         r = HidePopup();
230         if (IsFailed(r))
231         {
232                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
233         }
234         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
235         {
236                 delete this;
237         }
238 }
239
240
241 bool
242 _CertificateConfirmPopup::GetConfirmResult() const
243 {
244         return __confirm;
245 }
246
247
248 result
249 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
250 {
251         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
252
253         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
254         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
255         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
256
257         X509Certificate certificate;
258         result r = certificate.Construct(*pByteBuf);
259         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
260
261         //Issued to
262         certString.Append(L"ISSUED TO:\n\n");
263         String subject = certificate.GetSubject();
264
265         certString.Append(L"Common Name:\n");
266         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
267
268         certString.Append(L"Organization:\n");
269         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
270
271         certString.Append(L"Organizational Unit:\n");
272         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
273
274         certString.Append(L"Serial Number:\n");
275         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
276
277         //Issued by
278         certString.Append(L"ISSUER:\n\n");
279         String issuer = certificate.GetIssuer();
280
281         certString.Append(L"Common Name:\n");
282         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
283
284         certString.Append(L"Organization:\n");
285         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
286
287         certString.Append(L"Organizational Unit:\n");
288         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
289
290         //Validity
291         certString.Append(L"VALIDITY:\n\n");
292         certString.Append(L"Valid From:\n");
293         certString.Append(certificate.GetNotBefore() + L"\n\n");
294
295         certString.Append(L"Valid Till:\n");
296         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
297
298         //FingerPrints
299         certString.Append(L"FINGERPRINTS:\n\n");
300         certString.Append(L"Signature Algorithm:\n");
301         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
302
303         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
304         if (pFingerPrint.get() && pFingerPrint->GetPointer())
305         {
306                 String fingerPrint;
307                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
308                 certString.Append(fingerPrint + L"\n\n");
309         }
310
311         return E_SUCCESS;
312 }
313
314
315 String
316 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
317 {
318         String inString(parseString);
319         int index = 0;
320
321         String outString;
322         outString.Append(L"\n");
323
324         result r = inString.IndexOf(parseToken, 0, index);
325         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
326
327         int prsTokLen = parseToken.GetLength();
328         r = inString.Remove(0, index + prsTokLen);
329         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
330
331         while (true)
332         {
333                 r = inString.IndexOf(parseToken, 0, index);
334
335                 switch (r)
336                 {
337                 case E_SUCCESS:
338                         r = inString.Remove(index, prsTokLen);
339                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
340
341                         inString.Insert(L"\n", index);
342                         continue;
343
344                 case E_OBJ_NOT_FOUND:
345                         r = inString.IndexOf('=', 0, index);
346                         if (r == E_SUCCESS)
347                         {
348                                 int slashIndex = 0;
349                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
350
351                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
352                                 {
353                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
354                                 }
355                         }
356                         inString.Append(L"\n");
357                         outString = inString;
358                         return outString;
359
360                 default:
361                         return outString;
362                 }
363         }
364 }
365
366
367 }}} // Tizen::Web::Controls