Skip to content
This repository was archived by the owner on Oct 30, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ZBundle::ZBundle()
{
m_pSignAssets = NULL;
m_pSignAsset = NULL;
m_pDefaultAsset = NULL;
m_bForceSign = false;
m_bWeakInject = false;
m_bRemoveProvision = false;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -57,6 +58,7 @@ class ZBundle
bool m_bWeakInject;
bool m_bRemoveProvision;
ZSignAsset* m_pSignAsset;
ZSignAsset* m_pDefaultAsset;
list<ZSignAsset>* m_pSignAssets;
vector<string> m_arrInjectDylibs;
vector<string> m_arrInjectDylibNames;
Expand Down
54 changes: 41 additions & 13 deletions src/lib/Zupersign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "macho.h"
#include "bundle.h"
#include "signing.h"
#include <list>

static void print_ssl_err(char **exception, const char *reason) {
unsigned long last_error = ERR_peek_last_error();
Expand Down Expand Up @@ -67,23 +68,50 @@ int zsign_sign(
}
}

ZSignAsset asset;
// TODO: Use diff ents for diff products
success = asset.Init(
cert, pkey, "",
string(static_cast<const char *>(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<ZSignAsset> 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<const char *>(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,
"",
"",
Expand Down
11 changes: 11 additions & 0 deletions src/macho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "openssl.h"
#include "signing.h"
#include "macho.h"
#include <sys/stat.h>

ZMachO::ZMachO()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ZSignAsset
string m_strProvData;
string m_strEntitleData;
string m_strApplicationId;
string m_strBundlePath;

private:
void* m_evpPKey;
Expand Down