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));
96 CertificatePtr getOldAuthorSignerCertificate(DPL::String appid)
98 WidgetDAOReadOnly dao(appid);
99 CertificateChainList chainList = dao.getWidgetCertificate(SIGNATURE_AUTHOR);
101 FOREACH(it, chainList)
103 ValidationCore::CertificateCollection chain;
104 if (false == chain.load(*it)) {
105 LogError("Chain is broken");
109 LogError("Chain failed at sorting");
112 ValidationCore::CertificateList list = chain.getCertificateList();
116 if (!(*cert)->isRootCert() && !(*cert)->isCA()) {
121 return CertificatePtr(NULL);
123 } // namespace anonymous
126 namespace WidgetInstall {
127 TaskCertify::TaskCertify(InstallerContext &inCont) :
128 DPL::TaskDecl<TaskCertify>(this),
129 WidgetInstallPopup(inCont),
130 m_contextData(inCont)
132 AddStep(&TaskCertify::stepSignature);
133 // certi comparison determines whether the update.
134 if (true == m_contextData.existingWidgetInfo.isExist) {
135 AddStep(&TaskCertify::stepVerifyUpdate);
138 // Block until fixed popup issues
139 if (!GlobalSettings::PopupsTestModeEnabled()
140 && !m_installContext.m_quiet && !isTizenWebApp())
142 AddStep(&TaskCertify::stepWarningPopup);
143 AddStep(&TaskCertify::stepWarningPopupAnswer);
144 AddStep(&TaskCertify::stepAuthorInfoPopup);
145 AddStep(&TaskCertify::stepAuthorInfoPopupAnswer);
146 AddStep(&TaskCertify::StepDeletePopupWin);
148 AddStep(&TaskCertify::stepFinalize);
151 void TaskCertify::processDistributorSignature(const SignatureData &data,
154 // this signature is verified -
155 // no point in check domain WAC_ROOT and WAC_RECOGNIZED
156 m_contextData.wacSecurity.setDistributorSigned(true);
158 if (data.getStorageType().contains(CertStoreId::WAC_ROOT)) {
159 m_contextData.wacSecurity.setWacSigned(true);
162 CertificateCollection collection;
163 collection.load(data.getCertList());
164 Assert(collection.sort() &&
165 "Certificate collection can't sort");
167 Assert(collection.isChain() &&
168 "Certificate collection is not able to create chain. "
169 "It is not possible to verify this signature.");
171 m_contextData.wacSecurity.getCertificateChainListRef().push_back(
175 m_contextData.wacSecurity.getCertificateListRef().push_back(
176 toWidgetCertificateData(data, true));
177 m_contextData.wacSecurity.getCertificateListRef().push_back(
178 toWidgetCertificateData(data, false));
182 void TaskCertify::processAuthorSignature(const SignatureData &data)
184 using namespace ValidationCore;
185 LogInfo("DNS Identity match!");
186 // this signature is verified or widget is distributor signed
187 m_contextData.wacSecurity.setAuthorCertificatePtr(data.getEndEntityCertificatePtr());
188 CertificatePtr test = m_contextData.wacSecurity.getAuthorCertificatePtr();
190 m_contextData.wacSecurity.getCertificateListRef().push_back(
191 toWidgetCertificateData(data, true));
192 m_contextData.wacSecurity.getCertificateListRef().push_back(
193 toWidgetCertificateData(data, false));
195 // match widget_id with one from dns identity set
196 WacWidgetId widgetId(m_contextData.widgetConfig.configInfo.widget_id);
198 CertificatePtr cert = data.getEndEntityCertificatePtr();
200 Certificate::AltNameSet dnsIdentity = cert->getAlternativeNameDNS();
202 CertificateCollection collection;
203 collection.load(data.getCertList());
205 Assert(collection.isChain() &&
206 "Certificate collection is not able to create chain. "
207 "It is not possible to verify this signature.");
209 m_contextData.wacSecurity.getAuthorsCertificateChainListRef().push_back(
212 FOREACH(it, dnsIdentity){
213 if (widgetId.matchHost(*it)) {
214 m_contextData.wacSecurity.setRecognized(true);
220 void TaskCertify::stepSignature()
222 LogInfo("================ Step: <<Signature>> ENTER ===============");
224 std::string widgetPath = m_contextData.locations->getTemporaryRootDir() +
227 SignatureFileInfoSet signatureFiles;
228 SignatureFinder signatureFinder(widgetPath);
229 if (SignatureFinder::NO_ERROR != signatureFinder.find(signatureFiles)) {
230 LogError("Error in Signature Finder");
231 ThrowMsg(Exceptions::InvalidPackage,
232 "Error openig temporary widget directory");
235 SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
236 LogInfo("Number of signatures: " << signatureFiles.size());
238 bool firstDistributorSignature = true;
239 bool testCertificate = false;
241 bool complianceMode = GlobalDAOReadOnly::getComplianceMode();
243 for (; iter != signatureFiles.rend(); ++iter) {
244 LogInfo("Checking signature with id=" << iter->getFileNumber());
245 SignatureData data(widgetPath + iter->getFileName(),
246 iter->getFileNumber());
250 xml.initialize(data, GlobalConfig::GetSignatureXmlSchema());
253 WrtSignatureValidator::AppType appType =
254 WrtSignatureValidator::WAC20;
256 if (m_installContext.widgetConfig.webAppType ==
257 APP_TYPE_TIZENWEBAPP)
259 appType = WrtSignatureValidator::TIZEN;
262 WrtSignatureValidator::Result result;
264 WrtSignatureValidator validator(
267 OCSPTestModeEnabled(),
269 CrlTestModeEnabled(),
272 result = validator.check(data, widgetPath);
274 if (m_contextData.widgetConfig.packagingType
275 == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
277 // In directory installation mode, the validation is skipped.
279 result = WrtSignatureValidator::SIGNATURE_VERIFIED;
282 if (result == WrtSignatureValidator::SIGNATURE_REVOKED) {
283 LogWarning("Certificate is REVOKED");
284 ThrowMsg(Exceptions::InvalidPackage,
285 "Certificate is REVOKED");
288 if (result == WrtSignatureValidator::SIGNATURE_INVALID) {
289 LogWarning("Signature is INVALID");
290 // TODO change exception name
291 ThrowMsg(Exceptions::InvalidPackage,
295 if (data.isAuthorSignature()) {
296 if (result == WrtSignatureValidator::SIGNATURE_VERIFIED ||
297 m_contextData.wacSecurity.isDistributorSigned())
299 processAuthorSignature(data);
301 WrtSignatureValidator::SIGNATURE_DISREGARD)
306 if (result == WrtSignatureValidator::SIGNATURE_DISREGARD) {
309 // now signature _must_ be verified
310 processDistributorSignature(data, firstDistributorSignature);
311 firstDistributorSignature = false;
314 bool developerMode = GlobalDAOReadOnly::GetDeveloperMode();
316 std::string realMEID;
317 TapiHandle *tapiHandle = tel_init(NULL);
318 char *meid = tel_get_misc_me_sn_sync(tapiHandle);
323 tel_deinit(tapiHandle);
325 DeveloperModeValidator developerModeValidator(
328 GlobalDAOReadOnly::getComplianceFakeImei(),
329 GlobalDAOReadOnly::getComplianceFakeMeid(),
332 developerModeValidator.check(data);
335 data.getStorageType().contains(CertStoreId::DEVELOPER);
337 if (testCertificate && !developerMode) {
338 LogError("Widget signed by test certificate, "
339 "but developer mode is off.");
340 ThrowMsg(Exceptions::InvalidPackage,
341 "Widget signed by test certificate, "
342 "but developer mode is off.");
344 m_contextData.widgetConfig.isTestWidget = testCertificate;
345 } Catch(ParserSchemaException::Base) {
346 LogError("Error occured in ParserSchema.");
347 ReThrowMsg(Exceptions::InvalidPackage,
348 "Error occured in ParserSchema.");
350 Catch(DeveloperModeValidator::Exception::Base) {
351 LogError("Cannot validate developer certificate.");
352 ReThrowMsg(Exceptions::InvalidPackage,
353 "Cannot validate developer certificate.");
357 if (signatureFiles.empty()) {
358 LogInfo("No signature files has been found.");
361 LogInfo("================ Step: <<Signature>> DONE ================");
363 m_contextData.job->UpdateProgress(
364 InstallerContext::INSTALL_DIGSIG_CHECK,
365 "Widget Signature checked");
368 void TaskCertify::createInstallPopup(PopupType type, const std::string &label)
370 m_contextData.job->Pause();
374 bool ret = createPopup();
376 loadPopup(type, label);
380 void TaskCertify::StepDeletePopupWin()
385 void TaskCertify::stepWarningPopup()
387 LogInfo("Step:: <<Warning Popup>>");
388 // SP-2151: If widget is not recognized (OCSP status of any of certificates
389 // it is signed with is not recognized) WRT must notify user that
390 // widget cannot be installed as a trusted application, and let the
391 // user decide whether it should be installed as an untrusted
393 if (!m_contextData.wacSecurity.isDistributorSigned()) {
394 std::string label = UNTRUSTED_WIDGET +
397 createInstallPopup(PopupType::WIDGET_UNRECOGNIZED, label);
401 std::string TaskCertify::createAuthorWidgetInfo() const
403 std::string authorInfo;
404 if (m_contextData.wacSecurity.isRecognized()) {
405 //authorInfo += _("IDS_IM_WIDGET_RECOGNISED");
406 authorInfo += _("WIDGET RECOGNISED");
408 //authorInfo += _("IDS_IM_WIDGET_UNRECOGNISED");
409 authorInfo += _("WIDGET UNRECOGNISED");
412 authorInfo += LABEL_NEW_LINE_2;
413 ValidationCore::CertificatePtr authorCert =
414 m_contextData.wacSecurity.getAuthorCertificatePtr();
416 DPL::Optional < DPL::String > organizationName =
417 authorCert->getOrganizationName();
419 //authorInfo += _("IDS_IM_WIDGET_AUTHOR_ORGANIZATION_NAME");
420 authorInfo += _("AUTHOR ORGANIZATION NAME");
421 authorInfo += LABEL_NEW_LINE;
423 if (!organizationName.IsNull()) {
424 authorInfo += DPL::ToUTF8String(*organizationName);
426 //authorInfo += _("IDS_IM_WIDGET_ORGANIZATION_UNKNOWN");
427 authorInfo += _("WIDGET ORGANIZATION UNKNOWN");
430 authorInfo += LABEL_NEW_LINE_2;
432 DPL::Optional < DPL::String > countryName =
433 authorCert->getCountryName();
435 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_NAME");
436 authorInfo += _("WIDGET COUNTRY NAME");
437 authorInfo += LABEL_NEW_LINE;
439 if (!countryName.IsNull()) {
440 authorInfo += DPL::ToUTF8String(*countryName);
442 //authorInfo += _("IDS_IM_WIDGET_COUNTRY_UNKNOWN");
443 authorInfo += _("WIDGET COUNTRY UNKNOWN");
447 //_("IDS_IM_WIDGET_DOES_NOT_CONTAIN_RECOGNIZED_AUTHOR_SIGNATURE");
448 _("Widget does not contain recognized author signature");
453 void TaskCertify::stepAuthorInfoPopup()
455 LogInfo("Step:: <<Author Popup Information>>");
457 = createAuthorWidgetInfo() + LABEL_NEW_LINE_2 + QUESTION;
458 createInstallPopup(PopupType::WIDGET_AUTHOR_INFO, label);
461 void TaskCertify::stepFinalize()
463 LogInfo("Step: <<CERTYFYING DONE>>");
465 m_contextData.job->UpdateProgress(
466 InstallerContext::INSTALL_CERT_CHECK,
467 "Widget Certification Check Finished");
470 void TaskCertify::stepWarningPopupAnswer()
472 LogInfo("Step: <<Warning Popup Answer>>");
473 if (false == m_contextData.wacSecurity.isDistributorSigned() &&
474 WRT_POPUP_BUTTON_CANCEL == m_installCancel)
476 LogWarning("User does not agreed to install unsigned widgets!");
477 m_installCancel = WRT_POPUP_BUTTON;
479 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
483 void TaskCertify::stepAuthorInfoPopupAnswer()
485 LogInfo("Step: <<Author Info Popup Answer>>");
486 if (WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
487 LogWarning("User does not agreed to install widget!");
488 m_installCancel = WRT_POPUP_BUTTON;
490 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
494 bool TaskCertify::isTizenWebApp() const
497 if (m_installContext.widgetConfig.webAppType.appType
498 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
506 void TaskCertify::stepVerifyUpdate()
508 LogInfo("Step: <<Check Update>>");
509 CertificatePtr newCertificate =
510 m_contextData.wacSecurity.getAuthorCertificatePtr();
511 CertificatePtr oldCertificate =
512 getOldAuthorSignerCertificate(m_installContext.widgetConfig.tzAppid);
514 if (!!newCertificate && !!oldCertificate) {
515 if (0 != newCertificate->getBase64().compare(oldCertificate->getBase64())) {
516 LogDebug("old widget's author signer certificate : " <<
517 oldCertificate->getBase64());
518 LogDebug("new widget's author signer certificate : " <<
519 newCertificate->getBase64());
520 ThrowMsg(Exceptions::InvalidPackage,
521 "Author signer certificates doesn't match \
522 between old widget and installing widget");
525 if (!(NULL == newCertificate.Get() && NULL == oldCertificate.Get())) {
526 ThrowMsg(Exceptions::InvalidPackage,
527 "Author signer certificates doesn't match \
528 between old widget and installing widget");
532 } //namespace WidgetInstall