#include "codedirectory.h"
#include "csutilities.h"
#include "CSCommonPriv.h"
using namespace UnixPlusPlus;
namespace Security {
namespace CodeSigning {
const char *CodeDirectory::canonicalSlotName(SpecialSlot slot)
{
switch (slot) {
case cdRequirementsSlot:
return kSecCS_REQUIREMENTSFILE;
case cdResourceDirSlot:
return kSecCS_RESOURCEDIRFILE;
case cdCodeDirectorySlot:
return kSecCS_CODEDIRECTORYFILE;
case cdSignatureSlot:
return kSecCS_SIGNATUREFILE;
case cdApplicationSlot:
return kSecCS_APPLICATIONFILE;
case cdEntitlementSlot:
return kSecCS_ENTITLEMENTFILE;
default:
return NULL;
}
}
unsigned CodeDirectory::slotAttributes(SpecialSlot slot)
{
switch (slot) {
case cdRequirementsSlot:
return cdComponentIsBlob; case cdCodeDirectorySlot:
return cdComponentPerArchitecture | cdComponentIsBlob;
case cdSignatureSlot:
return cdComponentPerArchitecture; case cdEntitlementSlot:
return cdComponentIsBlob; case cdIdentificationSlot:
return cdComponentPerArchitecture; default:
return 0; }
}
#if !defined(NDEBUG)
const char * const CodeDirectory::debugSlotName[] = {
"codedirectory",
"info",
"requirements",
"resources",
"application",
"entitlement"
};
#endif //NDEBUG
void CodeDirectory::checkIntegrity() const
{
if (!this->validateBlob())
MacOSError::throwMe(errSecCSSignatureInvalid); if (version > compatibilityLimit)
MacOSError::throwMe(errSecCSSignatureUnsupported); if (version < earliestVersion)
MacOSError::throwMe(errSecCSSignatureUnsupported); if (version > currentVersion)
secdebug("codedir", "%p version 0x%x newer than current 0x%x",
this, uint32_t(version), currentVersion);
if (!stringAt(identOffset))
MacOSError::throwMe(errSecCSSignatureFailed); if (!contains(hashOffset - hashSize * nSpecialSlots, hashSize * (nSpecialSlots + nCodeSlots)))
MacOSError::throwMe(errSecCSSignatureFailed); if (const Scatter *scatter = this->scatterVector()) {
unsigned int pagesConsumed = 0;
while (scatter->count) {
if (!contains(scatter, sizeof(Scatter)))
MacOSError::throwMe(errSecCSSignatureFailed);
pagesConsumed += scatter->count;
scatter++;
}
if (!contains(scatter, sizeof(Scatter))) MacOSError::throwMe(errSecCSSignatureFailed);
if (!contains((*this)[pagesConsumed-1], hashSize)) MacOSError::throwMe(errSecCSSignatureFailed);
}
}
bool CodeDirectory::validateSlot(const void *data, size_t length, Slot slot) const
{
secdebug("codedir", "%p validating slot %d", this, int(slot));
Hash::Byte digest[Hash::digestLength];
hash(data, length, digest);
return memcmp(digest, (*this)[slot], Hash::digestLength) == 0;
}
bool CodeDirectory::validateSlot(FileDesc fd, size_t length, Slot slot) const
{
Hash::Digest digest;
hash(fd, digest, length);
return memcmp(digest, (*this)[slot], Hash::digestLength) == 0;
}
bool CodeDirectory::slotIsPresent(Slot slot) const
{
if (slot >= -Slot(nSpecialSlots) && slot < Slot(nCodeSlots)) {
const Hash::Byte *digest = (*this)[slot];
for (unsigned n = 0; n < Hash::digestLength; n++)
if (digest[n])
return true; }
return false; }
size_t CodeDirectory::hash(FileDesc fd, Hash::Byte *digest, size_t limit)
{
SHA1 hasher;
size_t size = hashFileData(fd, hasher, limit);
hasher.finish(digest);
return size;
}
size_t CodeDirectory::hash(const void *data, size_t length, Hash::Byte *digest)
{
Hash hash;
hash(data, length);
hash.finish(digest);
return length;
}
} }
const SecCodeDirectoryFlagTable kSecCodeDirectoryFlagTable[] = {
{ "host", kSecCodeSignatureHost, true },
{ "adhoc", kSecCodeSignatureAdhoc, false },
{ "hard", kSecCodeSignatureForceHard, true },
{ "kill", kSecCodeSignatureForceKill, true },
{ "expires", kSecCodeSignatureForceExpiration, true },
{ NULL }
};