#include "csgeneric.h"
#include "cs.h"
#include "StaticCode.h"
#include <securityd_client/cshosting.h>
#include <sys/param.h>
namespace Security {
namespace CodeSigning {
using MachPlusPlus::Port;
#define CALL(host, name, args...) \
OSStatus result; \
if (cshosting_client_ ## name (host, mig_get_reply_port(), &result, args)) \
MacOSError::throwMe(errSecCSNotAHost); \
MacOSError::check(result);
GenericCode::GenericCode(SecCode *host, SecGuestRef guestRef)
: SecCode(host), mGuestRef(guestRef)
{
}
SecCode *GenericCode::locateGuest(CFDictionaryRef attributes)
{
if (Port host = hostingPort()) {
CFRef<CFDataRef> attrData;
void *attrPtr = NULL; size_t attrLength = 0;
if (attributes) {
attrData.take(CFPropertyListCreateXMLData(NULL, attributes));
attrPtr = (void *)CFDataGetBytePtr(attrData);
attrLength = CFDataGetLength(attrData);
}
GuestChain guestPath;
mach_msg_type_number_t guestPathLength;
mach_port_t subport;
CALL(host, findGuest, guestRef(), attrPtr, attrLength,
&guestPath, &guestPathLength, &subport);
secdebug("genericcode", "%p found guest chain length=%d",
this, guestPathLength);
SecPointer<SecCode> code = this;
for (unsigned n = 0; n < guestPathLength; n++)
code = new GenericCode(code, guestPath[n]);
return code.yield();
} else
return NULL; }
SecStaticCode *GenericCode::mapGuestToStatic(SecCode *guest)
{
if (Port host = hostingPort()) {
char path[MAXPATHLEN];
CALL(host, guestPath, safe_cast<GenericCode *>(guest)->guestRef(), path);
return (new GenericStaticCode(DiskRep::bestGuess(path)))->retain();
} else
MacOSError::throwMe(errSecCSNotAHost);
}
uint32_t GenericCode::getGuestStatus(SecCode *guest)
{
if (Port host = hostingPort()) {
uint32_t status;
CALL(host, guestStatus, safe_cast<GenericCode *>(guest)->guestRef(), &status);
return status;
} else
MacOSError::throwMe(errSecCSNotAHost);
}
Port GenericCode::hostingPort()
{
if (!mHostingPort) {
if (staticCode()->codeDirectory()->flags & kSecCodeSignatureHost)
mHostingPort = getHostingPort();
}
return mHostingPort;
}
mach_port_t GenericCode::getHostingPort()
{
if (GenericCode *genericHost = dynamic_cast<GenericCode *>(host()))
return genericHost->getHostingPort();
else
MacOSError::throwMe(errSecCSNotAHost);
}
} }