From d01d582c964779e92c8dc36b2c6bf2f6838780af Mon Sep 17 00:00:00 2001 From: Ugo Mignon <56931733+TartanLeGrand@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:31:10 +0200 Subject: [PATCH 1/3] Accept self-signed identities when no WWDR intermediate matches A leaf that is its own issuer has no chain to embed, so build the CMS signature without one instead of failing. Useful for tests. --- src/openssl.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/openssl.cpp b/src/openssl.cpp index aa92820..b57b1f8 100644 --- a/src/openssl.cpp +++ b/src/openssl.cpp @@ -391,11 +391,12 @@ bool ZSignAsset::GenerateCMS(void* pscert, void* pspkey, const string& strCDHash // Fall back to the embedded WWDR intermediate matching the leaf's issuer. unsigned long issuerHash = X509_issuer_name_hash(scert); const char* szIssuerCert = WWDRIntermediatePEM(issuerHash); - if (NULL == szIssuerCert) { + if (NULL == szIssuerCert && X509_V_OK == X509_check_issued(scert, scert)) { + // self-signed identity (e.g. generated for tests): there is no chain to embed + } else if (NULL == szIssuerCert) { ZLog::ErrorV(">>> Unknown issuer hash 0x%08lx! No embedded WWDR intermediate matches and the p12 carries no usable CA chain.\n", issuerHash); return false; - } - if (!AppendPEMCert(otherCerts, szIssuerCert)) { + } else if (!AppendPEMCert(otherCerts, szIssuerCert)) { return CMSError(); } } From b4a1ba2057b2e21c280148dea59bfe1b0160ca3c Mon Sep 17 00:00:00 2001 From: Ugo Mignon <56931733+TartanLeGrand@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:31:10 +0200 Subject: [PATCH 2/3] Apply per-bundle entitlements Zupersign built a single asset from entitlements[0] and applied it to every executable in the app. Build one asset per entitlements_data_t instead, bound to the bundle at its bundle_path, and let SignNode pick the asset of each bundle (falling back to the app's). --- src/bundle.cpp | 29 ++++++++++++++++++++++- src/bundle.h | 2 ++ src/lib/Zupersign.cpp | 54 ++++++++++++++++++++++++++++++++----------- src/openssl.h | 1 + 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/bundle.cpp b/src/bundle.cpp index b349697..0591d8f 100644 --- a/src/bundle.cpp +++ b/src/bundle.cpp @@ -8,6 +8,7 @@ ZBundle::ZBundle() { m_pSignAssets = NULL; m_pSignAsset = NULL; + m_pDefaultAsset = NULL; m_bForceSign = false; m_bWeakInject = false; m_bRemoveProvision = false; @@ -309,8 +310,24 @@ static bool IsAppExtensionPath(const string& strPath) return (0 == strPath.rfind("PlugIns/", 0) || 0 == strPath.rfind("Extensions/", 0)); } +// The asset bound to this bundle (by its path relative to the app, "/" for the app itself), +// falling back to the default one. This lets each bundle have its own entitlements. +ZSignAsset* ZBundle::AssetForFolder(const string& strFolder) +{ + if (m_pSignAssets) { + for (auto it = m_pSignAssets->begin(); it != m_pSignAssets->end(); ++it) { + if (!it->m_strBundlePath.empty() && it->m_strBundlePath == strFolder) { + return &(*it); + } + } + } + return m_pDefaultAsset; +} + bool ZBundle::SignNode(jvalue& jvNode) { + m_pSignAsset = AssetForFolder(jvNode["path"].as_cstr()); + if (jvNode.has("files")) { for (size_t i = 0; i < jvNode["files"].size(); i++) { string strFile = jvNode["files"][i]; @@ -334,6 +351,9 @@ bool ZBundle::SignNode(jvalue& jvNode) } } + // children may have switched the asset + m_pSignAsset = AssetForFolder(jvNode["path"].as_cstr()); + jbase64 b64; string strInfoSHA1; string strInfoSHA256; @@ -411,17 +431,23 @@ bool ZBundle::SignNode(jvalue& jvNode) auto endsWith = [](const string& str, const string& suffix) { return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix); }; + ZSignAsset* pBound = m_pSignAsset; + bool bMatched = false; for (auto it = m_pSignAssets->rbegin(); it != m_pSignAssets->rend(); ++it) { m_pSignAsset = &(*it); - if (endsWith(m_pSignAsset->m_strApplicationId, strBundleId)) { + if (!m_pSignAsset->m_strProvData.empty() && endsWith(m_pSignAsset->m_strApplicationId, strBundleId)) { if (!ZFile::WriteFileV(m_pSignAsset->m_strProvData, "%s/%s/embedded.mobileprovision", m_strAppFolder.c_str(), strFolder.c_str())) { ZLog::ErrorV(">>> Can't write embedded.mobileprovision!\n"); return false; } bForceSign = true; + bMatched = true; break; } } + if (!bMatched) { + m_pSignAsset = pBound; + } } ZFile::CreateFolderV("%s/_CodeSignature", strBaseFolder.c_str()); @@ -791,6 +817,7 @@ bool ZBundle::SignFolder(ZSignAsset* pSignAsset, { m_bForceSign = bForce; m_pSignAsset = pSignAsset; + m_pDefaultAsset = pSignAsset; m_bWeakInject = bWeakInject; m_bRemoveProvision = bRemoveProvision; m_setRemoveDylibs.clear(); diff --git a/src/bundle.h b/src/bundle.h index ce16c79..c682a8d 100644 --- a/src/bundle.h +++ b/src/bundle.h @@ -47,6 +47,7 @@ class ZBundle private: bool FindAppFolder(const string& strFolder, string& strAppFolder); bool GetObjectsToSign(const string& strFolder, jvalue& jvInfo); + ZSignAsset* AssetForFolder(const string& strFolder); bool GetSignFolderInfo(const string& strFolder, jvalue& jvNode, bool bGetName = false); private: @@ -57,6 +58,7 @@ class ZBundle bool m_bWeakInject; bool m_bRemoveProvision; ZSignAsset* m_pSignAsset; + ZSignAsset* m_pDefaultAsset; list* m_pSignAssets; vector m_arrInjectDylibs; vector m_arrInjectDylibNames; diff --git a/src/lib/Zupersign.cpp b/src/lib/Zupersign.cpp index 7b941e1..ddab14c 100644 --- a/src/lib/Zupersign.cpp +++ b/src/lib/Zupersign.cpp @@ -4,6 +4,7 @@ #include "macho.h" #include "bundle.h" #include "signing.h" +#include static void print_ssl_err(char **exception, const char *reason) { unsigned long last_error = ERR_peek_last_error(); @@ -67,23 +68,50 @@ int zsign_sign( } } - ZSignAsset asset; - // TODO: Use diff ents for diff products - success = asset.Init( - cert, pkey, "", - string(static_cast(entitlements[0].data), entitlements[0].len), - is_adhoc, false, false - ); - if (!success) { - *exception = strdup("Invalid developer certificate/private key"); - EVP_PKEY_free(pkey); - X509_free(cert); - return -1; + // One asset per entitlements set. The one for the app itself (or the first one if none + // is bound to the app) is the default; the others apply to the nested bundle at their path. + string appRoot(app_directory); + while (appRoot.size() > 1 && appRoot.back() == '/') { + appRoot.pop_back(); + } + std::list assets; + size_t defaultIndex = 0; + for (size_t i = 0; i < num_entitlements; i++) { + assets.emplace_back(); + ZSignAsset &asset = assets.back(); + success = asset.Init( + cert, pkey, "", + string(static_cast(entitlements[i].data), entitlements[i].len), + is_adhoc, false, false + ); + if (!success) { + *exception = strdup("Invalid developer certificate/private key"); + EVP_PKEY_free(pkey); + X509_free(cert); + return -1; + } + + string path(entitlements[i].bundle_path ? entitlements[i].bundle_path : ""); + while (path.size() > 1 && path.back() == '/') { + path.pop_back(); + } + if (path == appRoot) { + asset.m_strBundlePath = "/"; + defaultIndex = i; + } else if (path.size() > appRoot.size() + 1 && path.compare(0, appRoot.size() + 1, appRoot + "/") == 0) { + asset.m_strBundlePath = path.substr(appRoot.size() + 1); + } + } + // the default asset must come first: SignFolder uses the front of the list + if (defaultIndex != 0) { + auto it = assets.begin(); + std::advance(it, defaultIndex); + assets.splice(assets.begin(), assets, it); } ZBundle bundle; success = bundle.SignFolder( - &asset, + &assets, app_directory, "", "", diff --git a/src/openssl.h b/src/openssl.h index 3996611..49258be 100644 --- a/src/openssl.h +++ b/src/openssl.h @@ -67,6 +67,7 @@ class ZSignAsset string m_strProvData; string m_strEntitleData; string m_strApplicationId; + string m_strBundlePath; private: void* m_evpPKey; From 6ad872768db8333daf99fb17f1ce54e7a24e5602 Mon Sep 17 00:00:00 2001 From: Ugo Mignon <56931733+TartanLeGrand@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:31:10 +0200 Subject: [PATCH 3/3] Keep file permissions when rewriting a Mach-O Making room for the signature replaces the executable with a new file, which dropped the executable bit. --- src/macho.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/macho.cpp b/src/macho.cpp index a55b3fa..ca73e38 100644 --- a/src/macho.cpp +++ b/src/macho.cpp @@ -4,6 +4,7 @@ #include "openssl.h" #include "signing.h" #include "macho.h" +#include ZMachO::ZMachO() { @@ -178,11 +179,18 @@ bool ZMachO::ReallocCodeSignSpace() } ZLog::Warn(">>> Success!\n"); + // the rewritten file replaces the original, so it must keep its permissions (the executable bit) + struct stat fileStat; + bool bHasMode = 0 == stat(m_strFile.c_str(), &fileStat); + if (1 == m_arrArchOes.size()) { CloseFile(); ZFile::RemoveFile(m_strFile.c_str()); string strNewArchOFile = m_strFile + ".archo.0"; if (0 == rename(strNewArchOFile.c_str(), m_strFile.c_str())) { +#ifndef _WIN32 + if (bHasMode) chmod(m_strFile.c_str(), fileStat.st_mode & 07777); +#endif return OpenFile(m_strFile.c_str()); } } else { //fat @@ -250,6 +258,9 @@ bool ZMachO::ReallocCodeSignSpace() ZFile::RemoveFile(m_strFile.c_str()); if (0 == rename(strNewFatMachOFile.c_str(), m_strFile.c_str())) { +#ifndef _WIN32 + if (bHasMode) chmod(m_strFile.c_str(), fileStat.st_mode & 07777); +#endif return OpenFile(m_strFile.c_str()); } }