Fixed crash during saving certificate.
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_certificates.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 7285b50..a6693bc
@@ -37,10 +37,10 @@ using namespace WrtDB;
 
 namespace Jobs {
 namespace WidgetInstall {
-
 TaskCertificates::TaskCertificates(InstallerContext& context) :
     DPL::TaskDecl<TaskCertificates>(this),
-    m_context(context)
+    m_context(context),
+    m_pkgHandle(NULL)
 {
     AddStep(&TaskCertificates::StepSetCertiInfo);
     AddAbortStep(&TaskCertificates::StepAbortCertiInfo);
@@ -49,8 +49,35 @@ TaskCertificates::TaskCertificates(InstallerContext& context) :
 void TaskCertificates::StepSetCertiInfo()
 {
     LogDebug("StepSetCertiInfo");
+
+    if (pkgmgr_installer_create_certinfo_set_handle(&m_pkgHandle) < 0) {
+        LogError("pkgmgrInstallerCreateCertinfoSetHandle fail");
+        ThrowMsg(Exceptions::SetCertificateInfoFailed,
+                 "Failed to create certificate handle");
+    }
+
     SetCertiInfo(SIGNATURE_AUTHOR);
     SetCertiInfo(SIGNATURE_DISTRIBUTOR);
+
+    if ((pkgmgr_installer_save_certinfo(
+             const_cast<char*>(DPL::ToUTF8String(
+                                   m_context.widgetConfig.tzPkgid).c_str()),
+             m_pkgHandle)) < 0)
+    {
+        LogError("pkgmgrInstallerSaveCertinfo fail");
+        ThrowMsg(Exceptions::SetCertificateInfoFailed,
+                 "Failed to Installer Save Certinfo");
+    } else {
+        LogDebug("Succeed to save Certinfo");
+    }
+
+    if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
+        LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
+    }
+
+    m_context.job->UpdateProgress(
+            InstallerContext::INSTALL_SET_CERTINFO,
+            "Save certinfo to pkgmgr");
 }
 
 void TaskCertificates::SetCertiInfo(CertificateSource source)
@@ -59,68 +86,84 @@ void TaskCertificates::SetCertiInfo(CertificateSource source)
     CertificateChainList certificateChainList;
     m_context.wacSecurity.getCertificateChainList(certificateChainList, source);
 
-    pkgmgr_instcertinfo_h handle;
-
     FOREACH(it, certificateChainList)
     {
-        LogDebug("Insert certinfo to pkgmgr db");
+        LogDebug("Insert certinfo to pkgmgr structure");
 
         ValidationCore::CertificateCollection chain;
+
         if (false == chain.load(*it)) {
             LogError("Chain is broken");
+            ThrowMsg(Exceptions::SetCertificateInfoFailed,
+                     "Failed to Installer Save Certinfo");
         }
 
-        chain.sort();
+        if (!chain.sort()) {
+            LogError("Chain failed at sorting");
+        }
 
         ValidationCore::CertificateList list = chain.getCertificateList();
 
-        FOREACH(certIt, list) 
+        FOREACH(certIt, list)
         {
-            ValidationCore::Crypto::Hash::SHA1 sha1;
-            sha1.Append((*certIt)->getDER());
-            sha1.Finish();
-            std::string sha1String = sha1.ToBase64String();
-
-            if (pkgmgr_installer_create_certinfo_set_handle(&handle)< 0) {
-                LogError("pkgmgrInstallerCreateCertinfoSetHandle fail");
-                ThrowMsg(Exceptions::SetCertificateInfoFailed,
-                        "Failed to create certificate handle");
-            }
+            pkgmgr_instcert_type instCertType;
 
             if (source == SIGNATURE_DISTRIBUTOR) {
-                if((pkgmgr_installer_set_cert_value(
-                                handle,
-                                PM_SET_DISTRIBUTOR_SIGNER_CERT,
-                                const_cast<char*>(sha1String.c_str()))) < 0) {
-                    LogError("pkgmgrInstallerSetCertValue fail");
-                    ThrowMsg(Exceptions::SetCertificateInfoFailed,
-                            "Failed to Set CertValue");
-                }
-            }
-            else {
-                if((pkgmgr_installer_set_cert_value(
-                                handle,
-                                PM_SET_AUTHOR_SIGNER_CERT,
-                                const_cast<char*>(sha1String.c_str()))) < 0) {
-                    LogError("pkgmgrInstallerSetCertValue fail");
-                    ThrowMsg(Exceptions::SetCertificateInfoFailed,
-                            "Failed to Installer Set CertValue");
+                bool distributor1 = false;
+                if (!(*certIt)->getCommonName().IsNull()) {
+                    std::string
+                        Name(DPL::ToUTF8String(*(*certIt)->getCommonName()));
+                    std::string tizenStr("Tizen");
+                    if (0 == Name.compare(0, tizenStr.length(), tizenStr)) {
+                        distributor1 = true;
+                    }
                 }
-            }
 
-            if ((pkgmgr_installer_save_certinfo(
-                            const_cast<char*>(DPL::ToUTF8String(
-                                    *m_context.widgetConfig.pkgname).c_str()),
-                            handle)) < 0) {
-                LogError("pkgmgrInstallerSaveCertinfo fail");
-                ThrowMsg(Exceptions::SetCertificateInfoFailed,
-                        "Failed to Installer Save Certinfo");
+                if (distributor1) {
+                    LogDebug("Set SIGNATURE_DISTRIBUTOR");
+                    if ((*certIt)->isRootCert()) {
+                        instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
+                    } else {
+                        if ((*certIt)->isCA()) {
+                            instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
+                        } else {
+                            instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
+                        }
+                    }
+                } else {
+                    LogDebug("Set SIGNATURE_DISTRIBUTOR2");
+                    if ((*certIt)->isRootCert()) {
+                        instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
+                    } else {
+                        if ((*certIt)->isCA()) {
+                            instCertType =
+                                PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
+                        } else {
+                            instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
+                        }
+                    }
+                }
             } else {
-                LogDebug("Succeed to save Certinfo");
+                LogDebug("set SIGNATURE_AUTHOR");
+                if ((*certIt)->isRootCert()) {
+                    instCertType = PM_SET_AUTHOR_ROOT_CERT;
+                } else {
+                    if ((*certIt)->isCA()) {
+                        instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
+                    } else {
+                        instCertType = PM_SET_AUTHOR_SIGNER_CERT;
+                    }
+                }
             }
-
-            if (pkgmgr_installer_destroy_certinfo_set_handle(handle) < 0) {
-                LogError("pkgmgrInstallerDestroyCertinfoSetHandle fail");
+            LogDebug("cert type : " << instCertType);
+            if ((pkgmgr_installer_set_cert_value(
+                     m_pkgHandle,
+                     instCertType,
+                     const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
+            {
+                LogError("pkgmgrInstallerSetCertValue fail");
+                ThrowMsg(Exceptions::SetCertificateInfoFailed,
+                         "Failed to Set CertValue");
             }
         }
     }
@@ -128,13 +171,13 @@ void TaskCertificates::SetCertiInfo(CertificateSource source)
 
 void TaskCertificates::StepAbortCertiInfo()
 {
-    pkgmgr_instcertinfo_h handle;
-
     if ((pkgmgr_installer_delete_certinfo(
-            const_cast<char*>(DPL::ToUTF8String(
-                *m_context.widgetConfig.pkgname).c_str()))) < 0) {
+             const_cast<char*>(DPL::ToUTF8String(
+                                   m_context.widgetConfig.tzPkgid).c_str()))) <
+        0)
+    {
         LogError("pkgmgr_installer_delete_certinfo fail");
     }
 }
-}
-}
+} //namespace WidgetInstall
+} //namespace Jobs