2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_certify.cpp
18 * @author Pawel Sikorski (p.sikorski@samgsung.com)
26 #include <dpl/assert.h>
27 #include <appcore-common.h> //TODO is it necessary here?
31 #include <widget_install/task_certify.h>
32 #include <widget_install/job_widget_install.h>
33 #include <widget_install/widget_install_errors.h>
34 #include <widget_install/widget_install_context.h>
35 #include <dpl/log/log.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include "wac_widget_id.h"
39 #include <vcore/Certificate.h>
40 #include <vcore/SignatureReader.h>
41 #include <vcore/SignatureFinder.h>
42 #include <vcore/WrtSignatureValidator.h>
43 #include <vcore/DeveloperModeValidator.h>
44 #include <dpl/utils/wrt_global_settings.h>
45 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
47 #include <ITapiModem.h>
48 #include <tapi_common.h>
50 using namespace ValidationCore;
51 using namespace WrtDB;
54 const std::string LABEL_NEW_LINE = "<br>";
55 const std::string LABEL_NEW_LINE_2 = "<br><br>";
56 const std::string UNTRUSTED_WIDGET = "It is an Untrusted Widget";
57 const char *QUESTION = "Do you wanto to install?";
59 WidgetCertificateData toWidgetCertificateData(const SignatureData &data,
62 WidgetCertificateData result;
64 result.chainId = data.getSignatureNumber();
66 result.owner = data.isAuthorSignature() ?
67 WidgetCertificateData::AUTHOR : WidgetCertificateData::DISTRIBUTOR;
70 WidgetCertificateData::ROOT : WidgetCertificateData::ENDENTITY;
72 CertificatePtr certificate;
75 certificate = data.getRootCaCertificatePtr();
77 certificate = data.getEndEntityCertificatePtr();
80 Assert(certificate && !certificate->getCommonName().IsNull() &&
81 "CommonName is Null");
83 result.strCommonName = *certificate->getCommonName();
85 result.strMD5Fingerprint = std::string("md5 ") +
86 Certificate::FingerprintToColonHex(
87 certificate->getFingerprint(Certificate::FINGERPRINT_MD5));
89 result.strSHA1Fingerprint = std::string("sha-1 ") +
90 Certificate::FingerprintToColonHex(
91 certificate->getFingerprint(Certificate::FINGERPRINT_SHA1));
95 } // namespace anonymous
98 namespace WidgetInstall {
99 TaskCertify::TaskCertify(InstallerContext &inCont) :
100 DPL::TaskDecl<TaskCertify>(this),
101 WidgetInstallPopup(inCont),
102 m_contextData(inCont)
104 AddStep(&TaskCertify::stepSignature);
106 // Block until fixed popup issues
107 if (!GlobalSettings::PopupsTestModeEnabled()
108 && !m_installContext.m_quiet && !isTizenWebApp())
110 AddStep(&TaskCertify::stepWarningPopup);
111 AddStep(&TaskCertify::stepWarningPopupAnswer);
112 AddStep(&TaskCertify::stepAuthorInfoPopup);
113 AddStep(&TaskCertify::stepAuthorInfoPopupAnswer);
114 AddStep(&TaskCertify::StepDeletePopupWin);
116 AddStep(&TaskCertify::stepFinalize);
119 void TaskCertify::processDistributorSignature(const SignatureData &data,
122 // this signature is verified -
123 // no point in check domain WAC_ROOT and WAC_RECOGNIZED
124 m_contextData.wacSecurity.setDistributorSigned(true);
126 if (data.getStorageType().contains(CertStoreId::WAC_ROOT)) {
127 m_contextData.wacSecurity.setWacSigned(true);
130 CertificateCollection collection;
131 collection.load(data.getCertList());
132 Assert(collection.sort() &&
133 "Certificate collection can't sort");
135 Assert(collection.isChain() &&
136 "Certificate collection is not able to create chain. "
137 "It is not possible to verify this signature.");
139 m_contextData.wacSecurity.getCertificateChainListRef().push_back(
143 m_contextData.wacSecurity.getCertificateListRef().push_back(
144 toWidgetCertificateData(data, true));
145 m_contextData.wacSecurity.getCertificateListRef().push_back(
146 toWidgetCertificateData(data, false));
150 void TaskCertify::processAuthorSignature(const SignatureData &data)
152 using namespace ValidationCore;
153 LogInfo("DNS Identity match!");
154 // this signature is verified or widget is distributor signed
155 m_contextData.wacSecurity.getAuthorCertificatePtr() =
156 data.getEndEntityCertificatePtr();
157 m_contextData.wacSecurity.getCertificateListRef().push_back(
158 toWidgetCertificateData(data, true));
159 m_contextData.wacSecurity.getCertificateListRef().push_back(
160 toWidgetCertificateData(data, false));
162 // match widget_id with one from dns identity set
163 WacWidgetId widgetId(m_contextData.widgetConfig.configInfo.widget_id);
165 CertificatePtr cert = data.getEndEntityCertificatePtr();
167 Certificate::AltNameSet dnsIdentity = cert->getAlternativeNameDNS();
169 CertificateCollection collection;
170 collection.load(data.getCertList());
172 Assert(collection.isChain() &&
173 "Certificate collection is not able to create chain. "
174 "It is not possible to verify this signature.");
176 m_contextData.wacSecurity.getAuthorsCertificateChainListRef().push_back(
179 FOREACH(it, dnsIdentity){
180 if (widgetId.matchHost(*it)) {
181 m_contextData.wacSecurity.setRecognized(true);
187 void TaskCertify::stepSignature()
189 LogInfo("================ Step: <<Signature>> ENTER ===============");
191 std::string widgetPath = m_contextData.locations->getTemporaryRootDir() +
194 SignatureFileInfoSet signatureFiles;
195 SignatureFinder signatureFinder(widgetPath);
196 if (SignatureFinder::NO_ERROR != signatureFinder.find(signatureFiles)) {
197 LogError("Error in Signature Finder");
198 ThrowMsg(Exceptions::InvalidPackage,
199 "Error openig temporary widget directory");
202 SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
203 LogInfo("Number of signatures: " << signatureFiles.size());
205 bool firstDistributorSignature = true;
206 bool testCertificate = false;
208 bool complianceMode = GlobalDAOReadOnly::getComplianceMode();
210 for (; iter != signatureFiles.rend(); ++iter) {
211 LogInfo("Checking signature with id=" << iter->getFileNumber());
212 SignatureData data(widgetPath + iter->getFileName(),
213 iter->getFileNumber());
217 xml.initialize(data, GlobalConfig::GetSignatureXmlSchema());
220 WrtSignatureValidator::AppType appType =
221 WrtSignatureValidator::WAC20;
223 if (m_installContext.widgetConfig.webAppType ==
224 APP_TYPE_TIZENWEBAPP)
226 appType = WrtSignatureValidator::TIZEN;
229 WrtSignatureValidator::Result result;
231 WrtSignatureValidator validator(
234 OCSPTestModeEnabled(),
236 CrlTestModeEnabled(),
239 result = validator.check(data, widgetPath);
241 if (m_contextData.widgetConfig.packagingType
242 == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
244 // In directory installation mode, the validation is skipped.
246 result = WrtSignatureValidator::SIGNATURE_VERIFIED;
249 if (result == WrtSignatureValidator::SIGNATURE_REVOKED) {
250 LogWarning("Certificate is REVOKED");
251 ThrowMsg(Exceptions::InvalidPackage,
252 "Certificate is REVOKED");
255 if (result == WrtSignatureValidator::SIGNATURE_INVALID) {
256 LogWarning("Signature is INVALID");
257 // TODO change exception name
258 ThrowMsg(Exceptions::InvalidPackage,
262 if (data.isAuthorSignature()) {
263 if (result == WrtSignatureValidator::SIGNATURE_VERIFIED ||
264 m_contextData.wacSecurity.isDistributorSigned())
266 processAuthorSignature(data);
268 WrtSignatureValidator::SIGNATURE_DISREGARD)
273 if (result == WrtSignatureValidator::SIGNATURE_DISREGARD) {
276 // now signature _must_ be verified
277 processDistributorSignature(data, firstDistributorSignature);
278 firstDistributorSignature = false;
281 bool developerMode = GlobalDAOReadOnly::GetDeveloperMode();
283 std::string realMEID;
284 TapiHandle *tapiHandle = tel_init(NULL);
285 char *meid = tel_get_misc_me_sn_sync(tapiHandle);
290 tel_deinit(tapiHandle);
292 DeveloperModeValidator developerModeValidator(
295 GlobalDAOReadOnly::getComplianceFakeImei(),
296 GlobalDAOReadOnly::getComplianceFakeMeid(),
299 developerModeValidator.check(data);
302 data.getStorageType().contains(CertStoreId::DEVELOPER);
304 if (testCertificate && !developerMode) {
305 LogError("Widget signed by test certificate, "
306 "but developer mode is off.");
307 ThrowMsg(Exceptions::InvalidPackage,
308 "Widget signed by test certificate, "
309 "but developer mode is off.");
311 m_contextData.widgetConfig.isTestWidget = testCertificate;
312 } Catch(ParserSchemaException::Base) {
313 LogError("Error occured in ParserSchema.");
314 ReThrowMsg(Exceptions::InvalidPackage,
315 "Error occured in ParserSchema.");
317 Catch(DeveloperModeValidator::Exception::Base) {
318 LogError("Cannot validate developer certificate.");
319 ReThrowMsg(Exceptions::InvalidPackage,
320 "Cannot validate developer certificate.");
324 if (signatureFiles.empty()) {
325 LogInfo("No signature files has been found.");
328 LogInfo("================ Step: <<Signature>> DONE ================");
330 m_contextData.job->UpdateProgress(
331 InstallerContext::INSTALL_DIGSIG_CHECK,
332 "Widget Signature checked");
335 void TaskCertify::createInstallPopup(PopupType type, const std::string &label)
337 m_contextData.job->Pause();
341 bool ret = createPopup();
343 loadPopup(type, label);
347 void TaskCertify::StepDeletePopupWin()
352 void TaskCertify::stepWarningPopup()
354 LogInfo("Step:: <<Warning Popup>>");
355 // SP-2151: If widget is not recognized (OCSP status of any of certificates
356 // it is signed with is not recognized) WRT must notify user that
357 // widget cannot be installed as a trusted application, and let the
358 // user decide whether it should be installed as an untrusted
360 if (!m_contextData.wacSecurity.isDistributorSigned()) {
361 std::string label = UNTRUSTED_WIDGET +
364 createInstallPopup(PopupType::WIDGET_UNRECOGNIZED, label);
368 std::string TaskCertify::createAuthorWidgetInfo() const
370 std::string authorInfo;
371 if (m_contextData.wacSecurity.isRecognized()) {
372 //authorInfo += _("IDS_IM_WIDGET_RECOGNISED");
373 authorInfo += _("WIDGET RECOGNISED");
375 //authorInfo += _("IDS_IM_WIDGET_UNRECOGNISED");
376 authorInfo += _("WIDGET UNRECOGNISED");
379 authorInfo += LABEL_NEW_LINE_2;
380 ValidationCore::CertificatePtr authorCert =
381 m_contextData.wacSecurity.getAuthorCertificatePtr();
383 DPL::Optional < DPL::String > organizationName =
384 authorCert->getOrganizationName();
386 //authorInfo += _("IDS_IM_WIDGET_AUTHOR_ORGANIZATION_NAME");
387 authorInfo += _("AUTHOR ORGANIZATION NAME");
388 authorInfo += LABEL_NEW_LINE;
390 if (!organizationName.IsNull()) {
391 authorInfo += DPL::ToUTF8String(*organizationName);
393 //authorInfo += _("IDS_IM_WIDGET_ORGANIZATION_UNKNOWN");
394 authorInfo += _("WIDGET ORGANIZATION UNKNOWN");
397 authorInfo += LABEL_NEW_LINE_2;
399 DPL::Optional < DPL::String > countryName =
400 authorCert->getCountryName();
402 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_NAME");
403 authorInfo += _("WIDGET COUNTRY NAME");
404 authorInfo += LABEL_NEW_LINE;
406 if (!countryName.IsNull()) {
407 authorInfo += DPL::ToUTF8String(*countryName);
409 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_UNKNOWN");
410 authorInfo += _("WIDGET COUNTRY UNKNOWN");
414 //_("IDS_IM_WIDGET_DOES_NOT_CONTAIN_RECOGNIZED_AUTHOR_SIGNATURE");
415 _("Widget does not contain recognized author signature");
420 void TaskCertify::stepAuthorInfoPopup()
422 LogInfo("Step:: <<Author Popup Information>>");
424 = createAuthorWidgetInfo() + LABEL_NEW_LINE_2 + QUESTION;
425 createInstallPopup(PopupType::WIDGET_AUTHOR_INFO, label);
428 void TaskCertify::stepFinalize()
430 LogInfo("Step: <<CERTYFYING DONE>>");
432 m_contextData.job->UpdateProgress(
433 InstallerContext::INSTALL_CERT_CHECK,
434 "Widget Certification Check Finished");
437 void TaskCertify::stepWarningPopupAnswer()
439 LogInfo("Step: <<Warning Popup Answer>>");
440 if (false == m_contextData.wacSecurity.isDistributorSigned() &&
441 WRT_POPUP_BUTTON_CANCEL == m_installCancel)
443 LogWarning("User does not agreed to install unsigned widgets!");
444 m_installCancel = WRT_POPUP_BUTTON;
446 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
450 void TaskCertify::stepAuthorInfoPopupAnswer()
452 LogInfo("Step: <<Author Info Popup Answer>>");
453 if (WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
454 LogWarning("User does not agreed to install widget!");
455 m_installCancel = WRT_POPUP_BUTTON;
457 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
461 bool TaskCertify::isTizenWebApp() const
464 if (m_installContext.widgetConfig.webAppType.appType
465 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
472 } //namespace WidgetInstall