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)
28 #include <dpl/assert.h>
32 #include <widget_install/task_certify.h>
33 #include <widget_install/job_widget_install.h>
34 #include <widget_install/widget_install_errors.h>
35 #include <widget_install/widget_install_context.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>
45 #include <ITapiModem.h>
46 #include <tapi_common.h>
48 #include <installer_log.h>
50 using namespace ValidationCore;
51 using namespace WrtDB;
55 WidgetCertificateData toWidgetCertificateData(const SignatureData &data,
58 WidgetCertificateData result;
60 result.chainId = data.getSignatureNumber();
61 _D("result.chainId : %d", result.chainId);
63 result.owner = data.isAuthorSignature() ?
64 WidgetCertificateData::AUTHOR : WidgetCertificateData::DISTRIBUTOR;
67 WidgetCertificateData::ROOT : WidgetCertificateData::ENDENTITY;
69 CertificatePtr certificate;
72 certificate = data.getRootCaCertificatePtr();
74 certificate = data.getEndEntityCertificatePtr();
77 Assert(certificate && !certificate->getCommonName().IsNull() &&
78 "CommonName is Null");
80 result.strCommonName = *certificate->getCommonName();
82 result.strMD5Fingerprint = std::string("md5 ") +
83 Certificate::FingerprintToColonHex(
84 certificate->getFingerprint(Certificate::FINGERPRINT_MD5));
86 result.strSHA1Fingerprint = std::string("sha-1 ") +
87 Certificate::FingerprintToColonHex(
88 certificate->getFingerprint(Certificate::FINGERPRINT_SHA1));
93 CertificatePtr getOldAuthorSignerCertificate(DPL::String appid)
95 WidgetDAOReadOnly dao(appid);
96 CertificateChainList chainList = dao.getWidgetCertificate(SIGNATURE_AUTHOR);
98 FOREACH(it, chainList)
100 ValidationCore::CertificateCollection chain;
101 if (false == chain.load(*it)) {
102 _E("Chain is broken");
106 _E("Chain failed at sorting");
109 ValidationCore::CertificateList list = chain.getCertificateList();
113 if (!(*cert)->isRootCert() && !(*cert)->isCA()) {
118 return CertificatePtr(NULL);
120 } // namespace anonymous
123 namespace WidgetInstall {
124 TaskCertify::TaskCertify(InstallerContext &inCont) :
125 DPL::TaskDecl<TaskCertify>(this),
126 m_contextData(inCont)
128 AddStep(&TaskCertify::StartStep);
129 AddStep(&TaskCertify::stepSignature);
130 // certi comparison determines whether the update.
131 if (true == m_contextData.isUpdateMode) {
132 AddStep(&TaskCertify::stepVerifyUpdate);
134 AddStep(&TaskCertify::EndStep);
137 void TaskCertify::processDistributorSignature(const SignatureData &data)
139 // this signature is verified -
140 // no point in check domain WAC_ROOT and WAC_RECOGNIZED
141 m_contextData.widgetSecurity.setDistributorSigned(true);
143 CertificateCollection collection;
144 collection.load(data.getCertList());
145 Assert(collection.sort() &&
146 "Certificate collection can't sort");
148 Assert(collection.isChain() &&
149 "Certificate collection is not able to create chain. "
150 "It is not possible to verify this signature.");
152 m_contextData.widgetSecurity.getCertificateChainListRef().push_back(
155 if (data.getSignatureNumber() == 1) {
156 m_contextData.widgetSecurity.getCertificateListRef().push_back(
157 toWidgetCertificateData(data, true));
158 m_contextData.widgetSecurity.getCertificateListRef().push_back(
159 toWidgetCertificateData(data, false));
163 void TaskCertify::processAuthorSignature(const SignatureData &data)
165 using namespace ValidationCore;
166 _D("DNS Identity match!");
167 // this signature is verified or widget is distributor signed
168 m_contextData.widgetSecurity.setAuthorCertificatePtr(data.getEndEntityCertificatePtr());
169 CertificatePtr test = m_contextData.widgetSecurity.getAuthorCertificatePtr();
171 m_contextData.widgetSecurity.getCertificateListRef().push_back(
172 toWidgetCertificateData(data, true));
173 m_contextData.widgetSecurity.getCertificateListRef().push_back(
174 toWidgetCertificateData(data, false));
176 // match widget_id with one from dns identity set
177 WacWidgetId widgetId(m_contextData.widgetConfig.configInfo.widget_id);
179 CertificatePtr cert = data.getEndEntityCertificatePtr();
181 Certificate::AltNameSet dnsIdentity = cert->getAlternativeNameDNS();
183 CertificateCollection collection;
184 collection.load(data.getCertList());
186 Assert(collection.isChain() &&
187 "Certificate collection is not able to create chain. "
188 "It is not possible to verify this signature.");
190 m_contextData.widgetSecurity.getAuthorsCertificateChainListRef().push_back(
193 FOREACH(it, dnsIdentity){
194 if (widgetId.matchHost(*it)) {
195 m_contextData.widgetSecurity.setRecognized(true);
201 void TaskCertify::getSignatureFiles(std::string path, SignatureFileInfoSet& file)
203 _D("path : %s", path.c_str());
204 SignatureFileInfoSet signatureFiles;
205 SignatureFinder signatureFinder(path);
206 if (SignatureFinder::NO_ERROR != signatureFinder.find(file)) {
207 _E("Error in Signature Finder : %s", path.c_str());
208 ThrowMsg(Exceptions::SignatureNotFound,
209 "Error openig temporary widget directory");
213 void TaskCertify::stepSignature()
215 _D("================ Step: <<Signature>> ENTER ===============");
217 std::string widgetPath;
218 widgetPath = m_contextData.locations->getPackageInstallationDir() + "/";
220 SignatureFileInfoSet signatureFiles;
223 getSignatureFiles(widgetPath, signatureFiles);
225 if (signatureFiles.size() <= 0) {
226 widgetPath += std::string(WrtDB::GlobalConfig::GetWidgetSrcPath())
228 if (0 == access(widgetPath.c_str(), F_OK)) {
229 getSignatureFiles(widgetPath, signatureFiles);
232 } Catch(Exceptions::SignatureNotFound) {
233 ReThrowMsg(Exceptions::SignatureNotFound, widgetPath);
236 SignatureFileInfoSet::reverse_iterator iter = signatureFiles.rbegin();
237 _D("Number of signatures: %d", signatureFiles.size());
239 for (; iter != signatureFiles.rend(); ++iter) {
240 _D("Checking signature with id=%d", iter->getFileNumber());
241 SignatureData data(widgetPath + iter->getFileName(),
242 iter->getFileNumber());
246 xml.initialize(data, GlobalConfig::GetSignatureXmlSchema());
249 WrtSignatureValidator::Result result;
251 WrtSignatureValidator validator(
252 WrtSignatureValidator::TIZEN,
254 OCSPTestModeEnabled(),
256 CrlTestModeEnabled(),
259 result = validator.check(data, widgetPath);
261 if (m_contextData.mode.installTime
262 == InstallMode::InstallTime::PRELOAD)
264 result = WrtSignatureValidator::SIGNATURE_VERIFIED;
267 if (result == WrtSignatureValidator::SIGNATURE_REVOKED) {
268 _W("Certificate is REVOKED");
269 ThrowMsg(Exceptions::CertificateExpired,
270 "Certificate is REVOKED");
273 if (result == WrtSignatureValidator::SIGNATURE_INVALID &&
274 iter->getFileNumber() <= 1) {
275 _W("Signature is INVALID");
276 // TODO change exception name
277 ThrowMsg(Exceptions::SignatureInvalid,
281 if (data.isAuthorSignature()) {
282 if (result == WrtSignatureValidator::SIGNATURE_VERIFIED ) {
283 processAuthorSignature(data);
286 if (result != WrtSignatureValidator::SIGNATURE_INVALID) {
287 processDistributorSignature(data);
290 } Catch(ParserSchemaException::Base) {
291 _E("Error occured in ParserSchema.");
292 ReThrowMsg(Exceptions::SignatureInvalid,
293 "Error occured in ParserSchema.");
297 if (signatureFiles.empty()) {
298 _D("No signature files has been found.");
301 _D("================ Step: <<Signature>> DONE ================");
303 m_contextData.job->UpdateProgress(
304 InstallerContext::INSTALL_DIGSIG_CHECK,
305 "Widget Signature checked");
308 bool TaskCertify::isTizenWebApp() const
311 if (m_contextData.widgetConfig.webAppType.appType
312 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
320 void TaskCertify::stepVerifyUpdate()
322 _D("Step: <<Check Update>>");
323 CertificatePtr newCertificate =
324 m_contextData.widgetSecurity.getAuthorCertificatePtr();
325 CertificatePtr oldCertificate =
326 getOldAuthorSignerCertificate(m_contextData.widgetConfig.tzAppid);
328 if (!!newCertificate && !!oldCertificate) {
329 if (0 != newCertificate->getBase64().compare(oldCertificate->getBase64())) {
330 _D("old widget's author signer certificate : %s", (oldCertificate->getBase64()).c_str());
331 _D("new widget's author signer certificate : %s", (newCertificate->getBase64()).c_str());
332 ThrowMsg(Exceptions::NotMatchedCertification,
333 "Author signer certificates doesn't match \
334 between old widget and installing widget");
337 if (!(NULL == newCertificate.Get() && NULL == oldCertificate.Get())) {
338 ThrowMsg(Exceptions::NotMatchedCertification,
339 "Author signer certificates doesn't match \
340 between old widget and installing widget");
345 void TaskCertify::StartStep()
347 _D("--------- <TaskCertify> : START ----------");
350 void TaskCertify::EndStep()
352 _D("Step: <<CERTYFYING DONE>>");
354 m_contextData.job->UpdateProgress(
355 InstallerContext::INSTALL_CERT_CHECK,
356 "Widget Certification Check Finished");
358 _D("--------- <TaskCertify> : END ----------");
360 } //namespace WidgetInstall