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 <wrt_error.h>
37 #include <dpl/wrt-dao-ro/global_config.h>
38 #include "wac_widget_id.h"
40 #include <vcore/Certificate.h>
41 #include <vcore/SignatureReader.h>
42 #include <vcore/SignatureFinder.h>
43 #include <vcore/WrtSignatureValidator.h>
44 #include <vcore/DeveloperModeValidator.h>
45 #include <dpl/utils/wrt_global_settings.h>
46 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
48 #include <ITapiModem.h>
49 #include <tapi_common.h>
51 using namespace ValidationCore;
52 using namespace WrtDB;
55 const std::string LABEL_NEW_LINE = "<br>";
56 const std::string LABEL_NEW_LINE_2 = "<br><br>";
57 const std::string UNTRUSTED_WIDGET ="It is an Untrusted Widget";
58 const char *QUESTION ="Do you wanto to install?";
60 WidgetCertificateData toWidgetCertificateData(const SignatureData &data,
63 WidgetCertificateData result;
65 result.chainId = data.getSignatureNumber();
67 result.owner = data.isAuthorSignature() ?
68 WidgetCertificateData::AUTHOR : WidgetCertificateData::DISTRIBUTOR;
71 WidgetCertificateData::ROOT : WidgetCertificateData::ENDENTITY;
73 CertificatePtr certificate;
76 certificate = data.getRootCaCertificatePtr();
78 certificate = data.getEndEntityCertificatePtr();
81 Assert(certificate && !certificate->getCommonName().IsNull() &&
82 "CommonName is Null");
84 result.strCommonName = *certificate->getCommonName();
86 result.strMD5Fingerprint = std::string("md5 ") +
87 Certificate::FingerprintToColonHex(
88 certificate->getFingerprint(Certificate::FINGERPRINT_MD5));
90 result.strSHA1Fingerprint = std::string("sha-1 ") +
91 Certificate::FingerprintToColonHex(
92 certificate->getFingerprint(Certificate::FINGERPRINT_SHA1));
96 } // namespace anonymous
99 namespace WidgetInstall {
100 TaskCertify::TaskCertify(InstallerContext &inCont) :
101 DPL::TaskDecl<TaskCertify>(this),
102 m_contextData(inCont),
103 WidgetInstallPopup(inCont)
105 AddStep(&TaskCertify::stepSignature);
107 // Block until fixed popup issues
108 if (!GlobalSettings::PopupsTestModeEnabled()
109 && !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() + "/";
193 SignatureFileInfoSet signatureFiles;
194 SignatureFinder signatureFinder(widgetPath);
195 if (SignatureFinder::NO_ERROR != signatureFinder.find(signatureFiles)) {
196 LogError("Error in Signature Finder");
197 ThrowMsg(Exceptions::InvalidPackage,
198 "Error openig temporary widget directory");
201 SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
202 LogInfo("Number of signatures: " << signatureFiles.size());
204 bool firstDistributorSignature = true;
205 bool testCertificate = false;
207 bool complianceMode = GlobalDAOReadOnly::getComplianceMode();
209 for (; iter != signatureFiles.rend(); ++iter) {
210 LogInfo("Checking signature with id=" << iter->getFileNumber());
211 SignatureData data(widgetPath + iter->getFileName(),
212 iter->getFileNumber());
216 xml.initialize(data, GlobalConfig::GetSignatureXmlSchema());
219 WrtSignatureValidator::AppType appType = WrtSignatureValidator::WAC20;
221 if (m_installContext.widgetConfig.webAppType == APP_TYPE_TIZENWEBAPP) {
222 appType = WrtSignatureValidator::TIZEN;
225 WrtSignatureValidator::Result result;
227 if (m_contextData.widgetConfig.packagingType
228 == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
230 // In directory installation mode, the validation is skipped.
232 result = WrtSignatureValidator::SIGNATURE_VERIFIED;
236 WrtSignatureValidator validator(appType,
237 !GlobalSettings::OCSPTestModeEnabled(),
238 !GlobalSettings::CrlTestModeEnabled(),
241 result = validator.check(data, widgetPath);
244 if (result == WrtSignatureValidator::SIGNATURE_REVOKED) {
245 LogWarning("Certificate is REVOKED");
246 ThrowMsg(Exceptions::InvalidPackage,
247 "Certificate is REVOKED");
250 if (result == WrtSignatureValidator::SIGNATURE_INVALID) {
251 LogWarning("Signature is INVALID");
252 // TODO change exception name
253 ThrowMsg(Exceptions::InvalidPackage,
257 if (data.isAuthorSignature()) {
258 if (result == WrtSignatureValidator::SIGNATURE_VERIFIED ||
259 m_contextData.wacSecurity.isDistributorSigned())
261 processAuthorSignature(data);
262 } else if (result == WrtSignatureValidator::SIGNATURE_DISREGARD) {
266 if (result == WrtSignatureValidator::SIGNATURE_DISREGARD) {
269 // now signature _must_ be verified
270 processDistributorSignature(data, firstDistributorSignature);
271 firstDistributorSignature = false;
274 bool developerMode = GlobalDAOReadOnly::GetDeveloperMode();
276 std::string realMEID;
277 TapiHandle *tapiHandle = tel_init(NULL);
278 char *meid = tel_get_misc_me_sn_sync(tapiHandle);
284 tel_deinit(tapiHandle);
286 DeveloperModeValidator developerModeValidator(
289 GlobalDAOReadOnly::getComplianceFakeImei(),
290 GlobalDAOReadOnly::getComplianceFakeMeid(),
293 developerModeValidator.check(data);
296 data.getStorageType().contains(CertStoreId::DEVELOPER);
298 if (testCertificate && !developerMode) {
299 LogError("Widget signed by test certificate, "
300 "but developer mode is off.");
301 ThrowMsg(Exceptions::InvalidPackage,
302 "Widget signed by test certificate, "
303 "but developer mode is off.");
305 m_contextData.widgetConfig.isTestWidget = testCertificate;
306 } Catch(ParserSchemaException::Base) {
307 LogError("Error occured in ParserSchema.");
308 ReThrowMsg(Exceptions::InvalidPackage,
309 "Error occured in ParserSchema.");
311 Catch(DeveloperModeValidator::Exception::Base) {
312 LogError("Cannot validate developer certificate.");
313 ReThrowMsg(Exceptions::InvalidPackage,
314 "Cannot validate developer certificate.");
318 if (signatureFiles.empty()) {
319 LogInfo("No signature files has been found.");
322 LogInfo("================ Step: <<Signature>> DONE ================");
324 m_contextData.job->UpdateProgress(
325 InstallerContext::INSTALL_DIGSIG_CHECK,
326 "Widget Signature checked");
329 void TaskCertify::createInstallPopup(PopupType type, const std::string &label)
331 m_contextData.job->Pause();
334 bool ret = createPopup();
337 loadPopup(type, label);
341 void TaskCertify::StepDeletePopupWin()
346 void TaskCertify::stepWarningPopup()
348 LogInfo("Step:: <<Warning Popup>>");
349 // SP-2151: If widget is not recognized (OCSP status of any of certificates
350 // it is signed with is not recognized) WRT must notify user that
351 // widget cannot be installed as a trusted application, and let the
352 // user decide whether it should be installed as an untrusted
354 if (!m_contextData.wacSecurity.isDistributorSigned()) {
355 std::string label = UNTRUSTED_WIDGET +
358 createInstallPopup(PopupType::WIDGET_UNRECOGNIZED, label);
362 std::string TaskCertify::createAuthorWidgetInfo() const
364 std::string authorInfo;
365 if (m_contextData.wacSecurity.isRecognized()) {
366 //authorInfo += _("IDS_IM_WIDGET_RECOGNISED");
367 authorInfo += _("WIDGET RECOGNISED");
369 //authorInfo += _("IDS_IM_WIDGET_UNRECOGNISED");
370 authorInfo += _("WIDGET UNRECOGNISED");
373 authorInfo += LABEL_NEW_LINE_2;
374 ValidationCore::CertificatePtr authorCert =
375 m_contextData.wacSecurity.getAuthorCertificatePtr();
377 DPL::Optional < DPL::String > organizationName =
378 authorCert->getOrganizationName();
380 //authorInfo += _("IDS_IM_WIDGET_AUTHOR_ORGANIZATION_NAME");
381 authorInfo += _("AUTHOR ORGANIZATION NAME");
382 authorInfo += LABEL_NEW_LINE;
384 if (!organizationName.IsNull()) {
385 authorInfo += DPL::ToUTF8String(*organizationName);
387 //authorInfo += _("IDS_IM_WIDGET_ORGANIZATION_UNKNOWN");
388 authorInfo += _("WIDGET ORGANIZATION UNKNOWN");
391 authorInfo += LABEL_NEW_LINE_2;
393 DPL::Optional < DPL::String > countryName =
394 authorCert->getCountryName();
396 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_NAME");
397 authorInfo += _("WIDGET COUNTRY NAME");
398 authorInfo += LABEL_NEW_LINE;
400 if (!countryName.IsNull()) {
401 authorInfo += DPL::ToUTF8String(*countryName);
403 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_UNKNOWN");
404 authorInfo += _("WIDGET COUNTRY UNKNOWN");
408 //_("IDS_IM_WIDGET_DOES_NOT_CONTAIN_RECOGNIZED_AUTHOR_SIGNATURE");
409 _("Widget does not contain recognized author signature");
414 void TaskCertify::stepAuthorInfoPopup()
416 LogInfo("Step:: <<Author Popup Information>>");
418 = createAuthorWidgetInfo() + LABEL_NEW_LINE_2 + QUESTION;
419 createInstallPopup(PopupType::WIDGET_AUTHOR_INFO, label);
422 void TaskCertify::stepFinalize()
424 LogInfo("Step: <<CERTYFYING DONE>>");
426 m_contextData.job->UpdateProgress(
427 InstallerContext::INSTALL_CERT_CHECK,
428 "Widget Certification Check Finished");
432 void TaskCertify::stepWarningPopupAnswer()
434 LogInfo("Step: <<Warning Popup Answer>>");
435 if (false == m_contextData.wacSecurity.isDistributorSigned() &&
436 WRT_POPUP_BUTTON_CANCEL == m_installCancel)
438 LogWarning("User does not agreed to install unsigned widgets!");
439 m_installCancel = WRT_POPUP_BUTTON;
441 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
445 void TaskCertify::stepAuthorInfoPopupAnswer()
447 LogInfo("Step: <<Author Info Popup Answer>>");
448 if ( WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
449 LogWarning("User does not agreed to install widget!");
450 m_installCancel = WRT_POPUP_BUTTON;
452 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
456 bool TaskCertify::isTizenWebApp() const
459 if (m_installContext.widgetConfig.webAppType.appType
460 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
465 } //namespace WidgetInstall