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 <dpl/utils/wrt_global_settings.h>
44 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
46 #include <ITapiModem.h>
47 #include <tapi_common.h>
49 using namespace ValidationCore;
50 using namespace WrtDB;
53 const std::string LABEL_NEW_LINE = "<br>";
54 const std::string LABEL_NEW_LINE_2 = "<br><br>";
55 const std::string UNTRUSTED_WIDGET = "It is an Untrusted Widget";
56 const char *QUESTION = "Do you wanto to install?";
58 WidgetCertificateData toWidgetCertificateData(const SignatureData &data,
61 WidgetCertificateData result;
63 result.chainId = data.getSignatureNumber();
64 LogDebug("result.chainId : " << result.chainId);
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 m_contextData(inCont)
131 AddStep(&TaskCertify::stepSignature);
132 // certi comparison determines whether the update.
133 if (true == m_contextData.isUpdateMode) {
134 AddStep(&TaskCertify::stepVerifyUpdate);
136 AddStep(&TaskCertify::stepFinalize);
139 void TaskCertify::processDistributorSignature(const SignatureData &data)
141 // this signature is verified -
142 // no point in check domain WAC_ROOT and WAC_RECOGNIZED
143 m_contextData.wacSecurity.setDistributorSigned(true);
145 CertificateCollection collection;
146 collection.load(data.getCertList());
147 Assert(collection.sort() &&
148 "Certificate collection can't sort");
150 Assert(collection.isChain() &&
151 "Certificate collection is not able to create chain. "
152 "It is not possible to verify this signature.");
154 m_contextData.wacSecurity.getCertificateChainListRef().push_back(
157 if (data.getSignatureNumber() == 1) {
158 m_contextData.wacSecurity.getCertificateListRef().push_back(
159 toWidgetCertificateData(data, true));
160 m_contextData.wacSecurity.getCertificateListRef().push_back(
161 toWidgetCertificateData(data, false));
165 void TaskCertify::processAuthorSignature(const SignatureData &data)
167 using namespace ValidationCore;
168 LogInfo("DNS Identity match!");
169 // this signature is verified or widget is distributor signed
170 m_contextData.wacSecurity.setAuthorCertificatePtr(data.getEndEntityCertificatePtr());
171 CertificatePtr test = m_contextData.wacSecurity.getAuthorCertificatePtr();
173 m_contextData.wacSecurity.getCertificateListRef().push_back(
174 toWidgetCertificateData(data, true));
175 m_contextData.wacSecurity.getCertificateListRef().push_back(
176 toWidgetCertificateData(data, false));
178 // match widget_id with one from dns identity set
179 WacWidgetId widgetId(m_contextData.widgetConfig.configInfo.widget_id);
181 CertificatePtr cert = data.getEndEntityCertificatePtr();
183 Certificate::AltNameSet dnsIdentity = cert->getAlternativeNameDNS();
185 CertificateCollection collection;
186 collection.load(data.getCertList());
188 Assert(collection.isChain() &&
189 "Certificate collection is not able to create chain. "
190 "It is not possible to verify this signature.");
192 m_contextData.wacSecurity.getAuthorsCertificateChainListRef().push_back(
195 FOREACH(it, dnsIdentity){
196 if (widgetId.matchHost(*it)) {
197 m_contextData.wacSecurity.setRecognized(true);
203 void TaskCertify::stepSignature()
205 LogInfo("================ Step: <<Signature>> ENTER ===============");
207 std::string widgetPath;
208 if (m_contextData.widgetConfig.packagingType ==
209 WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
211 widgetPath = m_contextData.locations->getSourceDir() + "/";
213 widgetPath = m_contextData.locations->getTemporaryPackageDir() + "/";
216 SignatureFileInfoSet signatureFiles;
217 SignatureFinder signatureFinder(widgetPath);
218 if (SignatureFinder::NO_ERROR != signatureFinder.find(signatureFiles)) {
219 LogError("Error in Signature Finder");
220 ThrowMsg(Exceptions::SignatureNotFound,
221 "Error openig temporary widget directory");
224 SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
225 LogInfo("Number of signatures: " << signatureFiles.size());
227 bool complianceMode = GlobalDAOReadOnly::getComplianceMode();
229 for (; iter != signatureFiles.rend(); ++iter) {
230 LogInfo("Checking signature with id=" << iter->getFileNumber());
231 SignatureData data(widgetPath + iter->getFileName(),
232 iter->getFileNumber());
236 xml.initialize(data, GlobalConfig::GetSignatureXmlSchema());
239 WrtSignatureValidator::AppType appType =
240 WrtSignatureValidator::WAC20;
242 if (m_contextData.widgetConfig.webAppType ==
243 APP_TYPE_TIZENWEBAPP)
245 appType = WrtSignatureValidator::TIZEN;
248 WrtSignatureValidator::Result result;
250 WrtSignatureValidator validator(
253 OCSPTestModeEnabled(),
255 CrlTestModeEnabled(),
258 result = validator.check(data, widgetPath);
260 if (m_contextData.widgetConfig.packagingType
261 == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP ||
262 m_contextData.job->getInstallerStruct().m_installMode
263 == InstallMode::INSTALL_MODE_PRELOAD)
265 // In directory installation mode, the validation is skipped.
267 result = WrtSignatureValidator::SIGNATURE_VERIFIED;
270 if (result == WrtSignatureValidator::SIGNATURE_REVOKED) {
271 LogWarning("Certificate is REVOKED");
272 ThrowMsg(Exceptions::CertificateExpired,
273 "Certificate is REVOKED");
276 if (result == WrtSignatureValidator::SIGNATURE_INVALID) {
277 LogWarning("Signature is INVALID");
278 // TODO change exception name
279 ThrowMsg(Exceptions::SignatureInvalid,
283 if (data.isAuthorSignature()) {
284 if (result == WrtSignatureValidator::SIGNATURE_VERIFIED ||
285 m_contextData.wacSecurity.isDistributorSigned())
287 processAuthorSignature(data);
289 WrtSignatureValidator::SIGNATURE_DISREGARD)
294 // now signature _must_ be verified
295 processDistributorSignature(data);
297 } Catch(ParserSchemaException::Base) {
298 LogError("Error occured in ParserSchema.");
299 ReThrowMsg(Exceptions::SignatureInvalid,
300 "Error occured in ParserSchema.");
304 if (signatureFiles.empty()) {
305 LogInfo("No signature files has been found.");
308 LogInfo("================ Step: <<Signature>> DONE ================");
310 m_contextData.job->UpdateProgress(
311 InstallerContext::INSTALL_DIGSIG_CHECK,
312 "Widget Signature checked");
315 void TaskCertify::stepFinalize()
317 LogInfo("Step: <<CERTYFYING DONE>>");
319 m_contextData.job->UpdateProgress(
320 InstallerContext::INSTALL_CERT_CHECK,
321 "Widget Certification Check Finished");
324 bool TaskCertify::isTizenWebApp() const
327 if (m_contextData.widgetConfig.webAppType.appType
328 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
336 void TaskCertify::stepVerifyUpdate()
338 LogInfo("Step: <<Check Update>>");
339 CertificatePtr newCertificate =
340 m_contextData.wacSecurity.getAuthorCertificatePtr();
341 CertificatePtr oldCertificate =
342 getOldAuthorSignerCertificate(m_contextData.widgetConfig.tzAppid);
344 if (!!newCertificate && !!oldCertificate) {
345 if (0 != newCertificate->getBase64().compare(oldCertificate->getBase64())) {
346 LogDebug("old widget's author signer certificate : " <<
347 oldCertificate->getBase64());
348 LogDebug("new widget's author signer certificate : " <<
349 newCertificate->getBase64());
350 ThrowMsg(Exceptions::NotMatchedCertification,
351 "Author signer certificates doesn't match \
352 between old widget and installing widget");
355 if (!(NULL == newCertificate.Get() && NULL == oldCertificate.Get())) {
356 ThrowMsg(Exceptions::NotMatchedCertification,
357 "Author signer certificates doesn't match \
358 between old widget and installing widget");
362 } //namespace WidgetInstall